00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #include "lwip/opt.h"
00046
00047 #include "lwip/def.h"
00048 #include "lwip/mem.h"
00049 #include "lwip/netif.h"
00050 #include "lwip/ip_addr.h"
00051
00052
00053 struct netif *netif_list = NULL;
00054 struct netif *netif_default = NULL;
00055
00068 struct netif *
00069 netif_add(struct ip_addr *ipaddr, struct ip_addr *netmask,
00070 struct ip_addr *gw,
00071 void *state,
00072 err_t (* init)(struct netif *netif),
00073 err_t (* input)(struct pbuf *p, struct netif *netif))
00074 {
00075 struct netif *netif;
00076 static int netifnum = 0;
00077
00078
00079 netif = mem_malloc(sizeof(struct netif));
00080
00081 if(netif == NULL) {
00082 DEBUGF(NETIF_DEBUG, ("netif_add(): out of memory for netif\n"));
00083 return NULL;
00084 }
00085 #if LWIP_DHCP
00086
00087 netif->dhcp = NULL;
00088 #endif
00089
00090 netif->state = state;
00091 netif->num = netifnum++;
00092 netif->input = input;
00093
00094 netif_set_addr(netif, ipaddr, netmask, gw);
00095
00096
00097 if (init(netif) != ERR_OK) {
00098 mem_free(netif);
00099 return NULL;
00100 }
00101
00102
00103 netif->next = netif_list;
00104 netif_list = netif;
00105 #if NETIF_DEBUG
00106 DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
00107 netif->name[0], netif->name[1]));
00108 ip_addr_debug_print(ipaddr);
00109 DEBUGF(NETIF_DEBUG, (" netmask "));
00110 ip_addr_debug_print(netmask);
00111 DEBUGF(NETIF_DEBUG, (" gw "));
00112 ip_addr_debug_print(gw);
00113 DEBUGF(NETIF_DEBUG, ("\n"));
00114 #endif
00115 return netif;
00116 }
00117
00118 void
00119 netif_set_addr(struct netif *netif,struct ip_addr *ipaddr, struct ip_addr *netmask,
00120 struct ip_addr *gw)
00121 {
00122 netif_set_ipaddr(netif, ipaddr);
00123 netif_set_netmask(netif, netmask);
00124 netif_set_gw(netif, gw);
00125 }
00126
00127
00128 void netif_remove(struct netif * netif)
00129 {
00130 if ( netif == NULL ) return;
00131
00132
00133 if(netif_list == netif) {
00134 netif_list = netif->next;
00135 }
00136 else
00137 {
00138
00139 struct netif * tmpNetif;
00140 for(tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) {
00141 if(tmpNetif->next == netif) {
00142 tmpNetif->next = netif->next;
00143 break;
00144 }
00145 }
00146 if(tmpNetif == NULL)
00147 return;
00148 }
00149
00150 if (netif_default == netif)
00151
00152 netif_default = NULL;
00153
00154 DEBUGF(NETIF_DEBUG, ("netif_remove: removed netif\n"));
00155 mem_free( netif );
00156 }
00157
00158
00159 struct netif *
00160 netif_find(char *name)
00161 {
00162 struct netif *netif;
00163 u8_t num;
00164
00165 if(name == NULL) {
00166 return NULL;
00167 }
00168
00169 num = name[2] - '0';
00170
00171 for(netif = netif_list; netif != NULL; netif = netif->next) {
00172 if(num == netif->num &&
00173 name[0] == netif->name[0] &&
00174 name[1] == netif->name[1]) {
00175 DEBUGF(NETIF_DEBUG, ("netif_find: found %s\n", name));
00176 return netif;
00177 }
00178 }
00179 DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %s\n", name));
00180 return NULL;
00181 }
00182
00183 void
00184 netif_set_ipaddr(struct netif *netif, struct ip_addr *ipaddr)
00185 {
00186 DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE, ("netif: setting IP address of interface %c%c%u to %u.%u.%u.%u\n",
00187 netif->name[0], netif->name[1], netif->num,
00188 (u8_t)(ntohl(ipaddr->addr) >> 24 & 0xff),
00189 (u8_t)(ntohl(ipaddr->addr) >> 16 & 0xff),
00190 (u8_t)(ntohl(ipaddr->addr) >> 8 & 0xff),
00191 (u8_t)(ntohl(ipaddr->addr) & 0xff)));
00192 ip_addr_set(&(netif->ip_addr), ipaddr);
00193 }
00194
00195 void
00196 netif_set_gw(struct netif *netif, struct ip_addr *gw)
00197 {
00198 DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE, ("netif: setting GW address of interface %c%c%u to %u.%u.%u.%u\n",
00199 netif->name[0], netif->name[1], netif->num,
00200 (u8_t)(ntohl(gw->addr) >> 24 & 0xff),
00201 (u8_t)(ntohl(gw->addr) >> 16 & 0xff),
00202 (u8_t)(ntohl(gw->addr) >> 8 & 0xff),
00203 (u8_t)(ntohl(gw->addr) & 0xff)));
00204 ip_addr_set(&(netif->gw), gw);
00205 }
00206
00207 void
00208 netif_set_netmask(struct netif *netif, struct ip_addr *netmask)
00209 {
00210 DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE, ("netif: setting netmask of interface %c%c%u to %u.%u.%u.%u\n",
00211 netif->name[0], netif->name[1], netif->num,
00212 (u8_t)(ntohl(netmask->addr) >> 24 & 0xff),
00213 (u8_t)(ntohl(netmask->addr) >> 16 & 0xff),
00214 (u8_t)(ntohl(netmask->addr) >> 8 & 0xff),
00215 (u8_t)(ntohl(netmask->addr) & 0xff)));
00216 ip_addr_set(&(netif->netmask), netmask);
00217 }
00218
00219 void
00220 netif_set_default(struct netif *netif)
00221 {
00222 netif_default = netif;
00223 DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
00224 netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
00225 }
00226
00227 void
00228 netif_init(void)
00229 {
00230 netif_list = netif_default = NULL;
00231 }
00232