3 iface_t * if_connect(string if_name, char mode) {
4 char pcap_errbuf[PCAP_ERRBUF_SIZE];
6 iface_t * iface = (iface_t *)malloc(sizeof(iface_t));
7 iface->name = new string();
9 cout << "device name : " << if_name << endl;
11 *(iface->name) = if_name;
16 // mode is relevant only under linux
19 if ((iface->pcap_interface = pcap_open_live((char*)if_name.c_str(), 65536, 1, 1, pcap_errbuf)) == NULL) {
20 JRLDBG("Could not initialize pcap\n");
24 iface->pcap_fd = pcap_fileno(iface->pcap_interface);
28 char libnet_errbuf[LIBNET_ERRORBUF_SIZE];
30 if ((iface->net_interface = libnet_init(LIBNET_LINK_ADV, (char *)if_name.c_str(), libnet_errbuf)) == NULL) {
31 JRLDBG("Could not initialize libnet\n");
37 if ((iface->pcap_interface = pcap_open_live((char*)if_name.c_str(), 65536, 1, 1, pcap_errbuf)) == NULL) {
38 JRLDBG("Could not initialize pcap\n");
42 pcap_setmintocopy(iface->pcap_interface, 40);
43 iface->pcap_event = pcap_getevent(iface->pcap_interface);
49 void if_disconnect(iface_t * iface) {
51 pcap_close(iface->pcap_interface);
56 HANDLE if_get_event(iface_t * iface) {
57 return iface->pcap_event;
58 // return pcap_getevent(iface->pcap_interface);
63 int if_get_fd(iface_t * iface) {
64 return iface->pcap_fd;
69 int if_loop(iface_t * iface, RawEthernetPacket * pkt) {
72 ret = pcap_loop(iface->pcap_interface, 1, pkt_handler, (u_char*)pkt);
76 } else if (ret == -2) {
78 } else if (ret == -1) {
85 void if_break_loop(iface_t * iface) {
86 pcap_breakloop(iface->pcap_interface);
89 void pkt_handler(u_char * pkt, const struct pcap_pkthdr * pkt_header, const u_char * pkt_data) {
90 RawEthernetPacket pkt2((const char *)pkt_data, (unsigned)(pkt_header->len));
91 *(RawEthernetPacket *)pkt = pkt2;
92 ((RawEthernetPacket*)pkt)->set_type("et");
96 int if_read_pkt(iface_t * iface, RawEthernetPacket * pkt) {
97 struct pcap_pkthdr header;
98 const u_char * pcap_pkt;
100 pcap_pkt = pcap_next(iface->pcap_interface, &header);
102 if (pcap_pkt == NULL) {
106 RawEthernetPacket pkt2((const char *)pcap_pkt, (unsigned)(header.len));
116 int if_write_pkt(iface_t * iface, RawEthernetPacket * pkt) {
117 ASSERT((iface != NULL) && (pkt != NULL) && (iface->net_interface != NULL));
120 JRLDBG("Writing pkt size(%d)\n", pkt->get_size());
121 if (libnet_adv_write_link(iface->net_interface,
122 (u_char *)(pkt->get_data()),
123 pkt->get_size()) < 0) {
124 JRLDBG("Libnet could not inject packet size (%d)\n", pkt->get_size());
129 if (pcap_sendpacket(iface->pcap_interface,
130 (u_char *)(pkt->get_data()),
131 pkt->get_size()) < 0) {
132 JRLDBG("PCAP could not inject packet\n");
141 int if_setup_filter(iface_t * iface, string bpf_str) {
142 struct bpf_program fcode;
145 char errbuf[PCAP_ERRBUF_SIZE];
149 filter_buf = (char *)malloc(bpf_str.length());
150 strcpy(filter_buf, bpf_str.c_str());
151 cout << "Setting Getting interface info for " << iface->name << endl;
152 if (pcap_lookupnet(iface->name->c_str(), &network, &netmask, errbuf) == -1) {
153 JRLDBG("Error looking up the network info\n");
158 cout << bpf_str << endl;
159 if (pcap_compile(iface->pcap_interface, &fcode, filter_buf, 1, netmask) < 0) {
160 JRLDBG("Could not compile bpf filter\n");
164 if (pcap_setfilter(iface->pcap_interface, &fcode) < 0) {
165 JRLDBG("Could not insert bpf filter\n");