Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

netif.c

Go to the documentation of this file.
00001 
00007 /*
00008  * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
00009  * All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without modification, 
00012  * are permitted provided that the following conditions are met:
00013  *
00014  * 1. Redistributions of source code must retain the above copyright notice,
00015  *    this list of conditions and the following disclaimer.
00016  * 2. Redistributions in binary form must reproduce the above copyright notice,
00017  *    this list of conditions and the following disclaimer in the documentation
00018  *    and/or other materials provided with the distribution.
00019  * 3. The name of the author may not be used to endorse or promote products
00020  *    derived from this software without specific prior written permission. 
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00023  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00024  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
00025  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00026  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
00027  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
00030  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
00031  * OF SUCH DAMAGE.
00032  *
00033  * This file is part of the lwIP TCP/IP stack.
00034  * 
00035  * Author: Adam Dunkels <adam@sics.se>
00036  *
00037  * CHANGELOG: this file has been modified by Sergio Perez Alcañiz <serpeal@disca.upv.es> 
00038  *            Departamento de Informática de Sistemas y Computadores          
00039  *            Universidad Politécnica de Valencia                             
00040  *            Valencia (Spain)    
00041  *            Date: April 2003                                          
00042  *  
00043  */
00044 
00045 #include "lwip/opt.h"
00046 
00047 #include "lwip/def.h"
00048 #include "lwip/mem.h"
00049 #include "lwip/netif.h"
00050 #include "lwip/ip_addr.h"
00051 
00052 
00053 struct netif *netif_list = NULL;
00054 struct netif *netif_default = NULL;
00055 
00068 struct netif *
00069 netif_add(struct ip_addr *ipaddr, struct ip_addr *netmask,
00070           struct ip_addr *gw,
00071           void *state,
00072           err_t (* init)(struct netif *netif),
00073           err_t (* input)(struct pbuf *p, struct netif *netif))
00074 {
00075   struct netif *netif;
00076   static int netifnum = 0;
00077 
00078   /* allocate netif structure */  
00079   netif = mem_malloc(sizeof(struct netif));
00080 
00081   if(netif == NULL) {
00082     DEBUGF(NETIF_DEBUG, ("netif_add(): out of memory for netif\n"));
00083     return NULL;
00084   }
00085 #if LWIP_DHCP
00086   /* netif not under DHCP control by default */
00087   netif->dhcp = NULL;
00088 #endif  
00089   /* remember netif specific state information data */
00090   netif->state = state;
00091   netif->num = netifnum++;
00092   netif->input = input;
00093 
00094   netif_set_addr(netif, ipaddr, netmask, gw);
00095   
00096   /* call user specified initialization function for netif */
00097   if (init(netif) != ERR_OK) {
00098       mem_free(netif);
00099       return NULL;
00100   }
00101  
00102   /* add this netif to the list */
00103   netif->next = netif_list;
00104   netif_list = netif;
00105 #if NETIF_DEBUG
00106   DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
00107                        netif->name[0], netif->name[1]));
00108   ip_addr_debug_print(ipaddr);
00109   DEBUGF(NETIF_DEBUG, (" netmask "));
00110   ip_addr_debug_print(netmask);
00111   DEBUGF(NETIF_DEBUG, (" gw "));  
00112   ip_addr_debug_print(gw);
00113   DEBUGF(NETIF_DEBUG, ("\n"));
00114 #endif /* NETIF_DEBUG */
00115   return netif;
00116 }
00117 
00118 void
00119 netif_set_addr(struct netif *netif,struct ip_addr *ipaddr, struct ip_addr *netmask,
00120           struct ip_addr *gw)
00121 {
00122   netif_set_ipaddr(netif, ipaddr);
00123   netif_set_netmask(netif, netmask);
00124   netif_set_gw(netif, gw);
00125 }
00126 
00127 /*-----------------------------------------------------------------------------------*/
00128 void netif_remove(struct netif * netif)
00129 {
00130         if ( netif == NULL ) return;  
00131  
00132         /*  is it the first netif? */
00133         if(netif_list == netif) {
00134                 netif_list = netif->next;
00135         }    
00136         else
00137         {       
00138                 /*  look for netif further down the list */
00139                 struct netif * tmpNetif;
00140                 for(tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) {
00141                         if(tmpNetif->next == netif) {
00142                                 tmpNetif->next = netif->next;
00143                                 break;
00144                         }    
00145                 }
00146                 if(tmpNetif == NULL)
00147                         return; /*  we didn't find any netif today */
00148         }
00149   /* this netif is default? */
00150         if (netif_default == netif)
00151     /* reset default netif */
00152                 netif_default = NULL;
00153 
00154         DEBUGF(NETIF_DEBUG, ("netif_remove: removed netif\n"));
00155         mem_free( netif );
00156 }
00157 
00158 /*-----------------------------------------------------------------------------------*/
00159 struct netif *
00160 netif_find(char *name)
00161 {
00162   struct netif *netif;
00163   u8_t num;
00164   
00165   if(name == NULL) {
00166     return NULL;
00167   }
00168 
00169   num = name[2] - '0';
00170  
00171   for(netif = netif_list; netif != NULL; netif = netif->next) {
00172     if(num == netif->num &&
00173        name[0] == netif->name[0] &&
00174        name[1] == netif->name[1]) {
00175       DEBUGF(NETIF_DEBUG, ("netif_find: found %s\n", name));
00176       return netif;
00177     }    
00178   }
00179   DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %s\n", name));
00180   return NULL;
00181 }
00182 /*-----------------------------------------------------------------------------------*/
00183 void
00184 netif_set_ipaddr(struct netif *netif, struct ip_addr *ipaddr)
00185 {
00186   DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE, ("netif: setting IP address of interface %c%c%u to %u.%u.%u.%u\n",
00187                        netif->name[0], netif->name[1], netif->num,
00188                        (u8_t)(ntohl(ipaddr->addr) >> 24 & 0xff),
00189                        (u8_t)(ntohl(ipaddr->addr) >> 16 & 0xff),
00190                        (u8_t)(ntohl(ipaddr->addr) >> 8 & 0xff),
00191                        (u8_t)(ntohl(ipaddr->addr) & 0xff)));
00192   ip_addr_set(&(netif->ip_addr), ipaddr);
00193 }
00194 /*-----------------------------------------------------------------------------------*/
00195 void
00196 netif_set_gw(struct netif *netif, struct ip_addr *gw)
00197 {
00198   DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE, ("netif: setting GW address of interface %c%c%u to %u.%u.%u.%u\n",
00199                        netif->name[0], netif->name[1], netif->num,
00200                        (u8_t)(ntohl(gw->addr) >> 24 & 0xff),
00201                        (u8_t)(ntohl(gw->addr) >> 16 & 0xff),
00202                        (u8_t)(ntohl(gw->addr) >> 8 & 0xff),
00203                        (u8_t)(ntohl(gw->addr) & 0xff)));
00204   ip_addr_set(&(netif->gw), gw);
00205 }
00206 /*-----------------------------------------------------------------------------------*/
00207 void
00208 netif_set_netmask(struct netif *netif, struct ip_addr *netmask)
00209 {
00210   DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE, ("netif: setting netmask of interface %c%c%u to %u.%u.%u.%u\n",
00211                        netif->name[0], netif->name[1], netif->num,
00212                        (u8_t)(ntohl(netmask->addr) >> 24 & 0xff),
00213                        (u8_t)(ntohl(netmask->addr) >> 16 & 0xff),
00214                        (u8_t)(ntohl(netmask->addr) >> 8 & 0xff),
00215                        (u8_t)(ntohl(netmask->addr) & 0xff)));
00216   ip_addr_set(&(netif->netmask), netmask);
00217 }
00218 /*-----------------------------------------------------------------------------------*/
00219 void
00220 netif_set_default(struct netif *netif)
00221 {
00222   netif_default = netif;
00223   DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
00224                        netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
00225 }
00226 /*-----------------------------------------------------------------------------------*/
00227 void
00228 netif_init(void)
00229 {
00230   netif_list = netif_default = NULL;
00231 }
00232 /*-----------------------------------------------------------------------------------*/

Generated on Wed Jan 14 12:58:56 2004 for RTL-lwIP-0.4 by doxygen 1.3.4