summaryrefslogtreecommitdiff
path: root/core/network.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-07-20 08:25:19 -0700
committerirungentoo <irungentoo@gmail.com>2013-07-20 08:25:19 -0700
commit72ea431489f1eff56c7409c6cacd55f819684c55 (patch)
treeac1cef0b790abf72f3e03672bce3593c7a3d010a /core/network.c
parent11e94066f710a602a9a071b028036a2b35de2741 (diff)
parent9616cc6a89247e0b13eb575f070467ba72a2024c (diff)
Merge pull request #62 from Captainhat/master
More comments in core fixed
Diffstat (limited to 'core/network.c')
-rw-r--r--core/network.c56
1 files changed, 28 insertions, 28 deletions
diff --git a/core/network.c b/core/network.c
index c08b3512..dbe4574c 100644
--- a/core/network.c
+++ b/core/network.c
@@ -25,12 +25,12 @@
25#include "network.h" 25#include "network.h"
26 26
27 27
28//returns current UNIX time in microseconds (us). 28/* returns current UNIX time in microseconds (us). */
29uint64_t current_time() 29uint64_t current_time()
30{ 30{
31 uint64_t time; 31 uint64_t time;
32 #ifdef WIN32 32 #ifdef WIN32
33 //This probably works fine 33 /* This probably works fine */
34 FILETIME ft; 34 FILETIME ft;
35 GetSystemTimeAsFileTime(&ft); 35 GetSystemTimeAsFileTime(&ft);
36 time = ft.dwHighDateTime; 36 time = ft.dwHighDateTime;
@@ -48,8 +48,8 @@ uint64_t current_time()
48 48
49} 49}
50 50
51//return a random number 51/* return a random number
52//NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary 52 NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary */
53uint32_t random_int() 53uint32_t random_int()
54{ 54{
55 #ifndef VANILLA_NACL 55 #ifndef VANILLA_NACL
@@ -60,11 +60,11 @@ uint32_t random_int()
60 #endif 60 #endif
61} 61}
62 62
63//our UDP socket, a global variable. 63/* our UDP socket, a global variable. */
64static int sock; 64static int sock;
65 65
66//Basic network functions: 66/* Basic network functions:
67//Function to send packet(data) of length length to ip_port 67 Function to send packet(data) of length length to ip_port */
68int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length) 68int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length)
69{ 69{
70 ADDR addr = {AF_INET, ip_port.port, ip_port.ip}; 70 ADDR addr = {AF_INET, ip_port.port, ip_port.ip};
@@ -72,10 +72,10 @@ int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length)
72 72
73} 73}
74 74
75//Function to receive data, ip and port of sender is put into ip_port 75/* Function to receive data, ip and port of sender is put into ip_port
76//the packet data into data 76 the packet data into data
77//the packet length into length. 77 the packet length into length.
78//dump all empty packets. 78 dump all empty packets. */
79int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length) 79int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
80{ 80{
81 ADDR addr; 81 ADDR addr;
@@ -87,8 +87,8 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
87 (*(int32_t *)length) = recvfrom(sock,(char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); 87 (*(int32_t *)length) = recvfrom(sock,(char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
88 if(*(int32_t *)length <= 0) 88 if(*(int32_t *)length <= 0)
89 { 89 {
90 //nothing received 90 /* nothing received
91 //or empty packet 91 or empty packet */
92 return -1; 92 return -1;
93 } 93 }
94 ip_port->ip = addr.ip; 94 ip_port->ip = addr.ip;
@@ -97,12 +97,12 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
97 97
98} 98}
99 99
100//initialize networking 100/* initialize networking
101//bind to ip and port 101 bind to ip and port
102//ip must be in network order EX: 127.0.0.1 = (7F000001) 102 ip must be in network order EX: 127.0.0.1 = (7F000001)
103//port is in host byte order (this means don't worry about it) 103 port is in host byte order (this means don't worry about it)
104//returns 0 if no problems 104 returns 0 if no problems
105//TODO: add something to check if there are errors 105 TODO: add something to check if there are errors */
106int init_networking(IP ip ,uint16_t port) 106int init_networking(IP ip ,uint16_t port)
107{ 107{
108 #ifdef WIN32 108 #ifdef WIN32
@@ -117,11 +117,11 @@ int init_networking(IP ip ,uint16_t port)
117 #endif 117 #endif
118 srand((uint32_t)current_time()); 118 srand((uint32_t)current_time());
119 119
120 //initialize our socket 120 /* initialize our socket */
121 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 121 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
122 122
123 //Functions to increase the size of the send and receive UDP buffers 123 /* Functions to increase the size of the send and receive UDP buffers
124 //NOTE: uncomment if necessary 124 NOTE: uncomment if necessary */
125 /* 125 /*
126 int n = 1024 * 1024 * 2; 126 int n = 1024 * 1024 * 2;
127 if(setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&n, sizeof(n)) == -1) 127 if(setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&n, sizeof(n)) == -1)
@@ -134,28 +134,28 @@ int init_networking(IP ip ,uint16_t port)
134 return -1; 134 return -1;
135 }*/ 135 }*/
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 return 0; 150 return 0;
151 151
152} 152}
153 153
154//function to cleanup networking stuff 154/* function to cleanup networking stuff */
155void shutdown_networking() 155void shutdown_networking()
156{ 156{
157 #ifdef WIN32 157 #ifdef WIN32
158 WSACleanup(); 158 WSACleanup();
159 #endif 159 #endif
160 return; 160 return;
161} \ No newline at end of file 161}