java发送原始套接字,Linux网络编程——原始套接字实例:MAC 头部报文分析

通过《Linux网络编程——原始套接字编程》得知,我们可以通过原始套接字以及 recvfrom( ) 可以获取链路层的数据包,那我们接收的链路层数据包到底长什么样的呢?

链路层封包格式

b0497f30e65c8fc2a3638a0be04beed3.png

MAC 头部(有线局域网)

22d40b0839913662d45a4e72cfd15169.png

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

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

unsigned char msg[1024] = {

//--------------组MAC--------14------

0xb8, 0x88, 0xe3, 0xe1, 0x10, 0xe6, // dst_mac: b8:88:e3:e1:10:e6

0xc8, 0x9c, 0xdc, 0xb7, 0x0f, 0x19, // src_mac: c8:9c:dc:b7:0f:19

0x08, 0x00,                        // 类型:0x0800 IP协议

// …… ……

// …… ……

};

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

#include

#include

#include

#include

#include

#include

#include

int main(int argc,char *argv[])

{

int i = 0;

unsigned char buf[1024] = "";

int sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));

while(1)

{

unsigned char src_mac[18] = "";

unsigned char dst_mac[18] = "";

//获取链路层的数据帧

recvfrom(sock_raw_fd, buf, sizeof(buf),0,NULL,NULL);

//从buf里提取目的mac、源mac

sprintf(dst_mac,"%02x:%02x:%02x:%02x:%02x:%02x", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);

sprintf(src_mac,"%02x:%02x:%02x:%02x:%02x:%02x", buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]);

//判断是否为IP数据包

if(buf[12]==0x08 && buf[13]==0x00)

{

printf("______________IP数据报_______________\n");

printf("MAC:%s >> %s\n",src_mac,dst_mac);

}//判断是否为ARP数据包

else if(buf[12]==0x08 && buf[13]==0x06)

{

printf("______________ARP数据报_______________\n");

printf("MAC:%s >> %s\n",src_mac,dst_mac);

}//判断是否为RARP数据包

else if(buf[12]==0x80 && buf[13]==0x35)

{

printf("______________RARP数据报_______________\n");

printf("MAC:%s>>%s\n",src_mac,dst_mac);

}

}

return 0;

}

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

8e82cc2ffafe7e1f30c36f7b1024f08e.png

每个报文头部都有一个相应的结构体,通过这些结构体对报文进行相应的组包或拆包会方便很多。

Ubuntu 12.04 中描述网络协议结构的文件如下:

65647489c9e42be8314b6d02b7632834.png

6e5d068b26f7d69d5319d994c16ca917.png

以太网头部(所需要头文件:#include ):

58118c984cb2724899945876211b7b9d.png

上面的例子,改为用结构体实现,如下:

#include

#include

#include

#include

#include

#include

#include

#include           // 以太网头部 头文件

#include             // ip头部 头文件

// #include             // arp头部 头文件

int main(int argc,char *argv[])

{

int i = 0;

unsigned char buf[1024] = "";

int sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));

while(1)

{

unsigned char src_mac[18] = "";

unsigned char dst_mac[18] = "";

//获取链路层的数据帧

recvfrom(sock_raw_fd, buf, sizeof(buf),0,NULL,NULL);

//从数据中提取mac首部信息(14个字节)

struct ether_header *ethdr = NULL;

ethdr = (struct ether_header *)buf;

//从buf里提取目的mac、源mac

sprintf(dst_mac,"%02x:%02x:%02x:%02x:%02x:%02x", ethdr->ether_dhost[0], ethdr->ether_dhost[1],ethdr->ether_dhost[2],ethdr->ether_dhost[3],ethdr->ether_dhost[4],ethdr->ether_dhost[5]);

sprintf(src_mac,"%02x:%02x:%02x:%02x:%02x:%02x", ethdr->ether_shost[0], ethdr->ether_shost[1],ethdr->ether_shost[2],ethdr->ether_shost[3],ethdr->ether_shost[4],ethdr->ether_shost[5]);

//判断是否为IP数据包

if( 0x0800 == ntohs(ethdr->ether_type) )

{

printf("______________IP数据报_______________\n");

printf("MAC:%s >> %s\n",src_mac,dst_mac);

}//0x0806为ARP数据包, 0x8035为RARP数据包

else if( 0x0806 == ntohs(ethdr->ether_type) || 0x8035 == ntohs(ethdr->ether_type) )

{

printf("______________ARP数据报_______________\n");

printf("MAC:%s >> %s\n",src_mac,dst_mac);

}

}

return 0;

}

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值