| Line No. | Rev | Author | Line | 
|---|---|---|---|
| 1 | 6 | kaklik | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> | 
         
| 2 | <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> | 
         ||
| 3 | <title>Procyon AVRlib: net/netstack.c Source File</title> | 
         ||
| 4 | <link href="dox.css" rel="stylesheet" type="text/css"> | 
         ||
| 5 | </head><body> | 
         ||
| 6 | <!-- Generated by Doxygen 1.4.2 --> | 
         ||
| 7 | <div class="qindex"><a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related Pages</a></div> | 
         ||
| 8 | <div class="nav"> | 
         ||
| 9 | <a class="el" href="dir_000001.html">net</a></div> | 
         ||
| 10 | <h1>netstack.c</h1><a href="netstack_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file netstack.c \brief Network Stack. */</span> | 
         ||
| 11 | 00002 <span class="comment">//*****************************************************************************</span> | 
         ||
| 12 | 00003 <span class="comment">//</span> | 
         ||
| 13 | 00004 <span class="comment">// File Name    : 'netstack.c'</span> | 
         ||
| 14 | 00005 <span class="comment">// Title        : Network Stack</span> | 
         ||
| 15 | 00006 <span class="comment">// Author       : Pascal Stang</span> | 
         ||
| 16 | 00007 <span class="comment">// Created      : 6/28/2005</span> | 
         ||
| 17 | 00008 <span class="comment">// Revised      : 9/20/2005</span> | 
         ||
| 18 | 00009 <span class="comment">// Version      : 0.3</span> | 
         ||
| 19 | 00010 <span class="comment">// Target MCU   : Atmel AVR series</span> | 
         ||
| 20 | 00011 <span class="comment">// Editor Tabs  : 4</span> | 
         ||
| 21 | 00012 <span class="comment">//</span> | 
         ||
| 22 | 00013 <span class="comment">//*****************************************************************************</span> | 
         ||
| 23 | 00014  | 
         ||
| 24 | 00015 <span class="preprocessor">#include "<a class="code" href="rprintf_8h.html">rprintf.h</a>"</span> | 
         ||
| 25 | 00016 <span class="preprocessor">#include "<a class="code" href="debug_8h.html">debug.h</a>"</span> | 
         ||
| 26 | 00017  | 
         ||
| 27 | 00018 <span class="preprocessor">#include "<a class="code" href="netstack_8h.html">netstack.h</a>"</span> | 
         ||
| 28 | 00019  | 
         ||
| 29 | 00020 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> NetBuffer[<a class="code" href="group__netstack.html#ga6">NETSTACK_BUFFERSIZE</a>]; | 
         ||
| 30 | 00021  | 
         ||
| 31 | <a name="l00022"></a><a class="code" href="group__netstack.html#ga0">00022</a> <span class="keywordtype">void</span> <a class="code" href="group__netstack.html#ga0">netstackInit</a>(uint32_t ipaddress, uint32_t netmask, uint32_t gatewayip) | 
         ||
| 32 | 00023 { | 
         ||
| 33 | 00024     <span class="comment">// init network device driver</span> | 
         ||
| 34 | 00025 <span class="preprocessor">    #ifdef NETSTACK_DEBUG</span> | 
         ||
| 35 | 00026 <span class="preprocessor"></span>    rprintf(<span class="stringliteral">"Initializing Network Device\r\n"</span>); | 
         ||
| 36 | 00027 <span class="preprocessor">    #endif</span> | 
         ||
| 37 | 00028 <span class="preprocessor"></span>    <a class="code" href="group__nic.html#ga0">nicInit</a>(); | 
         ||
| 38 | 00029     <span class="comment">// init ARP</span> | 
         ||
| 39 | 00030 <span class="preprocessor">    #ifdef NETSTACK_DEBUG</span> | 
         ||
| 40 | 00031 <span class="preprocessor"></span>    rprintf(<span class="stringliteral">"Initializing ARP cache\r\n"</span>); | 
         ||
| 41 | 00032 <span class="preprocessor">    #endif</span> | 
         ||
| 42 | 00033 <span class="preprocessor"></span>    <a class="code" href="group__arp.html#ga0">arpInit</a>(); | 
         ||
| 43 | 00034     <span class="comment">// init addressing</span> | 
         ||
| 44 | 00035 <span class="preprocessor">    #ifdef NETSTACK_DEBUG</span> | 
         ||
| 45 | 00036 <span class="preprocessor"></span>    rprintf(<span class="stringliteral">"Initializing Addressing\r\n"</span>); | 
         ||
| 46 | 00037 <span class="preprocessor">    #endif</span> | 
         ||
| 47 | 00038 <span class="preprocessor"></span>    <a class="code" href="group__ip.html#ga0">ipSetConfig</a>(ipaddress, netmask, gatewayip); | 
         ||
| 48 | 00039 } | 
         ||
| 49 | 00040  | 
         ||
| 50 | <a name="l00041"></a><a class="code" href="group__netstack.html#ga1">00041</a> u08* <a class="code" href="group__netstack.html#ga1">netstackGetBuffer</a>(<span class="keywordtype">void</span>) | 
         ||
| 51 | 00042 { | 
         ||
| 52 | 00043     <span class="keywordflow">return</span> NetBuffer; | 
         ||
| 53 | 00044 } | 
         ||
| 54 | 00045  | 
         ||
| 55 | <a name="l00046"></a><a class="code" href="group__netstack.html#ga2">00046</a> <span class="keywordtype">int</span> <a class="code" href="group__netstack.html#ga2">netstackService</a>(<span class="keywordtype">void</span>) | 
         ||
| 56 | 00047 { | 
         ||
| 57 | 00048     <span class="keywordtype">int</span> len; | 
         ||
| 58 | 00049     <span class="keyword">struct </span>netEthHeader* ethPacket; | 
         ||
| 59 | 00050  | 
         ||
| 60 | 00051     <span class="comment">// look for a packet</span> | 
         ||
| 61 | 00052     len = <a class="code" href="group__nic.html#ga2">nicPoll</a>(<a class="code" href="group__netstack.html#ga6">NETSTACK_BUFFERSIZE</a>, NetBuffer); | 
         ||
| 62 | 00053  | 
         ||
| 63 | 00054     <span class="keywordflow">if</span>(len) | 
         ||
| 64 | 00055     { | 
         ||
| 65 | 00056         ethPacket = (<span class="keyword">struct </span>netEthHeader*)&NetBuffer[0]; | 
         ||
| 66 | 00057  | 
         ||
| 67 | 00058 <span class="preprocessor">        #if NET_DEBUG >= 5</span> | 
         ||
| 68 | 00059 <span class="preprocessor"></span>        rprintf(<span class="stringliteral">"Received packet len: %d, type:"</span>, len); | 
         ||
| 69 | 00060         <a class="code" href="group__rprintf.html#ga8">rprintfu16</a>(<a class="code" href="group__net.html#ga2">htons</a>(ethPacket->type)); | 
         ||
| 70 | 00061         <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>(); | 
         ||
| 71 | 00062         rprintf(<span class="stringliteral">"Packet Contents\r\n"</span>); | 
         ||
| 72 | 00063         <a class="code" href="debug_8c.html#a0">debugPrintHexTable</a>(len, NetBuffer); | 
         ||
| 73 | 00064 <span class="preprocessor">        #endif</span> | 
         ||
| 74 | 00065 <span class="preprocessor"></span>         | 
         ||
| 75 | 00066         <span class="keywordflow">if</span>(ethPacket->type == <a class="code" href="group__net.html#ga2">htons</a>(ETHTYPE_IP)) | 
         ||
| 76 | 00067         { | 
         ||
| 77 | 00068             <span class="comment">// process an IP packet</span> | 
         ||
| 78 | 00069 <span class="preprocessor">            #ifdef NETSTACK_DEBUG</span> | 
         ||
| 79 | 00070 <span class="preprocessor"></span>            <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"NET Rx: IP packet\r\n"</span>); | 
         ||
| 80 | 00071 <span class="preprocessor">            #endif</span> | 
         ||
| 81 | 00072 <span class="preprocessor"></span>            <span class="comment">// add the source to the ARP cache</span> | 
         ||
| 82 | 00073             <span class="comment">// also correctly set the ethernet packet length before processing?</span> | 
         ||
| 83 | 00074             <a class="code" href="group__arp.html#ga3">arpIpIn</a>((<span class="keyword">struct</span> netEthIpHeader*)&NetBuffer[0]); | 
         ||
| 84 | 00075             <span class="comment">//arpPrintTable();</span> | 
         ||
| 85 | 00076              | 
         ||
| 86 | 00077             <a class="code" href="group__netstack.html#ga3">netstackIPProcess</a>( len-ETH_HEADER_LEN, (ip_hdr*)&NetBuffer[ETH_HEADER_LEN] ); | 
         ||
| 87 | 00078         } | 
         ||
| 88 | 00079         <span class="keywordflow">else</span> <span class="keywordflow">if</span>(ethPacket->type == <a class="code" href="group__net.html#ga2">htons</a>(ETHTYPE_ARP)) | 
         ||
| 89 | 00080         { | 
         ||
| 90 | 00081             <span class="comment">// process an ARP packet</span> | 
         ||
| 91 | 00082 <span class="preprocessor">            #ifdef NETSTACK_DEBUG</span> | 
         ||
| 92 | 00083 <span class="preprocessor"></span>            <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"NET Rx: ARP packet\r\n"</span>); | 
         ||
| 93 | 00084 <span class="preprocessor">            #endif</span> | 
         ||
| 94 | 00085 <span class="preprocessor"></span>            <a class="code" href="group__arp.html#ga2">arpArpIn</a>(len, ((<span class="keyword">struct</span> netEthArpHeader*)&NetBuffer[0]) ); | 
         ||
| 95 | 00086         } | 
         ||
| 96 | 00087     } | 
         ||
| 97 | 00088     <span class="keywordflow">return</span> len; | 
         ||
| 98 | 00089 } | 
         ||
| 99 | 00090  | 
         ||
| 100 | <a name="l00091"></a><a class="code" href="group__netstack.html#ga3">00091</a> <span class="keywordtype">void</span> <a class="code" href="group__netstack.html#ga3">netstackIPProcess</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> len, ip_hdr* packet) | 
         ||
| 101 | 00092 { | 
         ||
| 102 | 00093     <span class="comment">// check IP addressing, stop processing if not for me and not a broadcast</span> | 
         ||
| 103 | 00094     <span class="keywordflow">if</span>( (<a class="code" href="group__net.html#ga3">htonl</a>(packet->destipaddr) != <a class="code" href="group__ip.html#ga1">ipGetConfig</a>()->ip) && | 
         ||
| 104 | 00095         (<a class="code" href="group__net.html#ga3">htonl</a>(packet->destipaddr) != (<a class="code" href="group__ip.html#ga1">ipGetConfig</a>()->ip|<a class="code" href="group__ip.html#ga1">ipGetConfig</a>()->netmask)) && | 
         ||
| 105 | 00096         (<a class="code" href="group__net.html#ga3">htonl</a>(packet->destipaddr) != 0xFFFFFFFF) )  | 
         ||
| 106 | 00097         <span class="keywordflow">return</span>; | 
         ||
| 107 | 00098  | 
         ||
| 108 | 00099     <span class="comment">// handle ICMP packet</span> | 
         ||
| 109 | 00100     <span class="keywordflow">if</span>( packet->proto == IP_PROTO_ICMP ) | 
         ||
| 110 | 00101     { | 
         ||
| 111 | 00102 <span class="preprocessor">        #ifdef NETSTACK_DEBUG</span> | 
         ||
| 112 | 00103 <span class="preprocessor"></span>        <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"NET Rx: ICMP/IP packet\r\n"</span>); | 
         ||
| 113 | 00104         <span class="comment">//icmpPrintHeader((icmpip_hdr*)packet);</span> | 
         ||
| 114 | 00105 <span class="preprocessor">        #endif</span> | 
         ||
| 115 | 00106 <span class="preprocessor"></span>        <a class="code" href="group__icmp.html#ga1">icmpIpIn</a>((icmpip_hdr*)packet); | 
         ||
| 116 | 00107     } | 
         ||
| 117 | 00108     <span class="keywordflow">else</span> <span class="keywordflow">if</span>( packet->proto == IP_PROTO_UDP ) | 
         ||
| 118 | 00109     { | 
         ||
| 119 | 00110 <span class="preprocessor">        #ifdef NETSTACK_DEBUG</span> | 
         ||
| 120 | 00111 <span class="preprocessor"></span>        <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"NET Rx: UDP/IP packet\r\n"</span>); | 
         ||
| 121 | 00112         <span class="comment">//debugPrintHexTable(NetBufferLen-14, &NetBuffer[14]);</span> | 
         ||
| 122 | 00113 <span class="preprocessor">        #endif</span> | 
         ||
| 123 | 00114 <span class="preprocessor"></span>        <a class="code" href="group__netstack.html#ga4">netstackUDPIPProcess</a>(len, ((udpip_hdr*)packet) ); | 
         ||
| 124 | 00115     } | 
         ||
| 125 | 00116     <span class="keywordflow">else</span> <span class="keywordflow">if</span>( packet->proto == IP_PROTO_TCP ) | 
         ||
| 126 | 00117     { | 
         ||
| 127 | 00118 <span class="preprocessor">        #ifdef NETSTACK_DEBUG</span> | 
         ||
| 128 | 00119 <span class="preprocessor"></span>        <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"NET Rx: TCP/IP packet\r\n"</span>); | 
         ||
| 129 | 00120 <span class="preprocessor">        #endif</span> | 
         ||
| 130 | 00121 <span class="preprocessor"></span>        <a class="code" href="group__netstack.html#ga5">netstackTCPIPProcess</a>(len, ((tcpip_hdr*)packet) ); | 
         ||
| 131 | 00122     } | 
         ||
| 132 | 00123     <span class="keywordflow">else</span> | 
         ||
| 133 | 00124     { | 
         ||
| 134 | 00125 <span class="preprocessor">        #ifdef NETSTACK_DEBUG</span> | 
         ||
| 135 | 00126 <span class="preprocessor"></span>        <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"NET Rx: IP packet\r\n"</span>); | 
         ||
| 136 | 00127 <span class="preprocessor">        #endif</span> | 
         ||
| 137 | 00128 <span class="preprocessor"></span>    } | 
         ||
| 138 | 00129 } | 
         ||
| 139 | 00130  | 
         ||
| 140 | <a name="l00131"></a><a class="code" href="group__netstack.html#ga4">00131</a> <span class="keywordtype">void</span> <a class="code" href="group__netstack.html#ga4">netstackUDPIPProcess</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> len, udpip_hdr* packet) | 
         ||
| 141 | 00132 { | 
         ||
| 142 | 00133 <span class="preprocessor">    #ifdef NETSTACK_DEBUG</span> | 
         ||
| 143 | 00134 <span class="preprocessor"></span>    rprintf(<span class="stringliteral">"NetStack UDP/IP Rx Dummy Handler\r\n"</span>); | 
         ||
| 144 | 00135 <span class="preprocessor">    #endif</span> | 
         ||
| 145 | 00136 <span class="preprocessor"></span>} | 
         ||
| 146 | 00137  | 
         ||
| 147 | <a name="l00138"></a><a class="code" href="group__netstack.html#ga5">00138</a> <span class="keywordtype">void</span> <a class="code" href="group__netstack.html#ga5">netstackTCPIPProcess</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> len, tcpip_hdr* packet) | 
         ||
| 148 | 00139 { | 
         ||
| 149 | 00140 <span class="preprocessor">    #ifdef NETSTACK_DEBUG</span> | 
         ||
| 150 | 00141 <span class="preprocessor"></span>    rprintf(<span class="stringliteral">"NetStack TCP/IP Rx Dummy Handler\r\n"</span>); | 
         ||
| 151 | 00142 <span class="preprocessor">    #endif</span> | 
         ||
| 152 | 00143 <span class="preprocessor"></span>} | 
         ||
| 153 | </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by  | 
         ||
| 154 | <a href="http://www.doxygen.org/index.html"> | 
         ||
| 155 | <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address> | 
         ||
| 156 | </body> | 
         ||
| 157 | </html> | 
         
Powered by WebSVN v2.8.3