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