4 iface_t * if_connect(string if_name, char mode) {
5 char pcap_errbuf[PCAP_ERRBUF_SIZE];
7 iface_t * iface = (iface_t *)malloc(sizeof(iface_t));
8 iface->name = new string();
10 cout << "device name : " << if_name << endl;
12 *(iface->name) = if_name;
17 // mode is relevant only under linux
20 if ((iface->pcap_interface = pcap_open_live((char*)if_name.c_str(), 65536, 1, 1, pcap_errbuf)) == NULL) {
21 vtl_debug("Could not initialize pcap\n");
25 iface->pcap_fd = pcap_fileno(iface->pcap_interface);
29 char libnet_errbuf[LIBNET_ERRORBUF_SIZE];
31 if ((iface->net_interface = libnet_init(LIBNET_LINK_ADV, (char *)if_name.c_str(), libnet_errbuf)) == NULL) {
32 vtl_debug("Could not initialize libnet\n");
38 if ((iface->pcap_interface = pcap_open_live((char*)if_name.c_str(), 65536, 1, 1, pcap_errbuf)) == NULL) {
39 vtl_debug("Could not initialize pcap\n");
43 pcap_setmintocopy(iface->pcap_interface, 40);
44 iface->pcap_event = pcap_getevent(iface->pcap_interface);
50 void if_disconnect(iface_t * iface) {
52 pcap_close(iface->pcap_interface);
57 HANDLE if_get_event(iface_t * iface) {
58 return iface->pcap_event;
59 // return pcap_getevent(iface->pcap_interface);
64 int if_get_fd(iface_t * iface) {
65 return iface->pcap_fd;
70 int if_loop(iface_t * iface, RawEthernetPacket * pkt) {
73 ret = pcap_loop(iface->pcap_interface, 1, pkt_handler, (u_char*)pkt);
77 } else if (ret == -2) {
79 } else if (ret == -1) {
86 void if_break_loop(iface_t * iface) {
87 pcap_breakloop(iface->pcap_interface);
90 void pkt_handler(u_char * pkt, const struct pcap_pkthdr * pkt_header, const u_char * pkt_data) {
91 RawEthernetPacket pkt2((const char *)pkt_data, (unsigned)(pkt_header->len));
92 *(RawEthernetPacket *)pkt = pkt2;
93 ((RawEthernetPacket*)pkt)->set_type("et");
97 int if_read_pkt(iface_t * iface, RawEthernetPacket * pkt) {
98 struct pcap_pkthdr header;
99 const u_char * pcap_pkt;
101 pcap_pkt = pcap_next(iface->pcap_interface, &header);
103 if (pcap_pkt == NULL) {
107 RawEthernetPacket pkt2((const char *)pcap_pkt, (unsigned)(header.len));
117 int if_write_pkt(iface_t * iface, RawEthernetPacket * pkt) {
118 assert((iface != NULL) && (pkt != NULL) && (iface->net_interface != NULL));
121 vtl_debug("Writing pkt size(%lu)\n", pkt->get_size());
122 if (libnet_adv_write_link(iface->net_interface,
123 (u_char *)(pkt->get_data()),
124 pkt->get_size()) < 0) {
125 vtl_debug("Libnet could not inject packet size (%lu)\n", pkt->get_size());
130 if (pcap_sendpacket(iface->pcap_interface,
131 (u_char *)(pkt->get_data()),
132 pkt->get_size()) < 0) {
133 vtl_debug("PCAP could not inject packet\n");
142 int if_setup_filter(iface_t * iface, string bpf_str) {
143 struct bpf_program fcode;
146 char errbuf[PCAP_ERRBUF_SIZE];
150 filter_buf = (char *)malloc(bpf_str.length());
151 strcpy(filter_buf, bpf_str.c_str());
152 cout << "Setting Getting interface info for " << iface->name << endl;
153 if (pcap_lookupnet(iface->name->c_str(), &network, &netmask, errbuf) == -1) {
154 vtl_debug("Error looking up the network info\n");
159 cout << bpf_str << endl;
160 if (pcap_compile(iface->pcap_interface, &fcode, filter_buf, 1, netmask) < 0) {
161 vtl_debug("Could not compile bpf filter\n");
165 if (pcap_setfilter(iface->pcap_interface, &fcode) < 0) {
166 vtl_debug("Could not insert bpf filter\n");