| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 6 | kaklik | /*! \file netstack.c \brief Network Stack. */ |
| 2 | //***************************************************************************** |
||
| 3 | // |
||
| 4 | // File Name : 'netstack.c' |
||
| 5 | // Title : Network Stack |
||
| 6 | // Author : Pascal Stang |
||
| 7 | // Created : 6/28/2005 |
||
| 8 | // Revised : 9/20/2005 |
||
| 9 | // Version : 0.3 |
||
| 10 | // Target MCU : Atmel AVR series |
||
| 11 | // Editor Tabs : 4 |
||
| 12 | // |
||
| 13 | //***************************************************************************** |
||
| 14 | |||
| 15 | #include "rprintf.h" |
||
| 16 | #include "debug.h" |
||
| 17 | |||
| 18 | #include "netstack.h" |
||
| 19 | |||
| 20 | unsigned char NetBuffer[NETSTACK_BUFFERSIZE]; |
||
| 21 | |||
| 22 | void netstackInit(uint32_t ipaddress, uint32_t netmask, uint32_t gatewayip) |
||
| 23 | { |
||
| 24 | // init network device driver |
||
| 25 | #ifdef NETSTACK_DEBUG |
||
| 26 | rprintf("Initializing Network Device\r\n"); |
||
| 27 | #endif |
||
| 28 | nicInit(); |
||
| 29 | // init ARP |
||
| 30 | #ifdef NETSTACK_DEBUG |
||
| 31 | rprintf("Initializing ARP cache\r\n"); |
||
| 32 | #endif |
||
| 33 | arpInit(); |
||
| 34 | // init addressing |
||
| 35 | #ifdef NETSTACK_DEBUG |
||
| 36 | rprintf("Initializing Addressing\r\n"); |
||
| 37 | #endif |
||
| 38 | ipSetConfig(ipaddress, netmask, gatewayip); |
||
| 39 | } |
||
| 40 | |||
| 41 | u08* netstackGetBuffer(void) |
||
| 42 | { |
||
| 43 | return NetBuffer; |
||
| 44 | } |
||
| 45 | |||
| 46 | int netstackService(void) |
||
| 47 | { |
||
| 48 | int len; |
||
| 49 | struct netEthHeader* ethPacket; |
||
| 50 | |||
| 51 | // look for a packet |
||
| 52 | len = nicPoll(NETSTACK_BUFFERSIZE, NetBuffer); |
||
| 53 | |||
| 54 | if(len) |
||
| 55 | { |
||
| 56 | ethPacket = (struct netEthHeader*)&NetBuffer[0]; |
||
| 57 | |||
| 58 | #if NET_DEBUG >= 5 |
||
| 59 | rprintf("Received packet len: %d, type:", len); |
||
| 60 | rprintfu16(htons(ethPacket->type)); |
||
| 61 | rprintfCRLF(); |
||
| 62 | rprintf("Packet Contents\r\n"); |
||
| 63 | debugPrintHexTable(len, NetBuffer); |
||
| 64 | #endif |
||
| 65 | |||
| 66 | if(ethPacket->type == htons(ETHTYPE_IP)) |
||
| 67 | { |
||
| 68 | // process an IP packet |
||
| 69 | #ifdef NETSTACK_DEBUG |
||
| 70 | rprintfProgStrM("NET Rx: IP packet\r\n"); |
||
| 71 | #endif |
||
| 72 | // add the source to the ARP cache |
||
| 73 | // also correctly set the ethernet packet length before processing? |
||
| 74 | arpIpIn((struct netEthIpHeader*)&NetBuffer[0]); |
||
| 75 | //arpPrintTable(); |
||
| 76 | |||
| 77 | netstackIPProcess( len-ETH_HEADER_LEN, (ip_hdr*)&NetBuffer[ETH_HEADER_LEN] ); |
||
| 78 | } |
||
| 79 | else if(ethPacket->type == htons(ETHTYPE_ARP)) |
||
| 80 | { |
||
| 81 | // process an ARP packet |
||
| 82 | #ifdef NETSTACK_DEBUG |
||
| 83 | rprintfProgStrM("NET Rx: ARP packet\r\n"); |
||
| 84 | #endif |
||
| 85 | arpArpIn(len, ((struct netEthArpHeader*)&NetBuffer[0]) ); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | return len; |
||
| 89 | } |
||
| 90 | |||
| 91 | void netstackIPProcess(unsigned int len, ip_hdr* packet) |
||
| 92 | { |
||
| 93 | // check IP addressing, stop processing if not for me and not a broadcast |
||
| 94 | if( (htonl(packet->destipaddr) != ipGetConfig()->ip) && |
||
| 95 | (htonl(packet->destipaddr) != (ipGetConfig()->ip|ipGetConfig()->netmask)) && |
||
| 96 | (htonl(packet->destipaddr) != 0xFFFFFFFF) ) |
||
| 97 | return; |
||
| 98 | |||
| 99 | // handle ICMP packet |
||
| 100 | if( packet->proto == IP_PROTO_ICMP ) |
||
| 101 | { |
||
| 102 | #ifdef NETSTACK_DEBUG |
||
| 103 | rprintfProgStrM("NET Rx: ICMP/IP packet\r\n"); |
||
| 104 | //icmpPrintHeader((icmpip_hdr*)packet); |
||
| 105 | #endif |
||
| 106 | icmpIpIn((icmpip_hdr*)packet); |
||
| 107 | } |
||
| 108 | else if( packet->proto == IP_PROTO_UDP ) |
||
| 109 | { |
||
| 110 | #ifdef NETSTACK_DEBUG |
||
| 111 | rprintfProgStrM("NET Rx: UDP/IP packet\r\n"); |
||
| 112 | //debugPrintHexTable(NetBufferLen-14, &NetBuffer[14]); |
||
| 113 | #endif |
||
| 114 | netstackUDPIPProcess(len, ((udpip_hdr*)packet) ); |
||
| 115 | } |
||
| 116 | else if( packet->proto == IP_PROTO_TCP ) |
||
| 117 | { |
||
| 118 | #ifdef NETSTACK_DEBUG |
||
| 119 | rprintfProgStrM("NET Rx: TCP/IP packet\r\n"); |
||
| 120 | #endif |
||
| 121 | netstackTCPIPProcess(len, ((tcpip_hdr*)packet) ); |
||
| 122 | } |
||
| 123 | else |
||
| 124 | { |
||
| 125 | #ifdef NETSTACK_DEBUG |
||
| 126 | rprintfProgStrM("NET Rx: IP packet\r\n"); |
||
| 127 | #endif |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | void netstackUDPIPProcess(unsigned int len, udpip_hdr* packet) |
||
| 132 | { |
||
| 133 | #ifdef NETSTACK_DEBUG |
||
| 134 | rprintf("NetStack UDP/IP Rx Dummy Handler\r\n"); |
||
| 135 | #endif |
||
| 136 | } |
||
| 137 | |||
| 138 | void netstackTCPIPProcess(unsigned int len, tcpip_hdr* packet) |
||
| 139 | { |
||
| 140 | #ifdef NETSTACK_DEBUG |
||
| 141 | rprintf("NetStack TCP/IP Rx Dummy Handler\r\n"); |
||
| 142 | #endif |
||
| 143 | } |
Powered by WebSVN v2.8.3