Linux网络编程:原始套接字编程及实例分析(3)

原文出处:http://network.51cto.com/art/201503/470400_2.htm 如有侵权,请及时告知,将第一时间删除

======================================================================================================================================

原始套接字编程和之前的UDP 编程差不多,无非就是创建一个套接字后,通过这个套接字接收数据或者发送数据。区别在于,原始套接字可以自行组装数据包(伪装本地 IP,本地 MAC),可以接收本机网卡上所有的数据帧(数据包)。另外,必须在管理员权限下才能使用原始套接字。

====================================================================================================================================

三、原始套接字实例:MAC头部报文分析

由上得知,我们可以通过原始套接字以及 recvfrom( ) 可以获取链路层的数据包,那我们接收的链路层数据包到底长什么样的呢?

链路层封包格式 

 

MAC 头部(有线局域网) 

 

注意:CRC、PAD 在组包时可以忽略

链路层数据包的其中一种情况:

 
 
  1. unsigned char msg[1024] = {    
  2.     //--------------组MAC--------14------    
  3.     0xb8, 0x88, 0xe3, 0xe1, 0x10, 0xe6, // dst_mac: b8:88:e3:e1:10:e6    
  4.     0xc8, 0x9c, 0xdc, 0xb7, 0x0f, 0x19, // src_mac: c8:9c:dc:b7:0f:19    
  5.     0x08, 0x00,                         // 类型:0x0800 IP协议    
  6.     // …… ……    
  7.     // …… ……    
  8. };   

接收的链路层数据包,并对其进行简单分析:

 
 
  1. #include <stdio.h>    
  2. #include <string.h>    
  3. #include <stdlib.h>    
  4. #include <sys/socket.h>    
  5. #include <netinet/in.h>    
  6. #include <arpa/inet.h>    
  7. #include <netinet/ether.h>    
  8.     
  9. int main(int argc,charchar *argv[])    
  10. {    
  11.     int i = 0;    
  12.     unsigned char buf[1024] = "";    
  13.     int sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));    
  14.     while(1)    
  15.     {    
  16.         unsigned char src_mac[18] = "";    
  17.         unsigned char dst_mac[18] = "";    
  18.         //获取链路层的数据帧    
  19.         recvfrom(sock_raw_fd, buf, sizeof(buf),0,NULL,NULL);    
  20.         //从buf里提取目的mac、源mac    
  21.         sprintf(dst_mac,"%02x:%02x:%02x:%02x:%02x:%02x", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);    
  22.         sprintf(src_mac,"%02x:%02x:%02x:%02x:%02x:%02x", buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]);    
  23.         //判断是否为IP数据包    
  24.         if(buf[12]==0x08 && buf[13]==0x00)    
  25.         {       
  26.             printf("______________IP数据报_______________\n");    
  27.             printf("MAC:%s >> %s\n",src_mac,dst_mac);    
  28.         }//判断是否为ARP数据包    
  29.         else if(buf[12]==0x08 && buf[13]==0x06)    
  30.         {    
  31.             printf("______________ARP数据报_______________\n");    
  32.             printf("MAC:%s >> %s\n",src_mac,dst_mac);    
  33.         }//判断是否为RARP数据包    
  34.         else if(buf[12]==0x80 && buf[13]==0x35)    
  35.         {    
  36.             printf("______________RARP数据报_______________\n");    
  37.             printf("MAC:%s>>%s\n",src_mac,dst_mac);    
  38.         }    
  39.     }    
  40.     return 0;    
  41. }   

记得以管理者权限运行程序:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值