1
0
Fork 0

Fix bug, finally transmit packets properly

This commit is contained in:
Alex Kotov 2020-04-17 22:10:22 +05:00
parent 68e654d006
commit d10212a5e3
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
1 changed files with 5 additions and 4 deletions

View File

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