首页 > 代码库 > uboot arp地址解析

uboot arp地址解析

common/cmd_cache.c

int do_getmac(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){    //usend d43d7e45371c 192.168.1.134 1234 1234 hhaa    char ethsrc[6]={0xff ,0xff ,0xff ,0xff ,0xff, 0xff};    char ethdst[6]={0xff ,0xff ,0xff ,0xff ,0xff, 0xff};        _getmac(argv[3],ethsrc);             getmacfromipaddress(argv[1],ethdst,argv[2],ethsrc);    //printf("%s mac is  %x %x %x %x %x %x\n",argv[1],eth[0],eth[1],eth[2],eth[3],eth[4],eth[5]);        return 0;    }U_BOOT_CMD (getmac, 6, 1, do_getmac,    "getmac destip srcip srceth",    "[operation type] [device number] [image address] [image size]\n"    );
common/tftp.c
int global_raw_reveive = 0;
void getmacfromipaddress(const char*  remoteip, char* remoteeth,const char* ourip,char* oureth)
{
		/* and do the ARP request */
		 IPaddr_t RemoteIP; 
		  IPaddr_t MyIP; 
		  RemoteIP=  string_to_ip(remoteip);
		  MyIP = string_to_ip(ourip);
		  
		ArpRequestsend(RemoteIP,oureth,MyIP);
		global_raw_reveive = 1;
		eth_rx();
		global_raw_reveive = 0;
		// NetRxPacket = inpkt;
		// NetRxPacketLen = len;
		//printbuffer((unsigned char*)NetRxPacket,NetRxPacketLen);
		//_printarp((unsigned char*)NetRxPacket,NetRxPacketLen);
		return 1;	/* waiting */
}

net/net.c
void ArpRequestsend(IPaddr_t remoteip,char* oureth,IPaddr_t ourip)
{
	int i;
	volatile uchar *pkt;
	ARP_t *arp;	

	pkt = NetTxPacket;
	pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP);
	arp = (ARP_t *) pkt;
	arp->ar_hrd = htons(ARP_ETHER);
	arp->ar_pro = htons(PROT_IP);
	arp->ar_hln = 6;
	arp->ar_pln = 4;
	arp->ar_op = htons(ARPOP_REQUEST);
	/* source ET addr */
	memcpy(&arp->ar_data[0], oureth, 6);
	/* source IP addr */
	NetWriteIP((uchar *) &arp->ar_data[6], ourip);
	for (i = 10; i < 16; ++i) {
		/* dest ET addr = 0 */
		arp->ar_data[i] = 0;
	}
	NetWriteIP((uchar *) &arp->ar_data[16], remoteip);
	(void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
}
void
NetReceive(volatile uchar *inpkt, int len)
{
	Ethernet_t *et;
	IP_t	*ip;
	ARP_t	*arp;
	IPaddr_t tmp;
	IPaddr_t src_ip;
	int	x;
	uchar *pkt;
#if defined(CONFIG_CMD_CDP)
	int iscdp;
#endif
	ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;

	debug("packet received.%d\n", len);

	NetRxPacket = inpkt;
	NetRxPacketLen = len;
	et = (Ethernet_t *)inpkt;
	<span style="color:#FF0000;">if(global_raw_reveive){
		//global_raw_reveive=0;
		printpacket((Ethernet_t *)NetRxPacket,len);
		return;
	}</span>

driver/net/cpsw.c
void printbuffer(unsigned char* buffer, int len)
{
	int i;
	for(i=0;i<len;i++){
		printf("%2.2x ",buffer[i]);
		if((i&15)==15)printf("\n");
	}
	printf("\n");
}
#define printhex(a,b) printf(#b##"=%x\n",a->b);
void printarp(ARP_t * arp,int len){    // ushort        ar_hrd;        /* Format of hardware address    */    // ushort        ar_pro;        /* Format of protocol address    */    // uchar        ar_hln;        /* Length of hardware address    */    // uchar        ar_pln;        /* Length of protocol address    */    // ushort        ar_op;        /* Operation            */    // uchar        ar_data[0];     printf("ar_hrd %x\n",arp->ar_hrd);     printf("ar_pro %x\n",arp->ar_pro);     printf("ar_hln %x\n",arp->ar_hln);     printf("ar_pln %x\n",arp->ar_pln);     printf("ar_op %x\n",arp->ar_op);    printf("mac_src %.2x%.2x%.2x%.2x%.2x%.2x\n",arp->ar_data[0],arp->ar_data[1],arp->ar_data[2],arp->ar_data[3],arp->ar_data[4],arp->ar_data[5]);        printf("ip_src %d %d %d %d\n",arp->ar_data[6],arp->ar_data[7],arp->ar_data[8],arp->ar_data[9]);    printf("mac_dst %.2x%.2x%.2x%.2x%.2x%.2x\n",arp->ar_data[10],arp->ar_data[11],arp->ar_data[12],arp->ar_data[13],arp->ar_data[14],arp->ar_data[15]);    printf("ip_dst %d.%d.%d.%d\n",arp->ar_data[16],arp->ar_data[17],arp->ar_data[18],arp->ar_data[19]);}int printpacket(char* packet,int len){// typedef struct {    // uchar        et_dest[6];    /* Destination node        */    // uchar        et_src[6];    /* Source node            */    // ushort        et_protlen;    /* Protocol or length        */    // uchar        et_dsap;    /* 802 DSAP            */    // uchar        et_ssap;    /* 802 SSAP            */    // uchar        et_ctl;        /* 802 control            */    // uchar        et_snap1;    /* SNAP                */    // uchar        et_snap2;    // uchar        et_snap3;    // ushort        et_prot;    /* 802 protocol            */// } Ethernet_t;    Ethernet_t* eth =(Ethernet_t* )packet;    //printbuffer((unsigned char*)packet,len);        printf("\net_dest %.2x%.2x%.2x%.2x%.2x%.2x\n", eth->et_dest[0],eth->et_dest[1],eth->et_dest[2],eth->et_dest[3],eth->et_dest[4],eth->et_dest[5]);    printf("et_src %.2x%.2x%.2x%.2x%.2x%.2x\n", eth->et_src[0],eth->et_src[1],eth->et_src[2],eth->et_src[3],eth->et_src[4],eth->et_src[5]);    if(isnotvlan()){        printf("et_protlen %x",eth->et_protlen);        printf("et_dsap %x\n",eth->et_dsap);        printf("et_ssap %x\n",eth->et_ssap);        printf("et_ctl %x\n",eth->et_ctl);        printf("et_snap1 %x\n",eth->et_snap1);        switch(eth->et_protlen){            case 0x608:                printf("PROT_ARP");                printarp((ARP_t * )((unsigned char*)eth+ETHER_HDR_SIZE),len-ETHER_HDR_SIZE);                break;            case 0x8:                  printf("PROT_IP");                //printip((IP_t * )((unsigned char*)eth+ETHER_HDR_SIZE),len-ETHER_HDR_SIZE);                break;            case 0x3680:                printf("PROT_RARP");                printarp((ARP_t * )((unsigned char*)eth+ETHER_HDR_SIZE),len-ETHER_HDR_SIZE);                break;            case 0x81:                printf("PROT_VLAN");                break;        }        printf("\n");    }else{        VLAN_Ethernet_t *veth = (VLAN_Ethernet_t *)eth;        // uchar        vet_dest[6];    /* Destination node        */        // uchar        vet_src[6];    /* Source node            */        // ushort        vet_vlan_type;    /* PROT_VLAN            */        // ushort        vet_tag;    /* TAG of VLAN            */        // ushort        vet_type;    /* protocol type        */                printf("vet_vlan_type %x\n",veth->vet_vlan_type);        printf("vet_tag %x\n",veth->vet_tag);        printf("vet_type %x\n",veth->vet_type);                return VLAN_ETHER_HDR_SIZE;    }}
void printip(IP_t* ip,int len)
{
// typedef struct {
	// uchar		ip_hl_v;	/* header length and version	*/
	// uchar		ip_tos;		/* type of service		*/
	// ushort		ip_len;		/* total length			*/
	// ushort		ip_id;		/* identification		*/
	// ushort		ip_off;		/* fragment offset field	*/
	// uchar		ip_ttl;		/* time to live			*/
	// uchar		ip_p;		/* protocol			*/
	// ushort		ip_sum;		/* checksum			*/
	// IPaddr_t	ip_src;		/* Source IP address		*/
	// IPaddr_t	ip_dst;		/* Destination IP address	*/
	// ushort		udp_src;	/* UDP source port		*/
	// ushort		udp_dst;	/* UDP destination port		*/
	// ushort		udp_len;	/* Length of UDP packet		*/
	// ushort		udp_xsum;	/* Checksum			*/
// } IP_t;
	printf("ip_hl_v %x\n",ip->ip_hl_v);
	printf("ip_tos %x\n",ip->ip_tos);
	printf("ip_len %x\n",ip->ip_len);
	printf("ip_id %x\n",ip->ip_id);
	printf("ip_off %x\n",ip->ip_off);
	printf("ip_ttl %x\n",ip->ip_ttl);
	printf("ip_p %x\n",ip->ip_p);
	printf("ip_sum %x\n",ip->ip_sum);

	if(17==ip->ip_p){
		unsigned char* p1;
		printf("UDP\n");		
		printf("udp_src %x\n",ip->udp_src);
		printf("udp_dst %x\n",ip->udp_dst);
		printf("udp_len %x\n",ip->udp_len);
		printf("udp_xsum %x\n",ip->udp_xsum);
		p1 = (unsigned char*)&ip->ip_src;
		printf("ip_src %d %d %d %d\n",p1[0],p1[1],p1[2],p1[3]);
		p1 = (unsigned char*)&ip->ip_dst;
		printf("ip_dst %d %d %d %d\n",p1[0],p1[1],p1[2],p1[3]);
		
	}else if(1==ip->ip_p){
		printf("ICMP\n");		
	}
}
static int cpsw_send(struct eth_device *dev, volatile void *packet, int length)
{
	struct cpsw_priv	*priv = dev->priv;
	void *buffer;
	int len;
	int status;
	int i;
	<span style="color:#FF0000;">unsigned char* p=(unsigned char*)packet;
	//Ethernet_t *et = (Ethernet_t *)p;
	//IP_t *ip = (IP_t *)(p+ETHER_HDR_SIZE);
	printf("cpsw_send: length=%x\n",length);
	//00 0c 29 6a 86 4e
	// p[0]=0;
	// p[1]=0xc;
	// p[2]=0x29;
	// p[3]=0x6a;
	// p[4]=0x86;
	// p[5]=0x4e;
	printpacket(packet,length);
	printf("beging send ...\n");</span>


uboot arp地址解析