Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

netstack.c

Go to the documentation of this file.
00001 /*! \file netstack.c \brief Network Stack. */
00002 //*****************************************************************************
00003 //
00004 // File Name    : 'netstack.c'
00005 // Title        : Network Stack
00006 // Author       : Pascal Stang
00007 // Created      : 6/28/2005
00008 // Revised      : 9/20/2005
00009 // Version      : 0.3
00010 // Target MCU   : Atmel AVR series
00011 // Editor Tabs  : 4
00012 //
00013 //*****************************************************************************
00014 
00015 #include "rprintf.h"
00016 #include "debug.h"
00017 
00018 #include "netstack.h"
00019 
00020 unsigned char NetBuffer[NETSTACK_BUFFERSIZE];
00021 
00022 void netstackInit(uint32_t ipaddress, uint32_t netmask, uint32_t gatewayip)
00023 {
00024     // init network device driver
00025     #ifdef NETSTACK_DEBUG
00026     rprintf("Initializing Network Device\r\n");
00027     #endif
00028     nicInit();
00029     // init ARP
00030     #ifdef NETSTACK_DEBUG
00031     rprintf("Initializing ARP cache\r\n");
00032     #endif
00033     arpInit();
00034     // init addressing
00035     #ifdef NETSTACK_DEBUG
00036     rprintf("Initializing Addressing\r\n");
00037     #endif
00038     ipSetConfig(ipaddress, netmask, gatewayip);
00039 }
00040 
00041 u08* netstackGetBuffer(void)
00042 {
00043     return NetBuffer;
00044 }
00045 
00046 int netstackService(void)
00047 {
00048     int len;
00049     struct netEthHeader* ethPacket;
00050 
00051     // look for a packet
00052     len = nicPoll(NETSTACK_BUFFERSIZE, NetBuffer);
00053 
00054     if(len)
00055     {
00056         ethPacket = (struct netEthHeader*)&NetBuffer[0];
00057 
00058         #if NET_DEBUG >= 5
00059         rprintf("Received packet len: %d, type:", len);
00060         rprintfu16(htons(ethPacket->type));
00061         rprintfCRLF();
00062         rprintf("Packet Contents\r\n");
00063         debugPrintHexTable(len, NetBuffer);
00064         #endif
00065         
00066         if(ethPacket->type == htons(ETHTYPE_IP))
00067         {
00068             // process an IP packet
00069             #ifdef NETSTACK_DEBUG
00070             rprintfProgStrM("NET Rx: IP packet\r\n");
00071             #endif
00072             // add the source to the ARP cache
00073             // also correctly set the ethernet packet length before processing?
00074             arpIpIn((struct netEthIpHeader*)&NetBuffer[0]);
00075             //arpPrintTable();
00076             
00077             netstackIPProcess( len-ETH_HEADER_LEN, (ip_hdr*)&NetBuffer[ETH_HEADER_LEN] );
00078         }
00079         else if(ethPacket->type == htons(ETHTYPE_ARP))
00080         {
00081             // process an ARP packet
00082             #ifdef NETSTACK_DEBUG
00083             rprintfProgStrM("NET Rx: ARP packet\r\n");
00084             #endif
00085             arpArpIn(len, ((struct netEthArpHeader*)&NetBuffer[0]) );
00086         }
00087     }
00088     return len;
00089 }
00090 
00091 void netstackIPProcess(unsigned int len, ip_hdr* packet)
00092 {
00093     // check IP addressing, stop processing if not for me and not a broadcast
00094     if( (htonl(packet->destipaddr) != ipGetConfig()->ip) &&
00095         (htonl(packet->destipaddr) != (ipGetConfig()->ip|ipGetConfig()->netmask)) &&
00096         (htonl(packet->destipaddr) != 0xFFFFFFFF) ) 
00097         return;
00098 
00099     // handle ICMP packet
00100     if( packet->proto == IP_PROTO_ICMP )
00101     {
00102         #ifdef NETSTACK_DEBUG
00103         rprintfProgStrM("NET Rx: ICMP/IP packet\r\n");
00104         //icmpPrintHeader((icmpip_hdr*)packet);
00105         #endif
00106         icmpIpIn((icmpip_hdr*)packet);
00107     }
00108     else if( packet->proto == IP_PROTO_UDP )
00109     {
00110         #ifdef NETSTACK_DEBUG
00111         rprintfProgStrM("NET Rx: UDP/IP packet\r\n");
00112         //debugPrintHexTable(NetBufferLen-14, &NetBuffer[14]);
00113         #endif
00114         netstackUDPIPProcess(len, ((udpip_hdr*)packet) );
00115     }
00116     else if( packet->proto == IP_PROTO_TCP )
00117     {
00118         #ifdef NETSTACK_DEBUG
00119         rprintfProgStrM("NET Rx: TCP/IP packet\r\n");
00120         #endif
00121         netstackTCPIPProcess(len, ((tcpip_hdr*)packet) );
00122     }
00123     else
00124     {
00125         #ifdef NETSTACK_DEBUG
00126         rprintfProgStrM("NET Rx: IP packet\r\n");
00127         #endif
00128     }
00129 }
00130 
00131 void netstackUDPIPProcess(unsigned int len, udpip_hdr* packet)
00132 {
00133     #ifdef NETSTACK_DEBUG
00134     rprintf("NetStack UDP/IP Rx Dummy Handler\r\n");
00135     #endif
00136 }
00137 
00138 void netstackTCPIPProcess(unsigned int len, tcpip_hdr* packet)
00139 {
00140     #ifdef NETSTACK_DEBUG
00141     rprintf("NetStack TCP/IP Rx Dummy Handler\r\n");
00142     #endif
00143 }

Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by  doxygen 1.4.2