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

📄 arp.h

📁 linux 机顶盒终端内部的网络接入模块
💻 H
字号:
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if_arp.h>
#include <string.h>
#include <syslog.h>
#include <arpa/inet.h>
#include <net/ethernet.h>
#include <linux/types.h>
#include <linux/if_tr.h>

#ifdef __GLIBC__
#include <net/ethernet.h>
#include <linux/types.h>
#include <linux/if_tr.h>
#else
#include <linux/if_ether.h>
#define ETHERTYPE_IP		0x0800
#define ETHERTYPE_ARP		0x0806
#endif

#define MAC_BCAST_ADDR		"\xff\xff\xff\xff\xff\xff"
#define MAC_SOURCE_ADDR		"\x00\x11\xd8\x60\x09\xc4"
/*以太网帧头*/
struct packed_ether_header {
  u_int8_t  ether_dhost[ETH_ALEN];      /* destination eth addr */
  u_int8_t  ether_shost[ETH_ALEN];      /* source ether addr    */
  u_int16_t ether_type;                 /* packet type ID field */
} __attribute__((packed));

typedef struct arpMessage
{
  struct packed_ether_header	ethhdr;
  u_short htype;	/* hardware type (must be ARPHRD_ETHER) */
  u_short ptype;	/* protocol type (must be ETHERTYPE_IP) */
  u_char  hlen;		/* hardware address length (must be 6) */
  u_char  plen;		/* protocol address length (must be 4) */
  u_short operation;	/* ARP opcode */
  u_char  sHaddr[ETH_ALEN];	/* sender's hardware address */
  u_char  sInaddr[4];	/* sender's IP address */
  u_char  tHaddr[ETH_ALEN];	/* target's hardware address */
  u_char  tInaddr[4];	/* target's IP address */
  u_char  pad[18];	/* pad for min. Ethernet payload (60 bytes) */
} __attribute__((packed)) arpMessage;

int arpCheck(){
	arpMessage ArpMsgSend,ArpMsgRcv;
	//unsigned char		ClientHwAddr[6]="%0x0011D86009C4";
	int dhcpSocket;
	
	int o = 1;
	struct sockaddr addr;
	unsigned char *c ;
	int len;
	
	struct in_addr *myaddr;
	dhcpSocket = socket(AF_PACKET,SOCK_PACKET,htons(ETH_P_ALL));
	if ( dhcpSocket == -1 ){
      		syslog(LOG_ERR,"dhcpStart: socket: %m\n");
      		exit(1);
    	}
    	
    	if ( setsockopt(dhcpSocket,SOL_SOCKET,SO_BROADCAST,&o,sizeof(o)) == -1 ){
      		syslog(LOG_ERR,"dhcpStart: setsockopt: %m\n");
      		exit(1);
    	}
	memset(&ArpMsgSend,0,sizeof(arpMessage));
	printf("before zhen\n");
	/*构造以太网数据帧*/
	memcpy(ArpMsgSend.ethhdr.ether_dhost,MAC_BCAST_ADDR,6);
	memcpy(ArpMsgSend.ethhdr.ether_shost,MAC_SOURCE_ADDR,6);
  	ArpMsgSend.ethhdr.ether_type = htons(ETHERTYPE_ARP);
	ArpMsgSend.htype	= htons(ARPHRD_ETHER);
  	ArpMsgSend.ptype	= htons(ETHERTYPE_IP);
  	ArpMsgSend.hlen	= 6;
  	ArpMsgSend.plen	= 4;
  	ArpMsgSend.operation	= htons(ARPOP_REQUEST);
  	
  	memcpy(ArpMsgSend.sHaddr,MAC_SOURCE_ADDR,6);
  	inet_aton("192.168.3.1",myaddr);
  	memcpy(&ArpMsgSend.tInaddr,myaddr,4);
	
	printf("2\n");
	memset(&addr,0,sizeof(struct sockaddr));
      	memcpy(addr.sa_data,"eth0",4);
      	len = sizeof(arpMessage);
	for(;;){
		if ( sendto(dhcpSocket,&ArpMsgSend,len,0,
			&addr,sizeof(struct sockaddr)) == -1 ){
	      		syslog(LOG_ERR,"arpCheck: sendto: %m\n");
	      		return -1;
	    	}
	}
}

⌨️ 快捷键说明

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