PageRenderTime 95ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/libpcap/pcap-snf.c

https://github.com/blacklion/GEOM-Events
C | 306 lines | 243 code | 42 blank | 21 comment | 58 complexity | 14d9a78e4499610f682638563f7320ac MD5 | raw file
  1. #ifdef HAVE_CONFIG_H
  2. #include "config.h"
  3. #endif
  4. #include <sys/param.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <ctype.h>
  9. #include <netinet/in.h>
  10. #include <sys/mman.h>
  11. #include <sys/socket.h>
  12. #include <sys/types.h>
  13. #include <unistd.h>
  14. #include "snf.h"
  15. #include "pcap-int.h"
  16. #ifdef SNF_ONLY
  17. #define snf_create pcap_create
  18. #define snf_platform_finddevs pcap_platform_finddevs
  19. #endif
  20. static int
  21. snf_set_datalink(pcap_t *p, int dlt)
  22. {
  23. p->linktype = dlt;
  24. return (0);
  25. }
  26. static int
  27. snf_pcap_stats(pcap_t *p, struct pcap_stat *ps)
  28. {
  29. struct snf_ring_stats stats;
  30. int rc;
  31. if ((rc = snf_ring_getstats(p->md.snf_ring, &stats))) {
  32. snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "snf_get_stats: %s",
  33. pcap_strerror(rc));
  34. return -1;
  35. }
  36. ps->ps_recv = stats.ring_pkt_recv + stats.ring_pkt_overflow;
  37. ps->ps_drop = stats.ring_pkt_overflow;
  38. ps->ps_ifdrop = stats.nic_pkt_overflow + stats.nic_pkt_bad;
  39. return 0;
  40. }
  41. static void
  42. snf_platform_cleanup(pcap_t *p)
  43. {
  44. if (p == NULL)
  45. return;
  46. snf_ring_close(p->md.snf_ring);
  47. snf_close(p->md.snf_handle);
  48. pcap_cleanup_live_common(p);
  49. }
  50. static int
  51. snf_getnonblock(pcap_t *p, char *errbuf)
  52. {
  53. return (p->md.snf_timeout == 0);
  54. }
  55. static int
  56. snf_setnonblock(pcap_t *p, int nonblock, char *errbuf)
  57. {
  58. if (nonblock)
  59. p->md.snf_timeout = 0;
  60. else {
  61. if (p->md.timeout <= 0)
  62. p->md.snf_timeout = -1; /* forever */
  63. else
  64. p->md.snf_timeout = p->md.timeout;
  65. }
  66. return (0);
  67. }
  68. #define _NSEC_PER_SEC 1000000000
  69. static inline
  70. struct timeval
  71. snf_timestamp_to_timeval(const int64_t ts_nanosec)
  72. {
  73. struct timeval tv;
  74. int32_t rem;
  75. if (ts_nanosec == 0)
  76. return (struct timeval) { 0, 0 };
  77. tv.tv_sec = ts_nanosec / _NSEC_PER_SEC;
  78. tv.tv_usec = (ts_nanosec % _NSEC_PER_SEC) / 1000;
  79. return tv;
  80. }
  81. static int
  82. snf_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
  83. {
  84. struct pcap_pkthdr hdr;
  85. int i, flags, err, caplen, n;
  86. struct snf_recv_req req;
  87. if (!p || cnt == 0)
  88. return -1;
  89. n = 0;
  90. while (n < cnt || cnt < 0) {
  91. /*
  92. * Has "pcap_breakloop()" been called?
  93. */
  94. if (p->break_loop) {
  95. if (n == 0) {
  96. p->break_loop = 0;
  97. return (-2);
  98. } else {
  99. return (n);
  100. }
  101. }
  102. err = snf_ring_recv(p->md.snf_ring, p->md.snf_timeout, &req);
  103. if (err) {
  104. if (err == EBUSY || err == EAGAIN)
  105. return (0);
  106. if (err == EINTR)
  107. continue;
  108. if (err != 0) {
  109. snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "snf_read: %s",
  110. pcap_strerror(err));
  111. return -1;
  112. }
  113. }
  114. caplen = req.length;
  115. if (caplen > p->snapshot)
  116. caplen = p->snapshot;
  117. if ((p->fcode.bf_insns == NULL) ||
  118. bpf_filter(p->fcode.bf_insns, req.pkt_addr, req.length, caplen)) {
  119. hdr.ts = snf_timestamp_to_timeval(req.timestamp);
  120. hdr.caplen = caplen;
  121. hdr.len = req.length;
  122. callback(user, &hdr, req.pkt_addr);
  123. }
  124. n++;
  125. }
  126. return (n);
  127. }
  128. static int
  129. snf_setfilter(pcap_t *p, struct bpf_program *fp)
  130. {
  131. if (!p)
  132. return -1;
  133. if (!fp) {
  134. strncpy(p->errbuf, "setfilter: No filter specified",
  135. sizeof(p->errbuf));
  136. return -1;
  137. }
  138. /* Make our private copy of the filter */
  139. if (install_bpf_program(p, fp) < 0)
  140. return -1;
  141. p->md.use_bpf = 0;
  142. return (0);
  143. }
  144. static int
  145. snf_inject(pcap_t *p, const void *buf _U_, size_t size _U_)
  146. {
  147. strlcpy(p->errbuf, "Sending packets isn't supported with snf",
  148. PCAP_ERRBUF_SIZE);
  149. return (-1);
  150. }
  151. static int
  152. snf_activate(pcap_t* p)
  153. {
  154. char *device = p->opt.source;
  155. const char *nr = NULL;
  156. int err;
  157. int flags = 0;
  158. if (device == NULL) {
  159. snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  160. "device is NULL: %s", pcap_strerror(errno));
  161. return -1;
  162. }
  163. /* In Libpcap, we set pshared by default if NUM_RINGS is set to > 1.
  164. * Since libpcap isn't thread-safe */
  165. if ((nr = getenv("SNF_NUM_RINGS")) && *nr && atoi(nr) > 1)
  166. flags |= SNF_F_PSHARED;
  167. else
  168. nr = NULL;
  169. err = snf_open(p->md.snf_boardnum,
  170. 0, /* let SNF API parse SNF_NUM_RINGS, if set */
  171. NULL, /* default RSS, or use SNF_RSS_FLAGS env */
  172. 0, /* default to SNF_DATARING_SIZE from env */
  173. flags, /* may want pshared */
  174. &p->md.snf_handle);
  175. if (err != 0) {
  176. snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  177. "snf_open failed: %s", pcap_strerror(err));
  178. return -1;
  179. }
  180. err = snf_ring_open(p->md.snf_handle, &p->md.snf_ring);
  181. if (err != 0) {
  182. snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  183. "snf_ring_open failed: %s", pcap_strerror(err));
  184. return -1;
  185. }
  186. if (p->md.timeout <= 0)
  187. p->md.snf_timeout = -1;
  188. else
  189. p->md.snf_timeout = p->md.timeout;
  190. err = snf_start(p->md.snf_handle);
  191. if (err != 0) {
  192. snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  193. "snf_start failed: %s", pcap_strerror(err));
  194. return -1;
  195. }
  196. /*
  197. * "select()" and "poll()" don't work on snf descriptors.
  198. */
  199. p->selectable_fd = -1;
  200. p->linktype = DLT_EN10MB;
  201. p->read_op = snf_read;
  202. p->inject_op = snf_inject;
  203. p->setfilter_op = snf_setfilter;
  204. p->setdirection_op = NULL; /* Not implemented.*/
  205. p->set_datalink_op = snf_set_datalink;
  206. p->getnonblock_op = snf_getnonblock;
  207. p->setnonblock_op = snf_setnonblock;
  208. p->stats_op = snf_pcap_stats;
  209. p->cleanup_op = snf_platform_cleanup;
  210. p->md.stat.ps_recv = 0;
  211. p->md.stat.ps_drop = 0;
  212. p->md.stat.ps_ifdrop = 0;
  213. return 0;
  214. }
  215. int
  216. snf_platform_finddevs(pcap_if_t **devlistp, char *errbuf)
  217. {
  218. /*
  219. * There are no platform-specific devices since each device
  220. * exists as a regular Ethernet device.
  221. */
  222. return 0;
  223. }
  224. pcap_t *
  225. snf_create(const char *device, char *ebuf)
  226. {
  227. pcap_t *p;
  228. int boardnum = -1;
  229. struct snf_ifaddrs *ifaddrs, *ifa;
  230. size_t devlen;
  231. if (snf_init(SNF_VERSION_API))
  232. return NULL;
  233. /*
  234. * Match a given interface name to our list of interface names, from
  235. * which we can obtain the intended board number
  236. */
  237. if (snf_getifaddrs(&ifaddrs) || ifaddrs == NULL)
  238. return NULL;
  239. devlen = strlen(device) + 1;
  240. ifa = ifaddrs;
  241. while (ifa) {
  242. if (!strncmp(device, ifa->snf_ifa_name, devlen)) {
  243. boardnum = ifa->snf_ifa_boardnum;
  244. break;
  245. }
  246. ifa = ifa->snf_ifa_next;
  247. }
  248. snf_freeifaddrs(ifaddrs);
  249. if (ifa == NULL) {
  250. /*
  251. * If we can't find the device by name, support the name "snfX"
  252. * and "snf10gX" where X is the board number.
  253. */
  254. if (sscanf(device, "snf10g%d", &boardnum) != 1 &&
  255. sscanf(device, "snf%d", &boardnum) != 1)
  256. return NULL;
  257. }
  258. p = pcap_create_common(device, ebuf);
  259. if (p == NULL)
  260. return NULL;
  261. p->activate_op = snf_activate;
  262. p->md.snf_boardnum = boardnum;
  263. return p;
  264. }