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/ip.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>ip.c</h1><a href="ip_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file ip.c \brief IP (Internet Protocol) Library. */</span> |
||
11 | 00002 <span class="comment">//*****************************************************************************</span> |
||
12 | 00003 <span class="comment">//</span> |
||
13 | 00004 <span class="comment">// File Name : 'ip.c'</span> |
||
14 | 00005 <span class="comment">// Title : IP (Internet Protocol) Library</span> |
||
15 | 00006 <span class="comment">// Author : Pascal Stang</span> |
||
16 | 00007 <span class="comment">// Created : 8/30/2004</span> |
||
17 | 00008 <span class="comment">// Revised : 7/3/2005</span> |
||
18 | 00009 <span class="comment">// Version : 0.1</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 |
||
25 | 00016 <span class="preprocessor">#include <inttypes.h></span> |
||
26 | 00017 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span> |
||
27 | 00018 <span class="preprocessor">#include "<a class="code" href="rprintf_8h.html">rprintf.h</a>"</span> |
||
28 | 00019 |
||
29 | 00020 <span class="preprocessor">#include "<a class="code" href="net_8h.html">net.h</a>"</span> |
||
30 | 00021 <span class="preprocessor">#include "<a class="code" href="nic_8h.html">nic.h</a>"</span> |
||
31 | 00022 <span class="preprocessor">#include "<a class="code" href="arp_8h.html">arp.h</a>"</span> |
||
32 | 00023 <span class="preprocessor">#include "<a class="code" href="ip_8h.html">ip.h</a>"</span> |
||
33 | 00024 |
||
34 | <a name="l00025"></a><a class="code" href="ip_8c.html#a0">00025</a> <span class="keyword">struct </span><a class="code" href="structipConfig.html">ipConfig</a> IpMyConfig; <span class="comment">///< Local IP address/config structure</span> |
||
35 | 00026 <span class="comment"></span> |
||
36 | 00027 |
||
37 | <a name="l00028"></a><a class="code" href="group__ip.html#ga0">00028</a> <span class="keywordtype">void</span> <a class="code" href="group__ip.html#ga0">ipSetConfig</a>(uint32_t myIp, uint32_t <a class="code" href="structipConfig.html#o1">netmask</a>, uint32_t gatewayIp) |
||
38 | 00029 { |
||
39 | 00030 <span class="keyword">struct </span>netEthAddr ethaddr; |
||
40 | 00031 |
||
41 | 00032 <span class="comment">// set local addressing</span> |
||
42 | 00033 IpMyConfig.<a class="code" href="structipConfig.html#o0">ip</a> = myIp; |
||
43 | 00034 IpMyConfig.<a class="code" href="structipConfig.html#o1">netmask</a> = netmask; |
||
44 | 00035 IpMyConfig.<a class="code" href="structipConfig.html#o2">gateway</a> = gatewayIp; |
||
45 | 00036 |
||
46 | 00037 <span class="comment">// set ARP association</span> |
||
47 | 00038 nicGetMacAddress(ethaddr.addr); |
||
48 | 00039 <a class="code" href="group__arp.html#ga1">arpSetAddress</a>(&ethaddr, myIp); |
||
49 | 00040 } |
||
50 | 00041 |
||
51 | <a name="l00042"></a><a class="code" href="group__ip.html#ga1">00042</a> <span class="keyword">struct </span><a class="code" href="structipConfig.html">ipConfig</a>* <a class="code" href="group__ip.html#ga1">ipGetConfig</a>(<span class="keywordtype">void</span>) |
||
52 | 00043 { |
||
53 | 00044 <span class="keywordflow">return</span> &IpMyConfig; |
||
54 | 00045 } |
||
55 | 00046 |
||
56 | 00047 <span class="comment">// deprecated</span> |
||
57 | 00048 <span class="comment">/*</span> |
||
58 | 00049 <span class="comment">uint32_t ipGetMyAddress(void)</span> |
||
59 | 00050 <span class="comment">{</span> |
||
60 | 00051 <span class="comment"> return IpMyConfig.ip;</span> |
||
61 | 00052 <span class="comment">}</span> |
||
62 | 00053 <span class="comment">*/</span> |
||
63 | 00054 |
||
64 | <a name="l00055"></a><a class="code" href="group__ip.html#ga3">00055</a> <span class="keywordtype">void</span> <a class="code" href="group__ip.html#ga3">ipSend</a>(uint32_t dstIp, uint8_t protocol, uint16_t len, uint8_t* data) |
||
65 | 00056 { |
||
66 | 00057 <span class="comment">// make pointer to ethernet/IP header</span> |
||
67 | 00058 <span class="keyword">struct </span>netEthIpHeader* ethIpHeader; |
||
68 | 00059 |
||
69 | 00060 <span class="comment">// move data pointer to make room for headers</span> |
||
70 | 00061 data -= ETH_HEADER_LEN+IP_HEADER_LEN; |
||
71 | 00062 ethIpHeader = (<span class="keyword">struct </span>netEthIpHeader*)data; |
||
72 | 00063 |
||
73 | 00064 <span class="comment">// debugPrintHexTable(len+ETH_HEADER_LEN+IP_HEADER_LEN, data);</span> |
||
74 | 00065 |
||
75 | 00066 <span class="comment">// adjust length to add IP header</span> |
||
76 | 00067 len += IP_HEADER_LEN; |
||
77 | 00068 |
||
78 | 00069 <span class="comment">// fill IP header</span> |
||
79 | 00070 ethIpHeader->ip.destipaddr = <a class="code" href="group__net.html#ga34">HTONL</a>(dstIp); |
||
80 | 00071 ethIpHeader->ip.srcipaddr = <a class="code" href="group__net.html#ga34">HTONL</a>(IpMyConfig.<a class="code" href="structipConfig.html#o0">ip</a>); |
||
81 | 00072 ethIpHeader->ip.proto = protocol; |
||
82 | 00073 ethIpHeader->ip.len = <a class="code" href="group__net.html#ga2">htons</a>(len); |
||
83 | 00074 ethIpHeader->ip.vhl = 0x45; |
||
84 | 00075 ethIpHeader->ip.tos = 0; |
||
85 | 00076 ethIpHeader->ip.ipid = 0; |
||
86 | 00077 ethIpHeader->ip.ipoffset = 0; |
||
87 | 00078 ethIpHeader->ip.ttl = <a class="code" href="group__ip.html#ga5">IP_TIME_TO_LIVE</a>; |
||
88 | 00079 ethIpHeader->ip.ipchksum = 0; |
||
89 | 00080 |
||
90 | 00081 <span class="comment">// calculate and apply IP checksum</span> |
||
91 | 00082 <span class="comment">// DO THIS ONLY AFTER ALL CHANGES HAVE BEEN MADE TO IP HEADER</span> |
||
92 | 00083 ethIpHeader->ip.ipchksum = <a class="code" href="group__net.html#ga4">netChecksum</a>(&ethIpHeader->ip, IP_HEADER_LEN); |
||
93 | 00084 |
||
94 | 00085 <span class="comment">// add ethernet routing</span> |
||
95 | 00086 <span class="comment">// check if we need to send to gateway</span> |
||
96 | 00087 <span class="keywordflow">if</span>( (dstIp & IpMyConfig.<a class="code" href="structipConfig.html#o1">netmask</a>) == (IpMyConfig.<a class="code" href="structipConfig.html#o0">ip</a> & IpMyConfig.<a class="code" href="structipConfig.html#o1">netmask</a>) ) |
||
97 | 00088 { |
||
98 | 00089 <a class="code" href="group__arp.html#ga4">arpIpOut</a>(ethIpHeader,0); <span class="comment">// local send</span> |
||
99 | 00090 <span class="comment">//rprintf("Sending IP packet on local net\r\n");</span> |
||
100 | 00091 } |
||
101 | 00092 <span class="keywordflow">else</span> |
||
102 | 00093 { |
||
103 | 00094 <a class="code" href="group__arp.html#ga4">arpIpOut</a>(ethIpHeader,IpMyConfig.<a class="code" href="structipConfig.html#o2">gateway</a>); <span class="comment">// gateway send</span> |
||
104 | 00095 <span class="comment">//rprintf("Sending IP packet to gateway\r\n");</span> |
||
105 | 00096 } |
||
106 | 00097 |
||
107 | 00098 <span class="comment">// adjust length to add ethernet header</span> |
||
108 | 00099 len += ETH_HEADER_LEN; |
||
109 | 00100 |
||
110 | 00101 <span class="comment">// debug</span> |
||
111 | 00102 <span class="comment">//debugPrintHexTable(ETH_HEADER_LEN, &data[0]);</span> |
||
112 | 00103 <span class="comment">//debugPrintHexTable(len-ETH_HEADER_LEN, &data[ETH_HEADER_LEN]);</span> |
||
113 | 00104 |
||
114 | 00105 <span class="comment">// send it</span> |
||
115 | 00106 <a class="code" href="group__nic.html#ga1">nicSend</a>(len, data); |
||
116 | 00107 } |
||
117 | 00108 |
||
118 | <a name="l00109"></a><a class="code" href="group__ip.html#ga4">00109</a> <span class="keywordtype">void</span> <a class="code" href="group__ip.html#ga4">udpSend</a>(uint32_t dstIp, uint16_t dstPort, uint16_t len, uint8_t* data) |
||
119 | 00110 { |
||
120 | 00111 <span class="comment">// make pointer to UDP header</span> |
||
121 | 00112 <span class="keyword">struct </span>netUdpHeader* udpHeader; |
||
122 | 00113 <span class="comment">// move data pointer to make room for UDP header</span> |
||
123 | 00114 data -= UDP_HEADER_LEN; |
||
124 | 00115 udpHeader = (<span class="keyword">struct </span>netUdpHeader*)data; |
||
125 | 00116 <span class="comment">// adjust length to add UDP header</span> |
||
126 | 00117 len += UDP_HEADER_LEN; |
||
127 | 00118 <span class="comment">// fill UDP header</span> |
||
128 | 00119 udpHeader->destport = <a class="code" href="group__net.html#ga33">HTONS</a>(dstPort); |
||
129 | 00120 udpHeader->srcport = <a class="code" href="group__net.html#ga33">HTONS</a>(dstPort); |
||
130 | 00121 udpHeader->udplen = <a class="code" href="group__net.html#ga2">htons</a>(len); |
||
131 | 00122 udpHeader->udpchksum = 0; |
||
132 | 00123 |
||
133 | 00124 <span class="comment">// debugPrintHexTable(UDP_HEADER_LEN, (uint8_t*)udpPacket);</span> |
||
134 | 00125 |
||
135 | 00126 <a class="code" href="group__ip.html#ga3">ipSend</a>(dstIp, IP_PROTO_UDP, len, (uint8_t*)udpHeader); |
||
136 | 00127 } |
||
137 | 00128 |
||
138 | <a name="l00129"></a><a class="code" href="group__ip.html#ga2">00129</a> <span class="keywordtype">void</span> <a class="code" href="group__ip.html#ga2">ipPrintConfig</a>(<span class="keyword">struct</span> <a class="code" href="structipConfig.html">ipConfig</a>* config) |
||
139 | 00130 { |
||
140 | 00131 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"IP Addr : "</span>); <a class="code" href="group__net.html#ga6">netPrintIPAddr</a>(config-><a class="code" href="structipConfig.html#o0">ip</a>); <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>(); |
||
141 | 00132 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"Netmask : "</span>); <a class="code" href="group__net.html#ga6">netPrintIPAddr</a>(config-><a class="code" href="structipConfig.html#o1">netmask</a>); <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>(); |
||
142 | 00133 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"Gateway : "</span>); <a class="code" href="group__net.html#ga6">netPrintIPAddr</a>(config-><a class="code" href="structipConfig.html#o2">gateway</a>); <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>(); |
||
143 | 00134 } |
||
144 | </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by |
||
145 | <a href="http://www.doxygen.org/index.html"> |
||
146 | <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address> |
||
147 | </body> |
||
148 | </html> |
Powered by WebSVN v2.8.3