当前位置:   article > 正文

UDP 设计实现_udp数据协议设计

udp数据协议设计

七层网络设计

                                                  

利用netmap拿到传输到服务端的原始数据;它是由mmap将网卡的数据映射到内存中;使用nm_open("eth0",NULL,0,NULL)返回的fd是指向网卡的,当网卡里面来数据时就可以通过fd来知晓,操作数据是操作内存里面的数据

ARP请求

1:客户端会定时的发生arp请求

2:将服务器的IP地址,mac地址记录在客户端的ARP表当中;如果服务器没有回应就不会记录。

ARP表的作用是发送给服务器的包,查看服务器的地址是否在ARP表当中,如果没有就不会进行发送

实现

  1. #include <stdio.h>
  2. #include <sys/poll.h>
  3. #include <arpa/inet.h>
  4. #define NETMAP_WITH_LIBS
  5. #include <net/netmap_user.h>
  6. #pragma pack(1)
  7. #define ETH_ADDR_LENGTH 6
  8. #define PROTO_IP 0x0800
  9. #define PROTO_ARP 0x0806
  10. #define PROTO_UDP 17
  11. #define PROTO_ICMP 1
  12. struct ethhdr {
  13. unsigned char h_dst[ETH_ADDR_LENGTH];//源mac地址
  14. unsigned char h_src[ETH_ADDR_LENGTH];//目的mac地址
  15. unsigned short h_proto;//协议,保存的是上层的协议,是IP协议还是ICMP协议,还是其他的协议
  16. }; // 14
  17. struct iphdr {
  18. unsigned char hdrlen:4,
  19. version:4; // 0x45
  20. unsigned char tos;
  21. unsigned short totlen;
  22. unsigned short id;
  23. unsigned short flag_offset; //
  24. unsigned char ttl; //time to livem,默认为64,每过一个网关就减一
  25. // 0x1234// htons
  26. unsigned char type;//传输层协议是udp,还是tcp,
  27. unsigned short check;//校验值
  28. unsigned int sip;//源ip
  29. unsigned int dip;//目的Ip
  30. }; // 20
  31. struct udphdr {
  32. unsigned short sport;//源端口号
  33. unsigned short dport;//目的端口号
  34. unsigned short length;//udp总长度
  35. unsigned short check;
  36. }; // 8
  37. struct udppkt {
  38. struct ethhdr eh; // 14
  39. struct iphdr ip; // 20
  40. struct udphdr udp; // 8
  41. unsigned char data[0];
  42. }; // sizeof(struct udppkt) ==
  43. struct arphdr {
  44. unsigned short h_type;
  45. unsigned short h_proto;
  46. unsigned char h_addrlen;
  47. unsigned char h_protolen;
  48. unsigned short oper;
  49. unsigned char smac[ETH_ADDR_LENGTH];
  50. unsigned int sip;
  51. unsigned char dmac[ETH_ADDR_LENGTH];
  52. unsigned int dip;
  53. };
  54. struct arppkt {
  55. struct ethhdr eh;
  56. struct arphdr arp;
  57. };
  58. int str2mac(char *mac, char *str) {
  59. char *p = str;
  60. unsigned char value = 0x0;
  61. int i = 0;
  62. while (p != '\0') {
  63. if (*p == ':') {
  64. mac[i++] = value;
  65. value = 0x0;
  66. } else {
  67. unsigned char temp = *p;
  68. if (temp <= '9' && temp >= '0') {
  69. temp -= '0';
  70. } else if (temp <= 'f' && temp >= 'a') {
  71. temp -= 'a';
  72. temp += 10;
  73. } else if (temp <= 'F' && temp >= 'A') {
  74. temp -= 'A';
  75. temp += 10;
  76. } else {
  77. break;
  78. }
  79. value <<= 4;
  80. value |= temp;
  81. }
  82. p ++;
  83. }
  84. mac[i] = value;
  85. return 0;
  86. }
  87. //回应arp
  88. void echo_arp_pkt(struct arppkt *arp, struct arppkt *arp_rt, char *mac) {
  89. memcpy(arp_rt, arp, sizeof(struct arppkt));
  90. memcpy(arp_rt->eh.h_dst, arp->eh.h_src, ETH_ADDR_LENGTH);
  91. str2mac(arp_rt->eh.h_src, mac);
  92. arp_rt->eh.h_proto = arp->eh.h_proto;
  93. arp_rt->arp.h_addrlen = 6;
  94. arp_rt->arp.h_protolen = 4;
  95. arp_rt->arp.oper = htons(2);
  96. str2mac(arp_rt->arp.smac, mac);
  97. arp_rt->arp.sip = arp->arp.dip;
  98. memcpy(arp_rt->arp.dmac, arp->arp.smac, ETH_ADDR_LENGTH);
  99. arp_rt->arp.dip = arp->arp.sip;
  100. }
  101. //
  102. int main() {
  103. struct nm_pkthdr h;
  104. struct nm_desc *nmr = nm_open("netmap:eth0", NULL, 0, NULL);
  105. if (nmr == NULL) return -1;
  106. struct pollfd pfd = {0};
  107. pfd.fd = nmr->fd;
  108. pfd.events = POLLIN;
  109. while (1) {
  110. int ret = poll(&pfd, 1, -1);
  111. if (ret < 0) continue;
  112. if (pfd.revents & POLLIN) {
  113. unsigned char *stream = nm_nextpkt(nmr, &h);
  114. struct ethhdr *eh = (struct ethhdr *)stream;
  115. if (ntohs(eh->h_proto) == PROTO_IP) {//如果是IP协议
  116. struct udppkt *udp = (struct udppkt *)stream;
  117. if (udp->ip.type == PROTO_UDP) { //如果是udp协议就直接打印
  118. int udplength = ntohs(udp->udp.length);
  119. udp->data[udplength-8] = '\0';
  120. printf("udp --> %s\n", udp->data);
  121. } else if (udp->ip.type == PROTO_ICMP) {//这个是处理ping的请求
  122. }
  123. } else if (ntohs(eh->h_proto) == PROTO_ARP) {//如果是arp协议
  124. struct arppkt *arp = (struct arppkt *)stream;
  125. struct arppkt arp_rt;
  126. if (arp->arp.dip == inet_addr("192.168.0.123")) { //
  127. echo_arp_pkt(arp, &arp_rt, "00:50:56:33:1c:ca");
  128. nm_inject(nmr, &arp_rt, sizeof(arp_rt));
  129. printf("arp ret\n");
  130. }
  131. }
  132. }
  133. }
  134. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/187137
推荐阅读
相关标签
  

闽ICP备14008679号