日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

自己動手學TCP/IP--以太網幀

系統 2173 0

數據鏈路層主要的功能

  • 把網絡層的包封裝成幀
  • 對幀的校驗和確認
  • 流量的控制

數據鏈路層生成以太網幀,以太網幀的主要格式如下。


太網幀的傳輸大小是由傳輸媒介決定的,數據幀的大小是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

自己動手學TCP/IP--以太網幀


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!??!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 甘泉县| 和田市| 新民市| 凯里市| 墨江| 彭阳县| 武清区| 永善县| 扶绥县| 咸宁市| 友谊县| 翼城县| 伊吾县| 湘潭县| 涿鹿县| 筠连县| 广丰县| 汽车| 鄢陵县| 兴安盟| 桑植县| 昂仁县| 永定县| 达州市| 东乡族自治县| 龙山县| 扶沟县| 旺苍县| 景宁| 大兴区| 托克托县| 龙江县| 临夏市| 汉阴县| 申扎县| 渝北区| 安福县| 苏尼特左旗| 通州市| 天气| 无棣县|