IP Header

UDP 패킷을 만들었다고 전송할 수 있는 것이 아니었다. IP 헤더 부분을 추가해 주어야 한다고 한다. 직접 구현해 봐야 Pico에서 뭐를 수정해야 하는지 알 수 있을 것 같았다. 기본적인 것은 코파일럿이 해주었지만 관련 자료를 찾아 보았다.

IP 헤더와 관련된 것은 RFC 791이다. 구조는 아래와 같다.

코파일럿이 친절하게 거의 다 코드를 구현해 주었다.

ip_ver = 4
ip_ihl = 5
ip_tos = 0
ip_tot_len = 20 + udp_length
ip_id = 54321
ip_frag_off = 0
ip_ttl = 64
ip_proto = 17
ip_check = 0
ip_saddr = socket.inet_aton(src_addr)
ip_daddr = socket.inet_aton(dst_addr)
ip_ihl_ver = (ip_ver << 4) + ip_ihl
ip_header = struct.pack('!BBHHHBBH4s4s', ip_ihl_ver, ip_tos, ip_tot_len, ip_id, ip_frag_off, ip_ttl, ip_proto, ip_check, ip_saddr, ip_daddr)

checksum에 해당하는 값은 ip_check 이고 0으로 되어 있는 것을 볼 수 있다. 제대로 하고 싶다면 이것도 계산해 주어야 한다. UDP 패킷과 같은 구조로 되어 있다. 계산해 준 다음 다시 작성해 주면 된다.

ip_header_checksum = checksum(ip_header)

ip_header = struct.pack('!BBHHHBBH4s4s', ip_ihl_ver, ip_tos, ip_tot_len, ip_id, ip_frag_off, ip_ttl, ip_proto, ip_header_checksum, ip_saddr, ip_daddr)