diff options
author | Christian Grothoff <christian@grothoff.org> | 2019-10-05 15:09:28 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2019-10-05 15:09:28 +0200 |
commit | c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207 (patch) | |
tree | cac3ce030d77b4cbe7c7dc62ed58cfe6d24f73e1 /src/nat/gnunet-helper-nat-client.c | |
parent | fbb71d527c7d6babf269a8fefce1db291b9f7068 (diff) | |
download | gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.tar.gz gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.zip |
global reindent, now with uncrustify hook enabled
Diffstat (limited to 'src/nat/gnunet-helper-nat-client.c')
-rw-r--r-- | src/nat/gnunet-helper-nat-client.c | 314 |
1 files changed, 161 insertions, 153 deletions
diff --git a/src/nat/gnunet-helper-nat-client.c b/src/nat/gnunet-helper-nat-client.c index d5d774964..5d12092e7 100644 --- a/src/nat/gnunet-helper-nat-client.c +++ b/src/nat/gnunet-helper-nat-client.c | |||
@@ -75,7 +75,9 @@ | |||
75 | * @param src source of the copy, may be NULL if @a n is zero | 75 | * @param src source of the copy, may be NULL if @a n is zero |
76 | * @param n number of bytes to copy | 76 | * @param n number of bytes to copy |
77 | */ | 77 | */ |
78 | #define GNUNET_memcpy(dst, src, n) do { if (0 != n) { (void)memcpy(dst, src, n); } } while (0) | 78 | #define GNUNET_memcpy(dst, src, n) do { if (0 != n) { (void) memcpy (dst, src, \ |
79 | n); \ | ||
80 | } } while (0) | ||
79 | 81 | ||
80 | /** | 82 | /** |
81 | * Must match IP given in the server. | 83 | * Must match IP given in the server. |
@@ -92,7 +94,8 @@ | |||
92 | /** | 94 | /** |
93 | * IPv4 header. | 95 | * IPv4 header. |
94 | */ | 96 | */ |
95 | struct ip_header { | 97 | struct ip_header |
98 | { | ||
96 | /** | 99 | /** |
97 | * Version (4 bits) + Internet header length (4 bits) | 100 | * Version (4 bits) + Internet header length (4 bits) |
98 | */ | 101 | */ |
@@ -147,7 +150,8 @@ struct ip_header { | |||
147 | /** | 150 | /** |
148 | * Format of ICMP packet. | 151 | * Format of ICMP packet. |
149 | */ | 152 | */ |
150 | struct icmp_ttl_exceeded_header { | 153 | struct icmp_ttl_exceeded_header |
154 | { | ||
151 | uint8_t type; | 155 | uint8_t type; |
152 | 156 | ||
153 | uint8_t code; | 157 | uint8_t code; |
@@ -159,7 +163,8 @@ struct icmp_ttl_exceeded_header { | |||
159 | /* followed by original payload */ | 163 | /* followed by original payload */ |
160 | }; | 164 | }; |
161 | 165 | ||
162 | struct icmp_echo_header { | 166 | struct icmp_echo_header |
167 | { | ||
163 | uint8_t type; | 168 | uint8_t type; |
164 | 169 | ||
165 | uint8_t code; | 170 | uint8_t code; |
@@ -172,7 +177,8 @@ struct icmp_echo_header { | |||
172 | /** | 177 | /** |
173 | * Beginning of UDP packet. | 178 | * Beginning of UDP packet. |
174 | */ | 179 | */ |
175 | struct udp_header { | 180 | struct udp_header |
181 | { | ||
176 | uint16_t src_port; | 182 | uint16_t src_port; |
177 | 183 | ||
178 | uint16_t dst_port; | 184 | uint16_t dst_port; |
@@ -206,7 +212,7 @@ static uint16_t port; | |||
206 | * @return the CRC 16. | 212 | * @return the CRC 16. |
207 | */ | 213 | */ |
208 | static uint16_t | 214 | static uint16_t |
209 | calc_checksum(const uint16_t * data, unsigned int bytes) | 215 | calc_checksum (const uint16_t *data, unsigned int bytes) |
210 | { | 216 | { |
211 | uint32_t sum; | 217 | uint32_t sum; |
212 | unsigned int i; | 218 | unsigned int i; |
@@ -215,7 +221,7 @@ calc_checksum(const uint16_t * data, unsigned int bytes) | |||
215 | for (i = 0; i < bytes / 2; i++) | 221 | for (i = 0; i < bytes / 2; i++) |
216 | sum += data[i]; | 222 | sum += data[i]; |
217 | sum = (sum & 0xffff) + (sum >> 16); | 223 | sum = (sum & 0xffff) + (sum >> 16); |
218 | sum = htons(0xffff - sum); | 224 | sum = htons (0xffff - sum); |
219 | return sum; | 225 | return sum; |
220 | } | 226 | } |
221 | 227 | ||
@@ -227,11 +233,11 @@ calc_checksum(const uint16_t * data, unsigned int bytes) | |||
227 | * @param other target address | 233 | * @param other target address |
228 | */ | 234 | */ |
229 | static void | 235 | static void |
230 | send_icmp_udp(const struct in_addr *my_ip, const struct in_addr *other) | 236 | send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) |
231 | { | 237 | { |
232 | char packet[sizeof(struct ip_header) * 2 + | 238 | char packet[sizeof(struct ip_header) * 2 |
233 | sizeof(struct icmp_ttl_exceeded_header) + | 239 | + sizeof(struct icmp_ttl_exceeded_header) |
234 | sizeof(struct udp_header)]; | 240 | + sizeof(struct udp_header)]; |
235 | struct ip_header ip_pkt; | 241 | struct ip_header ip_pkt; |
236 | struct icmp_ttl_exceeded_header icmp_pkt; | 242 | struct icmp_ttl_exceeded_header icmp_pkt; |
237 | struct udp_header udp_pkt; | 243 | struct udp_header udp_pkt; |
@@ -246,9 +252,9 @@ send_icmp_udp(const struct in_addr *my_ip, const struct in_addr *other) | |||
246 | #ifdef FREEBSD | 252 | #ifdef FREEBSD |
247 | ip_pkt.pkt_len = sizeof(packet); /* Workaround PR kern/21737 */ | 253 | ip_pkt.pkt_len = sizeof(packet); /* Workaround PR kern/21737 */ |
248 | #else | 254 | #else |
249 | ip_pkt.pkt_len = htons(sizeof(packet)); | 255 | ip_pkt.pkt_len = htons (sizeof(packet)); |
250 | #endif | 256 | #endif |
251 | ip_pkt.id = htons(PACKET_ID); | 257 | ip_pkt.id = htons (PACKET_ID); |
252 | ip_pkt.flags_frag_offset = 0; | 258 | ip_pkt.flags_frag_offset = 0; |
253 | ip_pkt.ttl = 128; | 259 | ip_pkt.ttl = 128; |
254 | ip_pkt.proto = IPPROTO_ICMP; | 260 | ip_pkt.proto = IPPROTO_ICMP; |
@@ -256,27 +262,27 @@ send_icmp_udp(const struct in_addr *my_ip, const struct in_addr *other) | |||
256 | ip_pkt.src_ip = my_ip->s_addr; | 262 | ip_pkt.src_ip = my_ip->s_addr; |
257 | ip_pkt.dst_ip = other->s_addr; | 263 | ip_pkt.dst_ip = other->s_addr; |
258 | ip_pkt.checksum = | 264 | ip_pkt.checksum = |
259 | htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header))); | 265 | htons (calc_checksum ((uint16_t *) &ip_pkt, sizeof(struct ip_header))); |
260 | GNUNET_memcpy(&packet[off], | 266 | GNUNET_memcpy (&packet[off], |
261 | &ip_pkt, | 267 | &ip_pkt, |
262 | sizeof(struct ip_header)); | 268 | sizeof(struct ip_header)); |
263 | off += sizeof(struct ip_header); | 269 | off += sizeof(struct ip_header); |
264 | 270 | ||
265 | icmp_pkt.type = ICMP_TIME_EXCEEDED; | 271 | icmp_pkt.type = ICMP_TIME_EXCEEDED; |
266 | icmp_pkt.code = 0; | 272 | icmp_pkt.code = 0; |
267 | icmp_pkt.checksum = 0; | 273 | icmp_pkt.checksum = 0; |
268 | icmp_pkt.unused = 0; | 274 | icmp_pkt.unused = 0; |
269 | GNUNET_memcpy(&packet[off], | 275 | GNUNET_memcpy (&packet[off], |
270 | &icmp_pkt, | 276 | &icmp_pkt, |
271 | sizeof(struct icmp_ttl_exceeded_header)); | 277 | sizeof(struct icmp_ttl_exceeded_header)); |
272 | off += sizeof(struct icmp_ttl_exceeded_header); | 278 | off += sizeof(struct icmp_ttl_exceeded_header); |
273 | 279 | ||
274 | /* ip header of the presumably 'lost' udp packet */ | 280 | /* ip header of the presumably 'lost' udp packet */ |
275 | ip_pkt.vers_ihl = 0x45; | 281 | ip_pkt.vers_ihl = 0x45; |
276 | ip_pkt.tos = 0; | 282 | ip_pkt.tos = 0; |
277 | ip_pkt.pkt_len = | 283 | ip_pkt.pkt_len = |
278 | htons(sizeof(struct ip_header) + sizeof(struct udp_header)); | 284 | htons (sizeof(struct ip_header) + sizeof(struct udp_header)); |
279 | ip_pkt.id = htons(0); | 285 | ip_pkt.id = htons (0); |
280 | ip_pkt.flags_frag_offset = 0; | 286 | ip_pkt.flags_frag_offset = 0; |
281 | ip_pkt.ttl = 128; | 287 | ip_pkt.ttl = 128; |
282 | ip_pkt.proto = IPPROTO_UDP; | 288 | ip_pkt.proto = IPPROTO_UDP; |
@@ -284,49 +290,50 @@ send_icmp_udp(const struct in_addr *my_ip, const struct in_addr *other) | |||
284 | ip_pkt.src_ip = other->s_addr; | 290 | ip_pkt.src_ip = other->s_addr; |
285 | ip_pkt.dst_ip = dummy.s_addr; | 291 | ip_pkt.dst_ip = dummy.s_addr; |
286 | ip_pkt.checksum = | 292 | ip_pkt.checksum = |
287 | htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header))); | 293 | htons (calc_checksum ((uint16_t *) &ip_pkt, sizeof(struct ip_header))); |
288 | GNUNET_memcpy(&packet[off], | 294 | GNUNET_memcpy (&packet[off], |
289 | &ip_pkt, | 295 | &ip_pkt, |
290 | sizeof(struct ip_header)); | 296 | sizeof(struct ip_header)); |
291 | off += sizeof(struct ip_header); | 297 | off += sizeof(struct ip_header); |
292 | 298 | ||
293 | /* build UDP header */ | 299 | /* build UDP header */ |
294 | udp_pkt.src_port = htons(NAT_TRAV_PORT); | 300 | udp_pkt.src_port = htons (NAT_TRAV_PORT); |
295 | udp_pkt.dst_port = htons(NAT_TRAV_PORT); | 301 | udp_pkt.dst_port = htons (NAT_TRAV_PORT); |
296 | udp_pkt.length = htons(port); | 302 | udp_pkt.length = htons (port); |
297 | udp_pkt.crc = 0; | 303 | udp_pkt.crc = 0; |
298 | GNUNET_memcpy(&packet[off], | 304 | GNUNET_memcpy (&packet[off], |
299 | &udp_pkt, | 305 | &udp_pkt, |
300 | sizeof(struct udp_header)); | 306 | sizeof(struct udp_header)); |
301 | off += sizeof(struct udp_header); | 307 | off += sizeof(struct udp_header); |
302 | 308 | ||
303 | /* set ICMP checksum */ | 309 | /* set ICMP checksum */ |
304 | icmp_pkt.checksum = | 310 | icmp_pkt.checksum = |
305 | htons(calc_checksum | 311 | htons (calc_checksum |
306 | ((uint16_t *)&packet[sizeof(struct ip_header)], | 312 | ((uint16_t *) &packet[sizeof(struct ip_header)], |
307 | sizeof(struct icmp_ttl_exceeded_header) + | 313 | sizeof(struct icmp_ttl_exceeded_header) |
308 | sizeof(struct ip_header) + sizeof(struct udp_header))); | 314 | + sizeof(struct ip_header) + sizeof(struct udp_header))); |
309 | GNUNET_memcpy(&packet[sizeof(struct ip_header)], | 315 | GNUNET_memcpy (&packet[sizeof(struct ip_header)], |
310 | &icmp_pkt, | 316 | &icmp_pkt, |
311 | sizeof(struct icmp_ttl_exceeded_header)); | 317 | sizeof(struct icmp_ttl_exceeded_header)); |
312 | 318 | ||
313 | memset(&dst, 0, sizeof(dst)); | 319 | memset (&dst, 0, sizeof(dst)); |
314 | dst.sin_family = AF_INET; | 320 | dst.sin_family = AF_INET; |
315 | #if HAVE_SOCKADDR_IN_SIN_LEN | 321 | #if HAVE_SOCKADDR_IN_SIN_LEN |
316 | dst.sin_len = sizeof(struct sockaddr_in); | 322 | dst.sin_len = sizeof(struct sockaddr_in); |
317 | #endif | 323 | #endif |
318 | dst.sin_addr = *other; | 324 | dst.sin_addr = *other; |
319 | err = | 325 | err = |
320 | sendto(rawsock, packet, sizeof(packet), 0, (struct sockaddr *)&dst, | 326 | sendto (rawsock, packet, sizeof(packet), 0, (struct sockaddr *) &dst, |
321 | sizeof(dst)); | 327 | sizeof(dst)); |
322 | if (err < 0) | 328 | if (err < 0) |
323 | { | 329 | { |
324 | fprintf(stderr, "sendto failed: %s\n", strerror(errno)); | 330 | fprintf (stderr, "sendto failed: %s\n", strerror (errno)); |
325 | } | 331 | } |
326 | else if (sizeof(packet) != (size_t)err) | 332 | else if (sizeof(packet) != (size_t) err) |
327 | { | 333 | { |
328 | fprintf(stderr, "Error: partial send of ICMP message with size %lu\n", (unsigned long)off); | 334 | fprintf (stderr, "Error: partial send of ICMP message with size %lu\n", |
329 | } | 335 | (unsigned long) off); |
336 | } | ||
330 | } | 337 | } |
331 | 338 | ||
332 | 339 | ||
@@ -337,15 +344,15 @@ send_icmp_udp(const struct in_addr *my_ip, const struct in_addr *other) | |||
337 | * @param other target address | 344 | * @param other target address |
338 | */ | 345 | */ |
339 | static void | 346 | static void |
340 | send_icmp(const struct in_addr *my_ip, const struct in_addr *other) | 347 | send_icmp (const struct in_addr *my_ip, const struct in_addr *other) |
341 | { | 348 | { |
342 | struct ip_header ip_pkt; | 349 | struct ip_header ip_pkt; |
343 | struct icmp_ttl_exceeded_header icmp_ttl; | 350 | struct icmp_ttl_exceeded_header icmp_ttl; |
344 | struct icmp_echo_header icmp_echo; | 351 | struct icmp_echo_header icmp_echo; |
345 | struct sockaddr_in dst; | 352 | struct sockaddr_in dst; |
346 | char packet[sizeof(struct ip_header) * 2 + | 353 | char packet[sizeof(struct ip_header) * 2 |
347 | sizeof(struct icmp_ttl_exceeded_header) + | 354 | + sizeof(struct icmp_ttl_exceeded_header) |
348 | sizeof(struct icmp_echo_header)]; | 355 | + sizeof(struct icmp_echo_header)]; |
349 | size_t off; | 356 | size_t off; |
350 | int err; | 357 | int err; |
351 | 358 | ||
@@ -356,9 +363,9 @@ send_icmp(const struct in_addr *my_ip, const struct in_addr *other) | |||
356 | #ifdef FREEBSD | 363 | #ifdef FREEBSD |
357 | ip_pkt.pkt_len = sizeof(packet); /* Workaround PR kern/21737 */ | 364 | ip_pkt.pkt_len = sizeof(packet); /* Workaround PR kern/21737 */ |
358 | #else | 365 | #else |
359 | ip_pkt.pkt_len = htons(sizeof(packet)); | 366 | ip_pkt.pkt_len = htons (sizeof(packet)); |
360 | #endif | 367 | #endif |
361 | ip_pkt.id = htons(PACKET_ID); | 368 | ip_pkt.id = htons (PACKET_ID); |
362 | ip_pkt.flags_frag_offset = 0; | 369 | ip_pkt.flags_frag_offset = 0; |
363 | ip_pkt.ttl = IPDEFTTL; | 370 | ip_pkt.ttl = IPDEFTTL; |
364 | ip_pkt.proto = IPPROTO_ICMP; | 371 | ip_pkt.proto = IPPROTO_ICMP; |
@@ -366,10 +373,10 @@ send_icmp(const struct in_addr *my_ip, const struct in_addr *other) | |||
366 | ip_pkt.src_ip = my_ip->s_addr; | 373 | ip_pkt.src_ip = my_ip->s_addr; |
367 | ip_pkt.dst_ip = other->s_addr; | 374 | ip_pkt.dst_ip = other->s_addr; |
368 | ip_pkt.checksum = | 375 | ip_pkt.checksum = |
369 | htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header))); | 376 | htons (calc_checksum ((uint16_t *) &ip_pkt, sizeof(struct ip_header))); |
370 | GNUNET_memcpy(&packet[off], | 377 | GNUNET_memcpy (&packet[off], |
371 | &ip_pkt, | 378 | &ip_pkt, |
372 | sizeof(struct ip_header)); | 379 | sizeof(struct ip_header)); |
373 | off = sizeof(ip_pkt); | 380 | off = sizeof(ip_pkt); |
374 | 381 | ||
375 | /* icmp reply: time exceeded */ | 382 | /* icmp reply: time exceeded */ |
@@ -377,17 +384,17 @@ send_icmp(const struct in_addr *my_ip, const struct in_addr *other) | |||
377 | icmp_ttl.code = 0; | 384 | icmp_ttl.code = 0; |
378 | icmp_ttl.checksum = 0; | 385 | icmp_ttl.checksum = 0; |
379 | icmp_ttl.unused = 0; | 386 | icmp_ttl.unused = 0; |
380 | GNUNET_memcpy(&packet[off], | 387 | GNUNET_memcpy (&packet[off], |
381 | &icmp_ttl, | 388 | &icmp_ttl, |
382 | sizeof(struct icmp_ttl_exceeded_header)); | 389 | sizeof(struct icmp_ttl_exceeded_header)); |
383 | off += sizeof(struct icmp_ttl_exceeded_header); | 390 | off += sizeof(struct icmp_ttl_exceeded_header); |
384 | 391 | ||
385 | /* ip header of the presumably 'lost' udp packet */ | 392 | /* ip header of the presumably 'lost' udp packet */ |
386 | ip_pkt.vers_ihl = 0x45; | 393 | ip_pkt.vers_ihl = 0x45; |
387 | ip_pkt.tos = 0; | 394 | ip_pkt.tos = 0; |
388 | ip_pkt.pkt_len = | 395 | ip_pkt.pkt_len = |
389 | htons(sizeof(struct ip_header) + sizeof(struct icmp_echo_header)); | 396 | htons (sizeof(struct ip_header) + sizeof(struct icmp_echo_header)); |
390 | ip_pkt.id = htons(PACKET_ID); | 397 | ip_pkt.id = htons (PACKET_ID); |
391 | ip_pkt.flags_frag_offset = 0; | 398 | ip_pkt.flags_frag_offset = 0; |
392 | ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */ | 399 | ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */ |
393 | ip_pkt.proto = IPPROTO_ICMP; | 400 | ip_pkt.proto = IPPROTO_ICMP; |
@@ -395,57 +402,57 @@ send_icmp(const struct in_addr *my_ip, const struct in_addr *other) | |||
395 | ip_pkt.dst_ip = dummy.s_addr; | 402 | ip_pkt.dst_ip = dummy.s_addr; |
396 | ip_pkt.checksum = 0; | 403 | ip_pkt.checksum = 0; |
397 | ip_pkt.checksum = | 404 | ip_pkt.checksum = |
398 | htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header))); | 405 | htons (calc_checksum ((uint16_t *) &ip_pkt, sizeof(struct ip_header))); |
399 | GNUNET_memcpy(&packet[off], | 406 | GNUNET_memcpy (&packet[off], |
400 | &ip_pkt, | 407 | &ip_pkt, |
401 | sizeof(struct ip_header)); | 408 | sizeof(struct ip_header)); |
402 | off += sizeof(struct ip_header); | 409 | off += sizeof(struct ip_header); |
403 | 410 | ||
404 | icmp_echo.type = ICMP_ECHO; | 411 | icmp_echo.type = ICMP_ECHO; |
405 | icmp_echo.code = 0; | 412 | icmp_echo.code = 0; |
406 | icmp_echo.reserved = htonl(port); | 413 | icmp_echo.reserved = htonl (port); |
407 | icmp_echo.checksum = 0; | 414 | icmp_echo.checksum = 0; |
408 | icmp_echo.checksum = | 415 | icmp_echo.checksum = |
409 | htons(calc_checksum | 416 | htons (calc_checksum |
410 | ((uint16_t *)&icmp_echo, sizeof(struct icmp_echo_header))); | 417 | ((uint16_t *) &icmp_echo, sizeof(struct icmp_echo_header))); |
411 | GNUNET_memcpy(&packet[off], | 418 | GNUNET_memcpy (&packet[off], |
412 | &icmp_echo, | 419 | &icmp_echo, |
413 | sizeof(struct icmp_echo_header)); | 420 | sizeof(struct icmp_echo_header)); |
414 | 421 | ||
415 | /* no go back to calculate ICMP packet checksum */ | 422 | /* no go back to calculate ICMP packet checksum */ |
416 | off = sizeof(struct ip_header); | 423 | off = sizeof(struct ip_header); |
417 | icmp_ttl.checksum = | 424 | icmp_ttl.checksum = |
418 | htons(calc_checksum | 425 | htons (calc_checksum |
419 | ((uint16_t *)&packet[off], | 426 | ((uint16_t *) &packet[off], |
420 | sizeof(struct icmp_ttl_exceeded_header) + | 427 | sizeof(struct icmp_ttl_exceeded_header) |
421 | sizeof(struct ip_header) + sizeof(struct icmp_echo_header))); | 428 | + sizeof(struct ip_header) + sizeof(struct icmp_echo_header))); |
422 | GNUNET_memcpy(&packet[off], | 429 | GNUNET_memcpy (&packet[off], |
423 | &icmp_ttl, | 430 | &icmp_ttl, |
424 | sizeof(struct icmp_ttl_exceeded_header)); | 431 | sizeof(struct icmp_ttl_exceeded_header)); |
425 | 432 | ||
426 | /* prepare for transmission */ | 433 | /* prepare for transmission */ |
427 | memset(&dst, 0, sizeof(dst)); | 434 | memset (&dst, 0, sizeof(dst)); |
428 | dst.sin_family = AF_INET; | 435 | dst.sin_family = AF_INET; |
429 | #if HAVE_SOCKADDR_IN_SIN_LEN | 436 | #if HAVE_SOCKADDR_IN_SIN_LEN |
430 | dst.sin_len = sizeof(struct sockaddr_in); | 437 | dst.sin_len = sizeof(struct sockaddr_in); |
431 | #endif | 438 | #endif |
432 | dst.sin_addr = *other; | 439 | dst.sin_addr = *other; |
433 | err = | 440 | err = |
434 | sendto(rawsock, packet, sizeof(packet), 0, (struct sockaddr *)&dst, | 441 | sendto (rawsock, packet, sizeof(packet), 0, (struct sockaddr *) &dst, |
435 | sizeof(dst)); | 442 | sizeof(dst)); |
436 | if (err < 0) | 443 | if (err < 0) |
437 | { | 444 | { |
438 | fprintf(stderr, "sendto failed: %s\n", strerror(errno)); | 445 | fprintf (stderr, "sendto failed: %s\n", strerror (errno)); |
439 | } | 446 | } |
440 | else if (sizeof(packet) != (size_t)err) | 447 | else if (sizeof(packet) != (size_t) err) |
441 | { | 448 | { |
442 | fprintf(stderr, "Error: partial send of ICMP message\n"); | 449 | fprintf (stderr, "Error: partial send of ICMP message\n"); |
443 | } | 450 | } |
444 | } | 451 | } |
445 | 452 | ||
446 | 453 | ||
447 | int | 454 | int |
448 | main(int argc, char *const *argv) | 455 | main (int argc, char *const *argv) |
449 | { | 456 | { |
450 | const int one = 1; | 457 | const int one = 1; |
451 | struct in_addr external; | 458 | struct in_addr external; |
@@ -456,80 +463,81 @@ main(int argc, char *const *argv) | |||
456 | int global_ret; | 463 | int global_ret; |
457 | 464 | ||
458 | /* Create an ICMP raw socket for writing (only operation that requires root) */ | 465 | /* Create an ICMP raw socket for writing (only operation that requires root) */ |
459 | rawsock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); | 466 | rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_RAW); |
460 | raw_eno = errno; /* for later error checking */ | 467 | raw_eno = errno; /* for later error checking */ |
461 | 468 | ||
462 | /* now drop root privileges */ | 469 | /* now drop root privileges */ |
463 | uid = getuid(); | 470 | uid = getuid (); |
464 | #ifdef HAVE_SETRESUID | 471 | #ifdef HAVE_SETRESUID |
465 | if (0 != setresuid(uid, uid, uid)) | 472 | if (0 != setresuid (uid, uid, uid)) |
466 | { | 473 | { |
467 | fprintf(stderr, "Failed to setresuid: %s\n", strerror(errno)); | 474 | fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno)); |
468 | global_ret = 1; | 475 | global_ret = 1; |
469 | goto cleanup; | 476 | goto cleanup; |
470 | } | 477 | } |
471 | #else | 478 | #else |
472 | if (0 != (setuid(uid) | seteuid(uid))) | 479 | if (0 != (setuid (uid) | seteuid (uid))) |
473 | { | 480 | { |
474 | fprintf(stderr, "Failed to setuid: %s\n", strerror(errno)); | 481 | fprintf (stderr, "Failed to setuid: %s\n", strerror (errno)); |
475 | global_ret = 2; | 482 | global_ret = 2; |
476 | goto cleanup; | 483 | goto cleanup; |
477 | } | 484 | } |
478 | #endif | 485 | #endif |
479 | if (-1 == rawsock) | 486 | if (-1 == rawsock) |
480 | { | 487 | { |
481 | fprintf(stderr, "Error opening RAW socket: %s\n", strerror(raw_eno)); | 488 | fprintf (stderr, "Error opening RAW socket: %s\n", strerror (raw_eno)); |
482 | global_ret = 3; | 489 | global_ret = 3; |
483 | goto cleanup; | 490 | goto cleanup; |
484 | } | 491 | } |
485 | if (0 != | 492 | if (0 != |
486 | setsockopt(rawsock, SOL_SOCKET, SO_BROADCAST, (char *)&one, sizeof(one))) | 493 | setsockopt (rawsock, SOL_SOCKET, SO_BROADCAST, (char *) &one, |
487 | { | 494 | sizeof(one))) |
488 | fprintf(stderr, "setsockopt failed: %s\n", strerror(errno)); | 495 | { |
489 | global_ret = 4; | 496 | fprintf (stderr, "setsockopt failed: %s\n", strerror (errno)); |
490 | goto cleanup; | 497 | global_ret = 4; |
491 | } | 498 | goto cleanup; |
499 | } | ||
492 | if (0 != | 500 | if (0 != |
493 | setsockopt(rawsock, IPPROTO_IP, IP_HDRINCL, (char *)&one, sizeof(one))) | 501 | setsockopt (rawsock, IPPROTO_IP, IP_HDRINCL, (char *) &one, sizeof(one))) |
494 | { | 502 | { |
495 | fprintf(stderr, "setsockopt failed: %s\n", strerror(errno)); | 503 | fprintf (stderr, "setsockopt failed: %s\n", strerror (errno)); |
496 | global_ret = 5; | 504 | global_ret = 5; |
497 | goto cleanup; | 505 | goto cleanup; |
498 | } | 506 | } |
499 | 507 | ||
500 | if (4 != argc) | 508 | if (4 != argc) |
501 | { | 509 | { |
502 | fprintf(stderr, | 510 | fprintf (stderr, |
503 | "This program must be started with our IP, the targets external IP, and our port as arguments.\n"); | 511 | "This program must be started with our IP, the targets external IP, and our port as arguments.\n"); |
504 | global_ret = 6; | 512 | global_ret = 6; |
505 | goto cleanup; | 513 | goto cleanup; |
506 | } | 514 | } |
507 | if ((1 != inet_pton(AF_INET, argv[1], &external)) || | 515 | if ((1 != inet_pton (AF_INET, argv[1], &external)) || |
508 | (1 != inet_pton(AF_INET, argv[2], &target))) | 516 | (1 != inet_pton (AF_INET, argv[2], &target))) |
509 | { | 517 | { |
510 | fprintf(stderr, "Error parsing IPv4 address: %s\n", strerror(errno)); | 518 | fprintf (stderr, "Error parsing IPv4 address: %s\n", strerror (errno)); |
511 | global_ret = 7; | 519 | global_ret = 7; |
512 | goto cleanup; | 520 | goto cleanup; |
513 | } | 521 | } |
514 | if ((1 != sscanf(argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p)) | 522 | if ((1 != sscanf (argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p)) |
515 | { | 523 | { |
516 | fprintf(stderr, "Error parsing port value `%s'\n", argv[3]); | 524 | fprintf (stderr, "Error parsing port value `%s'\n", argv[3]); |
517 | global_ret = 8; | 525 | global_ret = 8; |
518 | goto cleanup; | 526 | goto cleanup; |
519 | } | 527 | } |
520 | port = (uint16_t)p; | 528 | port = (uint16_t) p; |
521 | if (1 != inet_pton(AF_INET, DUMMY_IP, &dummy)) | 529 | if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy)) |
522 | { | 530 | { |
523 | fprintf(stderr, "Internal error converting dummy IP to binary.\n"); | 531 | fprintf (stderr, "Internal error converting dummy IP to binary.\n"); |
524 | global_ret = 9; | 532 | global_ret = 9; |
525 | goto cleanup; | 533 | goto cleanup; |
526 | } | 534 | } |
527 | send_icmp(&external, &target); | 535 | send_icmp (&external, &target); |
528 | send_icmp_udp(&external, &target); | 536 | send_icmp_udp (&external, &target); |
529 | global_ret = 0; | 537 | global_ret = 0; |
530 | cleanup: | 538 | cleanup: |
531 | if (-1 != rawsock) | 539 | if (-1 != rawsock) |
532 | (void)close(rawsock); | 540 | (void) close (rawsock); |
533 | return global_ret; | 541 | return global_ret; |
534 | } | 542 | } |
535 | 543 | ||