用户态协议栈设计实现udp,arp与icmp协议( 五 )

< 0) {continue;}if (pfd.revents & POLLIN) {unsigned char *stream = nm_nextpkt(nmr, &h);struct ethhdr *eh = (struct ethhdr *) stream;if (ntohs(eh->h_proto) == PROTO_IP) {struct ippkt *iph = (struct ippkt *) stream;if (iph->ip.type == PROTP_UPD) {struct udppkt *udp = (struct udppkt *) stream;int udplength = ntohs(udp->udp.length);udp->data[udplength - 8] = '';printf("udp ---> %sn", udp->data);struct udppkt udp_rt;echo_udp_pkt(udp, &udp_rt);nm_inject(nmr, &udp_rt, sizeof(struct udppkt));}else if (iph->ip.type == PROTO_ICMP) {struct icmppkt *icmp = (struct icmppkt *) stream;printf("icmp ---------- --> %d, %xn", icmp->icmp.type, icmp->icmp.check);if (icmp->icmp.type == 0x08) {struct icmppkt icmp_rt = {0};echo_icmp_pkt(icmp, &icmp_rt);nm_inject(nmr, &icmp_rt, sizeof(struct icmppkt));}}}else if (ntohs(eh->h_proto) == PROTO_ARP) {struct arppkt *arp = (struct arppkt *) stream;struct arppkt arp_rt;if (arp->arp.dip == inet_addr("192.168.109.100")) {echo_arp_pkt(arp, &arp_rt, "00:0c:29:1b:18:20");nm_inject(nmr, &arp_rt, sizeof(arp_rt));printf("arp retn");}}}}nm_close(nmr);}//gcc -o main main.c -I /netmap/sys///insmod netmap.ko测试能否回应ping请求

  • 开启netmap
每次重启使用前都需要insmod netmap.ko ,然后我们查看ls /dev/netmap -l,出现下面的设备就说明开启成功了 。
root@wxf:/netmap/LINUX# insmod netmap.ko root@wxf:/netmap/LINUX# ls /dev/netmap -lcrw------- 1 root root 10, 54 Jul 18 17:28 /dev/netmap运行上面的代码,发现udp,arp,icmp都可以正常解析和发送
用户态协议栈设计实现udp,arp与icmp协议

文章插图
 

【用户态协议栈设计实现udp,arp与icmp协议】


推荐阅读