Newer
Older
UbixOS / doc / html / net_8c-source.html
<!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>UbixOS V2: src/sys/lib/net.c Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.7 -->
<div class="tabs">
  <ul>
    <li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
    <li><a href="classes.html"><span>Data&nbsp;Structures</span></a></li>
    <li id="current"><a href="files.html"><span>Files</span></a></li>
    <li><a href="dirs.html"><span>Directories</span></a></li>
    <li>
      <form action="search.php" method="get">
        <table cellspacing="0" cellpadding="0" border="0">
          <tr>
            <td><label>&nbsp;<u>S</u>earch&nbsp;for&nbsp;</label></td>
            <td><input type="text" name="query" value="" size="20" accesskey="s"/></td>
          </tr>
        </table>
      </form>
    </li>
  </ul></div>
<div class="tabs">
  <ul>
    <li><a href="files.html"><span>File&nbsp;List</span></a></li>
    <li><a href="globals.html"><span>Globals</span></a></li>
  </ul></div>
<div class="nav">
<a class="el" href="dir_897b6a2d7bab147dd1db58381aad3984.html">src</a>&nbsp;&raquo&nbsp;<a class="el" href="dir_832905b1f7f5feaf61a306b40c0ac817.html">sys</a>&nbsp;&raquo&nbsp;<a class="el" href="dir_d9dcf62a8e8b4cc91cbf2445d76a799b.html">lib</a></div>
<h1>net.c</h1><a href="net_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*****************************************************************************************</span>
<a name="l00002"></a>00002 <span class="comment"> Copyright (c) 2002 The UbixOS Project</span>
<a name="l00003"></a>00003 <span class="comment"> All rights reserved.</span>
<a name="l00004"></a>00004 <span class="comment"></span>
<a name="l00005"></a>00005 <span class="comment">Redistribution and use in source and binary forms, with or without modification, are</span>
<a name="l00006"></a>00006 <span class="comment">permitted provided that the following conditions are met:</span>
<a name="l00007"></a>00007 <span class="comment"></span>
<a name="l00008"></a>00008 <span class="comment">Redistributions of source code must retain the above copyright notice, this list of</span>
<a name="l00009"></a>00009 <span class="comment">conditions, the following disclaimer and the list of authors.  Redistributions in binary</span>
<a name="l00010"></a>00010 <span class="comment">form must reproduce the above copyright notice, this list of conditions, the following</span>
<a name="l00011"></a>00011 <span class="comment">disclaimer and the list of authors in the documentation and/or other materials provided</span>
<a name="l00012"></a>00012 <span class="comment">with the distribution. Neither the name of the UbixOS Project nor the names of its</span>
<a name="l00013"></a>00013 <span class="comment">contributors may be used to endorse or promote products derived from this software</span>
<a name="l00014"></a>00014 <span class="comment">without specific prior written permission.</span>
<a name="l00015"></a>00015 <span class="comment"></span>
<a name="l00016"></a>00016 <span class="comment">THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY</span>
<a name="l00017"></a>00017 <span class="comment">EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF</span>
<a name="l00018"></a>00018 <span class="comment">MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL</span>
<a name="l00019"></a>00019 <span class="comment">THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,</span>
<a name="l00020"></a>00020 <span class="comment">SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT</span>
<a name="l00021"></a>00021 <span class="comment">OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)</span>
<a name="l00022"></a>00022 <span class="comment">HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR</span>
<a name="l00023"></a>00023 <span class="comment">TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS</span>
<a name="l00024"></a>00024 <span class="comment">SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span>
<a name="l00025"></a>00025 <span class="comment"></span>
<a name="l00026"></a>00026 <span class="comment"> $Log: net_8c-source.html,v $
<a name="l00026"></a>00026 <span class="comment"> Revision 1.7  2006/12/15 17:47:06  reddawg
<a name="l00026"></a>00026 <span class="comment"> Updates
<a name="l00026"></a>00026 <span class="comment"></span>
<a name="l00027"></a>00027 <span class="comment"> Revision 1.1.1.1  2006/06/01 12:46:16  reddawg</span>
<a name="l00028"></a>00028 <span class="comment"> ubix2</span>
<a name="l00029"></a>00029 <span class="comment"></span>
<a name="l00030"></a>00030 <span class="comment"> Revision 1.2  2005/10/12 00:13:37  reddawg</span>
<a name="l00031"></a>00031 <span class="comment"> Removed</span>
<a name="l00032"></a>00032 <span class="comment"></span>
<a name="l00033"></a>00033 <span class="comment"> Revision 1.1.1.1  2005/09/26 17:24:12  reddawg</span>
<a name="l00034"></a>00034 <span class="comment"> no message</span>
<a name="l00035"></a>00035 <span class="comment"></span>
<a name="l00036"></a>00036 <span class="comment"> Revision 1.6  2004/07/21 10:02:09  reddawg</span>
<a name="l00037"></a>00037 <span class="comment"> devfs: renamed functions</span>
<a name="l00038"></a>00038 <span class="comment"> device system: renamed functions</span>
<a name="l00039"></a>00039 <span class="comment"> fdc: fixed a few potential bugs and cleaned up some unused variables</span>
<a name="l00040"></a>00040 <span class="comment"> strol: fixed definition</span>
<a name="l00041"></a>00041 <span class="comment"> endtask: made it print out freepage debug info</span>
<a name="l00042"></a>00042 <span class="comment"> kmalloc: fixed a huge memory leak we had some unhandled descriptor insertion so some descriptors were lost</span>
<a name="l00043"></a>00043 <span class="comment"> ld: fixed a pointer conversion</span>
<a name="l00044"></a>00044 <span class="comment"> file: cleaned up a few unused variables</span>
<a name="l00045"></a>00045 <span class="comment"> sched: broke task deletion</span>
<a name="l00046"></a>00046 <span class="comment"> kprintf: fixed ogPrintf definition</span>
<a name="l00047"></a>00047 <span class="comment"></span>
<a name="l00048"></a>00048 <span class="comment"> Revision 1.5  2004/06/28 23:12:58  reddawg</span>
<a name="l00049"></a>00049 <span class="comment"> file format now container:/path/to/file</span>
<a name="l00050"></a>00050 <span class="comment"></span>
<a name="l00051"></a>00051 <span class="comment"> Revision 1.4  2004/06/17 03:14:59  flameshadow</span>
<a name="l00052"></a>00052 <span class="comment"> chg: added missing #include for kprintf()</span>
<a name="l00053"></a>00053 <span class="comment"></span>
<a name="l00054"></a>00054 <span class="comment"> Revision 1.3  2004/05/20 22:54:02  reddawg</span>
<a name="l00055"></a>00055 <span class="comment"> Cleaned Up Warrnings</span>
<a name="l00056"></a>00056 <span class="comment"></span>
<a name="l00057"></a>00057 <span class="comment"> Revision 1.2  2004/04/30 14:16:04  reddawg</span>
<a name="l00058"></a>00058 <span class="comment"> Fixed all the datatypes to be consistant uInt8,uInt16,uInt32,Int8,Int16,Int32</span>
<a name="l00059"></a>00059 <span class="comment"></span>
<a name="l00060"></a>00060 <span class="comment"> Revision 1.1.1.1  2004/04/15 12:07:10  reddawg</span>
<a name="l00061"></a>00061 <span class="comment"> UbixOS v1.0</span>
<a name="l00062"></a>00062 <span class="comment"></span>
<a name="l00063"></a>00063 <span class="comment"> Revision 1.8  2004/04/13 21:29:52  reddawg</span>
<a name="l00064"></a>00064 <span class="comment"> We now have sockets working. Lots of functionality to be added to continually</span>
<a name="l00065"></a>00065 <span class="comment"> improve on the existing layers now its clean up time to get things in a better</span>
<a name="l00066"></a>00066 <span class="comment"> working order.</span>
<a name="l00067"></a>00067 <span class="comment"></span>
<a name="l00068"></a>00068 <span class="comment"> Revision 1.7  2004/04/13 16:36:33  reddawg</span>
<a name="l00069"></a>00069 <span class="comment"> Changed our copyright, it is all now under a BSD-Style license</span>
<a name="l00070"></a>00070 <span class="comment"></span>
<a name="l00071"></a>00071 <span class="comment"></span>
<a name="l00072"></a>00072 <span class="comment"></span>
<a name="l00073"></a>00073 <span class="comment"> $Id: net_8c-source.html 88 2016-01-12 00:11:29Z reddawg $</span>
<a name="l00074"></a>00074 <span class="comment"></span>
<a name="l00075"></a>00075 <span class="comment">*****************************************************************************************/</span>
<a name="l00076"></a>00076 
<a name="l00077"></a>00077 <span class="preprocessor">#include &lt;<a class="code" href="types_8h.html">ubixos/types.h</a>&gt;</span>
<a name="l00078"></a>00078 <span class="preprocessor">#include &lt;net/sockets.h&gt;</span>
<a name="l00079"></a>00079 <span class="preprocessor">#include &lt;<a class="code" href="string_8h.html">string.h</a>&gt;</span>
<a name="l00080"></a>00080 
<a name="l00081"></a>00081 <span class="preprocessor">#include "<a class="code" href="kprintf_8h.html">lib/kprintf.h</a>"</span>
<a name="l00082"></a>00082 
<a name="l00083"></a>00083 <span class="preprocessor">#ifndef _IN_ADDR_T_DECLARED</span>
<a name="l00084"></a><a class="code" href="net_8c.html#8713edfd5a984843afe38b50d22c50a5">00084</a> <span class="preprocessor"></span><span class="keyword">typedef</span> <a class="code" href="types_8h.html#5847ea0262a5aa61eee48cbe95544a78">uInt32</a>        <a class="code" href="net_8c.html#8713edfd5a984843afe38b50d22c50a5">in_addr_t</a>;
<a name="l00085"></a>00085 <span class="preprocessor">#define _IN_ADDR_T_DECLARED</span>
<a name="l00086"></a>00086 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
<a name="l00087"></a>00087 <span class="preprocessor"></span>
<a name="l00088"></a><a class="code" href="net_8c.html#d5bd59ef607c94932ced92e726cc3a09">00088</a> <a class="code" href="types_8h.html#5847ea0262a5aa61eee48cbe95544a78">uInt32</a> <a class="code" href="net_8c.html#d5bd59ef607c94932ced92e726cc3a09">htonl</a>(<a class="code" href="types_8h.html#5847ea0262a5aa61eee48cbe95544a78">uInt32</a> n) {
<a name="l00089"></a>00089   <a class="code" href="types_8h.html#5847ea0262a5aa61eee48cbe95544a78">uInt32</a> retVal = 0x0;
<a name="l00090"></a>00090   retVal += ((n &amp; 0xff) &lt;&lt; 24); 
<a name="l00091"></a>00091   retVal += ((n &amp; 0xff00) &lt;&lt; 8);
<a name="l00092"></a>00092   retVal += ((n &amp; 0xff0000) &gt;&gt; 8);
<a name="l00093"></a>00093   retVal += ((n &amp; 0xff000000) &gt;&gt; 24);
<a name="l00094"></a>00094   <span class="keywordflow">return</span>(retVal);
<a name="l00095"></a>00095   }
<a name="l00096"></a>00096 
<a name="l00097"></a><a class="code" href="net_8c.html#9eb6276a14294cbd10be630d1136220a">00097</a> <a class="code" href="types_8h.html#5847ea0262a5aa61eee48cbe95544a78">uInt32</a> <a class="code" href="net_8c.html#9eb6276a14294cbd10be630d1136220a">htons</a>(<a class="code" href="types_8h.html#5847ea0262a5aa61eee48cbe95544a78">uInt32</a> n) {
<a name="l00098"></a>00098   <a class="code" href="types_8h.html#5847ea0262a5aa61eee48cbe95544a78">uInt32</a> retVal = 0x0;
<a name="l00099"></a>00099   retVal = (((n &amp; 0xff) &lt;&lt; 8) | ((n &amp; 0xff00) &gt;&gt; 8));
<a name="l00100"></a>00100   <span class="keywordflow">return</span>(retVal);
<a name="l00101"></a>00101   }
<a name="l00102"></a>00102 
<a name="l00103"></a><a class="code" href="net_8c.html#04bffce8d62dbb1e8e4e80a5fcdfed9a">00103</a> <span class="keywordtype">void</span> <a class="code" href="bcopy_8c.html#7d2165a6aaf8577fe615ccf03eba693d">bcopy</a>(<span class="keyword">const</span> <span class="keywordtype">void</span> *src, <span class="keywordtype">void</span> *dest, <span class="keywordtype">int</span> len) {
<a name="l00104"></a>00104   <a class="code" href="lib_2string_8h.html#e33b18edb618c656b9b795b9fce2316c">memcpy</a>(dest,src,len);
<a name="l00105"></a>00105   }
<a name="l00106"></a>00106 
<a name="l00107"></a><a class="code" href="net_8c.html#dd5fdbef65776341fa73044b6d579279">00107</a> <span class="keywordtype">void</span> <a class="code" href="net_8c.html#dd5fdbef65776341fa73044b6d579279">bzero</a>(<span class="keywordtype">void</span> *data, <span class="keywordtype">int</span> n) {
<a name="l00108"></a>00108   <a class="code" href="lib_2string_8h.html#ce4b911463887af5e748326323e99a23">memset</a>(data, 0, n);
<a name="l00109"></a>00109   }
<a name="l00110"></a>00110 
<a name="l00111"></a>00111 
<a name="l00112"></a><a class="code" href="net_8c.html#a9e522808a681d651832da7d16ff1ebb">00112</a> <span class="keywordtype">int</span> <a class="code" href="net_8c.html#a9e522808a681d651832da7d16ff1ebb">inet_aton</a>(cp, addr)
<a name="l00113"></a>00113         const <span class="keywordtype">char</span> *cp;
<a name="l00114"></a>00114         struct in_addr *addr;
<a name="l00115"></a>00115 {
<a name="l00116"></a>00116         <a class="code" href="types_8h.html#5847ea0262a5aa61eee48cbe95544a78">uInt32</a> parts[4];
<a name="l00117"></a>00117         <a class="code" href="net_8c.html#8713edfd5a984843afe38b50d22c50a5">in_addr_t</a> val;
<a name="l00118"></a>00118         <span class="keywordtype">char</span> *c;
<a name="l00119"></a>00119         <span class="keywordtype">char</span> *endptr;
<a name="l00120"></a>00120         <span class="keywordtype">int</span> gotend, n;
<a name="l00121"></a>00121 
<a name="l00122"></a>00122         c = (<span class="keywordtype">char</span> *)cp;
<a name="l00123"></a>00123         n = 0;
<a name="l00124"></a>00124         <span class="comment">/*</span>
<a name="l00125"></a>00125 <span class="comment">         * Run through the string, grabbing numbers until</span>
<a name="l00126"></a>00126 <span class="comment">         * the end of the string, or some error</span>
<a name="l00127"></a>00127 <span class="comment">         */</span>
<a name="l00128"></a>00128         gotend = 0;
<a name="l00129"></a>00129         <span class="keywordflow">while</span> (!gotend) {
<a name="l00130"></a>00130                 <span class="comment">//errno = 0;</span>
<a name="l00131"></a>00131                 val = <a class="code" href="string_8h.html#23a80e470a9dae66d16e7d25fbba122a">strtol</a>(c, &amp;endptr, 0);
<a name="l00132"></a>00132                 <a class="code" href="kprint_8h.html#b2761bdf0cca73ad0fb5880895210cd8">kprintf</a>(<span class="stringliteral">"VAL: [%x]"</span>,val);
<a name="l00133"></a>00133 
<a name="l00134"></a>00134                 <span class="comment">//if (errno == ERANGE)    /* Fail completely if it overflowed. */</span>
<a name="l00135"></a>00135                 <span class="comment">//        return (0);</span>
<a name="l00136"></a>00136 
<a name="l00137"></a>00137                 <span class="comment">/*</span>
<a name="l00138"></a>00138 <span class="comment">                 * If the whole string is invalid, endptr will equal</span>
<a name="l00139"></a>00139 <span class="comment">                 * c.. this way we can make sure someone hasn't</span>
<a name="l00140"></a>00140 <span class="comment">                 * gone '.12' or something which would get past</span>
<a name="l00141"></a>00141 <span class="comment">                 * the next check.</span>
<a name="l00142"></a>00142 <span class="comment">                 */</span>
<a name="l00143"></a>00143                 <span class="keywordflow">if</span> (endptr == c)
<a name="l00144"></a>00144                         <span class="keywordflow">return</span> (0);
<a name="l00145"></a>00145                 parts[n] = val;
<a name="l00146"></a>00146                 c = endptr;
<a name="l00147"></a>00147 
<a name="l00148"></a>00148                 <span class="comment">/* Check the next character past the previous number's end */</span>
<a name="l00149"></a>00149                 <span class="keywordflow">switch</span> (*c) {
<a name="l00150"></a>00150                 <span class="keywordflow">case</span> <span class="charliteral">'.'</span> :
<a name="l00151"></a>00151                         <span class="comment">/* Make sure we only do 3 dots .. */</span>
<a name="l00152"></a>00152                         <span class="keywordflow">if</span> (n == 3)     <span class="comment">/* Whoops. Quit. */</span>
<a name="l00153"></a>00153                                 <span class="keywordflow">return</span> (0);
<a name="l00154"></a>00154                         n++;
<a name="l00155"></a>00155                         c++;
<a name="l00156"></a>00156                         <span class="keywordflow">break</span>;
<a name="l00157"></a>00157 
<a name="l00158"></a>00158                 <span class="keywordflow">case</span> <span class="charliteral">'\0'</span>:
<a name="l00159"></a>00159                         gotend = 1;
<a name="l00160"></a>00160                         <span class="keywordflow">break</span>;
<a name="l00161"></a>00161 
<a name="l00162"></a>00162                 <span class="keywordflow">default</span>:
<a name="l00163"></a>00163                        <span class="comment">/*</span>
<a name="l00164"></a>00164 <span class="comment">                        if (isspace((unsigned char)*c)) {</span>
<a name="l00165"></a>00165 <span class="comment">                                gotend = 1;</span>
<a name="l00166"></a>00166 <span class="comment">                                break;</span>
<a name="l00167"></a>00167 <span class="comment">                        } else</span>
<a name="l00168"></a>00168 <span class="comment">                       */</span>
<a name="l00169"></a>00169                                 <span class="keywordflow">return</span> (0);     <span class="comment">/* Invalid character, so fail */</span>
<a name="l00170"></a>00170                 }
<a name="l00171"></a>00171 
<a name="l00172"></a>00172         }
<a name="l00173"></a>00173 
<a name="l00174"></a>00174         <span class="comment">/*</span>
<a name="l00175"></a>00175 <span class="comment">         * Concoct the address according to</span>
<a name="l00176"></a>00176 <span class="comment">         * the number of parts specified.</span>
<a name="l00177"></a>00177 <span class="comment">         */</span>
<a name="l00178"></a>00178 
<a name="l00179"></a>00179         <span class="keywordflow">switch</span> (n) {
<a name="l00180"></a>00180         <span class="keywordflow">case</span> 0:                         <span class="comment">/* a -- 32 bits */</span>
<a name="l00181"></a>00181                 <span class="comment">/*</span>
<a name="l00182"></a>00182 <span class="comment">                 * Nothing is necessary here.  Overflow checking was</span>
<a name="l00183"></a>00183 <span class="comment">                 * already done in strtoul().</span>
<a name="l00184"></a>00184 <span class="comment">                 */</span>
<a name="l00185"></a>00185                 <span class="keywordflow">break</span>;
<a name="l00186"></a>00186         <span class="keywordflow">case</span> 1:                         <span class="comment">/* a.b -- 8.24 bits */</span>
<a name="l00187"></a>00187                 <span class="keywordflow">if</span> (val &gt; 0xffffff || parts[0] &gt; 0xff)
<a name="l00188"></a>00188                         <span class="keywordflow">return</span> (0);
<a name="l00189"></a>00189                 val |= parts[0] &lt;&lt; 24;
<a name="l00190"></a>00190                 <span class="keywordflow">break</span>;
<a name="l00191"></a>00191 
<a name="l00192"></a>00192         <span class="keywordflow">case</span> 2:                         <span class="comment">/* a.b.c -- 8.8.16 bits */</span>
<a name="l00193"></a>00193                 <span class="keywordflow">if</span> (val &gt; 0xffff || parts[0] &gt; 0xff || parts[1] &gt; 0xff)
<a name="l00194"></a>00194                         <span class="keywordflow">return</span> (0);
<a name="l00195"></a>00195                 val |= (parts[0] &lt;&lt; 24) | (parts[1] &lt;&lt; 16);
<a name="l00196"></a>00196                 <span class="keywordflow">break</span>;
<a name="l00197"></a>00197 
<a name="l00198"></a>00198         <span class="keywordflow">case</span> 3:                         <span class="comment">/* a.b.c.d -- 8.8.8.8 bits */</span>
<a name="l00199"></a>00199                 <span class="keywordflow">if</span> (val &gt; 0xff || parts[0] &gt; 0xff || parts[1] &gt; 0xff ||
<a name="l00200"></a>00200                     parts[2] &gt; 0xff)
<a name="l00201"></a>00201                         <span class="keywordflow">return</span> (0);
<a name="l00202"></a>00202                 val |= (parts[0] &lt;&lt; 24) | (parts[1] &lt;&lt; 16) | (parts[2] &lt;&lt; 8);
<a name="l00203"></a>00203                 <span class="keywordflow">break</span>;
<a name="l00204"></a>00204         }
<a name="l00205"></a>00205 
<a name="l00206"></a>00206         <span class="keywordflow">if</span> (addr != <a class="code" href="types_8h.html#070d2ce7b6bb7e5c05602aa8c308d0c4">NULL</a>)
<a name="l00207"></a>00207                 addr-&gt;s_addr = <a class="code" href="net_8c.html#d5bd59ef607c94932ced92e726cc3a09">htonl</a>(val);
<a name="l00208"></a>00208         <span class="keywordflow">return</span> (1);
<a name="l00209"></a>00209 }
<a name="l00210"></a>00210 
<a name="l00211"></a>00211 <span class="comment">/***</span>
<a name="l00212"></a>00212 <span class="comment"> END</span>
<a name="l00213"></a>00213 <span class="comment"> ***/</span>
<a name="l00214"></a>00214   
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Fri Dec 15 11:18:55 2006 for UbixOS V2 by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.7 </small></address>
</body>
</html>