]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/blob - src/core/netif.c
Updated lwIP module copyright years to include 2003. Committers must check theirs.
[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/debug.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 struct netif *
44 netif_add(struct ip_addr *ipaddr, struct ip_addr *netmask,
45           struct ip_addr *gw,
46           void (* init)(struct netif *netif),
47           err_t (* input)(struct pbuf *p, struct netif *netif))
48 {
49   struct netif *netif;
50   static int netifnum = 0;
51   
52   netif = mem_malloc(sizeof(struct netif));
53
54   if(netif == NULL) {
55     return NULL;
56   }
57   
58   netif->num = netifnum++;
59   netif->input = input;
60   ip_addr_set(&(netif->ip_addr), ipaddr);
61   ip_addr_set(&(netif->netmask), netmask);
62   ip_addr_set(&(netif->gw), gw);
63
64   init(netif);
65   
66   netif->next = netif_list;
67   netif_list = netif;
68 #if NETIF_DEBUG
69   DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
70                        netif->name[0], netif->name[1]));
71   ip_addr_debug_print(ipaddr);
72   DEBUGF(NETIF_DEBUG, (" netmask "));
73   ip_addr_debug_print(netmask);
74   DEBUGF(NETIF_DEBUG, (" gw "));  
75   ip_addr_debug_print(gw);
76   DEBUGF(NETIF_DEBUG, ("\n"));
77 #endif /* NETIF_DEBUG */
78   return netif;
79 }
80 /*-----------------------------------------------------------------------------------*/
81 struct netif *
82 netif_find(char *name)
83 {
84   struct netif *netif;
85   u8_t num;
86   
87   if(name == NULL) {
88     return NULL;
89   }
90
91   num = name[2] - '0';
92  
93   for(netif = netif_list; netif != NULL; netif = netif->next) {
94     if(num == netif->num &&
95        name[0] == netif->name[0] &&
96        name[1] == netif->name[1]) {
97       DEBUGF(NETIF_DEBUG, ("netif_find: found %s\n", name));
98       return netif;
99     }    
100   }
101   DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %s\n", name));
102   return NULL;
103 }
104 /*-----------------------------------------------------------------------------------*/
105 void
106 netif_set_ipaddr(struct netif *netif, struct ip_addr *ipaddr)
107 {
108   ip_addr_set(&(netif->ip_addr), ipaddr);
109   DEBUGF(NETIF_DEBUG, ("netif: setting IP address of interface %c%c to %d.%d.%d.%d\n",
110                        netif->name[0], netif->name[1],
111                        (u8_t)(ntohl(ipaddr->addr) >> 24 & 0xff),
112                        (u8_t)(ntohl(ipaddr->addr) >> 16 & 0xff),
113                        (u8_t)(ntohl(ipaddr->addr) >> 8 & 0xff),
114                        (u8_t)(ntohl(ipaddr->addr) & 0xff)));
115 }
116 /*-----------------------------------------------------------------------------------*/
117 void
118 netif_set_gw(struct netif *netif, struct ip_addr *gw)
119 {
120   ip_addr_set(&(netif->gw), gw);
121 }
122 /*-----------------------------------------------------------------------------------*/
123 void
124 netif_set_netmask(struct netif *netif, struct ip_addr *netmask)
125 {
126   ip_addr_set(&(netif->netmask), netmask);
127 }
128 /*-----------------------------------------------------------------------------------*/
129 void
130 netif_set_default(struct netif *netif)
131 {
132   netif_default = netif;
133   DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
134                        netif->name[0], netif->name[1]));
135 }
136 /*-----------------------------------------------------------------------------------*/
137 void
138 netif_init(void)
139 {
140   netif_list = netif_default = NULL;
141 }
142 /*-----------------------------------------------------------------------------------*/