diff --git a/README.md b/README.md index 86b71e3..115bf0f 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,10 @@ -# template +# 6to4_converter ## Description +Simple script to convert IPv4 addresses to 6to4 addresses. See RFC 3056 for details. ## How to install +- Clone the repository. ## How to use +- Run `python3 app/main.py --help` for information on how to use this script. diff --git a/requirements.txt b/app/__init__.py similarity index 100% rename from requirements.txt rename to app/__init__.py diff --git a/app/main.py b/app/main.py index a6053f8..3bcbf51 100644 --- a/app/main.py +++ b/app/main.py @@ -1,8 +1,54 @@ -#!/usr/local/bin/python3 +#!/usr/bin/env python3 -def main(): - # Commands here - +"""Get 6to4 address from IPv4 address.""" -if __name__ == "__main__": - main() +import argparse + + +def parse_args(): # pragma: no cover + parser = argparse.ArgumentParser(description='Get 6to4 address from IPv4 address.') + parser.add_argument('-a', '--address', dest='address', action='store', help='IPv4 address', required=True) + args = parser.parse_args() + return args + + +def ipv4_to_ipv6(ipv4): + ipv4Hex = '' + ipv6Hextet1 = '2002' + ipv6Hextet2 = '' + ipv6Hextet3 = '' + + # Split input to octets + ipv4 = ipv4.split('.',4) + + # Convert IPv4 address to hex string + for octet in ipv4: + if len(hex(int(octet))[2:])>1: + ipv4Hex += hex(int(octet))[2:] + else: + ipv4Hex += '0' + ipv4Hex += hex(int(octet))[2:] + + # Split into hextets + for i in range(4): + ipv6Hextet2 += (ipv4Hex[i]) + for i in range(4,8): + ipv6Hextet3 += (ipv4Hex[i]) + + # Convert to dec and back to remove leading zeros + ipv6Hextet2 = hex(int(ipv6Hextet2,16))[2:] + ipv6Hextet3 = hex(int(ipv6Hextet3,16))[2:] + + # Form 6to4 address + output_6to4 = f"{ipv6Hextet1}:{ipv6Hextet2}:{ipv6Hextet3}::/128" + return output_6to4 + + +def main(): # pragma: no cover + ipv4 = parse_args().address + output = ipv4_to_ipv6(ipv4) + print(output) + + +if __name__ == '__main__': + main() # pragma: no cover diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_main.py b/tests/test_main.py index e69de29..8f4f589 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python3 + +"""Test cases for main.py.""" + +from app.main import ipv4_to_ipv6 + +def test_ipv4_to_ipv6(): + result = ipv4_to_ipv6('192.168.0.1') + assert result == "2002:c0a8:1::/128"