uip/uip-neighbor.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2006, Swedish Institute of Computer Science.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
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. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the uIP TCP/IP stack
00030  *
00031  * $Id: uip-neighbor.c,v 1.2 2006/06/12 08:00:30 adam Exp $
00032  */
00033 
00034 /**
00035  * \file
00036  *         Database of link-local neighbors, used by IPv6 code and
00037  *         to be used by a future ARP code rewrite.
00038  * \author
00039  *         Adam Dunkels <adam@sics.se>
00040  */
00041 
00042 #include "uip-neighbor.h"
00043 
00044 #include <string.h>
00045 
00046 #define MAX_TIME 128
00047 
00048 #ifdef UIP_NEIGHBOR_CONF_ENTRIES
00049 #define ENTRIES UIP_NEIGHBOR_CONF_ENTRIES
00050 #else /* UIP_NEIGHBOR_CONF_ENTRIES */
00051 #define ENTRIES 8
00052 #endif /* UIP_NEIGHBOR_CONF_ENTRIES */
00053 
00054 struct neighbor_entry {
00055   uip_ipaddr_t ipaddr;
00056   struct uip_neighbor_addr addr;
00057   u8_t time;
00058 };
00059 static struct neighbor_entry entries[ENTRIES];
00060 
00061 /*---------------------------------------------------------------------------*/
00062 void
00063 uip_neighbor_init(void)
00064 {
00065   int i;
00066 
00067   for(i = 0; i < ENTRIES; ++i) {
00068     entries[i].time = MAX_TIME;
00069   }
00070 }
00071 /*---------------------------------------------------------------------------*/
00072 void
00073 uip_neighbor_periodic(void)
00074 {
00075   int i;
00076 
00077   for(i = 0; i < ENTRIES; ++i) {
00078     if(entries[i].time < MAX_TIME) {
00079       entries[i].time++;
00080     }
00081   }
00082 }
00083 /*---------------------------------------------------------------------------*/
00084 void
00085 uip_neighbor_add(uip_ipaddr_t ipaddr, struct uip_neighbor_addr *addr)
00086 {
00087   int i, oldest;
00088   u8_t oldest_time;
00089 
00090   printf("Adding neighbor with link address %02x:%02x:%02x:%02x:%02x:%02x\n",
00091          addr->addr.addr[0], addr->addr.addr[1], addr->addr.addr[2], addr->addr.addr[3],
00092          addr->addr.addr[4], addr->addr.addr[5]);
00093   
00094   /* Find the first unused entry or the oldest used entry. */
00095   oldest_time = 0;
00096   oldest = 0;
00097   for(i = 0; i < ENTRIES; ++i) {
00098     if(entries[i].time == MAX_TIME) {
00099       oldest = i;
00100       break;
00101     }
00102     if(uip_ipaddr_cmp(entries[i].ipaddr, addr)) {
00103       oldest = i;
00104       break;
00105     }
00106     if(entries[i].time > oldest_time) {
00107       oldest = i;
00108       oldest_time = entries[i].time;
00109     }
00110   }
00111 
00112   /* Use the oldest or first free entry (either pointed to by the
00113      "oldest" variable). */
00114   entries[oldest].time = 0;
00115   uip_ipaddr_copy(entries[oldest].ipaddr, ipaddr);
00116   memcpy(&entries[oldest].addr, addr, sizeof(struct uip_neighbor_addr));
00117 }
00118 /*---------------------------------------------------------------------------*/
00119 static struct neighbor_entry *
00120 find_entry(uip_ipaddr_t ipaddr)
00121 {
00122   int i;
00123   
00124   for(i = 0; i < ENTRIES; ++i) {
00125     if(uip_ipaddr_cmp(entries[i].ipaddr, ipaddr)) {
00126       return &entries[i];
00127     }
00128   }
00129   return NULL;
00130 }
00131 /*---------------------------------------------------------------------------*/
00132 void
00133 uip_neighbor_update(uip_ipaddr_t ipaddr)
00134 {
00135   struct neighbor_entry *e;
00136 
00137   e = find_entry(ipaddr);
00138   if(e != NULL) {
00139     e->time = 0;
00140   }
00141 }
00142 /*---------------------------------------------------------------------------*/
00143 struct uip_neighbor_addr *
00144 uip_neighbor_lookup(uip_ipaddr_t ipaddr)
00145 {
00146   struct neighbor_entry *e;
00147 
00148   e = find_entry(ipaddr);
00149   if(e != NULL) {
00150     /*    printf("Lookup neighbor with link address %02x:%02x:%02x:%02x:%02x:%02x\n",
00151            e->addr.addr.addr[0], e->addr.addr.addr[1], e->addr.addr.addr[2], e->addr.addr.addr[3],
00152            e->addr.addr.addr[4], e->addr.addr.addr[5]);*/
00153 
00154     return &e->addr;
00155   }
00156   return NULL;
00157 }
00158 /*---------------------------------------------------------------------------*/

Generated on Mon Jun 12 10:23:01 2006 for uIP 1.0 by  doxygen 1.4.6