diff options
author | irungentoo <irungentoo@gmail.com> | 2013-08-16 13:11:09 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-08-16 13:11:09 -0400 |
commit | 88ff81d9def5efe69cbaf91aa41906177ba7dde9 (patch) | |
tree | cb9f149e438bcd1f18d8c1eb5d8be6b0a22f58a4 /core/network.c | |
parent | c5af8f44a9d040a0bbe0442ec074d9fc8562dd32 (diff) |
Passed everything through astyle.
Diffstat (limited to 'core/network.c')
-rw-r--r-- | core/network.c | 44 |
1 files changed, 26 insertions, 18 deletions
diff --git a/core/network.c b/core/network.c index 7e3b344a..1977ce38 100644 --- a/core/network.c +++ b/core/network.c | |||
@@ -32,14 +32,14 @@ uint64_t current_time(void) | |||
32 | FILETIME ft; | 32 | FILETIME ft; |
33 | GetSystemTimeAsFileTime(&ft); | 33 | GetSystemTimeAsFileTime(&ft); |
34 | time = ft.dwHighDateTime; | 34 | time = ft.dwHighDateTime; |
35 | time <<=32; | 35 | time <<= 32; |
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 | } |
@@ -61,17 +61,17 @@ static int sock; | |||
61 | 61 | ||
62 | /* Basic network functions: | 62 | /* Basic network functions: |
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 | ||
70 | /* Function to receive data, ip and port of sender is put into ip_port | 70 | /* Function to receive data, ip and port of sender is put into ip_port |
71 | the packet data into data | 71 | the packet data into data |
72 | the packet length into length. | 72 | the packet length into length. |
73 | dump all empty packets. */ | 73 | dump all empty packets. */ |
74 | static int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length) | 74 | static 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 |
@@ -79,8 +79,9 @@ static int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length) | |||
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 | |
84 | if (*(int32_t *)length <= 0) | ||
84 | return -1; /* nothing received or empty packet */ | 85 | return -1; /* nothing received or empty packet */ |
85 | 86 | ||
86 | ip_port->ip = addr.ip; | 87 | ip_port->ip = addr.ip; |
@@ -100,11 +101,12 @@ void networking_poll() | |||
100 | IP_Port ip_port; | 101 | IP_Port ip_port; |
101 | uint8_t data[MAX_UDP_PACKET_SIZE]; | 102 | uint8_t data[MAX_UDP_PACKET_SIZE]; |
102 | uint32_t length; | 103 | uint32_t length; |
103 | 104 | ||
104 | while (receivepacket(&ip_port, data, &length) != -1) | 105 | while (receivepacket(&ip_port, data, &length) != -1) { |
105 | { | ||
106 | if (length < 1) continue; | 106 | if (length < 1) continue; |
107 | |||
107 | if (!packethandlers[data[0]]) continue; | 108 | if (!packethandlers[data[0]]) continue; |
109 | |||
108 | packethandlers[data[0]](ip_port, data, length); | 110 | packethandlers[data[0]](ip_port, data, length); |
109 | } | 111 | } |
110 | } | 112 | } |
@@ -119,8 +121,10 @@ int init_networking(IP ip, uint16_t port) | |||
119 | { | 121 | { |
120 | #ifdef WIN32 | 122 | #ifdef WIN32 |
121 | WSADATA wsaData; | 123 | WSADATA wsaData; |
122 | if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) | 124 | |
125 | if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR) | ||
123 | return -1; | 126 | return -1; |
127 | |||
124 | #else | 128 | #else |
125 | srandom((uint32_t)current_time()); | 129 | srandom((uint32_t)current_time()); |
126 | #endif | 130 | #endif |
@@ -131,11 +135,15 @@ int init_networking(IP ip, uint16_t port) | |||
131 | 135 | ||
132 | /* Check for socket error */ | 136 | /* Check for socket error */ |
133 | #ifdef WIN32 | 137 | #ifdef WIN32 |
138 | |||
134 | if (sock == INVALID_SOCKET) /* MSDN recommends this */ | 139 | if (sock == INVALID_SOCKET) /* MSDN recommends this */ |
135 | return -1; | 140 | return -1; |
141 | |||
136 | #else | 142 | #else |
143 | |||
137 | if (sock < 0) | 144 | if (sock < 0) |
138 | return -1; | 145 | return -1; |
146 | |||
139 | #endif | 147 | #endif |
140 | 148 | ||
141 | /* Functions to increase the size of the send and receive UDP buffers | 149 | /* Functions to increase the size of the send and receive UDP buffers |
@@ -153,7 +161,7 @@ int init_networking(IP ip, uint16_t port) | |||
153 | 161 | ||
154 | /* Enable broadcast on socket */ | 162 | /* Enable broadcast on socket */ |
155 | int broadcast = 1; | 163 | int broadcast = 1; |
156 | setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, sizeof(broadcast)); | 164 | setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast, sizeof(broadcast)); |
157 | 165 | ||
158 | /* Set socket nonblocking */ | 166 | /* Set socket nonblocking */ |
159 | #ifdef WIN32 | 167 | #ifdef WIN32 |
@@ -167,7 +175,7 @@ int init_networking(IP ip, uint16_t port) | |||
167 | 175 | ||
168 | /* Bind our socket to port PORT and address 0.0.0.0 */ | 176 | /* Bind our socket to port PORT and address 0.0.0.0 */ |
169 | ADDR addr = {AF_INET, htons(port), ip}; | 177 | ADDR addr = {AF_INET, htons(port), ip}; |
170 | bind(sock, (struct sockaddr*)&addr, sizeof(addr)); | 178 | bind(sock, (struct sockaddr *)&addr, sizeof(addr)); |
171 | 179 | ||
172 | return 0; | 180 | return 0; |
173 | } | 181 | } |
@@ -207,18 +215,18 @@ uint32_t resolve_addr(const char *address) | |||
207 | rc = getaddrinfo(address, "echo", &hints, &server); | 215 | rc = getaddrinfo(address, "echo", &hints, &server); |
208 | 216 | ||
209 | // Lookup failed. | 217 | // Lookup failed. |
210 | if(rc != 0) { | 218 | if (rc != 0) { |
211 | return 0; | 219 | return 0; |
212 | } | 220 | } |
213 | 221 | ||
214 | // IPv4 records only.. | 222 | // IPv4 records only.. |
215 | if(server->ai_family != AF_INET) { | 223 | if (server->ai_family != AF_INET) { |
216 | freeaddrinfo(server); | 224 | freeaddrinfo(server); |
217 | return 0; | 225 | return 0; |
218 | } | 226 | } |
219 | |||
220 | 227 | ||
221 | addr = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr; | 228 | |
229 | addr = ((struct sockaddr_in *)server->ai_addr)->sin_addr.s_addr; | ||
222 | 230 | ||
223 | freeaddrinfo(server); | 231 | freeaddrinfo(server); |
224 | return addr; | 232 | return addr; |