<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Procyon AVRlib: net/dhcp.c Source File</title>
<link href="dox.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.2 -->
<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>
<div class="nav">
<a class="el" href="dir_000001.html">net</a></div>
<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>
00002 <span class="comment">//*****************************************************************************</span>
00003 <span class="comment">//</span>
00004 <span class="comment">// File Name : 'dhcp.c'</span>
00005 <span class="comment">// Title : DHCP Protocol Library</span>
00006 <span class="comment">// Author : Pascal Stang</span>
00007 <span class="comment">// Created : 9/17/2005</span>
00008 <span class="comment">// Revised : 9/17/2005</span>
00009 <span class="comment">// Version : 0.1</span>
00010 <span class="comment">// Target MCU : Atmel AVR series</span>
00011 <span class="comment">// Editor Tabs : 4</span>
00012 <span class="comment">//</span>
00013 <span class="comment">//*****************************************************************************</span>
00014
00015 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>
00016 <span class="preprocessor">#include "<a class="code" href="net_8h.html">net.h</a>"</span>
00017 <span class="preprocessor">#include "<a class="code" href="nic_8h.html">nic.h</a>"</span>
00018 <span class="preprocessor">#include "<a class="code" href="ip_8h.html">ip.h</a>"</span>
00019 <span class="preprocessor">#include "<a class="code" href="netstack_8h.html">netstack.h</a>"</span>
00020
00021 <span class="preprocessor">#include "<a class="code" href="dhcp_8h.html">dhcp.h</a>"</span>
00022
00023 <span class="preprocessor">#include "<a class="code" href="rprintf_8h.html">rprintf.h</a>"</span>
00024
00025 <span class="comment">// global variables</span>
<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>
<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>
<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>
00029 <span class="comment"></span>
<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>)
00031 {
00032 uint8_t macaddr[6];
00033
00034 <span class="comment">// get interface mac address</span>
00035 nicGetMacAddress(macaddr);
00036 <span class="comment">// set transaction ID based on mac address</span>
00037 <a class="code" href="dhcp_8c.html#a1">DhcpTransactID</a> = *((uint32_t*)&macaddr);
00038 <span class="comment">// reset lease time</span>
00039 <a class="code" href="dhcp_8c.html#a2">DhcpLeaseTime</a> = 0;
00040 }
00041
<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)
00043 {
00044 uint8_t msgtype;
00045 uint32_t sid;
00046 uint8_t* optptr;
00047 uint32_t val;
00048 uint32_t netmask;
00049 uint32_t gateway;
00050
00051 <span class="preprocessor"> #if NET_DEBUG >= 3</span>
00052 <span class="preprocessor"></span> <a class="code" href="group__dhcp.html#ga8">dhcpPrintHeader</a>(packet);
00053 <span class="preprocessor"> #endif</span>
00054 <span class="preprocessor"></span>
00055 <span class="comment">// check that this is a reply, and for me</span>
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>))
00057 <span class="keywordflow">return</span>;
00058
00059 <span class="comment">// process incoming packet</span>
00060 <span class="comment">// check reply type</span>
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);
00062 <span class="preprocessor"> #if NET_DEBUG >= 2</span>
00063 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"DHCP: Received msgtype = %d\r\n"</span>, msgtype);
00064 <span class="preprocessor"> #endif</span>
00065 <span class="preprocessor"></span>
00066 <span class="keywordflow">if</span>(msgtype == <a class="code" href="group__dhcp.html#ga34">DHCP_MSG_DHCPOFFER</a>)
00067 {
00068 <span class="comment">// get DHCP server ID</span>
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);
00070 <span class="preprocessor"> #ifdef DHCP_DEBUG</span>
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>();
00072 <span class="preprocessor"> #endif</span>
00073 <span class="preprocessor"></span>
00074 <span class="comment">// build DHCP request (on top of this reply)</span>
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>
00076 <span class="comment">// set operation</span>
00077 val = <a class="code" href="group__dhcp.html#ga35">DHCP_MSG_DHCPREQUEST</a>;
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);
00079 <span class="comment">// set the server ID</span>
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);
00081 <span class="comment">// request the IP previously offered</span>
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>);
00083 <span class="comment">// request additional information</span>
00084 ((uint8_t*)&val)[0] = <a class="code" href="group__dhcp.html#ga18">DHCP_OPT_NETMASK</a>;
00085 ((uint8_t*)&val)[1] = <a class="code" href="group__dhcp.html#ga19">DHCP_OPT_ROUTERS</a>;
00086 ((uint8_t*)&val)[2] = <a class="code" href="group__dhcp.html#ga22">DHCP_OPT_DNSSERVERS</a>;
00087 ((uint8_t*)&val)[3] = <a class="code" href="group__dhcp.html#ga24">DHCP_OPT_DOMAINNAME</a>;
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);
00089
00090 <span class="preprocessor"> #ifdef DHCP_DEBUG</span>
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>);
00092 <span class="preprocessor"> #endif</span>
00093 <span class="preprocessor"></span> <span class="comment">// send DHCP request</span>
00094 <a class="code" href="dhcp_8c.html#a0">DhcpServerIP</a> = <a class="code" href="group__net.html#ga3">htonl</a>(sid);
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);
00096
00097 }
00098 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(msgtype == <a class="code" href="group__dhcp.html#ga37">DHCP_MSG_DHCPACK</a>)
00099 {
00100 <span class="comment">// get netmask</span>
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);
00102 netmask = <a class="code" href="group__net.html#ga3">htonl</a>(val);
00103 <span class="comment">// get gateway</span>
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);
00105 gateway = <a class="code" href="group__net.html#ga3">htonl</a>(val);
00106 <span class="comment">// get gateway</span>
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);
00108 <a class="code" href="dhcp_8c.html#a2">DhcpLeaseTime</a> = <a class="code" href="group__net.html#ga3">htonl</a>(val);
00109
00110 <span class="comment">// assign new network info</span>
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);
00112
00113 <span class="preprocessor"> #ifdef DHCP_DEBUG</span>
00114 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"DHCP: Got request ACK, bind complete\r\n"</span>);
00115 <span class="comment">//debugPrintHexTable(len-DHCP_HEADER_LEN, (packet->options));</span>
00116 <span class="comment">// print info</span>
00117 <a class="code" href="group__ip.html#ga2">ipPrintConfig</a>(<a class="code" href="group__ip.html#ga1">ipGetConfig</a>());
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>();
00119 <span class="preprocessor"> #endif</span>
00120 <span class="preprocessor"></span> }
00121 }
00122
<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>)
00124 {
00125 <span class="keyword">struct </span><a class="code" href="structnetDhcpHeader.html">netDhcpHeader</a>* packet;
00126 uint32_t val;
00127
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];
00129
00130 <span class="comment">// build BOOTP/DHCP header</span>
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>
00132 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o1">htype</a> = BOOTP_HTYPE_ETHERNET;
00133 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o2">hlen</a> = BOOTP_HLEN_ETHERNET;
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);
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);
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);
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);
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>
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>;
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);
00141
00142 <span class="comment">// build DHCP request</span>
00143 <span class="comment">// begin with magic cookie</span>
00144 packet-><a class="code" href="structnetDhcpHeader.html#o1">cookie</a> = 0x63538263;
00145 <span class="comment">// set operation</span>
00146 val = <a class="code" href="group__dhcp.html#ga33">DHCP_MSG_DHCPDISCOVER</a>;
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);
00148
00149 <span class="preprocessor"> #ifdef DHCP_DEBUG</span>
00150 <span class="preprocessor"></span> <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"DHCP: Sending Query\r\n"</span>);
00151 <span class="comment">//dhcpPrintHeader(packet);</span>
00152 <span class="preprocessor"> #endif</span>
00153 <span class="preprocessor"></span>
00154 <span class="comment">// send request</span>
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);
00156 }
00157
<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>)
00159 {
00160 <span class="keyword">struct </span><a class="code" href="structnetDhcpHeader.html">netDhcpHeader</a>* packet;
00161 uint32_t val;
00162 uint8_t* optptr;
00163
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];
00165
00166 <span class="comment">// build BOOTP/DHCP header</span>
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>
00168 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o1">htype</a> = BOOTP_HTYPE_ETHERNET;
00169 packet-><a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o2">hlen</a> = BOOTP_HLEN_ETHERNET;
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);
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);
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);
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);
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>
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>
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);
00177
00178 <span class="comment">// build DHCP request</span>
00179 <span class="comment">// begin with magic cookie</span>
00180 packet-><a class="code" href="structnetDhcpHeader.html#o1">cookie</a> = 0x63538263;
00181 <span class="comment">// set operation</span>
00182 val = <a class="code" href="group__dhcp.html#ga39">DHCP_MSG_DHCPRELEASE</a>;
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);
00184 <span class="comment">// set the server ID</span>
00185 val = <a class="code" href="group__net.html#ga3">htonl</a>(<a class="code" href="dhcp_8c.html#a0">DhcpServerIP</a>);
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);
00187 <span class="comment">// request the IP previously offered</span>
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>);
00189
00190 <span class="preprocessor"> #ifdef DHCP_DEBUG</span>
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>();
00192 <span class="comment">//dhcpPrintHeader(packet);</span>
00193 <span class="preprocessor"> #endif</span>
00194 <span class="preprocessor"></span>
00195 <span class="comment">// send release</span>
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);
00197
00198 <span class="comment">// deconfigure ip addressing</span>
00199 <a class="code" href="group__ip.html#ga0">ipSetConfig</a>(0,0,0);
00200 <a class="code" href="dhcp_8c.html#a2">DhcpLeaseTime</a> = 0;
00201 }
00202
<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>)
00204 {
00205 <span class="comment">// this function to be called once per second</span>
00206
00207 <span class="comment">// decrement lease time</span>
00208 <span class="keywordflow">if</span>(<a class="code" href="dhcp_8c.html#a2">DhcpLeaseTime</a>)
00209 <a class="code" href="dhcp_8c.html#a2">DhcpLeaseTime</a>--;
00210 }
00211
<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)
00213 {
00214 uint8_t i;
00215
00216 <span class="comment">// parse for desired option</span>
00217 <span class="keywordflow">for</span> (;;)
00218 {
00219 <span class="comment">// skip pad characters</span>
00220 <span class="keywordflow">if</span>(*options == <a class="code" href="group__dhcp.html#ga17">DHCP_OPT_PAD</a>)
00221 options++;
00222 <span class="comment">// break if end reached</span>
00223 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(*options == <a class="code" href="group__dhcp.html#ga32">DHCP_OPT_END</a>)
00224 <span class="keywordflow">break</span>;
00225 <span class="comment">// check for desired option</span>
00226 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(*options == optcode)
00227 {
00228 <span class="comment">// found desired option</span>
00229 <span class="comment">// limit size to actual option length</span>
00230 optlen = MIN(optlen, *(options+1));
00231 <span class="comment">//if(*(options+1) < optlen)</span>
00232 <span class="comment">// optlen = *(options+1);</span>
00233
00234 <span class="comment">// copy contents of option</span>
00235 <span class="keywordflow">for</span>(i=0; i<optlen; i++)
00236 *(((uint8_t*)optvalptr)+i) = *(options+i+2);
00237 <span class="comment">// return length of option</span>
00238 <span class="keywordflow">return</span> *(options+1);
00239 }
00240 <span class="keywordflow">else</span>
00241 {
00242 <span class="comment">// skip to next option</span>
00243 options++;
00244 options+=*options;
00245 options++;
00246 }
00247 }
00248 <span class="comment">// failed to find desired option</span>
00249 <span class="keywordflow">return</span> 0;
00250 }
00251
00252
<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)
00254 {
00255 <span class="comment">// use current options address as write point</span>
00256
00257 <span class="comment">// set optcode</span>
00258 *options++ = optcode;
00259 <span class="comment">// set optlen</span>
00260 *options++ = optlen;
00261 <span class="comment">// copy in argument/data</span>
00262 <span class="keywordflow">while</span>(optlen--)
00263 {
00264 *options++ = *(uint8_t*)optvalptr++;
00265 }
00266 <span class="comment">// write end marker</span>
00267 *options = <a class="code" href="group__dhcp.html#ga32">DHCP_OPT_END</a>;
00268
00269 <span class="comment">// return address of end marker, to be used as a future write point</span>
00270 <span class="keywordflow">return</span> options;
00271 }
00272
00273
00274 <span class="preprocessor">#ifdef DHCP_DEBUG_PRINT</span>
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)
00276 {
00277 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"DHCP Packet:\r\n"</span>);
00278 <span class="comment">// print op</span>
00279 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"Op : "</span>);
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>)
00281 {
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>;
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>;
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>;
00285 }
00286 <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>();
00287 <span class="comment">// print transaction ID</span>
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>();
00289 <span class="comment">// print client IP address</span>
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>();
00291 <span class="comment">// print 'your' IP address</span>
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>();
00293 <span class="comment">// print server IP address</span>
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>();
00295 <span class="comment">// print gateway IP address</span>
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>();
00297 <span class="comment">// print client hardware address</span>
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>();
00299 }
00300 <span class="preprocessor">#endif</span>
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address>
</body>
</html>
|