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