diff options
Diffstat (limited to 'src/nat/gnunet-helper-nat-client-windows.c')
-rw-r--r-- | src/nat/gnunet-helper-nat-client-windows.c | 325 |
1 files changed, 161 insertions, 164 deletions
diff --git a/src/nat/gnunet-helper-nat-client-windows.c b/src/nat/gnunet-helper-nat-client-windows.c index dda6552d8..2dac5d48e 100644 --- a/src/nat/gnunet-helper-nat-client-windows.c +++ b/src/nat/gnunet-helper-nat-client-windows.c | |||
@@ -11,12 +11,12 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 15 | You should have received a copy of the GNU Affero General Public License |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file src/nat/gnunet-helper-nat-client-windows.c | 22 | * @file src/nat/gnunet-helper-nat-client-windows.c |
@@ -43,7 +43,7 @@ | |||
43 | */ | 43 | */ |
44 | #define _GNU_SOURCE | 44 | #define _GNU_SOURCE |
45 | /* Instead of including gnunet_common.h */ | 45 | /* Instead of including gnunet_common.h */ |
46 | #define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0) | 46 | #define GNUNET_memcpy(dst, src, n) do { if (0 != n) { (void)memcpy(dst, src, n); } } while (0) |
47 | 47 | ||
48 | #define FD_SETSIZE 1024 | 48 | #define FD_SETSIZE 1024 |
49 | #include <winsock2.h> | 49 | #include <winsock2.h> |
@@ -73,9 +73,7 @@ | |||
73 | /** | 73 | /** |
74 | * IPv4 header. | 74 | * IPv4 header. |
75 | */ | 75 | */ |
76 | struct ip_header | 76 | struct ip_header { |
77 | { | ||
78 | |||
79 | /** | 77 | /** |
80 | * Version (4 bits) + Internet header length (4 bits) | 78 | * Version (4 bits) + Internet header length (4 bits) |
81 | */ | 79 | */ |
@@ -131,8 +129,7 @@ struct ip_header | |||
131 | /** | 129 | /** |
132 | * Format of ICMP packet. | 130 | * Format of ICMP packet. |
133 | */ | 131 | */ |
134 | struct icmp_ttl_exceeded_header | 132 | struct icmp_ttl_exceeded_header { |
135 | { | ||
136 | uint8_t type; | 133 | uint8_t type; |
137 | 134 | ||
138 | uint8_t code; | 135 | uint8_t code; |
@@ -144,8 +141,7 @@ struct icmp_ttl_exceeded_header | |||
144 | /* followed by original payload */ | 141 | /* followed by original payload */ |
145 | }; | 142 | }; |
146 | 143 | ||
147 | struct icmp_echo_header | 144 | struct icmp_echo_header { |
148 | { | ||
149 | uint8_t type; | 145 | uint8_t type; |
150 | 146 | ||
151 | uint8_t code; | 147 | uint8_t code; |
@@ -158,8 +154,7 @@ struct icmp_echo_header | |||
158 | /** | 154 | /** |
159 | * Beginning of UDP packet. | 155 | * Beginning of UDP packet. |
160 | */ | 156 | */ |
161 | struct udp_header | 157 | struct udp_header { |
162 | { | ||
163 | uint16_t src_port; | 158 | uint16_t src_port; |
164 | 159 | ||
165 | uint16_t dst_port; | 160 | uint16_t dst_port; |
@@ -200,14 +195,14 @@ static uint16_t port; | |||
200 | * @return 1 on success | 195 | * @return 1 on success |
201 | */ | 196 | */ |
202 | static int | 197 | static int |
203 | inet_pton (int af, const char *cp, struct in_addr *buf) | 198 | inet_pton(int af, const char *cp, struct in_addr *buf) |
204 | { | 199 | { |
205 | buf->s_addr = inet_addr (cp); | 200 | buf->s_addr = inet_addr(cp); |
206 | if (buf->s_addr == INADDR_NONE) | 201 | if (buf->s_addr == INADDR_NONE) |
207 | { | 202 | { |
208 | fprintf (stderr, "Error %d handling address %s", WSAGetLastError (), cp); | 203 | fprintf(stderr, "Error %d handling address %s", WSAGetLastError(), cp); |
209 | return 0; | 204 | return 0; |
210 | } | 205 | } |
211 | return 1; | 206 | return 1; |
212 | } | 207 | } |
213 | 208 | ||
@@ -220,7 +215,7 @@ inet_pton (int af, const char *cp, struct in_addr *buf) | |||
220 | * @return the CRC 16. | 215 | * @return the CRC 16. |
221 | */ | 216 | */ |
222 | static uint16_t | 217 | static uint16_t |
223 | calc_checksum (const uint16_t * data, unsigned int bytes) | 218 | calc_checksum(const uint16_t * data, unsigned int bytes) |
224 | { | 219 | { |
225 | uint32_t sum; | 220 | uint32_t sum; |
226 | unsigned int i; | 221 | unsigned int i; |
@@ -229,7 +224,7 @@ calc_checksum (const uint16_t * data, unsigned int bytes) | |||
229 | for (i = 0; i < bytes / 2; i++) | 224 | for (i = 0; i < bytes / 2; i++) |
230 | sum += data[i]; | 225 | sum += data[i]; |
231 | sum = (sum & 0xffff) + (sum >> 16); | 226 | sum = (sum & 0xffff) + (sum >> 16); |
232 | sum = htons (0xffff - sum); | 227 | sum = htons(0xffff - sum); |
233 | return sum; | 228 | return sum; |
234 | } | 229 | } |
235 | 230 | ||
@@ -241,11 +236,11 @@ calc_checksum (const uint16_t * data, unsigned int bytes) | |||
241 | * @param other target address | 236 | * @param other target address |
242 | */ | 237 | */ |
243 | static void | 238 | static void |
244 | send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) | 239 | send_icmp_udp(const struct in_addr *my_ip, const struct in_addr *other) |
245 | { | 240 | { |
246 | char packet[sizeof (struct ip_header) * 2 + | 241 | char packet[sizeof(struct ip_header) * 2 + |
247 | sizeof (struct icmp_ttl_exceeded_header) + | 242 | sizeof(struct icmp_ttl_exceeded_header) + |
248 | sizeof (struct udp_header)]; | 243 | sizeof(struct udp_header)]; |
249 | struct ip_header ip_pkt; | 244 | struct ip_header ip_pkt; |
250 | struct icmp_ttl_exceeded_header icmp_pkt; | 245 | struct icmp_ttl_exceeded_header icmp_pkt; |
251 | struct udp_header udp_pkt; | 246 | struct udp_header udp_pkt; |
@@ -257,8 +252,8 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) | |||
257 | off = 0; | 252 | off = 0; |
258 | ip_pkt.vers_ihl = 0x45; | 253 | ip_pkt.vers_ihl = 0x45; |
259 | ip_pkt.tos = 0; | 254 | ip_pkt.tos = 0; |
260 | ip_pkt.pkt_len = htons (sizeof (packet)); | 255 | ip_pkt.pkt_len = htons(sizeof(packet)); |
261 | ip_pkt.id = htons (256); | 256 | ip_pkt.id = htons(256); |
262 | ip_pkt.flags_frag_offset = 0; | 257 | ip_pkt.flags_frag_offset = 0; |
263 | ip_pkt.ttl = 128; | 258 | ip_pkt.ttl = 128; |
264 | ip_pkt.proto = IPPROTO_ICMP; | 259 | ip_pkt.proto = IPPROTO_ICMP; |
@@ -266,23 +261,23 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) | |||
266 | ip_pkt.src_ip = my_ip->s_addr; | 261 | ip_pkt.src_ip = my_ip->s_addr; |
267 | ip_pkt.dst_ip = other->s_addr; | 262 | ip_pkt.dst_ip = other->s_addr; |
268 | ip_pkt.checksum = | 263 | ip_pkt.checksum = |
269 | htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); | 264 | htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header))); |
270 | GNUNET_memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); | 265 | GNUNET_memcpy(&packet[off], &ip_pkt, sizeof(struct ip_header)); |
271 | off += sizeof (struct ip_header); | 266 | off += sizeof(struct ip_header); |
272 | 267 | ||
273 | icmp_pkt.type = ICMP_TIME_EXCEEDED; | 268 | icmp_pkt.type = ICMP_TIME_EXCEEDED; |
274 | icmp_pkt.code = 0; | 269 | icmp_pkt.code = 0; |
275 | icmp_pkt.checksum = 0; | 270 | icmp_pkt.checksum = 0; |
276 | icmp_pkt.unused = 0; | 271 | icmp_pkt.unused = 0; |
277 | GNUNET_memcpy (&packet[off], &icmp_pkt, sizeof (struct icmp_ttl_exceeded_header)); | 272 | GNUNET_memcpy(&packet[off], &icmp_pkt, sizeof(struct icmp_ttl_exceeded_header)); |
278 | off += sizeof (struct icmp_ttl_exceeded_header); | 273 | off += sizeof(struct icmp_ttl_exceeded_header); |
279 | 274 | ||
280 | /* ip header of the presumably 'lost' udp packet */ | 275 | /* ip header of the presumably 'lost' udp packet */ |
281 | ip_pkt.vers_ihl = 0x45; | 276 | ip_pkt.vers_ihl = 0x45; |
282 | ip_pkt.tos = 0; | 277 | ip_pkt.tos = 0; |
283 | ip_pkt.pkt_len = | 278 | ip_pkt.pkt_len = |
284 | htons (sizeof (struct ip_header) + sizeof (struct udp_header)); | 279 | htons(sizeof(struct ip_header) + sizeof(struct udp_header)); |
285 | ip_pkt.id = htons (0); | 280 | ip_pkt.id = htons(0); |
286 | ip_pkt.flags_frag_offset = 0; | 281 | ip_pkt.flags_frag_offset = 0; |
287 | ip_pkt.ttl = 128; | 282 | ip_pkt.ttl = 128; |
288 | ip_pkt.proto = IPPROTO_UDP; | 283 | ip_pkt.proto = IPPROTO_UDP; |
@@ -290,41 +285,41 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) | |||
290 | ip_pkt.src_ip = other->s_addr; | 285 | ip_pkt.src_ip = other->s_addr; |
291 | ip_pkt.dst_ip = dummy.s_addr; | 286 | ip_pkt.dst_ip = dummy.s_addr; |
292 | ip_pkt.checksum = | 287 | ip_pkt.checksum = |
293 | htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); | 288 | htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header))); |
294 | GNUNET_memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); | 289 | GNUNET_memcpy(&packet[off], &ip_pkt, sizeof(struct ip_header)); |
295 | off += sizeof (struct ip_header); | 290 | off += sizeof(struct ip_header); |
296 | 291 | ||
297 | /* build UDP header */ | 292 | /* build UDP header */ |
298 | udp_pkt.src_port = htons (NAT_TRAV_PORT); | 293 | udp_pkt.src_port = htons(NAT_TRAV_PORT); |
299 | udp_pkt.dst_port = htons (NAT_TRAV_PORT); | 294 | udp_pkt.dst_port = htons(NAT_TRAV_PORT); |
300 | udp_pkt.length = htons (port); | 295 | udp_pkt.length = htons(port); |
301 | udp_pkt.crc = 0; | 296 | udp_pkt.crc = 0; |
302 | GNUNET_memcpy (&packet[off], &udp_pkt, sizeof (struct udp_header)); | 297 | GNUNET_memcpy(&packet[off], &udp_pkt, sizeof(struct udp_header)); |
303 | off += sizeof (struct udp_header); | 298 | off += sizeof(struct udp_header); |
304 | 299 | ||
305 | /* no go back to calculate ICMP packet checksum */ | 300 | /* no go back to calculate ICMP packet checksum */ |
306 | icmp_pkt.checksum = | 301 | icmp_pkt.checksum = |
307 | htons (calc_checksum | 302 | htons(calc_checksum |
308 | ((uint16_t *) & packet[off], | 303 | ((uint16_t *)&packet[off], |
309 | sizeof (struct icmp_ttl_exceeded_header) + | 304 | sizeof(struct icmp_ttl_exceeded_header) + |
310 | sizeof (struct ip_header) + sizeof (struct udp_header))); | 305 | sizeof(struct ip_header) + sizeof(struct udp_header))); |
311 | GNUNET_memcpy (&packet[sizeof (struct ip_header)], &icmp_pkt, | 306 | GNUNET_memcpy(&packet[sizeof(struct ip_header)], &icmp_pkt, |
312 | sizeof (struct icmp_ttl_exceeded_header)); | 307 | sizeof(struct icmp_ttl_exceeded_header)); |
313 | 308 | ||
314 | memset (&dst, 0, sizeof (dst)); | 309 | memset(&dst, 0, sizeof(dst)); |
315 | dst.sin_family = AF_INET; | 310 | dst.sin_family = AF_INET; |
316 | dst.sin_addr = *other; | 311 | dst.sin_addr = *other; |
317 | err = | 312 | err = |
318 | sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst, | 313 | sendto(rawsock, packet, sizeof(packet), 0, (struct sockaddr *)&dst, |
319 | sizeof (dst)); | 314 | sizeof(dst)); |
320 | if (err < 0) | 315 | if (err < 0) |
321 | { | 316 | { |
322 | fprintf (stderr, "sendto failed: %s\n", strerror (errno)); | 317 | fprintf(stderr, "sendto failed: %s\n", strerror(errno)); |
323 | } | 318 | } |
324 | else if (sizeof (packet) != (size_t) err) | 319 | else if (sizeof(packet) != (size_t)err) |
325 | { | 320 | { |
326 | fprintf (stderr, "Error: partial send of ICMP message\n"); | 321 | fprintf(stderr, "Error: partial send of ICMP message\n"); |
327 | } | 322 | } |
328 | } | 323 | } |
329 | 324 | ||
330 | 325 | ||
@@ -335,15 +330,15 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) | |||
335 | * @param other target address | 330 | * @param other target address |
336 | */ | 331 | */ |
337 | static void | 332 | static void |
338 | send_icmp (const struct in_addr *my_ip, const struct in_addr *other) | 333 | send_icmp(const struct in_addr *my_ip, const struct in_addr *other) |
339 | { | 334 | { |
340 | struct ip_header ip_pkt; | 335 | struct ip_header ip_pkt; |
341 | struct icmp_ttl_exceeded_header icmp_ttl; | 336 | struct icmp_ttl_exceeded_header icmp_ttl; |
342 | struct icmp_echo_header icmp_echo; | 337 | struct icmp_echo_header icmp_echo; |
343 | struct sockaddr_in dst; | 338 | struct sockaddr_in dst; |
344 | char packet[sizeof (struct ip_header) * 2 + | 339 | char packet[sizeof(struct ip_header) * 2 + |
345 | sizeof (struct icmp_ttl_exceeded_header) + | 340 | sizeof(struct icmp_ttl_exceeded_header) + |
346 | sizeof (struct icmp_echo_header)]; | 341 | sizeof(struct icmp_echo_header)]; |
347 | size_t off; | 342 | size_t off; |
348 | int err; | 343 | int err; |
349 | 344 | ||
@@ -351,8 +346,8 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other) | |||
351 | off = 0; | 346 | off = 0; |
352 | ip_pkt.vers_ihl = 0x45; | 347 | ip_pkt.vers_ihl = 0x45; |
353 | ip_pkt.tos = 0; | 348 | ip_pkt.tos = 0; |
354 | ip_pkt.pkt_len = htons (sizeof (packet)); | 349 | ip_pkt.pkt_len = htons(sizeof(packet)); |
355 | ip_pkt.id = htons (256); | 350 | ip_pkt.id = htons(256); |
356 | ip_pkt.flags_frag_offset = 0; | 351 | ip_pkt.flags_frag_offset = 0; |
357 | ip_pkt.ttl = IPDEFTTL; | 352 | ip_pkt.ttl = IPDEFTTL; |
358 | ip_pkt.proto = IPPROTO_ICMP; | 353 | ip_pkt.proto = IPPROTO_ICMP; |
@@ -360,24 +355,24 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other) | |||
360 | ip_pkt.src_ip = my_ip->s_addr; | 355 | ip_pkt.src_ip = my_ip->s_addr; |
361 | ip_pkt.dst_ip = other->s_addr; | 356 | ip_pkt.dst_ip = other->s_addr; |
362 | ip_pkt.checksum = | 357 | ip_pkt.checksum = |
363 | htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); | 358 | htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header))); |
364 | GNUNET_memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); | 359 | GNUNET_memcpy(&packet[off], &ip_pkt, sizeof(struct ip_header)); |
365 | off += sizeof (ip_pkt); | 360 | off += sizeof(ip_pkt); |
366 | 361 | ||
367 | /* icmp reply: time exceeded */ | 362 | /* icmp reply: time exceeded */ |
368 | icmp_ttl.type = ICMP_TIME_EXCEEDED; | 363 | icmp_ttl.type = ICMP_TIME_EXCEEDED; |
369 | icmp_ttl.code = 0; | 364 | icmp_ttl.code = 0; |
370 | icmp_ttl.checksum = 0; | 365 | icmp_ttl.checksum = 0; |
371 | icmp_ttl.unused = 0; | 366 | icmp_ttl.unused = 0; |
372 | GNUNET_memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header)); | 367 | GNUNET_memcpy(&packet[off], &icmp_ttl, sizeof(struct icmp_ttl_exceeded_header)); |
373 | off += sizeof (struct icmp_ttl_exceeded_header); | 368 | off += sizeof(struct icmp_ttl_exceeded_header); |
374 | 369 | ||
375 | /* ip header of the presumably 'lost' udp packet */ | 370 | /* ip header of the presumably 'lost' udp packet */ |
376 | ip_pkt.vers_ihl = 0x45; | 371 | ip_pkt.vers_ihl = 0x45; |
377 | ip_pkt.tos = 0; | 372 | ip_pkt.tos = 0; |
378 | ip_pkt.pkt_len = | 373 | ip_pkt.pkt_len = |
379 | htons (sizeof (struct ip_header) + sizeof (struct icmp_echo_header)); | 374 | htons(sizeof(struct ip_header) + sizeof(struct icmp_echo_header)); |
380 | ip_pkt.id = htons (256); | 375 | ip_pkt.id = htons(256); |
381 | ip_pkt.flags_frag_offset = 0; | 376 | ip_pkt.flags_frag_offset = 0; |
382 | ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */ | 377 | ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */ |
383 | ip_pkt.proto = IPPROTO_ICMP; | 378 | ip_pkt.proto = IPPROTO_ICMP; |
@@ -385,44 +380,44 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other) | |||
385 | ip_pkt.dst_ip = dummy.s_addr; | 380 | ip_pkt.dst_ip = dummy.s_addr; |
386 | ip_pkt.checksum = 0; | 381 | ip_pkt.checksum = 0; |
387 | ip_pkt.checksum = | 382 | ip_pkt.checksum = |
388 | htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); | 383 | htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header))); |
389 | GNUNET_memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); | 384 | GNUNET_memcpy(&packet[off], &ip_pkt, sizeof(struct ip_header)); |
390 | off += sizeof (struct ip_header); | 385 | off += sizeof(struct ip_header); |
391 | 386 | ||
392 | icmp_echo.type = ICMP_ECHO; | 387 | icmp_echo.type = ICMP_ECHO; |
393 | icmp_echo.code = 0; | 388 | icmp_echo.code = 0; |
394 | icmp_echo.reserved = htonl (port); | 389 | icmp_echo.reserved = htonl(port); |
395 | icmp_echo.checksum = 0; | 390 | icmp_echo.checksum = 0; |
396 | icmp_echo.checksum = | 391 | icmp_echo.checksum = |
397 | htons (calc_checksum | 392 | htons(calc_checksum |
398 | ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header))); | 393 | ((uint16_t *)&icmp_echo, sizeof(struct icmp_echo_header))); |
399 | GNUNET_memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header)); | 394 | GNUNET_memcpy(&packet[off], &icmp_echo, sizeof(struct icmp_echo_header)); |
400 | 395 | ||
401 | /* no go back to calculate ICMP packet checksum */ | 396 | /* no go back to calculate ICMP packet checksum */ |
402 | off = sizeof (struct ip_header); | 397 | off = sizeof(struct ip_header); |
403 | icmp_ttl.checksum = | 398 | icmp_ttl.checksum = |
404 | htons (calc_checksum | 399 | htons(calc_checksum |
405 | ((uint16_t *) & packet[off], | 400 | ((uint16_t *)&packet[off], |
406 | sizeof (struct icmp_ttl_exceeded_header) + | 401 | sizeof(struct icmp_ttl_exceeded_header) + |
407 | sizeof (struct ip_header) + sizeof (struct icmp_echo_header))); | 402 | sizeof(struct ip_header) + sizeof(struct icmp_echo_header))); |
408 | GNUNET_memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header)); | 403 | GNUNET_memcpy(&packet[off], &icmp_ttl, sizeof(struct icmp_ttl_exceeded_header)); |
409 | 404 | ||
410 | memset (&dst, 0, sizeof (dst)); | 405 | memset(&dst, 0, sizeof(dst)); |
411 | dst.sin_family = AF_INET; | 406 | dst.sin_family = AF_INET; |
412 | dst.sin_addr = *other; | 407 | dst.sin_addr = *other; |
413 | 408 | ||
414 | err = | 409 | err = |
415 | sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst, | 410 | sendto(rawsock, packet, sizeof(packet), 0, (struct sockaddr *)&dst, |
416 | sizeof (dst)); | 411 | sizeof(dst)); |
417 | 412 | ||
418 | if (err < 0) | 413 | if (err < 0) |
419 | { | 414 | { |
420 | fprintf (stderr, "sendto failed: %s\n", strerror (errno)); | 415 | fprintf(stderr, "sendto failed: %s\n", strerror(errno)); |
421 | } | 416 | } |
422 | else if (sizeof (packet) != (size_t) err) | 417 | else if (sizeof(packet) != (size_t)err) |
423 | { | 418 | { |
424 | fprintf (stderr, "Error: partial send of ICMP message\n"); | 419 | fprintf(stderr, "Error: partial send of ICMP message\n"); |
425 | } | 420 | } |
426 | } | 421 | } |
427 | 422 | ||
428 | 423 | ||
@@ -432,100 +427,102 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other) | |||
432 | * @return INVALID_SOCKET on error | 427 | * @return INVALID_SOCKET on error |
433 | */ | 428 | */ |
434 | static SOCKET | 429 | static SOCKET |
435 | make_raw_socket () | 430 | make_raw_socket() |
436 | { | 431 | { |
437 | DWORD bOptVal = TRUE; | 432 | DWORD bOptVal = TRUE; |
438 | int bOptLen = sizeof (bOptVal); | 433 | int bOptLen = sizeof(bOptVal); |
439 | SOCKET ret; | 434 | SOCKET ret; |
440 | 435 | ||
441 | ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW); | 436 | ret = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); |
442 | if (INVALID_SOCKET == ret) | 437 | if (INVALID_SOCKET == ret) |
443 | { | 438 | { |
444 | fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno)); | 439 | fprintf(stderr, "Error opening RAW socket: %s\n", strerror(errno)); |
445 | return INVALID_SOCKET; | 440 | return INVALID_SOCKET; |
446 | } | 441 | } |
447 | if (0 != | 442 | if (0 != |
448 | setsockopt (ret, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal, bOptLen)) | 443 | setsockopt(ret, SOL_SOCKET, SO_BROADCAST, (char *)&bOptVal, bOptLen)) |
449 | { | 444 | { |
450 | fprintf (stderr, "Error setting SO_BROADCAST to ON: %s\n", | 445 | fprintf(stderr, "Error setting SO_BROADCAST to ON: %s\n", |
451 | strerror (errno)); | 446 | strerror(errno)); |
452 | closesocket (rawsock); | 447 | closesocket(rawsock); |
453 | return INVALID_SOCKET; | 448 | return INVALID_SOCKET; |
454 | } | 449 | } |
455 | 450 | ||
456 | if (0 != setsockopt (ret, IPPROTO_IP, IP_HDRINCL, (char *) &bOptVal, bOptLen)) | 451 | if (0 != setsockopt(ret, IPPROTO_IP, IP_HDRINCL, (char *)&bOptVal, bOptLen)) |
457 | { | 452 | { |
458 | fprintf (stderr, "Error setting IP_HDRINCL to ON: %s\n", strerror (errno)); | 453 | fprintf(stderr, "Error setting IP_HDRINCL to ON: %s\n", strerror(errno)); |
459 | closesocket (rawsock); | 454 | closesocket(rawsock); |
460 | return INVALID_SOCKET; | 455 | return INVALID_SOCKET; |
461 | } | 456 | } |
462 | return ret; | 457 | return ret; |
463 | } | 458 | } |
464 | 459 | ||
465 | 460 | ||
466 | int | 461 | int |
467 | main (int argc, char *const *argv) | 462 | main(int argc, char *const *argv) |
468 | { | 463 | { |
469 | struct in_addr external; | 464 | struct in_addr external; |
470 | struct in_addr target; | 465 | struct in_addr target; |
471 | WSADATA wsaData; | 466 | WSADATA wsaData; |
472 | unsigned int p; | 467 | unsigned int p; |
473 | 468 | ||
474 | if (argc > 1 && 0 != strcmp (argv[1], "-d")){ | 469 | if (argc > 1 && 0 != strcmp(argv[1], "-d")) |
470 | { | ||
475 | privilege_testing = TRUE; | 471 | privilege_testing = TRUE; |
476 | fprintf (stderr, | 472 | fprintf(stderr, |
477 | "%s", | 473 | "%s", |
478 | "DEBUG: Running binary in privilege testing mode."); | 474 | "DEBUG: Running binary in privilege testing mode."); |
479 | argv++; | 475 | argv++; |
480 | argc--; | 476 | argc--; |
481 | } | 477 | } |
482 | 478 | ||
483 | if (argc != 4) | 479 | if (argc != 4) |
484 | { | 480 | { |
485 | fprintf (stderr, | 481 | fprintf(stderr, |
486 | "%s", | 482 | "%s", |
487 | "This program must be started with our IP, the targets external IP, and our port as arguments.\n"); | 483 | "This program must be started with our IP, the targets external IP, and our port as arguments.\n"); |
488 | return 1; | 484 | return 1; |
489 | } | 485 | } |
490 | if ((1 != inet_pton (AF_INET, argv[1], &external)) || | 486 | if ((1 != inet_pton(AF_INET, argv[1], &external)) || |
491 | (1 != inet_pton (AF_INET, argv[2], &target))) | 487 | (1 != inet_pton(AF_INET, argv[2], &target))) |
492 | { | 488 | { |
493 | fprintf (stderr, | 489 | fprintf(stderr, |
494 | "Error parsing IPv4 address: %s\n", | 490 | "Error parsing IPv4 address: %s\n", |
495 | strerror (errno)); | 491 | strerror(errno)); |
496 | return 1; | 492 | return 1; |
497 | } | 493 | } |
498 | if ((1 != sscanf (argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p)) | 494 | if ((1 != sscanf(argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p)) |
499 | { | 495 | { |
500 | fprintf (stderr, | 496 | fprintf(stderr, |
501 | "Error parsing port value `%s'\n", | 497 | "Error parsing port value `%s'\n", |
502 | argv[3]); | 498 | argv[3]); |
503 | return 1; | 499 | return 1; |
504 | } | 500 | } |
505 | port = (uint16_t) p; | 501 | port = (uint16_t)p; |
506 | 502 | ||
507 | if (0 != WSAStartup (MAKEWORD (2, 1), &wsaData)) | 503 | if (0 != WSAStartup(MAKEWORD(2, 1), &wsaData)) |
508 | { | 504 | { |
509 | fprintf (stderr, | 505 | fprintf(stderr, |
510 | "%s", | 506 | "%s", |
511 | "Failed to find Winsock 2.1 or better.\n"); | 507 | "Failed to find Winsock 2.1 or better.\n"); |
512 | return 2; | 508 | return 2; |
513 | } | 509 | } |
514 | if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy)) | 510 | if (1 != inet_pton(AF_INET, DUMMY_IP, &dummy)) |
515 | { | 511 | { |
516 | fprintf (stderr, | 512 | fprintf(stderr, |
517 | "%s", | 513 | "%s", |
518 | "Internal error converting dummy IP to binary.\n"); | 514 | "Internal error converting dummy IP to binary.\n"); |
519 | return 2; | 515 | return 2; |
520 | } | 516 | } |
521 | if (-1 == (rawsock = make_raw_socket ())) | 517 | if (-1 == (rawsock = make_raw_socket())) |
522 | return 3; | 518 | return 3; |
523 | if (!privilege_testing){ | 519 | if (!privilege_testing) |
524 | send_icmp (&external, &target); | 520 | { |
525 | send_icmp_udp (&external, &target); | 521 | send_icmp(&external, &target); |
526 | } | 522 | send_icmp_udp(&external, &target); |
527 | closesocket (rawsock); | 523 | } |
528 | WSACleanup (); | 524 | closesocket(rawsock); |
525 | WSACleanup(); | ||
529 | return 0; | 526 | return 0; |
530 | } | 527 | } |
531 | 528 | ||