uIP 1.0 based webserver for LPC1114 + ENC28J60

Dependencies:   mbed TMP102

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers uip-neighbor.c Source File

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 Contiki operating system.
00030  *
00031  */
00032 
00033 /**
00034  * \file
00035  *         Database of link-local neighbors, used by IPv6 code and
00036  *         to be used by a future ARP code rewrite.
00037  * \author
00038  *         Adam Dunkels <adam@sics.se>
00039  */
00040 
00041 #include "uip-neighbor.h"
00042 
00043 #include <string.h>
00044 
00045 #define MAX_TIME 128
00046 
00047 #ifdef UIP_NEIGHBOR_CONF_ENTRIES
00048 #define ENTRIES UIP_NEIGHBOR_CONF_ENTRIES
00049 #else /* UIP_NEIGHBOR_CONF_ENTRIES */
00050 #define ENTRIES 8
00051 #endif /* UIP_NEIGHBOR_CONF_ENTRIES */
00052 
00053 struct neighbor_entry {
00054   uip_ipaddr_t ipaddr;
00055   struct uip_neighbor_addr addr;
00056   uint8_t time;
00057 };
00058 static struct neighbor_entry entries[ENTRIES];
00059 
00060 /*---------------------------------------------------------------------------*/
00061 void
00062 uip_neighbor_init(void)
00063 {
00064   int i;
00065 
00066   for(i = 0; i < ENTRIES; ++i) {
00067     entries[i].time = MAX_TIME;
00068   }
00069 }
00070 /*---------------------------------------------------------------------------*/
00071 void
00072 uip_neighbor_periodic(void)
00073 {
00074   int i;
00075 
00076   for(i = 0; i < ENTRIES; ++i) {
00077     if(entries[i].time < MAX_TIME) {
00078       entries[i].time++;
00079     }
00080   }
00081 }
00082 /*---------------------------------------------------------------------------*/
00083 void
00084 uip_neighbor_add(uip_ipaddr_t *ipaddr, struct uip_neighbor_addr *addr)
00085 {
00086   int i, oldest;
00087   uint8_t oldest_time;
00088 
00089   /*  printf("Adding neighbor with link address %02x:%02x:%02x:%02x:%02x:%02x\n",
00090      addr->addr.addr[0], addr->addr.addr[1], addr->addr.addr[2], addr->addr.addr[3],
00091      addr->addr.addr[4], addr->addr.addr[5]);*/
00092   
00093   /* Find the first unused entry or the oldest used entry. */
00094   oldest_time = 0;
00095   oldest = 0;
00096   for(i = 0; i < ENTRIES; ++i) {
00097     if(entries[i].time == MAX_TIME) {
00098       oldest = i;
00099       break;
00100     }
00101     if(uip_ipaddr_cmp(&entries[i].ipaddr, ipaddr)) {
00102       oldest = i;
00103       break;
00104     }
00105     if(entries[i].time > oldest_time) {
00106       oldest = i;
00107       oldest_time = entries[i].time;
00108     }
00109   }
00110 
00111   /* Use the oldest or first free entry (either pointed to by the
00112      "oldest" variable). */
00113   entries[oldest].time = 0;
00114   uip_ipaddr_copy(&entries[oldest].ipaddr, ipaddr);
00115   memcpy(&entries[oldest].addr, addr, sizeof(struct uip_neighbor_addr));
00116 }
00117 /*---------------------------------------------------------------------------*/
00118 static struct neighbor_entry *
00119 find_entry(uip_ipaddr_t *ipaddr)
00120 {
00121   int i;
00122   
00123   for(i = 0; i < ENTRIES; ++i) {
00124     if(uip_ipaddr_cmp(&entries[i].ipaddr, ipaddr)) {
00125       return &entries[i];
00126     }
00127   }
00128   return NULL;
00129 }
00130 /*---------------------------------------------------------------------------*/
00131 void
00132 uip_neighbor_update(uip_ipaddr_t *ipaddr)
00133 {
00134   struct neighbor_entry *e;
00135 
00136   e = find_entry(ipaddr);
00137   if(e != NULL) {
00138     e->time = 0;
00139   }
00140 }
00141 /*---------------------------------------------------------------------------*/
00142 struct uip_neighbor_addr *
00143 uip_neighbor_lookup(uip_ipaddr_t *ipaddr)
00144 {
00145   struct neighbor_entry *e;
00146 
00147   e = find_entry(ipaddr);
00148   if(e != NULL) {
00149     /*    printf("Lookup neighbor with link address %02x:%02x:%02x:%02x:%02x:%02x\n",
00150        e->addr.addr.addr[0], e->addr.addr.addr[1], e->addr.addr.addr[2], e->addr.addr.addr[3],
00151        e->addr.addr.addr[4], e->addr.addr.addr[5]);*/
00152 
00153     return &e->addr;
00154   }
00155   return NULL;
00156 }
00157 /*---------------------------------------------------------------------------*/