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

📄 dhcp_8c-source.html

📁 avr应用测试程序
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!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&nbsp;Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Data&nbsp;Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related&nbsp;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">///&lt; 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">///&lt; 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">///&lt; 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*)&amp;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 &gt;= 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-&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#ga11">BOOTP_OP_BOOTREPLY</a>) || (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>))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-&gt;<a class="code" href="structnetDhcpHeader.html#o2">options</a>, <a class="code" href="group__dhcp.html#ga27">DHCP_OPT_DHCPMSGTYPE</a>, 1, &amp;msgtype);00062 <span class="preprocessor">    #if NET_DEBUG &gt;= 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-&gt;<a class="code" href="structnetDhcpHeader.html#o2">options</a>, <a class="code" href="group__dhcp.html#ga28">DHCP_OPT_SERVERID</a>, 4, &amp;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-&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>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-&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);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, &amp;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, &amp;packet-&gt;<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*)&amp;val)[0] = <a class="code" href="group__dhcp.html#ga18">DHCP_OPT_NETMASK</a>;00085         ((uint8_t*)&amp;val)[1] = <a class="code" href="group__dhcp.html#ga19">DHCP_OPT_ROUTERS</a>;00086         ((uint8_t*)&amp;val)[2] = <a class="code" href="group__dhcp.html#ga22">DHCP_OPT_DNSSERVERS</a>;00087         ((uint8_t*)&amp;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, &amp;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 

⌨️ 快捷键说明

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