]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/blob - src/core/netif.c
fix warning
[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 #include "lwip/tcp.h"
46
47
48 struct netif *netif_list = NULL;
49 struct netif *netif_default = NULL;
50
51 /**
52  * Add a network interface to the list of lwIP netifs.
53  *
54  * @param ipaddr IP address for the new netif
55  * @param netmask network mask for the new netif
56  * @param gw default gateway IP address for the new netif
57  * @param state opaque data passed to the new netif
58  * @param init callback function that initializes the interface
59  * @param input callback function that...
60  *
61  * @return netif, or NULL if failed.
62  */
63 struct netif *
64 netif_add(struct ip_addr *ipaddr, struct ip_addr *netmask,
65   struct ip_addr *gw,
66   void *state,
67   err_t (* init)(struct netif *netif),
68   err_t (* input)(struct pbuf *p, struct netif *netif))
69 {
70   struct netif *netif;
71   static int netifnum = 0;
72
73   /* allocate netif structure */  
74   netif = mem_malloc(sizeof(struct netif));
75
76   if (netif == NULL) {
77     DEBUGF(NETIF_DEBUG, ("netif_add(): out of memory for netif\n"));
78     return NULL;
79   }
80 #if LWIP_DHCP
81   /* netif not under DHCP control by default */
82   netif->dhcp = NULL;
83 #endif  
84   /* remember netif specific state information data */
85   netif->state = state;
86   netif->num = netifnum++;
87   netif->input = input;
88
89   netif_set_addr(netif, ipaddr, netmask, gw);
90   
91   /* call user specified initialization function for netif */
92   if (init(netif) != ERR_OK) {
93     mem_free(netif);
94     return NULL;
95   }
96  
97   /* add this netif to the list */
98   netif->next = netif_list;
99   netif_list = netif;
100 #if NETIF_DEBUG
101   DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
102     netif->name[0], netif->name[1]));
103   ip_addr_debug_print(ipaddr);
104   DEBUGF(NETIF_DEBUG, (" netmask "));
105   ip_addr_debug_print(netmask);
106   DEBUGF(NETIF_DEBUG, (" gw "));  
107   ip_addr_debug_print(gw);
108   DEBUGF(NETIF_DEBUG, ("\n"));
109 #endif /* NETIF_DEBUG */
110   return netif;
111 }
112
113 void
114 netif_set_addr(struct netif *netif,struct ip_addr *ipaddr, struct ip_addr *netmask,
115           struct ip_addr *gw)
116 {
117   netif_set_ipaddr(netif, ipaddr);
118   netif_set_netmask(netif, netmask);
119   netif_set_gw(netif, gw);
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     /*  look for netif further down the list */
132     struct netif * tmpNetif;
133     for (tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) {
134                         if (tmpNetif->next == netif) {
135                                 tmpNetif->next = netif->next;
136         break;
137         }    
138                 }
139                 if (tmpNetif == NULL)
140                         return; /*  we didn't find any netif today */
141   }
142   /* this netif is default? */
143   if (netif_default == netif)
144     /* reset default netif */
145     netif_default = NULL;
146   DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
147   mem_free( netif );
148 }
149
150 struct netif *
151 netif_find(char *name)
152 {
153   struct netif *netif;
154   u8_t num;
155   
156   if (name == NULL) {
157     return NULL;
158   }
159
160   num = name[2] - '0';
161  
162   for(netif = netif_list; netif != NULL; netif = netif->next) {
163     if (num == netif->num &&
164        name[0] == netif->name[0] &&
165        name[1] == netif->name[1]) {
166       DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
167       return netif;
168     }    
169   }
170   DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
171   return NULL;
172 }
173 /*-----------------------------------------------------------------------------------*/
174 void
175 netif_set_ipaddr(struct netif *netif, struct ip_addr *ipaddr)
176 {
177   /* TODO: Handling of obsolete pcbs */
178   /* See:  http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */
179 #if LWIP_TCP 
180   struct tcp_pcb *pcb;
181   struct tcp_pcb_listen *lpcb;
182
183   /* address has changed? */
184   if ((ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0)
185   {
186     extern struct tcp_pcb *tcp_active_pcbs;
187     DEBUGF(NETIF_DEBUG | 1, ("netif_set_ipaddr: netif address changed\n"));
188     pcb = tcp_active_pcbs;
189     while (pcb != NULL) {
190       if (ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))) {
191         /* The PCB is connected using the old ipaddr and must be aborted */
192         struct tcp_pcb *next = pcb->next;
193         DEBUGF(NETIF_DEBUG | 1, ("netif_set_ipaddr: aborting pcb %p\n", (void *)pcb));
194         tcp_abort(pcb);
195         pcb = next;
196       } else {
197         pcb = pcb->next;
198       }
199     }
200     for (lpcb = tcp_listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
201       if (ip_addr_cmp(&(lpcb->local_ip), &(netif->ip_addr))) {
202         /* The PCB is listening to the old ipaddr and
203          * is set to listen to the  new one instead */
204         ip_addr_set(&(lpcb->local_ip), ipaddr);
205       }
206     }
207   }
208 #endif
209   ip_addr_set(&(netif->ip_addr), ipaddr);
210   DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE | 3, ("netif: IP address of interface %c%c set to %u.%u.%u.%u\n",
211                        netif->name[0], netif->name[1], 
212     (u8_t)(ntohl(netif->ip_addr.addr) >> 24 & 0xff),
213     (u8_t)(ntohl(netif->ip_addr.addr) >> 16 & 0xff),
214     (u8_t)(ntohl(netif->ip_addr.addr) >> 8 & 0xff),
215     (u8_t)(ntohl(netif->ip_addr.addr) & 0xff)));
216 }
217 /*-----------------------------------------------------------------------------------*/
218 void
219 netif_set_gw(struct netif *netif, struct ip_addr *gw)
220 {
221   ip_addr_set(&(netif->gw), gw);
222   DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE | 3, ("netif: GW address of interface %c%c set to %u.%u.%u.%u\n",
223                        netif->name[0], netif->name[1], 
224                        (u8_t)(ntohl(netif->gw.addr) >> 24 & 0xff),
225                        (u8_t)(ntohl(netif->gw.addr) >> 16 & 0xff),
226                        (u8_t)(ntohl(netif->gw.addr) >> 8 & 0xff),
227                        (u8_t)(ntohl(netif->gw.addr) & 0xff)));
228 }
229 /*-----------------------------------------------------------------------------------*/
230 void
231 netif_set_netmask(struct netif *netif, struct ip_addr *netmask)
232 {
233   ip_addr_set(&(netif->netmask), netmask);
234   DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE | 3, ("netif: netmask of interface %c%c set to %u.%u.%u.%u\n",
235                        netif->name[0], netif->name[1],
236                        (u8_t)(ntohl(netif->netmask.addr) >> 24 & 0xff),
237                        (u8_t)(ntohl(netif->netmask.addr) >> 16 & 0xff),
238                        (u8_t)(ntohl(netif->netmask.addr) >> 8 & 0xff),
239                        (u8_t)(ntohl(netif->netmask.addr) & 0xff)));
240 }
241 /*-----------------------------------------------------------------------------------*/
242 void
243 netif_set_default(struct netif *netif)
244 {
245   netif_default = netif;
246   DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
247                        netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
248 }
249 /*-----------------------------------------------------------------------------------*/
250 void
251 netif_init(void)
252 {
253   netif_list = netif_default = NULL;
254 }
255 /*-----------------------------------------------------------------------------------*/