/*- - ptag = n:eth{src=ethmac, dst=ethmac, type=int, payload=str, ptag=int} type is optional, defaults to IP ptag is optional, defaults to creating a new protocol block */ static int lnet_eth (lua_State *L) { libnet_t** ud = luaL_checkudata(L, 1, L_NET_REGID); luaL_argcheck(L, *ud, 1, "net has been destroyed"); const char* src = v_arg_string(L, 2, "src"); const char* dst = v_arg_string(L, 2, "dst"); int type = v_arg_integer_opt(L, 2, "type", ETHERTYPE_IP); size_t payloadsz = 0; const char* payload = v_arg_lstring(L, 2, "payload", &payloadsz, ""); int ptag = lnet_arg_ptag(L, 2); if(payloadsz == 0) { payload = NULL; } #ifdef NET_DUMP printf("net eth src %s dst %s type %d payloadsz %lu ptag %d\n", src, dst, type, payloadsz, ptag); #endif eth_addr_t src_n = check_eth_pton(L, src, "src"); eth_addr_t dst_n = check_eth_pton(L, dst, "dst"); ptag = libnet_build_ethernet(dst_n.data, src_n.data, type, (uint8_t*)payload, payloadsz, *ud, ptag); check_error(L, *ud, ptag); lua_pushinteger(L, ptag); return 1; }
/////////////////////////////////////////////////////////////////////////// // Applies another random Ethernet source address to a given Ethernet-PTAG. // (The calling function should check 'tx.eth_src_rand' whether the SA // should be randomized.) // int update_Eth_SA(libnet_t *l, libnet_ptag_t t) { tx.eth_src[0] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256) & 0xFE; // keeps bcast-bit zero tx.eth_src[1] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256); tx.eth_src[2] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256); tx.eth_src[3] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256); tx.eth_src[4] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256); tx.eth_src[5] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256); t = libnet_build_ethernet (tx.eth_dst, tx.eth_src, tx.eth_type, NULL, // the payload 0, l, t); if (t == -1) { fprintf(stderr, " mz/update_Eth_SA: Can't build Ethernet header: %s\n", libnet_geterror(l)); exit(EXIT_FAILURE); } return 0; }
int send_tcp(struct libnet_link_int *l, u_char *device, u_long src_ip, u_short src_prt, u_long dst_ip, u_short dst_prt) { int n; u_char *buf; if (libnet_init_packet(LIBNET_TCP_H + LIBNET_IP_H + LIBNET_ETH_H, &buf) == -1) { perror("no packet memory"); exit(EXIT_FAILURE); } /* * Ethernet header */ libnet_build_ethernet(enet_dst, enet_src, ETHERTYPE_IP, NULL, 0, buf); libnet_build_ip(LIBNET_TCP_H, 0, 242, 0, 64, IPPROTO_TCP, src_ip, dst_ip, NULL, 0, buf + LIBNET_ETH_H); libnet_build_tcp(src_prt, dst_prt, 111111, 999999, TH_SYN, 32767, 0, NULL, 0, buf + LIBNET_IP_H + LIBNET_ETH_H); libnet_do_checksum(buf + LIBNET_ETH_H, IPPROTO_IP, LIBNET_IP_H); libnet_do_checksum(buf + LIBNET_ETH_H, IPPROTO_TCP, LIBNET_TCP_H); n = libnet_write_link_layer(l, device, buf, LIBNET_ETH_H + LIBNET_IP_H + LIBNET_TCP_H); if (n != LIBNET_ETH_H + LIBNET_IP_H + LIBNET_TCP_H) { fprintf(stderr, "Oopz. Only wrote %d bytes\n", n); } else { printf("Wrote %d byte TCP packet through linktype %d\n", n, l->linktype); } libnet_destroy_packet(&buf); return (n); }
void spoof_arp(in_addr_t ipaddr, in_addr_t destip, u_int8_t *macaddr, u_int8_t *destmacaddr) { libnet_ptag_t arp = 0; /* ARP protocol tag */ libnet_ptag_t eth = 0; /* Ethernet protocol tag */ libnet_t *l; char errbuf[LIBNET_ERRBUF_SIZE]; l = libnet_init(LIBNET_LINK, dev, errbuf); if (l == NULL) { fprintf (stderr, "Error Opening Context: %s\n", errbuf); exit(1); } arp = libnet_autobuild_arp(ARPOP_REPLY, macaddr, (u_int8_t *) &ipaddr, destmacaddr, (u_int8_t *) &destip, l); if (arp == -1) { fprintf(stderr, "Unable to build ARP header: %s\n", libnet_geterror (l)); exit(1); } eth = libnet_build_ethernet(destmacaddr, macaddr, ETHERTYPE_ARP, NULL, 0, l, eth); if (eth == -1) { fprintf (stderr, "Unable to build Ethernet header: %s\n", libnet_geterror (l)); exit (1); } /* write the packet */ if ((libnet_write (l)) == -1) { fprintf (stderr, "Unable to send packet: %s\n", libnet_geterror (l)); exit (1); } /* exit cleanly */ libnet_destroy (l); }
libnet_t* mk_packet(libnet_t* lntag, u_int32_t ip, u_char *device, u_char macaddr[6], u_char *broadcast, u_char *netmask, u_short arptype) { u_char *target_mac; u_char device_mac[6]; u_char bcast_mac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; u_char zero_mac[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; if (arptype == ARPOP_REQUEST) { target_mac = zero_mac; } else if (arptype == ARPOP_REPLY) { target_mac = macaddr; } else { cl_log(LOG_ERR, "unkonwn arptype:"); return NULL; } /* * ARP header */ if (libnet_build_arp(ARPHRD_ETHER, /* hardware address type */ ETHERTYPE_IP, /* protocol address type */ 6, /* Hardware address length */ 4, /* protocol address length */ arptype, /* ARP operation type */ macaddr, /* sender Hardware address */ (u_int8_t *)&ip, /* sender protocol address */ target_mac, /* target hardware address */ (u_int8_t *)&ip, /* target protocol address */ NULL, /* Payload */ 0, /* Length of payload */ lntag, /* libnet context pointer */ 0 /* packet id */ ) == -1 ) { cl_log(LOG_ERR, "libnet_build_arp failed:"); return NULL; } /* Ethernet header */ if (get_hw_addr((char *)device, device_mac) < 0) { cl_log(LOG_ERR, "Cannot find mac address for %s", device); return NULL; } if (libnet_build_ethernet(bcast_mac, device_mac, ETHERTYPE_ARP, NULL, 0 , lntag, 0) == -1 ) { cl_log(LOG_ERR, "libnet_build_ethernet failed:"); return NULL; } return lntag; }
/* We really only need to spoof the gw.. It's just lagged enough usually that * we might just win the race :)*/ void arp_spoof_reply(struct libnet_link_int *iface, unsigned char *spa, unsigned char *sha, unsigned char *tpa, unsigned char *tha) { unsigned char pkt[ETH_H + ARP_H]; libnet_build_ethernet(tha, sha, ETHERTYPE_ARP, NULL, 0, pkt); libnet_build_arp(ARPHRD_ETHER, ETHERTYPE_IP, ETH_ALEN, 4, ARPOP_REPLY, sha, spa, tha, tpa, NULL, 0, pkt + ETH_H); if(libnet_write_link_layer(iface, INTERFACE, pkt, sizeof(pkt)) != sizeof(pkt)) perror("Write link layer"); }
int init(void) { /* Ethernet header */ etherhdr.ether_type = ETHERTYPE_IP; /* Ethernet type IP */ memset(etherhdr.ether_shost, 0, 6); /* Ethernet source address */ memset(etherhdr.ether_dhost, 0xff, 6); /* Ethernet destination address */ /* IP header */ memset(&iphdr.ip_src.s_addr, 0, 4); /* IP source address 0.0.0.0 * (pretend to be proxy to * avoid being elected as master) */ inet_aton(MCAST_ALL_HOSTS, &iphdr.ip_dst); /* IP destination address */ iphdr.ip_tos = 0; /* IP type of services */ iphdr.ip_id = (u_int16_t)libnet_get_prand(PRu16); /* IP ID */ iphdr.ip_p = IPPROTO_IGMP; /* IP protocol IGMP */ iphdr.ip_off = 0; /* IP fragmentation offset */ iphdr.ip_ttl = 1; /* IP TTL - set to 1 purposely */ /* IGMP header */ igmphdr.igmp_type = IGMP_MEMBERSHIP_QUERY; /* IGMP type */ igmphdr.igmp_code = 0; /* IGMP code */ inet_aton(MCAST_MDNS, &igmphdr.igmp_group); /* IGMP group address */ /* Create packet */ linkint = libnet_open_link_interface(LINK_INTERFACE, errbuf); if (linkint == NULL) { return -1; } igmp_packetlen = LIBNET_ETH_H + LIBNET_IP_H + LIBNET_IGMP_H; if (libnet_init_packet(igmp_packetlen, &pkt) == -1) { return -1; } libnet_build_ethernet(etherhdr.ether_dhost, etherhdr.ether_shost, ETHERTYPE_IP, NULL, 0, pkt); libnet_build_ip(LIBNET_IGMP_H, iphdr.ip_tos, iphdr.ip_id, iphdr.ip_off, iphdr.ip_ttl, iphdr.ip_p, iphdr.ip_src.s_addr, iphdr.ip_dst.s_addr, NULL, 0, pkt + LIBNET_ETH_H); libnet_build_igmp(igmphdr.igmp_type, igmphdr.igmp_code, igmphdr.igmp_group.s_addr, NULL, 0, pkt + LIBNET_ETH_H + LIBNET_IP_H); libnet_do_checksum(pkt + LIBNET_ETH_H, IPPROTO_IP, LIBNET_IP_H); libnet_do_checksum(pkt + LIBNET_ETH_H, IPPROTO_IGMP, LIBNET_IGMP_H); }
static int arp_send(libnet_t *l, int op, u_int8_t *sha, in_addr_t spa, u_int8_t *tha, in_addr_t tpa) { int retval; if (sha == NULL && (sha = (u_int8_t *)libnet_get_hwaddr(l)) == NULL) { return (-1); } if (spa == 0) { if ((spa = libnet_get_ipaddr4(l)) == -1) return (-1); } if (tha == NULL) tha = "\xff\xff\xff\xff\xff\xff"; libnet_autobuild_arp(op, sha, (u_int8_t *)&spa, tha, (u_int8_t *)&tpa, l); libnet_build_ethernet(tha, sha, ETHERTYPE_ARP, NULL, 0, l, 0); fprintf(stderr, "%s ", ether_ntoa((struct ether_addr *)sha)); if (op == ARPOP_REQUEST) { fprintf(stderr, "%s 0806 42: arp who-has %s tell %s\n", ether_ntoa((struct ether_addr *)tha), libnet_addr2name4(tpa, LIBNET_DONT_RESOLVE), libnet_addr2name4(spa, LIBNET_DONT_RESOLVE)); } else { fprintf(stderr, "%s 0806 42: arp reply %s is-at ", ether_ntoa((struct ether_addr *)tha), libnet_addr2name4(spa, LIBNET_DONT_RESOLVE)); fprintf(stderr, "%s\n", ether_ntoa((struct ether_addr *)sha)); } retval = libnet_write(l); char *libnetError = libnet_geterror(l); if (strlen(libnetError) > 0) { fprintf(stderr, "%s", libnetError); exit(1); } libnet_clear_packet(l); return retval; }
/* TODO: List of all IP's to snoop */ void arp_spoof_request(struct link_int *iface, char *dev, unsigned char *gwip, unsigned char *tgtip) { unsigned char pkt[ARP_H + ETH_H]; unsigned char bcast[ETH_ALEN]; unsigned char zero[ETH_ALEN]; memset(bcast, 0xff, ETH_ALEN); memset(zero, 0x00, ETH_ALEN); libnet_build_ethernet(bcast, bcast, ETHERTYPE_ARP, NULL, 0, pkt); libnet_build_arp(ARPHRD_ETHER, ETH_P_IP, ETH_ALEN, 4, ARPOP_REQUEST, bcast, gwip, zero, tgtip, NULL, 0, pkt + ETH_H); if(libnet_write_link_layer(iface, dev, pkt, sizeof(pkt)) != sizeof(pkt)) perror("Write link layer"); }
/* We really only need to spoof the gw.. It's just lagged enough usually that * we might just win the race :)*/ void arp_spoof_reply(struct link_int *iface, char *dev, struct arp_hdr *arp) { unsigned char pkt[ETH_H + sizeof(struct arp_hdr)]; unsigned char bcast[ETH_ALEN]; unsigned char tmpIP[4]; memset(bcast, 0xff, ETH_ALEN); memcpy(arp->ar_tha, arp->ar_sha, ETH_ALEN); memcpy(tmpIP, arp->ar_tip, 4); memcpy(arp->ar_tip, arp->ar_sip, 4); memcpy(arp->ar_sip, tmpIP, 4); memcpy(arp->ar_sha, bcast, ETH_ALEN); arp->ar_op = ARPOP_REPLY; libnet_build_ethernet(arp->ar_tha, arp->ar_sha, ETHERTYPE_ARP, (unsigned char *)arp, sizeof(struct arp_hdr), pkt); }
void write_icmpether(struct libnet_link_int *iface, char *dev, struct eth_pair *dst, struct eth_pair *src) { unsigned char pkt[ETH_H + IP_H + ICMP_ECHO_H]; libnet_build_ethernet(dst->eth, src->eth, ETHERTYPE_IP, NULL, 0, pkt); libnet_build_ip(ICMP_ECHO_H, 0, 0, 0, 2, IPPROTO_ICMP, src->ip, dst->ip, NULL, 0, pkt + ETH_H); libnet_build_icmp_echo(ICMP_ECHO, 0, 0, 0, NULL, 0, pkt + ETH_H + IP_H); if(libnet_do_checksum(pkt+ETH_H, IPPROTO_IP, IP_H) == -1) perror("ipv4 cksum"); if(libnet_do_checksum(pkt+ETH_H, IPPROTO_ICMP, ICMP_ECHO_H) == -1) perror("ipv4 cksum"); if(libnet_write_link_layer(iface, dev, pkt, sizeof(pkt)) != sizeof(pkt)) perror("Write link layer"); }
void main(int argc,char *argv[]) { if(argc!=2) { printf("用法错误:需要添加目的IP参数\n"); return; } char error_buf[LIBNET_ERRBUF_SIZE]; libnet_t *l; l= libnet_init(LIBNET_LINK_ADV,NULL,error_buf); if(l==NULL) { printf("libnet初始化错误:%s\n",error_buf); libnet_destroy(l); return; } u_char arp_sha[6]={0x00,0x1e,0x90,0xc7,0xe3,0xbb}; u_char arp_dha[6]={0x00,0x00,0x00,0x00,0x00,0x00}; char *arp_src_ip="192.168.1.6"; u_int arp_spa=libnet_name2addr4(l,arp_src_ip,LIBNET_RESOLVE); char *arp_dst_ip=argv[1]; u_int arp_dpa=libnet_name2addr4(l,arp_dst_ip,LIBNET_RESOLVE); if(libnet_build_arp(ARPHRD_ETHER,ETHERTYPE_IP,6,4,ARPOP_REQUEST,arp_sha,(u_int8_t *)&arp_spa,arp_dha,(u_int8_t *)&arp_dpa,NULL,0,l,0)==-1) { printf("构造ARP错误:%s\n",libnet_geterror(l)); libnet_destroy(l); return; } u_char ether_sha[6]={0x00,0x1e,0x90,0xc7,0xe3,0xbb}; u_char ether_dha[6]={0xff,0xff,0xff,0xff,0xff,0xff}; if(libnet_build_ethernet(ether_dha,ether_sha,ETHERTYPE_ARP,NULL,0,l,0)==-1) { printf("构造以太错误:%s\n",libnet_geterror(l)); libnet_destroy(l); return; } //libnet_diag_dump_pblock(l); //libnet_diag_dump_context(l); libnet_write(l); libnet_destroy(l); return; }
void write_ipether(struct libnet_link_int *iface, char *dev, struct eth_pair *src) { unsigned char pkt[ETH_H + IP_H + UDP_H]; struct eth_pair *dst = get_dest(); libnet_build_ethernet(dst->eth, src->eth, ETHERTYPE_IP, NULL, 0, pkt); libnet_build_ip(UDP_H, 0, 0, 0, 2, IPPROTO_UDP, src->ip, dst->ip, NULL, 0, pkt + ETH_H); libnet_build_udp(rand() % (0xffff-1024) + 1024, rand() % 1024, NULL, 0, pkt + ETH_H + IP_H); if(libnet_do_checksum(pkt+ETH_H, IPPROTO_IP, IP_H) == -1) perror("ipv4 cksum"); if(libnet_do_checksum(pkt+ETH_H, IPPROTO_UDP, UDP_H) == -1) perror("ipv4 cksum"); if(libnet_write_link_layer(iface, dev, pkt, sizeof(pkt)) != sizeof(pkt)) perror("Write link layer"); }
static int build_ethernet(libnet_t* l, libnet_ptag_t ptag) { uint8_t enet_src[6] = {0x11, 0x11, 0x11, 0x11, 0x11, 0x11}; uint8_t enet_dst[6] = {0x22, 0x22, 0x22, 0x22, 0x22, 0x22}; ptag = libnet_build_ethernet( enet_dst, /* ethernet destination */ enet_src, /* ethernet source */ ETHERTYPE_IP, /* protocol type */ NULL, /* payload */ 0, /* payload size */ l, /* libnet handle */ ptag); /* libnet id */ ptag_error(l, ptag); return ptag; }
int broadcast_arp(uint16_t type, uint32_t addr) { uint8_t broadcast[6]; memset(broadcast, 0xFF, 6); bob.arp=libnet_build_arp( ARPHRD_ETHER, /* ethernet */ ETHERTYPE_IP, /* proto for addr res */ 6, /* hardware addr len */ 4, /* proto addr len */ type, /* arp type */ (uint8_t *)&bob.shwaddr.octet[0], /* source */ (uint8_t *)&addr, /* ip src */ &broadcast[0], /* dst hw */ (uint8_t *)&bob.saddr, /* src ip */ NULL, /* no pl */ 0, /* zero len */ bob.l, /* libnet handle */ bob.arp); /* arp id? */ if (bob.arp < 0) { fprintf(stderr, "Can't build ARP header: %s\n", libnet_geterror(bob.l)); return -1; } bob.eth=libnet_build_ethernet( &broadcast[0], /* dest host hw addr */ (uint8_t *)&bob.shwaddr.octet[0], /* dest host src addr */ ETHERTYPE_ARP, /* ethernet, arp */ NULL, /* no payload */ 0, /* obviously is 0 len */ bob.l, /* libnet handle */ bob.eth); /* eth id? */ if (bob.eth < 0) { fprintf(stderr, "Can't build ethernet header: %s\n", libnet_geterror(bob.l)); return -1; } if (libnet_write(bob.l) == -1) { fprintf(stderr, "%s", libnet_geterror(bob.l)); return -1; } return 1; }
int send_arp(struct myetheraddr *dst) { printf("Sending ARP resp to: "); decode_mac((uint8_t *)&dst->octet[0]); printf("\n"); fflush(stdout); bob.arp=libnet_build_arp( ARPHRD_ETHER, /* ethernet follows */ ETHERTYPE_IP, /* proto for addr res */ 6, /* hardware addr len */ 4, /* proto addr len */ ARPOP_REPLY, /* duh */ (uint8_t *)&bob.shwaddr.octet[0], /* source */ (uint8_t *)&bob.saddr, /* ip src */ (uint8_t *)&dst->octet[0], /* dst hw */ (uint8_t *)&bob.saddr, /* src ip */ NULL, /* no pl */ 0, /* zero len */ bob.l, /* libnet handle */ bob.arp); /* arp id? */ if (bob.arp < 0) { fprintf(stderr, "Can't build ARP header: %s\n", libnet_geterror(bob.l)); return -1; } bob.eth=libnet_build_ethernet( (uint8_t *)&dst->octet[0], /* dest host hw addr */ (uint8_t *)&bob.shwaddr.octet[0], /* dest host src addr */ ETHERTYPE_ARP, /* ethernet, arp */ NULL, /* no payload */ 0, /* obviously is 0 len */ bob.l, /* libnet handle */ bob.eth); /* eth id? */ if (bob.eth < 0) { fprintf(stderr, "Can't build ethernet header: %s\n", libnet_geterror(bob.l)); return -1; } if (libnet_write(bob.l) == -1) { fprintf(stderr, "%s", libnet_geterror(bob.l)); return -1; } return 1; }
int main(int argc, char* argv[]) { srandomdev(); if (argc != 2) { errx(EX_USAGE, "Usage: %s <device>", argv[0]); } char* dev = argv[1]; char errbuf[LIBNET_ERRBUF_SIZE]; libnet_t* context = libnet_init(LIBNET_LINK, dev, errbuf); if (context == NULL) { errx(EX_UNAVAILABLE, "libnet_init: %s", errbuf); } uint8_t buf[MAX_DHCP_SIZE]; uint16_t size = MAX_DHCP_SIZE; build_dhcp_discover(source_mac, "lozenge", buf, &size); libnet_ptag_t ether_tag = LIBNET_PTAG_INITIALIZER; libnet_ptag_t ip_tag = LIBNET_PTAG_INITIALIZER; libnet_ptag_t udp_tag = LIBNET_PTAG_INITIALIZER; udp_tag = libnet_build_udp(68, 67, LIBNET_UDP_H + size, 0, buf, size, context, udp_tag); u_short ipid = random() & 0xffff; ip_tag = libnet_build_ipv4( LIBNET_IPV4_H + LIBNET_UDP_H + size, 0, ipid, 0, 0xff, IPPROTO_UDP, 0, 0, 0xffffffff, NULL, 0, context, ip_tag); ether_tag = libnet_build_ethernet(broadcast_mac, source_mac, ETHERTYPE_IP, NULL, 0, context, ether_tag); if (libnet_write(context) == -1) { libnet_destroy(context); errx(EX_UNAVAILABLE, "libnet_write"); } printf("Wrote\n"); libnet_destroy(context); return 0; }
int buildEth(int ack){ libnet_ptag_t etherTag; char temp[18]; if(ack){ memcpy(temp,DA,18); memcpy(DA,SA,18); memcpy(SA,temp,18); } etherTag = libnet_build_ethernet( DA, SA, ETHERTYPE_IP, NULL, 0, l, 0); return etherTag; }
void sendEth(uint8_t* src, uint8_t* dst, uint8_t* pl, uint8_t num, libnet_t* libnet){ static libnet_ptag_t t = 0; // 0x800 is ipv4 code for ethernet t = libnet_build_ethernet(dst, src, 0x0800, pl, num, libnet,t); if (t == -1){ fprintf(stderr, "Can't build ethernet header: %s\n", libnet_geterror(libnet)); return; } int ec = libnet_write(libnet); if (ec == -1){ fprintf(stderr, "Write error: %s\n", libnet_geterror(libnet)); } else{ fprintf(stdout, "Wrote %d byte Eth packet\n", ec); } }
int send_arp(struct libnet_link_int *l, u_char *device) { int n; u_char *buf; if (libnet_init_packet(LIBNET_ARP_H + LIBNET_ETH_H, &buf) == -1) { perror("libnet_init_packet memory:"); exit(EXIT_FAILURE); } /* * Ethernet header */ libnet_build_ethernet(enet_dst, enet_src, ETHERTYPE_ARP, NULL, 0, buf); /* * ARP header */ libnet_build_arp(ARPHRD_ETHER, ETHERTYPE_IP, 6, 4, ARPOP_REQUEST, enet_src, ip_src, enet_dst, ip_dst, NULL, 0, buf + LIBNET_ETH_H); n = libnet_write_link_layer(l, device, buf, LIBNET_ARP_H + LIBNET_ETH_H); printf("Wrote %d byte ARP packet through linktype %d\n", n, l->linktype); libnet_destroy_packet(&buf); return (n); }
int send_arp(struct link_int *l, u_long ip, u_char *device) { int n; u_char *buf; if (libnet_init_packet(ARP_H + ETH_H, &buf) == -1) { perror("libnet_init_packet memory:"); exit(EXIT_FAILURE); } /* * Ethernet header */ libnet_build_ethernet(enet_dst, enet_src, ETHERTYPE_ARP, NULL, 0, buf); /* * ARP header */ libnet_build_arp(ARPHRD_ETHER, ETHERTYPE_IP, 6, 4, ARPOP_REQUEST, enet_src, (u_char *)&ip, enet_dst, (u_char *)&ip, NULL, 0, buf + ETH_H); n = libnet_write_link_layer(l, device, buf, ARP_H + ETH_H); fprintf(stderr, "."); libnet_destroy_packet(&buf); return (n); }
uint16_t buildETH_h(libnet_t *l, uint8_t *DA, uint8_t *SA) { /*struct libnet_ether_addr * mac_addr; mac_addr = libnet_get_hwaddr(l); sprintf((char*)SA,"%02x:%02x:%02x:%02x:%02x:%02x", (uint8_t)mac_addr->ether_addr_octet[0], (uint8_t)mac_addr->ether_addr_octet[1], (uint8_t)mac_addr->ether_addr_octet[2], (uint8_t)mac_addr->ether_addr_octet[3], (uint8_t)mac_addr->ether_addr_octet[4], (uint8_t)mac_addr->ether_addr_octet[5]); sprintf((char*)DA,"%02x:%02x:%02x:%02x:%02x:%02x", 0xD0,0x27,0x88,0xBC,0xA8,0xE9); */ /*printf("DA: "); for (int i=0;i<6;i++){ printf("%c", DA[i]); //DA+=(uint8_t)1; } printf("\nSA: "); for (int i=0;i<6;i++){ printf("%c", SA[i]); //SA+=(uint8_t)1; } printf("\n");*/ eth_t = libnet_build_ethernet ( DA, SA, ETHERTYPE_IP, NULL, 0, l, eth_t); if(!eth_t) { fprintf(stderr,"libnet_build_ethernet() failed: %s\n",errbuf); goto bad; } return 1; bad: return -1; }
static int arp_send(libnet_t *l, int op, u_int8_t *sha, in_addr_t spa, u_int8_t *tha, in_addr_t tpa, u_int8_t *me) { int retval; if (!me) me = sha; libnet_autobuild_arp(op, sha, (u_int8_t *)&spa, tha, (u_int8_t *)&tpa, l); libnet_build_ethernet(tha, me, ETHERTYPE_ARP, NULL, 0, l, 0); fprintf(stderr, "%s ", ether_ntoa((struct ether_addr *)me)); if (op == ARPOP_REQUEST) { fprintf(stderr, "%s 0806 42: arp who-has %s tell %s\n", ether_ntoa((struct ether_addr *)tha), libnet_addr2name4(tpa, LIBNET_DONT_RESOLVE), libnet_addr2name4(spa, LIBNET_DONT_RESOLVE)); } else { fprintf(stderr, "%s 0806 42: arp reply %s is-at ", ether_ntoa((struct ether_addr *)tha), libnet_addr2name4(spa, LIBNET_DONT_RESOLVE)); fprintf(stderr, "%s\n", ether_ntoa((struct ether_addr *)sha)); } retval = libnet_write(l); if (retval) fprintf(stderr, "%s", libnet_geterror(l)); libnet_clear_packet(l); return retval; }
/*- -- ptag = n:eth{src=ethmac, dst=ethmac, type=int, payload=str, ptag=int} type is optional, defaults to IP ptag is optional, defaults to creating a new protocol block */ static int lnet_eth (lua_State *L) { libnet_t* ud = checkudata(L); const char* src = v_arg_string(L, 2, "src"); const char* dst = v_arg_string(L, 2, "dst"); int type = v_arg_integer_opt(L, 2, "type", ETHERTYPE_IP); uint32_t payloadsz = 0; const uint8_t* payload = checkpayload(L, 2, &payloadsz); int ptag = lnet_arg_ptag(L, ud, 2, LIBNET_PBLOCK_ETH_H); #ifdef NET_DUMP printf("net eth src %s dst %s type %d payloadsz %lu ptag %d\n", src, dst, type, payloadsz, ptag); #endif { eth_addr_t src_n = check_eth_pton(L, src, "src"); eth_addr_t dst_n = check_eth_pton(L, dst, "dst"); ptag = libnet_build_ethernet(dst_n.data, src_n.data, type, payload, payloadsz, ud, ptag); } check_error(L, ud, ptag); lua_pushinteger(L, ptag); return 1; }
libnet_ptag_t libnet_build_link(u_int8_t *dst, u_int8_t *src, u_int8_t *oui, u_int16_t type, u_int8_t *payload, u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag) { u_int8_t org[3] = {0x00, 0x00, 0x00}; switch (l->link_type) { /* add FDDI */ case DLT_EN10MB: return (libnet_build_ethernet(dst, src, type, payload, payload_s, l, ptag)); case DLT_IEEE802: return (libnet_build_token_ring(LIBNET_TOKEN_RING_FRAME, LIBNET_TOKEN_RING_LLC_FRAME, dst, src, LIBNET_SAP_SNAP, LIBNET_SAP_SNAP, 0x03, org, type, payload, payload_s, l, ptag)); default: break; } snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): linktype %d not supported\n", __func__, l->link_type); return (-1); }
int8_t dot1x_send_raw(struct interface_data *iface, u_int8_t *payload, u_int16_t len, u_int8_t *mac_source, u_int8_t *mac_dest) { libnet_ptag_t t; int32_t sent; t = libnet_build_ethernet( mac_dest, /* ethernet destination */ mac_source, /* ethernet source */ ETHERTYPE_EAP, /* protocol type */ payload, /* payload */ len, /* payload size */ iface->libnet_handler, /* libnet handle */ 0); /* libnet id */ if (t == -1) { thread_libnet_error("Can't build Ethernet_II header",iface->libnet_handler); libnet_clear_packet(iface->libnet_handler); return -1; } sent = libnet_write(iface->libnet_handler); if (sent == -1) { thread_libnet_error("libnet_write error", iface->libnet_handler); libnet_clear_packet(iface->libnet_handler); return -1; } libnet_clear_packet(iface->libnet_handler); protocols[PROTO_DOT1X].packets_out++; iface->packets_out[PROTO_DOT1X]++; return 0; }
int main() { int c, i, seqn; libnet_t *l; libnet_ptag_t tcp, ip, eth; uint8_t *payload, SA[6], DA[6]; ushort payload_s; uint32_t src_ip, dst_ip; uint16_t src_prt, dst_prt; char errbuf[LIBNET_ERRBUF_SIZE]; struct libnet_ether_addr *enet_src; payload_s = 10; payload = malloc(payload_s*sizeof(uint8_t)); memset(payload,0,payload_s); l = libnet_init(LIBNET_LINK, "nf2c0", errbuf); if(l == NULL){ printf("libnet_init() error\n"); goto bad; } enet_src = libnet_get_hwaddr(l); src_ip = libnet_get_ipaddr4(l); dst_ip = (192) | (168<<8) | (101<<16) | (20); //char *srcip; //srcip = libnet_addr2name4(dst_ip,LIBNET_DONT_RESOLVE); //printf("conversion: %s %d\n", srcip,LIBNET_IPV4_H+LIBNET_TCP_H+LIBNET_ETH_H+payload_s); //return 0; sprintf((char*)SA,"%02x:%02x:%02x:%02x:%02x:%02x", (uint8_t)enet_src->ether_addr_octet[0], (uint8_t)enet_src->ether_addr_octet[1], (uint8_t)enet_src->ether_addr_octet[2], (uint8_t)enet_src->ether_addr_octet[3], (uint8_t)enet_src->ether_addr_octet[4], (uint8_t)enet_src->ether_addr_octet[5]); sprintf((char*)DA,"%02x:%02x:%02x:%02x:%02x:%02x", 0x0,0x4E,0x46,0x32,0x43,0x0); src_prt = 80; dst_prt = 1010; //src_ip = (192) | (164<<8)| (0<<16) | (55<<24); tcp = ip = eth = LIBNET_PTAG_INITIALIZER; //for(i=1;i<11;i++){ for(i=0;i<10;i++){ seqn=i*(LIBNET_TCP_H+payload_s+1); tcp = libnet_build_tcp( src_prt, dst_prt, seqn, seqn+LIBNET_TCP_H+payload_s+1, TH_SYN | TH_ACK, 0, 0, 10, LIBNET_TCP_H+payload_s, payload, payload_s, l, tcp); if(tcp ==-1){ printf("libnet_build_tcp() error\n"); goto bad; } ip = libnet_build_ipv4( LIBNET_IPV4_H+LIBNET_TCP_H+payload_s, 0, 242, 0, 64, IPPROTO_TCP, 0, src_ip, dst_ip, NULL, 0, l, ip); if(ip==-1){ printf("libnet_build_ipv4() error\n"); goto bad; } eth = libnet_build_ethernet( DA, SA, ETHERTYPE_IP, NULL, 0, l, eth); if(eth==-1){ printf("libnet_build_ethernet() error\n"); goto bad; } c = libnet_write(l); if(c==-1){ printf("libnet_write() error\n"); goto bad; } else printf("Wrote %d byte TCP packet.\nSeqNum: %d, ackNum: %d\n\n",c,seqn,seqn+LIBNET_TCP_H+payload_s+1); //printf("Wrote %d byte TCP packet.\n",c); sleep(1); } libnet_destroy(l); return 0; bad: libnet_destroy(l); exit(EXIT_FAILURE); }
int main(int argc, char **argv) { int c, n, i, proto, packet_size, pause_us, retransmit, file_size, num_packets, npacket; char *end; libnet_t *ln_ctx; char ln_errbuf[LIBNET_ERRBUF_SIZE]; struct libnet_ether_addr *ln_hwaddr; libnet_ptag_t ln_ptag; pcap_t *pcap_ctx; char pcap_errbuf[PCAP_ERRBUF_SIZE], pcap_fp_str[64]; struct bpf_program pcap_fp; struct pcap_pkthdr pcap_hdr; FILE *fp; unsigned char buf[ETH_DATA_LEN], dest_mac_addr[ETH_ALEN]; struct pkt_hdr *pkt_hdr; struct vlan_eth_hdr *vlan_eth_hdr; proto = 0xCAFE; packet_size = ETH_DATA_LEN - PKT_HDR_SIZE; pause_us = 1000; retransmit = 3; while ((c = getopt(argc, argv, "p:s:w:r:")) != -1) { switch (c) { case 'p': proto = strtol(optarg, &end, 0); if ((*end != '\0')) usage(argv[0]); break; case 's': packet_size = strtol(optarg, &end, 0); if ((*end != '\0')) usage(argv[0]); if ((packet_size <= 0) || (packet_size > (ETH_DATA_LEN - PKT_HDR_SIZE))) packet_size = ETH_DATA_LEN - PKT_HDR_SIZE; break; case 'w': pause_us = strtol(optarg, &end, 0); if ((*end != '\0')) usage(argv[0]); if (pause_us <= 0) pause_us = 1; break; case 'r': retransmit = strtol(optarg, &end, 0); if ((*end != '\0')) usage(argv[0]); if (retransmit < 0) retransmit = 0; break; case '?': default: fprintf(stderr, "unrecognized option: %c\n", c); usage(argv[0]); } } if (argc != (optind + 3)) usage(argv[0]); if (strlen(argv[optind]) <= 0) usage(argv[0]); ln_ctx = libnet_init(LIBNET_LINK, argv[optind], ln_errbuf); if (ln_ctx == NULL) { fprintf(stderr, "couldn't initialize libnet context: %s\n", ln_errbuf); exit(1); } if (str2mac(argv[optind + 1], dest_mac_addr) != 0) usage(argv[0]); pcap_ctx = pcap_open_live(argv[optind], BUFSIZ, 1, 1000, pcap_errbuf); if (pcap_ctx == NULL) { fprintf(stderr, "couldn't initialize pcap context: %s\n", pcap_errbuf); exit(1); } sprintf(pcap_fp_str, "ether proto 0x%04x and ether src %02x:%02x:%02x:%02x:%02x:%02x", proto, dest_mac_addr[0], dest_mac_addr[1], dest_mac_addr[2], dest_mac_addr[3], dest_mac_addr[4], dest_mac_addr[5]); printf("pcap filter: %s\n", pcap_fp_str); if (pcap_compile(pcap_ctx, &pcap_fp, pcap_fp_str, 0, PCAP_NETMASK_UNKNOWN) == -1) { fprintf(stderr, "couldn't compile pcap filter: %s\n", pcap_geterr(pcap_ctx)); exit(1); } if (pcap_setfilter(pcap_ctx, &pcap_fp) == -1) { fprintf(stderr, "couldn't set pcap filter: %s\n", pcap_geterr(pcap_ctx)); exit(1); } fp = fopen(argv[optind + 2], "r"); if (fp == NULL) { fprintf(stderr, "couldn't open file '%s'\n", argv[optind + 2]); exit(1); } fseek(fp, 0, SEEK_END); file_size = ftell(fp); fseek(fp, 0, SEEK_SET); printf("file size #%d\n", file_size); if (file_size == 0) { printf("file is empty, terminating\n"); exit(0); } ln_hwaddr = libnet_get_hwaddr(ln_ctx); ln_ptag = libnet_build_ethernet(dest_mac_addr, (u_int8_t *) ln_hwaddr, proto, NULL, 0, ln_ctx, 0); if (ln_ptag == -1) { fprintf(stderr, "couldn't create libnet packet: %s\n", libnet_geterror(ln_ctx)); exit(1); } num_packets = (file_size + packet_size - 1) / packet_size; npacket = 0; while ((n = fread(buf + PKT_HDR_SIZE, 1, packet_size, fp)) > 0) { pkt_hdr = (struct pkt_hdr *) buf; memset(pkt_hdr, 0, PKT_HDR_SIZE); pkt_hdr->magic = htonl(PKT_MAGIC); pkt_hdr->offset = htonl(npacket * packet_size); pkt_hdr->size = htonl(n); if (n < packet_size) pkt_hdr->flags |= PKT_FLAG_LAST; pkt_hdr->flags = htonl(pkt_hdr->flags); npacket++; if (libnet_build_ethernet(dest_mac_addr, (u_int8_t *) ln_hwaddr, proto, buf, n + PKT_HDR_SIZE, ln_ctx, ln_ptag) == -1) { fprintf(stderr, "couldn't modify libnet packet #%d: %s\n", npacket, libnet_geterror(ln_ctx)); exit(1); } for (i = 0; i < retransmit + 1; i++) { printf("sending packet #%d of #%d\n", npacket, num_packets); if ((libnet_write(ln_ctx)) == -1) { fprintf(stderr, "couldn't send packet #%d: %s\n", npacket, libnet_geterror(ln_ctx)); exit(1); } /*vlan_eth_hdr = (struct vlan_eth_hdr *) pcap_next(pcap_ctx, &pcap_hdr); if (vlan_eth_hdr == NULL) { fprintf(stderr, "timeout ack for packet #%d\n", npacket); goto next; }*/ /*if (pcap_hdr.len < (VLAN_ETH_HLEN + PKT_HDR_SIZE)) { fprintf(stderr, "invalid ack for packet #%d\n", npacket); goto next; } pkt_hdr = (struct pkt_hdr *) (vlan_eth_hdr + 1); pkt_hdr->offset = ntohl(pkt_hdr->offset); if (pkt_hdr->offset == ((npacket - 1) * packet_size + n)) { printf("received ack for packet #%d\n", npacket); break; } else { fprintf(stderr, "bad ack for packet #%d, offset 0x%x\n", npacket, pkt_hdr->offset); goto next; }*/ next: usleep(1000); } if (i == (retransmit + 1)) { //fprintf(stderr, "no ack received for packet #%d\n", npacket); //exit(1); } if (pause_us > 0) usleep(pause_us); } fclose(fp); pcap_close(pcap_ctx); libnet_destroy(ln_ctx); exit(0); }
int send_eth() { // Tasks: // 1. Check 'eth_src_txt' and 'eth_dst_txt' which contain either a MAC address or a keyword // 'eth_dst' can be set without having 'eth_src_txt' specified (the next 6 bytes of the // 'arg_string' will be used). But if 'eth_src_txt' is given then also 'eth_dst_txt' // should have been specified, otherwise a default (ff-ff-ff-ff-ff-ff) will be used. // 2. Check whether 'arg_string' contains a hex-string. If YES then convert it into an // 'eth_payload' and extract eth_type. // 3. Apply 'padding' if specified // 4. Check if frame has at least minimum length (14 Bytes). // 5. Send frame 'count' times and // 6. Apply 'delay' (make precautions for better delay functions) int src, // flag telling whether user has specified a source address dst, // flag telling whether user has specified a destination address src_random=0, dst_random=0, byte_ptr=1, bytestring_s=0, min_size=15, pad=0, repeat, loop, update, i=0, j=0; char err_buf[LIBNET_ERRBUF_SIZE], message[MAX_PAYLOAD_SIZE*3]; u_int8_t bytestring[MAX_PAYLOAD_SIZE]; libnet_ptag_t t; libnet_t *l; if (tx.dot1Q) { fprintf(stderr," Note: raw layer 2 mode does not support 802.1Q builder.\n" " If you want to create VLAN tags then you must do it by hand.\n"); exit(1); } if (tx.mpls) { fprintf(stderr," Note: raw layer 2 mode does not support MPLS builder.\n" " If you want to create MPLS labels then you must do it by hand.\n"); exit(1); } // So other functions can use this function for sending Ethernet frames // These other functions must set dst, src, type and payload! if (tx.eth_params_already_set) goto ALL_SPECIFIED; if ((tx.padding) && (tx.padding<15)) // Note: ignored if padding==0 { tx.padding=15; if (mz_port) { cli_print(gcli, "Note: Set padding to 15 bytes (total length)\n"); } else fprintf(stderr, " mz/send_eth: [Note] adjusted minimum frame size to 15 bytes.\n"); } // Create a temporal, local bytestring: // for (i=0; i<MAX_PAYLOAD_SIZE; i++) bytestring[i]=0x00; bytestring_s = str2hex (tx.arg_string, bytestring, MAX_PAYLOAD_SIZE); // Set the flags to shorten subsequent decisions: src = strlen(tx.eth_src_txt); dst = strlen(tx.eth_dst_txt); // IN ANY CASE if src has been specified: // if (src) { // Evaluate Ethernet CLI options (-a and -b) if (check_eth_mac_txt(ETH_SRC)) // if true then problem! { // use own (already set in init.c) } src_random = tx.eth_src_rand; // local vars are faster } // IN ANY CASE if dst has been specified: // if (dst) { // Evaluate Ethernet CLI options (-a and -b) if (check_eth_mac_txt(ETH_DST)) // if true then problem! { str2hex("ff:ff:ff:ff:ff:ff",tx.eth_dst, 6); // the default } dst_random = tx.eth_dst_rand; // local vars are faster } // Catch errors with too short bytestrings: // if (src) { // bytestring only needs to contain eth_type min_size-=12; } else if (dst) { // bytstring must contain src and type min_size-=6; } if (bytestring_s < min_size) { j = min_size - bytestring_s; // this is what's missing bytestring_s += j; // note that bytestring has been filled up with 0x00, so we can do that } // ADDENDUM: If src specified, dst missing: // if ( (!dst) && (src) ) { str2hex_mac ("ff:ff:ff:ff:ff:ff", tx.eth_dst); } // ADDENDUM: If dst specified, src missing: // if ((dst) && (!src)) { // Get eth_src from bytestring: if (bytestring_s>=6) { (void) getbytes (bytestring, tx.eth_src, byte_ptr, byte_ptr+5); byte_ptr=7; // now points to eth_type within bytestring } } // FINALLY: If both dst and src have NOT been specified: // if ((!dst) && (!src)) { if (bytestring_s>=6) { (void) getbytes (bytestring, tx.eth_dst, byte_ptr, byte_ptr+5); byte_ptr=7; } if (bytestring_s>=12) { (void) getbytes (bytestring, tx.eth_src, byte_ptr, byte_ptr+5); byte_ptr=13; // points to eth_type } } // Set eth_type: // if (bytestring_s>=2) { tx.eth_type = 256 * bytestring[byte_ptr-1] + bytestring[byte_ptr]; // don't forget: byte_ptr counts from 1 not 0 byte_ptr+=2; // points to first payload byte (if available) } // Get remaining payload: // if ( (tx.eth_payload_s = bytestring_s - byte_ptr +1) > 0 ) // if there are any remaining bytes { (void) getbytes (bytestring, tx.eth_payload, byte_ptr, bytestring_s); } // Add padding if desired. // Note: padding means 'extend to given length' (not 'add these number of bytes') if (tx.padding) { pad = tx.padding - (14 + tx.eth_payload_s); // number of additonal pad bytes required for (i=0; i<pad; i++) { // tx.eth_payload[i+tx.eth_payload_s] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256); tx.eth_payload[i+tx.eth_payload_s] = 0x00; } tx.eth_payload_s += pad; } ALL_SPECIFIED: // *** All Ethernet parameters have been determined ! // *** Now let's send the frame! l = libnet_init (LIBNET_LINK_ADV, tx.device, err_buf ); if (l == NULL) { fprintf(stderr, " mz/send_eth: libnet_init() failed (%s)", err_buf); return -1; } repeat=1; if (tx.count == 0) { loop = 1000000; if (!quiet) fprintf(stderr, " mz: !!! Infinite mode! Will send frames until you stop Mausezahn!!!\n"); } else loop = tx.count; if ( (!quiet) && (!tx.delay) && (tx.count==0) ) fprintf(stderr, " mz: !!! Will send at maximum frame rate without feedback!!!\n"); t=0; update=1; // this is for the statistics: mz_start = clock(); total_d = tx.count; while (repeat) { if (tx.count!=0) repeat=0; // count=0 means repeat ad inifinitum for (i=0; i<loop; i++) { if (src_random) { tx.eth_src[0] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256) & 0xFE; tx.eth_src[1] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256); tx.eth_src[2] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256); tx.eth_src[3] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256); tx.eth_src[4] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256); tx.eth_src[5] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256); update=1; } if (dst_random) { tx.eth_dst[0] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256); tx.eth_dst[1] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256); tx.eth_dst[2] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256); tx.eth_dst[3] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256); tx.eth_dst[4] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256); tx.eth_dst[5] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256); update=1; } if (update) // new frame parameters { t = libnet_build_ethernet (tx.eth_dst, tx.eth_src, tx.eth_type, tx.eth_payload, tx.eth_payload_s, l, t); if (t == -1) { fprintf(stderr, " mz/send_eth: %s", libnet_geterror(l)); return -1; } if (verbose) { bs2str (tx.eth_dst, message, 6); // DA fprintf(stderr, " mz: send %s",message); bs2str (tx.eth_src, message, 6); // SA fprintf(stderr, " %s",message); type2str(tx.eth_type, message); fprintf(stderr, " %s",message); // Type bs2str (tx.eth_payload, message, tx.eth_payload_s); // Payload fprintf(stderr, " %s\n",message); } update=0; if (verbose==2) { fprintf(stderr, "\n"); fprintf(stderr, "*** NOTE: Simulation only! Nothing has been sent! ***\n"); libnet_destroy(l); return 0; } } libnet_write(l); if (tx.delay) { SLEEP (tx.delay); if ( (verbose) && (!src_random) && (!dst_random) ) { fprintf(stderr, "."); } } } // end for } // end while if (verbose) { if ((tx.delay) || (tx.count==0)) { fprintf(stderr,"\n"); } fprintf(stderr, " mz: sent %u frames.\n",loop); } libnet_destroy(l); return 0; }
void PcapGen::send_tcp_packet( const TcpState& from, const TcpState& to, uint8_t control, const uint8_t* data, size_t size) { if (!lnet_) return; libnet_build_tcp(from.port(), // sp to.port(), // dp from.seq(), // seq control & TH_ACK ? from.ack() : 0, // ack control, // control from.win(), // win 0, // sum 0, // urg LIBNET_TCP_H + size, // len data, (uint32_t)size, lnet_.get(), 0); libnet_build_ipv4(LIBNET_IPV4_H + LIBNET_TCP_H + size, // ip_len 0, // tos libnet_get_prand(LIBNET_PRu32), // id 0, // frag 64, // ttl IPPROTO_TCP, 0, // sum from.ip_addr(), // src to.ip_addr(), // dst 0, 0, lnet_.get(), 0); libnet_build_ethernet(from.mac_addr(), to.mac_addr(), ETHERTYPE_IP, 0, 0, lnet_.get(), 0); // see libnet_write() source code; calling internal function here uint32_t packet_size; uint8_t *packet_data; auto c = libnet_pblock_coalesce(lnet_.get(), &packet_data, &packet_size); if (c == -1) { libnet_clear_packet(lnet_.get()); return; } pcap_pkthdr packet_hdr; packet_hdr.ts.tv_sec = 0; packet_hdr.ts.tv_usec = 0; packet_hdr.len = packet_size; packet_hdr.caplen = packet_size; pcap_dump(reinterpret_cast<u_char *>(pcap_dump_.get()), &packet_hdr, packet_data); // see libnet_write() source code if (lnet_->aligner > 0) { packet_data -= lnet_->aligner; } free(packet_data); libnet_clear_packet(lnet_.get()); }