⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.c

📁 linux链路层源码
💻 C
字号:
#include <stdlib.h>#include <stdio.h>#include <string.h>#include <netinet/in.h>#include <linux/socket.h>#include <sys/ioctl.h>#include <linux/if_packet.h>#include <asm/types.h>#include <linux/if_ether.h>#include <linux/if_arp.h>#include <linux/wireless.h>int main(){    	int 			sock;    	unsigned char 		buf[4000];	struct ifreq		ifr;	struct sockaddr_ll	addr;    	int 			len,i,m=0;    	if( (sock = socket( PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0 )	{       		printf("create socket error\n");		return -1;	}		memset( &ifr, 0, sizeof( ifr ) );	snprintf( ifr.ifr_name, sizeof( ifr.ifr_name ), "eth0" );	if( ioctl( sock, SIOCGIFINDEX, &ifr ) != 0 )	{		perror( " SIOCGIFINDEX" );		exit(1);	}	memset( &addr, 0, sizeof( addr ) );	addr.sll_family = AF_PACKET;	addr.sll_ifindex = ifr.ifr_ifindex;	if( bind( sock, ( struct sockaddr * )&addr, sizeof( addr ) ) < 0 )	{		perror( " bind" );		exit( 1 );	}    	while(1)	{ 		printf( "\n-------------------------------------\n" );            	len = recvfrom( sock, buf, 4000, 0, NULL, NULL );            	if ( len < 42 )	    	{ 			if( len < 0 )				perror( "recvfrom :" );			else            			printf( "receive len < 42 \n" );			    	}            	else            	{			if( len > 42000 )			{				FILE *fp;				char fn[100];				sprintf( fn, "f%02d.dat", m++ );				fp = fopen( fn, "wb" );				if( fp == NULL )				{					printf( " Error open file \n" );					return -1;				}				fwrite( buf, 1, len, fp );				fclose( fp );						}               		printf( "************len:          %d\n", len ); 			printf("destination MAC:%02x:%02x:%02x:%02x:%02x:%02x\n"			,buf[0], buf[1], buf[2], buf[3], buf[4], buf[5] );			printf( "source MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",                        buf[6], buf[7], buf[8], buf[9], buf[10], buf[11] ); 					printf( "Link flags: %02x %02x\n", buf[12], buf[13] );					printf( "packet content:\n" );			for( i=14; i<len; i++ )			{	       			printf(" %02x",buf[i]);				if( !((i-13)%16) )					printf( "\n" );			}            	}	}       	closesocket( sock );}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -