uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
api_msg.c
Go to the documentation of this file.
1 
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 #include "lwip/opt.h"
40 
41 #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
42 
43 #include "lwip/api_msg.h"
44 
45 #include "lwip/ip.h"
46 #include "lwip/udp.h"
47 #include "lwip/tcp.h"
48 #include "lwip/raw.h"
49 
50 #include "lwip/memp.h"
51 #include "lwip/tcpip.h"
52 #include "lwip/igmp.h"
53 #include "lwip/dns.h"
54 
55 #include <string.h>
56 
57 #define SET_NONBLOCKING_CONNECT(conn, val) do { if(val) { \
58  (conn)->flags |= NETCONN_FLAG_IN_NONBLOCKING_CONNECT; \
59 } else { \
60  (conn)->flags &= ~ NETCONN_FLAG_IN_NONBLOCKING_CONNECT; }} while(0)
61 #define IN_NONBLOCKING_CONNECT(conn) (((conn)->flags & NETCONN_FLAG_IN_NONBLOCKING_CONNECT) != 0)
62 
63 /* forward declarations */
64 #if LWIP_TCP
65 static err_t do_writemore(struct netconn *conn);
66 static void do_close_internal(struct netconn *conn);
67 #endif
68 
69 #if LWIP_RAW
70 
77 static u8_t
78 recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p,
79  ip_addr_t *addr)
80 {
81  struct pbuf *q;
82  struct netbuf *buf;
83  struct netconn *conn;
84 
85  LWIP_UNUSED_ARG(addr);
86  conn = (struct netconn *)arg;
87 
88  if ((conn != NULL) && sys_mbox_valid(&conn->recvmbox)) {
89 #if LWIP_SO_RCVBUF
90  int recv_avail;
91  SYS_ARCH_GET(conn->recv_avail, recv_avail);
92  if ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize) {
93  return 0;
94  }
95 #endif /* LWIP_SO_RCVBUF */
96  /* copy the whole packet into new pbufs */
98  if(q != NULL) {
99  if (pbuf_copy(q, p) != ERR_OK) {
100  pbuf_free(q);
101  q = NULL;
102  }
103  }
104 
105  if (q != NULL) {
106  u16_t len;
107  buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
108  if (buf == NULL) {
109  pbuf_free(q);
110  return 0;
111  }
112 
113  buf->p = q;
114  buf->ptr = q;
116  buf->port = pcb->protocol;
117 
118  len = q->tot_len;
119  if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) {
120  netbuf_delete(buf);
121  return 0;
122  } else {
123 #if LWIP_SO_RCVBUF
124  SYS_ARCH_INC(conn->recv_avail, len);
125 #endif /* LWIP_SO_RCVBUF */
126  /* Register event with callback */
127  API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
128  }
129  }
130  }
131 
132  return 0; /* do not eat the packet */
133 }
134 #endif /* LWIP_RAW*/
135 
136 #if LWIP_UDP
137 
143 static void
144 recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
145  ip_addr_t *addr, u16_t port)
146 {
147  struct netbuf *buf;
148  struct netconn *conn;
149  u16_t len;
150 #if LWIP_SO_RCVBUF
151  int recv_avail;
152 #endif /* LWIP_SO_RCVBUF */
153 
154  LWIP_UNUSED_ARG(pcb); /* only used for asserts... */
155  LWIP_ASSERT("recv_udp must have a pcb argument", pcb != NULL);
156  LWIP_ASSERT("recv_udp must have an argument", arg != NULL);
157  conn = (struct netconn *)arg;
158  LWIP_ASSERT("recv_udp: recv for wrong pcb!", conn->pcb.udp == pcb);
159 
160 #if LWIP_SO_RCVBUF
161  SYS_ARCH_GET(conn->recv_avail, recv_avail);
162  if ((conn == NULL) || !sys_mbox_valid(&conn->recvmbox) ||
163  ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize)) {
164 #else /* LWIP_SO_RCVBUF */
165  if ((conn == NULL) || !sys_mbox_valid(&conn->recvmbox)) {
166 #endif /* LWIP_SO_RCVBUF */
167  pbuf_free(p);
168  return;
169  }
170 
171  buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
172  if (buf == NULL) {
173  pbuf_free(p);
174  return;
175  } else {
176  buf->p = p;
177  buf->ptr = p;
178  ip_addr_set(&buf->addr, addr);
179  buf->port = port;
180 #if LWIP_NETBUF_RECVINFO
181  {
182  const struct ip_hdr* iphdr = ip_current_header();
183  /* get the UDP header - always in the first pbuf, ensured by udp_input */
184  const struct udp_hdr* udphdr = (void*)(((char*)iphdr) + IPH_LEN(iphdr));
185 #if LWIP_CHECKSUM_ON_COPY
186  buf->flags = NETBUF_FLAG_DESTADDR;
187 #endif /* LWIP_CHECKSUM_ON_COPY */
188  ip_addr_set(&buf->toaddr, ip_current_dest_addr());
189  buf->toport_chksum = udphdr->dest;
190  }
191 #endif /* LWIP_NETBUF_RECVINFO */
192  }
193 
194  len = p->tot_len;
195  if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) {
196  netbuf_delete(buf);
197  return;
198  } else {
199 #if LWIP_SO_RCVBUF
200  SYS_ARCH_INC(conn->recv_avail, len);
201 #endif /* LWIP_SO_RCVBUF */
202  /* Register event with callback */
203  API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
204  }
205 }
206 #endif /* LWIP_UDP */
207 
208 #if LWIP_TCP
209 
215 static err_t
216 recv_tcp(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
217 {
218  struct netconn *conn;
219  u16_t len;
220 
221  LWIP_UNUSED_ARG(pcb);
222  LWIP_ASSERT("recv_tcp must have a pcb argument", pcb != NULL);
223  LWIP_ASSERT("recv_tcp must have an argument", arg != NULL);
224  conn = (struct netconn *)arg;
225  LWIP_ASSERT("recv_tcp: recv for wrong pcb!", conn->pcb.tcp == pcb);
226 
227  if (conn == NULL) {
228  return ERR_VAL;
229  }
230  if (!sys_mbox_valid(&conn->recvmbox)) {
231  /* recvmbox already deleted */
232  if (p != NULL) {
233  tcp_recved(pcb, p->tot_len);
234  pbuf_free(p);
235  }
236  return ERR_OK;
237  }
238  /* Unlike for UDP or RAW pcbs, don't check for available space
239  using recv_avail since that could break the connection
240  (data is already ACKed) */
241 
242  /* don't overwrite fatal errors! */
243  NETCONN_SET_SAFE_ERR(conn, err);
244 
245  if (p != NULL) {
246  len = p->tot_len;
247  } else {
248  len = 0;
249  }
250 
251  if (sys_mbox_trypost(&conn->recvmbox, p) != ERR_OK) {
252  /* don't deallocate p: it is presented to us later again from tcp_fasttmr! */
253  return ERR_MEM;
254  } else {
255 #if LWIP_SO_RCVBUF
256  SYS_ARCH_INC(conn->recv_avail, len);
257 #endif /* LWIP_SO_RCVBUF */
258  /* Register event with callback */
259  API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
260  }
261 
262  return ERR_OK;
263 }
264 
276 static err_t
277 poll_tcp(void *arg, struct tcp_pcb *pcb)
278 {
279  struct netconn *conn = (struct netconn *)arg;
280 
281  LWIP_UNUSED_ARG(pcb);
282  LWIP_ASSERT("conn != NULL", (conn != NULL));
283 
284  if (conn->state == NETCONN_WRITE) {
285  do_writemore(conn);
286  } else if (conn->state == NETCONN_CLOSE) {
287  do_close_internal(conn);
288  }
289  /* @todo: implement connect timeout here? */
290 
291  /* Did a nonblocking write fail before? Then check available write-space. */
292  if (conn->flags & NETCONN_FLAG_CHECK_WRITESPACE) {
293  /* If the queued byte- or pbuf-count drops below the configured low-water limit,
294  let select mark this pcb as writable again. */
295  if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT) &&
296  (tcp_sndqueuelen(conn->pcb.tcp) < TCP_SNDQUEUELOWAT)) {
297  conn->flags &= ~NETCONN_FLAG_CHECK_WRITESPACE;
298  API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
299  }
300  }
301 
302  return ERR_OK;
303 }
304 
312 static err_t
313 sent_tcp(void *arg, struct tcp_pcb *pcb, u16_t len)
314 {
315  struct netconn *conn = (struct netconn *)arg;
316 
317  LWIP_UNUSED_ARG(pcb);
318  LWIP_ASSERT("conn != NULL", (conn != NULL));
319 
320  if (conn->state == NETCONN_WRITE) {
321  do_writemore(conn);
322  } else if (conn->state == NETCONN_CLOSE) {
323  do_close_internal(conn);
324  }
325 
326  if (conn) {
327  /* If the queued byte- or pbuf-count drops below the configured low-water limit,
328  let select mark this pcb as writable again. */
329  if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT) &&
330  (tcp_sndqueuelen(conn->pcb.tcp) < TCP_SNDQUEUELOWAT)) {
331  conn->flags &= ~NETCONN_FLAG_CHECK_WRITESPACE;
332  API_EVENT(conn, NETCONN_EVT_SENDPLUS, len);
333  }
334  }
335 
336  return ERR_OK;
337 }
338 
346 static void
347 err_tcp(void *arg, err_t err)
348 {
349  struct netconn *conn;
350  enum netconn_state old_state;
352 
353  conn = (struct netconn *)arg;
354  LWIP_ASSERT("conn != NULL", (conn != NULL));
355 
356  conn->pcb.tcp = NULL;
357 
358  /* no check since this is always fatal! */
359  SYS_ARCH_PROTECT(lev);
360  conn->last_err = err;
361  SYS_ARCH_UNPROTECT(lev);
362 
363  /* reset conn->state now before waking up other threads */
364  old_state = conn->state;
365  conn->state = NETCONN_NONE;
366 
367  /* Notify the user layer about a connection error. Used to signal
368  select. */
369  API_EVENT(conn, NETCONN_EVT_ERROR, 0);
370  /* Try to release selects pending on 'read' or 'write', too.
371  They will get an error if they actually try to read or write. */
372  API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
373  API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
374 
375  /* pass NULL-message to recvmbox to wake up pending recv */
376  if (sys_mbox_valid(&conn->recvmbox)) {
377  /* use trypost to prevent deadlock */
378  sys_mbox_trypost(&conn->recvmbox, NULL);
379  }
380  /* pass NULL-message to acceptmbox to wake up pending accept */
381  if (sys_mbox_valid(&conn->acceptmbox)) {
382  /* use trypost to preven deadlock */
383  sys_mbox_trypost(&conn->acceptmbox, NULL);
384  }
385 
386  if ((old_state == NETCONN_WRITE) || (old_state == NETCONN_CLOSE) ||
387  (old_state == NETCONN_CONNECT)) {
388  /* calling do_writemore/do_close_internal is not necessary
389  since the pcb has already been deleted! */
390  int was_nonblocking_connect = IN_NONBLOCKING_CONNECT(conn);
391  SET_NONBLOCKING_CONNECT(conn, 0);
392 
393  if (!was_nonblocking_connect) {
394  /* set error return code */
395  LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
396  conn->current_msg->err = err;
397  conn->current_msg = NULL;
398  /* wake up the waiting task */
399  sys_sem_signal(&conn->op_completed);
400  }
401  } else {
402  LWIP_ASSERT("conn->current_msg == NULL", conn->current_msg == NULL);
403  }
404 }
405 
412 static void
413 setup_tcp(struct netconn *conn)
414 {
415  struct tcp_pcb *pcb;
416 
417  pcb = conn->pcb.tcp;
418  tcp_arg(pcb, conn);
419  tcp_recv(pcb, recv_tcp);
420  tcp_sent(pcb, sent_tcp);
421  tcp_poll(pcb, poll_tcp, 4);
422  tcp_err(pcb, err_tcp);
423 }
424 
431 static err_t
432 accept_function(void *arg, struct tcp_pcb *newpcb, err_t err)
433 {
434  struct netconn *newconn;
435  struct netconn *conn = (struct netconn *)arg;
436 
437  LWIP_DEBUGF(API_MSG_DEBUG, ("accept_function: newpcb->tate: %s\n", tcp_debug_state_str(newpcb->state)));
438 
439  if (!sys_mbox_valid(&conn->acceptmbox)) {
440  LWIP_DEBUGF(API_MSG_DEBUG, ("accept_function: acceptmbox already deleted\n"));
441  return ERR_VAL;
442  }
443 
444  /* We have to set the callback here even though
445  * the new socket is unknown. conn->socket is marked as -1. */
446  newconn = netconn_alloc(conn->type, conn->callback);
447  if (newconn == NULL) {
448  return ERR_MEM;
449  }
450  newconn->pcb.tcp = newpcb;
451  setup_tcp(newconn);
452  /* no protection: when creating the pcb, the netconn is not yet known
453  to the application thread */
454  newconn->last_err = err;
455 
456  if (sys_mbox_trypost(&conn->acceptmbox, newconn) != ERR_OK) {
457  /* When returning != ERR_OK, the pcb is aborted in tcp_process(),
458  so do nothing here! */
459  newconn->pcb.tcp = NULL;
460  /* no need to drain since we know the recvmbox is empty. */
461  sys_mbox_free(&newconn->recvmbox);
462  sys_mbox_set_invalid(&newconn->recvmbox);
463  netconn_free(newconn);
464  return ERR_MEM;
465  } else {
466  /* Register event with callback */
467  API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
468  }
469 
470  return ERR_OK;
471 }
472 #endif /* LWIP_TCP */
473 
481 static void
482 pcb_new(struct api_msg_msg *msg)
483 {
484  LWIP_ASSERT("pcb_new: pcb already allocated", msg->conn->pcb.tcp == NULL);
485 
486  /* Allocate a PCB for this connection */
487  switch(NETCONNTYPE_GROUP(msg->conn->type)) {
488 #if LWIP_RAW
489  case NETCONN_RAW:
490  msg->conn->pcb.raw = raw_new(msg->msg.n.proto);
491  if(msg->conn->pcb.raw == NULL) {
492  msg->err = ERR_MEM;
493  break;
494  }
495  raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
496  break;
497 #endif /* LWIP_RAW */
498 #if LWIP_UDP
499  case NETCONN_UDP:
500  msg->conn->pcb.udp = udp_new();
501  if(msg->conn->pcb.udp == NULL) {
502  msg->err = ERR_MEM;
503  break;
504  }
505 #if LWIP_UDPLITE
506  if (msg->conn->type==NETCONN_UDPLITE) {
507  udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE);
508  }
509 #endif /* LWIP_UDPLITE */
510  if (msg->conn->type==NETCONN_UDPNOCHKSUM) {
511  udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM);
512  }
513  udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
514  break;
515 #endif /* LWIP_UDP */
516 #if LWIP_TCP
517  case NETCONN_TCP:
518  msg->conn->pcb.tcp = tcp_new();
519  if(msg->conn->pcb.tcp == NULL) {
520  msg->err = ERR_MEM;
521  break;
522  }
523  setup_tcp(msg->conn);
524  break;
525 #endif /* LWIP_TCP */
526  default:
527  /* Unsupported netconn type, e.g. protocol disabled */
528  msg->err = ERR_VAL;
529  break;
530  }
531 }
532 
539 void
540 do_newconn(struct api_msg_msg *msg)
541 {
542  msg->err = ERR_OK;
543  if(msg->conn->pcb.tcp == NULL) {
544  pcb_new(msg);
545  }
546  /* Else? This "new" connection already has a PCB allocated. */
547  /* Is this an error condition? Should it be deleted? */
548  /* We currently just are happy and return. */
549 
550  TCPIP_APIMSG_ACK(msg);
551 }
552 
563 struct netconn*
564 netconn_alloc(enum netconn_type t, netconn_callback callback)
565 {
566  struct netconn *conn;
567  int size;
568 
569  conn = (struct netconn *)memp_malloc(MEMP_NETCONN);
570  if (conn == NULL) {
571  return NULL;
572  }
573 
574  conn->last_err = ERR_OK;
575  conn->type = t;
576  conn->pcb.tcp = NULL;
577 
578 #if (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_UDP_RECVMBOX_SIZE) && \
579  (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_TCP_RECVMBOX_SIZE)
581 #else
582  switch(NETCONNTYPE_GROUP(t)) {
583 #if LWIP_RAW
584  case NETCONN_RAW:
586  break;
587 #endif /* LWIP_RAW */
588 #if LWIP_UDP
589  case NETCONN_UDP:
591  break;
592 #endif /* LWIP_UDP */
593 #if LWIP_TCP
594  case NETCONN_TCP:
596  break;
597 #endif /* LWIP_TCP */
598  default:
599  LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0);
600  break;
601  }
602 #endif
603 
604  if (sys_sem_new(&conn->op_completed, 0) != ERR_OK) {
605  memp_free(MEMP_NETCONN, conn);
606  return NULL;
607  }
608  if (sys_mbox_new(&conn->recvmbox, size) != ERR_OK) {
609  sys_sem_free(&conn->op_completed);
610  memp_free(MEMP_NETCONN, conn);
611  return NULL;
612  }
613 
614 #if LWIP_TCP
615  sys_mbox_set_invalid(&conn->acceptmbox);
616 #endif
617  conn->state = NETCONN_NONE;
618 #if LWIP_SOCKET
619  /* initialize socket to -1 since 0 is a valid socket */
620  conn->socket = -1;
621 #endif /* LWIP_SOCKET */
622  conn->callback = callback;
623 #if LWIP_TCP
624  conn->current_msg = NULL;
625  conn->write_offset = 0;
626 #endif /* LWIP_TCP */
627 #if LWIP_SO_RCVTIMEO
628  conn->recv_timeout = 0;
629 #endif /* LWIP_SO_RCVTIMEO */
630 #if LWIP_SO_RCVBUF
631  conn->recv_bufsize = RECV_BUFSIZE_DEFAULT;
632  conn->recv_avail = 0;
633 #endif /* LWIP_SO_RCVBUF */
634  conn->flags = 0;
635  return conn;
636 }
637 
644 void
645 netconn_free(struct netconn *conn)
646 {
647  LWIP_ASSERT("PCB must be deallocated outside this function", conn->pcb.tcp == NULL);
648  LWIP_ASSERT("recvmbox must be deallocated before calling this function",
649  !sys_mbox_valid(&conn->recvmbox));
650 #if LWIP_TCP
651  LWIP_ASSERT("acceptmbox must be deallocated before calling this function",
652  !sys_mbox_valid(&conn->acceptmbox));
653 #endif /* LWIP_TCP */
654 
655  sys_sem_free(&conn->op_completed);
656  sys_sem_set_invalid(&conn->op_completed);
657 
658  memp_free(MEMP_NETCONN, conn);
659 }
660 
669 static void
670 netconn_drain(struct netconn *conn)
671 {
672  void *mem;
673 #if LWIP_TCP
674  struct pbuf *p;
675 #endif /* LWIP_TCP */
676 
677  /* This runs in tcpip_thread, so we don't need to lock against rx packets */
678 
679  /* Delete and drain the recvmbox. */
680  if (sys_mbox_valid(&conn->recvmbox)) {
681  while (sys_mbox_tryfetch(&conn->recvmbox, &mem) != SYS_MBOX_EMPTY) {
682 #if LWIP_TCP
683  if (conn->type == NETCONN_TCP) {
684  if(mem != NULL) {
685  p = (struct pbuf*)mem;
686  /* pcb might be set to NULL already by err_tcp() */
687  if (conn->pcb.tcp != NULL) {
688  tcp_recved(conn->pcb.tcp, p->tot_len);
689  }
690  pbuf_free(p);
691  }
692  } else
693 #endif /* LWIP_TCP */
694  {
695  netbuf_delete((struct netbuf *)mem);
696  }
697  }
698  sys_mbox_free(&conn->recvmbox);
699  sys_mbox_set_invalid(&conn->recvmbox);
700  }
701 
702  /* Delete and drain the acceptmbox. */
703 #if LWIP_TCP
704  if (sys_mbox_valid(&conn->acceptmbox)) {
705  while (sys_mbox_tryfetch(&conn->acceptmbox, &mem) != SYS_MBOX_EMPTY) {
706  struct netconn *newconn = (struct netconn *)mem;
707  /* Only tcp pcbs have an acceptmbox, so no need to check conn->type */
708  /* pcb might be set to NULL already by err_tcp() */
709  if (conn->pcb.tcp != NULL) {
710  tcp_accepted(conn->pcb.tcp);
711  }
712  /* drain recvmbox */
713  netconn_drain(newconn);
714  if (newconn->pcb.tcp != NULL) {
715  tcp_abort(newconn->pcb.tcp);
716  newconn->pcb.tcp = NULL;
717  }
718  netconn_free(newconn);
719  }
720  sys_mbox_free(&conn->acceptmbox);
721  sys_mbox_set_invalid(&conn->acceptmbox);
722  }
723 #endif /* LWIP_TCP */
724 }
725 
726 #if LWIP_TCP
727 
734 static void
735 do_close_internal(struct netconn *conn)
736 {
737  err_t err;
738  u8_t shut, shut_rx, shut_tx, close;
739 
740  LWIP_ASSERT("invalid conn", (conn != NULL));
741  LWIP_ASSERT("this is for tcp netconns only", (conn->type == NETCONN_TCP));
742  LWIP_ASSERT("conn must be in state NETCONN_CLOSE", (conn->state == NETCONN_CLOSE));
743  LWIP_ASSERT("pcb already closed", (conn->pcb.tcp != NULL));
744  LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
745 
746  shut = conn->current_msg->msg.sd.shut;
747  shut_rx = shut & NETCONN_SHUT_RD;
748  shut_tx = shut & NETCONN_SHUT_WR;
749  /* shutting down both ends is the same as closing */
750  close = shut == NETCONN_SHUT_RDWR;
751 
752  /* Set back some callback pointers */
753  if (close) {
754  tcp_arg(conn->pcb.tcp, NULL);
755  }
756  if (conn->pcb.tcp->state == LISTEN) {
757  tcp_accept(conn->pcb.tcp, NULL);
758  } else {
759  /* some callbacks have to be reset if tcp_close is not successful */
760  if (shut_rx) {
761  tcp_recv(conn->pcb.tcp, NULL);
762  tcp_accept(conn->pcb.tcp, NULL);
763  }
764  if (shut_tx) {
765  tcp_sent(conn->pcb.tcp, NULL);
766  }
767  if (close) {
768  tcp_poll(conn->pcb.tcp, NULL, 4);
769  tcp_err(conn->pcb.tcp, NULL);
770  }
771  }
772  /* Try to close the connection */
773  if (shut == NETCONN_SHUT_RDWR) {
774  err = tcp_close(conn->pcb.tcp);
775  } else {
776  err = tcp_shutdown(conn->pcb.tcp, shut & NETCONN_SHUT_RD, shut & NETCONN_SHUT_WR);
777  }
778  if (err == ERR_OK) {
779  /* Closing succeeded */
780  conn->current_msg->err = ERR_OK;
781  conn->current_msg = NULL;
782  conn->state = NETCONN_NONE;
783  /* Set back some callback pointers as conn is going away */
784  conn->pcb.tcp = NULL;
785  /* Trigger select() in socket layer. Make sure everybody notices activity
786  on the connection, error first! */
787  if (close) {
788  API_EVENT(conn, NETCONN_EVT_ERROR, 0);
789  }
790  if (shut_rx) {
791  API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
792  }
793  if (shut_tx) {
794  API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
795  }
796  /* wake up the application task */
797  sys_sem_signal(&conn->op_completed);
798  } else {
799  /* Closing failed, restore some of the callbacks */
800  /* Closing of listen pcb will never fail! */
801  LWIP_ASSERT("Closing a listen pcb may not fail!", (conn->pcb.tcp->state != LISTEN));
802  tcp_sent(conn->pcb.tcp, sent_tcp);
803  tcp_poll(conn->pcb.tcp, poll_tcp, 4);
804  tcp_err(conn->pcb.tcp, err_tcp);
805  tcp_arg(conn->pcb.tcp, conn);
806  /* don't restore recv callback: we don't want to receive any more data */
807  }
808  /* If closing didn't succeed, we get called again either
809  from poll_tcp or from sent_tcp */
810 }
811 #endif /* LWIP_TCP */
812 
819 void
820 do_delconn(struct api_msg_msg *msg)
821 {
822  /* @todo TCP: abort running write/connect? */
823  if ((msg->conn->state != NETCONN_NONE) &&
824  (msg->conn->state != NETCONN_LISTEN) &&
825  (msg->conn->state != NETCONN_CONNECT)) {
826  /* this only happens for TCP netconns */
827  LWIP_ASSERT("msg->conn->type == NETCONN_TCP", msg->conn->type == NETCONN_TCP);
828  msg->err = ERR_INPROGRESS;
829  } else {
830  LWIP_ASSERT("blocking connect in progress",
831  (msg->conn->state != NETCONN_CONNECT) || IN_NONBLOCKING_CONNECT(msg->conn));
832  /* Drain and delete mboxes */
833  netconn_drain(msg->conn);
834 
835  if (msg->conn->pcb.tcp != NULL) {
836 
837  switch (NETCONNTYPE_GROUP(msg->conn->type)) {
838 #if LWIP_RAW
839  case NETCONN_RAW:
840  raw_remove(msg->conn->pcb.raw);
841  break;
842 #endif /* LWIP_RAW */
843 #if LWIP_UDP
844  case NETCONN_UDP:
845  msg->conn->pcb.udp->recv_arg = NULL;
846  udp_remove(msg->conn->pcb.udp);
847  break;
848 #endif /* LWIP_UDP */
849 #if LWIP_TCP
850  case NETCONN_TCP:
851  LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
852  msg->conn->write_offset == 0);
853  msg->conn->state = NETCONN_CLOSE;
854  msg->msg.sd.shut = NETCONN_SHUT_RDWR;
855  msg->conn->current_msg = msg;
856  do_close_internal(msg->conn);
857  /* API_EVENT is called inside do_close_internal, before releasing
858  the application thread, so we can return at this point! */
859  return;
860 #endif /* LWIP_TCP */
861  default:
862  break;
863  }
864  msg->conn->pcb.tcp = NULL;
865  }
866  /* tcp netconns don't come here! */
867 
868  /* @todo: this lets select make the socket readable and writable,
869  which is wrong! errfd instead? */
870  API_EVENT(msg->conn, NETCONN_EVT_RCVPLUS, 0);
871  API_EVENT(msg->conn, NETCONN_EVT_SENDPLUS, 0);
872  }
873  if (sys_sem_valid(&msg->conn->op_completed)) {
874  sys_sem_signal(&msg->conn->op_completed);
875  }
876 }
877 
885 void
886 do_bind(struct api_msg_msg *msg)
887 {
888  if (ERR_IS_FATAL(msg->conn->last_err)) {
889  msg->err = msg->conn->last_err;
890  } else {
891  msg->err = ERR_VAL;
892  if (msg->conn->pcb.tcp != NULL) {
893  switch (NETCONNTYPE_GROUP(msg->conn->type)) {
894 #if LWIP_RAW
895  case NETCONN_RAW:
896  msg->err = raw_bind(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
897  break;
898 #endif /* LWIP_RAW */
899 #if LWIP_UDP
900  case NETCONN_UDP:
901  msg->err = udp_bind(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
902  break;
903 #endif /* LWIP_UDP */
904 #if LWIP_TCP
905  case NETCONN_TCP:
906  msg->err = tcp_bind(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port);
907  break;
908 #endif /* LWIP_TCP */
909  default:
910  break;
911  }
912  }
913  }
914  TCPIP_APIMSG_ACK(msg);
915 }
916 
917 #if LWIP_TCP
918 
924 static err_t
925 do_connected(void *arg, struct tcp_pcb *pcb, err_t err)
926 {
927  struct netconn *conn;
928  int was_blocking;
929 
930  LWIP_UNUSED_ARG(pcb);
931 
932  conn = (struct netconn *)arg;
933 
934  if (conn == NULL) {
935  return ERR_VAL;
936  }
937 
938  LWIP_ASSERT("conn->state == NETCONN_CONNECT", conn->state == NETCONN_CONNECT);
939  LWIP_ASSERT("(conn->current_msg != NULL) || conn->in_non_blocking_connect",
940  (conn->current_msg != NULL) || IN_NONBLOCKING_CONNECT(conn));
941 
942  if (conn->current_msg != NULL) {
943  conn->current_msg->err = err;
944  }
945  if ((conn->type == NETCONN_TCP) && (err == ERR_OK)) {
946  setup_tcp(conn);
947  }
948  was_blocking = !IN_NONBLOCKING_CONNECT(conn);
949  SET_NONBLOCKING_CONNECT(conn, 0);
950  conn->current_msg = NULL;
951  conn->state = NETCONN_NONE;
952  if (!was_blocking) {
954  SYS_ARCH_PROTECT(lev);
955  if (conn->last_err == ERR_INPROGRESS) {
956  conn->last_err = ERR_OK;
957  }
958  SYS_ARCH_UNPROTECT(lev);
959  }
960  API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
961 
962  if (was_blocking) {
963  sys_sem_signal(&conn->op_completed);
964  }
965  return ERR_OK;
966 }
967 #endif /* LWIP_TCP */
968 
976 void
977 do_connect(struct api_msg_msg *msg)
978 {
979  if (msg->conn->pcb.tcp == NULL) {
980  /* This may happen when calling netconn_connect() a second time */
981  msg->err = ERR_CLSD;
982  } else {
983  switch (NETCONNTYPE_GROUP(msg->conn->type)) {
984 #if LWIP_RAW
985  case NETCONN_RAW:
986  msg->err = raw_connect(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
987  break;
988 #endif /* LWIP_RAW */
989 #if LWIP_UDP
990  case NETCONN_UDP:
991  msg->err = udp_connect(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
992  break;
993 #endif /* LWIP_UDP */
994 #if LWIP_TCP
995  case NETCONN_TCP:
996  /* Prevent connect while doing any other action. */
997  if (msg->conn->state != NETCONN_NONE) {
998  msg->err = ERR_ISCONN;
999  } else {
1000  setup_tcp(msg->conn);
1001  msg->err = tcp_connect(msg->conn->pcb.tcp, msg->msg.bc.ipaddr,
1002  msg->msg.bc.port, do_connected);
1003  if (msg->err == ERR_OK) {
1004  u8_t non_blocking = netconn_is_nonblocking(msg->conn);
1005  msg->conn->state = NETCONN_CONNECT;
1006  SET_NONBLOCKING_CONNECT(msg->conn, non_blocking);
1007  if (non_blocking) {
1008  msg->err = ERR_INPROGRESS;
1009  } else {
1010  msg->conn->current_msg = msg;
1011  /* sys_sem_signal() is called from do_connected (or err_tcp()),
1012  * when the connection is established! */
1013  return;
1014  }
1015  }
1016  }
1017  break;
1018 #endif /* LWIP_TCP */
1019  default:
1020  LWIP_ERROR("Invalid netconn type", 0, do{ msg->err = ERR_VAL; }while(0));
1021  break;
1022  }
1023  }
1024  sys_sem_signal(&msg->conn->op_completed);
1025 }
1026 
1034 void
1035 do_disconnect(struct api_msg_msg *msg)
1036 {
1037 #if LWIP_UDP
1038  if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
1039  udp_disconnect(msg->conn->pcb.udp);
1040  msg->err = ERR_OK;
1041  } else
1042 #endif /* LWIP_UDP */
1043  {
1044  msg->err = ERR_VAL;
1045  }
1046  TCPIP_APIMSG_ACK(msg);
1047 }
1048 
1049 #if LWIP_TCP
1050 
1056 void
1057 do_listen(struct api_msg_msg *msg)
1058 {
1059  if (ERR_IS_FATAL(msg->conn->last_err)) {
1060  msg->err = msg->conn->last_err;
1061  } else {
1062  msg->err = ERR_CONN;
1063  if (msg->conn->pcb.tcp != NULL) {
1064  if (msg->conn->type == NETCONN_TCP) {
1065  if (msg->conn->state == NETCONN_NONE) {
1066 #if TCP_LISTEN_BACKLOG
1067  struct tcp_pcb* lpcb = tcp_listen_with_backlog(msg->conn->pcb.tcp, msg->msg.lb.backlog);
1068 #else /* TCP_LISTEN_BACKLOG */
1069  struct tcp_pcb* lpcb = tcp_listen(msg->conn->pcb.tcp);
1070 #endif /* TCP_LISTEN_BACKLOG */
1071  if (lpcb == NULL) {
1072  /* in this case, the old pcb is still allocated */
1073  msg->err = ERR_MEM;
1074  } else {
1075  /* delete the recvmbox and allocate the acceptmbox */
1076  if (sys_mbox_valid(&msg->conn->recvmbox)) {
1078  sys_mbox_free(&msg->conn->recvmbox);
1079  sys_mbox_set_invalid(&msg->conn->recvmbox);
1080  }
1081  msg->err = ERR_OK;
1082  if (!sys_mbox_valid(&msg->conn->acceptmbox)) {
1083  msg->err = sys_mbox_new(&msg->conn->acceptmbox, DEFAULT_ACCEPTMBOX_SIZE);
1084  }
1085  if (msg->err == ERR_OK) {
1086  msg->conn->state = NETCONN_LISTEN;
1087  msg->conn->pcb.tcp = lpcb;
1088  tcp_arg(msg->conn->pcb.tcp, msg->conn);
1089  tcp_accept(msg->conn->pcb.tcp, accept_function);
1090  } else {
1091  /* since the old pcb is already deallocated, free lpcb now */
1092  tcp_close(lpcb);
1093  msg->conn->pcb.tcp = NULL;
1094  }
1095  }
1096  }
1097  }
1098  }
1099  }
1100  TCPIP_APIMSG_ACK(msg);
1101 }
1102 #endif /* LWIP_TCP */
1103 
1110 void
1111 do_send(struct api_msg_msg *msg)
1112 {
1113  if (ERR_IS_FATAL(msg->conn->last_err)) {
1114  msg->err = msg->conn->last_err;
1115  } else {
1116  msg->err = ERR_CONN;
1117  if (msg->conn->pcb.tcp != NULL) {
1118  switch (NETCONNTYPE_GROUP(msg->conn->type)) {
1119 #if LWIP_RAW
1120  case NETCONN_RAW:
1121  if (ip_addr_isany(&msg->msg.b->addr)) {
1122  msg->err = raw_send(msg->conn->pcb.raw, msg->msg.b->p);
1123  } else {
1124  msg->err = raw_sendto(msg->conn->pcb.raw, msg->msg.b->p, &msg->msg.b->addr);
1125  }
1126  break;
1127 #endif
1128 #if LWIP_UDP
1129  case NETCONN_UDP:
1130 #if LWIP_CHECKSUM_ON_COPY
1131  if (ip_addr_isany(&msg->msg.b->addr)) {
1132  msg->err = udp_send_chksum(msg->conn->pcb.udp, msg->msg.b->p,
1133  msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum);
1134  } else {
1135  msg->err = udp_sendto_chksum(msg->conn->pcb.udp, msg->msg.b->p,
1136  &msg->msg.b->addr, msg->msg.b->port,
1137  msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum);
1138  }
1139 #else /* LWIP_CHECKSUM_ON_COPY */
1140  if (ip_addr_isany(&msg->msg.b->addr)) {
1141  msg->err = udp_send(msg->conn->pcb.udp, msg->msg.b->p);
1142  } else {
1143  msg->err = udp_sendto(msg->conn->pcb.udp, msg->msg.b->p, &msg->msg.b->addr, msg->msg.b->port);
1144  }
1145 #endif /* LWIP_CHECKSUM_ON_COPY */
1146  break;
1147 #endif /* LWIP_UDP */
1148  default:
1149  break;
1150  }
1151  }
1152  }
1153  TCPIP_APIMSG_ACK(msg);
1154 }
1155 
1156 #if LWIP_TCP
1157 
1163 void
1164 do_recv(struct api_msg_msg *msg)
1165 {
1166  msg->err = ERR_OK;
1167  if (msg->conn->pcb.tcp != NULL) {
1168  if (msg->conn->type == NETCONN_TCP) {
1169 #if TCP_LISTEN_BACKLOG
1170  if (msg->conn->pcb.tcp->state == LISTEN) {
1171  tcp_accepted(msg->conn->pcb.tcp);
1172  } else
1173 #endif /* TCP_LISTEN_BACKLOG */
1174  {
1175  u32_t remaining = msg->msg.r.len;
1176  do {
1177  u16_t recved = (remaining > 0xffff) ? 0xffff : (u16_t)remaining;
1178  tcp_recved(msg->conn->pcb.tcp, recved);
1179  remaining -= recved;
1180  }while(remaining != 0);
1181  }
1182  }
1183  }
1184  TCPIP_APIMSG_ACK(msg);
1185 }
1186 
1198 static err_t
1199 do_writemore(struct netconn *conn)
1200 {
1201  err_t err = ERR_OK;
1202  void *dataptr;
1203  u16_t len, available;
1204  u8_t write_finished = 0;
1205  size_t diff;
1206  u8_t dontblock = netconn_is_nonblocking(conn) ||
1207  (conn->current_msg->msg.w.apiflags & NETCONN_DONTBLOCK);
1208  u8_t apiflags = conn->current_msg->msg.w.apiflags;
1209 
1210  LWIP_ASSERT("conn != NULL", conn != NULL);
1211  LWIP_ASSERT("conn->state == NETCONN_WRITE", (conn->state == NETCONN_WRITE));
1212  LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
1213  LWIP_ASSERT("conn->pcb.tcp != NULL", conn->pcb.tcp != NULL);
1214  LWIP_ASSERT("conn->write_offset < conn->current_msg->msg.w.len",
1215  conn->write_offset < conn->current_msg->msg.w.len);
1216 
1217  dataptr = (u8_t*)conn->current_msg->msg.w.dataptr + conn->write_offset;
1218  diff = conn->current_msg->msg.w.len - conn->write_offset;
1219  if (diff > 0xffffUL) { /* max_u16_t */
1220  len = 0xffff;
1221 #if LWIP_TCPIP_CORE_LOCKING
1222  conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
1223 #endif
1224  apiflags |= TCP_WRITE_FLAG_MORE;
1225  } else {
1226  len = (u16_t)diff;
1227  }
1228  available = tcp_sndbuf(conn->pcb.tcp);
1229  if (available < len) {
1230  /* don't try to write more than sendbuf */
1231  len = available;
1232 #if LWIP_TCPIP_CORE_LOCKING
1233  conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
1234 #endif
1235  apiflags |= TCP_WRITE_FLAG_MORE;
1236  }
1237  if (dontblock && (len < conn->current_msg->msg.w.len)) {
1238  /* failed to send all data at once -> nonblocking write not possible */
1239  err = ERR_MEM;
1240  }
1241  if (err == ERR_OK) {
1242  LWIP_ASSERT("do_writemore: invalid length!", ((conn->write_offset + len) <= conn->current_msg->msg.w.len));
1243  err = tcp_write(conn->pcb.tcp, dataptr, len, apiflags);
1244  }
1245  if (dontblock && (err == ERR_MEM)) {
1246  /* nonblocking write failed */
1247  write_finished = 1;
1248  err = ERR_WOULDBLOCK;
1249  /* let poll_tcp check writable space to mark the pcb
1250  writable again */
1251  conn->flags |= NETCONN_FLAG_CHECK_WRITESPACE;
1252  /* let select mark this pcb as non-writable. */
1253  API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
1254  } else {
1255  /* if OK or memory error, check available space */
1256  if (((err == ERR_OK) || (err == ERR_MEM)) &&
1257  ((tcp_sndbuf(conn->pcb.tcp) <= TCP_SNDLOWAT) ||
1258  (tcp_sndqueuelen(conn->pcb.tcp) >= TCP_SNDQUEUELOWAT))) {
1259  /* The queued byte- or pbuf-count exceeds the configured low-water limit,
1260  let select mark this pcb as non-writable. */
1261  API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
1262  }
1263 
1264  if (err == ERR_OK) {
1265  conn->write_offset += len;
1266  if (conn->write_offset == conn->current_msg->msg.w.len) {
1267  /* everything was written */
1268  write_finished = 1;
1269  conn->write_offset = 0;
1270  }
1271  tcp_output(conn->pcb.tcp);
1272  } else if (err == ERR_MEM) {
1273  /* If ERR_MEM, we wait for sent_tcp or poll_tcp to be called
1274  we do NOT return to the application thread, since ERR_MEM is
1275  only a temporary error! */
1276 
1277  /* tcp_write returned ERR_MEM, try tcp_output anyway */
1278  tcp_output(conn->pcb.tcp);
1279 
1280  #if LWIP_TCPIP_CORE_LOCKING
1281  conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
1282  #endif
1283  } else {
1284  /* On errors != ERR_MEM, we don't try writing any more but return
1285  the error to the application thread. */
1286  write_finished = 1;
1287  }
1288  }
1289 
1290  if (write_finished) {
1291  /* everything was written: set back connection state
1292  and back to application task */
1293  conn->current_msg->err = err;
1294  conn->current_msg = NULL;
1295  conn->state = NETCONN_NONE;
1296 #if LWIP_TCPIP_CORE_LOCKING
1297  if ((conn->flags & NETCONN_FLAG_WRITE_DELAYED) != 0)
1298 #endif
1299  {
1300  sys_sem_signal(&conn->op_completed);
1301  }
1302  }
1303 #if LWIP_TCPIP_CORE_LOCKING
1304  else
1305  return ERR_MEM;
1306 #endif
1307  return ERR_OK;
1308 }
1309 #endif /* LWIP_TCP */
1310 
1317 void
1318 do_write(struct api_msg_msg *msg)
1319 {
1320  if (ERR_IS_FATAL(msg->conn->last_err)) {
1321  msg->err = msg->conn->last_err;
1322  } else {
1323  if (msg->conn->type == NETCONN_TCP) {
1324 #if LWIP_TCP
1325  if (msg->conn->state != NETCONN_NONE) {
1326  /* netconn is connecting, closing or in blocking write */
1327  msg->err = ERR_INPROGRESS;
1328  } else if (msg->conn->pcb.tcp != NULL) {
1329  msg->conn->state = NETCONN_WRITE;
1330  /* set all the variables used by do_writemore */
1331  LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
1332  msg->conn->write_offset == 0);
1333  LWIP_ASSERT("msg->msg.w.len != 0", msg->msg.w.len != 0);
1334  msg->conn->current_msg = msg;
1335  msg->conn->write_offset = 0;
1336 #if LWIP_TCPIP_CORE_LOCKING
1337  msg->conn->flags &= ~NETCONN_FLAG_WRITE_DELAYED;
1338  if (do_writemore(msg->conn) != ERR_OK) {
1339  LWIP_ASSERT("state!", msg->conn->state == NETCONN_WRITE);
1341  sys_arch_sem_wait(&msg->conn->op_completed, 0);
1342  LOCK_TCPIP_CORE();
1343  LWIP_ASSERT("state!", msg->conn->state == NETCONN_NONE);
1344  }
1345 #else /* LWIP_TCPIP_CORE_LOCKING */
1346  do_writemore(msg->conn);
1347 #endif /* LWIP_TCPIP_CORE_LOCKING */
1348  /* for both cases: if do_writemore was called, don't ACK the APIMSG
1349  since do_writemore ACKs it! */
1350  return;
1351  } else {
1352  msg->err = ERR_CONN;
1353  }
1354 #else /* LWIP_TCP */
1355  msg->err = ERR_VAL;
1356 #endif /* LWIP_TCP */
1357 #if (LWIP_UDP || LWIP_RAW)
1358  } else {
1359  msg->err = ERR_VAL;
1360 #endif /* (LWIP_UDP || LWIP_RAW) */
1361  }
1362  }
1363  TCPIP_APIMSG_ACK(msg);
1364 }
1365 
1372 void
1373 do_getaddr(struct api_msg_msg *msg)
1374 {
1375  if (msg->conn->pcb.ip != NULL) {
1376  *(msg->msg.ad.ipaddr) = (msg->msg.ad.local ? msg->conn->pcb.ip->local_ip :
1377  msg->conn->pcb.ip->remote_ip);
1378 
1379  msg->err = ERR_OK;
1380  switch (NETCONNTYPE_GROUP(msg->conn->type)) {
1381 #if LWIP_RAW
1382  case NETCONN_RAW:
1383  if (msg->msg.ad.local) {
1384  *(msg->msg.ad.port) = msg->conn->pcb.raw->protocol;
1385  } else {
1386  /* return an error as connecting is only a helper for upper layers */
1387  msg->err = ERR_CONN;
1388  }
1389  break;
1390 #endif /* LWIP_RAW */
1391 #if LWIP_UDP
1392  case NETCONN_UDP:
1393  if (msg->msg.ad.local) {
1394  *(msg->msg.ad.port) = msg->conn->pcb.udp->local_port;
1395  } else {
1396  if ((msg->conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0) {
1397  msg->err = ERR_CONN;
1398  } else {
1399  *(msg->msg.ad.port) = msg->conn->pcb.udp->remote_port;
1400  }
1401  }
1402  break;
1403 #endif /* LWIP_UDP */
1404 #if LWIP_TCP
1405  case NETCONN_TCP:
1406  *(msg->msg.ad.port) = (msg->msg.ad.local?msg->conn->pcb.tcp->local_port:msg->conn->pcb.tcp->remote_port);
1407  break;
1408 #endif /* LWIP_TCP */
1409  default:
1410  LWIP_ASSERT("invalid netconn_type", 0);
1411  break;
1412  }
1413  } else {
1414  msg->err = ERR_CONN;
1415  }
1416  TCPIP_APIMSG_ACK(msg);
1417 }
1418 
1425 void
1426 do_close(struct api_msg_msg *msg)
1427 {
1428 #if LWIP_TCP
1429  /* @todo: abort running write/connect? */
1430  if ((msg->conn->state != NETCONN_NONE) && (msg->conn->state != NETCONN_LISTEN)) {
1431  /* this only happens for TCP netconns */
1432  LWIP_ASSERT("msg->conn->type == NETCONN_TCP", msg->conn->type == NETCONN_TCP);
1433  msg->err = ERR_INPROGRESS;
1434  } else if ((msg->conn->pcb.tcp != NULL) && (msg->conn->type == NETCONN_TCP)) {
1435  if ((msg->msg.sd.shut != NETCONN_SHUT_RDWR) && (msg->conn->state == NETCONN_LISTEN)) {
1436  /* LISTEN doesn't support half shutdown */
1437  msg->err = ERR_CONN;
1438  } else {
1439  if (msg->msg.sd.shut & NETCONN_SHUT_RD) {
1440  /* Drain and delete mboxes */
1441  netconn_drain(msg->conn);
1442  }
1443  LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
1444  msg->conn->write_offset == 0);
1445  msg->conn->state = NETCONN_CLOSE;
1446  msg->conn->current_msg = msg;
1447  do_close_internal(msg->conn);
1448  /* for tcp netconns, do_close_internal ACKs the message */
1449  return;
1450  }
1451  } else
1452 #endif /* LWIP_TCP */
1453  {
1454  msg->err = ERR_VAL;
1455  }
1456  sys_sem_signal(&msg->conn->op_completed);
1457 }
1458 
1459 #if LWIP_IGMP
1460 
1466 void
1467 do_join_leave_group(struct api_msg_msg *msg)
1468 {
1469  if (ERR_IS_FATAL(msg->conn->last_err)) {
1470  msg->err = msg->conn->last_err;
1471  } else {
1472  if (msg->conn->pcb.tcp != NULL) {
1473  if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
1474 #if LWIP_UDP
1475  if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
1476  msg->err = igmp_joingroup(msg->msg.jl.netif_addr, msg->msg.jl.multiaddr);
1477  } else {
1478  msg->err = igmp_leavegroup(msg->msg.jl.netif_addr, msg->msg.jl.multiaddr);
1479  }
1480 #endif /* LWIP_UDP */
1481 #if (LWIP_TCP || LWIP_RAW)
1482  } else {
1483  msg->err = ERR_VAL;
1484 #endif /* (LWIP_TCP || LWIP_RAW) */
1485  }
1486  } else {
1487  msg->err = ERR_CONN;
1488  }
1489  }
1490  TCPIP_APIMSG_ACK(msg);
1491 }
1492 #endif /* LWIP_IGMP */
1493 
1494 #if LWIP_DNS
1495 
1500 static void
1501 do_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
1502 {
1503  struct dns_api_msg *msg = (struct dns_api_msg*)arg;
1504 
1505  LWIP_ASSERT("DNS response for wrong host name", strcmp(msg->name, name) == 0);
1506  LWIP_UNUSED_ARG(name);
1507 
1508  if (ipaddr == NULL) {
1509  /* timeout or memory error */
1510  *msg->err = ERR_VAL;
1511  } else {
1512  /* address was resolved */
1513  *msg->err = ERR_OK;
1514  *msg->addr = *ipaddr;
1515  }
1516  /* wake up the application task waiting in netconn_gethostbyname */
1517  sys_sem_signal(msg->sem);
1518 }
1519 
1526 void
1527 do_gethostbyname(void *arg)
1528 {
1529  struct dns_api_msg *msg = (struct dns_api_msg*)arg;
1530 
1531  *msg->err = dns_gethostbyname(msg->name, msg->addr, do_dns_found, msg);
1532  if (*msg->err != ERR_INPROGRESS) {
1533  /* on error or immediate success, wake up the application
1534  * task waiting in netconn_gethostbyname */
1535  sys_sem_signal(msg->sem);
1536  }
1537 }
1538 #endif /* LWIP_DNS */
1539 
1540 #endif /* LWIP_NETCONN */