net: Refactor packet length computations
[pandora-u-boot.git] / net / arp.c
index 5b16de7..e58a074 100644 (file)
--- a/net/arp.c
+++ b/net/arp.c
@@ -52,27 +52,29 @@ void ArpRequest(void)
 {
        uchar *pkt;
        struct arp_hdr *arp;
+       int eth_hdr_size;
 
        debug("ARP broadcast %d\n", NetArpWaitTry);
 
        pkt = NetTxPacket;
 
-       pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP);
+       eth_hdr_size = NetSetEther(pkt, NetBcastAddr, PROT_ARP);
+       pkt += eth_hdr_size;
 
        arp = (struct arp_hdr *) pkt;
 
        arp->ar_hrd = htons(ARP_ETHER);
        arp->ar_pro = htons(PROT_IP);
-       arp->ar_hln = 6;
-       arp->ar_pln = 4;
+       arp->ar_hln = ARP_HLEN;
+       arp->ar_pln = ARP_PLEN;
        arp->ar_op = htons(ARPOP_REQUEST);
 
        /* source ET addr */
-       memcpy(&arp->ar_data[0], NetOurEther, 6);
+       memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN);
        /* source IP addr */
-       NetWriteIP((uchar *) &arp->ar_data[6], NetOurIP);
+       NetWriteIP(&arp->ar_spa, NetOurIP);
        /* dest ET addr = 0 */
-       memset(&arp->ar_data[10], '\0', 6);
+       memset(&arp->ar_tha, 0, ARP_HLEN);
        if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
            (NetOurIP & NetOurSubnetMask)) {
                if (NetOurGatewayIP == 0) {
@@ -85,8 +87,8 @@ void ArpRequest(void)
                NetArpWaitReplyIP = NetArpWaitPacketIP;
        }
 
-       NetWriteIP((uchar *) &arp->ar_data[16], NetArpWaitReplyIP);
-       (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
+       NetWriteIP(&arp->ar_tpa, NetArpWaitReplyIP);
+       (void) eth_send(NetTxPacket, eth_hdr_size + ARP_HDR_SIZE);
 }
 
 void ArpTimeoutCheck(void)
@@ -116,8 +118,9 @@ void ArpTimeoutCheck(void)
 void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
 {
        struct arp_hdr *arp;
-       IPaddr_t tmp;
+       IPaddr_t reply_ip_addr;
        uchar *pkt;
+       int eth_hdr_size;
 
        /*
         * We have to deal with two types of ARP packets:
@@ -139,15 +142,15 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
                return;
        if (ntohs(arp->ar_pro) != PROT_IP)
                return;
-       if (arp->ar_hln != 6)
+       if (arp->ar_hln != ARP_HLEN)
                return;
-       if (arp->ar_pln != 4)
+       if (arp->ar_pln != ARP_PLEN)
                return;
 
        if (NetOurIP == 0)
                return;
 
-       if (NetReadIP(&arp->ar_data[16]) != NetOurIP)
+       if (NetReadIP(&arp->ar_tpa) != NetOurIP)
                return;
 
        switch (ntohs(arp->ar_op)) {
@@ -155,14 +158,14 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
                /* reply with our IP address */
                debug("Got ARP REQUEST, return our IP\n");
                pkt = (uchar *)et;
-               pkt += NetSetEther(pkt, et->et_src, PROT_ARP);
+               eth_hdr_size = NetSetEther(pkt, et->et_src, PROT_ARP);
+               pkt += eth_hdr_size;
                arp->ar_op = htons(ARPOP_REPLY);
-               memcpy(&arp->ar_data[10], &arp->ar_data[0], 6);
-               NetCopyIP(&arp->ar_data[16], &arp->ar_data[6]);
-               memcpy(&arp->ar_data[0], NetOurEther, 6);
-               NetCopyIP(&arp->ar_data[6], &NetOurIP);
-               (void) eth_send((uchar *)et,
-                               (pkt - (uchar *)et) + ARP_HDR_SIZE);
+               memcpy(&arp->ar_tha, &arp->ar_sha, ARP_HLEN);
+               NetCopyIP(&arp->ar_tpa, &arp->ar_spa);
+               memcpy(&arp->ar_sha, NetOurEther, ARP_HLEN);
+               NetCopyIP(&arp->ar_spa, &NetOurIP);
+               (void) eth_send((uchar *)et, eth_hdr_size + ARP_HDR_SIZE);
                return;
 
        case ARPOP_REPLY:               /* arp reply */
@@ -173,28 +176,28 @@ void ArpReceive(struct ethernet_hdr *et, struct ip_udp_hdr *ip, int len)
 #ifdef CONFIG_KEEP_SERVERADDR
                if (NetServerIP == NetArpWaitPacketIP) {
                        char buf[20];
-                       sprintf(buf, "%pM", arp->ar_data);
+                       sprintf(buf, "%pM", arp->ar_sha);
                        setenv("serveraddr", buf);
                }
 #endif
 
-               tmp = NetReadIP(&arp->ar_data[6]);
+               reply_ip_addr = NetReadIP(&arp->ar_spa);
 
                /* matched waiting packet's address */
-               if (tmp == NetArpWaitReplyIP) {
+               if (reply_ip_addr == NetArpWaitReplyIP) {
                        debug("Got ARP REPLY, set eth addr (%pM)\n",
                                arp->ar_data);
 
                        /* save address for later use */
                        memcpy(NetArpWaitPacketMAC,
-                              &arp->ar_data[0], 6);
+                               &arp->ar_sha, ARP_HLEN);
 
 #ifdef CONFIG_NETCONSOLE
                        NetGetHandler()(0, 0, 0, 0, 0);
 #endif
                        /* modify header, and transmit it */
                        memcpy(((struct ethernet_hdr *)NetArpWaitTxPacket)->
-                               et_dest, NetArpWaitPacketMAC, 6);
+                               et_dest, NetArpWaitPacketMAC, ARP_HLEN);
                        (void) eth_send(NetArpWaitTxPacket,
                                        NetArpWaitTxPacketSize);