From 4aeded2d0eb735c1b8c7529c7ca9edf924208769 Mon Sep 17 00:00:00 2001 From: Astonex Date: Tue, 23 Jul 2013 16:17:33 +0100 Subject: [PATCH 1/5] Disregard --- testing/rect.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/testing/rect.py b/testing/rect.py index 59d6c158..2f9c9256 100644 --- a/testing/rect.py +++ b/testing/rect.py @@ -2,6 +2,7 @@ #for testing only import socket import random +import code UDP_IP = "127.0.0.1" UDP_PORT = 5004 @@ -13,7 +14,7 @@ sock.bind((UDP_IP, UDP_PORT)) #our client_id client_id = str(''.join(random.choice("abcdefghijklmnopqrstuvwxyz") for x in range(32))) -print client_id +print (client_id) a = 1; #send ping request to our DHT on localhost. sock.sendto("0012345678".decode("hex") + client_id, ('127.0.0.1', 33445)) @@ -21,25 +22,25 @@ sock.sendto("0012345678".decode("hex") + client_id, ('127.0.0.1', 33445)) #print all packets recieved and respond to ping requests properly while True: data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes - print "received message:", data.encode('hex'), " From:", addr + print ("received message:", data.encode('hex'), " From:", addr) #if we recieve a ping request. - print data[0].encode('hex') + print (data[0].encode('hex')) if data[0] == "00".decode('hex'): - print "Sending ping resp" + print ("Sending ping resp") sock.sendto("01".decode('hex') + data[1:5] + client_id, addr) #if we recieve a get_nodes request. if data[0] == "02".decode('hex'): - print "Sending getn resp" + print ("Sending getn resp") #send send nodes packet with a couple 127.0.0.1 ips and ports. #127.0.0.1:5000, 127.0.0.1:5001, 127.0.0.1:5002 sock.sendto("03".decode('hex') + data[1:5] + client_id + ("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH" + "7F00000113880000".decode('hex') + "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH" + "7F00000113890000".decode('hex') + "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH" + "7F000001138A0000".decode('hex')), addr) if data[0] == "10".decode('hex'): - print "Sending handshake resp" + print ("Sending handshake resp") sock.sendto("10".decode('hex') + data[1:5] + client_id[:4], addr) if data[0] == "11".decode('hex'): - print "Sending SYNC resp" + print ("Sending SYNC resp") a+=1 sock.sendto("11".decode('hex') + chr(a) + data[1:9], addr) - + From 29aa702a4f0cc50c091e0e9624d5ee897138ddf3 Mon Sep 17 00:00:00 2001 From: Astonex Date: Wed, 24 Jul 2013 00:32:50 +0100 Subject: [PATCH 2/5] Added simple init_networking() error checking --- core/Messenger.c | 8 ++++++-- core/Messenger.h | 6 ++++-- core/network.c | 8 +++++--- core/network.h | 3 +-- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/core/Messenger.c b/core/Messenger.c index 1330e8ed..043700e5 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -393,14 +393,18 @@ void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t)) #define PORT 33445 /* run this at startup */ -void initMessenger() +int initMessenger() { new_keys(); m_set_userstatus((uint8_t*)"Online", sizeof("Online")); initNetCrypto(); IP ip; ip.i = 0; - init_networking(ip, PORT); + + if(init_networking(ip,PORT) == -1) + return -1; + + return 0; } static void doFriends() diff --git a/core/Messenger.h b/core/Messenger.h index 2484692c..6afe84ac 100644 --- a/core/Messenger.h +++ b/core/Messenger.h @@ -132,8 +132,10 @@ void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t)); you are not responsible for freeing newstatus */ void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t)); -/* run this at startup */ -void initMessenger(); +/* run this at startup + returns 0 if no connection problems + returns -1 if there are problems */ +int initMessenger(); /* the main loop that needs to be run at least 200 times per second */ diff --git a/core/network.c b/core/network.c index 6337651a..fa6c99fc 100644 --- a/core/network.c +++ b/core/network.c @@ -101,8 +101,8 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length) bind to ip and port ip must be in network order EX: 127.0.0.1 = (7F000001) port is in host byte order (this means don't worry about it) - returns 0 if no problems - TODO: add something to check if there are errors */ + returns 0 if no problems + returns -1 if there are problems */ int init_networking(IP ip ,uint16_t port) { #ifdef WIN32 @@ -146,7 +146,9 @@ int init_networking(IP ip ,uint16_t port) /* Bind our socket to port PORT and address 0.0.0.0 */ ADDR addr = {AF_INET, htons(port), ip}; - bind(sock, (struct sockaddr*)&addr, sizeof(addr)); + if(bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) + return -1; + return 0; } diff --git a/core/network.h b/core/network.h index 6f140d0c..62169d88 100644 --- a/core/network.h +++ b/core/network.h @@ -121,10 +121,9 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length); ip must be in network order EX: 127.0.0.1 = (7F000001) port is in host byte order (this means don't worry about it) returns 0 if no problems - TODO: add something to check if there are errors */ + returns -1 if there were problems */ int init_networking(IP ip ,uint16_t port); - /* function to cleanup networking stuff(doesn't do much right now) */ void shutdown_networking(); From 4398763097f87483f5efa56c4214a3f3597ed3eb Mon Sep 17 00:00:00 2001 From: Astonex Date: Wed, 24 Jul 2013 00:39:53 +0100 Subject: [PATCH 3/5] Update rect.py --- testing/rect.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/testing/rect.py b/testing/rect.py index 2f9c9256..59d6c158 100644 --- a/testing/rect.py +++ b/testing/rect.py @@ -2,7 +2,6 @@ #for testing only import socket import random -import code UDP_IP = "127.0.0.1" UDP_PORT = 5004 @@ -14,7 +13,7 @@ sock.bind((UDP_IP, UDP_PORT)) #our client_id client_id = str(''.join(random.choice("abcdefghijklmnopqrstuvwxyz") for x in range(32))) -print (client_id) +print client_id a = 1; #send ping request to our DHT on localhost. sock.sendto("0012345678".decode("hex") + client_id, ('127.0.0.1', 33445)) @@ -22,25 +21,25 @@ sock.sendto("0012345678".decode("hex") + client_id, ('127.0.0.1', 33445)) #print all packets recieved and respond to ping requests properly while True: data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes - print ("received message:", data.encode('hex'), " From:", addr) + print "received message:", data.encode('hex'), " From:", addr #if we recieve a ping request. - print (data[0].encode('hex')) + print data[0].encode('hex') if data[0] == "00".decode('hex'): - print ("Sending ping resp") + print "Sending ping resp" sock.sendto("01".decode('hex') + data[1:5] + client_id, addr) #if we recieve a get_nodes request. if data[0] == "02".decode('hex'): - print ("Sending getn resp") + print "Sending getn resp" #send send nodes packet with a couple 127.0.0.1 ips and ports. #127.0.0.1:5000, 127.0.0.1:5001, 127.0.0.1:5002 sock.sendto("03".decode('hex') + data[1:5] + client_id + ("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH" + "7F00000113880000".decode('hex') + "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH" + "7F00000113890000".decode('hex') + "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH" + "7F000001138A0000".decode('hex')), addr) if data[0] == "10".decode('hex'): - print ("Sending handshake resp") + print "Sending handshake resp" sock.sendto("10".decode('hex') + data[1:5] + client_id[:4], addr) if data[0] == "11".decode('hex'): - print ("Sending SYNC resp") + print "Sending SYNC resp" a+=1 sock.sendto("11".decode('hex') + chr(a) + data[1:9], addr) - + From 0af0c274d345020c8002c42b4dddb854dc78073e Mon Sep 17 00:00:00 2001 From: Astonex Date: Wed, 24 Jul 2013 01:22:45 +0100 Subject: [PATCH 4/5] Changed bind() == -1 check to INVALID_SOCK check --- core/network.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/core/network.c b/core/network.c index fa6c99fc..8c8d8875 100644 --- a/core/network.c +++ b/core/network.c @@ -119,6 +119,10 @@ int init_networking(IP ip ,uint16_t port) /* initialize our socket */ sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + + /* Check for socket error */ + if (sock == INVALID_SOCKET) + return -1; /* Functions to increase the size of the send and receive UDP buffers NOTE: uncomment if necessary */ @@ -146,8 +150,7 @@ int init_networking(IP ip ,uint16_t port) /* Bind our socket to port PORT and address 0.0.0.0 */ ADDR addr = {AF_INET, htons(port), ip}; - if(bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) - return -1; + bind(sock, (struct sockaddr*)&addr, sizeof(addr)); return 0; From 92398844a05bf5f0ce4333e0d4bd03c6e6633f77 Mon Sep 17 00:00:00 2001 From: Astonex Date: Wed, 24 Jul 2013 01:33:08 +0100 Subject: [PATCH 5/5] Changed the error checking to not just be Windows only --- core/network.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/core/network.c b/core/network.c index 8c8d8875..bc6738ac 100644 --- a/core/network.c +++ b/core/network.c @@ -121,9 +121,15 @@ int init_networking(IP ip ,uint16_t port) sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); /* Check for socket error */ - if (sock == INVALID_SOCKET) + #ifdef WIN32 + if (sock == INVALID_SOCKET) //MSDN recommends this return -1; - + #else + if (sock < 0) + return -1; + #endif + + /* Functions to increase the size of the send and receive UDP buffers NOTE: uncomment if necessary */ /*