]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/blob - src/core/netif.c
Print debug message before actually changing netif IP address config.
[pes-rpp/rpp-lwip.git] / src / core / netif.c
1 /**
2  * @file
3  *
4  * lwIP network interface abstraction
5  */
6
7 /*
8  * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
9  * All rights reserved. 
10  * 
11  * Redistribution and use in source and binary forms, with or without modification, 
12  * are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  *    this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  *    this list of conditions and the following disclaimer in the documentation
18  *    and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission. 
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
25  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
27  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
30  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
31  * OF SUCH DAMAGE.
32  *
33  * This file is part of the lwIP TCP/IP stack.
34  * 
35  * Author: Adam Dunkels <adam@sics.se>
36  *
37  */
38
39 #include "lwip/opt.h"
40
41 #include "lwip/def.h"
42 #include "lwip/mem.h"
43 #include "lwip/netif.h"
44 #include "lwip/ip_addr.h"
45
46
47 struct netif *netif_list = NULL;
48 struct netif *netif_default = NULL;
49
50 /**
51  * Add a network interface to the list of lwIP netifs.
52  *
53  * @param ipaddr IP address for the new netif
54  * @param netmask network mask for the new netif
55  * @param gw default gateway IP address for the new netif
56  * @param state opaque data passed to the new netif
57  * @param init callback function that initializes the interface
58  * @param input callback function that...
59  *
60  * @return netif, or NULL if failed.
61  */
62 struct netif *
63 netif_add(struct ip_addr *ipaddr, struct ip_addr *netmask,
64           struct ip_addr *gw,
65           void *state,
66           err_t (* init)(struct netif *netif),
67           err_t (* input)(struct pbuf *p, struct netif *netif))
68 {
69   struct netif *netif;
70   static int netifnum = 0;
71
72   /* allocate netif structure */  
73   netif = mem_malloc(sizeof(struct netif));
74
75   if(netif == NULL) {
76     DEBUGF(NETIF_DEBUG, ("netif_add(): out of memory for netif\n"));
77     return NULL;
78   }
79 #if LWIP_DHCP
80   /* netif not under DHCP control by default */
81   netif->dhcp = NULL;
82 #endif  
83   /* remember netif specific state information data */
84   netif->state = state;
85   netif->num = netifnum++;
86   netif->input = input;
87
88   netif_set_addr(netif, ipaddr, netmask, gw);
89   
90   /* call user specified initialization function for netif */
91   if (init(netif) != ERR_OK) {
92       mem_free(netif);
93       return NULL;
94   }
95  
96   /* add this netif to the list */
97   netif->next = netif_list;
98   netif_list = netif;
99 #if NETIF_DEBUG
100   DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
101                        netif->name[0], netif->name[1]));
102   ip_addr_debug_print(ipaddr);
103   DEBUGF(NETIF_DEBUG, (" netmask "));
104   ip_addr_debug_print(netmask);
105   DEBUGF(NETIF_DEBUG, (" gw "));  
106   ip_addr_debug_print(gw);
107   DEBUGF(NETIF_DEBUG, ("\n"));
108 #endif /* NETIF_DEBUG */
109   return netif;
110 }
111
112 void
113 netif_set_addr(struct netif *netif,struct ip_addr *ipaddr, struct ip_addr *netmask,
114           struct ip_addr *gw)
115 {
116   netif_set_ipaddr(netif, ipaddr);
117   netif_set_netmask(netif, netmask);
118   netif_set_gw(netif, gw);
119 }
120
121 /*-----------------------------------------------------------------------------------*/
122 void netif_remove(struct netif * netif)
123 {
124         if ( netif == NULL ) return;  
125  
126         /*  is it the first netif? */
127         if(netif_list == netif) {
128                 netif_list = netif->next;
129         }    
130         else
131         {       
132                 /*  look for netif further down the list */
133                 struct netif * tmpNetif;
134                 for(tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) {
135                         if(tmpNetif->next == netif) {
136                                 tmpNetif->next = netif->next;
137                                 break;
138                         }    
139                 }
140                 if(tmpNetif == NULL)
141                         return; /*  we didn't find any netif today */
142         }
143   /* this netif is default? */
144         if (netif_default == netif)
145     /* reset default netif */
146                 netif_default = NULL;
147
148         DEBUGF(NETIF_DEBUG, ("netif_remove: removed netif\n"));
149         mem_free( netif );
150 }
151
152 /*-----------------------------------------------------------------------------------*/
153 struct netif *
154 netif_find(char *name)
155 {
156   struct netif *netif;
157   u8_t num;
158   
159   if(name == NULL) {
160     return NULL;
161   }
162
163   num = name[2] - '0';
164  
165   for(netif = netif_list; netif != NULL; netif = netif->next) {
166     if(num == netif->num &&
167        name[0] == netif->name[0] &&
168        name[1] == netif->name[1]) {
169       DEBUGF(NETIF_DEBUG, ("netif_find: found %s\n", name));
170       return netif;
171     }    
172   }
173   DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %s\n", name));
174   return NULL;
175 }
176 /*-----------------------------------------------------------------------------------*/
177 void
178 netif_set_ipaddr(struct netif *netif, struct ip_addr *ipaddr)
179 {
180   DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE, ("netif: setting IP address of interface %c%c%u to %u.%u.%u.%u\n",
181                        netif->name[0], netif->name[1], netif->num,
182                        (u8_t)(ntohl(ipaddr->addr) >> 24 & 0xff),
183                        (u8_t)(ntohl(ipaddr->addr) >> 16 & 0xff),
184                        (u8_t)(ntohl(ipaddr->addr) >> 8 & 0xff),
185                        (u8_t)(ntohl(ipaddr->addr) & 0xff)));
186   ip_addr_set(&(netif->ip_addr), ipaddr);
187 }
188 /*-----------------------------------------------------------------------------------*/
189 void
190 netif_set_gw(struct netif *netif, struct ip_addr *gw)
191 {
192   DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE, ("netif: setting GW address of interface %c%c%u to %u.%u.%u.%u\n",
193                        netif->name[0], netif->name[1], netif->num,
194                        (u8_t)(ntohl(gw->addr) >> 24 & 0xff),
195                        (u8_t)(ntohl(gw->addr) >> 16 & 0xff),
196                        (u8_t)(ntohl(gw->addr) >> 8 & 0xff),
197                        (u8_t)(ntohl(gw->addr) & 0xff)));
198   ip_addr_set(&(netif->gw), gw);
199 }
200 /*-----------------------------------------------------------------------------------*/
201 void
202 netif_set_netmask(struct netif *netif, struct ip_addr *netmask)
203 {
204   DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE, ("netif: setting netmask of interface %c%c%u to %u.%u.%u.%u\n",
205                        netif->name[0], netif->name[1], netif->num,
206                        (u8_t)(ntohl(netmask->addr) >> 24 & 0xff),
207                        (u8_t)(ntohl(netmask->addr) >> 16 & 0xff),
208                        (u8_t)(ntohl(netmask->addr) >> 8 & 0xff),
209                        (u8_t)(ntohl(netmask->addr) & 0xff)));
210   ip_addr_set(&(netif->netmask), netmask);
211 }
212 /*-----------------------------------------------------------------------------------*/
213 void
214 netif_set_default(struct netif *netif)
215 {
216   netif_default = netif;
217   DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
218                        netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
219 }
220 /*-----------------------------------------------------------------------------------*/
221 void
222 netif_init(void)
223 {
224   netif_list = netif_default = NULL;
225 }
226 /*-----------------------------------------------------------------------------------*/