The Pedigree Project  0.1
netifapi.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
33 /*
34  * Redistribution and use in source and binary forms, with or without modification,
35  * are permitted provided that the following conditions are met:
36  *
37  * 1. Redistributions of source code must retain the above copyright notice,
38  * this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright notice,
40  * this list of conditions and the following disclaimer in the documentation
41  * and/or other materials provided with the distribution.
42  * 3. The name of the author may not be used to endorse or promote products
43  * derived from this software without specific prior written permission.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
46  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
47  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
48  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
49  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
50  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
53  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
54  * OF SUCH DAMAGE.
55  *
56  * This file is part of the lwIP TCP/IP stack.
57  *
58  */
59 
60 #include "lwip/opt.h"
61 
62 #if LWIP_NETIF_API /* don't build if not configured for use in lwipopts.h */
63 
64 #include "lwip/netifapi.h"
65 #include "lwip/memp.h"
66 #include "lwip/priv/tcpip_priv.h"
67 
68 #define NETIFAPI_VAR_REF(name) API_VAR_REF(name)
69 #define NETIFAPI_VAR_DECLARE(name) API_VAR_DECLARE(struct netifapi_msg, name)
70 #define NETIFAPI_VAR_ALLOC(name) API_VAR_ALLOC(struct netifapi_msg, MEMP_NETIFAPI_MSG, name, ERR_MEM)
71 #define NETIFAPI_VAR_FREE(name) API_VAR_FREE(MEMP_NETIFAPI_MSG, name)
72 
76 static err_t
77 netifapi_do_netif_add(struct tcpip_api_call_data *m)
78 {
79  /* cast through void* to silence alignment warnings.
80  * We know it works because the structs have been instantiated as struct netifapi_msg */
81  struct netifapi_msg *msg = (struct netifapi_msg*)(void*)m;
82 
83  if (!netif_add( msg->netif,
84 #if LWIP_IPV4
85  API_EXPR_REF(msg->msg.add.ipaddr),
86  API_EXPR_REF(msg->msg.add.netmask),
87  API_EXPR_REF(msg->msg.add.gw),
88 #endif /* LWIP_IPV4 */
89  msg->msg.add.state,
90  msg->msg.add.init,
91  msg->msg.add.input)) {
92  return ERR_IF;
93  } else {
94  return ERR_OK;
95  }
96 }
97 
98 #if LWIP_IPV4
99 
102 static err_t
103 netifapi_do_netif_set_addr(struct tcpip_api_call_data *m)
104 {
105  /* cast through void* to silence alignment warnings.
106  * We know it works because the structs have been instantiated as struct netifapi_msg */
107  struct netifapi_msg *msg = (struct netifapi_msg*)(void*)m;
108 
109  netif_set_addr( msg->netif,
110  API_EXPR_REF(msg->msg.add.ipaddr),
111  API_EXPR_REF(msg->msg.add.netmask),
112  API_EXPR_REF(msg->msg.add.gw));
113  return ERR_OK;
114 }
115 #endif /* LWIP_IPV4 */
116 
121 static err_t
122 netifapi_do_netif_common(struct tcpip_api_call_data *m)
123 {
124  /* cast through void* to silence alignment warnings.
125  * We know it works because the structs have been instantiated as struct netifapi_msg */
126  struct netifapi_msg *msg = (struct netifapi_msg*)(void*)m;
127 
128  if (msg->msg.common.errtfunc != NULL) {
129  return msg->msg.common.errtfunc(msg->netif);
130  } else {
131  msg->msg.common.voidfunc(msg->netif);
132  return ERR_OK;
133  }
134 }
135 
143 err_t
144 netifapi_netif_add(struct netif *netif,
145 #if LWIP_IPV4
146  const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw,
147 #endif /* LWIP_IPV4 */
148  void *state, netif_init_fn init, netif_input_fn input)
149 {
150  err_t err;
151  NETIFAPI_VAR_DECLARE(msg);
152  NETIFAPI_VAR_ALLOC(msg);
153 
154 #if LWIP_IPV4
155  if (ipaddr == NULL) {
156  ipaddr = IP4_ADDR_ANY4;
157  }
158  if (netmask == NULL) {
159  netmask = IP4_ADDR_ANY4;
160  }
161  if (gw == NULL) {
162  gw = IP4_ADDR_ANY4;
163  }
164 #endif /* LWIP_IPV4 */
165 
166  NETIFAPI_VAR_REF(msg).netif = netif;
167 #if LWIP_IPV4
168  NETIFAPI_VAR_REF(msg).msg.add.ipaddr = NETIFAPI_VAR_REF(ipaddr);
169  NETIFAPI_VAR_REF(msg).msg.add.netmask = NETIFAPI_VAR_REF(netmask);
170  NETIFAPI_VAR_REF(msg).msg.add.gw = NETIFAPI_VAR_REF(gw);
171 #endif /* LWIP_IPV4 */
172  NETIFAPI_VAR_REF(msg).msg.add.state = state;
173  NETIFAPI_VAR_REF(msg).msg.add.init = init;
174  NETIFAPI_VAR_REF(msg).msg.add.input = input;
175  err = tcpip_api_call(netifapi_do_netif_add, &API_VAR_REF(msg).call);
176  NETIFAPI_VAR_FREE(msg);
177  return err;
178 }
179 
180 #if LWIP_IPV4
181 
188 err_t
189 netifapi_netif_set_addr(struct netif *netif,
190  const ip4_addr_t *ipaddr,
191  const ip4_addr_t *netmask,
192  const ip4_addr_t *gw)
193 {
194  err_t err;
195  NETIFAPI_VAR_DECLARE(msg);
196  NETIFAPI_VAR_ALLOC(msg);
197 
198  if (ipaddr == NULL) {
199  ipaddr = IP4_ADDR_ANY4;
200  }
201  if (netmask == NULL) {
202  netmask = IP4_ADDR_ANY4;
203  }
204  if (gw == NULL) {
205  gw = IP4_ADDR_ANY4;
206  }
207 
208  NETIFAPI_VAR_REF(msg).netif = netif;
209  NETIFAPI_VAR_REF(msg).msg.add.ipaddr = NETIFAPI_VAR_REF(ipaddr);
210  NETIFAPI_VAR_REF(msg).msg.add.netmask = NETIFAPI_VAR_REF(netmask);
211  NETIFAPI_VAR_REF(msg).msg.add.gw = NETIFAPI_VAR_REF(gw);
212  err = tcpip_api_call(netifapi_do_netif_set_addr, &API_VAR_REF(msg).call);
213  NETIFAPI_VAR_FREE(msg);
214  return err;
215 }
216 #endif /* LWIP_IPV4 */
217 
224 err_t
225 netifapi_netif_common(struct netif *netif, netifapi_void_fn voidfunc,
226  netifapi_errt_fn errtfunc)
227 {
228  err_t err;
229  NETIFAPI_VAR_DECLARE(msg);
230  NETIFAPI_VAR_ALLOC(msg);
231 
232  NETIFAPI_VAR_REF(msg).netif = netif;
233  NETIFAPI_VAR_REF(msg).msg.common.voidfunc = voidfunc;
234  NETIFAPI_VAR_REF(msg).msg.common.errtfunc = errtfunc;
235  err = tcpip_api_call(netifapi_do_netif_common, &API_VAR_REF(msg).call);
236  NETIFAPI_VAR_FREE(msg);
237  return err;
238 }
239 
240 #endif /* LWIP_NETIF_API */
err_t(* netif_input_fn)(struct pbuf *p, struct netif *inp)
Definition: netif.h:181
#define LWIP_IPV4
Definition: opt.h:659
struct netif * netif_add(struct netif *netif, void *state, netif_init_fn init, netif_input_fn input)
Definition: netif.c:260
err_t tcpip_api_call(tcpip_api_call_fn fn, struct tcpip_api_call_data *call)
Definition: tcpip.c:384
Definition: netif.h:244
err_t(* netif_init_fn)(struct netif *netif)
Definition: netif.h:174
Definition: err.h:106
s8_t err_t
Definition: err.h:76
Definition: err.h:82