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/dhcp.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>dhcp.c</h1><a href="dhcp_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file dhcp.c \brief DHCP Protocol Library. */</span> |
||
11 | 00002 <span class="comment">//*****************************************************************************</span> |
||
12 | 00003 <span class="comment">//</span> |
||
13 | 00004 <span class="comment">// File Name : 'dhcp.c'</span> |
||
14 | 00005 <span class="comment">// Title : DHCP Protocol Library</span> |
||
15 | 00006 <span class="comment">// Author : Pascal Stang</span> |
||
16 | 00007 <span class="comment">// Created : 9/17/2005</span> |
||
17 | 00008 <span class="comment">// Revised : 9/17/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 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span> |
||
25 | 00016 <span class="preprocessor">#include "<a class="code" href="net_8h.html">net.h</a>"</span> |
||
26 | 00017 <span class="preprocessor">#include "<a class="code" href="nic_8h.html">nic.h</a>"</span> |
||
27 | 00018 <span class="preprocessor">#include "<a class="code" href="ip_8h.html">ip.h</a>"</span> |
||
28 | 00019 <span class="preprocessor">#include "<a class="code" href="netstack_8h.html">netstack.h</a>"</span> |
||
29 | 00020 |
||
30 | 00021 <span class="preprocessor">#include "<a class="code" href="dhcp_8h.html">dhcp.h</a>"</span> |
||
31 | 00022 |
||
32 | 00023 <span class="preprocessor">#include "<a class="code" href="rprintf_8h.html">rprintf.h</a>"</span> |
||
33 | 00024 |
||
34 | 00025 <span class="comment">// global variables</span> |
||
35 | <a name="l00026"></a><a class="code" href="dhcp_8c.html#a0">00026</a> uint32_t <a class="code" href="dhcp_8c.html#a0">DhcpServerIP</a>; <span class="comment">///< IP address of the DHCP server that offered lease</span> |
||
36 | <a name="l00027"></a><a class="code" href="dhcp_8c.html#a1">00027</a> <span class="comment"></span>uint32_t <a class="code" href="dhcp_8c.html#a1">DhcpTransactID</a>; <span class="comment">///< Unique transaction ID that identifies DHCP request/replies</span> |
||
37 | <a name="l00028"></a><a class="code" href="dhcp_8c.html#a2">00028</a> <span class="comment"></span>uint32_t <a class="code" href="dhcp_8c.html#a2">DhcpLeaseTime</a>; <span class="comment">///< Number of seconds left in DHCP lease</span> |
||
38 | 00029 <span class="comment"></span> |
||
39 | <a name="l00030"></a><a class="code" href="group__dhcp.html#ga1">00030</a> <span class="keywordtype">void</span> <a class="code" href="group__dhcp.html#ga1">dhcpInit</a>(<span class="keywordtype">void</span>) |
||
40 | 00031 { |
||
41 | 00032 uint8_t macaddr[6]; |
||
42 | 00033 |
||
43 | 00034 <span class="comment">// get interface mac address</span> |
||
44 | 00035 nicGetMacAddress(macaddr); |
||
45 | 00036 <span class="comment">// set transaction ID based on mac address</span> |
||
46 | 00037 <a class="code" href="dhcp_8c.html#a1">DhcpTransactID</a> = *((uint32_t*)&macaddr); |
||
47 | 00038 <span class="comment">// reset lease time</span> |
||
48 | 00039 <a class="code" href="dhcp_8c.html#a2">DhcpLeaseTime</a> = 0; |
||
49 | 00040 } |
||
50 | 00041 |
||
51 | <a name="l00042"></a><a class="code" href="group__dhcp.html#ga2">00042</a> <span class="keywordtype">void</span> <a class="code" href="group__dhcp.html#ga2">dhcpIn</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> len, <span class="keyword">struct</span> <a class="code" href="structnetDhcpHeader.html">netDhcpHeader</a>* packet) |
||
52 | 00043 { |
||
53 | 00044 uint8_t msgtype; |
||
54 | 00045 uint32_t sid; |
||
55 | 00046 uint8_t* optptr; |
||
56 | 00047 uint32_t val; |
||
57 | 00048 uint32_t netmask; |
||
58 | 00049 uint32_t gateway; |
||
59 | 00050 |
||
60 | 00051 <span class="preprocessor"> #if NET_DEBUG >= 3</span> |
||
61 | 00052 <span class="preprocessor"></span> <a class="code" href="group__dhcp.html#ga8">dhcpPrintHeader</a>(packet); |
||
62 | 00053 <span class="preprocessor"> #endif</span> |
||
63 | 00054 <span class="preprocessor"></span> |
||
64 | 00055 <span class="comment">// check that this is a reply, and for me</span> |
||
65 | 00056 <span class="keywordflow">if</span>((packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o0">op</a> != <a class="code" href="group__dhcp.html#ga11">BOOTP_OP_BOOTREPLY</a>) || (packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o4">xid</a> != <a class="code" href="dhcp_8c.html#a1">DhcpTransactID</a>)) |
||
66 | 00057 <span class="keywordflow">return</span>; |
||
67 | 00058 |
||
68 | 00059 <span class="comment">// process incoming packet</span> |
||
69 | 00060 <span class="comment">// check reply type</span> |
||
70 | 00061 <a class="code" href="group__dhcp.html#ga6">dhcpGetOption</a>(packet-><a class="code" href="structnetDhcpHeader.html#o2">options</a>, <a class="code" href="group__dhcp.html#ga27">DHCP_OPT_DHCPMSGTYPE</a>, 1, &msgtype); |
||
71 | 00062 <span class="preprocessor"> #if NET_DEBUG >= 2</span> |
||
72 | 00063 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"DHCP: Received msgtype = %d\r\n"</span>, msgtype); |
||
73 | 00064 <span class="preprocessor"> #endif</span> |
||
74 | 00065 <span class="preprocessor"></span> |
||
75 | 00066 <span class="keywordflow">if</span>(msgtype == <a class="code" href="group__dhcp.html#ga34">DHCP_MSG_DHCPOFFER</a>) |
||
76 | 00067 { |
||
77 | 00068 <span class="comment">// get DHCP server ID</span> |
||
78 | 00069 <a class="code" href="group__dhcp.html#ga6">dhcpGetOption</a>(packet-><a class="code" href="structnetDhcpHeader.html#o2">options</a>, <a class="code" href="group__dhcp.html#ga28">DHCP_OPT_SERVERID</a>, 4, &sid); |
||
79 | 00070 <span class="preprocessor"> #ifdef DHCP_DEBUG</span> |
||
80 | 00071 <span class="preprocessor"></span> <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"DHCP: Got offer from server "</span>); <a class="code" href="group__net.html#ga6">netPrintIPAddr</a>(<a class="code" href="group__net.html#ga3">htonl</a>(sid)); <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>(); |
||
81 | 00072 <span class="preprocessor"> #endif</span> |
||
82 | 00073 <span class="preprocessor"></span> |
||
83 | 00074 <span class="comment">// build DHCP request (on top of this reply)</span> |
||
84 | 00075 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o0">op</a> = <a class="code" href="group__dhcp.html#ga10">BOOTP_OP_BOOTREQUEST</a>; <span class="comment">// request type</span> |
||
85 | 00076 <span class="comment">// set operation</span> |
||
86 | 00077 val = <a class="code" href="group__dhcp.html#ga35">DHCP_MSG_DHCPREQUEST</a>; |
||
87 | 00078 optptr = <a class="code" href="group__dhcp.html#ga7">dhcpSetOption</a>(packet-><a class="code" href="structnetDhcpHeader.html#o2">options</a>, <a class="code" href="group__dhcp.html#ga27">DHCP_OPT_DHCPMSGTYPE</a>, 1, &val); |
||
88 | 00079 <span class="comment">// set the server ID</span> |
||
89 | 00080 optptr = <a class="code" href="group__dhcp.html#ga7">dhcpSetOption</a>(optptr, <a class="code" href="group__dhcp.html#ga28">DHCP_OPT_SERVERID</a>, 4, &sid); |
||
90 | 00081 <span class="comment">// request the IP previously offered</span> |
||
91 | 00082 optptr = <a class="code" href="group__dhcp.html#ga7">dhcpSetOption</a>(optptr, <a class="code" href="group__dhcp.html#ga25">DHCP_OPT_REQUESTEDIP</a>, 4, &packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o8">yiaddr</a>); |
||
92 | 00083 <span class="comment">// request additional information</span> |
||
93 | 00084 ((uint8_t*)&val)[0] = <a class="code" href="group__dhcp.html#ga18">DHCP_OPT_NETMASK</a>; |
||
94 | 00085 ((uint8_t*)&val)[1] = <a class="code" href="group__dhcp.html#ga19">DHCP_OPT_ROUTERS</a>; |
||
95 | 00086 ((uint8_t*)&val)[2] = <a class="code" href="group__dhcp.html#ga22">DHCP_OPT_DNSSERVERS</a>; |
||
96 | 00087 ((uint8_t*)&val)[3] = <a class="code" href="group__dhcp.html#ga24">DHCP_OPT_DOMAINNAME</a>; |
||
97 | 00088 optptr = <a class="code" href="group__dhcp.html#ga7">dhcpSetOption</a>(optptr, <a class="code" href="group__dhcp.html#ga29">DHCP_OPT_PARAMREQLIST</a>, 4, &val); |
||
98 | 00089 |
||
99 | 00090 <span class="preprocessor"> #ifdef DHCP_DEBUG</span> |
||
100 | 00091 <span class="preprocessor"></span> <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"DHCP: Sending request in response to offer\r\n"</span>); |
||
101 | 00092 <span class="preprocessor"> #endif</span> |
||
102 | 00093 <span class="preprocessor"></span> <span class="comment">// send DHCP request</span> |
||
103 | 00094 <a class="code" href="dhcp_8c.html#a0">DhcpServerIP</a> = <a class="code" href="group__net.html#ga3">htonl</a>(sid); |
||
104 | 00095 <a class="code" href="group__ip.html#ga4">udpSend</a>(<a class="code" href="dhcp_8c.html#a0">DhcpServerIP</a>, <a class="code" href="group__dhcp.html#ga15">DHCP_UDP_SERVER_PORT</a>, <a class="code" href="group__dhcp.html#ga14">DHCP_HEADER_LEN</a>+3+6+6+6+1, (uint8_t*)packet); |
||
105 | 00096 |
||
106 | 00097 } |
||
107 | 00098 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(msgtype == <a class="code" href="group__dhcp.html#ga37">DHCP_MSG_DHCPACK</a>) |
||
108 | 00099 { |
||
109 | 00100 <span class="comment">// get netmask</span> |
||
110 | 00101 <a class="code" href="group__dhcp.html#ga6">dhcpGetOption</a>(packet-><a class="code" href="structnetDhcpHeader.html#o2">options</a>, <a class="code" href="group__dhcp.html#ga18">DHCP_OPT_NETMASK</a>, 4, &val); |
||
111 | 00102 netmask = <a class="code" href="group__net.html#ga3">htonl</a>(val); |
||
112 | 00103 <span class="comment">// get gateway</span> |
||
113 | 00104 <a class="code" href="group__dhcp.html#ga6">dhcpGetOption</a>(packet-><a class="code" href="structnetDhcpHeader.html#o2">options</a>, <a class="code" href="group__dhcp.html#ga19">DHCP_OPT_ROUTERS</a>, 4, &val); |
||
114 | 00105 gateway = <a class="code" href="group__net.html#ga3">htonl</a>(val); |
||
115 | 00106 <span class="comment">// get gateway</span> |
||
116 | 00107 <a class="code" href="group__dhcp.html#ga6">dhcpGetOption</a>(packet-><a class="code" href="structnetDhcpHeader.html#o2">options</a>, <a class="code" href="group__dhcp.html#ga26">DHCP_OPT_LEASETIME</a>, 4, &val); |
||
117 | 00108 <a class="code" href="dhcp_8c.html#a2">DhcpLeaseTime</a> = <a class="code" href="group__net.html#ga3">htonl</a>(val); |
||
118 | 00109 |
||
119 | 00110 <span class="comment">// assign new network info</span> |
||
120 | 00111 <a class="code" href="group__ip.html#ga0">ipSetConfig</a>(<a class="code" href="group__net.html#ga3">htonl</a>(packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o8">yiaddr</a>), netmask, gateway); |
||
121 | 00112 |
||
122 | 00113 <span class="preprocessor"> #ifdef DHCP_DEBUG</span> |
||
123 | 00114 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"DHCP: Got request ACK, bind complete\r\n"</span>); |
||
124 | 00115 <span class="comment">//debugPrintHexTable(len-DHCP_HEADER_LEN, (packet->options));</span> |
||
125 | 00116 <span class="comment">// print info</span> |
||
126 | 00117 <a class="code" href="group__ip.html#ga2">ipPrintConfig</a>(<a class="code" href="group__ip.html#ga1">ipGetConfig</a>()); |
||
127 | 00118 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"LeaseTm : "</span>); <a class="code" href="group__rprintf.html#ga10">rprintfNum</a>(10,8,FALSE,<span class="charliteral">' '</span>,<a class="code" href="dhcp_8c.html#a2">DhcpLeaseTime</a>); <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>(); |
||
128 | 00119 <span class="preprocessor"> #endif</span> |
||
129 | 00120 <span class="preprocessor"></span> } |
||
130 | 00121 } |
||
131 | 00122 |
||
132 | <a name="l00123"></a><a class="code" href="group__dhcp.html#ga3">00123</a> <span class="keywordtype">void</span> <a class="code" href="group__dhcp.html#ga3">dhcpRequest</a>(<span class="keywordtype">void</span>) |
||
133 | 00124 { |
||
134 | 00125 <span class="keyword">struct </span><a class="code" href="structnetDhcpHeader.html">netDhcpHeader</a>* packet; |
||
135 | 00126 uint32_t val; |
||
136 | 00127 |
||
137 | 00128 packet = (<span class="keyword">struct </span><a class="code" href="structnetDhcpHeader.html">netDhcpHeader</a>*)&<a class="code" href="group__netstack.html#ga1">netstackGetBuffer</a>()[ETH_HEADER_LEN+IP_HEADER_LEN+UDP_HEADER_LEN]; |
||
138 | 00129 |
||
139 | 00130 <span class="comment">// build BOOTP/DHCP header</span> |
||
140 | 00131 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o0">op</a> = <a class="code" href="group__dhcp.html#ga10">BOOTP_OP_BOOTREQUEST</a>; <span class="comment">// request type</span> |
||
141 | 00132 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o1">htype</a> = BOOTP_HTYPE_ETHERNET; |
||
142 | 00133 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o2">hlen</a> = BOOTP_HLEN_ETHERNET; |
||
143 | 00134 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o7">ciaddr</a> = <a class="code" href="group__net.html#ga3">htonl</a>(<a class="code" href="group__ip.html#ga1">ipGetConfig</a>()->ip); |
||
144 | 00135 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o8">yiaddr</a> = <a class="code" href="group__net.html#ga34">HTONL</a>(0l); |
||
145 | 00136 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o9">siaddr</a> = <a class="code" href="group__net.html#ga34">HTONL</a>(0l); |
||
146 | 00137 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o10">giaddr</a> = <a class="code" href="group__net.html#ga34">HTONL</a>(0l); |
||
147 | 00138 nicGetMacAddress(&packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o11">chaddr</a>[0]); <span class="comment">// fill client hardware address</span> |
||
148 | 00139 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o4">xid</a> = <a class="code" href="dhcp_8c.html#a1">DhcpTransactID</a>; |
||
149 | 00140 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o6">flags</a> = <a class="code" href="group__net.html#ga33">HTONS</a>(1); |
||
150 | 00141 |
||
151 | 00142 <span class="comment">// build DHCP request</span> |
||
152 | 00143 <span class="comment">// begin with magic cookie</span> |
||
153 | 00144 packet-><a class="code" href="structnetDhcpHeader.html#o1">cookie</a> = 0x63538263; |
||
154 | 00145 <span class="comment">// set operation</span> |
||
155 | 00146 val = <a class="code" href="group__dhcp.html#ga33">DHCP_MSG_DHCPDISCOVER</a>; |
||
156 | 00147 <a class="code" href="group__dhcp.html#ga7">dhcpSetOption</a>(packet-><a class="code" href="structnetDhcpHeader.html#o2">options</a>, <a class="code" href="group__dhcp.html#ga27">DHCP_OPT_DHCPMSGTYPE</a>, 1, &val); |
||
157 | 00148 |
||
158 | 00149 <span class="preprocessor"> #ifdef DHCP_DEBUG</span> |
||
159 | 00150 <span class="preprocessor"></span> <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"DHCP: Sending Query\r\n"</span>); |
||
160 | 00151 <span class="comment">//dhcpPrintHeader(packet);</span> |
||
161 | 00152 <span class="preprocessor"> #endif</span> |
||
162 | 00153 <span class="preprocessor"></span> |
||
163 | 00154 <span class="comment">// send request</span> |
||
164 | 00155 <a class="code" href="group__ip.html#ga4">udpSend</a>(0xFFFFFFFF, <a class="code" href="group__dhcp.html#ga15">DHCP_UDP_SERVER_PORT</a>, <a class="code" href="group__dhcp.html#ga14">DHCP_HEADER_LEN</a>+3+1, (uint8_t*)packet); |
||
165 | 00156 } |
||
166 | 00157 |
||
167 | <a name="l00158"></a><a class="code" href="group__dhcp.html#ga4">00158</a> <span class="keywordtype">void</span> <a class="code" href="group__dhcp.html#ga4">dhcpRelease</a>(<span class="keywordtype">void</span>) |
||
168 | 00159 { |
||
169 | 00160 <span class="keyword">struct </span><a class="code" href="structnetDhcpHeader.html">netDhcpHeader</a>* packet; |
||
170 | 00161 uint32_t val; |
||
171 | 00162 uint8_t* optptr; |
||
172 | 00163 |
||
173 | 00164 packet = (<span class="keyword">struct </span><a class="code" href="structnetDhcpHeader.html">netDhcpHeader</a>*)&<a class="code" href="group__netstack.html#ga1">netstackGetBuffer</a>()[ETH_HEADER_LEN+IP_HEADER_LEN+UDP_HEADER_LEN]; |
||
174 | 00165 |
||
175 | 00166 <span class="comment">// build BOOTP/DHCP header</span> |
||
176 | 00167 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o0">op</a> = <a class="code" href="group__dhcp.html#ga10">BOOTP_OP_BOOTREQUEST</a>; <span class="comment">// request type</span> |
||
177 | 00168 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o1">htype</a> = BOOTP_HTYPE_ETHERNET; |
||
178 | 00169 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o2">hlen</a> = BOOTP_HLEN_ETHERNET; |
||
179 | 00170 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o7">ciaddr</a> = <a class="code" href="group__net.html#ga3">htonl</a>(<a class="code" href="group__ip.html#ga1">ipGetConfig</a>()->ip); |
||
180 | 00171 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o8">yiaddr</a> = <a class="code" href="group__net.html#ga34">HTONL</a>(0l); |
||
181 | 00172 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o9">siaddr</a> = <a class="code" href="group__net.html#ga34">HTONL</a>(0l); |
||
182 | 00173 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o10">giaddr</a> = <a class="code" href="group__net.html#ga34">HTONL</a>(0l); |
||
183 | 00174 nicGetMacAddress(&packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o11">chaddr</a>[0]); <span class="comment">// fill client hardware address</span> |
||
184 | 00175 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o4">xid</a> = <a class="code" href="dhcp_8c.html#a1">DhcpTransactID</a>; <span class="comment">// set trans ID (use part of MAC address)</span> |
||
185 | 00176 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o6">flags</a> = <a class="code" href="group__net.html#ga33">HTONS</a>(1); |
||
186 | 00177 |
||
187 | 00178 <span class="comment">// build DHCP request</span> |
||
188 | 00179 <span class="comment">// begin with magic cookie</span> |
||
189 | 00180 packet-><a class="code" href="structnetDhcpHeader.html#o1">cookie</a> = 0x63538263; |
||
190 | 00181 <span class="comment">// set operation</span> |
||
191 | 00182 val = <a class="code" href="group__dhcp.html#ga39">DHCP_MSG_DHCPRELEASE</a>; |
||
192 | 00183 optptr = <a class="code" href="group__dhcp.html#ga7">dhcpSetOption</a>(packet-><a class="code" href="structnetDhcpHeader.html#o2">options</a>, <a class="code" href="group__dhcp.html#ga27">DHCP_OPT_DHCPMSGTYPE</a>, 1, &val); |
||
193 | 00184 <span class="comment">// set the server ID</span> |
||
194 | 00185 val = <a class="code" href="group__net.html#ga3">htonl</a>(<a class="code" href="dhcp_8c.html#a0">DhcpServerIP</a>); |
||
195 | 00186 optptr = <a class="code" href="group__dhcp.html#ga7">dhcpSetOption</a>(optptr, <a class="code" href="group__dhcp.html#ga28">DHCP_OPT_SERVERID</a>, 4, &val); |
||
196 | 00187 <span class="comment">// request the IP previously offered</span> |
||
197 | 00188 optptr = <a class="code" href="group__dhcp.html#ga7">dhcpSetOption</a>(optptr, <a class="code" href="group__dhcp.html#ga25">DHCP_OPT_REQUESTEDIP</a>, 4, &packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o7">ciaddr</a>); |
||
198 | 00189 |
||
199 | 00190 <span class="preprocessor"> #ifdef DHCP_DEBUG</span> |
||
200 | 00191 <span class="preprocessor"></span> <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"DHCP: Sending Release to "</span>); <a class="code" href="group__net.html#ga6">netPrintIPAddr</a>(<a class="code" href="dhcp_8c.html#a0">DhcpServerIP</a>); <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>(); |
||
201 | 00192 <span class="comment">//dhcpPrintHeader(packet);</span> |
||
202 | 00193 <span class="preprocessor"> #endif</span> |
||
203 | 00194 <span class="preprocessor"></span> |
||
204 | 00195 <span class="comment">// send release</span> |
||
205 | 00196 <a class="code" href="group__ip.html#ga4">udpSend</a>(<a class="code" href="dhcp_8c.html#a0">DhcpServerIP</a>, <a class="code" href="group__dhcp.html#ga15">DHCP_UDP_SERVER_PORT</a>, <a class="code" href="group__dhcp.html#ga14">DHCP_HEADER_LEN</a>+3+6+6+1, (uint8_t*)packet); |
||
206 | 00197 |
||
207 | 00198 <span class="comment">// deconfigure ip addressing</span> |
||
208 | 00199 <a class="code" href="group__ip.html#ga0">ipSetConfig</a>(0,0,0); |
||
209 | 00200 <a class="code" href="dhcp_8c.html#a2">DhcpLeaseTime</a> = 0; |
||
210 | 00201 } |
||
211 | 00202 |
||
212 | <a name="l00203"></a><a class="code" href="group__dhcp.html#ga5">00203</a> <span class="keywordtype">void</span> <a class="code" href="group__dhcp.html#ga5">dhcpTimer</a>(<span class="keywordtype">void</span>) |
||
213 | 00204 { |
||
214 | 00205 <span class="comment">// this function to be called once per second</span> |
||
215 | 00206 |
||
216 | 00207 <span class="comment">// decrement lease time</span> |
||
217 | 00208 <span class="keywordflow">if</span>(<a class="code" href="dhcp_8c.html#a2">DhcpLeaseTime</a>) |
||
218 | 00209 <a class="code" href="dhcp_8c.html#a2">DhcpLeaseTime</a>--; |
||
219 | 00210 } |
||
220 | 00211 |
||
221 | <a name="l00212"></a><a class="code" href="group__dhcp.html#ga6">00212</a> uint8_t <a class="code" href="group__dhcp.html#ga6">dhcpGetOption</a>(uint8_t* <a class="code" href="structnetDhcpHeader.html#o2">options</a>, uint8_t optcode, uint8_t optlen, <span class="keywordtype">void</span>* optvalptr) |
||
222 | 00213 { |
||
223 | 00214 uint8_t i; |
||
224 | 00215 |
||
225 | 00216 <span class="comment">// parse for desired option</span> |
||
226 | 00217 <span class="keywordflow">for</span> (;;) |
||
227 | 00218 { |
||
228 | 00219 <span class="comment">// skip pad characters</span> |
||
229 | 00220 <span class="keywordflow">if</span>(*options == <a class="code" href="group__dhcp.html#ga17">DHCP_OPT_PAD</a>) |
||
230 | 00221 options++; |
||
231 | 00222 <span class="comment">// break if end reached</span> |
||
232 | 00223 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(*options == <a class="code" href="group__dhcp.html#ga32">DHCP_OPT_END</a>) |
||
233 | 00224 <span class="keywordflow">break</span>; |
||
234 | 00225 <span class="comment">// check for desired option</span> |
||
235 | 00226 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(*options == optcode) |
||
236 | 00227 { |
||
237 | 00228 <span class="comment">// found desired option</span> |
||
238 | 00229 <span class="comment">// limit size to actual option length</span> |
||
239 | 00230 optlen = MIN(optlen, *(options+1)); |
||
240 | 00231 <span class="comment">//if(*(options+1) < optlen)</span> |
||
241 | 00232 <span class="comment">// optlen = *(options+1);</span> |
||
242 | 00233 |
||
243 | 00234 <span class="comment">// copy contents of option</span> |
||
244 | 00235 <span class="keywordflow">for</span>(i=0; i<optlen; i++) |
||
245 | 00236 *(((uint8_t*)optvalptr)+i) = *(options+i+2); |
||
246 | 00237 <span class="comment">// return length of option</span> |
||
247 | 00238 <span class="keywordflow">return</span> *(options+1); |
||
248 | 00239 } |
||
249 | 00240 <span class="keywordflow">else</span> |
||
250 | 00241 { |
||
251 | 00242 <span class="comment">// skip to next option</span> |
||
252 | 00243 options++; |
||
253 | 00244 options+=*options; |
||
254 | 00245 options++; |
||
255 | 00246 } |
||
256 | 00247 } |
||
257 | 00248 <span class="comment">// failed to find desired option</span> |
||
258 | 00249 <span class="keywordflow">return</span> 0; |
||
259 | 00250 } |
||
260 | 00251 |
||
261 | 00252 |
||
262 | <a name="l00253"></a><a class="code" href="group__dhcp.html#ga7">00253</a> uint8_t* <a class="code" href="group__dhcp.html#ga7">dhcpSetOption</a>(uint8_t* <a class="code" href="structnetDhcpHeader.html#o2">options</a>, uint8_t optcode, uint8_t optlen, <span class="keywordtype">void</span>* optvalptr) |
||
263 | 00254 { |
||
264 | 00255 <span class="comment">// use current options address as write point</span> |
||
265 | 00256 |
||
266 | 00257 <span class="comment">// set optcode</span> |
||
267 | 00258 *options++ = optcode; |
||
268 | 00259 <span class="comment">// set optlen</span> |
||
269 | 00260 *options++ = optlen; |
||
270 | 00261 <span class="comment">// copy in argument/data</span> |
||
271 | 00262 <span class="keywordflow">while</span>(optlen--) |
||
272 | 00263 { |
||
273 | 00264 *options++ = *(uint8_t*)optvalptr++; |
||
274 | 00265 } |
||
275 | 00266 <span class="comment">// write end marker</span> |
||
276 | 00267 *options = <a class="code" href="group__dhcp.html#ga32">DHCP_OPT_END</a>; |
||
277 | 00268 |
||
278 | 00269 <span class="comment">// return address of end marker, to be used as a future write point</span> |
||
279 | 00270 <span class="keywordflow">return</span> options; |
||
280 | 00271 } |
||
281 | 00272 |
||
282 | 00273 |
||
283 | 00274 <span class="preprocessor">#ifdef DHCP_DEBUG_PRINT</span> |
||
284 | 00275 <span class="preprocessor"></span><span class="keywordtype">void</span> <a class="code" href="group__dhcp.html#ga8">dhcpPrintHeader</a>(<span class="keyword">struct</span> <a class="code" href="structnetDhcpHeader.html">netDhcpHeader</a>* packet) |
||
285 | 00276 { |
||
286 | 00277 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"DHCP Packet:\r\n"</span>); |
||
287 | 00278 <span class="comment">// print op</span> |
||
288 | 00279 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"Op : "</span>); |
||
289 | 00280 <span class="keywordflow">switch</span>(packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o0">op</a>) |
||
290 | 00281 { |
||
291 | 00282 <span class="keywordflow">case</span> <a class="code" href="group__dhcp.html#ga10">BOOTP_OP_BOOTREQUEST</a>: <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"BOOTREQUEST"</span>); <span class="keywordflow">break</span>; |
||
292 | 00283 <span class="keywordflow">case</span> <a class="code" href="group__dhcp.html#ga11">BOOTP_OP_BOOTREPLY</a>: <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"BOOTREPLY"</span>); <span class="keywordflow">break</span>; |
||
293 | 00284 <span class="keywordflow">default</span>: <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"UNKNOWN"</span>); <span class="keywordflow">break</span>; |
||
294 | 00285 } |
||
295 | 00286 <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>(); |
||
296 | 00287 <span class="comment">// print transaction ID</span> |
||
297 | 00288 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"XID : 0x"</span>); <a class="code" href="group__rprintf.html#ga9">rprintfu32</a>(packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o4">xid</a>); <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>(); |
||
298 | 00289 <span class="comment">// print client IP address</span> |
||
299 | 00290 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"ClIpAddr: "</span>); <a class="code" href="group__net.html#ga6">netPrintIPAddr</a>(<a class="code" href="group__net.html#ga3">htonl</a>(packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o7">ciaddr</a>)); <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>(); |
||
300 | 00291 <span class="comment">// print 'your' IP address</span> |
||
301 | 00292 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"YrIpAddr: "</span>); <a class="code" href="group__net.html#ga6">netPrintIPAddr</a>(<a class="code" href="group__net.html#ga3">htonl</a>(packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o8">yiaddr</a>)); <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>(); |
||
302 | 00293 <span class="comment">// print server IP address</span> |
||
303 | 00294 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"SvIpAddr: "</span>); <a class="code" href="group__net.html#ga6">netPrintIPAddr</a>(<a class="code" href="group__net.html#ga3">htonl</a>(packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o9">siaddr</a>)); <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>(); |
||
304 | 00295 <span class="comment">// print gateway IP address</span> |
||
305 | 00296 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"GwIpAddr: "</span>); <a class="code" href="group__net.html#ga6">netPrintIPAddr</a>(<a class="code" href="group__net.html#ga3">htonl</a>(packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o10">giaddr</a>)); <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>(); |
||
306 | 00297 <span class="comment">// print client hardware address</span> |
||
307 | 00298 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"ClHwAddr: "</span>); <a class="code" href="group__net.html#ga5">netPrintEthAddr</a>((<span class="keyword">struct</span> netEthAddr*)packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o11">chaddr</a>); <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>(); |
||
308 | 00299 } |
||
309 | 00300 <span class="preprocessor">#endif</span> |
||
310 | </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by |
||
311 | <a href="http://www.doxygen.org/index.html"> |
||
312 | <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address> |
||
313 | </body> |
||
314 | </html> |
Powered by WebSVN v2.8.3