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 NetBufferData[NETSTACK_BUFFERSIZE] __attribute__ ((aligned));
    00021 unsigned char* NetBuffer = &NetBufferData[2];
    00022 //#define NetBuffer (&NetBufferData[2])
    00023
    00024 void netstackInit(uint32_t ipaddress, uint32_t netmask, uint32_t gatewayip)
    00025 {
    00026     // init network device driver
    00027     #ifdef NETSTACK_DEBUG
    00028     rprintf("Initializing Network Device\r\n");
    00029     #endif
    00030     nicInit();
    00031     // init ARP
    00032     #ifdef NETSTACK_DEBUG
    00033     rprintf("Initializing ARP cache\r\n");
    00034     #endif
    00035     arpInit();
    00036     // init addressing
    00037     #ifdef NETSTACK_DEBUG
    00038     rprintf("Initializing Addressing\r\n");
    00039     #endif
    00040     ipSetConfig(ipaddress, netmask, gatewayip);
    00041 }
    00042
    00043 u08* netstackGetBuffer(void)
    00044 {
    00045     return NetBuffer;
    00046 }
    00047
    00048 int netstackService(void)
    00049 {
    00050     int len;
    00051     struct netEthHeader* ethPacket;
    00052
    00053     // look for a packet
    00054     len = nicPoll(NETSTACK_BUFFERSIZE, NetBuffer);
    00055
    00056     if(len)
    00057     {
    00058         ethPacket = (struct netEthHeader*)&NetBuffer[0];
    00059
    00060         #if NET_DEBUG >= 5
    00061         rprintf("Received packet len: %d, type:", len);
    00062         rprintfu16(htons(ethPacket->type));
    00063         rprintfCRLF();
    00064         rprintf("Packet Contents\r\n");
    00065         debugPrintHexTable(len, NetBuffer);
    00066         #endif
    00067
    00068         if(ethPacket->type == htons(ETHTYPE_IP))
    00069         {
    00070             // process an IP packet
    00071             #ifdef NETSTACK_DEBUG
    00072             rprintfProgStrM("NET Rx: IP packet\r\n");
    00073             #endif
    00074             // add the source to the ARP cache
    00075             // also correctly set the ethernet packet length before processing?
    00076             arpIpIn((struct netEthIpHeader*)&NetBuffer[0]);
    00077             //arpPrintTable();
    00078
    00079             netstackIPProcess( len-ETH_HEADER_LEN, (ip_hdr*)&NetBuffer[ETH_HEADER_LEN] );
    00080         }
    00081         else if(ethPacket->type == htons(ETHTYPE_ARP))
    00082         {
    00083             // process an ARP packet
    00084             #ifdef NETSTACK_DEBUG
    00085             rprintfProgStrM("NET Rx: ARP packet\r\n");
    00086             #endif
    00087             arpArpIn(len, ((struct netEthArpHeader*)&NetBuffer[0]) );
    00088         }
    00089     }
    00090     return len;
    00091 }
    00092
    00093 void netstackIPProcess(unsigned int len, ip_hdr* packet)
    00094 {
    00095     // check IP addressing, stop processing if not for me and not a broadcast
    00096     if( (htonl(packet->destipaddr) != ipGetConfig()->ip) &&
    00097         (htonl(packet->destipaddr) != (ipGetConfig()->ip|ipGetConfig()->netmask)) &&
    00098         (htonl(packet->destipaddr) != 0xFFFFFFFF) )
    00099         return;
    00100
    00101     // handle ICMP packet
    00102     if( packet->proto == IP_PROTO_ICMP )
    00103     {
    00104         #ifdef NETSTACK_DEBUG
    00105         rprintfProgStrM("NET Rx: ICMP/IP packet\r\n");
    00106         //icmpPrintHeader((icmpip_hdr*)packet);
    00107         #endif
    00108         icmpIpIn((icmpip_hdr*)packet);
    00109     }
    00110     else if( packet->proto == IP_PROTO_UDP )
    00111     {
    00112         #ifdef NETSTACK_DEBUG
    00113         rprintfProgStrM("NET Rx: UDP/IP packet\r\n");
    00114         //debugPrintHexTable(NetBufferLen-14, &NetBuffer[14]);
    00115         #endif
    00116         netstackUDPIPProcess(len, ((udpip_hdr*)packet) );
    00117     }
    00118     else if( packet->proto == IP_PROTO_TCP )
    00119     {
    00120         #ifdef NETSTACK_DEBUG
    00121         rprintfProgStrM("NET Rx: TCP/IP packet\r\n");
    00122         #endif
    00123         netstackTCPIPProcess(len, ((tcpip_hdr*)packet) );
    00124     }
    00125     else
    00126     {
    00127         #ifdef NETSTACK_DEBUG
    00128         rprintfProgStrM("NET Rx: IP packet\r\n");
    00129         #endif
    00130     }
    00131 }
    00132
    00133 void netstackUDPIPProcess(unsigned int len, udpip_hdr* packet)
    00134 {
    00135     #ifdef NETSTACK_DEBUG
    00136     rprintf("NetStack UDP/IP Rx Dummy Handler\r\n");
    00137     #endif
    00138     asm volatile ("nop");
    00139 }
    00140
    00141 void netstackTCPIPProcess(unsigned int len, tcpip_hdr* packet)
    00142 {
    00143     #ifdef NETSTACK_DEBUG
    00144     rprintf("NetStack TCP/IP Rx Dummy Handler\r\n");
    00145     #endif
    00146     asm volatile ("nop");
    00147 }