diff options
author | irungentoo <irungentoo@gmail.com> | 2013-06-26 09:56:15 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-06-26 09:56:15 -0400 |
commit | c7f7e30c7521bcdafbddd9d21af288442c325a72 (patch) | |
tree | aba9e6ac90b0b418001bae57d22f7207c05dcce8 | |
parent | f7574c61fc935f514da4afd4994f86d8062efb38 (diff) |
Moved the network functions from the DHT into network.
Also made a nice function to init networking.
-rw-r--r-- | CMakeLists.txt | 3 | ||||
-rw-r--r-- | core/DHT.c | 42 | ||||
-rw-r--r-- | core/DHT.h | 74 | ||||
-rw-r--r-- | core/network.c | 74 | ||||
-rw-r--r-- | core/network.h | 79 | ||||
-rw-r--r-- | testing/DHT_test.c | 28 |
6 files changed, 178 insertions, 122 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index c1fa8cad..388e6d91 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
@@ -2,7 +2,8 @@ cmake_minimum_required(VERSION 2.6.0) | |||
2 | project(TOX C) | 2 | project(TOX C) |
3 | 3 | ||
4 | set(core_sources | 4 | set(core_sources |
5 | core/DHT.c) | 5 | core/DHT.c |
6 | core/network.c) | ||
6 | 7 | ||
7 | set(test_sources | 8 | set(test_sources |
8 | testing/DHT_test.c) | 9 | testing/DHT_test.c) |
@@ -1,39 +1,13 @@ | |||
1 | #include "DHT.h" | 1 | /* DHT.c |
2 | 2 | * | |
3 | 3 | * An implementation of the DHT as seen in docs/DHT.txt | |
4 | //Basic network functions: | 4 | * |
5 | //TODO: put them somewhere else than here | 5 | */ |
6 | |||
7 | //Function to send packet(data) of length length to ip_port | ||
8 | int sendpacket(IP_Port ip_port, char * data, uint32_t length) | ||
9 | { | ||
10 | ADDR addr = {AF_INET, ip_port.port, ip_port.ip}; | ||
11 | return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); | ||
12 | |||
13 | } | ||
14 | 6 | ||
15 | //Function to recieve data, ip and port of sender is put into ip_port | ||
16 | //the packet data into data | ||
17 | //the packet length into length. | ||
18 | //dump all empty packets. | ||
19 | int recievepacket(IP_Port * ip_port, char * data, uint32_t * length) | ||
20 | { | ||
21 | ADDR addr; | ||
22 | uint32_t addrlen = sizeof(addr); | ||
23 | (*(int *)length) = recvfrom(sock, data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); | ||
24 | if(*(int *)length <= 0) | ||
25 | { | ||
26 | //nothing recieved | ||
27 | //or empty packet | ||
28 | return -1; | ||
29 | } | ||
30 | ip_port->ip = addr.ip; | ||
31 | ip_port->port = addr.port; | ||
32 | return 0; | ||
33 | |||
34 | } | ||
35 | 7 | ||
8 | #include "DHT.h" | ||
36 | 9 | ||
10 | char self_client_id[CLIENT_ID_SIZE]; | ||
37 | 11 | ||
38 | //Compares client_id1 and client_id2 with client_id | 12 | //Compares client_id1 and client_id2 with client_id |
39 | //return 0 if both are same distance | 13 | //return 0 if both are same distance |
@@ -744,7 +718,7 @@ void doFriends() | |||
744 | static uint32_t close_lastgetnodes; | 718 | static uint32_t close_lastgetnodes; |
745 | 719 | ||
746 | //Ping each client in the close nodes list every 60 seconds. | 720 | //Ping each client in the close nodes list every 60 seconds. |
747 | //Send a get nodes request every 20 seconds to a random good node int the list. | 721 | //Send a get nodes request every 20 seconds to a random good node in the list. |
748 | void doClose()//tested | 722 | void doClose()//tested |
749 | { | 723 | { |
750 | uint32_t i; | 724 | uint32_t i; |
@@ -1,25 +1,14 @@ | |||
1 | #ifndef DHT_H | 1 | /* DHT.h |
2 | #define DHT_H | 2 | * |
3 | 3 | * An implementation of the DHT as seen in docs/DHT.txt | |
4 | #include <stdlib.h> | 4 | * |
5 | #include <stdio.h> | 5 | */ |
6 | #include <stdint.h> | ||
7 | #include <string.h> | ||
8 | #include <time.h> | ||
9 | 6 | ||
10 | #ifdef WIN32 //Put win32 includes here | ||
11 | 7 | ||
12 | #include <winsock2.h> | 8 | #ifndef DHT_H |
13 | #include <windows.h> | 9 | #define DHT_H |
14 | |||
15 | #else //Linux includes | ||
16 | |||
17 | #include <fcntl.h> | ||
18 | #include <sys/socket.h> | ||
19 | #include <netinet/in.h> | ||
20 | #include <errno.h> | ||
21 | 10 | ||
22 | #endif | 11 | #include "network.h" |
23 | 12 | ||
24 | //Current time, unix format | 13 | //Current time, unix format |
25 | #define unix_time() ((uint32_t)time(NULL)) | 14 | #define unix_time() ((uint32_t)time(NULL)) |
@@ -27,24 +16,6 @@ | |||
27 | //size of the client_id in bytes | 16 | //size of the client_id in bytes |
28 | #define CLIENT_ID_SIZE 32 | 17 | #define CLIENT_ID_SIZE 32 |
29 | 18 | ||
30 | #define MAX_UDP_PACKET_SIZE 65507 | ||
31 | |||
32 | typedef union | ||
33 | { | ||
34 | uint8_t c[4]; | ||
35 | uint16_t s[2]; | ||
36 | uint32_t i; | ||
37 | }IP; | ||
38 | |||
39 | typedef struct | ||
40 | { | ||
41 | IP ip; | ||
42 | uint16_t port; | ||
43 | //not used for anything right now | ||
44 | uint16_t padding; | ||
45 | }IP_Port; | ||
46 | |||
47 | |||
48 | typedef struct | 19 | typedef struct |
49 | { | 20 | { |
50 | char client_id[CLIENT_ID_SIZE]; | 21 | char client_id[CLIENT_ID_SIZE]; |
@@ -77,19 +48,6 @@ typedef struct | |||
77 | }Pinged; | 48 | }Pinged; |
78 | 49 | ||
79 | 50 | ||
80 | typedef struct | ||
81 | { | ||
82 | int16_t family; | ||
83 | uint16_t port; | ||
84 | IP ip; | ||
85 | uint8_t zeroes[8]; | ||
86 | #ifdef ENABLE_IPV6 | ||
87 | uint8_t zeroes2[12]; | ||
88 | #endif | ||
89 | }ADDR; | ||
90 | |||
91 | |||
92 | |||
93 | //Add a new friend to the friends list | 51 | //Add a new friend to the friends list |
94 | //client_id must be CLIENT_ID_SIZE bytes long. | 52 | //client_id must be CLIENT_ID_SIZE bytes long. |
95 | //returns 0 if success | 53 | //returns 0 if success |
@@ -133,11 +91,8 @@ void bootstrap(IP_Port ip_port); | |||
133 | //Global variables | 91 | //Global variables |
134 | 92 | ||
135 | //Our client id | 93 | //Our client id |
136 | char self_client_id[CLIENT_ID_SIZE]; | 94 | extern char self_client_id[CLIENT_ID_SIZE]; |
137 | 95 | ||
138 | //Our UDP socket. | ||
139 | //We only use one so it's much easier to have it as a global variable | ||
140 | int sock; | ||
141 | 96 | ||
142 | //TODO: Move these out of here and put them into the .c file. | 97 | //TODO: Move these out of here and put them into the .c file. |
143 | //A list of the clients mathematically closest to ours. | 98 | //A list of the clients mathematically closest to ours. |
@@ -162,15 +117,4 @@ Pinged pings[LPING_ARRAY]; | |||
162 | Pinged send_nodes[LSEND_NODES_ARRAY]; | 117 | Pinged send_nodes[LSEND_NODES_ARRAY]; |
163 | 118 | ||
164 | 119 | ||
165 | //Basic network functions: | ||
166 | //TODO: put them somewhere else than here | ||
167 | |||
168 | //Function to send packet(data) of length length to ip_port | ||
169 | int sendpacket(IP_Port ip_port, char * data, uint32_t length); | ||
170 | |||
171 | //Function to recieve data, ip and port of sender is put into ip_port | ||
172 | //the packet data into data | ||
173 | //the packet length into length. | ||
174 | int recievepacket(IP_Port * ip_port, char * data, uint32_t * length); | ||
175 | |||
176 | #endif \ No newline at end of file | 120 | #endif \ No newline at end of file |
diff --git a/core/network.c b/core/network.c new file mode 100644 index 00000000..10a37b80 --- /dev/null +++ b/core/network.c | |||
@@ -0,0 +1,74 @@ | |||
1 | |||
2 | |||
3 | #include "network.h" | ||
4 | |||
5 | //our UDP socket, a global variable. | ||
6 | static int sock; | ||
7 | |||
8 | //Basic network functions: | ||
9 | //TODO: put them somewhere else than here | ||
10 | |||
11 | //Function to send packet(data) of length length to ip_port | ||
12 | int sendpacket(IP_Port ip_port, char * data, uint32_t length) | ||
13 | { | ||
14 | ADDR addr = {AF_INET, ip_port.port, ip_port.ip}; | ||
15 | return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); | ||
16 | |||
17 | } | ||
18 | |||
19 | //Function to recieve data, ip and port of sender is put into ip_port | ||
20 | //the packet data into data | ||
21 | //the packet length into length. | ||
22 | //dump all empty packets. | ||
23 | int recievepacket(IP_Port * ip_port, char * data, uint32_t * length) | ||
24 | { | ||
25 | ADDR addr; | ||
26 | uint32_t addrlen = sizeof(addr); | ||
27 | (*(int *)length) = recvfrom(sock, data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); | ||
28 | if(*(int *)length <= 0) | ||
29 | { | ||
30 | //nothing recieved | ||
31 | //or empty packet | ||
32 | return -1; | ||
33 | } | ||
34 | ip_port->ip = addr.ip; | ||
35 | ip_port->port = addr.port; | ||
36 | return 0; | ||
37 | |||
38 | } | ||
39 | |||
40 | //initialize networking | ||
41 | //bind to ip and port | ||
42 | //ip must be in network order EX: 127.0.0.1 = (7F000001) | ||
43 | //port is in host byte order (this means don't worry about it) | ||
44 | //returns 0 if no problems | ||
45 | //TODO: add something to check if there are errors | ||
46 | int init_networking(IP ip ,uint16_t port) | ||
47 | { | ||
48 | #ifdef WIN32 | ||
49 | WSADATA wsaData; | ||
50 | if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) | ||
51 | { | ||
52 | return -1; | ||
53 | } | ||
54 | #endif | ||
55 | |||
56 | //initialize our socket | ||
57 | sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | ||
58 | |||
59 | //Set socket nonblocking | ||
60 | #ifdef WIN32 | ||
61 | //I think this works for windows | ||
62 | u_long mode = 1; | ||
63 | //ioctl(sock, FIONBIO, &mode); | ||
64 | ioctlsocket(sock, FIONBIO, &mode); | ||
65 | #else | ||
66 | fcntl(sock, F_SETFL, O_NONBLOCK, 1); | ||
67 | #endif | ||
68 | |||
69 | //Bind our socket to port PORT and address 0.0.0.0 | ||
70 | ADDR addr = {AF_INET, htons(port), ip}; | ||
71 | bind(sock, (struct sockaddr*)&addr, sizeof(addr)); | ||
72 | return 0; | ||
73 | |||
74 | } \ No newline at end of file | ||
diff --git a/core/network.h b/core/network.h new file mode 100644 index 00000000..17b29f2c --- /dev/null +++ b/core/network.h | |||
@@ -0,0 +1,79 @@ | |||
1 | /* network.h | ||
2 | * | ||
3 | * Datatypes, functions and includes for the core networking. | ||
4 | * | ||
5 | */ | ||
6 | |||
7 | |||
8 | #ifndef NETWORK_H | ||
9 | #define NETWORK_H | ||
10 | |||
11 | #include <stdlib.h> | ||
12 | #include <stdio.h> | ||
13 | #include <stdint.h> | ||
14 | #include <string.h> | ||
15 | #include <time.h> | ||
16 | |||
17 | #ifdef WIN32 //Put win32 includes here | ||
18 | |||
19 | #include <winsock2.h> | ||
20 | #include <windows.h> | ||
21 | |||
22 | #else //Linux includes | ||
23 | |||
24 | #include <fcntl.h> | ||
25 | #include <sys/socket.h> | ||
26 | #include <netinet/in.h> | ||
27 | #include <errno.h> | ||
28 | |||
29 | #endif | ||
30 | |||
31 | #define MAX_UDP_PACKET_SIZE 65507 | ||
32 | |||
33 | typedef union | ||
34 | { | ||
35 | uint8_t c[4]; | ||
36 | uint16_t s[2]; | ||
37 | uint32_t i; | ||
38 | }IP; | ||
39 | |||
40 | typedef struct | ||
41 | { | ||
42 | IP ip; | ||
43 | uint16_t port; | ||
44 | //not used for anything right now | ||
45 | uint16_t padding; | ||
46 | }IP_Port; | ||
47 | |||
48 | typedef struct | ||
49 | { | ||
50 | int16_t family; | ||
51 | uint16_t port; | ||
52 | IP ip; | ||
53 | uint8_t zeroes[8]; | ||
54 | #ifdef ENABLE_IPV6 | ||
55 | uint8_t zeroes2[12]; | ||
56 | #endif | ||
57 | }ADDR; | ||
58 | |||
59 | |||
60 | |||
61 | |||
62 | //Basic network functions: | ||
63 | |||
64 | //Function to send packet(data) of length length to ip_port | ||
65 | int sendpacket(IP_Port ip_port, char * data, uint32_t length); | ||
66 | |||
67 | //Function to recieve data, ip and port of sender is put into ip_port | ||
68 | //the packet data into data | ||
69 | //the packet length into length. | ||
70 | int recievepacket(IP_Port * ip_port, char * data, uint32_t * length); | ||
71 | |||
72 | //initialize networking | ||
73 | //bind to ip and port | ||
74 | //ip must be in network order EX: 127.0.0.1 = (7F000001) | ||
75 | //port is in host byte order (this means don't worry about it) | ||
76 | //returns 0 if no problems | ||
77 | //TODO: add something to check if there are errors | ||
78 | int init_networking(IP ip ,uint16_t port); | ||
79 | #endif \ No newline at end of file | ||
diff --git a/testing/DHT_test.c b/testing/DHT_test.c index 53712e6e..03a49a3e 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c | |||
@@ -87,35 +87,19 @@ int main(int argc, char *argv[]) | |||
87 | memcpy(self_client_id, &randdomnum, 4); | 87 | memcpy(self_client_id, &randdomnum, 4); |
88 | //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); | 88 | //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); |
89 | 89 | ||
90 | #ifdef WIN32 | ||
91 | WSADATA wsaData; | ||
92 | if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) | ||
93 | { | ||
94 | return -1; | ||
95 | } | ||
96 | #endif | ||
97 | |||
98 | if (argc < 4) { | 90 | if (argc < 4) { |
99 | printf("usage %s ip port client_id(of friend to find ip_port of)\n", argv[0]); | 91 | printf("usage %s ip port client_id(of friend to find ip_port of)\n", argv[0]); |
100 | exit(0); | 92 | exit(0); |
101 | } | 93 | } |
102 | addfriend(argv[3]); | 94 | addfriend(argv[3]); |
103 | 95 | ||
104 | //initialize our socket | ||
105 | sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | ||
106 | //Set socket nonblocking | ||
107 | #ifdef WIN32 | ||
108 | //I think this works for windows | ||
109 | u_long mode = 1; | ||
110 | //ioctl(sock, FIONBIO, &mode); | ||
111 | ioctlsocket(sock, FIONBIO, &mode); | ||
112 | #else | ||
113 | fcntl(sock, F_SETFL, O_NONBLOCK, 1); | ||
114 | #endif | ||
115 | 96 | ||
116 | //Bind our socket to port PORT and address 0.0.0.0 | 97 | //initialize networking |
117 | ADDR addr = {AF_INET, htons(PORT), {{0}}}; | 98 | //bind to ip 0.0.0.0:PORT |
118 | bind(sock, (struct sockaddr*)&addr, sizeof(addr)); | 99 | IP ip; |
100 | ip.i = 0; | ||
101 | init_networking(ip, PORT); | ||
102 | |||
119 | perror("Initialization"); | 103 | perror("Initialization"); |
120 | IP_Port bootstrap_ip_port; | 104 | IP_Port bootstrap_ip_port; |
121 | bootstrap_ip_port.port = htons(atoi(argv[2])); | 105 | bootstrap_ip_port.port = htons(atoi(argv[2])); |