发送arp请求报文

栏目: 服务器 · 发布时间: 6年前

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/ethernet.h>
#include <netpacket/packet.h>
#include <string.h>
#include <net/if.h>
 
#define SRC_MAC  { 0x00,0x0C,0x29,0x6F,0x57,0xE7 }//源MAC地址
#define DEST_MAC { 0x00,0x0C,0x29,0xD3,0xD6,0xF7 }//目的MAC地址
 
struct arppacket
{
	unsigned char dest_mac[ETH_ALEN];//接收方MAC
	unsigned char src_mac[ETH_ALEN];//发送方MAC
	unsigned short type;       //0x0806是ARP帧的类型值
	unsigned short ar_hrd;//硬件类型 - 以太网类型值0x1
	unsigned short ar_pro;//上层协议类型 - IP协议(0x0800)
	unsigned char  ar_hln;//MAC地址长度
	unsigned char  ar_pln;//IP地址长度
	unsigned short ar_op;//操作码 - 0x1表示ARP请求包,0x2表示应答包
	unsigned char  ar_sha[ETH_ALEN];//发送方mac
	unsigned char ar_sip[4];//发送方ip
	unsigned char ar_tha[ETH_ALEN];//接收方mac
	unsigned char ar_tip[4];//接收方ip
} __attribute__ ((__packed__));

int main(int argc,char *argv[])
{
	int fd = 0;
	struct in_addr s,r;
	struct sockaddr_ll sl;
 
	struct arppacket arp={
		DEST_MAC,
		SRC_MAC,
		htons(0x0806),
		htons(0x01),
		htons(0x0800),
		ETH_ALEN,
		4,
		htons(0x01),
		SRC_MAC,
		{0},
		DEST_MAC,
		{0}
	};
 
	fd = socket(AF_PACKET,SOCK_RAW,htons(ETH_P_ALL));
	if (fd < 0) {
		printf("socket error\n");
		return -1;
	}
	memset(&sl, 0, sizeof(sl));
 
	inet_aton("192.168.11.220", &s);
	memcpy(&arp.ar_sip, &s, sizeof(s));
 
	inet_aton("192.168.11.192", &r);
	memcpy(&arp.ar_tip, &r, sizeof(r));
 
 
	sl.sll_family = AF_PACKET;
	sl.sll_ifindex = IFF_BROADCAST;
	
	while (1) {
		if (sendto(fd, &arp, sizeof(arp), 0, (struct sockaddr*)&sl, sizeof(sl)) <= 0)
			printf("send error\n");
		else
			printf("send success\n");
 
		sleep(1);
	}
 
	close(fd);
 
	return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

运营实战指南

运营实战指南

韩利 / 电子工业出版社 / 2016-9-1 / 49

《运营实战指南》架构清晰,前8章主要通过故事形式深入浅出理解运营,将运营基础知识和概念融入到故事中。第9章讲解运营核心方法论,从目标、关键驱动元素、试错调优、高效运行4部分来完整讲解一个运营项目从0到1的过程。第10章、11章、12章深入讲解了运营人拿业绩最核心的知识点:用户、内容和文案。其中数据分析、活动运营等内容以案例形式穿插在各个章节中。最后两章,主谈运营人在日常生活中如何历练以及一个运营人......一起来看看 《运营实战指南》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具