From 4b11c9166b86ccc5f8f02fda01ded0f9e9760df0 Mon Sep 17 00:00:00 2001 From: Joe Hershberger Date: Wed, 23 May 2012 07:59:07 +0000 Subject: net: Refactor IP, UPD, and ICMP header writing functions ICMP (ping) was reimplementing IP header code... it now shares code. Signed-off-by: Joe Hershberger Acked-by: Simon Glass Acked-by: Mike Frysinger --- net/net.c | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) (limited to 'net/net.c') diff --git a/net/net.c b/net/net.c index de5352c..1c7bf60 100644 --- a/net/net.c +++ b/net/net.c @@ -618,7 +618,7 @@ int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, pkt = NetArpWaitTxPacket; pkt += NetSetEther(pkt, NetArpWaitPacketMAC, PROT_IP); - NetSetIP(pkt, dest, dport, sport, payload_len); + net_set_udp_header(pkt, dest, dport, sport, payload_len); memcpy(pkt + IP_UDP_HDR_SIZE, (uchar *)NetTxPacket + (pkt - (uchar *)NetArpWaitTxPacket) + IP_UDP_HDR_SIZE, payload_len); @@ -638,7 +638,7 @@ int NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, pkt = (uchar *)NetTxPacket; pkt += NetSetEther(pkt, ether, PROT_IP); - NetSetIP(pkt, dest, dport, sport, payload_len); + net_set_udp_header(pkt, dest, dport, sport, payload_len); eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_UDP_HDR_SIZE + payload_len); @@ -1245,40 +1245,49 @@ NetSetEther(uchar *xet, uchar * addr, uint prot) } } -void NetSetIP(uchar *xip, IPaddr_t dest, int dport, int sport, int len) +void net_set_ip_header(uchar *pkt, IPaddr_t dest, IPaddr_t source) { - struct ip_udp_hdr *ip = (struct ip_udp_hdr *)xip; + struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt; /* - * If the data is an odd number of bytes, zero the - * byte after the last byte so that the checksum - * will work. - */ - if (len & 1) - xip[IP_UDP_HDR_SIZE + len] = 0; - - /* - * Construct an IP and UDP header. - * (need to set no fragment bit - XXX) + * Construct an IP header. */ /* IP_HDR_SIZE / 4 (not including UDP) */ ip->ip_hl_v = 0x45; ip->ip_tos = 0; - ip->ip_len = htons(IP_UDP_HDR_SIZE + len); + ip->ip_len = htons(IP_HDR_SIZE); ip->ip_id = htons(NetIPID++); ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */ ip->ip_ttl = 255; - ip->ip_p = 17; /* UDP */ ip->ip_sum = 0; /* already in network byte order */ - NetCopyIP((void *)&ip->ip_src, &NetOurIP); - /* - "" - */ + NetCopyIP((void *)&ip->ip_src, &source); + /* already in network byte order */ NetCopyIP((void *)&ip->ip_dst, &dest); +} + +void net_set_udp_header(uchar *pkt, IPaddr_t dest, int dport, int sport, + int len) +{ + struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt; + + /* + * If the data is an odd number of bytes, zero the + * byte after the last byte so that the checksum + * will work. + */ + if (len & 1) + pkt[IP_UDP_HDR_SIZE + len] = 0; + + net_set_ip_header(pkt, dest, NetOurIP); + ip->ip_len = htons(IP_UDP_HDR_SIZE + len); + ip->ip_p = IPPROTO_UDP; + ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE >> 1); + ip->udp_src = htons(sport); ip->udp_dst = htons(dport); ip->udp_len = htons(UDP_HDR_SIZE + len); ip->udp_xsum = 0; - ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE / 2); } void copy_filename(char *dst, const char *src, int size) -- cgit v1.1