Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Related Pages  

C:/proj/src/ethernut/nut/eboot/arp.c

00001 /*
00002  * Copyright (C) 2001-2002 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. All advertising materials mentioning features or use of this
00014  *    software must display the following acknowledgement:
00015  *
00016  *    This product includes software developed by egnite Software GmbH
00017  *    and its contributors.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00020  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00022  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00023  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00024  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00025  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00026  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00027  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00028  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00029  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00030  * SUCH DAMAGE.
00031  *
00032  * For additional information see http://www.ethernut.de/
00033  *
00034  * -
00035  * Portions Copyright (c) 1983, 1993 by
00036  *  The Regents of the University of California.  All rights reserved.
00037  *
00038  * Redistribution and use in source and binary forms, with or without
00039  * modification, are permitted provided that the following conditions
00040  * are met:
00041  * 1. Redistributions of source code must retain the above copyright
00042  *    notice, this list of conditions and the following disclaimer.
00043  * 2. Redistributions in binary form must reproduce the above copyright
00044  *    notice, this list of conditions and the following disclaimer in the
00045  *    documentation and/or other materials provided with the distribution.
00046  * 3. All advertising materials mentioning features or use of this software
00047  *    must display the following acknowledgement:
00048  *  This product includes software developed by the University of
00049  *  California, Berkeley and its contributors.
00050  * 4. Neither the name of the University nor the names of its contributors
00051  *    may be used to endorse or promote products derived from this software
00052  *    without specific prior written permission.
00053  *
00054  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00055  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00056  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00057  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00058  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00059  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00060  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00061  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00062  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00063  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00064  * SUCH DAMAGE.
00065  * -
00066  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
00067  *
00068  * Permission to use, copy, modify, and distribute this software for any
00069  * purpose with or without fee is hereby granted, provided that the above
00070  * copyright notice and this permission notice appear in all copies, and that
00071  * the name of Digital Equipment Corporation not be used in advertising or
00072  * publicity pertaining to distribution of the document or software without
00073  * specific, written prior permission.
00074  * 
00075  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
00076  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
00077  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
00078  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00079  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00080  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00081  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00082  * SOFTWARE.
00083  */
00084 
00085 /*
00086  * $Log: arp.c,v $
00087  * Revision 1.1  2002/08/01 17:34:29  harald
00088  * First check in
00089  *
00090  */
00091 
00092 #include <string.h>
00093 
00094 #include "eboot.h"
00095 #include "ip.h"
00096 #include "arp.h"
00097 
00098 /*!
00099  * \addtogroup xgStack
00100  */
00101 /*@{*/
00102 
00103 ARPENTRY ae;
00104 ARPFRAME arpframe;
00105 
00106 /*!
00107  * \brief Request the MAC address of a specified IP address.
00108  *
00109  * \param dip  IP address in network byte order.
00110  * \param dmac Points to the buffer, that will receive the MAC address.
00111  *
00112  * \return 0 on success, -1 otherwise.
00113  */
00114 int ArpRequest(u_long dip, u_char *dmac)
00115 {
00116     ETHERARP *ea = &arpframe.eth_arp;
00117     u_char retry;
00118     int rlen;
00119     u_char i;
00120 
00121     if(dip == INADDR_BROADCAST) {
00122         for(i = 0; i < 6; i++)
00123             dmac[i] = 0xFF;
00124         return 0;
00125     }
00126     if(ae.ae_ip != dip) {
00127 
00128         /*
00129          * Set ARP header.
00130          */
00131         ea->arp_hrd = htons(ARPHRD_ETHER);
00132         ea->arp_pro = ETHERTYPE_IP;
00133         ea->arp_hln = 6;
00134         ea->arp_pln = 4;
00135         ea->arp_op = htons(ARPOP_REQUEST);
00136         for(i = 0; i < 6; i++)
00137             ea->arp_sha[i] = mac[i];
00138         ea->arp_spa = local_ip;
00139         for(i = 0; i < 6; i++)
00140             ea->arp_tha[i] = 0xFF;
00141         ea->arp_tpa = dip;
00142 
00143         for(rlen = retry = 0; rlen == 0 && retry < 10; retry++) {
00144             if(EtherOutput(0, ETHERTYPE_ARP, sizeof(ETHERARP)) < 0)
00145                 break;
00146             if((rlen = EtherInput(ETHERTYPE_ARP, 1000)) > 0) {
00147                 ARPFRAME *af = (ARPFRAME *)&rframe;
00148 
00149                 ea = &af->eth_arp;
00150                 if(ea->arp_tpa == local_ip) {
00151                     if(htons(ea->arp_op) == ARPOP_REPLY) {
00152                         ae.ae_ip = ea->arp_spa;
00153                         for(i = 0; i < 6; i++)
00154                             ae.ae_ha[i] = ea->arp_sha[i];
00155                         break;
00156                     }
00157                 }
00158                 rlen = 0;
00159             }
00160         }
00161     }
00162     if(ae.ae_ip != dip) 
00163         return -1;
00164 
00165     for(i = 0; i < 6; i++)
00166         dmac[i] = ae.ae_ha[i];
00167 
00168     return 0;
00169 }
00170 
00171 /*!
00172  * \brief Process incoming ARP packets.
00173  *
00174  * We do not really respond to ARP requests.
00175  */
00176 void ArpRespond(void)
00177 {
00178     u_char i;
00179     ARPFRAME *af = (ARPFRAME *)&rframe;
00180     ETHERARP *ea = &af->eth_arp;
00181 
00182     ea = &arpframe.eth_arp;
00183     if(ea->arp_tpa == local_ip) {
00184         if(htons(ea->arp_op) == ARPOP_REPLY) {
00185             ae.ae_ip = ea->arp_spa;
00186             for(i = 0; i < 6; i++)
00187                 ae.ae_ha[i] = ea->arp_sha[i];
00188         }
00189     }
00190 }
00191 /*@}*/

© 2002 by egnite Software GmbH - visit http://www.ethernut.de/