Lines Matching refs:handle

215 static int	fix_program(pcap_t *handle, struct sock_fprog *fcode);
217 static int set_kernel_filter(pcap_t *handle, struct sock_fprog *fcode);
218 static int reset_kernel_filter(pcap_t *handle);
227 * Get a handle for a live capture from the given device. You can
240 pcap_t *handle;
258 /* Allocate a handle for this session. */
260 handle = malloc(sizeof(*handle));
261 if (handle == NULL) {
269 memset(handle, 0, sizeof(*handle));
270 handle->snapshot = snaplen;
271 handle->md.timeout = to_ms;
279 handle->md.device = strdup("any");
288 handle->md.device = strdup(device);
290 if (handle->md.device == NULL) {
293 free(handle);
307 if ((err = live_open_new(handle, device, promisc, to_ms, ebuf)) == 1)
311 if (live_open_old(handle, device, promisc, to_ms, ebuf))
321 if (handle->md.device != NULL)
322 free(handle->md.device);
323 free(handle);
333 if (handle->md.sock_packet && (uname(&utsname) < 0 ||
378 mtu = iface_get_mtu(handle->fd, device, ebuf);
380 pcap_close_linux(handle);
381 free(handle);
384 handle->bufsize = MAX_LINKHEADER_SIZE + mtu;
385 if (handle->bufsize < handle->snapshot)
386 handle->bufsize = handle->snapshot;
403 if (handle->md.cooked) {
404 if (handle->snapshot < SLL_HDR_LEN + 1)
405 handle->snapshot = SLL_HDR_LEN + 1;
407 handle->bufsize = handle->snapshot;
412 handle->buffer = malloc(handle->bufsize + handle->offset);
413 if (!handle->buffer) {
416 pcap_close_linux(handle);
417 free(handle);
422 * "handle->fd" is a socket, so "select()" and "poll()"
425 handle->selectable_fd = handle->fd;
427 handle->read_op = pcap_read_linux;
428 handle->inject_op = pcap_inject_linux;
429 handle->setfilter_op = pcap_setfilter_linux;
430 handle->setdirection_op = pcap_setdirection_linux;
431 handle->set_datalink_op = NULL; /* can't change data link type */
432 handle->getnonblock_op = pcap_getnonblock_fd;
433 handle->setnonblock_op = pcap_setnonblock_fd;
434 handle->stats_op = pcap_stats_linux;
435 handle->close_op = pcap_close_linux;
437 return handle;
446 pcap_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
452 return pcap_read_packet(handle, callback, user);
461 pcap_read_packet(pcap_t *handle, pcap_handler callback, u_char *userdata)
480 if (handle->md.cooked)
494 bp = handle->buffer + handle->offset;
499 if (handle->break_loop) {
505 handle->break_loop = 0;
510 handle->fd, bp + offset,
511 handle->bufsize - offset, MSG_TRUNC,
521 snprintf(handle->errbuf, sizeof(handle->errbuf),
528 if (!handle->md.sock_packet) {
537 * that won't handle packet sockets without socket
541 if (handle->md.ifindex != -1 &&
542 from.sll_ifindex != handle->md.ifindex)
558 if (from.sll_ifindex == handle->md.lo_ifindex)
564 if (handle->direction == PCAP_D_IN)
571 if (handle->direction == PCAP_D_OUT)
581 if (handle->md.cooked) {
596 * able to handle DLT_LINUX_SLL captures.
660 * We currently handle this by making a copy of the filter
668 if (caplen > handle->snapshot)
669 caplen = handle->snapshot;
672 if (!handle->md.use_bpf && handle->fcode.bf_insns) {
673 if (bpf_filter(handle->fcode.bf_insns, bp,
683 if (ioctl(handle->fd, SIOCGSTAMP, &pcap_header.ts) == -1) {
684 snprintf(handle->errbuf, sizeof(handle->errbuf),
734 handle->md.packets_read++;
743 pcap_inject_linux(pcap_t *handle, const void *buf, size_t size)
748 if (!handle->md.sock_packet) {
750 if (handle->md.ifindex == -1) {
754 strlcpy(handle->errbuf,
760 if (handle->md.cooked) {
768 strlcpy(handle->errbuf,
776 ret = send(handle->fd, buf, size, 0);
778 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
786 * Get the statistics for the given packet capture handle.
794 pcap_stats_linux(pcap_t *handle, struct pcap_stat *stats)
805 if (getsockopt(handle->fd, SOL_PACKET, PACKET_STATISTICS,
849 * getsockopt(handle->fd, SOL_PACKET, PACKET_STATISTICS, ....
852 handle->md.stat.ps_recv += kstats.tp_packets;
853 handle->md.stat.ps_drop += kstats.tp_drops;
854 *stats = handle->md.stat;
867 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
892 stats->ps_recv = handle->md.packets_read;
925 pcap_setfilter_linux(pcap_t *handle, struct bpf_program *filter)
933 if (!handle)
936 strncpy(handle->errbuf, "setfilter: No filter specified",
937 sizeof(handle->errbuf));
943 if (install_bpf_program(handle, filter) < 0)
951 handle->md.use_bpf = 0;
957 if (handle->fcode.bf_len > USHRT_MAX) {
984 switch (fix_program(handle, &fcode)) {
1013 if ((err = set_kernel_filter(handle, &fcode)) == 0)
1016 handle->md.use_bpf = 1;
1041 if (!handle->md.use_bpf)
1042 reset_kernel_filter(handle);
1063 pcap_setdirection_linux(pcap_t *handle, pcap_direction_t d)
1066 if (!handle->md.sock_packet) {
1067 handle->direction = d;
1075 snprintf(handle->errbuf, sizeof(handle->errbuf),
1084 * constant, as arguments, and sets "handle->linktype" to the
1085 * appropriate DLT_XXX constant and sets "handle->offset" to
1086 * the appropriate value (to make "handle->offset" plus link-layer
1098 static void map_arphrd_to_dlt(pcap_t *handle, int arptype, int cooked_ok)
1120 handle->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
1124 if (handle->dlt_list != NULL) {
1125 handle->dlt_list[0] = DLT_EN10MB;
1126 handle->dlt_list[1] = DLT_DOCSIS;
1127 handle->dlt_count = 2;
1133 handle->linktype = DLT_EN10MB;
1134 handle->offset = 2;
1138 handle->linktype = DLT_EN3MB;
1142 handle->linktype = DLT_AX25;
1146 handle->linktype = DLT_PRONET;
1150 handle->linktype = DLT_CHAOS;
1158 handle->linktype = DLT_IEEE802;
1159 handle->offset = 2;
1163 handle->linktype = DLT_ARCNET_LINUX;
1170 handle->linktype = DLT_FDDI;
1171 handle->offset = 3;
1215 handle->linktype = DLT_LINUX_SLL;
1217 handle->linktype = -1;
1224 handle->linktype = DLT_IEEE802_11;
1231 handle->linktype = DLT_PRISM_HEADER;
1238 handle->linktype = DLT_IEEE802_11_RADIO;
1262 handle->linktype = DLT_LINUX_SLL;
1265 * XXX - handle ISDN types here? We can't fall
1280 handle->linktype = DLT_RAW;
1288 handle->linktype = DLT_C_HDLC;
1315 handle->linktype = DLT_RAW;
1322 handle->linktype = DLT_FRELAY;
1326 handle->linktype = DLT_LTALK;
1350 handle->linktype = DLT_IP_OVER_FC;
1358 handle->linktype = DLT_LINUX_IRDA;
1361 //handle->md.cooked = 1;
1371 handle->linktype = DLT_LINUX_LAPD;
1375 handle->linktype = -1;
1388 live_open_new(pcap_t *handle, const char *device, int promisc,
1416 handle->md.sock_packet = 0;
1429 handle->md.lo_ifindex = iface_get_id(sock_fd, "lo", ebuf);
1435 handle->offset = 0;
1444 handle->md.cooked = 0;
1451 map_arphrd_to_dlt(handle, arptype, 1);
1452 if (handle->linktype == -1 ||
1453 handle->linktype == DLT_LINUX_SLL ||
1454 handle->linktype == DLT_LINUX_IRDA ||
1455 handle->linktype == DLT_LINUX_LAPD ||
1456 (handle->linktype == DLT_EN10MB &&
1480 handle->md.cooked = 1;
1487 if (handle->dlt_list != NULL) {
1488 free(handle->dlt_list);
1489 handle->dlt_list = NULL;
1490 handle->dlt_count = 0;
1493 if (handle->linktype == -1) {
1498 * to handle the new type.
1509 if (handle->linktype != DLT_LINUX_IRDA &&
1510 handle->linktype != DLT_LINUX_LAPD)
1511 handle->linktype = DLT_LINUX_SLL;
1514 handle->md.ifindex = iface_get_id(sock_fd, device, ebuf);
1515 if (handle->md.ifindex == -1)
1518 if ((err = iface_bind(sock_fd, handle->md.ifindex,
1528 handle->md.cooked = 1;
1529 handle->linktype = DLT_LINUX_SLL;
1540 handle->md.ifindex = -1;
1564 mr.mr_ifindex = handle->md.ifindex;
1577 handle->fd = sock_fd;
1590 if (handle->dlt_list != NULL)
1591 free(handle->dlt_list);
1694 struct pcap *handle;
1696 while ((handle = pcaps_to_close) != NULL)
1697 pcap_close(handle);
1700 static void pcap_close_linux( pcap_t *handle )
1705 if (handle->md.clear_promisc) {
1716 strncpy(ifr.ifr_name, handle->md.device, sizeof(ifr.ifr_name));
1717 if (ioctl(handle->fd, SIOCGIFFLAGS, &ifr) == -1) {
1730 if (ioctl(handle->fd, SIOCSIFFLAGS, &ifr) == -1) {
1746 if (p == handle) {
1766 if (handle->md.device != NULL)
1767 free(handle->md.device);
1768 handle->md.device = NULL;
1769 pcap_close_common(handle);
1778 live_open_old(pcap_t *handle, const char *device, int promisc,
1787 handle->fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL));
1788 if (handle->fd == -1) {
1795 handle->md.sock_packet = 1;
1798 handle->md.cooked = 0;
1807 if (iface_bind_old(handle->fd, device, ebuf) == -1)
1813 arptype = iface_get_arptype(handle->fd, device, ebuf);
1821 map_arphrd_to_dlt(handle, arptype, 0);
1822 if (handle->linktype == -1) {
1833 if (ioctl(handle->fd, SIOCGIFFLAGS, &ifr) == -1) {
1867 if (ioctl(handle->fd, SIOCSIFFLAGS, &ifr) == -1) {
1873 handle->md.clear_promisc = 1;
1879 handle->md.next = pcaps_to_close;
1880 pcaps_to_close = handle;
1888 handle->offset = 0;
1894 pcap_close_linux(handle);
1982 fix_program(pcap_t *handle, struct sock_fprog *fcode)
1994 prog_size = sizeof(*handle->fcode.bf_insns) * handle->fcode.bf_len;
1995 len = handle->fcode.bf_len;
1998 snprintf(handle->errbuf, sizeof(handle->errbuf),
2002 memcpy(f, handle->fcode.bf_insns, prog_size);
2052 if (handle->md.cooked) {
2106 set_kernel_filter(pcap_t *handle, struct sock_fprog *fcode)
2144 if (setsockopt(handle->fd, SOL_SOCKET, SO_ATTACH_FILTER,
2159 save_mode = fcntl(handle->fd, F_GETFL, 0);
2161 fcntl(handle->fd, F_SETFL, save_mode | O_NONBLOCK) >= 0) {
2162 while (recv(handle->fd, &drain, sizeof drain,
2166 fcntl(handle->fd, F_SETFL, save_mode);
2169 reset_kernel_filter(handle);
2170 snprintf(handle->errbuf, sizeof(handle->errbuf),
2180 ret = setsockopt(handle->fd, SOL_SOCKET, SO_ATTACH_FILTER,
2200 reset_kernel_filter(handle);
2208 reset_kernel_filter(pcap_t *handle)
2218 return setsockopt(handle->fd, SOL_SOCKET, SO_DETACH_FILTER,