summaryrefslogtreecommitdiff
path: root/toxcore/network.h
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/network.h')
-rw-r--r--toxcore/network.h54
1 files changed, 29 insertions, 25 deletions
diff --git a/toxcore/network.h b/toxcore/network.h
index 1f4b757c..15e1c0a4 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -92,12 +92,11 @@ typedef int sock_t;
92#endif 92#endif
93#endif 93#endif
94 94
95#define MAX_UDP_PACKET_SIZE 65507 95#define MAX_UDP_PACKET_SIZE 2048
96 96
97#define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID. */ 97#define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID. */
98#define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID. */ 98#define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID. */
99#define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID. */ 99#define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID. */
100#define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID for IPv4 addresses. */
101#define NET_PACKET_SEND_NODES_IPV6 4 /* Send nodes response packet ID for other addresses. */ 100#define NET_PACKET_SEND_NODES_IPV6 4 /* Send nodes response packet ID for other addresses. */
102#define NET_PACKET_COOKIE_REQUEST 24 /* Cookie request packet */ 101#define NET_PACKET_COOKIE_REQUEST 24 /* Cookie request packet */
103#define NET_PACKET_COOKIE_RESPONSE 25 /* Cookie response packet */ 102#define NET_PACKET_COOKIE_RESPONSE 25 /* Cookie response packet */
@@ -105,7 +104,6 @@ typedef int sock_t;
105#define NET_PACKET_CRYPTO_DATA 27 /* Crypto data packet */ 104#define NET_PACKET_CRYPTO_DATA 27 /* Crypto data packet */
106#define NET_PACKET_CRYPTO 32 /* Encrypted data packet ID. */ 105#define NET_PACKET_CRYPTO 32 /* Encrypted data packet ID. */
107#define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */ 106#define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */
108#define NET_PACKET_GROUP_CHATS 48 /* Group chats packet ID. */
109 107
110/* See: docs/Prevent_Tracking.txt and onion.{c, h} */ 108/* See: docs/Prevent_Tracking.txt and onion.{c, h} */
111#define NET_PACKET_ONION_SEND_INITIAL 128 109#define NET_PACKET_ONION_SEND_INITIAL 128
@@ -135,8 +133,7 @@ typedef int sock_t;
135#define TCP_INET6 (AF_INET6 + 3) 133#define TCP_INET6 (AF_INET6 + 3)
136#define TCP_FAMILY (AF_INET6 + 4) 134#define TCP_FAMILY (AF_INET6 + 4)
137 135
138typedef union __attribute__ ((__packed__)) 136typedef union {
139{
140 uint8_t uint8[4]; 137 uint8_t uint8[4];
141 uint16_t uint16[2]; 138 uint16_t uint16[2];
142 uint32_t uint32; 139 uint32_t uint32;
@@ -144,8 +141,7 @@ typedef union __attribute__ ((__packed__))
144} 141}
145IP4; 142IP4;
146 143
147typedef union __attribute__ ((__packed__)) 144typedef union {
148{
149 uint8_t uint8[16]; 145 uint8_t uint8[16];
150 uint16_t uint16[8]; 146 uint16_t uint16[8];
151 uint32_t uint32[4]; 147 uint32_t uint32[4];
@@ -154,8 +150,7 @@ typedef union __attribute__ ((__packed__))
154} 150}
155IP6; 151IP6;
156 152
157typedef struct __attribute__ ((__packed__)) 153typedef struct {
158{
159 uint8_t family; 154 uint8_t family;
160 union { 155 union {
161 IP4 ip4; 156 IP4 ip4;
@@ -164,8 +159,7 @@ typedef struct __attribute__ ((__packed__))
164} 159}
165IP; 160IP;
166 161
167typedef struct __attribute__ ((__packed__)) __attribute__((gcc_struct)) 162typedef struct {
168{
169 IP ip; 163 IP ip;
170 uint16_t port; 164 uint16_t port;
171} 165}
@@ -185,10 +179,30 @@ IP_Port;
185/* ip_ntoa 179/* ip_ntoa
186 * converts ip into a string 180 * converts ip into a string
187 * uses a static buffer, so mustn't used multiple times in the same output 181 * uses a static buffer, so mustn't used multiple times in the same output
182 *
183 * IPv6 addresses are enclosed into square brackets, i.e. "[IPv6]"
184 * writes error message into the buffer on error
188 */ 185 */
189const char *ip_ntoa(const IP *ip); 186const char *ip_ntoa(const IP *ip);
190 187
191/* 188/*
189 * ip_parse_addr
190 * parses IP structure into an address string
191 *
192 * input
193 * ip: ip of AF_INET or AF_INET6 families
194 * length: length of the address buffer
195 * Must be at least INET_ADDRSTRLEN for AF_INET
196 * and INET6_ADDRSTRLEN for AF_INET6
197 *
198 * output
199 * address: dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6)
200 *
201 * returns 1 on success, 0 on failure
202 */
203int ip_parse_addr(const IP *ip, char *address, size_t length);
204
205/*
192 * addr_parse_ip 206 * addr_parse_ip
193 * directly parses the input into an IP structure 207 * directly parses the input into an IP structure
194 * tries IPv4 first, then IPv6 208 * tries IPv4 first, then IPv6
@@ -232,16 +246,6 @@ void ip_copy(IP *target, const IP *source);
232/* copies an ip_port structure */ 246/* copies an ip_port structure */
233void ipport_copy(IP_Port *target, const IP_Port *source); 247void ipport_copy(IP_Port *target, const IP_Port *source);
234 248
235
236/* packs IP into data, writes SIZE_IP bytes to data */
237void ip_pack(uint8_t *data, const IP *source);
238/* unpacks IP from data, reads SIZE_IP bytes from data */
239void ip_unpack(IP *target, const uint8_t *data);
240/* packs IP_Port into data, writes SIZE_IPPORT bytes to data */
241void ipport_pack(uint8_t *data, const IP_Port *source);
242/* unpacks IP_Port from data, reads SIZE_IPPORT bytes to data */
243void ipport_unpack(IP_Port *target, const uint8_t *data);
244
245/* 249/*
246 * addr_resolve(): 250 * addr_resolve():
247 * uses getaddrinfo to resolve an address into an IP address 251 * uses getaddrinfo to resolve an address into an IP address
@@ -282,7 +286,7 @@ int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra);
282 * Packet data is put into data. 286 * Packet data is put into data.
283 * Packet length is put into length. 287 * Packet length is put into length.
284 */ 288 */
285typedef int (*packet_handler_callback)(void *object, IP_Port ip_port, const uint8_t *data, uint32_t len); 289typedef int (*packet_handler_callback)(void *object, IP_Port ip_port, const uint8_t *data, uint16_t len);
286 290
287typedef struct { 291typedef struct {
288 packet_handler_callback function; 292 packet_handler_callback function;
@@ -343,7 +347,7 @@ uint64_t current_time_monotonic(void);
343/* Basic network functions: */ 347/* Basic network functions: */
344 348
345/* Function to send packet(data) of length length to ip_port. */ 349/* Function to send packet(data) of length length to ip_port. */
346int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint32_t length); 350int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint16_t length);
347 351
348/* Function to call when packet beginning with byte is received. */ 352/* Function to call when packet beginning with byte is received. */
349void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object); 353void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object);
@@ -356,8 +360,8 @@ void networking_poll(Networking_Core *net);
356 * ip must be in network order EX: 127.0.0.1 = (7F000001). 360 * ip must be in network order EX: 127.0.0.1 = (7F000001).
357 * port is in host byte order (this means don't worry about it). 361 * port is in host byte order (this means don't worry about it).
358 * 362 *
359 * return 0 if no problems. 363 * return Networking_Core object if no problems
360 * return -1 if there were problems. 364 * return NULL if there are problems.
361 */ 365 */
362Networking_Core *new_networking(IP ip, uint16_t port); 366Networking_Core *new_networking(IP ip, uint16_t port);
363 367