diff options
Diffstat (limited to 'core/network.c')
-rw-r--r-- | core/network.c | 64 |
1 files changed, 32 insertions, 32 deletions
diff --git a/core/network.c b/core/network.c index 8804ab4e..aa16bda9 100644 --- a/core/network.c +++ b/core/network.c | |||
@@ -1,7 +1,7 @@ | |||
1 | /* network.h | 1 | /* network.h |
2 | * | 2 | * |
3 | * Functions for the core networking. | 3 | * Functions for the core networking. |
4 | * | 4 | * |
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | 5 | * Copyright (C) 2013 Tox project All Rights Reserved. |
6 | * | 6 | * |
7 | * This file is part of Tox. | 7 | * This file is part of Tox. |
@@ -18,7 +18,7 @@ | |||
18 | * | 18 | * |
19 | * You should have received a copy of the GNU General Public License | 19 | * You should have received a copy of the GNU General Public License |
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | 20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. |
21 | * | 21 | * |
22 | */ | 22 | */ |
23 | 23 | ||
24 | #include "network.h" | 24 | #include "network.h" |
@@ -27,7 +27,7 @@ | |||
27 | uint64_t current_time() | 27 | uint64_t current_time() |
28 | { | 28 | { |
29 | uint64_t time; | 29 | uint64_t time; |
30 | #ifdef WIN32 | 30 | #ifdef WIN32 |
31 | /* This probably works fine */ | 31 | /* This probably works fine */ |
32 | FILETIME ft; | 32 | FILETIME ft; |
33 | GetSystemTimeAsFileTime(&ft); | 33 | GetSystemTimeAsFileTime(&ft); |
@@ -36,24 +36,24 @@ uint64_t current_time() | |||
36 | time |= ft.dwLowDateTime; | 36 | time |= ft.dwLowDateTime; |
37 | time -= 116444736000000000UL; | 37 | time -= 116444736000000000UL; |
38 | return time/10; | 38 | return time/10; |
39 | #else | 39 | #else |
40 | struct timeval a; | 40 | struct timeval a; |
41 | gettimeofday(&a, NULL); | 41 | gettimeofday(&a, NULL); |
42 | time = 1000000UL*a.tv_sec + a.tv_usec; | 42 | time = 1000000UL*a.tv_sec + a.tv_usec; |
43 | return time; | 43 | return time; |
44 | #endif | 44 | #endif |
45 | } | 45 | } |
46 | 46 | ||
47 | /* return a random number | 47 | /* return a random number |
48 | NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary */ | 48 | NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary */ |
49 | uint32_t random_int() | 49 | uint32_t random_int() |
50 | { | 50 | { |
51 | #ifndef VANILLA_NACL | 51 | #ifndef VANILLA_NACL |
52 | //NOTE: this function comes from libsodium | 52 | //NOTE: this function comes from libsodium |
53 | return randombytes_random(); | 53 | return randombytes_random(); |
54 | #else | 54 | #else |
55 | return random(); | 55 | return random(); |
56 | #endif | 56 | #endif |
57 | } | 57 | } |
58 | 58 | ||
59 | /* our UDP socket, a global variable. */ | 59 | /* our UDP socket, a global variable. */ |
@@ -63,7 +63,7 @@ static int sock; | |||
63 | Function to send packet(data) of length length to ip_port */ | 63 | Function to send packet(data) of length length to ip_port */ |
64 | int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length) | 64 | int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length) |
65 | { | 65 | { |
66 | ADDR addr = {AF_INET, ip_port.port, ip_port.ip}; | 66 | ADDR addr = {AF_INET, ip_port.port, ip_port.ip}; |
67 | return sendto(sock,(char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); | 67 | return sendto(sock,(char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); |
68 | } | 68 | } |
69 | 69 | ||
@@ -74,15 +74,15 @@ int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length) | |||
74 | int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length) | 74 | int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length) |
75 | { | 75 | { |
76 | ADDR addr; | 76 | ADDR addr; |
77 | #ifdef WIN32 | 77 | #ifdef WIN32 |
78 | int addrlen = sizeof(addr); | 78 | int addrlen = sizeof(addr); |
79 | #else | 79 | #else |
80 | uint32_t addrlen = sizeof(addr); | 80 | uint32_t addrlen = sizeof(addr); |
81 | #endif | 81 | #endif |
82 | (*(int32_t*)length) = recvfrom(sock,(char*) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr*)&addr, &addrlen); | 82 | (*(int32_t*)length) = recvfrom(sock,(char*) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr*)&addr, &addrlen); |
83 | if (*(int32_t*)length <= 0) | 83 | if (*(int32_t*)length <= 0) |
84 | return -1; /* nothing received or empty packet */ | 84 | return -1; /* nothing received or empty packet */ |
85 | 85 | ||
86 | ip_port->ip = addr.ip; | 86 | ip_port->ip = addr.ip; |
87 | ip_port->port = addr.port; | 87 | ip_port->port = addr.port; |
88 | return 0; | 88 | return 0; |
@@ -92,30 +92,30 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length) | |||
92 | bind to ip and port | 92 | bind to ip and port |
93 | ip must be in network order EX: 127.0.0.1 = (7F000001) | 93 | ip must be in network order EX: 127.0.0.1 = (7F000001) |
94 | port is in host byte order (this means don't worry about it) | 94 | port is in host byte order (this means don't worry about it) |
95 | returns 0 if no problems | 95 | returns 0 if no problems |
96 | returns -1 if there are problems */ | 96 | returns -1 if there are problems */ |
97 | int init_networking(IP ip, uint16_t port) | 97 | int init_networking(IP ip, uint16_t port) |
98 | { | 98 | { |
99 | #ifdef WIN32 | 99 | #ifdef WIN32 |
100 | WSADATA wsaData; | 100 | WSADATA wsaData; |
101 | if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) | 101 | if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) |
102 | return -1; | 102 | return -1; |
103 | #else | 103 | #else |
104 | srandom((uint32_t)current_time()); | 104 | srandom((uint32_t)current_time()); |
105 | #endif | 105 | #endif |
106 | srand((uint32_t)current_time()); | 106 | srand((uint32_t)current_time()); |
107 | 107 | ||
108 | /* initialize our socket */ | 108 | /* initialize our socket */ |
109 | sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | 109 | sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
110 | 110 | ||
111 | /* Check for socket error */ | 111 | /* Check for socket error */ |
112 | #ifdef WIN32 | 112 | #ifdef WIN32 |
113 | if (sock == INVALID_SOCKET) /* MSDN recommends this */ | 113 | if (sock == INVALID_SOCKET) /* MSDN recommends this */ |
114 | return -1; | 114 | return -1; |
115 | #else | 115 | #else |
116 | if (sock < 0) | 116 | if (sock < 0) |
117 | return -1; | 117 | return -1; |
118 | #endif | 118 | #endif |
119 | 119 | ||
120 | /* Functions to increase the size of the send and receive UDP buffers | 120 | /* Functions to increase the size of the send and receive UDP buffers |
121 | NOTE: uncomment if necessary */ | 121 | NOTE: uncomment if necessary */ |
@@ -129,23 +129,23 @@ int init_networking(IP ip, uint16_t port) | |||
129 | if(setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char*)&n, sizeof(n)) == -1) | 129 | if(setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char*)&n, sizeof(n)) == -1) |
130 | return -1; | 130 | return -1; |
131 | */ | 131 | */ |
132 | 132 | ||
133 | /* Enable broadcast on socket */ | 133 | /* Enable broadcast on socket */ |
134 | int broadcast = 1; | 134 | int broadcast = 1; |
135 | setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, sizeof(broadcast)); | 135 | setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, sizeof(broadcast)); |
136 | 136 | ||
137 | /* Set socket nonblocking */ | 137 | /* Set socket nonblocking */ |
138 | #ifdef WIN32 | 138 | #ifdef WIN32 |
139 | /* I think this works for windows */ | 139 | /* I think this works for windows */ |
140 | u_long mode = 1; | 140 | u_long mode = 1; |
141 | /* ioctl(sock, FIONBIO, &mode); */ | 141 | /* ioctl(sock, FIONBIO, &mode); */ |
142 | ioctlsocket(sock, FIONBIO, &mode); | 142 | ioctlsocket(sock, FIONBIO, &mode); |
143 | #else | 143 | #else |
144 | fcntl(sock, F_SETFL, O_NONBLOCK, 1); | 144 | fcntl(sock, F_SETFL, O_NONBLOCK, 1); |
145 | #endif | 145 | #endif |
146 | 146 | ||
147 | /* Bind our socket to port PORT and address 0.0.0.0 */ | 147 | /* Bind our socket to port PORT and address 0.0.0.0 */ |
148 | ADDR addr = {AF_INET, htons(port), ip}; | 148 | ADDR addr = {AF_INET, htons(port), ip}; |
149 | bind(sock, (struct sockaddr*)&addr, sizeof(addr)); | 149 | bind(sock, (struct sockaddr*)&addr, sizeof(addr)); |
150 | 150 | ||
151 | return 0; | 151 | return 0; |
@@ -155,12 +155,12 @@ int init_networking(IP ip, uint16_t port) | |||
155 | /* function to cleanup networking stuff */ | 155 | /* function to cleanup networking stuff */ |
156 | void shutdown_networking() | 156 | void shutdown_networking() |
157 | { | 157 | { |
158 | #ifdef WIN32 | 158 | #ifdef WIN32 |
159 | closesocket(sock); | 159 | closesocket(sock); |
160 | WSACleanup(); | 160 | WSACleanup(); |
161 | #else | 161 | #else |
162 | close(sock); | 162 | close(sock); |
163 | #endif | 163 | #endif |
164 | return; | 164 | return; |
165 | } | 165 | } |
166 | 166 | ||