struct l2_packet_data * l2_packet_init( const char *ifname, const u8 *own_addr, unsigned short protocol, void (*rx_callback)(void *ctx, const u8 *src_addr, const u8 *buf, size_t len), void *rx_callback_ctx, int l2_hdr) { struct l2_packet_data *l2; l2 = os_zalloc(sizeof(struct l2_packet_data)); if (l2 == NULL) return NULL; os_strncpy(l2->ifname, ifname, sizeof(l2->ifname)); l2->rx_callback = rx_callback; l2->rx_callback_ctx = rx_callback_ctx; l2->l2_hdr = l2_hdr; #ifdef CONFIG_WINPCAP if (own_addr) os_memcpy(l2->own_addr, own_addr, ETH_ALEN); #else /* CONFIG_WINPCAP */ if (l2_packet_init_libdnet(l2)) return NULL; #endif /* CONFIG_WINPCAP */ if (l2_packet_init_libpcap(l2, protocol)) { #ifndef CONFIG_WINPCAP eth_close(l2->eth); #endif /* CONFIG_WINPCAP */ os_free(l2); return NULL; } return l2; }
struct l2_packet_data * l2_packet_init( const char *ifname, const u8 *own_addr, unsigned short protocol, void (*rx_callback)(void *ctx, unsigned char *src_addr, unsigned char *buf, size_t len), void *rx_callback_ctx) { struct l2_packet_data *l2; l2 = malloc(sizeof(struct l2_packet_data)); if (l2 == NULL) return NULL; memset(l2, 0, sizeof(*l2)); strncpy(l2->ifname, ifname, sizeof(l2->ifname)); l2->rx_callback = rx_callback; l2->rx_callback_ctx = rx_callback_ctx; #ifdef CONFIG_WINPCAP if (own_addr) memcpy(l2->own_addr, own_addr, ETH_ALEN); #else if (l2_packet_init_libdnet(l2)) return NULL; #endif if (l2_packet_init_libpcap(l2, protocol)) { #ifndef CONFIG_WINPCAP eth_close(l2->eth); #endif free(l2); return NULL; } return l2; }
struct l2_packet_data * l2_packet_init( const char *ifname, const u8 *own_addr, unsigned short protocol, void (*rx_callback)(void *ctx, const u8 *src_addr, const u8 *buf, size_t len), void *rx_callback_ctx, int l2_hdr) { struct l2_packet_data *l2; l2 = os_zalloc(sizeof(struct l2_packet_data)); if (l2 == NULL) return NULL; os_strlcpy(l2->ifname, ifname, sizeof(l2->ifname)); l2->rx_callback = rx_callback; l2->rx_callback_ctx = rx_callback_ctx; l2->l2_hdr = l2_hdr; if (eth_get(l2->ifname, l2->own_addr) < 0) { fprintf(stderr, "Failed to get link-level address for " "interface '%s'.\n", l2->ifname); os_free(l2); return NULL; } if (l2_packet_init_libpcap(l2, protocol)) { os_free(l2); return NULL; } return l2; }
struct l2_packet_data * l2_packet_init( const char *ifname, const u8 *own_addr, unsigned short protocol, void (*rx_callback)(void *ctx, const u8 *src_addr, const u8 *buf, size_t len), void *rx_callback_ctx, int l2_hdr) { struct l2_packet_data *l2; DWORD thread_id; l2 = os_zalloc(sizeof(struct l2_packet_data)); if (l2 == NULL) return NULL; if (os_strncmp(ifname, "\\Device\\NPF_", 12) == 0) os_strlcpy(l2->ifname, ifname, sizeof(l2->ifname)); else os_snprintf(l2->ifname, sizeof(l2->ifname), "\\Device\\NPF_%s", ifname); l2->rx_callback = rx_callback; l2->rx_callback_ctx = rx_callback_ctx; l2->l2_hdr = l2_hdr; if (own_addr) os_memcpy(l2->own_addr, own_addr, ETH_ALEN); if (l2_packet_init_libpcap(l2, protocol)) { os_free(l2); return NULL; } l2->rx_avail = CreateEvent(NULL, TRUE, FALSE, NULL); l2->rx_done = CreateEvent(NULL, TRUE, FALSE, NULL); l2->rx_notify = CreateEvent(NULL, TRUE, FALSE, NULL); if (l2->rx_avail == NULL || l2->rx_done == NULL || l2->rx_notify == NULL) { CloseHandle(l2->rx_avail); CloseHandle(l2->rx_done); CloseHandle(l2->rx_notify); pcap_close(l2->pcap); os_free(l2); return NULL; } eloop_register_event(l2->rx_avail, sizeof(l2->rx_avail), l2_packet_rx_event, l2, NULL); l2->running = 1; l2->rx_thread = CreateThread(NULL, 0, l2_packet_receive_thread, l2, 0, &thread_id); return l2; }