42 |
#include "lwip/mem.h" |
#include "lwip/mem.h" |
43 |
#include "lwip/netif.h" |
#include "lwip/netif.h" |
44 |
#include "lwip/ip_addr.h" |
#include "lwip/ip_addr.h" |
45 |
|
#include "lwip/tcp.h" |
46 |
|
|
47 |
|
|
48 |
struct netif *netif_list = NULL; |
struct netif *netif_list = NULL; |
62 |
*/ |
*/ |
63 |
struct netif * |
struct netif * |
64 |
netif_add(struct ip_addr *ipaddr, struct ip_addr *netmask, |
netif_add(struct ip_addr *ipaddr, struct ip_addr *netmask, |
65 |
struct ip_addr *gw, |
struct ip_addr *gw, |
66 |
void *state, |
void *state, |
67 |
err_t (* init)(struct netif *netif), |
err_t (* init)(struct netif *netif), |
68 |
err_t (* input)(struct pbuf *p, struct netif *netif)) |
err_t (* input)(struct pbuf *p, struct netif *netif)) |
69 |
{ |
{ |
70 |
struct netif *netif; |
struct netif *netif; |
71 |
static int netifnum = 0; |
static int netifnum = 0; |
90 |
|
|
91 |
/* call user specified initialization function for netif */ |
/* call user specified initialization function for netif */ |
92 |
if (init(netif) != ERR_OK) { |
if (init(netif) != ERR_OK) { |
93 |
mem_free(netif); |
mem_free(netif); |
94 |
return NULL; |
return NULL; |
95 |
} |
} |
96 |
|
|
97 |
/* add this netif to the list */ |
/* add this netif to the list */ |
99 |
netif_list = netif; |
netif_list = netif; |
100 |
#if NETIF_DEBUG |
#if NETIF_DEBUG |
101 |
DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ", |
DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ", |
102 |
netif->name[0], netif->name[1])); |
netif->name[0], netif->name[1])); |
103 |
ip_addr_debug_print(ipaddr); |
ip_addr_debug_print(ipaddr); |
104 |
DEBUGF(NETIF_DEBUG, (" netmask ")); |
DEBUGF(NETIF_DEBUG, (" netmask ")); |
105 |
ip_addr_debug_print(netmask); |
ip_addr_debug_print(netmask); |
119 |
netif_set_gw(netif, gw); |
netif_set_gw(netif, gw); |
120 |
} |
} |
121 |
|
|
|
/*-----------------------------------------------------------------------------------*/ |
|
122 |
void netif_remove(struct netif * netif) |
void netif_remove(struct netif * netif) |
123 |
{ |
{ |
124 |
if ( netif == NULL ) return; |
if ( netif == NULL ) return; |
134 |
for(tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) { |
for(tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) { |
135 |
if (tmpNetif->next == netif) { |
if (tmpNetif->next == netif) { |
136 |
tmpNetif->next = netif->next; |
tmpNetif->next = netif->next; |
137 |
break; |
break; |
138 |
} |
} |
139 |
} |
} |
140 |
if (tmpNetif == NULL) |
if (tmpNetif == NULL) |
141 |
return; /* we didn't find any netif today */ |
return; /* we didn't find any netif today */ |
142 |
} |
} |
143 |
/* this netif is default? */ |
/* this netif is default? */ |
144 |
if (netif_default == netif) |
if (netif_default == netif) |
145 |
/* reset default netif */ |
/* reset default netif */ |
146 |
netif_default = NULL; |
netif_default = NULL; |
147 |
|
DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") ); |
148 |
DEBUGF(NETIF_DEBUG, ("netif_remove: removed netif\n")); |
mem_free( netif ); |
|
mem_free( netif ); |
|
149 |
} |
} |
150 |
|
|
|
/*-----------------------------------------------------------------------------------*/ |
|
151 |
struct netif * |
struct netif * |
152 |
netif_find(char *name) |
netif_find(char *name) |
153 |
{ |
{ |
164 |
if (num == netif->num && |
if (num == netif->num && |
165 |
name[0] == netif->name[0] && |
name[0] == netif->name[0] && |
166 |
name[1] == netif->name[1]) { |
name[1] == netif->name[1]) { |
167 |
DEBUGF(NETIF_DEBUG, ("netif_find: found %s\n", name)); |
DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1])); |
168 |
return netif; |
return netif; |
169 |
} |
} |
170 |
} |
} |
171 |
DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %s\n", name)); |
DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1])); |
172 |
return NULL; |
return NULL; |
173 |
} |
} |
174 |
/*-----------------------------------------------------------------------------------*/ |
/*-----------------------------------------------------------------------------------*/ |
175 |
void |
void |
176 |
netif_set_ipaddr(struct netif *netif, struct ip_addr *ipaddr) |
netif_set_ipaddr(struct netif *netif, struct ip_addr *ipaddr) |
177 |
{ |
{ |
178 |
|
/* TODO: Handling of obsolete pcbs */ |
179 |
|
/* See: http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */ |
180 |
|
#if LWIP_TCP |
181 |
|
struct tcp_pcb *pcb; |
182 |
|
struct tcp_pcb_listen *lpcb; |
183 |
|
|
184 |
|
/* address has changed? */ |
185 |
|
if ((ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0) |
186 |
|
{ |
187 |
|
extern struct tcp_pcb *tcp_active_pcbs; |
188 |
|
DEBUGF(NETIF_DEBUG | 1, ("netif_set_ipaddr: netif address changed\n")); |
189 |
|
pcb = tcp_active_pcbs; |
190 |
|
while (pcb != NULL) { |
191 |
|
if (ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))) { |
192 |
|
/* The PCB is connected using the old ipaddr and must be aborted */ |
193 |
|
struct tcp_pcb *next = pcb->next; |
194 |
|
DEBUGF(NETIF_DEBUG | 1, ("netif_set_ipaddr: aborting pcb %p\n", pcb)); |
195 |
|
tcp_abort(pcb); |
196 |
|
pcb = next; |
197 |
|
} else { |
198 |
|
pcb = pcb->next; |
199 |
|
} |
200 |
|
} |
201 |
|
for (lpcb = tcp_listen_pcbs; lpcb != NULL; lpcb = lpcb->next) { |
202 |
|
if (ip_addr_cmp(&(lpcb->local_ip), &(netif->ip_addr))) { |
203 |
|
/* The PCB is listening to the old ipaddr and |
204 |
|
* is set to listen to the new one instead */ |
205 |
|
ip_addr_set(&(lpcb->local_ip), ipaddr); |
206 |
|
} |
207 |
|
} |
208 |
|
} |
209 |
|
#endif |
210 |
ip_addr_set(&(netif->ip_addr), ipaddr); |
ip_addr_set(&(netif->ip_addr), ipaddr); |
211 |
DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE | 3, ("netif: IP address of interface %c%c set to %u.%u.%u.%u\n", |
DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE | 3, ("netif: IP address of interface %c%c set to %u.%u.%u.%u\n", |
212 |
netif->name[0], netif->name[1], |
netif->name[0], netif->name[1], |