summaryrefslogtreecommitdiff
path: root/toxcore/network.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/network.c')
-rw-r--r--toxcore/network.c59
1 files changed, 33 insertions, 26 deletions
diff --git a/toxcore/network.c b/toxcore/network.c
index 2bcf7d61..34775570 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -23,7 +23,7 @@
23 23
24#include "network.h" 24#include "network.h"
25 25
26/* returns current UNIX time in microseconds (us). */ 26/* return current UNIX time in microseconds (us). */
27uint64_t current_time(void) 27uint64_t current_time(void)
28{ 28{
29 uint64_t time; 29 uint64_t time;
@@ -44,12 +44,13 @@ uint64_t current_time(void)
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 */
49uint32_t random_int(void) 50uint32_t random_int(void)
50{ 51{
51#ifndef VANILLA_NACL 52#ifndef VANILLA_NACL
52 //NOTE: this function comes from libsodium 53 /* NOTE: this function comes from libsodium. */
53 return randombytes_random(); 54 return randombytes_random();
54#else 55#else
55 return random(); 56 return random();
@@ -57,17 +58,20 @@ uint32_t random_int(void)
57} 58}
58 59
59/* Basic network functions: 60/* Basic network functions:
60 Function to send packet(data) of length length to ip_port */ 61 * Function to send packet(data) of length length to ip_port.
62 */
61int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length) 63int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length)
62{ 64{
63 ADDR addr = {AF_INET, ip_port.port, ip_port.ip}; 65 ADDR addr = {AF_INET, ip_port.port, ip_port.ip};
64 return sendto(sock, (char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); 66 return sendto(sock, (char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr));
65} 67}
66 68
67/* Function to receive data, ip and port of sender is put into ip_port 69/* Function to receive data
68 the packet data into data 70 * ip and port of sender is put into ip_port.
69 the packet length into length. 71 * Packet data is put into data.
70 dump all empty packets. */ 72 * Packet length is put into length.
73 * Dump all empty packets.
74 */
71static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *length) 75static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *length)
72{ 76{
73 ADDR addr; 77 ADDR addr;
@@ -79,7 +83,7 @@ static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *le
79 (*(int32_t *)length) = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); 83 (*(int32_t *)length) = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
80 84
81 if (*(int32_t *)length <= 0) 85 if (*(int32_t *)length <= 0)
82 return -1; /* nothing received or empty packet */ 86 return -1; /* Nothing received or empty packet. */
83 87
84 ip_port->ip = addr.ip; 88 ip_port->ip = addr.ip;
85 ip_port->port = addr.port; 89 ip_port->port = addr.port;
@@ -127,7 +131,7 @@ static int at_startup(void)
127 return 0; 131 return 0;
128} 132}
129 133
130/* TODO: put this somewhere 134/* TODO: Put this somewhere
131static void at_shutdown(void) 135static void at_shutdown(void)
132{ 136{
133#ifdef WIN32 137#ifdef WIN32
@@ -136,18 +140,20 @@ static void at_shutdown(void)
136} 140}
137*/ 141*/
138 142
139/* initialize networking 143/* Initialize networking.
140 bind to ip and port 144 * Bind to ip and port.
141 ip must be in network order EX: 127.0.0.1 = (7F000001) 145 * ip must be in network order EX: 127.0.0.1 = (7F000001).
142 port is in host byte order (this means don't worry about it) 146 * port is in host byte order (this means don't worry about it).
143 returns Networking_Core object if no problems 147 *
144 returns NULL if there are problems */ 148 * returns Networking_Core object if no problems
149 * returns NULL if there are problems.
150 */
145Networking_Core *new_networking(IP ip, uint16_t port) 151Networking_Core *new_networking(IP ip, uint16_t port)
146{ 152{
147 if (at_startup() != 0) 153 if (at_startup() != 0)
148 return NULL; 154 return NULL;
149 155
150 /* initialize our socket */ 156 /* Initialize our socket. */
151 Networking_Core *temp = calloc(1, sizeof(Networking_Core)); 157 Networking_Core *temp = calloc(1, sizeof(Networking_Core));
152 158
153 if (temp == NULL) 159 if (temp == NULL)
@@ -155,10 +161,10 @@ Networking_Core *new_networking(IP ip, uint16_t port)
155 161
156 temp->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 162 temp->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
157 163
158 /* Check for socket error */ 164 /* Check for socket error. */
159#ifdef WIN32 165#ifdef WIN32
160 166
161 if (temp->sock == INVALID_SOCKET) { /* MSDN recommends this */ 167 if (temp->sock == INVALID_SOCKET) { /* MSDN recommends this. */
162 free(temp); 168 free(temp);
163 return NULL; 169 return NULL;
164 } 170 }
@@ -172,8 +178,9 @@ Networking_Core *new_networking(IP ip, uint16_t port)
172 178
173#endif 179#endif
174 180
175 /* Functions to increase the size of the send and receive UDP buffers 181 /* Functions to increase the size of the send and receive UDP buffers.
176 NOTE: uncomment if necessary */ 182 * NOTE: Uncomment if necessary.
183 */
177 /* 184 /*
178 int n = 1024 * 1024 * 2; 185 int n = 1024 * 1024 * 2;
179 if(setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&n, sizeof(n)) == -1) 186 if(setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&n, sizeof(n)) == -1)
@@ -185,13 +192,13 @@ Networking_Core *new_networking(IP ip, uint16_t port)
185 return -1; 192 return -1;
186 */ 193 */
187 194
188 /* Enable broadcast on socket */ 195 /* Enable broadcast on socket. */
189 int broadcast = 1; 196 int broadcast = 1;
190 setsockopt(temp->sock, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast, sizeof(broadcast)); 197 setsockopt(temp->sock, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast, sizeof(broadcast));
191 198
192 /* Set socket nonblocking */ 199 /* Set socket nonblocking. */
193#ifdef WIN32 200#ifdef WIN32
194 /* I think this works for windows */ 201 /* I think this works for Windows. */
195 u_long mode = 1; 202 u_long mode = 1;
196 /* ioctl(sock, FIONBIO, &mode); */ 203 /* ioctl(sock, FIONBIO, &mode); */
197 ioctlsocket(temp->sock, FIONBIO, &mode); 204 ioctlsocket(temp->sock, FIONBIO, &mode);
@@ -205,7 +212,7 @@ Networking_Core *new_networking(IP ip, uint16_t port)
205 return temp; 212 return temp;
206} 213}
207 214
208/* function to cleanup networking stuff */ 215/* Function to cleanup networking stuff. */
209void kill_networking(Networking_Core *net) 216void kill_networking(Networking_Core *net)
210{ 217{
211#ifdef WIN32 218#ifdef WIN32