diff options
author | mannol <eniz_vukovic@hotmail.com> | 2014-05-20 00:10:40 +0200 |
---|---|---|
committer | mannol <eniz_vukovic@hotmail.com> | 2014-05-20 00:10:40 +0200 |
commit | a1f2a18ae4146ebea11fbd3f83df803b715813da (patch) | |
tree | 63b29cf3770a65e94e3e5d0fcd53b33f2a654585 /toxcore/network.h | |
parent | ea96c1758ae42c59978c450f9e11496a55d49ee5 (diff) |
Merge upstream and other stuff
Diffstat (limited to 'toxcore/network.h')
-rw-r--r-- | toxcore/network.h | 80 |
1 files changed, 39 insertions, 41 deletions
diff --git a/toxcore/network.h b/toxcore/network.h index 42ade800..0eb7bbd0 100644 --- a/toxcore/network.h +++ b/toxcore/network.h | |||
@@ -89,7 +89,7 @@ typedef int sock_t; | |||
89 | #endif | 89 | #endif |
90 | 90 | ||
91 | #if defined(__sun__) | 91 | #if defined(__sun__) |
92 | #define __EXTENSIONS__ 1 // SunOS! | 92 | #define __EXTENSIONS__ 1 // SunOS! |
93 | #if defined(__SunOS5_6__) || defined(__SunOS5_7__) || defined(__SunOS5_8__) || defined(__SunOS5_9__) || defined(__SunOS5_10__) | 93 | #if defined(__SunOS5_6__) || defined(__SunOS5_7__) || defined(__SunOS5_8__) || defined(__SunOS5_9__) || defined(__SunOS5_10__) |
94 | //Nothing needed | 94 | //Nothing needed |
95 | #else | 95 | #else |
@@ -97,21 +97,6 @@ typedef int sock_t; | |||
97 | #endif | 97 | #endif |
98 | #endif | 98 | #endif |
99 | 99 | ||
100 | #ifndef VANILLA_NACL | ||
101 | /* We use libsodium by default. */ | ||
102 | #include <sodium.h> | ||
103 | #else | ||
104 | #include <crypto_box.h> | ||
105 | #include <crypto_secretbox.h> | ||
106 | #include <randombytes.h> | ||
107 | #include <crypto_hash_sha256.h> | ||
108 | #define crypto_box_MACBYTES (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) | ||
109 | #endif | ||
110 | |||
111 | #ifndef crypto_secretbox_MACBYTES | ||
112 | #define crypto_secretbox_MACBYTES (crypto_secretbox_ZEROBYTES - crypto_secretbox_BOXZEROBYTES) | ||
113 | #endif | ||
114 | |||
115 | #ifndef IPV6_ADD_MEMBERSHIP | 100 | #ifndef IPV6_ADD_MEMBERSHIP |
116 | #ifdef IPV6_JOIN_GROUP | 101 | #ifdef IPV6_JOIN_GROUP |
117 | #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP | 102 | #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP |
@@ -129,6 +114,10 @@ typedef int sock_t; | |||
129 | #define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID. */ | 114 | #define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID. */ |
130 | #define NET_PACKET_SYNC 17 /* SYNC packet ID. */ | 115 | #define NET_PACKET_SYNC 17 /* SYNC packet ID. */ |
131 | #define NET_PACKET_DATA 18 /* Data packet ID. */ | 116 | #define NET_PACKET_DATA 18 /* Data packet ID. */ |
117 | #define NET_PACKET_COOKIE_REQUEST 24 /* Cookie request packet */ | ||
118 | #define NET_PACKET_COOKIE_RESPONSE 25 /* Cookie response packet */ | ||
119 | #define NET_PACKET_CRYPTO_HS 26 /* Crypto handshake packet */ | ||
120 | #define NET_PACKET_CRYPTO_DATA 27 /* Crypto data packet */ | ||
132 | #define NET_PACKET_CRYPTO 32 /* Encrypted data packet ID. */ | 121 | #define NET_PACKET_CRYPTO 32 /* Encrypted data packet ID. */ |
133 | #define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */ | 122 | #define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */ |
134 | #define NET_PACKET_GROUP_CHATS 48 /* Group chats packet ID. */ | 123 | #define NET_PACKET_GROUP_CHATS 48 /* Group chats packet ID. */ |
@@ -161,8 +150,11 @@ typedef int sock_t; | |||
161 | #define TOX_PORTRANGE_TO 33545 | 150 | #define TOX_PORTRANGE_TO 33545 |
162 | #define TOX_PORT_DEFAULT TOX_PORTRANGE_FROM | 151 | #define TOX_PORT_DEFAULT TOX_PORTRANGE_FROM |
163 | 152 | ||
164 | 153 | /* TCP related */ | |
165 | /* TODO: remove padding bytes next time we need to break compatibility with old versions of core. */ | 154 | #define TCP_ONION_FAMILY (AF_INET6 + 1) |
155 | #define TCP_INET (AF_INET6 + 2) | ||
156 | #define TCP_INET6 (AF_INET6 + 3) | ||
157 | #define TCP_FAMILY (AF_INET6 + 4) | ||
166 | 158 | ||
167 | typedef union __attribute__ ((__packed__)) | 159 | typedef union __attribute__ ((__packed__)) |
168 | { | 160 | { |
@@ -186,8 +178,6 @@ IP6; | |||
186 | typedef struct __attribute__ ((__packed__)) | 178 | typedef struct __attribute__ ((__packed__)) |
187 | { | 179 | { |
188 | uint8_t family; | 180 | uint8_t family; |
189 | /* Not used for anything right now. */ | ||
190 | uint8_t padding[3]; | ||
191 | union { | 181 | union { |
192 | IP4 ip4; | 182 | IP4 ip4; |
193 | IP6 ip6; | 183 | IP6 ip6; |
@@ -195,23 +185,19 @@ typedef struct __attribute__ ((__packed__)) | |||
195 | } | 185 | } |
196 | IP; | 186 | IP; |
197 | 187 | ||
198 | typedef union __attribute__ ((__packed__)) | 188 | typedef struct __attribute__ ((__packed__)) __attribute__((gcc_struct)) |
199 | { | 189 | { |
200 | struct { | ||
201 | IP4 ip; | ||
202 | uint16_t port; | ||
203 | /* Not used for anything right now. */ | ||
204 | uint16_t padding; | ||
205 | }; | ||
206 | uint8_t uint8[8]; | ||
207 | } | ||
208 | IP4_Port; | ||
209 | |||
210 | typedef struct __attribute__ ((__packed__)) IP_Port { | ||
211 | IP ip; | 190 | IP ip; |
212 | uint16_t port; | 191 | uint16_t port; |
213 | uint16_t padding; | 192 | } |
214 | } IP_Port; | 193 | IP_Port; |
194 | |||
195 | |||
196 | #define SIZE_IP4 4 | ||
197 | #define SIZE_IP6 16 | ||
198 | #define SIZE_IP (1 + SIZE_IP6) | ||
199 | #define SIZE_PORT 2 | ||
200 | #define SIZE_IPPORT (SIZE_IP + SIZE_PORT) | ||
215 | 201 | ||
216 | #define TOX_ENABLE_IPV6_DEFAULT 1 | 202 | #define TOX_ENABLE_IPV6_DEFAULT 1 |
217 | 203 | ||
@@ -250,6 +236,16 @@ void ip_copy(IP *target, IP *source); | |||
250 | /* copies an ip_port structure */ | 236 | /* copies an ip_port structure */ |
251 | void ipport_copy(IP_Port *target, IP_Port *source); | 237 | void ipport_copy(IP_Port *target, IP_Port *source); |
252 | 238 | ||
239 | |||
240 | /* packs IP into data, writes SIZE_IP bytes to data */ | ||
241 | void ip_pack(uint8_t *data, IP *source); | ||
242 | /* unpacks IP from data, reads SIZE_IP bytes from data */ | ||
243 | void ip_unpack(IP *target, uint8_t *data); | ||
244 | /* packs IP_Port into data, writes SIZE_IPPORT bytes to data */ | ||
245 | void ipport_pack(uint8_t *data, IP_Port *source); | ||
246 | /* unpacks IP_Port from data, reads SIZE_IPPORT bytes to data */ | ||
247 | void ipport_unpack(IP_Port *target, uint8_t *data); | ||
248 | |||
253 | /* | 249 | /* |
254 | * addr_resolve(): | 250 | * addr_resolve(): |
255 | * uses getaddrinfo to resolve an address into an IP address | 251 | * uses getaddrinfo to resolve an address into an IP address |
@@ -332,6 +328,13 @@ void kill_sock(sock_t sock); | |||
332 | */ | 328 | */ |
333 | int set_socket_nonblock(sock_t sock); | 329 | int set_socket_nonblock(sock_t sock); |
334 | 330 | ||
331 | /* Set socket to not emit SIGPIPE | ||
332 | * | ||
333 | * return 1 on success | ||
334 | * return 0 on failure | ||
335 | */ | ||
336 | int set_socket_nosigpipe(sock_t sock); | ||
337 | |||
335 | /* Set socket to dual (IPv4 + IPv6 socket) | 338 | /* Set socket to dual (IPv4 + IPv6 socket) |
336 | * | 339 | * |
337 | * return 1 on success | 340 | * return 1 on success |
@@ -339,13 +342,8 @@ int set_socket_nonblock(sock_t sock); | |||
339 | */ | 342 | */ |
340 | int set_socket_dualstack(sock_t sock); | 343 | int set_socket_dualstack(sock_t sock); |
341 | 344 | ||
342 | /* return current time in milleseconds since the epoch. */ | 345 | /* return current monotonic time in milliseconds (ms). */ |
343 | uint64_t current_time(void); | 346 | uint64_t current_time_monotonic(void); |
344 | |||
345 | /* return a random number. | ||
346 | */ | ||
347 | uint32_t random_int(void); | ||
348 | uint64_t random_64b(void); | ||
349 | 347 | ||
350 | /* Basic network functions: */ | 348 | /* Basic network functions: */ |
351 | 349 | ||