⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dhcp_8c-source.html

📁 avr应用测试程序
💻 HTML
📖 第 1 页 / 共 3 页
字号:
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-&gt;<a class="code" href="structnetDhcpHeader.html#o2">options</a>, <a class="code" href="group__dhcp.html#ga18">DHCP_OPT_NETMASK</a>, 4, &amp;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-&gt;<a class="code" href="structnetDhcpHeader.html#o2">options</a>, <a class="code" href="group__dhcp.html#ga19">DHCP_OPT_ROUTERS</a>, 4, &amp;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-&gt;<a class="code" href="structnetDhcpHeader.html#o2">options</a>, <a class="code" href="group__dhcp.html#ga26">DHCP_OPT_LEASETIME</a>, 4, &amp;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-&gt;<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-&gt;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>*)&amp;<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-&gt;<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-&gt;<a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o1">htype</a> = BOOTP_HTYPE_ETHERNET;00133     packet-&gt;<a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o2">hlen</a> = BOOTP_HLEN_ETHERNET;00134     packet-&gt;<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>()-&gt;ip);00135     packet-&gt;<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-&gt;<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-&gt;<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(&amp;packet-&gt;<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-&gt;<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-&gt;<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-&gt;<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-&gt;<a class="code" href="structnetDhcpHeader.html#o2">options</a>, <a class="code" href="group__dhcp.html#ga27">DHCP_OPT_DHCPMSGTYPE</a>, 1, &amp;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>*)&amp;<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-&gt;<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-&gt;<a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o1">htype</a> = BOOTP_HTYPE_ETHERNET;00169     packet-&gt;<a class="code" href="structnetDhcpHeader.html#o0">bootp</a>.<a class="code" href="structnetBootpHeader.html#o2">hlen</a> = BOOTP_HLEN_ETHERNET;00170     packet-&gt;<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>()-&gt;ip);00171     packet-&gt;<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-&gt;<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-&gt;<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(&amp;packet-&gt;<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-&gt;<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-&gt;<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-&gt;<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-&gt;<a class="code" href="structnetDhcpHeader.html#o2">options</a>, <a class="code" href="group__dhcp.html#ga27">DHCP_OPT_DHCPMSGTYPE</a>, 1, &amp;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, &amp;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, &amp;packet-&gt;<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 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -