数据链路层主要的功能
-
把网络层的包封装成帧
-
对帧的校验和确认
-
流量的控制
数据链路层生成以太网帧,以太网帧的主要格式如下。
太网帧的传输大小是由传输媒介决定的,数据帧的大小是64—1518。帧头14个字节,分别是目标MAC地址
(6个字节)+源MAC地址(6个字节)+协议类型(2个字节),帧尾是32位的CRC冗余校验。对于两个字
节协议字段,0×0800主要用于IP协议,还有一些其他的网络协议,比如Novell IPX等。
这里先介绍以太网帧头格式,用libcap抓出来的包最后的CRC冗余校验已经在物理网卡上完成。下面是代码
#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/ethernet.h>
#include <netinet/ip_icmp.h> //Provides declarations for icmp header
#include <netinet/udp.h> //Provides declarations for udp header
#include <netinet/tcp.h> //Provides declarations for tcp header
#include <netinet/ip.h> //Provides declarations for ip header
#define BUFFER_SIZE 2048
#define HIGH4(a) (((a&0xf0))>>4)
#define LOW4(a) (a&0x0f)
int size ;
void process_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *buffer);
void print_mac(const u_char *packet_buffer);
void mac_to_str(char *szMac, unsigned char mac[6]);
int main(int argc,char *argv[])
{
pcap_if_t *alldevsp , *device;
pcap_t *handle; //Handle of the device that shall be sniffed
char errbuf[100] , *devname , devs[100][100];
int count = 1 , n;
//First get the list of available devices
printf("Finding available devices ... ");
if( pcap_findalldevs( &alldevsp , errbuf) )
{
printf("Error finding devices : %s" , errbuf);
exit(1);
}
printf("Done");
//Print the available devices
printf("\nAvailable Devices are :\n");
for(device = alldevsp ; device != NULL ; device = device->next)
{
printf("%d. %s - %s\n" , count , device->name , device->description);
if(device->name != NULL)
{
strcpy(devs[count] , device->name);
}
count++;
}
//Ask user which device to sniff
printf("Enter the number of the device you want to sniff : ");
scanf("%d" , &n);
devname = devs[n];
//Open the device for sniffing
printf("Opening device %s for sniffing ... " , devname);
handle = pcap_open_live(devname , 65536 , 1 , 0 , errbuf);
if (handle == NULL)
{
fprintf(stderr, "Couldn't open device %s : %s\n" , devname , errbuf);
exit(1);
}
printf("Done\n");
//Put the device in sniff loop
pcap_loop(handle , -1 , process_packet , NULL);
return 0;
}
void process_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *buffer)
{
size = header->len;
print_mac(buffer);
}
void print_mac(const u_char *packet_buffer)
{
struct ethhdr *ethernet_header = NULL; //ether header
char sour_mac[40] = {'\0'}; //<linux/if_ether.h>
char des_mac[40] = {'\0'}; //#define ETH_ALEN 6
ethernet_header = (struct ethhdr *)packet_buffer;
mac_to_str(sour_mac,ethernet_header->h_source);
mac_to_str(des_mac,ethernet_header->h_dest);
printf("length of ethernet_head : %d \n",sizeof(struct ethhdr));
printf("des_MAC : %s\n",des_mac);
printf("sour_MAC : %s\n",sour_mac);
printf("Protocal : %x\n",ntohs(ethernet_header->h_proto));
}
//MAC地址翻译
void mac_to_str(char *szMac, unsigned char mac[6])
{
sprintf(szMac, "%X%X:%X%X:%X%X:%X%X:%X%X:%X%X"
, HIGH4(mac[0]), LOW4(mac[0])
, HIGH4(mac[1]), LOW4(mac[1])
, HIGH4(mac[2]), LOW4(mac[2])
, HIGH4(mac[3]), LOW4(mac[3])
, HIGH4(mac[4]), LOW4(mac[4])
, HIGH4(mac[5]), LOW4(mac[5])
);
}
$ gcc get_mac.c -o get_mac -lpcap
分享到:
相关推荐
从iplog参数分析,使用TCP/IP协议来分析问题,如何解决大量数据包重传的问题。
本资源是uC/OS-II官方为uC/OS-II写的TCP/IP协议栈。里面包含了DM9000,CS8900等网卡驱动。很适合进一步学习uC/OS-II嵌入式操作系统。
TCP/IP-UDP/IP网络调试工具软件.rar 可以作为TCP/IP的客户端与服务端通信,UDP/IP的通信,用于网络的报文接收与发送的调试,非常好用
tcp/ip-HTTP-Header-消息报头 本文来自: E点废墟(www.xok.la) 详细出处参考:http://xok.la/2009/07/tcp-ip-http-header.html
初识Modbus TCP/IP-C#编写Modbus TCP客户端程序 配合相关博文 http://blog.csdn.net/thebestleo/article/details/52331976 学习
文件夹说明 UC-FS:是μC/OS-II的文件系统代码 UC-GUI:是μC/OS-II的图形界面的代码 UC-TCP-IP:是μC/OS-II的TCP/IP协议栈的代码
ipv6 中文详解希望用的着的朋友,多多支持!!
基于官方dtu_demo.lua的修改,配合:合宙Air105|Socket|UDP |TCP/IP|W5500|Serial 串口|透传|DTU|网络测试助手|双机互联|内网测试|官方demo|学习(8-3):Socket网络接口-双机互联透传使用:双机互连DTU透传, AIR...
《tcp/ip高效编程:改善网络程序的44个技巧》是tcp/ip 领域的经典著作,对tcp/ip 网络编程中存在的各种问题进行了全面解析,旨在帮助读者深入透彻地理解tcp/ip 网络编程。本书组织方式比较特别,正文部分包括4 章,将...
uC/OS-II下TCP/IP协议栈的实现.pdf,杨晔,LWIP
TCP/IP详解 TCP/IP详解 TCP/IP详解
两本合集。里面可能含有一些自己pdf标记,自行删除即可。 TCP/IP协议栈 TCP-IP详解卷一:协议 TCP IP进行网际互联 卷1 原理、协议与结构
《TCP-IP详解卷三TCP事务协议,HTTP,NNTP和UNIX域协议》是“TCP/IP详解系列”的延续。主要内容包括:TCP事务协议,即T/TCP,这是对TCP的扩展,使客户-服务器事务更快、更高效和更可靠;TCP/IP应用,主要是HTTP和NNTP...
TCP/IP Architecture, Design and Implementation in Linux 1/5 For All: [Part 1/5] http://download.csdn.net/source/1345609 [Part 2/5] http://download.csdn.net/source/1345649 [Part 3/5] ...
TCPIP详解-linux版sock 源码 学TCP/IP协议必备
UC-OS下的TCP-IP-PPP协议栈
包含软硬件的全部资料. 包括TCP/IP的说明,概念,硬件电路,以及软件清单.
TCP-IP详解
TCP-IP详解卷1(TCP/IP理论) TCP-IP详解卷1(TCP/IP理论) TCP-IP详解卷1(TCP/IP理论) TCP-IP详解卷1(TCP/IP理论) TCP-IP详解卷1(TCP/IP理论) TCP-IP详解卷1(TCP/IP理论)