]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ankh/lib/lwip/lib/contrib/src/api/api_lib.c
update
[l4.git] / l4 / pkg / ankh / lib / lwip / lib / contrib / src / api / api_lib.c
1 /**
2  * @file
3  * Sequential API External module
4  *
5  */
6  
7 /*
8  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9  * All rights reserved. 
10  * 
11  * Redistribution and use in source and binary forms, with or without modification, 
12  * are permitted provided that the following conditions are met:
13  *
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. 
21  *
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 
31  * OF SUCH DAMAGE.
32  *
33  * This file is part of the lwIP TCP/IP stack.
34  * 
35  * Author: Adam Dunkels <adam@sics.se>
36  *
37  */
38
39 /* This is the part of the API that is linked with
40    the application */
41
42 #include "lwip/opt.h"
43
44 #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
45
46 #include "lwip/api.h"
47 #include "lwip/tcpip.h"
48 #include "lwip/memp.h"
49
50 #include "lwip/ip.h"
51 #include "lwip/raw.h"
52 #include "lwip/udp.h"
53 #include "lwip/tcp.h"
54
55 #include <string.h>
56
57 /**
58  * Create a new netconn (of a specific type) that has a callback function.
59  * The corresponding pcb is also created.
60  *
61  * @param t the type of 'connection' to create (@see enum netconn_type)
62  * @param proto the IP protocol for RAW IP pcbs
63  * @param callback a function to call on status changes (RX available, TX'ed)
64  * @return a newly allocated struct netconn or
65  *         NULL on memory error
66  */
67 struct netconn*
68 netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto, netconn_callback callback)
69 {
70   struct netconn *conn;
71   struct api_msg msg;
72
73   conn = netconn_alloc(t, callback);
74   if (conn != NULL) {
75     msg.function = do_newconn;
76     msg.msg.msg.n.proto = proto;
77     msg.msg.conn = conn;
78     if (TCPIP_APIMSG(&msg) != ERR_OK) {
79       LWIP_ASSERT("freeing conn without freeing pcb", conn->pcb.tcp == NULL);
80       LWIP_ASSERT("conn has no op_completed", sys_sem_valid(&conn->op_completed));
81       LWIP_ASSERT("conn has no recvmbox", sys_mbox_valid(&conn->recvmbox));
82 #if LWIP_TCP
83       LWIP_ASSERT("conn->acceptmbox shouldn't exist", !sys_mbox_valid(&conn->acceptmbox));
84 #endif /* LWIP_TCP */
85       sys_sem_free(&conn->op_completed);
86       sys_mbox_free(&conn->recvmbox);
87       memp_free(MEMP_NETCONN, conn);
88       return NULL;
89     }
90   }
91   return conn;
92 }
93
94 /**
95  * Close a netconn 'connection' and free its resources.
96  * UDP and RAW connection are completely closed, TCP pcbs might still be in a waitstate
97  * after this returns.
98  *
99  * @param conn the netconn to delete
100  * @return ERR_OK if the connection was deleted
101  */
102 err_t
103 netconn_delete(struct netconn *conn)
104 {
105   struct api_msg msg;
106
107   /* No ASSERT here because possible to get a (conn == NULL) if we got an accept error */
108   if (conn == NULL) {
109     return ERR_OK;
110   }
111
112   msg.function = do_delconn;
113   msg.msg.conn = conn;
114   tcpip_apimsg(&msg);
115
116   netconn_free(conn);
117
118   /* don't care for return value of do_delconn since it only calls void functions */
119
120   return ERR_OK;
121 }
122
123 /**
124  * Get the local or remote IP address and port of a netconn.
125  * For RAW netconns, this returns the protocol instead of a port!
126  *
127  * @param conn the netconn to query
128  * @param addr a pointer to which to save the IP address
129  * @param port a pointer to which to save the port (or protocol for RAW)
130  * @param local 1 to get the local IP address, 0 to get the remote one
131  * @return ERR_CONN for invalid connections
132  *         ERR_OK if the information was retrieved
133  */
134 err_t
135 netconn_getaddr(struct netconn *conn, ip_addr_t *addr, u16_t *port, u8_t local)
136 {
137   struct api_msg msg;
138   err_t err;
139
140   LWIP_ERROR("netconn_getaddr: invalid conn", (conn != NULL), return ERR_ARG;);
141   LWIP_ERROR("netconn_getaddr: invalid addr", (addr != NULL), return ERR_ARG;);
142   LWIP_ERROR("netconn_getaddr: invalid port", (port != NULL), return ERR_ARG;);
143
144   msg.function = do_getaddr;
145   msg.msg.conn = conn;
146   msg.msg.msg.ad.ipaddr = addr;
147   msg.msg.msg.ad.port = port;
148   msg.msg.msg.ad.local = local;
149   err = TCPIP_APIMSG(&msg);
150
151   NETCONN_SET_SAFE_ERR(conn, err);
152   return err;
153 }
154
155 /**
156  * Bind a netconn to a specific local IP address and port.
157  * Binding one netconn twice might not always be checked correctly!
158  *
159  * @param conn the netconn to bind
160  * @param addr the local IP address to bind the netconn to (use IP_ADDR_ANY
161  *             to bind to all addresses)
162  * @param port the local port to bind the netconn to (not used for RAW)
163  * @return ERR_OK if bound, any other err_t on failure
164  */
165 err_t
166 netconn_bind(struct netconn *conn, ip_addr_t *addr, u16_t port)
167 {
168   struct api_msg msg;
169   err_t err;
170
171   LWIP_ERROR("netconn_bind: invalid conn", (conn != NULL), return ERR_ARG;);
172
173   msg.function = do_bind;
174   msg.msg.conn = conn;
175   msg.msg.msg.bc.ipaddr = addr;
176   msg.msg.msg.bc.port = port;
177   err = TCPIP_APIMSG(&msg);
178
179   NETCONN_SET_SAFE_ERR(conn, err);
180   return err;
181 }
182
183 /**
184  * Connect a netconn to a specific remote IP address and port.
185  *
186  * @param conn the netconn to connect
187  * @param addr the remote IP address to connect to
188  * @param port the remote port to connect to (no used for RAW)
189  * @return ERR_OK if connected, return value of tcp_/udp_/raw_connect otherwise
190  */
191 err_t
192 netconn_connect(struct netconn *conn, ip_addr_t *addr, u16_t port)
193 {
194   struct api_msg msg;
195   err_t err;
196
197   LWIP_ERROR("netconn_connect: invalid conn", (conn != NULL), return ERR_ARG;);
198
199   msg.function = do_connect;
200   msg.msg.conn = conn;
201   msg.msg.msg.bc.ipaddr = addr;
202   msg.msg.msg.bc.port = port;
203   /* This is the only function which need to not block tcpip_thread */
204   err = tcpip_apimsg(&msg);
205
206   NETCONN_SET_SAFE_ERR(conn, err);
207   return err;
208 }
209
210 /**
211  * Disconnect a netconn from its current peer (only valid for UDP netconns).
212  *
213  * @param conn the netconn to disconnect
214  * @return TODO: return value is not set here...
215  */
216 err_t
217 netconn_disconnect(struct netconn *conn)
218 {
219   struct api_msg msg;
220   err_t err;
221
222   LWIP_ERROR("netconn_disconnect: invalid conn", (conn != NULL), return ERR_ARG;);
223
224   msg.function = do_disconnect;
225   msg.msg.conn = conn;
226   err = TCPIP_APIMSG(&msg);
227
228   NETCONN_SET_SAFE_ERR(conn, err);
229   return err;
230 }
231
232 /**
233  * Set a TCP netconn into listen mode
234  *
235  * @param conn the tcp netconn to set to listen mode
236  * @param backlog the listen backlog, only used if TCP_LISTEN_BACKLOG==1
237  * @return ERR_OK if the netconn was set to listen (UDP and RAW netconns
238  *         don't return any error (yet?))
239  */
240 err_t
241 netconn_listen_with_backlog(struct netconn *conn, u8_t backlog)
242 {
243   struct api_msg msg;
244   err_t err;
245
246   /* This does no harm. If TCP_LISTEN_BACKLOG is off, backlog is unused. */
247   LWIP_UNUSED_ARG(backlog);
248
249   LWIP_ERROR("netconn_listen: invalid conn", (conn != NULL), return ERR_ARG;);
250
251   msg.function = do_listen;
252   msg.msg.conn = conn;
253 #if TCP_LISTEN_BACKLOG
254   msg.msg.msg.lb.backlog = backlog;
255 #endif /* TCP_LISTEN_BACKLOG */
256   err = TCPIP_APIMSG(&msg);
257
258   NETCONN_SET_SAFE_ERR(conn, err);
259   return err;
260 }
261
262 /**
263  * Accept a new connection on a TCP listening netconn.
264  *
265  * @param conn the TCP listen netconn
266  * @param new_conn pointer where the new connection is stored
267  * @return ERR_OK if a new connection has been received or an error
268  *                code otherwise
269  */
270 err_t
271 netconn_accept(struct netconn *conn, struct netconn **new_conn)
272 {
273 #if LWIP_TCP
274   struct netconn *newconn;
275   err_t err;
276 #if TCP_LISTEN_BACKLOG
277   struct api_msg msg;
278 #endif /* TCP_LISTEN_BACKLOG */
279
280   LWIP_ERROR("netconn_accept: invalid pointer",    (new_conn != NULL),                  return ERR_ARG;);
281   *new_conn = NULL;
282   LWIP_ERROR("netconn_accept: invalid conn",       (conn != NULL),                      return ERR_ARG;);
283   LWIP_ERROR("netconn_accept: invalid acceptmbox", sys_mbox_valid(&conn->acceptmbox),   return ERR_ARG;);
284
285   err = conn->last_err;
286   if (ERR_IS_FATAL(err)) {
287     /* don't recv on fatal errors: this might block the application task
288        waiting on acceptmbox forever! */
289     return err;
290   }
291
292 #if LWIP_SO_RCVTIMEO
293   if (sys_arch_mbox_fetch(&conn->acceptmbox, (void **)&newconn, conn->recv_timeout) == SYS_ARCH_TIMEOUT) {
294     NETCONN_SET_SAFE_ERR(conn, ERR_TIMEOUT);
295     return ERR_TIMEOUT;
296   }
297 #else
298   sys_arch_mbox_fetch(&conn->acceptmbox, (void **)&newconn, 0);
299 #endif /* LWIP_SO_RCVTIMEO*/
300     /* Register event with callback */
301     API_EVENT(conn, NETCONN_EVT_RCVMINUS, 0);
302
303   if (newconn == NULL) {
304     /* connection has been closed */
305     NETCONN_SET_SAFE_ERR(conn, ERR_CLSD);
306     return ERR_CLSD;
307   }
308 #if TCP_LISTEN_BACKLOG
309       /* Let the stack know that we have accepted the connection. */
310       msg.function = do_recv;
311       msg.msg.conn = conn;
312   /* don't care for the return value of do_recv */
313       TCPIP_APIMSG(&msg);
314 #endif /* TCP_LISTEN_BACKLOG */
315
316   *new_conn = newconn;
317   /* don't set conn->last_err: it's only ERR_OK, anyway */
318   return ERR_OK;
319 #else /* LWIP_TCP */
320   LWIP_UNUSED_ARG(conn);
321   LWIP_UNUSED_ARG(new_conn);
322   return ERR_ARG;
323 #endif /* LWIP_TCP */
324 }
325
326 /**
327  * Receive data: actual implementation that doesn't care whether pbuf or netbuf
328  * is received
329  *
330  * @param conn the netconn from which to receive data
331  * @param new_buf pointer where a new pbuf/netbuf is stored when received data
332  * @return ERR_OK if data has been received, an error code otherwise (timeout,
333  *                memory error or another error)
334  */
335 static err_t
336 netconn_recv_data(struct netconn *conn, void **new_buf)
337 {
338   void *buf = NULL;
339   u16_t len;
340   err_t err;
341 #if LWIP_TCP
342   struct api_msg msg;
343 #endif /* LWIP_TCP */
344
345   LWIP_ERROR("netconn_recv: invalid pointer", (new_buf != NULL), return ERR_ARG;);
346   *new_buf = NULL;
347   LWIP_ERROR("netconn_recv: invalid conn",    (conn != NULL),    return ERR_ARG;);
348   LWIP_ERROR("netconn_accept: invalid recvmbox", sys_mbox_valid(&conn->recvmbox), return ERR_CONN;);
349
350   err = conn->last_err;
351   if (ERR_IS_FATAL(err)) {
352     /* don't recv on fatal errors: this might block the application task
353        waiting on recvmbox forever! */
354     /* @todo: this does not allow us to fetch data that has been put into recvmbox
355        before the fatal error occurred - is that a problem? */
356     return err;
357   }
358
359 #if LWIP_SO_RCVTIMEO
360   if (sys_arch_mbox_fetch(&conn->recvmbox, &buf, conn->recv_timeout) == SYS_ARCH_TIMEOUT) {
361     NETCONN_SET_SAFE_ERR(conn, ERR_TIMEOUT);
362     return ERR_TIMEOUT;
363   }
364 #else
365   sys_arch_mbox_fetch(&conn->recvmbox, &buf, 0);
366 #endif /* LWIP_SO_RCVTIMEO*/
367
368 #if LWIP_TCP
369   if (conn->type == NETCONN_TCP) {
370     if (!netconn_get_noautorecved(conn) || (buf == NULL)) {
371       /* Let the stack know that we have taken the data. */
372       /* TODO: Speedup: Don't block and wait for the answer here
373          (to prevent multiple thread-switches). */
374       msg.function = do_recv;
375       msg.msg.conn = conn;
376       if (buf != NULL) {
377         msg.msg.msg.r.len = ((struct pbuf *)buf)->tot_len;
378       } else {
379         msg.msg.msg.r.len = 1;
380       }
381       /* don't care for the return value of do_recv */
382       TCPIP_APIMSG(&msg);
383     }
384
385     /* If we are closed, we indicate that we no longer wish to use the socket */
386     if (buf == NULL) {
387       API_EVENT(conn, NETCONN_EVT_RCVMINUS, 0);
388       /* Avoid to lose any previous error code */
389       NETCONN_SET_SAFE_ERR(conn, ERR_CLSD);
390       return ERR_CLSD;
391     }
392     len = ((struct pbuf *)buf)->tot_len;
393     }
394 #endif /* LWIP_TCP */
395 #if LWIP_TCP && (LWIP_UDP || LWIP_RAW)
396   else
397 #endif /* LWIP_TCP && (LWIP_UDP || LWIP_RAW) */
398 #if (LWIP_UDP || LWIP_RAW)
399   {
400     LWIP_ASSERT("buf != NULL", buf != NULL);
401     len = netbuf_len((struct netbuf *)buf);
402     }
403 #endif /* (LWIP_UDP || LWIP_RAW) */
404
405   SYS_ARCH_DEC(conn->recv_avail, len);
406     /* Register event with callback */
407     API_EVENT(conn, NETCONN_EVT_RCVMINUS, len);
408
409   LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_recv_data: received %p, len=%"U16_F"\n", buf, len));
410
411   *new_buf = buf;
412   /* don't set conn->last_err: it's only ERR_OK, anyway */
413   return ERR_OK;
414 }
415
416 /**
417  * Receive data (in form of a pbuf) from a TCP netconn
418  *
419  * @param conn the netconn from which to receive data
420  * @param new_buf pointer where a new pbuf is stored when received data
421  * @return ERR_OK if data has been received, an error code otherwise (timeout,
422  *                memory error or another error)
423  *         ERR_ARG if conn is not a TCP netconn
424  */
425 err_t
426 netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf)
427 {
428   LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL) &&
429              netconn_type(conn) == NETCONN_TCP, return ERR_ARG;);
430
431   return netconn_recv_data(conn, (void **)new_buf);
432 }
433
434 /**
435  * Receive data (in form of a netbuf containing a packet buffer) from a netconn
436  *
437  * @param conn the netconn from which to receive data
438  * @param new_buf pointer where a new netbuf is stored when received data
439  * @return ERR_OK if data has been received, an error code otherwise (timeout,
440  *                memory error or another error)
441  */
442 err_t
443 netconn_recv(struct netconn *conn, struct netbuf **new_buf)
444 {
445 #if LWIP_TCP
446   struct netbuf *buf = NULL;
447   err_t err;
448 #endif /* LWIP_TCP */
449
450   LWIP_ERROR("netconn_recv: invalid pointer", (new_buf != NULL), return ERR_ARG;);
451   *new_buf = NULL;
452   LWIP_ERROR("netconn_recv: invalid conn",    (conn != NULL),    return ERR_ARG;);
453   LWIP_ERROR("netconn_accept: invalid recvmbox", sys_mbox_valid(&conn->recvmbox), return ERR_CONN;);
454
455 #if LWIP_TCP
456   if (conn->type == NETCONN_TCP) {
457     struct pbuf *p = NULL;
458     /* This is not a listening netconn, since recvmbox is set */
459
460     buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
461     if (buf == NULL) {
462       NETCONN_SET_SAFE_ERR(conn, ERR_MEM);
463       return ERR_MEM;
464       }
465
466     err = netconn_recv_data(conn, (void **)&p);
467     if (err != ERR_OK) {
468       memp_free(MEMP_NETBUF, buf);
469       return err;
470     }
471     LWIP_ASSERT("p != NULL", p != NULL);
472
473     buf->p = p;
474     buf->ptr = p;
475     buf->port = 0;
476     buf->addr = NULL;
477     *new_buf = buf;
478     /* don't set conn->last_err: it's only ERR_OK, anyway */
479     return ERR_OK;
480   } else
481 #endif /* LWIP_TCP */
482   {
483 #if (LWIP_UDP || LWIP_RAW)
484     return netconn_recv_data(conn, (void **)new_buf);
485 #endif /* (LWIP_UDP || LWIP_RAW) */
486   }
487 }
488
489 /**
490  * TCP: update the receive window: by calling this, the application
491  * tells the stack that it has processed data and is able to accept
492  * new data.
493  * ATTENTION: use with care, this is mainly used for sockets!
494  * Can only be used when calling netconn_set_noautorecved(conn, 1) before.
495  *
496  * @param conn the netconn for which to update the receive window
497  * @param length amount of data processed (ATTENTION: this must be accurate!)
498  */
499 void
500 netconn_recved(struct netconn *conn, u32_t length)
501 {
502   if ((conn != NULL) && (conn->type == NETCONN_TCP) &&
503       (netconn_get_noautorecved(conn))) {
504     struct api_msg msg;
505     /* Let the stack know that we have taken the data. */
506     /* TODO: Speedup: Don't block and wait for the answer here
507        (to prevent multiple thread-switches). */
508     msg.function = do_recv;
509     msg.msg.conn = conn;
510     msg.msg.msg.r.len = length;
511     /* don't care for the return value of do_recv */
512     TCPIP_APIMSG(&msg);
513     }
514 }
515
516 /**
517  * Send data (in form of a netbuf) to a specific remote IP address and port.
518  * Only to be used for UDP and RAW netconns (not TCP).
519  *
520  * @param conn the netconn over which to send data
521  * @param buf a netbuf containing the data to send
522  * @param addr the remote IP address to which to send the data
523  * @param port the remote port to which to send the data
524  * @return ERR_OK if data was sent, any other err_t on error
525  */
526 err_t
527 netconn_sendto(struct netconn *conn, struct netbuf *buf, ip_addr_t *addr, u16_t port)
528 {
529   if (buf != NULL) {
530     buf->addr = addr;
531     buf->port = port;
532     return netconn_send(conn, buf);
533   }
534   return ERR_VAL;
535 }
536
537 /**
538  * Send data over a UDP or RAW netconn (that is already connected).
539  *
540  * @param conn the UDP or RAW netconn over which to send data
541  * @param buf a netbuf containing the data to send
542  * @return ERR_OK if data was sent, any other err_t on error
543  */
544 err_t
545 netconn_send(struct netconn *conn, struct netbuf *buf)
546 {
547   struct api_msg msg;
548   err_t err;
549
550   LWIP_ERROR("netconn_send: invalid conn",  (conn != NULL), return ERR_ARG;);
551
552   LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %"U16_F" bytes\n", buf->p->tot_len));
553   msg.function = do_send;
554   msg.msg.conn = conn;
555   msg.msg.msg.b = buf;
556   err = TCPIP_APIMSG(&msg);
557
558   NETCONN_SET_SAFE_ERR(conn, err);
559   return err;
560 }
561
562 /**
563  * Send data over a TCP netconn.
564  *
565  * @param conn the TCP netconn over which to send data
566  * @param dataptr pointer to the application buffer that contains the data to send
567  * @param size size of the application data to send
568  * @param apiflags combination of following flags :
569  * - NETCONN_COPY: data will be copied into memory belonging to the stack
570  * - NETCONN_MORE: for TCP connection, PSH flag will be set on last segment sent
571  * - NETCONN_DONTBLOCK: only write the data if all dat can be written at once
572  * @return ERR_OK if data was sent, any other err_t on error
573  */
574 err_t
575 netconn_write(struct netconn *conn, const void *dataptr, size_t size, u8_t apiflags)
576 {
577   struct api_msg msg;
578   err_t err;
579
580   LWIP_ERROR("netconn_write: invalid conn",  (conn != NULL), return ERR_ARG;);
581   LWIP_ERROR("netconn_write: invalid conn->type",  (conn->type == NETCONN_TCP), return ERR_VAL;);
582   if (size == 0) {
583     return ERR_OK;
584   }
585
586   /* @todo: for non-blocking write, check if 'size' would ever fit into
587             snd_queue or snd_buf */
588   msg.function = do_write;
589   msg.msg.conn = conn;
590   msg.msg.msg.w.dataptr = dataptr;
591   msg.msg.msg.w.apiflags = apiflags;
592   msg.msg.msg.w.len = size;
593   /* For locking the core: this _can_ be delayed on low memory/low send buffer,
594      but if it is, this is done inside api_msg.c:do_write(), so we can use the
595      non-blocking version here. */
596   err = TCPIP_APIMSG(&msg);
597
598   NETCONN_SET_SAFE_ERR(conn, err);
599   return err;
600 }
601
602 /**
603  * Close a TCP netconn (doesn't delete it).
604  *
605  * @param conn the TCP netconn to close
606  * @return ERR_OK if the netconn was closed, any other err_t on error
607  */
608 err_t
609 netconn_close(struct netconn *conn)
610 {
611   struct api_msg msg;
612   err_t err;
613
614   LWIP_ERROR("netconn_close: invalid conn",  (conn != NULL), return ERR_ARG;);
615
616   msg.function = do_close;
617   msg.msg.conn = conn;
618   /* because of the LWIP_TCPIP_CORE_LOCKING implementation of do_close,
619      don't use TCPIP_APIMSG here */
620   err = tcpip_apimsg(&msg);
621
622   NETCONN_SET_SAFE_ERR(conn, err);
623   return err;
624 }
625
626 #if LWIP_IGMP
627 /**
628  * Join multicast groups for UDP netconns.
629  *
630  * @param conn the UDP netconn for which to change multicast addresses
631  * @param multiaddr IP address of the multicast group to join or leave
632  * @param netif_addr the IP address of the network interface on which to send
633  *                  the igmp message
634  * @param join_or_leave flag whether to send a join- or leave-message
635  * @return ERR_OK if the action was taken, any err_t on error
636  */
637 err_t
638 netconn_join_leave_group(struct netconn *conn,
639                          ip_addr_t *multiaddr,
640                          ip_addr_t *netif_addr,
641                          enum netconn_igmp join_or_leave)
642 {
643   struct api_msg msg;
644   err_t err;
645
646   LWIP_ERROR("netconn_join_leave_group: invalid conn",  (conn != NULL), return ERR_ARG;);
647
648   msg.function = do_join_leave_group;
649   msg.msg.conn = conn;
650   msg.msg.msg.jl.multiaddr = multiaddr;
651   msg.msg.msg.jl.netif_addr = netif_addr;
652   msg.msg.msg.jl.join_or_leave = join_or_leave;
653   err = TCPIP_APIMSG(&msg);
654
655   NETCONN_SET_SAFE_ERR(conn, err);
656   return err;
657 }
658 #endif /* LWIP_IGMP */
659
660 #if LWIP_DNS
661 /**
662  * Execute a DNS query, only one IP address is returned
663  *
664  * @param name a string representation of the DNS host name to query
665  * @param addr a preallocated ip_addr_t where to store the resolved IP address
666  * @return ERR_OK: resolving succeeded
667  *         ERR_MEM: memory error, try again later
668  *         ERR_ARG: dns client not initialized or invalid hostname
669  *         ERR_VAL: dns server response was invalid
670  */
671 err_t
672 netconn_gethostbyname(const char *name, ip_addr_t *addr)
673 {
674   struct dns_api_msg msg;
675   err_t err;
676   sys_sem_t sem;
677
678   LWIP_ERROR("netconn_gethostbyname: invalid name", (name != NULL), return ERR_ARG;);
679   LWIP_ERROR("netconn_gethostbyname: invalid addr", (addr != NULL), return ERR_ARG;);
680
681   err = sys_sem_new(&sem, 0);
682   if (err != ERR_OK) {
683     return err;
684   }
685
686   msg.name = name;
687   msg.addr = addr;
688   msg.err = &err;
689   msg.sem = &sem;
690
691   tcpip_callback(do_gethostbyname, &msg);
692   sys_sem_wait(&sem);
693   sys_sem_free(&sem);
694
695   return err;
696 }
697 #endif /* LWIP_DNS*/
698
699 #endif /* LWIP_NETCONN */