]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ankh/lib/lwip/contrib/src/api/api_msg.c
update
[l4.git] / l4 / pkg / ankh / lib / lwip / contrib / src / api / api_msg.c
1 /**
2  * @file
3  * Sequential API Internal 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 #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 /**
71  * Receive callback function for RAW netconns.
72  * Doesn't 'eat' the packet, only references it and sends it to
73  * conn->recvmbox
74  *
75  * @see raw.h (struct raw_pcb.recv) for parameters and return value
76  */
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 */
97     q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
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;
115       buf->addr = &(((struct ip_hdr*)(q->payload))->src);
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         SYS_ARCH_INC(conn->recv_avail, len);
124         /* Register event with callback */
125         API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
126       }
127     }
128   }
129
130   return 0; /* do not eat the packet */
131 }
132 #endif /* LWIP_RAW*/
133
134 #if LWIP_UDP
135 /**
136  * Receive callback function for UDP netconns.
137  * Posts the packet to conn->recvmbox or deletes it on memory error.
138  *
139  * @see udp.h (struct udp_pcb.recv) for parameters
140  */
141 static void
142 recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
143    ip_addr_t *addr, u16_t port)
144 {
145   struct netbuf *buf;
146   struct netconn *conn;
147   u16_t len;
148 #if LWIP_SO_RCVBUF
149   int recv_avail;
150 #endif /* LWIP_SO_RCVBUF */
151
152   LWIP_UNUSED_ARG(pcb); /* only used for asserts... */
153   LWIP_ASSERT("recv_udp must have a pcb argument", pcb != NULL);
154   LWIP_ASSERT("recv_udp must have an argument", arg != NULL);
155   conn = (struct netconn *)arg;
156   LWIP_ASSERT("recv_udp: recv for wrong pcb!", conn->pcb.udp == pcb);
157
158 #if LWIP_SO_RCVBUF
159   SYS_ARCH_GET(conn->recv_avail, recv_avail);
160   if ((conn == NULL) || !sys_mbox_valid(&conn->recvmbox) ||
161       ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize)) {
162 #else  /* LWIP_SO_RCVBUF */
163   if ((conn == NULL) || !sys_mbox_valid(&conn->recvmbox)) {
164 #endif /* LWIP_SO_RCVBUF */
165     pbuf_free(p);
166     return;
167   }
168
169   buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
170   if (buf == NULL) {
171     pbuf_free(p);
172     return;
173   } else {
174     buf->p = p;
175     buf->ptr = p;
176     buf->addr = addr;
177     buf->port = port;
178 #if LWIP_NETBUF_RECVINFO
179     {
180       const struct ip_hdr* iphdr = ip_current_header();
181       /* get the UDP header - always in the first pbuf, ensured by udp_input */
182       const struct udp_hdr* udphdr = (void*)(((char*)iphdr) + IPH_LEN(iphdr));
183 #if LWIP_CHECKSUM_ON_COPY
184       buf->flags = NETBUF_FLAG_DESTADDR;
185 #endif /* LWIP_CHECKSUM_ON_COPY */
186       buf->toaddr = (ip_addr_t*)&iphdr->dest;
187       buf->toport_chksum = udphdr->dest;
188     }
189 #endif /* LWIP_NETBUF_RECVINFO */
190   }
191
192   len = p->tot_len;
193   if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) {
194     netbuf_delete(buf);
195     return;
196   } else {
197     SYS_ARCH_INC(conn->recv_avail, len);
198     /* Register event with callback */
199     API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
200   }
201 }
202 #endif /* LWIP_UDP */
203
204 #if LWIP_TCP
205 /**
206  * Receive callback function for TCP netconns.
207  * Posts the packet to conn->recvmbox, but doesn't delete it on errors.
208  *
209  * @see tcp.h (struct tcp_pcb.recv) for parameters and return value
210  */
211 static err_t
212 recv_tcp(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
213 {
214   struct netconn *conn;
215   u16_t len;
216
217   LWIP_UNUSED_ARG(pcb);
218   LWIP_ASSERT("recv_tcp must have a pcb argument", pcb != NULL);
219   LWIP_ASSERT("recv_tcp must have an argument", arg != NULL);
220   conn = (struct netconn *)arg;
221   LWIP_ASSERT("recv_tcp: recv for wrong pcb!", conn->pcb.tcp == pcb);
222
223   if (conn == NULL) {
224     return ERR_VAL;
225   }
226   if (!sys_mbox_valid(&conn->recvmbox)) {
227     /* recvmbox already deleted */
228     if (p != NULL) {
229       tcp_recved(pcb, p->tot_len);
230       pbuf_free(p);
231     }
232     return ERR_OK;
233   }
234   /* Unlike for UDP or RAW pcbs, don't check for available space
235      using recv_avail since that could break the connection
236      (data is already ACKed) */
237
238   /* don't overwrite fatal errors! */
239   NETCONN_SET_SAFE_ERR(conn, err);
240
241   if (p != NULL) {
242     len = p->tot_len;
243   } else {
244     len = 0;
245   }
246
247   if (sys_mbox_trypost(&conn->recvmbox, p) != ERR_OK) {
248     /* don't deallocate p: it is presented to us later again from tcp_fasttmr! */
249     return ERR_MEM;
250   } else {
251     SYS_ARCH_INC(conn->recv_avail, len);
252     /* Register event with callback */
253     API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
254   }
255
256   return ERR_OK;
257 }
258
259 /**
260  * Poll callback function for TCP netconns.
261  * Wakes up an application thread that waits for a connection to close
262  * or data to be sent. The application thread then takes the
263  * appropriate action to go on.
264  *
265  * Signals the conn->sem.
266  * netconn_close waits for conn->sem if closing failed.
267  *
268  * @see tcp.h (struct tcp_pcb.poll) for parameters and return value
269  */
270 static err_t
271 poll_tcp(void *arg, struct tcp_pcb *pcb)
272 {
273   struct netconn *conn = (struct netconn *)arg;
274
275   LWIP_UNUSED_ARG(pcb);
276   LWIP_ASSERT("conn != NULL", (conn != NULL));
277
278   if (conn->state == NETCONN_WRITE) {
279     do_writemore(conn);
280   } else if (conn->state == NETCONN_CLOSE) {
281     do_close_internal(conn);
282   }
283   /* @todo: implement connect timeout here? */
284
285   /* Did a nonblocking write fail before? Then check available write-space. */
286   if (conn->flags & NETCONN_FLAG_CHECK_WRITESPACE) {
287     /* If the queued byte- or pbuf-count drops below the configured low-water limit,
288        let select mark this pcb as writable again. */
289     if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT) &&
290       (tcp_sndqueuelen(conn->pcb.tcp) < TCP_SNDQUEUELOWAT)) {
291       conn->flags &= ~NETCONN_FLAG_CHECK_WRITESPACE;
292       API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
293     }
294   }
295
296   return ERR_OK;
297 }
298
299 /**
300  * Sent callback function for TCP netconns.
301  * Signals the conn->sem and calls API_EVENT.
302  * netconn_write waits for conn->sem if send buffer is low.
303  *
304  * @see tcp.h (struct tcp_pcb.sent) for parameters and return value
305  */
306 static err_t
307 sent_tcp(void *arg, struct tcp_pcb *pcb, u16_t len)
308 {
309   struct netconn *conn = (struct netconn *)arg;
310
311   LWIP_UNUSED_ARG(pcb);
312   LWIP_ASSERT("conn != NULL", (conn != NULL));
313
314   if (conn->state == NETCONN_WRITE) {
315     do_writemore(conn);
316   } else if (conn->state == NETCONN_CLOSE) {
317     do_close_internal(conn);
318   }
319
320   if (conn) {
321     /* If the queued byte- or pbuf-count drops below the configured low-water limit,
322        let select mark this pcb as writable again. */
323     if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT) &&
324       (tcp_sndqueuelen(conn->pcb.tcp) < TCP_SNDQUEUELOWAT)) {
325       conn->flags &= ~NETCONN_FLAG_CHECK_WRITESPACE;
326       API_EVENT(conn, NETCONN_EVT_SENDPLUS, len);
327     }
328   }
329   
330   return ERR_OK;
331 }
332
333 /**
334  * Error callback function for TCP netconns.
335  * Signals conn->sem, posts to all conn mboxes and calls API_EVENT.
336  * The application thread has then to decide what to do.
337  *
338  * @see tcp.h (struct tcp_pcb.err) for parameters
339  */
340 static void
341 err_tcp(void *arg, err_t err)
342 {
343   struct netconn *conn;
344   enum netconn_state old_state;
345   SYS_ARCH_DECL_PROTECT(lev);
346
347   conn = (struct netconn *)arg;
348   LWIP_ASSERT("conn != NULL", (conn != NULL));
349
350   conn->pcb.tcp = NULL;
351
352   /* no check since this is always fatal! */
353   SYS_ARCH_PROTECT(lev);
354   conn->last_err = err;
355   SYS_ARCH_UNPROTECT(lev);
356
357   /* reset conn->state now before waking up other threads */
358   old_state = conn->state;
359     conn->state = NETCONN_NONE;
360
361   /* Notify the user layer about a connection error. Used to signal
362      select. */
363   API_EVENT(conn, NETCONN_EVT_ERROR, 0);
364   /* Try to release selects pending on 'read' or 'write', too.
365      They will get an error if they actually try to read or write. */
366     API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
367   API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
368
369   /* pass NULL-message to recvmbox to wake up pending recv */
370   if (sys_mbox_valid(&conn->recvmbox)) {
371     /* use trypost to prevent deadlock */
372     sys_mbox_trypost(&conn->recvmbox, NULL);
373   }
374   /* pass NULL-message to acceptmbox to wake up pending accept */
375   if (sys_mbox_valid(&conn->acceptmbox)) {
376     /* use trypost to preven deadlock */
377     sys_mbox_trypost(&conn->acceptmbox, NULL);
378   }
379
380   if ((old_state == NETCONN_WRITE) || (old_state == NETCONN_CLOSE) ||
381       (old_state == NETCONN_CONNECT)) {
382     /* calling do_writemore/do_close_internal is not necessary
383        since the pcb has already been deleted! */
384     int was_nonblocking_connect = IN_NONBLOCKING_CONNECT(conn);
385     SET_NONBLOCKING_CONNECT(conn, 0);
386
387     if (!was_nonblocking_connect) {
388       /* set error return code */
389       LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
390       conn->current_msg->err = err;
391       conn->current_msg = NULL;
392     /* wake up the waiting task */
393       sys_sem_signal(&conn->op_completed);
394     }
395   } else {
396     LWIP_ASSERT("conn->current_msg == NULL", conn->current_msg == NULL);
397   }
398 }
399
400 /**
401  * Setup a tcp_pcb with the correct callback function pointers
402  * and their arguments.
403  *
404  * @param conn the TCP netconn to setup
405  */
406 static void
407 setup_tcp(struct netconn *conn)
408 {
409   struct tcp_pcb *pcb;
410
411   pcb = conn->pcb.tcp;
412   tcp_arg(pcb, conn);
413   tcp_recv(pcb, recv_tcp);
414   tcp_sent(pcb, sent_tcp);
415   tcp_poll(pcb, poll_tcp, 4);
416   tcp_err(pcb, err_tcp);
417 }
418
419 /**
420  * Accept callback function for TCP netconns.
421  * Allocates a new netconn and posts that to conn->acceptmbox.
422  *
423  * @see tcp.h (struct tcp_pcb_listen.accept) for parameters and return value
424  */
425 static err_t
426 accept_function(void *arg, struct tcp_pcb *newpcb, err_t err)
427 {
428   struct netconn *newconn;
429   struct netconn *conn = (struct netconn *)arg;
430
431   LWIP_DEBUGF(API_MSG_DEBUG, ("accept_function: newpcb->tate: %s\n", tcp_debug_state_str(newpcb->state)));
432
433   if (!sys_mbox_valid(&conn->acceptmbox)) {
434     LWIP_DEBUGF(API_MSG_DEBUG, ("accept_function: acceptmbox already deleted\n"));
435     return ERR_VAL;
436   }
437
438   /* We have to set the callback here even though
439    * the new socket is unknown. conn->socket is marked as -1. */
440   newconn = netconn_alloc(conn->type, conn->callback);
441   if (newconn == NULL) {
442     return ERR_MEM;
443   }
444   newconn->pcb.tcp = newpcb;
445   setup_tcp(newconn);
446   /* no protection: when creating the pcb, the netconn is not yet known
447      to the application thread */
448   newconn->last_err = err;
449
450   if (sys_mbox_trypost(&conn->acceptmbox, newconn) != ERR_OK) {
451     /* When returning != ERR_OK, the pcb is aborted in tcp_process(),
452        so do nothing here! */
453     newconn->pcb.tcp = NULL;
454     /* no need to drain since we know the recvmbox is empty. */
455     sys_mbox_free(&newconn->recvmbox);
456     sys_mbox_set_invalid(&newconn->recvmbox);
457     netconn_free(newconn);
458     return ERR_MEM;
459   } else {
460     /* Register event with callback */
461     API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
462   }
463
464   return ERR_OK;
465 }
466 #endif /* LWIP_TCP */
467
468 /**
469  * Create a new pcb of a specific type.
470  * Called from do_newconn().
471  *
472  * @param msg the api_msg_msg describing the connection type
473  * @return msg->conn->err, but the return value is currently ignored
474  */
475 static void
476 pcb_new(struct api_msg_msg *msg)
477 {
478    LWIP_ASSERT("pcb_new: pcb already allocated", msg->conn->pcb.tcp == NULL);
479
480    /* Allocate a PCB for this connection */
481    switch(NETCONNTYPE_GROUP(msg->conn->type)) {
482 #if LWIP_RAW
483    case NETCONN_RAW:
484      msg->conn->pcb.raw = raw_new(msg->msg.n.proto);
485      if(msg->conn->pcb.raw == NULL) {
486       msg->err = ERR_MEM;
487        break;
488      }
489      raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
490      break;
491 #endif /* LWIP_RAW */
492 #if LWIP_UDP
493    case NETCONN_UDP:
494      msg->conn->pcb.udp = udp_new();
495      if(msg->conn->pcb.udp == NULL) {
496       msg->err = ERR_MEM;
497        break;
498      }
499 #if LWIP_UDPLITE
500      if (msg->conn->type==NETCONN_UDPLITE) {
501        udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE);
502      }
503 #endif /* LWIP_UDPLITE */
504      if (msg->conn->type==NETCONN_UDPNOCHKSUM) {
505        udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM);
506      }
507      udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
508      break;
509 #endif /* LWIP_UDP */
510 #if LWIP_TCP
511    case NETCONN_TCP:
512      msg->conn->pcb.tcp = tcp_new();
513      if(msg->conn->pcb.tcp == NULL) {
514       msg->err = ERR_MEM;
515        break;
516      }
517      setup_tcp(msg->conn);
518      break;
519 #endif /* LWIP_TCP */
520    default:
521      /* Unsupported netconn type, e.g. protocol disabled */
522     msg->err = ERR_VAL;
523      break;
524    }
525 }
526
527 /**
528  * Create a new pcb of a specific type inside a netconn.
529  * Called from netconn_new_with_proto_and_callback.
530  *
531  * @param msg the api_msg_msg describing the connection type
532  */
533 void
534 do_newconn(struct api_msg_msg *msg)
535 {
536   msg->err = ERR_OK;
537    if(msg->conn->pcb.tcp == NULL) {
538      pcb_new(msg);
539    }
540    /* Else? This "new" connection already has a PCB allocated. */
541    /* Is this an error condition? Should it be deleted? */
542    /* We currently just are happy and return. */
543
544    TCPIP_APIMSG_ACK(msg);
545 }
546
547 /**
548  * Create a new netconn (of a specific type) that has a callback function.
549  * The corresponding pcb is NOT created!
550  *
551  * @param t the type of 'connection' to create (@see enum netconn_type)
552  * @param proto the IP protocol for RAW IP pcbs
553  * @param callback a function to call on status changes (RX available, TX'ed)
554  * @return a newly allocated struct netconn or
555  *         NULL on memory error
556  */
557 struct netconn*
558 netconn_alloc(enum netconn_type t, netconn_callback callback)
559 {
560   struct netconn *conn;
561   int size;
562
563   conn = (struct netconn *)memp_malloc(MEMP_NETCONN);
564   if (conn == NULL) {
565     return NULL;
566   }
567
568   conn->last_err = ERR_OK;
569   conn->type = t;
570   conn->pcb.tcp = NULL;
571
572 #if (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_UDP_RECVMBOX_SIZE) && \
573     (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_TCP_RECVMBOX_SIZE)
574   size = DEFAULT_RAW_RECVMBOX_SIZE;
575 #else
576   switch(NETCONNTYPE_GROUP(t)) {
577 #if LWIP_RAW
578   case NETCONN_RAW:
579     size = DEFAULT_RAW_RECVMBOX_SIZE;
580     break;
581 #endif /* LWIP_RAW */
582 #if LWIP_UDP
583   case NETCONN_UDP:
584     size = DEFAULT_UDP_RECVMBOX_SIZE;
585     break;
586 #endif /* LWIP_UDP */
587 #if LWIP_TCP
588   case NETCONN_TCP:
589     size = DEFAULT_TCP_RECVMBOX_SIZE;
590     break;
591 #endif /* LWIP_TCP */
592   default:
593     LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0);
594     break;
595   }
596 #endif
597
598   if (sys_sem_new(&conn->op_completed, 0) != ERR_OK) {
599     memp_free(MEMP_NETCONN, conn);
600     return NULL;
601   }
602   if (sys_mbox_new(&conn->recvmbox, size) != ERR_OK) {
603     sys_sem_free(&conn->op_completed);
604     memp_free(MEMP_NETCONN, conn);
605     return NULL;
606   }
607
608 #if LWIP_TCP
609   sys_mbox_set_invalid(&conn->acceptmbox);
610 #endif
611   conn->state        = NETCONN_NONE;
612 #if LWIP_SOCKET
613   /* initialize socket to -1 since 0 is a valid socket */
614   conn->socket       = -1;
615 #endif /* LWIP_SOCKET */
616   conn->callback     = callback;
617   conn->recv_avail   = 0;
618 #if LWIP_TCP
619   conn->current_msg  = NULL;
620   conn->write_offset = 0;
621 #endif /* LWIP_TCP */
622 #if LWIP_SO_RCVTIMEO
623   conn->recv_timeout = 0;
624 #endif /* LWIP_SO_RCVTIMEO */
625 #if LWIP_SO_RCVBUF
626   conn->recv_bufsize = RECV_BUFSIZE_DEFAULT;
627 #endif /* LWIP_SO_RCVBUF */
628   conn->flags = 0;
629   return conn;
630 }
631
632 /**
633  * Delete a netconn and all its resources.
634  * The pcb is NOT freed (since we might not be in the right thread context do this).
635  *
636  * @param conn the netconn to free
637  */
638 void
639 netconn_free(struct netconn *conn)
640 {
641   LWIP_ASSERT("PCB must be deallocated outside this function", conn->pcb.tcp == NULL);
642   LWIP_ASSERT("recvmbox must be deallocated before calling this function",
643     !sys_mbox_valid(&conn->recvmbox));
644 #if LWIP_TCP
645   LWIP_ASSERT("acceptmbox must be deallocated before calling this function",
646     !sys_mbox_valid(&conn->acceptmbox));
647 #endif /* LWIP_TCP */
648
649   sys_sem_free(&conn->op_completed);
650   sys_sem_set_invalid(&conn->op_completed);
651
652   memp_free(MEMP_NETCONN, conn);
653 }
654
655 /**
656  * Delete rcvmbox and acceptmbox of a netconn and free the left-over data in
657  * these mboxes
658  *
659  * @param conn the netconn to free
660  * @bytes_drained bytes drained from recvmbox
661  * @accepts_drained pending connections drained from acceptmbox
662  */
663 static void
664 netconn_drain(struct netconn *conn)
665 {
666   void *mem;
667 #if LWIP_TCP
668   struct pbuf *p;
669 #endif /* LWIP_TCP */
670
671   /* This runs in tcpip_thread, so we don't need to lock against rx packets */
672
673   /* Delete and drain the recvmbox. */
674   if (sys_mbox_valid(&conn->recvmbox)) {
675     while (sys_mbox_tryfetch(&conn->recvmbox, &mem) != SYS_MBOX_EMPTY) {
676 #if LWIP_TCP
677       if (conn->type == NETCONN_TCP) {
678         if(mem != NULL) {
679           p = (struct pbuf*)mem;
680           /* pcb might be set to NULL already by err_tcp() */
681           if (conn->pcb.tcp != NULL) {
682             tcp_recved(conn->pcb.tcp, p->tot_len);
683         }
684           pbuf_free(p);
685         }
686       } else
687 #endif /* LWIP_TCP */
688       {
689         netbuf_delete((struct netbuf *)mem);
690       }
691     }
692     sys_mbox_free(&conn->recvmbox);
693     sys_mbox_set_invalid(&conn->recvmbox);
694   }
695
696   /* Delete and drain the acceptmbox. */
697 #if LWIP_TCP
698   if (sys_mbox_valid(&conn->acceptmbox)) {
699     while (sys_mbox_tryfetch(&conn->acceptmbox, &mem) != SYS_MBOX_EMPTY) {
700       struct netconn *newconn = (struct netconn *)mem;
701       /* Only tcp pcbs have an acceptmbox, so no need to check conn->type */
702       /* pcb might be set to NULL already by err_tcp() */
703       if (conn->pcb.tcp != NULL) {
704         tcp_accepted(conn->pcb.tcp);
705       }
706       /* drain recvmbox */
707       netconn_drain(newconn);
708       if (newconn->pcb.tcp != NULL) {
709         tcp_abort(newconn->pcb.tcp);
710         newconn->pcb.tcp = NULL;
711     }
712       netconn_free(newconn);
713   }
714     sys_mbox_free(&conn->acceptmbox);
715     sys_mbox_set_invalid(&conn->acceptmbox);
716   }
717 #endif /* LWIP_TCP */
718 }
719
720 #if LWIP_TCP
721 /**
722  * Internal helper function to close a TCP netconn: since this sometimes
723  * doesn't work at the first attempt, this function is called from multiple
724  * places.
725  *
726  * @param conn the TCP netconn to close
727  */
728 static void
729 do_close_internal(struct netconn *conn)
730 {
731   err_t err;
732
733   LWIP_ASSERT("invalid conn", (conn != NULL));
734   LWIP_ASSERT("this is for tcp netconns only", (conn->type == NETCONN_TCP));
735   LWIP_ASSERT("conn must be in state NETCONN_CLOSE", (conn->state == NETCONN_CLOSE));
736   LWIP_ASSERT("pcb already closed", (conn->pcb.tcp != NULL));
737   LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
738
739   /* Set back some callback pointers */
740   tcp_arg(conn->pcb.tcp, NULL);
741   if (conn->pcb.tcp->state == LISTEN) {
742     tcp_accept(conn->pcb.tcp, NULL);
743   } else {
744     tcp_recv(conn->pcb.tcp, NULL);
745     tcp_accept(conn->pcb.tcp, NULL);
746     /* some callbacks have to be reset if tcp_close is not successful */
747     tcp_sent(conn->pcb.tcp, NULL);
748     tcp_poll(conn->pcb.tcp, NULL, 4);
749     tcp_err(conn->pcb.tcp, NULL);
750   }
751   /* Try to close the connection */
752   err = tcp_close(conn->pcb.tcp);
753   if (err == ERR_OK) {
754     /* Closing succeeded */
755     conn->current_msg->err = ERR_OK;
756     conn->current_msg = NULL;
757     conn->state = NETCONN_NONE;
758     /* Set back some callback pointers as conn is going away */
759     conn->pcb.tcp = NULL;
760     /* Trigger select() in socket layer. Make sure everybody notices activity
761        on the connection, error first! */
762     API_EVENT(conn, NETCONN_EVT_ERROR, 0);
763     API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
764     API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
765     /* wake up the application task */
766     sys_sem_signal(&conn->op_completed);
767   } else {
768     /* Closing failed, restore some of the callbacks */
769     /* Closing of listen pcb will never fail! */
770     LWIP_ASSERT("Closing a listen pcb may not fail!", (conn->pcb.tcp->state != LISTEN));
771     tcp_sent(conn->pcb.tcp, sent_tcp);
772     tcp_poll(conn->pcb.tcp, poll_tcp, 4);
773     tcp_err(conn->pcb.tcp, err_tcp);
774     tcp_arg(conn->pcb.tcp, conn);
775   }
776   /* If closing didn't succeed, we get called again either
777      from poll_tcp or from sent_tcp */
778 }
779 #endif /* LWIP_TCP */
780
781 /**
782  * Delete the pcb inside a netconn.
783  * Called from netconn_delete.
784  *
785  * @param msg the api_msg_msg pointing to the connection
786  */
787 void
788 do_delconn(struct api_msg_msg *msg)
789 {
790   /* @todo TCP: abort running write/connect? */
791  if ((msg->conn->state != NETCONN_NONE) &&
792      (msg->conn->state != NETCONN_LISTEN) &&
793      (msg->conn->state != NETCONN_CONNECT)) {
794     /* this only happens for TCP netconns */
795     LWIP_ASSERT("msg->conn->type == NETCONN_TCP", msg->conn->type == NETCONN_TCP);
796     msg->err = ERR_INPROGRESS;
797   } else {
798     LWIP_ASSERT("blocking connect in progress",
799       (msg->conn->state != NETCONN_CONNECT) || IN_NONBLOCKING_CONNECT(msg->conn));
800     /* Drain and delete mboxes */
801     netconn_drain(msg->conn);
802
803   if (msg->conn->pcb.tcp != NULL) {
804
805     switch (NETCONNTYPE_GROUP(msg->conn->type)) {
806 #if LWIP_RAW
807     case NETCONN_RAW:
808       raw_remove(msg->conn->pcb.raw);
809       break;
810 #endif /* LWIP_RAW */
811 #if LWIP_UDP
812     case NETCONN_UDP:
813       msg->conn->pcb.udp->recv_arg = NULL;
814       udp_remove(msg->conn->pcb.udp);
815       break;
816 #endif /* LWIP_UDP */
817 #if LWIP_TCP
818     case NETCONN_TCP:
819         LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
820           msg->conn->write_offset == 0);
821       msg->conn->state = NETCONN_CLOSE;
822         msg->conn->current_msg = msg;
823       do_close_internal(msg->conn);
824       /* API_EVENT is called inside do_close_internal, before releasing
825          the application thread, so we can return at this point! */
826       return;
827 #endif /* LWIP_TCP */
828     default:
829       break;
830     }
831       msg->conn->pcb.tcp = NULL;
832   }
833   /* tcp netconns don't come here! */
834
835     /* @todo: this lets select make the socket readable and writable,
836        which is wrong! errfd instead? */
837   API_EVENT(msg->conn, NETCONN_EVT_RCVPLUS, 0);
838   API_EVENT(msg->conn, NETCONN_EVT_SENDPLUS, 0);
839   }
840   if (sys_sem_valid(&msg->conn->op_completed)) {
841     sys_sem_signal(&msg->conn->op_completed);
842   }
843 }
844
845 /**
846  * Bind a pcb contained in a netconn
847  * Called from netconn_bind.
848  *
849  * @param msg the api_msg_msg pointing to the connection and containing
850  *            the IP address and port to bind to
851  */
852 void
853 do_bind(struct api_msg_msg *msg)
854 {
855   if (ERR_IS_FATAL(msg->conn->last_err)) {
856     msg->err = msg->conn->last_err;
857   } else {
858     msg->err = ERR_VAL;
859     if (msg->conn->pcb.tcp != NULL) {
860       switch (NETCONNTYPE_GROUP(msg->conn->type)) {
861 #if LWIP_RAW
862       case NETCONN_RAW:
863         msg->err = raw_bind(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
864         break;
865 #endif /* LWIP_RAW */
866 #if LWIP_UDP
867       case NETCONN_UDP:
868         msg->err = udp_bind(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
869         break;
870 #endif /* LWIP_UDP */
871 #if LWIP_TCP
872       case NETCONN_TCP:
873         msg->err = tcp_bind(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port);
874         break;
875 #endif /* LWIP_TCP */
876       default:
877         break;
878       }
879     }
880   }
881   TCPIP_APIMSG_ACK(msg);
882 }
883
884 #if LWIP_TCP
885 /**
886  * TCP callback function if a connection (opened by tcp_connect/do_connect) has
887  * been established (or reset by the remote host).
888  *
889  * @see tcp.h (struct tcp_pcb.connected) for parameters and return values
890  */
891 static err_t
892 do_connected(void *arg, struct tcp_pcb *pcb, err_t err)
893 {
894   struct netconn *conn;
895   int was_blocking;
896
897   LWIP_UNUSED_ARG(pcb);
898
899   conn = (struct netconn *)arg;
900
901   if (conn == NULL) {
902     return ERR_VAL;
903   }
904
905   LWIP_ASSERT("conn->state == NETCONN_CONNECT", conn->state == NETCONN_CONNECT);
906   LWIP_ASSERT("(conn->current_msg != NULL) || conn->in_non_blocking_connect",
907     (conn->current_msg != NULL) || IN_NONBLOCKING_CONNECT(conn));
908
909   if (conn->current_msg != NULL) {
910     conn->current_msg->err = err;
911   }
912   if ((conn->type == NETCONN_TCP) && (err == ERR_OK)) {
913     setup_tcp(conn);
914   }
915   was_blocking = !IN_NONBLOCKING_CONNECT(conn);
916   SET_NONBLOCKING_CONNECT(conn, 0);
917   conn->current_msg = NULL;
918   conn->state = NETCONN_NONE;
919   if (!was_blocking) {
920     SYS_ARCH_DECL_PROTECT(lev);
921     SYS_ARCH_PROTECT(lev);
922     if (conn->last_err == ERR_INPROGRESS) {
923       conn->last_err = ERR_OK;
924     }
925     SYS_ARCH_UNPROTECT(lev);
926   }
927   API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
928
929   if (was_blocking) {
930     sys_sem_signal(&conn->op_completed);
931   }
932   return ERR_OK;
933 }
934 #endif /* LWIP_TCP */
935
936 /**
937  * Connect a pcb contained inside a netconn
938  * Called from netconn_connect.
939  *
940  * @param msg the api_msg_msg pointing to the connection and containing
941  *            the IP address and port to connect to
942  */
943 void
944 do_connect(struct api_msg_msg *msg)
945 {
946   if (msg->conn->pcb.tcp == NULL) {
947     /* This may happen when calling netconn_connect() a second time */
948     msg->err = ERR_CLSD;
949   } else {
950   switch (NETCONNTYPE_GROUP(msg->conn->type)) {
951 #if LWIP_RAW
952   case NETCONN_RAW:
953     msg->err = raw_connect(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
954     break;
955 #endif /* LWIP_RAW */
956 #if LWIP_UDP
957   case NETCONN_UDP:
958     msg->err = udp_connect(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
959     break;
960 #endif /* LWIP_UDP */
961 #if LWIP_TCP
962   case NETCONN_TCP:
963     /* Prevent connect while doing any other action. */
964     if (msg->conn->state != NETCONN_NONE) {
965       msg->err = ERR_ISCONN;
966     } else {
967     setup_tcp(msg->conn);
968       msg->err = tcp_connect(msg->conn->pcb.tcp, msg->msg.bc.ipaddr,
969         msg->msg.bc.port, do_connected);
970       if (msg->err == ERR_OK) {
971         u8_t non_blocking = netconn_is_nonblocking(msg->conn);
972         msg->conn->state = NETCONN_CONNECT;
973         SET_NONBLOCKING_CONNECT(msg->conn, non_blocking);
974         if (non_blocking) {
975           msg->err = ERR_INPROGRESS;
976         } else {
977           msg->conn->current_msg = msg;
978     /* sys_sem_signal() is called from do_connected (or err_tcp()),
979      * when the connection is established! */
980           return;
981         }
982       }
983     }
984     break;
985 #endif /* LWIP_TCP */
986   default:
987     LWIP_ERROR("Invalid netconn type", 0, do{ msg->err = ERR_VAL; }while(0));
988     break;
989   }
990   }
991   sys_sem_signal(&msg->conn->op_completed);
992 }
993
994 /**
995  * Connect a pcb contained inside a netconn
996  * Only used for UDP netconns.
997  * Called from netconn_disconnect.
998  *
999  * @param msg the api_msg_msg pointing to the connection to disconnect
1000  */
1001 void
1002 do_disconnect(struct api_msg_msg *msg)
1003 {
1004 #if LWIP_UDP
1005   if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
1006     udp_disconnect(msg->conn->pcb.udp);
1007     msg->err = ERR_OK;
1008   } else
1009 #endif /* LWIP_UDP */
1010   {
1011     msg->err = ERR_VAL;
1012   }
1013   TCPIP_APIMSG_ACK(msg);
1014 }
1015
1016 /**
1017  * Set a TCP pcb contained in a netconn into listen mode
1018  * Called from netconn_listen.
1019  *
1020  * @param msg the api_msg_msg pointing to the connection
1021  */
1022 void
1023 do_listen(struct api_msg_msg *msg)
1024 {
1025 #if LWIP_TCP
1026   if (ERR_IS_FATAL(msg->conn->last_err)) {
1027     msg->err = msg->conn->last_err;
1028   } else {
1029     msg->err = ERR_CONN;
1030     if (msg->conn->pcb.tcp != NULL) {
1031       if (msg->conn->type == NETCONN_TCP) {
1032         if (msg->conn->state == NETCONN_NONE) {
1033 #if TCP_LISTEN_BACKLOG
1034           struct tcp_pcb* lpcb = tcp_listen_with_backlog(msg->conn->pcb.tcp, msg->msg.lb.backlog);
1035 #else  /* TCP_LISTEN_BACKLOG */
1036           struct tcp_pcb* lpcb = tcp_listen(msg->conn->pcb.tcp);
1037 #endif /* TCP_LISTEN_BACKLOG */
1038           if (lpcb == NULL) {
1039             /* in this case, the old pcb is still allocated */
1040             msg->err = ERR_MEM;
1041           } else {
1042             /* delete the recvmbox and allocate the acceptmbox */
1043             if (sys_mbox_valid(&msg->conn->recvmbox)) {
1044               /** @todo: should we drain the recvmbox here? */
1045               sys_mbox_free(&msg->conn->recvmbox);
1046               sys_mbox_set_invalid(&msg->conn->recvmbox);
1047             }
1048             msg->err = ERR_OK;
1049             if (!sys_mbox_valid(&msg->conn->acceptmbox)) {
1050               msg->err = sys_mbox_new(&msg->conn->acceptmbox, DEFAULT_ACCEPTMBOX_SIZE);
1051               }
1052             if (msg->err == ERR_OK) {
1053               msg->conn->state = NETCONN_LISTEN;
1054               msg->conn->pcb.tcp = lpcb;
1055               tcp_arg(msg->conn->pcb.tcp, msg->conn);
1056               tcp_accept(msg->conn->pcb.tcp, accept_function);
1057             } else {
1058               /* since the old pcb is already deallocated, free lpcb now */
1059               tcp_close(lpcb);
1060               msg->conn->pcb.tcp = NULL;
1061             }
1062           }
1063         }
1064       }
1065     }
1066   }
1067 #endif /* LWIP_TCP */
1068   TCPIP_APIMSG_ACK(msg);
1069 }
1070
1071 /**
1072  * Send some data on a RAW or UDP pcb contained in a netconn
1073  * Called from netconn_send
1074  *
1075  * @param msg the api_msg_msg pointing to the connection
1076  */
1077 void
1078 do_send(struct api_msg_msg *msg)
1079 {
1080   if (ERR_IS_FATAL(msg->conn->last_err)) {
1081     msg->err = msg->conn->last_err;
1082   } else {
1083     msg->err = ERR_CONN;
1084     if (msg->conn->pcb.tcp != NULL) {
1085       switch (NETCONNTYPE_GROUP(msg->conn->type)) {
1086 #if LWIP_RAW
1087       case NETCONN_RAW:
1088         if (msg->msg.b->addr == NULL) {
1089           msg->err = raw_send(msg->conn->pcb.raw, msg->msg.b->p);
1090         } else {
1091           msg->err = raw_sendto(msg->conn->pcb.raw, msg->msg.b->p, msg->msg.b->addr);
1092         }
1093         break;
1094 #endif
1095 #if LWIP_UDP
1096       case NETCONN_UDP:
1097 #if LWIP_CHECKSUM_ON_COPY
1098         if (msg->msg.b->addr == NULL) {
1099           msg->err = udp_send_chksum(msg->conn->pcb.udp, msg->msg.b->p,
1100             msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum);
1101         } else {
1102           msg->err = udp_sendto_chksum(msg->conn->pcb.udp, msg->msg.b->p,
1103             msg->msg.b->addr, msg->msg.b->port,
1104             msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum);
1105         }
1106 #else /* LWIP_CHECKSUM_ON_COPY */
1107         if (msg->msg.b->addr == NULL) {
1108           msg->err = udp_send(msg->conn->pcb.udp, msg->msg.b->p);
1109         } else {
1110           msg->err = udp_sendto(msg->conn->pcb.udp, msg->msg.b->p, msg->msg.b->addr, msg->msg.b->port);
1111         }
1112 #endif /* LWIP_CHECKSUM_ON_COPY */
1113         break;
1114 #endif /* LWIP_UDP */
1115       default:
1116         break;
1117       }
1118     }
1119   }
1120   TCPIP_APIMSG_ACK(msg);
1121 }
1122
1123 /**
1124  * Indicate data has been received from a TCP pcb contained in a netconn
1125  * Called from netconn_recv
1126  *
1127  * @param msg the api_msg_msg pointing to the connection
1128  */
1129 void
1130 do_recv(struct api_msg_msg *msg)
1131 {
1132 #if LWIP_TCP
1133   msg->err = ERR_OK;
1134     if (msg->conn->pcb.tcp != NULL) {
1135       if (msg->conn->type == NETCONN_TCP) {
1136 #if TCP_LISTEN_BACKLOG
1137         if (msg->conn->pcb.tcp->state == LISTEN) {
1138           tcp_accepted(msg->conn->pcb.tcp);
1139         } else
1140 #endif /* TCP_LISTEN_BACKLOG */
1141         {
1142         u32_t remaining = msg->msg.r.len;
1143         do {
1144           u16_t recved = (remaining > 0xffff) ? 0xffff : (u16_t)remaining;
1145           tcp_recved(msg->conn->pcb.tcp, recved);
1146           remaining -= recved;
1147         }while(remaining != 0);
1148       }
1149     }
1150   }
1151 #endif /* LWIP_TCP */
1152   TCPIP_APIMSG_ACK(msg);
1153 }
1154
1155 #if LWIP_TCP
1156 /**
1157  * See if more data needs to be written from a previous call to netconn_write.
1158  * Called initially from do_write. If the first call can't send all data
1159  * (because of low memory or empty send-buffer), this function is called again
1160  * from sent_tcp() or poll_tcp() to send more data. If all data is sent, the
1161  * blocking application thread (waiting in netconn_write) is released.
1162  *
1163  * @param conn netconn (that is currently in state NETCONN_WRITE) to process
1164  * @return ERR_OK
1165  *         ERR_MEM if LWIP_TCPIP_CORE_LOCKING=1 and sending hasn't yet finished
1166  */
1167 static err_t
1168 do_writemore(struct netconn *conn)
1169 {
1170   err_t err = ERR_OK;
1171   void *dataptr;
1172   u16_t len, available;
1173   u8_t write_finished = 0;
1174   size_t diff;
1175   u8_t dontblock = netconn_is_nonblocking(conn) ||
1176        (conn->current_msg->msg.w.apiflags & NETCONN_DONTBLOCK);
1177   u8_t apiflags = conn->current_msg->msg.w.apiflags;
1178
1179   LWIP_ASSERT("conn != NULL", conn != NULL);
1180   LWIP_ASSERT("conn->state == NETCONN_WRITE", (conn->state == NETCONN_WRITE));
1181   LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
1182   LWIP_ASSERT("conn->pcb.tcp != NULL", conn->pcb.tcp != NULL);
1183   LWIP_ASSERT("conn->write_offset < conn->current_msg->msg.w.len",
1184     conn->write_offset < conn->current_msg->msg.w.len);
1185
1186   dataptr = (u8_t*)conn->current_msg->msg.w.dataptr + conn->write_offset;
1187   diff = conn->current_msg->msg.w.len - conn->write_offset;
1188   if (diff > 0xffffUL) { /* max_u16_t */
1189     len = 0xffff;
1190 #if LWIP_TCPIP_CORE_LOCKING
1191     conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
1192 #endif
1193     apiflags |= TCP_WRITE_FLAG_MORE;
1194   } else {
1195     len = (u16_t)diff;
1196   }
1197   available = tcp_sndbuf(conn->pcb.tcp);
1198   if (available < len) {
1199     /* don't try to write more than sendbuf */
1200     len = available;
1201 #if LWIP_TCPIP_CORE_LOCKING
1202     conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
1203 #endif
1204     apiflags |= TCP_WRITE_FLAG_MORE;
1205   }
1206   if (dontblock && (len < conn->current_msg->msg.w.len)) {
1207     /* failed to send all data at once -> nonblocking write not possible */
1208     err = ERR_MEM;
1209   }
1210   if (err == ERR_OK) {
1211     LWIP_ASSERT("do_writemore: invalid length!", ((conn->write_offset + len) <= conn->current_msg->msg.w.len));
1212     err = tcp_write(conn->pcb.tcp, dataptr, len, apiflags);
1213   }
1214   if (dontblock && (err == ERR_MEM)) {
1215     /* nonblocking write failed */
1216     write_finished = 1;
1217     err = ERR_WOULDBLOCK;
1218     /* let poll_tcp check writable space to mark the pcb
1219        writable again */
1220     conn->flags |= NETCONN_FLAG_CHECK_WRITESPACE;
1221     /* let select mark this pcb as non-writable. */
1222     API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
1223   } else {
1224     /* if OK or memory error, check available space */
1225     if (((err == ERR_OK) || (err == ERR_MEM)) &&
1226         ((tcp_sndbuf(conn->pcb.tcp) <= TCP_SNDLOWAT) ||
1227          (tcp_sndqueuelen(conn->pcb.tcp) >= TCP_SNDQUEUELOWAT))) {
1228       /* The queued byte- or pbuf-count exceeds the configured low-water limit,
1229          let select mark this pcb as non-writable. */
1230       API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
1231   }
1232
1233   if (err == ERR_OK) {
1234     conn->write_offset += len;
1235       if (conn->write_offset == conn->current_msg->msg.w.len) {
1236       /* everything was written */
1237       write_finished = 1;
1238       conn->write_offset = 0;
1239     }
1240       tcp_output(conn->pcb.tcp);
1241   } else if (err == ERR_MEM) {
1242     /* If ERR_MEM, we wait for sent_tcp or poll_tcp to be called
1243        we do NOT return to the application thread, since ERR_MEM is
1244        only a temporary error! */
1245
1246       /* tcp_write returned ERR_MEM, try tcp_output anyway */
1247       tcp_output(conn->pcb.tcp);
1248
1249   #if LWIP_TCPIP_CORE_LOCKING
1250       conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
1251   #endif
1252   } else {
1253     /* On errors != ERR_MEM, we don't try writing any more but return
1254        the error to the application thread. */
1255     write_finished = 1;
1256   }
1257   }
1258
1259   if (write_finished) {
1260     /* everything was written: set back connection state
1261        and back to application task */
1262     conn->current_msg->err = err;
1263     conn->current_msg = NULL;
1264     conn->state = NETCONN_NONE;
1265 #if LWIP_TCPIP_CORE_LOCKING
1266     if ((conn->flags & NETCONN_FLAG_WRITE_DELAYED) != 0)
1267 #endif
1268     {
1269       sys_sem_signal(&conn->op_completed);
1270     }
1271   }
1272 #if LWIP_TCPIP_CORE_LOCKING
1273   else
1274     return ERR_MEM;
1275 #endif
1276   return ERR_OK;
1277 }
1278 #endif /* LWIP_TCP */
1279
1280 /**
1281  * Send some data on a TCP pcb contained in a netconn
1282  * Called from netconn_write
1283  *
1284  * @param msg the api_msg_msg pointing to the connection
1285  */
1286 void
1287 do_write(struct api_msg_msg *msg)
1288 {
1289   if (ERR_IS_FATAL(msg->conn->last_err)) {
1290     msg->err = msg->conn->last_err;
1291   } else {
1292     if (msg->conn->type == NETCONN_TCP) {
1293 #if LWIP_TCP
1294       if (msg->conn->state != NETCONN_NONE) {
1295         /* netconn is connecting, closing or in blocking write */
1296         msg->err = ERR_INPROGRESS;
1297       } else if (msg->conn->pcb.tcp != NULL) {
1298       msg->conn->state = NETCONN_WRITE;
1299       /* set all the variables used by do_writemore */
1300         LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
1301         msg->conn->write_offset == 0);
1302         LWIP_ASSERT("msg->msg.w.len != 0", msg->msg.w.len != 0);
1303         msg->conn->current_msg = msg;
1304       msg->conn->write_offset = 0;
1305 #if LWIP_TCPIP_CORE_LOCKING
1306         msg->conn->flags &= ~NETCONN_FLAG_WRITE_DELAYED;
1307       if (do_writemore(msg->conn) != ERR_OK) {
1308         LWIP_ASSERT("state!", msg->conn->state == NETCONN_WRITE);
1309         UNLOCK_TCPIP_CORE();
1310           sys_arch_sem_wait(&msg->conn->op_completed, 0);
1311         LOCK_TCPIP_CORE();
1312         LWIP_ASSERT("state!", msg->conn->state == NETCONN_NONE);
1313       }
1314 #else /* LWIP_TCPIP_CORE_LOCKING */
1315       do_writemore(msg->conn);
1316 #endif /* LWIP_TCPIP_CORE_LOCKING */
1317         /* for both cases: if do_writemore was called, don't ACK the APIMSG
1318            since do_writemore ACKs it! */
1319       return;
1320       } else {
1321         msg->err = ERR_CONN;
1322       }
1323 #else /* LWIP_TCP */
1324       msg->err = ERR_VAL;
1325 #endif /* LWIP_TCP */
1326 #if (LWIP_UDP || LWIP_RAW)
1327     } else {
1328       msg->err = ERR_VAL;
1329 #endif /* (LWIP_UDP || LWIP_RAW) */
1330     }
1331   }
1332   TCPIP_APIMSG_ACK(msg);
1333 }
1334
1335 /**
1336  * Return a connection's local or remote address
1337  * Called from netconn_getaddr
1338  *
1339  * @param msg the api_msg_msg pointing to the connection
1340  */
1341 void
1342 do_getaddr(struct api_msg_msg *msg)
1343 {
1344   if (msg->conn->pcb.ip != NULL) {
1345     *(msg->msg.ad.ipaddr) = (msg->msg.ad.local ? msg->conn->pcb.ip->local_ip :
1346                              msg->conn->pcb.ip->remote_ip);
1347     
1348     msg->err = ERR_OK;
1349     switch (NETCONNTYPE_GROUP(msg->conn->type)) {
1350 #if LWIP_RAW
1351     case NETCONN_RAW:
1352       if (msg->msg.ad.local) {
1353         *(msg->msg.ad.port) = msg->conn->pcb.raw->protocol;
1354       } else {
1355         /* return an error as connecting is only a helper for upper layers */
1356         msg->err = ERR_CONN;
1357       }
1358       break;
1359 #endif /* LWIP_RAW */
1360 #if LWIP_UDP
1361     case NETCONN_UDP:
1362       if (msg->msg.ad.local) {
1363         *(msg->msg.ad.port) = msg->conn->pcb.udp->local_port;
1364       } else {
1365         if ((msg->conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0) {
1366           msg->err = ERR_CONN;
1367         } else {
1368           *(msg->msg.ad.port) = msg->conn->pcb.udp->remote_port;
1369         }
1370       }
1371       break;
1372 #endif /* LWIP_UDP */
1373 #if LWIP_TCP
1374     case NETCONN_TCP:
1375       *(msg->msg.ad.port) = (msg->msg.ad.local?msg->conn->pcb.tcp->local_port:msg->conn->pcb.tcp->remote_port);
1376       break;
1377 #endif /* LWIP_TCP */
1378     default:
1379       LWIP_ASSERT("invalid netconn_type", 0);
1380       break;
1381     }
1382   } else {
1383     msg->err = ERR_CONN;
1384   }
1385   TCPIP_APIMSG_ACK(msg);
1386 }
1387
1388 /**
1389  * Close a TCP pcb contained in a netconn
1390  * Called from netconn_close
1391  *
1392  * @param msg the api_msg_msg pointing to the connection
1393  */
1394 void
1395 do_close(struct api_msg_msg *msg)
1396 {
1397 #if LWIP_TCP
1398   /* @todo: abort running write/connect? */
1399   if ((msg->conn->state != NETCONN_NONE) && (msg->conn->state != NETCONN_LISTEN)) {
1400     /* this only happens for TCP netconns */
1401     LWIP_ASSERT("msg->conn->type == NETCONN_TCP", msg->conn->type == NETCONN_TCP);
1402     msg->err = ERR_INPROGRESS;
1403   } else if ((msg->conn->pcb.tcp != NULL) && (msg->conn->type == NETCONN_TCP)) {
1404     /* Drain and delete mboxes */
1405     netconn_drain(msg->conn);
1406     LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
1407       msg->conn->write_offset == 0);
1408       msg->conn->state = NETCONN_CLOSE;
1409     msg->conn->current_msg = msg;
1410       do_close_internal(msg->conn);
1411       /* for tcp netconns, do_close_internal ACKs the message */
1412     return;
1413   } else
1414 #endif /* LWIP_TCP */
1415   {
1416     msg->err = ERR_VAL;
1417   }
1418   sys_sem_signal(&msg->conn->op_completed);
1419 }
1420
1421 #if LWIP_IGMP
1422 /**
1423  * Join multicast groups for UDP netconns.
1424  * Called from netconn_join_leave_group
1425  *
1426  * @param msg the api_msg_msg pointing to the connection
1427  */
1428 void
1429 do_join_leave_group(struct api_msg_msg *msg)
1430
1431   if (ERR_IS_FATAL(msg->conn->last_err)) {
1432     msg->err = msg->conn->last_err;
1433   } else {
1434     if (msg->conn->pcb.tcp != NULL) {
1435       if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
1436 #if LWIP_UDP
1437         if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
1438           msg->err = igmp_joingroup(msg->msg.jl.netif_addr, msg->msg.jl.multiaddr);
1439         } else {
1440           msg->err = igmp_leavegroup(msg->msg.jl.netif_addr, msg->msg.jl.multiaddr);
1441         }
1442 #endif /* LWIP_UDP */
1443 #if (LWIP_TCP || LWIP_RAW)
1444       } else {
1445         msg->err = ERR_VAL;
1446 #endif /* (LWIP_TCP || LWIP_RAW) */
1447       }
1448     } else {
1449       msg->err = ERR_CONN;
1450     }
1451   }
1452   TCPIP_APIMSG_ACK(msg);
1453 }
1454 #endif /* LWIP_IGMP */
1455
1456 #if LWIP_DNS
1457 /**
1458  * Callback function that is called when DNS name is resolved
1459  * (or on timeout). A waiting application thread is waked up by
1460  * signaling the semaphore.
1461  */
1462 static void
1463 do_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
1464 {
1465   struct dns_api_msg *msg = (struct dns_api_msg*)arg;
1466
1467   LWIP_ASSERT("DNS response for wrong host name", strcmp(msg->name, name) == 0);
1468   LWIP_UNUSED_ARG(name);
1469
1470   if (ipaddr == NULL) {
1471     /* timeout or memory error */
1472     *msg->err = ERR_VAL;
1473   } else {
1474     /* address was resolved */
1475     *msg->err = ERR_OK;
1476     *msg->addr = *ipaddr;
1477   }
1478   /* wake up the application task waiting in netconn_gethostbyname */
1479   sys_sem_signal(msg->sem);
1480 }
1481
1482 /**
1483  * Execute a DNS query
1484  * Called from netconn_gethostbyname
1485  *
1486  * @param arg the dns_api_msg pointing to the query
1487  */
1488 void
1489 do_gethostbyname(void *arg)
1490 {
1491   struct dns_api_msg *msg = (struct dns_api_msg*)arg;
1492
1493   *msg->err = dns_gethostbyname(msg->name, msg->addr, do_dns_found, msg);
1494   if (*msg->err != ERR_INPROGRESS) {
1495     /* on error or immediate success, wake up the application
1496      * task waiting in netconn_gethostbyname */
1497     sys_sem_signal(msg->sem);
1498   }
1499 }
1500 #endif /* LWIP_DNS */
1501
1502 #endif /* LWIP_NETCONN */