1
0
Fork 0

Fix bug, finally transmit packets properly

master
Alex Kotov 3 years ago
parent 68e654d006
commit d10212a5e3
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
  1. 9
      main.py

@ -8,6 +8,7 @@ import json
import socket
MAGIC = b'\x16\xd9\x6b\x52'
IP_V4_PROTO = b'\x08\x00'
class Config:
def __init__(self, data):
@ -119,7 +120,7 @@ def handle_iface_data(conn, tun_iface, config):
flags = buf[:2]
proto = buf[2:4]
if proto != b'\x08\x00':
if proto != IP_V4_PROTO:
return
ip_packet = IpPacket(IpHeader(buf[4:28]), buf[28:])
@ -161,18 +162,18 @@ def handle_ip_packet(ip_packet, conn, tun_iface, config):
)
ip_packet_packed = ip_packet.to_byte_string()
ip_packet_length = struct.pack('>I', len(ip_packet_packed))
if len(ip_packet_packed) != ip_packet.header.total_length:
raise RuntimeError('invalid "total length" header value')
buf = MAGIC + ip_packet_length + ip_packet_packed
if ip_packet.header.dst == config.iface_addr:
print('sending to tun iface')
buf = b'\x00\x00' + IP_V4_PROTO + ip_packet_packed
tun_iface.write(buf)
elif ip_packet.header.dst == config.iface_dstaddr:
print('sending to remote peer')
ip_packet_length = struct.pack('>I', len(ip_packet_packed))
buf = MAGIC + ip_packet_length + ip_packet_packed
conn.setblocking(1)
conn.sendall(buf)
else: