opentest.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 <pcap.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdarg.h>
  32. #ifdef _WIN32
  33. #include "getopt.h"
  34. #else
  35. #include <unistd.h>
  36. #endif
  37. #include <errno.h>
  38. #include "pcap/funcattrs.h"
  39. #ifdef _WIN32
  40. #include "portability.h"
  41. #endif
  42. #define MAXIMUM_SNAPLEN 65535
  43. static char *program_name;
  44. /* Forwards */
  45. static void PCAP_NORETURN usage(void);
  46. static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
  47. static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
  48. int
  49. main(int argc, char **argv)
  50. {
  51. register int op;
  52. register char *cp, *device;
  53. int dorfmon, dopromisc, snaplen, useactivate, bufsize;
  54. char ebuf[PCAP_ERRBUF_SIZE];
  55. pcap_if_t *devlist;
  56. pcap_t *pd;
  57. int status = 0;
  58. device = NULL;
  59. dorfmon = 0;
  60. dopromisc = 0;
  61. snaplen = MAXIMUM_SNAPLEN;
  62. bufsize = 0;
  63. useactivate = 0;
  64. if ((cp = strrchr(argv[0], '/')) != NULL)
  65. program_name = cp + 1;
  66. else
  67. program_name = argv[0];
  68. opterr = 0;
  69. while ((op = getopt(argc, argv, "i:Ips:aB:")) != -1) {
  70. switch (op) {
  71. case 'i':
  72. device = optarg;
  73. break;
  74. case 'I':
  75. dorfmon = 1;
  76. useactivate = 1; /* required for rfmon */
  77. break;
  78. case 'p':
  79. dopromisc = 1;
  80. break;
  81. case 's': {
  82. char *end;
  83. snaplen = strtol(optarg, &end, 0);
  84. if (optarg == end || *end != '\0'
  85. || snaplen < 0 || snaplen > MAXIMUM_SNAPLEN)
  86. error("invalid snaplen %s", optarg);
  87. else if (snaplen == 0)
  88. snaplen = MAXIMUM_SNAPLEN;
  89. break;
  90. }
  91. case 'B':
  92. bufsize = atoi(optarg)*1024;
  93. if (bufsize <= 0)
  94. error("invalid packet buffer size %s", optarg);
  95. useactivate = 1; /* required for bufsize */
  96. break;
  97. case 'a':
  98. useactivate = 1;
  99. break;
  100. default:
  101. usage();
  102. /* NOTREACHED */
  103. }
  104. }
  105. if (device == NULL) {
  106. if (pcap_findalldevs(&devlist, ebuf) == -1)
  107. error("%s", ebuf);
  108. if (devlist == NULL)
  109. error("no interfaces available for capture");
  110. device = strdup(devlist->name);
  111. pcap_freealldevs(devlist);
  112. }
  113. if (useactivate) {
  114. pd = pcap_create(device, ebuf);
  115. if (pd == NULL)
  116. error("%s: pcap_create failed: %s", device, ebuf);
  117. status = pcap_set_snaplen(pd, snaplen);
  118. if (status != 0)
  119. error("%s: pcap_set_snaplen failed: %s",
  120. device, pcap_statustostr(status));
  121. if (dopromisc) {
  122. status = pcap_set_promisc(pd, 1);
  123. if (status != 0)
  124. error("%s: pcap_set_promisc failed: %s",
  125. device, pcap_statustostr(status));
  126. }
  127. if (dorfmon) {
  128. status = pcap_set_rfmon(pd, 1);
  129. if (status != 0)
  130. error("%s: pcap_set_rfmon failed: %s",
  131. device, pcap_statustostr(status));
  132. }
  133. status = pcap_set_timeout(pd, 1000);
  134. if (status != 0)
  135. error("%s: pcap_set_timeout failed: %s",
  136. device, pcap_statustostr(status));
  137. if (bufsize != 0) {
  138. status = pcap_set_buffer_size(pd, bufsize);
  139. if (status != 0)
  140. error("%s: pcap_set_buffer_size failed: %s",
  141. device, pcap_statustostr(status));
  142. }
  143. status = pcap_activate(pd);
  144. if (status < 0) {
  145. /*
  146. * pcap_activate() failed.
  147. */
  148. error("%s: %s\n(%s)", device,
  149. pcap_statustostr(status), pcap_geterr(pd));
  150. } else if (status > 0) {
  151. /*
  152. * pcap_activate() succeeded, but it's warning us
  153. * of a problem it had.
  154. */
  155. warning("%s: %s\n(%s)", device,
  156. pcap_statustostr(status), pcap_geterr(pd));
  157. } else
  158. printf("%s opened successfully\n", device);
  159. } else {
  160. *ebuf = '\0';
  161. pd = pcap_open_live(device, 65535, 0, 1000, ebuf);
  162. if (pd == NULL)
  163. error("%s", ebuf);
  164. else if (*ebuf)
  165. warning("%s", ebuf);
  166. else
  167. printf("%s opened successfully\n", device);
  168. }
  169. pcap_close(pd);
  170. exit(status < 0 ? 1 : 0);
  171. }
  172. static void
  173. usage(void)
  174. {
  175. (void)fprintf(stderr,
  176. "Usage: %s [ -Ipa ] [ -i interface ] [ -s snaplen ] [ -B bufsize ]\n",
  177. program_name);
  178. exit(1);
  179. }
  180. /* VARARGS */
  181. static void
  182. error(const char *fmt, ...)
  183. {
  184. va_list ap;
  185. (void)fprintf(stderr, "%s: ", program_name);
  186. va_start(ap, fmt);
  187. (void)vfprintf(stderr, fmt, ap);
  188. va_end(ap);
  189. if (*fmt) {
  190. fmt += strlen(fmt);
  191. if (fmt[-1] != '\n')
  192. (void)fputc('\n', stderr);
  193. }
  194. exit(1);
  195. /* NOTREACHED */
  196. }
  197. /* VARARGS */
  198. static void
  199. warning(const char *fmt, ...)
  200. {
  201. va_list ap;
  202. (void)fprintf(stderr, "%s: WARNING: ", program_name);
  203. va_start(ap, fmt);
  204. (void)vfprintf(stderr, fmt, ap);
  205. va_end(ap);
  206. if (*fmt) {
  207. fmt += strlen(fmt);
  208. if (fmt[-1] != '\n')
  209. (void)fputc('\n', stderr);
  210. }
  211. }