capturetest.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that: (1) source code distributions
  7. * retain the above copyright notice and this paragraph in its entirety, (2)
  8. * distributions including binary code include the above copyright notice and
  9. * this paragraph in its entirety in the documentation or other materials
  10. * provided with the distribution, and (3) all advertising materials mentioning
  11. * features or use of this software display the following acknowledgement:
  12. * ``This product includes software developed by the University of California,
  13. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14. * the University nor the names of its contributors may be used to endorse
  15. * or promote products derived from this software without specific prior
  16. * written permission.
  17. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. */
  21. #include "varattrs.h"
  22. #ifndef lint
  23. static const char copyright[] _U_ =
  24. "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
  25. The Regents of the University of California. All rights reserved.\n";
  26. #endif
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <stdarg.h>
  31. #include <limits.h>
  32. #ifdef _WIN32
  33. #include "getopt.h"
  34. #else
  35. #include <unistd.h>
  36. #endif
  37. #include <errno.h>
  38. #include <sys/types.h>
  39. #include <pcap.h>
  40. #include "pcap/funcattrs.h"
  41. #ifdef _WIN32
  42. #include "portability.h"
  43. #endif
  44. static char *program_name;
  45. /* Forwards */
  46. static void countme(u_char *, const struct pcap_pkthdr *, const u_char *);
  47. static void PCAP_NORETURN usage(void);
  48. static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
  49. static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
  50. static char *copy_argv(char **);
  51. static pcap_t *pd;
  52. int
  53. main(int argc, char **argv)
  54. {
  55. register int op;
  56. register char *cp, *cmdbuf, *device;
  57. long longarg;
  58. char *p;
  59. int timeout = 1000;
  60. int immediate = 0;
  61. int nonblock = 0;
  62. pcap_if_t *devlist;
  63. bpf_u_int32 localnet, netmask;
  64. struct bpf_program fcode;
  65. char ebuf[PCAP_ERRBUF_SIZE];
  66. int status;
  67. int packet_count;
  68. device = NULL;
  69. if ((cp = strrchr(argv[0], '/')) != NULL)
  70. program_name = cp + 1;
  71. else
  72. program_name = argv[0];
  73. opterr = 0;
  74. while ((op = getopt(argc, argv, "i:mnt:")) != -1) {
  75. switch (op) {
  76. case 'i':
  77. device = optarg;
  78. break;
  79. case 'm':
  80. immediate = 1;
  81. break;
  82. case 'n':
  83. nonblock = 1;
  84. break;
  85. case 't':
  86. longarg = strtol(optarg, &p, 10);
  87. if (p == optarg || *p != '\0') {
  88. error("Timeout value \"%s\" is not a number",
  89. optarg);
  90. /* NOTREACHED */
  91. }
  92. if (longarg < 0) {
  93. error("Timeout value %ld is negative", longarg);
  94. /* NOTREACHED */
  95. }
  96. if (longarg > INT_MAX) {
  97. error("Timeout value %ld is too large (> %d)",
  98. longarg, INT_MAX);
  99. /* NOTREACHED */
  100. }
  101. timeout = (int)longarg;
  102. break;
  103. default:
  104. usage();
  105. /* NOTREACHED */
  106. }
  107. }
  108. if (device == NULL) {
  109. if (pcap_findalldevs(&devlist, ebuf) == -1)
  110. error("%s", ebuf);
  111. if (devlist == NULL)
  112. error("no interfaces available for capture");
  113. device = strdup(devlist->name);
  114. pcap_freealldevs(devlist);
  115. }
  116. *ebuf = '\0';
  117. pd = pcap_create(device, ebuf);
  118. if (pd == NULL)
  119. error("%s", ebuf);
  120. status = pcap_set_snaplen(pd, 65535);
  121. if (status != 0)
  122. error("%s: pcap_set_snaplen failed: %s",
  123. device, pcap_statustostr(status));
  124. if (immediate) {
  125. status = pcap_set_immediate_mode(pd, 1);
  126. if (status != 0)
  127. error("%s: pcap_set_immediate_mode failed: %s",
  128. device, pcap_statustostr(status));
  129. }
  130. status = pcap_set_timeout(pd, timeout);
  131. if (status != 0)
  132. error("%s: pcap_set_timeout failed: %s",
  133. device, pcap_statustostr(status));
  134. status = pcap_activate(pd);
  135. if (status < 0) {
  136. /*
  137. * pcap_activate() failed.
  138. */
  139. error("%s: %s\n(%s)", device,
  140. pcap_statustostr(status), pcap_geterr(pd));
  141. } else if (status > 0) {
  142. /*
  143. * pcap_activate() succeeded, but it's warning us
  144. * of a problem it had.
  145. */
  146. warning("%s: %s\n(%s)", device,
  147. pcap_statustostr(status), pcap_geterr(pd));
  148. }
  149. if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
  150. localnet = 0;
  151. netmask = 0;
  152. warning("%s", ebuf);
  153. }
  154. cmdbuf = copy_argv(&argv[optind]);
  155. if (pcap_compile(pd, &fcode, cmdbuf, 1, netmask) < 0)
  156. error("%s", pcap_geterr(pd));
  157. if (pcap_setfilter(pd, &fcode) < 0)
  158. error("%s", pcap_geterr(pd));
  159. if (pcap_setnonblock(pd, nonblock, ebuf) == -1)
  160. error("pcap_setnonblock failed: %s", ebuf);
  161. printf("Listening on %s\n", device);
  162. for (;;) {
  163. packet_count = 0;
  164. status = pcap_dispatch(pd, -1, countme,
  165. (u_char *)&packet_count);
  166. if (status < 0)
  167. break;
  168. if (status != 0) {
  169. printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
  170. status, packet_count);
  171. }
  172. }
  173. if (status == -2) {
  174. /*
  175. * We got interrupted, so perhaps we didn't
  176. * manage to finish a line we were printing.
  177. * Print an extra newline, just in case.
  178. */
  179. putchar('\n');
  180. }
  181. (void)fflush(stdout);
  182. if (status == -1) {
  183. /*
  184. * Error. Report it.
  185. */
  186. (void)fprintf(stderr, "%s: pcap_loop: %s\n",
  187. program_name, pcap_geterr(pd));
  188. }
  189. pcap_close(pd);
  190. pcap_freecode(&fcode);
  191. free(cmdbuf);
  192. exit(status == -1 ? 1 : 0);
  193. }
  194. static void
  195. countme(u_char *user, const struct pcap_pkthdr *h _U_, const u_char *sp _U_)
  196. {
  197. int *counterp = (int *)user;
  198. (*counterp)++;
  199. }
  200. static void
  201. usage(void)
  202. {
  203. (void)fprintf(stderr, "Usage: %s [ -mn ] [ -i interface ] [ -t timeout] [expression]\n",
  204. program_name);
  205. exit(1);
  206. }
  207. /* VARARGS */
  208. static void
  209. error(const char *fmt, ...)
  210. {
  211. va_list ap;
  212. (void)fprintf(stderr, "%s: ", program_name);
  213. va_start(ap, fmt);
  214. (void)vfprintf(stderr, fmt, ap);
  215. va_end(ap);
  216. if (*fmt) {
  217. fmt += strlen(fmt);
  218. if (fmt[-1] != '\n')
  219. (void)fputc('\n', stderr);
  220. }
  221. exit(1);
  222. /* NOTREACHED */
  223. }
  224. /* VARARGS */
  225. static void
  226. warning(const char *fmt, ...)
  227. {
  228. va_list ap;
  229. (void)fprintf(stderr, "%s: WARNING: ", program_name);
  230. va_start(ap, fmt);
  231. (void)vfprintf(stderr, fmt, ap);
  232. va_end(ap);
  233. if (*fmt) {
  234. fmt += strlen(fmt);
  235. if (fmt[-1] != '\n')
  236. (void)fputc('\n', stderr);
  237. }
  238. }
  239. /*
  240. * Copy arg vector into a new buffer, concatenating arguments with spaces.
  241. */
  242. static char *
  243. copy_argv(register char **argv)
  244. {
  245. register char **p;
  246. register u_int len = 0;
  247. char *buf;
  248. char *src, *dst;
  249. p = argv;
  250. if (*p == 0)
  251. return 0;
  252. while (*p)
  253. len += strlen(*p++) + 1;
  254. buf = (char *)malloc(len);
  255. if (buf == NULL)
  256. error("copy_argv: malloc");
  257. p = argv;
  258. dst = buf;
  259. while ((src = *p++) != NULL) {
  260. while ((*dst++ = *src++) != '\0')
  261. ;
  262. dst[-1] = ' ';
  263. }
  264. dst[-1] = '\0';
  265. return buf;
  266. }