diff options
Diffstat (limited to 'toxcore/network.h')
-rw-r--r-- | toxcore/network.h | 139 |
1 files changed, 116 insertions, 23 deletions
diff --git a/toxcore/network.h b/toxcore/network.h index 98307e5b..12a48868 100644 --- a/toxcore/network.h +++ b/toxcore/network.h | |||
@@ -39,17 +39,22 @@ | |||
39 | #include <windows.h> | 39 | #include <windows.h> |
40 | #include <ws2tcpip.h> | 40 | #include <ws2tcpip.h> |
41 | 41 | ||
42 | typedef unsigned int sock_t; | ||
43 | |||
42 | #else // Linux includes | 44 | #else // Linux includes |
43 | 45 | ||
44 | #include <fcntl.h> | 46 | #include <fcntl.h> |
45 | #include <sys/socket.h> | 47 | #include <sys/socket.h> |
46 | #include <netinet/in.h> | 48 | #include <netinet/in.h> |
49 | #include <arpa/inet.h> | ||
47 | #include <errno.h> | 50 | #include <errno.h> |
48 | #include <sys/time.h> | 51 | #include <sys/time.h> |
49 | #include <sys/types.h> | 52 | #include <sys/types.h> |
50 | #include <netdb.h> | 53 | #include <netdb.h> |
51 | #include <unistd.h> | 54 | #include <unistd.h> |
52 | 55 | ||
56 | typedef int sock_t; | ||
57 | |||
53 | #endif | 58 | #endif |
54 | 59 | ||
55 | #ifndef VANILLA_NACL | 60 | #ifndef VANILLA_NACL |
@@ -66,7 +71,8 @@ | |||
66 | #define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID. */ | 71 | #define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID. */ |
67 | #define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID. */ | 72 | #define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID. */ |
68 | #define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID. */ | 73 | #define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID. */ |
69 | #define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID. */ | 74 | #define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID for IPv4 addresses. */ |
75 | #define NET_PACKET_SEND_NODES_IPV6 4 /* Send nodes response packet ID for other addresses. */ | ||
70 | #define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID. */ | 76 | #define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID. */ |
71 | #define NET_PACKET_SYNC 17 /* SYNC packet ID. */ | 77 | #define NET_PACKET_SYNC 17 /* SYNC packet ID. */ |
72 | #define NET_PACKET_DATA 18 /* Data packet ID. */ | 78 | #define NET_PACKET_DATA 18 /* Data packet ID. */ |
@@ -74,6 +80,10 @@ | |||
74 | #define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */ | 80 | #define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */ |
75 | #define NET_PACKET_GROUP_CHATS 48 /* Group chats packet ID. */ | 81 | #define NET_PACKET_GROUP_CHATS 48 /* Group chats packet ID. */ |
76 | 82 | ||
83 | #define TOX_PORTRANGE_FROM 33445 | ||
84 | #define TOX_PORTRANGE_TO 33455 | ||
85 | #define TOX_PORT_DEFAULT TOX_PORTRANGE_FROM | ||
86 | |||
77 | /* Current time, unix format */ | 87 | /* Current time, unix format */ |
78 | #define unix_time() ((uint64_t)time(NULL)) | 88 | #define unix_time() ((uint64_t)time(NULL)) |
79 | 89 | ||
@@ -82,27 +92,118 @@ typedef union { | |||
82 | uint8_t uint8[4]; | 92 | uint8_t uint8[4]; |
83 | uint16_t uint16[2]; | 93 | uint16_t uint16[2]; |
84 | uint32_t uint32; | 94 | uint32_t uint32; |
85 | } IP; | 95 | struct in_addr in_addr; |
96 | } IP4; | ||
97 | |||
98 | typedef struct in6_addr IP6; | ||
99 | |||
100 | typedef struct { | ||
101 | sa_family_t family; | ||
102 | union { | ||
103 | IP4 ip4; | ||
104 | IP6 ip6; | ||
105 | }; | ||
106 | } IPAny; | ||
86 | 107 | ||
87 | typedef union { | 108 | typedef union { |
88 | struct { | 109 | struct { |
89 | IP ip; | 110 | IP4 ip; |
90 | uint16_t port; | 111 | uint16_t port; |
91 | /* Not used for anything right now. */ | 112 | /* Not used for anything right now. */ |
92 | uint16_t padding; | 113 | uint16_t padding; |
93 | }; | 114 | }; |
94 | uint8_t uint8[8]; | 115 | uint8_t uint8[8]; |
95 | } IP_Port; | 116 | } IP4_Port; |
96 | 117 | ||
118 | /* will replace IP_Port as soon as the complete infrastructure is in place | ||
119 | * removed the unused union and padding also */ | ||
97 | typedef struct { | 120 | typedef struct { |
98 | int16_t family; | 121 | IPAny ip; |
99 | uint16_t port; | 122 | uint16_t port; |
100 | IP ip; | 123 | } IPAny_Port; |
101 | uint8_t zeroes[8]; | 124 | |
102 | #ifdef ENABLE_IPV6 | 125 | #undef TOX_ENABLE_IPV6 |
103 | uint8_t zeroes2[12]; | 126 | /* #define TOX_ENABLE_IPV6 */ |
127 | #ifdef TOX_ENABLE_IPV6 | ||
128 | #define TOX_ENABLE_IPV6_DEFAULT 1 | ||
129 | typedef IPAny IP; | ||
130 | typedef IPAny_Port IP_Port; | ||
131 | #else | ||
132 | #define TOX_ENABLE_IPV6_DEFAULT 0 | ||
133 | typedef IP4 IP; | ||
134 | typedef IP4_Port IP_Port; | ||
104 | #endif | 135 | #endif |
105 | } ADDR; | 136 | |
137 | /* ip_ntoa | ||
138 | * converts ip into a string | ||
139 | * uses a static buffer, so mustn't used multiple times in the same output | ||
140 | */ | ||
141 | const char *ip_ntoa(IP *ip); | ||
142 | |||
143 | /* ip_equal | ||
144 | * compares two IPAny structures | ||
145 | * unset means unequal | ||
146 | * | ||
147 | * returns 0 when not equal or when uninitialized | ||
148 | */ | ||
149 | int ip_equal(IP *a, IP *b); | ||
150 | |||
151 | /* ipport_equal | ||
152 | * compares two IPAny_Port structures | ||
153 | * unset means unequal | ||
154 | * | ||
155 | * returns 0 when not equal or when uninitialized | ||
156 | */ | ||
157 | int ipport_equal(IP_Port *a, IP_Port *b); | ||
158 | |||
159 | /* nulls out ip */ | ||
160 | void ip_reset(IP *ip); | ||
161 | /* nulls out ip, sets family according to flag */ | ||
162 | void ip_init(IP *ip, uint8_t ipv6enabled); | ||
163 | /* checks if ip is valid */ | ||
164 | int ip_isset(IP *ip); | ||
165 | /* checks if ip is valid */ | ||
166 | int ipport_isset(IP_Port *ipport); | ||
167 | /* copies an ip structure */ | ||
168 | void ip_copy(IP *target, IP *source); | ||
169 | /* copies an ip_port structure */ | ||
170 | void ipport_copy(IP_Port *target, IP_Port *source); | ||
171 | |||
172 | /* | ||
173 | * addr_resolve(): | ||
174 | * uses getaddrinfo to resolve an address into an IP address | ||
175 | * uses the first IPv4/IPv6 addresses returned by getaddrinfo | ||
176 | * | ||
177 | * input | ||
178 | * address: a hostname (or something parseable to an IP address) | ||
179 | * to: to.family MUST be initialized, either set to a specific IP version | ||
180 | * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both | ||
181 | * IP versions are acceptable | ||
182 | * extra can be NULL and is only set in special circumstances, see returns | ||
183 | * | ||
184 | * returns in *to a valid IPAny (v4/v6), | ||
185 | * prefers v6 if ip.family was AF_UNSPEC and both available | ||
186 | * returns in *extra an IPv4 address, if family was AF_UNSPEC and *to is AF_INET6 | ||
187 | * returns 0 on failure | ||
188 | */ | ||
189 | int addr_resolve(const char *address, IP *to, IP *extra); | ||
190 | |||
191 | /* | ||
192 | * addr_resolve_or_parse_ip | ||
193 | * resolves string into an IP address | ||
194 | * | ||
195 | * address: a hostname (or something parseable to an IP address) | ||
196 | * to: to.family MUST be initialized, either set to a specific IP version | ||
197 | * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both | ||
198 | * IP versions are acceptable | ||
199 | * extra can be NULL and is only set in special circumstances, see returns | ||
200 | * | ||
201 | * returns in *tro a matching address (IPv6 or IPv4) | ||
202 | * returns in *extra, if not NULL, an IPv4 address, if to->family was AF_UNSPEC | ||
203 | * returns 1 on success | ||
204 | * returns 0 on failure | ||
205 | */ | ||
206 | int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra); | ||
106 | 207 | ||
107 | /* Function to receive data, ip and port of sender is put into ip_port. | 208 | /* Function to receive data, ip and port of sender is put into ip_port. |
108 | * Packet data is put into data. | 209 | * Packet data is put into data. |
@@ -117,13 +218,11 @@ typedef struct { | |||
117 | 218 | ||
118 | typedef struct { | 219 | typedef struct { |
119 | Packet_Handles packethandlers[256]; | 220 | Packet_Handles packethandlers[256]; |
120 | /* Our UDP socket. */ | ||
121 | #ifdef WIN32 | ||
122 | unsigned int sock; | ||
123 | #else | ||
124 | int sock; | ||
125 | #endif | ||
126 | 221 | ||
222 | /* Our UDP socket. */ | ||
223 | sa_family_t family; | ||
224 | uint16_t port; | ||
225 | sock_t sock; | ||
127 | } Networking_Core; | 226 | } Networking_Core; |
128 | 227 | ||
129 | /* return current time in milleseconds since the epoch. */ | 228 | /* return current time in milleseconds since the epoch. */ |
@@ -137,12 +236,7 @@ uint32_t random_int(void); | |||
137 | /* Basic network functions: */ | 236 | /* Basic network functions: */ |
138 | 237 | ||
139 | /* Function to send packet(data) of length length to ip_port. */ | 238 | /* Function to send packet(data) of length length to ip_port. */ |
140 | #ifdef WIN32 | 239 | int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t length); |
141 | int sendpacket(unsigned int sock, IP_Port ip_port, uint8_t *data, uint32_t length); | ||
142 | #else | ||
143 | int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length); | ||
144 | #endif | ||
145 | |||
146 | 240 | ||
147 | /* Function to call when packet beginning with byte is received. */ | 241 | /* Function to call when packet beginning with byte is received. */ |
148 | void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object); | 242 | void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object); |
@@ -163,5 +257,4 @@ Networking_Core *new_networking(IP ip, uint16_t port); | |||
163 | /* Function to cleanup networking stuff (doesn't do much right now). */ | 257 | /* Function to cleanup networking stuff (doesn't do much right now). */ |
164 | void kill_networking(Networking_Core *net); | 258 | void kill_networking(Networking_Core *net); |
165 | 259 | ||
166 | |||
167 | #endif | 260 | #endif |