4 * lwIP network interface abstraction
8 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
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.
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
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Adam Dunkels <adam@sics.se>
43 #include "lwip/netif.h"
45 struct netif *netif_list = NULL;
46 struct netif *netif_default = NULL;
49 * Add a network interface to the list of lwIP netifs.
51 * @param ipaddr IP address for the new netif
52 * @param netmask network mask for the new netif
53 * @param gw default gateway IP address for the new netif
54 * @param state opaque data passed to the new netif
55 * @param init callback function that initializes the interface
56 * @param input callback function that...
58 * @return netif, or NULL if failed.
61 netif_add(struct ip_addr *ipaddr, struct ip_addr *netmask,
64 err_t (* init)(struct netif *netif),
65 err_t (* input)(struct pbuf *p, struct netif *netif))
68 static int netifnum = 0;
70 /* allocate netif structure */
71 netif = mem_malloc(sizeof(struct netif));
74 DEBUGF(NETIF_DEBUG, ("netif_add(): out of memory for netif\n"));
78 /* netif not under DHCP control by default */
81 /* remember netif specific state information data */
83 netif->num = netifnum++;
86 netif_set_addr(netif, ipaddr, netmask, gw);
88 /* call user specified initialization function for netif */
89 if (init(netif) != ERR_OK) {
94 /* add this netif to the list */
95 netif->next = netif_list;
98 DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
99 netif->name[0], netif->name[1]));
100 ip_addr_debug_print(ipaddr);
101 DEBUGF(NETIF_DEBUG, (" netmask "));
102 ip_addr_debug_print(netmask);
103 DEBUGF(NETIF_DEBUG, (" gw "));
104 ip_addr_debug_print(gw);
105 DEBUGF(NETIF_DEBUG, ("\n"));
106 #endif /* NETIF_DEBUG */
111 netif_set_addr(struct netif *netif,struct ip_addr *ipaddr, struct ip_addr *netmask,
114 netif_set_ipaddr(netif, ipaddr);
115 netif_set_netmask(netif, netmask);
116 netif_set_gw(netif, gw);
119 /*-----------------------------------------------------------------------------------*/
120 void netif_remove(struct netif * netif)
122 if ( netif == NULL ) return;
124 /* is it the first netif? */
125 if(netif_list == netif) {
126 netif_list = netif->next;
130 /* look for netif further down the list */
131 struct netif * tmpNetif;
132 for(tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) {
133 if(tmpNetif->next == netif) {
134 tmpNetif->next = netif->next;
139 return; /* we didn't find any netif today */
141 /* this netif is default? */
142 if (netif_default == netif)
143 /* reset default netif */
144 netif_default = NULL;
146 DEBUGF(NETIF_DEBUG, ("netif_remove: removed netif\n"));
150 /*-----------------------------------------------------------------------------------*/
152 netif_find(char *name)
163 for(netif = netif_list; netif != NULL; netif = netif->next) {
164 if(num == netif->num &&
165 name[0] == netif->name[0] &&
166 name[1] == netif->name[1]) {
167 DEBUGF(NETIF_DEBUG, ("netif_find: found %s\n", name));
171 DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %s\n", name));
174 /*-----------------------------------------------------------------------------------*/
176 netif_set_ipaddr(struct netif *netif, struct ip_addr *ipaddr)
178 ip_addr_set(&(netif->ip_addr), ipaddr);
179 DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE, ("netif: setting IP address of interface %c%c%u to %u.%u.%u.%u\n",
180 netif->name[0], netif->name[1], netif->num,
181 (u8_t)(ntohl(ipaddr->addr) >> 24 & 0xff),
182 (u8_t)(ntohl(ipaddr->addr) >> 16 & 0xff),
183 (u8_t)(ntohl(ipaddr->addr) >> 8 & 0xff),
184 (u8_t)(ntohl(ipaddr->addr) & 0xff)));
186 /*-----------------------------------------------------------------------------------*/
188 netif_set_gw(struct netif *netif, struct ip_addr *gw)
190 ip_addr_set(&(netif->gw), gw);
191 DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE, ("netif: setting GW address of interface %c%c%u to %u.%u.%u.%u\n",
192 netif->name[0], netif->name[1], netif->num,
193 (u8_t)(ntohl(gw->addr) >> 24 & 0xff),
194 (u8_t)(ntohl(gw->addr) >> 16 & 0xff),
195 (u8_t)(ntohl(gw->addr) >> 8 & 0xff),
196 (u8_t)(ntohl(gw->addr) & 0xff)));
198 /*-----------------------------------------------------------------------------------*/
200 netif_set_netmask(struct netif *netif, struct ip_addr *netmask)
202 ip_addr_set(&(netif->netmask), netmask);
203 DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE, ("netif: setting netmask of interface %c%c%u to %u.%u.%u.%u\n",
204 netif->name[0], netif->name[1], netif->num,
205 (u8_t)(ntohl(netmask->addr) >> 24 & 0xff),
206 (u8_t)(ntohl(netmask->addr) >> 16 & 0xff),
207 (u8_t)(ntohl(netmask->addr) >> 8 & 0xff),
208 (u8_t)(ntohl(netmask->addr) & 0xff)));
210 /*-----------------------------------------------------------------------------------*/
212 netif_set_default(struct netif *netif)
214 netif_default = netif;
215 DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
216 netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
218 /*-----------------------------------------------------------------------------------*/
222 netif_list = netif_default = NULL;
224 /*-----------------------------------------------------------------------------------*/