Embedded Systems September 2000 Vol13_10

Issue link:

Contents of this Issue


Page 77 of 229

CONNECTING ..• LlmNG 3 int NetlpRcv(NetlpPkt * plpPkt) { INT16U checksum; '* * Check IP header version and Length. if (plpPkt->ipHdr.ver_rlen != IP_VER_HLEN) { *' '* * Unsupported header version or Length. return (NET_ERROR); *' } checksum = plpPkt->ipHdr.checksum; plpPkt->ipHdr.checksum '* *' if (checksum != NetlpChecksum((!NT16U *) plpPkt, IP_HEADER_LEN)) { *' return CNET_ERROR); } '* * Route the packet to the appropriate Layer 3 protocol. switch (plpPkt->ipHdr.protocol) { *' case PROTO_UDP: return (NetUdpRcv((NetUdpPkt *) plpPkt, ntohs(plpPkt->ipHdr.length) - IP_HEADER_LEN)); #if defined (NET_TCP_EN) case PROTO_TCP: return (NetTcpRcv((NetTcpPkt *) plpPkt, ntohs(plpPkt->ipHdr.length) - IP_HEADER_LEN)); #endif default: return (NET_ERROR); } } /* NetlpRcv() *' II Unsupported protocol Michael Bm·r is the editor in chief of Embedded Systems Programming. He holds BS and MS degrees in electrical engi- neering from the University of Ma· ryland. He is the author of the book Programming Embedded Systems in C and C++ (O 'Reilly & Associates). Michael can be reached via e-rnail at 76 SEPTEMBER 2000 Embedded Systems Programming I /Bad checksum = 0; '* * Compute checksum and compare with received value. * Move the IP header checksum out of the header. it 's small , easily coded and under- stood, and yet-because it imple- me nts a subset of the fu ll protocol- can communicate with any system on the network. Some fie lds of th e IP head e r a re filled in by the UDP layer above. Specifically, the UDP layer fi lls in the IP header's source and d estina- tion IP addresses, which it combines with the UDP head er and payload to compute a UDP ch ecksum. The UDP sending function then passes the e ntire packet to NetlpSndO . After fi ll in g in th e rema in ing fi e lds of the IP head e r, NetipSndC) com- putes its own ch ecksum (of the IP head er conte nts o n ly) and passes the completed IP packet on to the ne twork driver, to be sent out over the physical network. The NetipRcv() function, shown in Listing 3, handles incoming IP pack- ets. This function is called from within the context of a task tha t's part of the network driver. The driver task becomes active whenever a new packet arrives over the network. It then sees wha t type of packet it is (ARP, IP, and so on ) and routes it to the appropriate function. NetlpRcvO processes a ll incoming IP packets. After ch ecking the IP version, head e r le ngth, and ch ecksum , each incoming IP packet is routed to th e layer above. If it is a UDP p acket, NetUdpRcvO is called. If it is a TCP packet and TCP suppo n is included, NetTcpRcvO is called instead. Next mo nth I'll tell you everything you ever wanted to know about the UDP layer that lies above this one. And, of course, I' ll share my imple- men tation wi th you. In the meantime, stay connected... esp

Articles in this issue

Archives of this issue

view archives of EETimes - Embedded Systems September 2000 Vol13_10