main.lc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* include main1 */
  2. #include "udpcksum.h"## 1 ##src/udpcksum/main.c##
  3. /* define global variables */## 2 ##src/udpcksum/main.c##
  4. struct sockaddr *dest, *local;## 3 ##src/udpcksum/main.c##
  5. struct sockaddr_in locallookup;## 4 ##src/udpcksum/main.c##
  6. socklen_t destlen, locallen;## 5 ##src/udpcksum/main.c##
  7. int datalink; /* from pcap_datalink(), in <net/bpf.h> */## 6 ##src/udpcksum/main.c##
  8. char *device; /* pcap device */## 7 ##src/udpcksum/main.c##
  9. pcap_t *pd; /* packet capture struct pointer */## 8 ##src/udpcksum/main.c##
  10. int rawfd; /* raw socket to write on */## 9 ##src/udpcksum/main.c##
  11. int snaplen = 200; /* amount of data to capture */## 10 ##src/udpcksum/main.c##
  12. int verbose;## 11 ##src/udpcksum/main.c##
  13. int zerosum; /* send UDP query with no checksum */## 12 ##src/udpcksum/main.c##
  14. static void usage(const char *);## 13 ##src/udpcksum/main.c##
  15. int## 14 ##src/udpcksum/main.c##
  16. main(int argc, char *argv[])## 15 ##src/udpcksum/main.c##
  17. {## 16 ##src/udpcksum/main.c##
  18. int c, lopt = 0;## 17 ##src/udpcksum/main.c##
  19. char *ptr, localname[1024], *localport;## 18 ##src/udpcksum/main.c##
  20. struct addrinfo *aip;## 19 ##src/udpcksum/main.c##
  21. if (argc < 2)## 20 ##src/udpcksum/main.c##
  22. usage("");## 21 ##src/udpcksum/main.c##
  23. /* end main1 */
  24. /* include main2 */
  25. opterr = 0; /* don't want getopt() writing to stderr */## 22 ##src/udpcksum/main.c##
  26. while ((c = getopt(argc, argv, "0i:l:v")) != -1) {## 23 ##src/udpcksum/main.c##
  27. switch (c) {## 24 ##src/udpcksum/main.c##
  28. case '0':## 25 ##src/udpcksum/main.c##
  29. zerosum = 1;## 26 ##src/udpcksum/main.c##
  30. break;## 27 ##src/udpcksum/main.c##
  31. case 'i':## 28 ##src/udpcksum/main.c##
  32. device = optarg; /* pcap device */## 29 ##src/udpcksum/main.c##
  33. break;## 30 ##src/udpcksum/main.c##
  34. case 'l': /* local IP address and port#: a.b.c.d.p */## 31 ##src/udpcksum/main.c##
  35. if ((ptr = strrchr(optarg, '.')) == NULL)## 32 ##src/udpcksum/main.c##
  36. usage("invalid -l option");## 33 ##src/udpcksum/main.c##
  37. *ptr++ = 0; /* null replaces final period */## 34 ##src/udpcksum/main.c##
  38. localport = ptr; /* service name or port number */## 35 ##src/udpcksum/main.c##
  39. strncpy(localname, optarg, sizeof(localname));## 36 ##src/udpcksum/main.c##
  40. lopt = 1;## 37 ##src/udpcksum/main.c##
  41. break;## 38 ##src/udpcksum/main.c##
  42. case 'v':## 39 ##src/udpcksum/main.c##
  43. verbose = 1;## 40 ##src/udpcksum/main.c##
  44. break;## 41 ##src/udpcksum/main.c##
  45. case '?':## 42 ##src/udpcksum/main.c##
  46. usage("unrecognized option");## 43 ##src/udpcksum/main.c##
  47. }## 44 ##src/udpcksum/main.c##
  48. }## 45 ##src/udpcksum/main.c##
  49. /* end main2 */
  50. /* include main3 */
  51. if (optind != argc - 2)## 46 ##src/udpcksum/main.c##
  52. usage("missing <host> and/or <serv>");## 47 ##src/udpcksum/main.c##
  53. /* 4convert destination name and service */## 48 ##src/udpcksum/main.c##
  54. aip = Host_serv(argv[optind], argv[optind + 1], AF_INET, SOCK_DGRAM);## 49 ##src/udpcksum/main.c##
  55. dest = aip->ai_addr; /* don't freeaddrinfo() */## 50 ##src/udpcksum/main.c##
  56. destlen = aip->ai_addrlen;## 51 ##src/udpcksum/main.c##
  57. /* ## 52 ##src/udpcksum/main.c##
  58. * Need local IP address for source IP address for UDP datagrams.## 53 ##src/udpcksum/main.c##
  59. * Can't specify 0 and let IP choose, as we need to know it for## 54 ##src/udpcksum/main.c##
  60. * the pseudo-header to calculate the UDP checksum.## 55 ##src/udpcksum/main.c##
  61. * If -l option supplied, then use those values; otherwise,## 56 ##src/udpcksum/main.c##
  62. * connect a UDP socket to the destination to determine the right## 57 ##src/udpcksum/main.c##
  63. * source address.## 58 ##src/udpcksum/main.c##
  64. */## 59 ##src/udpcksum/main.c##
  65. if (lopt) {## 60 ##src/udpcksum/main.c##
  66. /* 4convert local name and service */## 61 ##src/udpcksum/main.c##
  67. aip = Host_serv(localname, localport, AF_INET, SOCK_DGRAM);## 62 ##src/udpcksum/main.c##
  68. local = aip->ai_addr; /* don't freeaddrinfo() */## 63 ##src/udpcksum/main.c##
  69. locallen = aip->ai_addrlen;## 64 ##src/udpcksum/main.c##
  70. } else {## 65 ##src/udpcksum/main.c##
  71. int s;## 66 ##src/udpcksum/main.c##
  72. s = Socket(AF_INET, SOCK_DGRAM, 0);## 67 ##src/udpcksum/main.c##
  73. Connect(s, dest, destlen);## 68 ##src/udpcksum/main.c##
  74. /* the kernel chooses the correct local address for dest */## 69 ##src/udpcksum/main.c##
  75. locallen = sizeof(locallookup);## 70 ##src/udpcksum/main.c##
  76. local = (struct sockaddr *) &locallookup;## 71 ##src/udpcksum/main.c##
  77. Getsockname(s, local, &locallen);## 72 ##src/udpcksum/main.c##
  78. if (locallookup.sin_addr.s_addr == htonl(INADDR_ANY))## 73 ##src/udpcksum/main.c##
  79. err_quit("Can't determine local address - use -l\n");## 74 ##src/udpcksum/main.c##
  80. close(s);## 75 ##src/udpcksum/main.c##
  81. }## 76 ##src/udpcksum/main.c##
  82. open_output(); /* open output, either raw socket or libnet */## 77 ##src/udpcksum/main.c##
  83. open_pcap(); /* open packet capture device */## 78 ##src/udpcksum/main.c##
  84. setuid(getuid()); /* don't need superuser privileges any more */## 79 ##src/udpcksum/main.c##
  85. Signal(SIGTERM, cleanup);## 80 ##src/udpcksum/main.c##
  86. Signal(SIGINT, cleanup);## 81 ##src/udpcksum/main.c##
  87. Signal(SIGHUP, cleanup);## 82 ##src/udpcksum/main.c##
  88. test_udp();## 83 ##src/udpcksum/main.c##
  89. cleanup(0);## 84 ##src/udpcksum/main.c##
  90. }## 85 ##src/udpcksum/main.c##
  91. /* end main3 */
  92. static void## 86 ##src/udpcksum/main.c##
  93. usage(const char *msg)## 87 ##src/udpcksum/main.c##
  94. {## 88 ##src/udpcksum/main.c##
  95. err_msg("usage: udpcksum [ options ] <host> <serv>\n"## 89 ##src/udpcksum/main.c##
  96. "options: -0 send UDP datagram with checksum set to 0\n"## 90 ##src/udpcksum/main.c##
  97. " -i s packet capture device\n"## 91 ##src/udpcksum/main.c##
  98. " -l a.b.c.d.p local IP=a.b.c.d, local port=p\n"## 92 ##src/udpcksum/main.c##
  99. " -v verbose output");## 93 ##src/udpcksum/main.c##
  100. if (msg[0] != 0)## 94 ##src/udpcksum/main.c##
  101. err_quit("%s", msg);## 95 ##src/udpcksum/main.c##
  102. exit(1);## 96 ##src/udpcksum/main.c##
  103. }## 97 ##src/udpcksum/main.c##