diff options
author | Diadlo <polsha3@gmail.com> | 2017-01-03 19:26:15 +0300 |
---|---|---|
committer | Diadlo <polsha3@gmail.com> | 2017-01-26 23:11:15 +0300 |
commit | f00006cf1d966270b6142519ad7d8487fb5eff5a (patch) | |
tree | b62ef423ad71abfe2f6a53ca0fede7510c07fdd5 /toxcore/network.h | |
parent | 287a29b8269eabfb33f07b9e0388a68be7ad2ba0 (diff) |
Add platform-independent Socket and IP implementation
Diffstat (limited to 'toxcore/network.h')
-rw-r--r-- | toxcore/network.h | 62 |
1 files changed, 18 insertions, 44 deletions
diff --git a/toxcore/network.h b/toxcore/network.h index 26126a22..a3b746c9 100644 --- a/toxcore/network.h +++ b/toxcore/network.h | |||
@@ -58,53 +58,21 @@ | |||
58 | #include <windows.h> | 58 | #include <windows.h> |
59 | #include <ws2tcpip.h> | 59 | #include <ws2tcpip.h> |
60 | 60 | ||
61 | #ifndef IPV6_V6ONLY | ||
62 | #define IPV6_V6ONLY 27 | ||
63 | #endif | ||
64 | |||
65 | typedef unsigned int sock_t; | ||
66 | /* sa_family_t is the sockaddr_in / sockaddr_in6 family field */ | 61 | /* sa_family_t is the sockaddr_in / sockaddr_in6 family field */ |
67 | typedef short sa_family_t; | 62 | typedef short sa_family_t; |
68 | 63 | ||
69 | #ifndef EWOULDBLOCK | ||
70 | #define EWOULDBLOCK WSAEWOULDBLOCK | ||
71 | #endif | ||
72 | |||
73 | #else // Linux includes | 64 | #else // Linux includes |
74 | 65 | ||
75 | #include <arpa/inet.h> | 66 | #include <arpa/inet.h> |
76 | #include <errno.h> | ||
77 | #include <fcntl.h> | ||
78 | #include <netdb.h> | 67 | #include <netdb.h> |
79 | #include <netinet/in.h> | ||
80 | #include <sys/socket.h> | ||
81 | #include <sys/time.h> | ||
82 | #include <sys/types.h> | ||
83 | #include <unistd.h> | 68 | #include <unistd.h> |
84 | 69 | ||
85 | typedef int sock_t; | ||
86 | |||
87 | #endif | ||
88 | |||
89 | #if defined(__AIX__) | ||
90 | # define _XOPEN_SOURCE 1 | ||
91 | #endif | 70 | #endif |
92 | 71 | ||
93 | #if defined(__sun__) | 72 | struct in_addr; |
94 | #define __EXTENSIONS__ 1 // SunOS! | 73 | struct in6_addr; |
95 | #if defined(__SunOS5_6__) || defined(__SunOS5_7__) || defined(__SunOS5_8__) || defined(__SunOS5_9__) || defined(__SunOS5_10__) | ||
96 | //Nothing needed | ||
97 | #else | ||
98 | #define __MAKECONTEXT_V2_SOURCE 1 | ||
99 | #endif | ||
100 | #endif | ||
101 | 74 | ||
102 | #ifndef IPV6_ADD_MEMBERSHIP | 75 | typedef int Socket; |
103 | #ifdef IPV6_JOIN_GROUP | ||
104 | #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP | ||
105 | #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP | ||
106 | #endif | ||
107 | #endif | ||
108 | 76 | ||
109 | #define MAX_UDP_PACKET_SIZE 2048 | 77 | #define MAX_UDP_PACKET_SIZE 2048 |
110 | 78 | ||
@@ -154,7 +122,6 @@ typedef union { | |||
154 | uint8_t uint8[4]; | 122 | uint8_t uint8[4]; |
155 | uint16_t uint16[2]; | 123 | uint16_t uint16[2]; |
156 | uint32_t uint32; | 124 | uint32_t uint32; |
157 | struct in_addr in_addr; | ||
158 | } | 125 | } |
159 | IP4; | 126 | IP4; |
160 | 127 | ||
@@ -163,7 +130,6 @@ typedef union { | |||
163 | uint16_t uint16[8]; | 130 | uint16_t uint16[8]; |
164 | uint32_t uint32[4]; | 131 | uint32_t uint32[4]; |
165 | uint64_t uint64[2]; | 132 | uint64_t uint64[2]; |
166 | struct in6_addr in6_addr; | ||
167 | } | 133 | } |
168 | IP6; | 134 | IP6; |
169 | 135 | ||
@@ -182,6 +148,14 @@ typedef struct { | |||
182 | } | 148 | } |
183 | IP_Port; | 149 | IP_Port; |
184 | 150 | ||
151 | /* Convert in_addr to IP */ | ||
152 | void get_ip4(IP4 *ip, const struct in_addr *addr); | ||
153 | void get_ip6(IP6 *ip, const struct in6_addr *addr); | ||
154 | |||
155 | /* Conevrt IP to in_addr */ | ||
156 | void fill_addr4(IP4 ip, struct in_addr *addr); | ||
157 | void fill_addr6(IP6 ip, struct in6_addr *addr); | ||
158 | |||
185 | /* Does the IP6 struct a contain an IPv4 address in an IPv6 one? */ | 159 | /* Does the IP6 struct a contain an IPv4 address in an IPv6 one? */ |
186 | #define IPV6_IPV4_IN_V6(a) ((a.uint64[0] == 0) && (a.uint32[2] == htonl (0xffff))) | 160 | #define IPV6_IPV4_IN_V6(a) ((a.uint64[0] == 0) && (a.uint32[2] == htonl (0xffff))) |
187 | 161 | ||
@@ -326,7 +300,7 @@ typedef struct { | |||
326 | sa_family_t family; | 300 | sa_family_t family; |
327 | uint16_t port; | 301 | uint16_t port; |
328 | /* Our UDP socket. */ | 302 | /* Our UDP socket. */ |
329 | sock_t sock; | 303 | Socket sock; |
330 | } Networking_Core; | 304 | } Networking_Core; |
331 | 305 | ||
332 | /* Run this before creating sockets. | 306 | /* Run this before creating sockets. |
@@ -341,39 +315,39 @@ int networking_at_startup(void); | |||
341 | * return 1 if valid | 315 | * return 1 if valid |
342 | * return 0 if not valid | 316 | * return 0 if not valid |
343 | */ | 317 | */ |
344 | int sock_valid(sock_t sock); | 318 | int sock_valid(Socket sock); |
345 | 319 | ||
346 | /* Close the socket. | 320 | /* Close the socket. |
347 | */ | 321 | */ |
348 | void kill_sock(sock_t sock); | 322 | void kill_sock(Socket sock); |
349 | 323 | ||
350 | /* Set socket as nonblocking | 324 | /* Set socket as nonblocking |
351 | * | 325 | * |
352 | * return 1 on success | 326 | * return 1 on success |
353 | * return 0 on failure | 327 | * return 0 on failure |
354 | */ | 328 | */ |
355 | int set_socket_nonblock(sock_t sock); | 329 | int set_socket_nonblock(Socket sock); |
356 | 330 | ||
357 | /* Set socket to not emit SIGPIPE | 331 | /* Set socket to not emit SIGPIPE |
358 | * | 332 | * |
359 | * return 1 on success | 333 | * return 1 on success |
360 | * return 0 on failure | 334 | * return 0 on failure |
361 | */ | 335 | */ |
362 | int set_socket_nosigpipe(sock_t sock); | 336 | int set_socket_nosigpipe(Socket sock); |
363 | 337 | ||
364 | /* Enable SO_REUSEADDR on socket. | 338 | /* Enable SO_REUSEADDR on socket. |
365 | * | 339 | * |
366 | * return 1 on success | 340 | * return 1 on success |
367 | * return 0 on failure | 341 | * return 0 on failure |
368 | */ | 342 | */ |
369 | int set_socket_reuseaddr(sock_t sock); | 343 | int set_socket_reuseaddr(Socket sock); |
370 | 344 | ||
371 | /* Set socket to dual (IPv4 + IPv6 socket) | 345 | /* Set socket to dual (IPv4 + IPv6 socket) |
372 | * | 346 | * |
373 | * return 1 on success | 347 | * return 1 on success |
374 | * return 0 on failure | 348 | * return 0 on failure |
375 | */ | 349 | */ |
376 | int set_socket_dualstack(sock_t sock); | 350 | int set_socket_dualstack(Socket sock); |
377 | 351 | ||
378 | /* return current monotonic time in milliseconds (ms). */ | 352 | /* return current monotonic time in milliseconds (ms). */ |
379 | uint64_t current_time_monotonic(void); | 353 | uint64_t current_time_monotonic(void); |