2 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
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.
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
27 * This file is part of the lwIP TCP/IP stack.
29 * Author: Adam Dunkels <adam@sics.se>
37 #include "lwip/netif.h"
39 struct netif *netif_list = NULL;
40 struct netif *netif_default = NULL;
43 * Add a network interface to the list of lwIP netifs.
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...
52 * @return netif, or NULL if failed.
55 netif_add(struct ip_addr *ipaddr, struct ip_addr *netmask,
58 err_t (* init)(struct netif *netif),
59 err_t (* input)(struct pbuf *p, struct netif *netif))
62 static int netifnum = 0;
64 netif = mem_malloc(sizeof(struct netif));
71 netif->num = netifnum++;
74 netif_set_addr(netif, ipaddr, netmask, gw);
76 if (init(netif) != ERR_OK) {
81 netif->next = netif_list;
84 DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
85 netif->name[0], netif->name[1]));
86 ip_addr_debug_print(ipaddr);
87 DEBUGF(NETIF_DEBUG, (" netmask "));
88 ip_addr_debug_print(netmask);
89 DEBUGF(NETIF_DEBUG, (" gw "));
90 ip_addr_debug_print(gw);
91 DEBUGF(NETIF_DEBUG, ("\n"));
92 #endif /* NETIF_DEBUG */
97 netif_set_addr(struct netif *netif,struct ip_addr *ipaddr, struct ip_addr *netmask,
100 netif_set_ipaddr(netif, ipaddr);
101 netif_set_netmask(netif, netmask);
102 netif_set_gw(netif, gw);
105 /*-----------------------------------------------------------------------------------*/
106 void netif_remove(struct netif * netif)
108 if ( netif == NULL ) return;
110 /* is it the first netif? */
111 if(netif_list == netif) {
112 netif_list = netif->next;
116 /* look for netif further down the list */
117 struct netif * tmpNetif;
118 for(tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) {
119 if(tmpNetif->next == netif) {
120 tmpNetif->next = netif->next;
125 return; /* we didn't find any netif today */
128 if(netif_default == netif)
129 netif_default = NULL;
131 DEBUGF(NETIF_DEBUG, ("netif_remove: removed netif"));
135 /*-----------------------------------------------------------------------------------*/
137 netif_find(char *name)
148 for(netif = netif_list; netif != NULL; netif = netif->next) {
149 if(num == netif->num &&
150 name[0] == netif->name[0] &&
151 name[1] == netif->name[1]) {
152 DEBUGF(NETIF_DEBUG, ("netif_find: found %s\n", name));
156 DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %s\n", name));
159 /*-----------------------------------------------------------------------------------*/
161 netif_set_ipaddr(struct netif *netif, struct ip_addr *ipaddr)
163 ip_addr_set(&(netif->ip_addr), ipaddr);
164 DEBUGF(NETIF_DEBUG, ("netif: setting IP address of interface %c%c to %d.%d.%d.%d\n",
165 netif->name[0], netif->name[1],
166 (u8_t)(ntohl(ipaddr->addr) >> 24 & 0xff),
167 (u8_t)(ntohl(ipaddr->addr) >> 16 & 0xff),
168 (u8_t)(ntohl(ipaddr->addr) >> 8 & 0xff),
169 (u8_t)(ntohl(ipaddr->addr) & 0xff)));
171 /*-----------------------------------------------------------------------------------*/
173 netif_set_gw(struct netif *netif, struct ip_addr *gw)
175 ip_addr_set(&(netif->gw), gw);
177 /*-----------------------------------------------------------------------------------*/
179 netif_set_netmask(struct netif *netif, struct ip_addr *netmask)
181 ip_addr_set(&(netif->netmask), netmask);
183 /*-----------------------------------------------------------------------------------*/
185 netif_set_default(struct netif *netif)
187 netif_default = netif;
188 DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
189 netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
191 /*-----------------------------------------------------------------------------------*/
195 netif_list = netif_default = NULL;
197 /*-----------------------------------------------------------------------------------*/