mirror of
https://github.com/barronwaffles/dwc_network_server_emulator.git
synced 2026-08-20 17:45:23 -05:00
[WIP] NATNEG: Added utils.get_local_addr
This commit is contained in:
@@ -97,14 +97,7 @@ def handle_natneg_init(nn, recv_data, addr, socket):
|
||||
# Try to connect to the server
|
||||
gameid = utils.get_string(recv_data, 0x15)
|
||||
client_id = "%02x" % ord(recv_data[13])
|
||||
|
||||
localip_raw = recv_data[15:19]
|
||||
localip_int_le = utils.get_ip(recv_data, 15)
|
||||
localip_int_be = utils.get_ip(recv_data, 15, True)
|
||||
localip = '.'.join(["%d" % ord(x) for x in localip_raw])
|
||||
localport_raw = recv_data[19:21]
|
||||
localport = utils.get_short(localport_raw, 0, True)
|
||||
localaddr = (localip, localport, localip_int_le, localip_int_be)
|
||||
localaddr = utils.get_local_addr(recv_data, 15)
|
||||
|
||||
nn.session_list \
|
||||
.setdefault(session_id, {}) \
|
||||
@@ -451,7 +444,7 @@ def handle_natneg_address_check(nn, recv_data, addr, socket):
|
||||
output += utils.get_bytes_from_short(addr[1], True)
|
||||
output += bytearray(recv_data[len(output):])
|
||||
|
||||
output[7] = 0x0b
|
||||
output[7] = 0x0b # NN_ADDRESS_REPLY
|
||||
nn.write_queue.put((output, addr, socket))
|
||||
|
||||
logger.log(logging.DEBUG, "Sent address check response to %s:%d...", *addr)
|
||||
@@ -481,7 +474,7 @@ def handle_natneg_address_reply(nn, recv_data, addr, socket):
|
||||
"""
|
||||
logger.log(logging.WARNING,
|
||||
"Received server record type command NN_ADDRESS_REPLY (0x0B)"
|
||||
" from %s:%s...", *addr)
|
||||
" from %s:%d...", *addr)
|
||||
logger.log(logging.DEBUG, "%s", utils.pretty_print_hex(output))
|
||||
|
||||
|
||||
@@ -755,8 +748,7 @@ class GameSpyNatNegUDPServer(SocketServer.UDPServer):
|
||||
self.write_queue = Queue.Queue()
|
||||
threading.Thread(target=self.write_queue_worker).start()
|
||||
|
||||
def write_queue_send(self, data, address, socket=None):
|
||||
socket = socket or self.socket
|
||||
def write_queue_send(self, data, address, socket):
|
||||
time.sleep(0.05)
|
||||
socket.sendto(data, address)
|
||||
|
||||
|
||||
@@ -165,6 +165,11 @@ def get_ip(data, idx, be=False):
|
||||
return ctypes.c_int32(get_int(data, idx, be)).value
|
||||
|
||||
|
||||
def get_ip_str(data, idx):
|
||||
"""Get IP string from bytes."""
|
||||
return '.'.join("%d" % x for x in bytearray(data[idx:idx+4]))
|
||||
|
||||
|
||||
def get_ip_from_str(ip_str, be=False):
|
||||
"""Get IP from string.
|
||||
|
||||
@@ -173,6 +178,15 @@ def get_ip_from_str(ip_str, be=False):
|
||||
return get_ip(bytearray([int(x) for x in ip_str.split('.')]), 0, be)
|
||||
|
||||
|
||||
def get_local_addr(data, idx):
|
||||
"""Get local address."""
|
||||
localip = get_ip_str(data, idx)
|
||||
localip_int_le = get_ip(data, idx)
|
||||
localip_int_be = get_ip(data, idx, True)
|
||||
localport = get_short(data, idx + 4, True)
|
||||
return (localip, localport, localip_int_le, localip_int_be)
|
||||
|
||||
|
||||
def get_string(data, idx):
|
||||
"""Get string from bytes."""
|
||||
data = data[idx:]
|
||||
|
||||
Reference in New Issue
Block a user