#!/usr/bin/env python2
# coding: utf-8
"""
Solution from:
<name 1> (<matriculation 1>)
<name 2> (<matriculation 2>)
"""
import struct
from binascii import hexlify, unhexlify
def applyByteStuffing(flagbyte, escapebyte, payload):
"""
Returns the frame with byte stuffing applied on payload.
:param flagbyte: flag byte used for byte stuffing
:param escapebyte: byte used for escaping
:param payload: payload
return: string Frame with byte stuffing applied on payload
"""
#TODO
pass
def removeByteStuffing(flagbyte, escapebyte, frame):
"""
Removes byte stuffing from the frame.
:param flagbyte: flag byte used for byte stuffing
:param escapebyte: byte used for escaping
:param frame: Byte stuffed frame
return: string payload
"""
#TODO
pass
def MACToByteString(mac):
"""
Converts MAC address from the human-friendly format to a byte string
:param mac: human-readable MAC address
return: string byte string
"""
pass
return unhexlify(mac.replace(":", ""))
def byteStringToMAC(mac):
"""
Converts MAC address from a byte string to the human-friendly format
:param mac: MAC as byte string
return: string human-friendly MAC
"""
#TODO
pass
return ':'.join([(hexlify(mac))[i:i+2] for i in range(0, len(hexlify(mac)), 2)])
def parseEthernetFrame(frame):
"""
Parses an Ethernet II frame
You don't have to handle 802.1Q tags.
:param frame: ethernet frame as byte string
return: (str, str, int, int, str, int)
(human-readable MAC dst, human-readableMAC src, type, length, Payload, CRC)
"""
#TODO
frame = hexlify(frame)
dst = byteStringToMAC(unhexlify(frame[:-len(frame)+12]))
src = byteStringToMAC(unhexlify(frame[12:-len(frame)+24]))
type_ = int(frame[24:-len(frame)+28], 16)
length = (len(frame) - 36)/2
CRC = int(frame[28+length*2:],16)
Payload = unhexlify(frame[28:len(frame)-len(str(CRC))+1])
pass
return(dst, src, type_, length, Payload, CRC)
def ipv6ToString(ipv6):
"""
Converts ipv6 address byte string into human-readable representation.
You do not have to ommit the zeros
:param ip46: ipv6 address as byte string
return: str human-readable ipv6 address
"""
#TODO
pass
return ':'.join([(hexlify(ipv6))[i:i+4] for i in range(0, len(hexlify(ipv6)), 4)])
def parseIPv6Header(header):
"""
Parses an IPv6 header.
:param header: IPv6 header
return: (int, int, int, int, int, int, str, str)
(version, traffic class, flow label, payload length,
next header, hop limit, human-readable src address, human-readable dst address)
"""
#TODO
pass
def test_solution():
#assert applyByteStuffing("Z", "A", "JUMBO") == "ZJUMBOZ"
#assert applyByteStuffing("Z", "A", "TAZZA") == "ZTAAAZAZAAZ"
#assert removeByteStuffing("Z", "A", "ZJUMBOZ") == "JUMBO"
#assert removeByteStuffing("Z", "A", "ZTAAAZAZAAZ") == "TAZZA"
assert MACToByteString("18:cf:5e:7c:54:d5") == unhexlify('18cf5e7c54d5')
assert byteStringToMAC(unhexlify('18cf5e7c54d5')) == "18:cf:5e:7c:54:d5"
assert parseEthernetFrame(unhexlify('553e202b3f7b72d25345fa940800450000340001000040067cc17f0000017f000001003500160000000000000000500220003f9a000048656c6c6f20576f726c64211d01d43c')) == ('55:3e:20:2b:3f:7b', '72:d2:53:45:fa:94', 2048, 52, unhexlify('450000340001000040067cc17f0000017f000001003500160000000000000000500220003f9a000048656c6c6f20576f726c6421'), 486659132)
assert ipv6ToString(unhexlify('20010db8ac10fe010000000000000000')) == "2001:0db8:ac10:fe01:0000:0000:0000:0000"
#assert parseIPv6Header(unhexlify('6e00000000403afffe800000000000000214f2fffe070af0ff020000000000000000000000000001')) == (6, 224, 0, 64, 58, 255, 'fe80:0000:0000:0000:0214:f2ff:fe07:0af0', 'ff02:0000:0000:0000:0000:0000:0000:0001')
print "OK"
if __name__ == '__main__':
test_solution()