ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

原始套接字发送自定义协议的链路帧

2021-09-12 13:00:02  阅读:127  来源: 互联网

标签:字节 自定义 int server 60 client 链路 接字 include


server.c
===============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/if_ether.h>
#include <net/if_arp.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <netpacket/packet.h>
#include <netinet/ether.h>
#include <string.h>
#include <pthread.h>
void print_hex(unsigned char *buffer, int len){
	int i;
	printf("******************start code**********************************\n");
	for(i = 1; i <= len; i++){
		printf("0x%02X ",buffer[i-1]);					
		if(i % 16 == 0){
			printf("\n");
		}
	}
	printf("\n");
	printf("********************end code************************************\n");
}
int main()
{
	unsigned char buf[2048];
	int sock_raw_fd ;  
	sock_raw_fd = socket(PF_PACKET,SOCK_RAW,htons(0x3001));//ETH_P_ALL));
	printf("sock_raw_fd = %d\n",sock_raw_fd);

	int nread;
	while(1)
	{
		nread = recvfrom(sock_raw_fd,buf,2048,0,NULL,NULL);
		printf("nread = %d\n",nread);
		print_hex(buf,nread);
		
	}
	return 0;
}
//当socket函数的最后一个参数协议字段,填写ETH_P_ALL时,将收到所有类型的二层链路帧。
//比如这种情形,server运行在PC机上(ETH_P_ALL);client运行在同一台PC机上(0x3001),通过ens33网口向外发送自定义的二层链路帧,此时server能够接收到client的二层链路帧。
//server运行在PC机上(0x3001);client运行在同一台PC机上(0x3001),通过ens33网口向外发送自定义的二层链路帧,此时server不能接收到client的二层链路帧。
client.c
==============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/if_ether.h>
#include <net/if_arp.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <netpacket/packet.h>
#include <netinet/ether.h>
#include <string.h>
#include <pthread.h>
void print_hex(unsigned char *buffer, int len){
	int i;
	printf("******************start code**********************************\n");
	for(i = 1; i <= len; i++){
		printf("0x%02X ",buffer[i-1]);					
		if(i % 16 == 0){
			printf("\n");
		}
	}
	printf("\n");
	printf("********************end code************************************\n");
}
int main()
{
//00:0c:29:de:a6:c5 目的MAC地址
//52:54:00:9b:38:7a 源MAC地址,这个可以随便填写,只关心目的MAC地址
	unsigned char buf[128]={0x0,0x0c,0x29,0xde,0xa6,0xc5,0x52,0x54,0,0x9b,0x38,0x7a,0x30,0x1};
	int sock_raw_fd ;
	sock_raw_fd = socket(PF_PACKET,SOCK_RAW,htons(0x3001));
	int nwrite;
	short type = htons(0x3001);
	memcpy(buf+12,&type,2);
	//memset(buf,0x01,128);
	struct sockaddr_ll sll;                 //原始套接字地址结构    
	struct ifreq ethreq;                    //网络接口地址    

	strncpy(ethreq.ifr_name, "ens33", IFNAMSIZ);         //指定网卡名称    
	if(-1 == ioctl(sock_raw_fd, SIOCGIFINDEX, &ethreq))    //获取网络接口    
	{    
	    perror("ioctl");    
	    close(sock_raw_fd);    
	    exit(-1);    
	}    
	 
	/*将网络接口赋值给原始套接字地址结构*/    
	bzero(&sll, sizeof(sll));    
	sll.sll_ifindex = ethreq.ifr_ifindex;    
	 
	// 发送数据        
	int len = sendto(sock_raw_fd, buf, 14, 0 , (struct sockaddr *)&sll, sizeof(sll));    
	if(len == -1)    
	{    
	    perror("sendto");    
	}   
	print_hex(buf,14);
	printf("len = %d\n",len);
}

server和client运行在不同的机器下:

1、client的目的MAC地址填写正确,在server是否为混杂模式下都可以收到数据。

2、client的目的MAC地址填写错误,server必须在混杂模式下才能收到数据。

注:

  client发送大于等于14字节且小于60字节,server收到的都是60字节。大于60字节时,server收到的是client发送的实际字节数。  

标签:字节,自定义,int,server,60,client,链路,接字,include
来源: https://blog.csdn.net/xinshuai111/article/details/120249414

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有