SAMV71 Xplained Ultra Software Package 1.0

api_msg.c

Go to the documentation of this file.
00001  /**
00002  * @file
00003  * Sequential API Internal module
00004  *
00005  */
00006 
00007 /*
00008  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00009  * All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without modification, 
00012  * are permitted provided that the following conditions are met:
00013  *
00014  * 1. Redistributions of source code must retain the above copyright notice,
00015  *    this list of conditions and the following disclaimer.
00016  * 2. Redistributions in binary form must reproduce the above copyright notice,
00017  *    this list of conditions and the following disclaimer in the documentation
00018  *    and/or other materials provided with the distribution.
00019  * 3. The name of the author may not be used to endorse or promote products
00020  *    derived from this software without specific prior written permission. 
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00023  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00024  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
00025  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00026  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
00027  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
00030  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
00031  * OF SUCH DAMAGE.
00032  *
00033  * This file is part of the lwIP TCP/IP stack.
00034  * 
00035  * Author: Adam Dunkels <adam@sics.se>
00036  *
00037  */
00038 
00039 #include "lwip/opt.h"
00040 
00041 #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
00042 
00043 #include "lwip/api_msg.h"
00044 
00045 #include "lwip/ip.h"
00046 #include "lwip/udp.h"
00047 #include "lwip/tcp.h"
00048 #include "lwip/raw.h"
00049 
00050 #include "lwip/memp.h"
00051 #include "lwip/tcpip.h"
00052 #include "lwip/igmp.h"
00053 #include "lwip/dns.h"
00054 
00055 #include <string.h>
00056 
00057 /* forward declarations */
00058 #if LWIP_TCP
00059 static err_t do_writemore(struct netconn *conn);
00060 static void do_close_internal(struct netconn *conn);
00061 #endif
00062 
00063 #if LWIP_RAW
00064 /**
00065  * Receive callback function for RAW netconns.
00066  * Doesn't 'eat' the packet, only references it and sends it to
00067  * conn->recvmbox
00068  *
00069  * @see raw.h (struct raw_pcb.recv) for parameters and return value
00070  */
00071 static u8_t
00072 recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p,
00073     struct ip_addr *addr)
00074 {
00075   struct pbuf *q;
00076   struct netbuf *buf;
00077   struct netconn *conn;
00078 #if LWIP_SO_RCVBUF
00079   int recv_avail;
00080 #endif /* LWIP_SO_RCVBUF */
00081 
00082   LWIP_UNUSED_ARG(addr);
00083   conn = arg;
00084 
00085 #if LWIP_SO_RCVBUF
00086   SYS_ARCH_GET(conn->recv_avail, recv_avail);
00087   if ((conn != NULL) && (conn->recvmbox != SYS_MBOX_NULL) &&
00088       ((recv_avail + (int)(p->tot_len)) <= conn->recv_bufsize)) {
00089 #else  /* LWIP_SO_RCVBUF */
00090   if ((conn != NULL) && (conn->recvmbox != SYS_MBOX_NULL)) {
00091 #endif /* LWIP_SO_RCVBUF */
00092     /* copy the whole packet into new pbufs */
00093     q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
00094     if(q != NULL) {
00095       if (pbuf_copy(q, p) != ERR_OK) {
00096         pbuf_free(q);
00097         q = NULL;
00098       }
00099     }
00100 
00101     if(q != NULL) {
00102       buf = memp_malloc(MEMP_NETBUF);
00103       if (buf == NULL) {
00104         pbuf_free(q);
00105         return 0;
00106       }
00107 
00108       buf->p = q;
00109       buf->ptr = q;
00110       buf->addr = &(((struct ip_hdr*)(q->payload))->src);
00111       buf->port = pcb->protocol;
00112 
00113       if (sys_mbox_trypost(conn->recvmbox, buf) != ERR_OK) {
00114         netbuf_delete(buf);
00115         return 0;
00116       } else {
00117         SYS_ARCH_INC(conn->recv_avail, q->tot_len);
00118         /* Register event with callback */
00119         API_EVENT(conn, NETCONN_EVT_RCVPLUS, q->tot_len);
00120       }
00121     }
00122   }
00123 
00124   return 0; /* do not eat the packet */
00125 }
00126 #endif /* LWIP_RAW*/
00127 
00128 #if LWIP_UDP
00129 /**
00130  * Receive callback function for UDP netconns.
00131  * Posts the packet to conn->recvmbox or deletes it on memory error.
00132  *
00133  * @see udp.h (struct udp_pcb.recv) for parameters
00134  */
00135 static void
00136 recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
00137    struct ip_addr *addr, u16_t port)
00138 {
00139   struct netbuf *buf;
00140   struct netconn *conn;
00141 #if LWIP_SO_RCVBUF
00142   int recv_avail;
00143 #endif /* LWIP_SO_RCVBUF */
00144 
00145   LWIP_UNUSED_ARG(pcb); /* only used for asserts... */
00146   LWIP_ASSERT("recv_udp must have a pcb argument", pcb != NULL);
00147   LWIP_ASSERT("recv_udp must have an argument", arg != NULL);
00148   conn = arg;
00149   LWIP_ASSERT("recv_udp: recv for wrong pcb!", conn->pcb.udp == pcb);
00150 
00151 #if LWIP_SO_RCVBUF
00152   SYS_ARCH_GET(conn->recv_avail, recv_avail);
00153   if ((conn == NULL) || (conn->recvmbox == SYS_MBOX_NULL) ||
00154       ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize)) {
00155 #else  /* LWIP_SO_RCVBUF */
00156   if ((conn == NULL) || (conn->recvmbox == SYS_MBOX_NULL)) {
00157 #endif /* LWIP_SO_RCVBUF */
00158     pbuf_free(p);
00159     return;
00160   }
00161 
00162   buf = memp_malloc(MEMP_NETBUF);
00163   if (buf == NULL) {
00164     pbuf_free(p);
00165     return;
00166   } else {
00167     buf->p = p;
00168     buf->ptr = p;
00169     buf->addr = addr;
00170     buf->port = port;
00171 #if LWIP_NETBUF_RECVINFO
00172     {
00173       const struct ip_hdr* iphdr = ip_current_header();
00174       /* get the UDP header - always in the first pbuf, ensured by udp_input */
00175       const struct udp_hdr* udphdr = (void*)(((char*)iphdr) + IPH_LEN(iphdr));
00176       buf->toaddr = (struct ip_addr*)&iphdr->dest;
00177       buf->toport = udphdr->dest;
00178     }
00179 #endif /* LWIP_NETBUF_RECVINFO */
00180   }
00181 
00182   if (sys_mbox_trypost(conn->recvmbox, buf) != ERR_OK) {
00183     netbuf_delete(buf);
00184     return;
00185   } else {
00186     SYS_ARCH_INC(conn->recv_avail, p->tot_len);
00187     /* Register event with callback */
00188     API_EVENT(conn, NETCONN_EVT_RCVPLUS, p->tot_len);
00189   }
00190 }
00191 #endif /* LWIP_UDP */
00192 
00193 #if LWIP_TCP
00194 /**
00195  * Receive callback function for TCP netconns.
00196  * Posts the packet to conn->recvmbox, but doesn't delete it on errors.
00197  *
00198  * @see tcp.h (struct tcp_pcb.recv) for parameters and return value
00199  */
00200 static err_t
00201 recv_tcp(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
00202 {
00203   struct netconn *conn;
00204   u16_t len;
00205 
00206   LWIP_UNUSED_ARG(pcb);
00207   LWIP_ASSERT("recv_tcp must have a pcb argument", pcb != NULL);
00208   LWIP_ASSERT("recv_tcp must have an argument", arg != NULL);
00209   conn = arg;
00210   LWIP_ASSERT("recv_tcp: recv for wrong pcb!", conn->pcb.tcp == pcb);
00211 
00212   if ((conn == NULL) || (conn->recvmbox == SYS_MBOX_NULL)) {
00213     return ERR_VAL;
00214   }
00215 
00216   conn->err = err;
00217   if (p != NULL) {
00218     len = p->tot_len;
00219     SYS_ARCH_INC(conn->recv_avail, len);
00220   } else {
00221     len = 0;
00222   }
00223 
00224   if (sys_mbox_trypost(conn->recvmbox, p) != ERR_OK) {
00225     return ERR_MEM;
00226   } else {
00227     /* Register event with callback */
00228     API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
00229   }
00230 
00231   return ERR_OK;
00232 }
00233 
00234 /**
00235  * Poll callback function for TCP netconns.
00236  * Wakes up an application thread that waits for a connection to close
00237  * or data to be sent. The application thread then takes the
00238  * appropriate action to go on.
00239  *
00240  * Signals the conn->sem.
00241  * netconn_close waits for conn->sem if closing failed.
00242  *
00243  * @see tcp.h (struct tcp_pcb.poll) for parameters and return value
00244  */
00245 static err_t
00246 poll_tcp(void *arg, struct tcp_pcb *pcb)
00247 {
00248   struct netconn *conn = arg;
00249 
00250   LWIP_UNUSED_ARG(pcb);
00251   LWIP_ASSERT("conn != NULL", (conn != NULL));
00252 
00253   if (conn->state == NETCONN_WRITE) {
00254     do_writemore(conn);
00255   } else if (conn->state == NETCONN_CLOSE) {
00256     do_close_internal(conn);
00257   }
00258 
00259   return ERR_OK;
00260 }
00261 
00262 /**
00263  * Sent callback function for TCP netconns.
00264  * Signals the conn->sem and calls API_EVENT.
00265  * netconn_write waits for conn->sem if send buffer is low.
00266  *
00267  * @see tcp.h (struct tcp_pcb.sent) for parameters and return value
00268  */
00269 static err_t
00270 sent_tcp(void *arg, struct tcp_pcb *pcb, u16_t len)
00271 {
00272   struct netconn *conn = arg;
00273 
00274   LWIP_UNUSED_ARG(pcb);
00275   LWIP_ASSERT("conn != NULL", (conn != NULL));
00276 
00277   if (conn->state == NETCONN_WRITE) {
00278     LWIP_ASSERT("conn->pcb.tcp != NULL", conn->pcb.tcp != NULL);
00279     do_writemore(conn);
00280   } else if (conn->state == NETCONN_CLOSE) {
00281     do_close_internal(conn);
00282   }
00283 
00284   if (conn) {
00285     if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT)) {
00286       API_EVENT(conn, NETCONN_EVT_SENDPLUS, len);
00287     }
00288   }
00289   
00290   return ERR_OK;
00291 }
00292 
00293 /**
00294  * Error callback function for TCP netconns.
00295  * Signals conn->sem, posts to all conn mboxes and calls API_EVENT.
00296  * The application thread has then to decide what to do.
00297  *
00298  * @see tcp.h (struct tcp_pcb.err) for parameters
00299  */
00300 static void
00301 err_tcp(void *arg, err_t err)
00302 {
00303   struct netconn *conn;
00304 
00305   conn = arg;
00306   LWIP_ASSERT("conn != NULL", (conn != NULL));
00307 
00308   conn->pcb.tcp = NULL;
00309 
00310   conn->err = err;
00311   if (conn->recvmbox != SYS_MBOX_NULL) {
00312     /* Register event with callback */
00313     API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
00314     sys_mbox_post(conn->recvmbox, NULL);
00315   }
00316   if (conn->op_completed != SYS_SEM_NULL && conn->state == NETCONN_CONNECT) {
00317     conn->state = NETCONN_NONE;
00318     sys_sem_signal(conn->op_completed);
00319   }
00320   if (conn->acceptmbox != SYS_MBOX_NULL) {
00321     /* Register event with callback */
00322     API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
00323     sys_mbox_post(conn->acceptmbox, NULL);
00324   }
00325   if ((conn->state == NETCONN_WRITE) || (conn->state == NETCONN_CLOSE)) {
00326     /* calling do_writemore/do_close_internal is not necessary
00327        since the pcb has already been deleted! */
00328     conn->state = NETCONN_NONE;
00329     /* wake up the waiting task */
00330     sys_sem_signal(conn->op_completed);
00331   }
00332 }
00333 
00334 /**
00335  * Setup a tcp_pcb with the correct callback function pointers
00336  * and their arguments.
00337  *
00338  * @param conn the TCP netconn to setup
00339  */
00340 static void
00341 setup_tcp(struct netconn *conn)
00342 {
00343   struct tcp_pcb *pcb;
00344 
00345   pcb = conn->pcb.tcp;
00346   tcp_arg(pcb, conn);
00347   tcp_recv(pcb, recv_tcp);
00348   tcp_sent(pcb, sent_tcp);
00349   tcp_poll(pcb, poll_tcp, 4);
00350   tcp_err(pcb, err_tcp);
00351 }
00352 
00353 /**
00354  * Accept callback function for TCP netconns.
00355  * Allocates a new netconn and posts that to conn->acceptmbox.
00356  *
00357  * @see tcp.h (struct tcp_pcb_listen.accept) for parameters and return value
00358  */
00359 static err_t
00360 accept_function(void *arg, struct tcp_pcb *newpcb, err_t err)
00361 {
00362   struct netconn *newconn;
00363   struct netconn *conn;
00364 
00365 #if API_MSG_DEBUG
00366 #if TCP_DEBUG
00367   tcp_debug_print_state(newpcb->state);
00368 #endif /* TCP_DEBUG */
00369 #endif /* API_MSG_DEBUG */
00370   conn = (struct netconn *)arg;
00371 
00372   LWIP_ERROR("accept_function: invalid conn->acceptmbox",
00373              conn->acceptmbox != SYS_MBOX_NULL, return ERR_VAL;);
00374 
00375   /* We have to set the callback here even though
00376    * the new socket is unknown. conn->socket is marked as -1. */
00377   newconn = netconn_alloc(conn->type, conn->callback);
00378   if (newconn == NULL) {
00379     return ERR_MEM;
00380   }
00381   newconn->pcb.tcp = newpcb;
00382   setup_tcp(newconn);
00383   newconn->err = err;
00384 
00385   if (sys_mbox_trypost(conn->acceptmbox, newconn) != ERR_OK) {
00386     /* When returning != ERR_OK, the connection is aborted in tcp_process(),
00387        so do nothing here! */
00388     newconn->pcb.tcp = NULL;
00389     netconn_free(newconn);
00390     return ERR_MEM;
00391   } else {
00392     /* Register event with callback */
00393     API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
00394   }
00395 
00396   return ERR_OK;
00397 }
00398 #endif /* LWIP_TCP */
00399 
00400 /**
00401  * Create a new pcb of a specific type.
00402  * Called from do_newconn().
00403  *
00404  * @param msg the api_msg_msg describing the connection type
00405  * @return msg->conn->err, but the return value is currently ignored
00406  */
00407 static err_t
00408 pcb_new(struct api_msg_msg *msg)
00409 {
00410    msg->conn->err = ERR_OK;
00411 
00412    LWIP_ASSERT("pcb_new: pcb already allocated", msg->conn->pcb.tcp == NULL);
00413 
00414    /* Allocate a PCB for this connection */
00415    switch(NETCONNTYPE_GROUP(msg->conn->type)) {
00416 #if LWIP_RAW
00417    case NETCONN_RAW:
00418      msg->conn->pcb.raw = raw_new(msg->msg.n.proto);
00419      if(msg->conn->pcb.raw == NULL) {
00420        msg->conn->err = ERR_MEM;
00421        break;
00422      }
00423      raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
00424      break;
00425 #endif /* LWIP_RAW */
00426 #if LWIP_UDP
00427    case NETCONN_UDP:
00428      msg->conn->pcb.udp = udp_new();
00429      if(msg->conn->pcb.udp == NULL) {
00430        msg->conn->err = ERR_MEM;
00431        break;
00432      }
00433 #if LWIP_UDPLITE
00434      if (msg->conn->type==NETCONN_UDPLITE) {
00435        udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE);
00436      }
00437 #endif /* LWIP_UDPLITE */
00438      if (msg->conn->type==NETCONN_UDPNOCHKSUM) {
00439        udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM);
00440      }
00441      udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
00442      break;
00443 #endif /* LWIP_UDP */
00444 #if LWIP_TCP
00445    case NETCONN_TCP:
00446      msg->conn->pcb.tcp = tcp_new();
00447      if(msg->conn->pcb.tcp == NULL) {
00448        msg->conn->err = ERR_MEM;
00449        break;
00450      }
00451      setup_tcp(msg->conn);
00452      break;
00453 #endif /* LWIP_TCP */
00454    default:
00455      /* Unsupported netconn type, e.g. protocol disabled */
00456      msg->conn->err = ERR_VAL;
00457      break;
00458    }
00459 
00460   return msg->conn->err;
00461 }
00462 
00463 /**
00464  * Create a new pcb of a specific type inside a netconn.
00465  * Called from netconn_new_with_proto_and_callback.
00466  *
00467  * @param msg the api_msg_msg describing the connection type
00468  */
00469 void
00470 do_newconn(struct api_msg_msg *msg)
00471 {
00472    if(msg->conn->pcb.tcp == NULL) {
00473      pcb_new(msg);
00474    }
00475    /* Else? This "new" connection already has a PCB allocated. */
00476    /* Is this an error condition? Should it be deleted? */
00477    /* We currently just are happy and return. */
00478 
00479    TCPIP_APIMSG_ACK(msg);
00480 }
00481 
00482 /**
00483  * Create a new netconn (of a specific type) that has a callback function.
00484  * The corresponding pcb is NOT created!
00485  *
00486  * @param t the type of 'connection' to create (@see enum netconn_type)
00487  * @param proto the IP protocol for RAW IP pcbs
00488  * @param callback a function to call on status changes (RX available, TX'ed)
00489  * @return a newly allocated struct netconn or
00490  *         NULL on memory error
00491  */
00492 struct netconn*
00493 netconn_alloc(enum netconn_type t, netconn_callback callback)
00494 {
00495   struct netconn *conn;
00496   int size;
00497 
00498   conn = memp_malloc(MEMP_NETCONN);
00499   if (conn == NULL) {
00500     return NULL;
00501   }
00502 
00503   conn->err = ERR_OK;
00504   conn->type = t;
00505   conn->pcb.tcp = NULL;
00506 
00507 #if (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_UDP_RECVMBOX_SIZE) && \
00508     (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_TCP_RECVMBOX_SIZE)
00509   size = DEFAULT_RAW_RECVMBOX_SIZE;
00510 #else
00511   switch(NETCONNTYPE_GROUP(t)) {
00512 #if LWIP_RAW
00513   case NETCONN_RAW:
00514     size = DEFAULT_RAW_RECVMBOX_SIZE;
00515     break;
00516 #endif /* LWIP_RAW */
00517 #if LWIP_UDP
00518   case NETCONN_UDP:
00519     size = DEFAULT_UDP_RECVMBOX_SIZE;
00520     break;
00521 #endif /* LWIP_UDP */
00522 #if LWIP_TCP
00523   case NETCONN_TCP:
00524     size = DEFAULT_TCP_RECVMBOX_SIZE;
00525     break;
00526 #endif /* LWIP_TCP */
00527   default:
00528     LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0);
00529     break;
00530   }
00531 #endif
00532 
00533   if ((conn->op_completed = sys_sem_new(0)) == SYS_SEM_NULL) {
00534     memp_free(MEMP_NETCONN, conn);
00535     return NULL;
00536   }
00537   if ((conn->recvmbox = sys_mbox_new(size)) == SYS_MBOX_NULL) {
00538     sys_sem_free(conn->op_completed);
00539     memp_free(MEMP_NETCONN, conn);
00540     return NULL;
00541   }
00542 
00543   conn->acceptmbox   = SYS_MBOX_NULL;
00544   conn->state        = NETCONN_NONE;
00545   /* initialize socket to -1 since 0 is a valid socket */
00546   conn->socket       = -1;
00547   conn->callback     = callback;
00548   conn->recv_avail   = 0;
00549 #if LWIP_TCP
00550   conn->write_msg    = NULL;
00551   conn->write_offset = 0;
00552 #if LWIP_TCPIP_CORE_LOCKING
00553   conn->write_delayed = 0;
00554 #endif /* LWIP_TCPIP_CORE_LOCKING */
00555 #endif /* LWIP_TCP */
00556 #if LWIP_SO_RCVTIMEO
00557   conn->recv_timeout = 0;
00558 #endif /* LWIP_SO_RCVTIMEO */
00559 #if LWIP_SO_RCVBUF
00560   conn->recv_bufsize = RECV_BUFSIZE_DEFAULT;
00561 #endif /* LWIP_SO_RCVBUF */
00562   return conn;
00563 }
00564 
00565 /**
00566  * Delete a netconn and all its resources.
00567  * The pcb is NOT freed (since we might not be in the right thread context do this).
00568  *
00569  * @param conn the netconn to free
00570  */
00571 void
00572 netconn_free(struct netconn *conn)
00573 {
00574   void *mem;
00575   LWIP_ASSERT("PCB must be deallocated outside this function", conn->pcb.tcp == NULL);
00576 
00577   /* Drain the recvmbox. */
00578   if (conn->recvmbox != SYS_MBOX_NULL) {
00579     while (sys_mbox_tryfetch(conn->recvmbox, &mem) != SYS_MBOX_EMPTY) {
00580       if (conn->type == NETCONN_TCP) {
00581         if(mem != NULL) {
00582           pbuf_free((struct pbuf *)mem);
00583         }
00584       } else {
00585         netbuf_delete((struct netbuf *)mem);
00586       }
00587     }
00588     sys_mbox_free(conn->recvmbox);
00589     conn->recvmbox = SYS_MBOX_NULL;
00590   }
00591 
00592   /* Drain the acceptmbox. */
00593   if (conn->acceptmbox != SYS_MBOX_NULL) {
00594     while (sys_mbox_tryfetch(conn->acceptmbox, &mem) != SYS_MBOX_EMPTY) {
00595       netconn_delete((struct netconn *)mem);
00596     }
00597     sys_mbox_free(conn->acceptmbox);
00598     conn->acceptmbox = SYS_MBOX_NULL;
00599   }
00600 
00601   sys_sem_free(conn->op_completed);
00602   conn->op_completed = SYS_SEM_NULL;
00603 
00604   memp_free(MEMP_NETCONN, conn);
00605 }
00606 
00607 #if LWIP_TCP
00608 /**
00609  * Internal helper function to close a TCP netconn: since this sometimes
00610  * doesn't work at the first attempt, this function is called from multiple
00611  * places.
00612  *
00613  * @param conn the TCP netconn to close
00614  */
00615 static void
00616 do_close_internal(struct netconn *conn)
00617 {
00618   err_t err;
00619 
00620   LWIP_ASSERT("invalid conn", (conn != NULL));
00621   LWIP_ASSERT("this is for tcp netconns only", (conn->type == NETCONN_TCP));
00622   LWIP_ASSERT("conn must be in state NETCONN_CLOSE", (conn->state == NETCONN_CLOSE));
00623   LWIP_ASSERT("pcb already closed", (conn->pcb.tcp != NULL));
00624 
00625   /* Set back some callback pointers */
00626   tcp_arg(conn->pcb.tcp, NULL);
00627   if (conn->pcb.tcp->state == LISTEN) {
00628     tcp_accept(conn->pcb.tcp, NULL);
00629   } else {
00630     tcp_recv(conn->pcb.tcp, NULL);
00631     tcp_accept(conn->pcb.tcp, NULL);
00632     /* some callbacks have to be reset if tcp_close is not successful */
00633     tcp_sent(conn->pcb.tcp, NULL);
00634     tcp_poll(conn->pcb.tcp, NULL, 4);
00635     tcp_err(conn->pcb.tcp, NULL);
00636   }
00637   /* Try to close the connection */
00638   err = tcp_close(conn->pcb.tcp);
00639   if (err == ERR_OK) {
00640     /* Closing succeeded */
00641     conn->state = NETCONN_NONE;
00642     /* Set back some callback pointers as conn is going away */
00643     conn->pcb.tcp = NULL;
00644     conn->err = ERR_OK;
00645     /* Trigger select() in socket layer. This send should something else so the
00646        errorfd is set, not the read and write fd! */
00647     API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
00648     API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
00649     /* wake up the application task */
00650     sys_sem_signal(conn->op_completed);
00651   } else {
00652     /* Closing failed, restore some of the callbacks */
00653     /* Closing of listen pcb will never fail! */
00654     LWIP_ASSERT("Closing a listen pcb may not fail!", (conn->pcb.tcp->state != LISTEN));
00655     tcp_sent(conn->pcb.tcp, sent_tcp);
00656     tcp_poll(conn->pcb.tcp, poll_tcp, 4);
00657     tcp_err(conn->pcb.tcp, err_tcp);
00658     tcp_arg(conn->pcb.tcp, conn);
00659   }
00660   /* If closing didn't succeed, we get called again either
00661      from poll_tcp or from sent_tcp */
00662 }
00663 #endif /* LWIP_TCP */
00664 
00665 /**
00666  * Delete the pcb inside a netconn.
00667  * Called from netconn_delete.
00668  *
00669  * @param msg the api_msg_msg pointing to the connection
00670  */
00671 void
00672 do_delconn(struct api_msg_msg *msg)
00673 {
00674   if (msg->conn->pcb.tcp != NULL) {
00675     switch (NETCONNTYPE_GROUP(msg->conn->type)) {
00676 #if LWIP_RAW
00677     case NETCONN_RAW:
00678       raw_remove(msg->conn->pcb.raw);
00679       break;
00680 #endif /* LWIP_RAW */
00681 #if LWIP_UDP
00682     case NETCONN_UDP:
00683       msg->conn->pcb.udp->recv_arg = NULL;
00684       udp_remove(msg->conn->pcb.udp);
00685       break;
00686 #endif /* LWIP_UDP */
00687 #if LWIP_TCP
00688     case NETCONN_TCP:
00689       msg->conn->state = NETCONN_CLOSE;
00690       do_close_internal(msg->conn);
00691       /* API_EVENT is called inside do_close_internal, before releasing
00692          the application thread, so we can return at this point! */
00693       return;
00694 #endif /* LWIP_TCP */
00695     default:
00696       break;
00697     }
00698   }
00699   /* tcp netconns don't come here! */
00700 
00701   /* Trigger select() in socket layer. This send should something else so the
00702      errorfd is set, not the read and write fd! */
00703   API_EVENT(msg->conn, NETCONN_EVT_RCVPLUS, 0);
00704   API_EVENT(msg->conn, NETCONN_EVT_SENDPLUS, 0);
00705 
00706   if (msg->conn->op_completed != SYS_SEM_NULL) {
00707     sys_sem_signal(msg->conn->op_completed);
00708   }
00709 }
00710 
00711 /**
00712  * Bind a pcb contained in a netconn
00713  * Called from netconn_bind.
00714  *
00715  * @param msg the api_msg_msg pointing to the connection and containing
00716  *            the IP address and port to bind to
00717  */
00718 void
00719 do_bind(struct api_msg_msg *msg)
00720 {
00721   if (!ERR_IS_FATAL(msg->conn->err)) {
00722     if (msg->conn->pcb.tcp != NULL) {
00723       switch (NETCONNTYPE_GROUP(msg->conn->type)) {
00724 #if LWIP_RAW
00725       case NETCONN_RAW:
00726         msg->conn->err = raw_bind(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
00727         break;
00728 #endif /* LWIP_RAW */
00729 #if LWIP_UDP
00730       case NETCONN_UDP:
00731         msg->conn->err = udp_bind(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
00732         break;
00733 #endif /* LWIP_UDP */
00734 #if LWIP_TCP
00735       case NETCONN_TCP:
00736         msg->conn->err = tcp_bind(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port);
00737         break;
00738 #endif /* LWIP_TCP */
00739       default:
00740         break;
00741       }
00742     } else {
00743       /* msg->conn->pcb is NULL */
00744       msg->conn->err = ERR_VAL;
00745     }
00746   }
00747   TCPIP_APIMSG_ACK(msg);
00748 }
00749 
00750 #if LWIP_TCP
00751 /**
00752  * TCP callback function if a connection (opened by tcp_connect/do_connect) has
00753  * been established (or reset by the remote host).
00754  *
00755  * @see tcp.h (struct tcp_pcb.connected) for parameters and return values
00756  */
00757 static err_t
00758 do_connected(void *arg, struct tcp_pcb *pcb, err_t err)
00759 {
00760   struct netconn *conn;
00761 
00762   LWIP_UNUSED_ARG(pcb);
00763 
00764   conn = arg;
00765 
00766   if (conn == NULL) {
00767     return ERR_VAL;
00768   }
00769 
00770   conn->err = err;
00771   if ((conn->type == NETCONN_TCP) && (err == ERR_OK)) {
00772     setup_tcp(conn);
00773   }
00774   conn->state = NETCONN_NONE;
00775   sys_sem_signal(conn->op_completed);
00776   return ERR_OK;
00777 }
00778 #endif /* LWIP_TCP */
00779 
00780 /**
00781  * Connect a pcb contained inside a netconn
00782  * Called from netconn_connect.
00783  *
00784  * @param msg the api_msg_msg pointing to the connection and containing
00785  *            the IP address and port to connect to
00786  */
00787 void
00788 do_connect(struct api_msg_msg *msg)
00789 {
00790   if (msg->conn->pcb.tcp == NULL) {
00791     sys_sem_signal(msg->conn->op_completed);
00792     return;
00793   }
00794 
00795   switch (NETCONNTYPE_GROUP(msg->conn->type)) {
00796 #if LWIP_RAW
00797   case NETCONN_RAW:
00798     msg->conn->err = raw_connect(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
00799     sys_sem_signal(msg->conn->op_completed);
00800     break;
00801 #endif /* LWIP_RAW */
00802 #if LWIP_UDP
00803   case NETCONN_UDP:
00804     msg->conn->err = udp_connect(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
00805     sys_sem_signal(msg->conn->op_completed);
00806     break;
00807 #endif /* LWIP_UDP */
00808 #if LWIP_TCP
00809   case NETCONN_TCP:
00810     msg->conn->state = NETCONN_CONNECT;
00811     setup_tcp(msg->conn);
00812     msg->conn->err = tcp_connect(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port,
00813                                  do_connected);
00814     /* sys_sem_signal() is called from do_connected (or err_tcp()),
00815      * when the connection is established! */
00816     break;
00817 #endif /* LWIP_TCP */
00818   default:
00819     LWIP_ERROR("Invalid netconn type", 0, do{ msg->conn->err = ERR_VAL;
00820       sys_sem_signal(msg->conn->op_completed); }while(0));
00821     break;
00822   }
00823 }
00824 
00825 /**
00826  * Connect a pcb contained inside a netconn
00827  * Only used for UDP netconns.
00828  * Called from netconn_disconnect.
00829  *
00830  * @param msg the api_msg_msg pointing to the connection to disconnect
00831  */
00832 void
00833 do_disconnect(struct api_msg_msg *msg)
00834 {
00835 #if LWIP_UDP
00836   if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
00837     udp_disconnect(msg->conn->pcb.udp);
00838   }
00839 #endif /* LWIP_UDP */
00840   TCPIP_APIMSG_ACK(msg);
00841 }
00842 
00843 /**
00844  * Set a TCP pcb contained in a netconn into listen mode
00845  * Called from netconn_listen.
00846  *
00847  * @param msg the api_msg_msg pointing to the connection
00848  */
00849 void
00850 do_listen(struct api_msg_msg *msg)
00851 {
00852 #if LWIP_TCP
00853   if (!ERR_IS_FATAL(msg->conn->err)) {
00854     if (msg->conn->pcb.tcp != NULL) {
00855       if (msg->conn->type == NETCONN_TCP) {
00856         if (msg->conn->pcb.tcp->state == CLOSED) {
00857 #if TCP_LISTEN_BACKLOG
00858           struct tcp_pcb* lpcb = tcp_listen_with_backlog(msg->conn->pcb.tcp, msg->msg.lb.backlog);
00859 #else  /* TCP_LISTEN_BACKLOG */
00860           struct tcp_pcb* lpcb = tcp_listen(msg->conn->pcb.tcp);
00861 #endif /* TCP_LISTEN_BACKLOG */
00862           if (lpcb == NULL) {
00863             msg->conn->err = ERR_MEM;
00864           } else {
00865             /* delete the recvmbox and allocate the acceptmbox */
00866             if (msg->conn->recvmbox != SYS_MBOX_NULL) {
00867               /** @todo: should we drain the recvmbox here? */
00868               sys_mbox_free(msg->conn->recvmbox);
00869               msg->conn->recvmbox = SYS_MBOX_NULL;
00870             }
00871             if (msg->conn->acceptmbox == SYS_MBOX_NULL) {
00872               if ((msg->conn->acceptmbox = sys_mbox_new(DEFAULT_ACCEPTMBOX_SIZE)) == SYS_MBOX_NULL) {
00873                 msg->conn->err = ERR_MEM;
00874               }
00875             }
00876             if (msg->conn->err == ERR_OK) {
00877               msg->conn->state = NETCONN_LISTEN;
00878               msg->conn->pcb.tcp = lpcb;
00879               tcp_arg(msg->conn->pcb.tcp, msg->conn);
00880               tcp_accept(msg->conn->pcb.tcp, accept_function);
00881             }
00882           }
00883         } else {
00884           msg->conn->err = ERR_CONN;
00885         }
00886       }
00887     }
00888   }
00889 #endif /* LWIP_TCP */
00890   TCPIP_APIMSG_ACK(msg);
00891 }
00892 
00893 /**
00894  * Send some data on a RAW or UDP pcb contained in a netconn
00895  * Called from netconn_send
00896  *
00897  * @param msg the api_msg_msg pointing to the connection
00898  */
00899 void
00900 do_send(struct api_msg_msg *msg)
00901 {
00902   if (!ERR_IS_FATAL(msg->conn->err)) {
00903     if (msg->conn->pcb.tcp != NULL) {
00904       switch (NETCONNTYPE_GROUP(msg->conn->type)) {
00905 #if LWIP_RAW
00906       case NETCONN_RAW:
00907         if (msg->msg.b->addr == NULL) {
00908           msg->conn->err = raw_send(msg->conn->pcb.raw, msg->msg.b->p);
00909         } else {
00910           msg->conn->err = raw_sendto(msg->conn->pcb.raw, msg->msg.b->p, msg->msg.b->addr);
00911         }
00912         break;
00913 #endif
00914 #if LWIP_UDP
00915       case NETCONN_UDP:
00916         if (msg->msg.b->addr == NULL) {
00917           msg->conn->err = udp_send(msg->conn->pcb.udp, msg->msg.b->p);
00918         } else {
00919           msg->conn->err = udp_sendto(msg->conn->pcb.udp, msg->msg.b->p, msg->msg.b->addr, msg->msg.b->port);
00920         }
00921         break;
00922 #endif /* LWIP_UDP */
00923       default:
00924         break;
00925       }
00926     }
00927   }
00928   TCPIP_APIMSG_ACK(msg);
00929 }
00930 
00931 /**
00932  * Indicate data has been received from a TCP pcb contained in a netconn
00933  * Called from netconn_recv
00934  *
00935  * @param msg the api_msg_msg pointing to the connection
00936  */
00937 void
00938 do_recv(struct api_msg_msg *msg)
00939 {
00940 #if LWIP_TCP
00941   if (!ERR_IS_FATAL(msg->conn->err)) {
00942     if (msg->conn->pcb.tcp != NULL) {
00943       if (msg->conn->type == NETCONN_TCP) {
00944 #if TCP_LISTEN_BACKLOG
00945         if (msg->conn->pcb.tcp->state == LISTEN) {
00946           tcp_accepted(msg->conn->pcb.tcp);
00947         } else
00948 #endif /* TCP_LISTEN_BACKLOG */
00949         {
00950           tcp_recved(msg->conn->pcb.tcp, msg->msg.r.len);
00951         }
00952       }
00953     }
00954   }
00955 #endif /* LWIP_TCP */
00956   TCPIP_APIMSG_ACK(msg);
00957 }
00958 
00959 #if LWIP_TCP
00960 /**
00961  * See if more data needs to be written from a previous call to netconn_write.
00962  * Called initially from do_write. If the first call can't send all data
00963  * (because of low memory or empty send-buffer), this function is called again
00964  * from sent_tcp() or poll_tcp() to send more data. If all data is sent, the
00965  * blocking application thread (waiting in netconn_write) is released.
00966  *
00967  * @param conn netconn (that is currently in state NETCONN_WRITE) to process
00968  * @return ERR_OK
00969  *         ERR_MEM if LWIP_TCPIP_CORE_LOCKING=1 and sending hasn't yet finished
00970  */
00971 static err_t
00972 do_writemore(struct netconn *conn)
00973 {
00974   err_t err;
00975   void *dataptr;
00976   u16_t len, available;
00977   u8_t write_finished = 0;
00978   size_t diff;
00979 
00980   LWIP_ASSERT("conn->state == NETCONN_WRITE", (conn->state == NETCONN_WRITE));
00981 
00982   dataptr = (u8_t*)conn->write_msg->msg.w.dataptr + conn->write_offset;
00983   diff = conn->write_msg->msg.w.len - conn->write_offset;
00984   if (diff > 0xffffUL) { /* max_u16_t */
00985     len = 0xffff;
00986 #if LWIP_TCPIP_CORE_LOCKING
00987     conn->write_delayed = 1;
00988 #endif
00989   } else {
00990     len = (u16_t)diff;
00991   }
00992   available = tcp_sndbuf(conn->pcb.tcp);
00993   if (available < len) {
00994     /* don't try to write more than sendbuf */
00995     len = available;
00996 #if LWIP_TCPIP_CORE_LOCKING
00997     conn->write_delayed = 1;
00998 #endif
00999   }
01000 
01001   err = tcp_write(conn->pcb.tcp, dataptr, len, conn->write_msg->msg.w.apiflags);
01002   LWIP_ASSERT("do_writemore: invalid length!", ((conn->write_offset + len) <= conn->write_msg->msg.w.len));
01003   if (err == ERR_OK) {
01004     conn->write_offset += len;
01005     if (conn->write_offset == conn->write_msg->msg.w.len) {
01006       /* everything was written */
01007       write_finished = 1;
01008       conn->write_msg = NULL;
01009       conn->write_offset = 0;
01010       /* API_EVENT might call tcp_tmr, so reset conn->state now */
01011       conn->state = NETCONN_NONE;
01012     }
01013     err = tcp_output_nagle(conn->pcb.tcp);
01014     conn->err = err;
01015     if ((err == ERR_OK) && (tcp_sndbuf(conn->pcb.tcp) <= TCP_SNDLOWAT)) {
01016       API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
01017     }
01018   } else if (err == ERR_MEM) {
01019     /* If ERR_MEM, we wait for sent_tcp or poll_tcp to be called
01020        we do NOT return to the application thread, since ERR_MEM is
01021        only a temporary error! */
01022 
01023     /* tcp_enqueue returned ERR_MEM, try tcp_output anyway */
01024     err = tcp_output(conn->pcb.tcp);
01025 
01026 #if LWIP_TCPIP_CORE_LOCKING
01027     conn->write_delayed = 1;
01028 #endif
01029   } else {
01030     /* On errors != ERR_MEM, we don't try writing any more but return
01031        the error to the application thread. */
01032     conn->err = err;
01033     write_finished = 1;
01034   }
01035 
01036   if (write_finished) {
01037     /* everything was written: set back connection state
01038        and back to application task */
01039     conn->state = NETCONN_NONE;
01040 #if LWIP_TCPIP_CORE_LOCKING
01041     if (conn->write_delayed != 0)
01042 #endif
01043     {
01044       sys_sem_signal(conn->op_completed);
01045     }
01046   }
01047 #if LWIP_TCPIP_CORE_LOCKING
01048   else
01049     return ERR_MEM;
01050 #endif
01051   return ERR_OK;
01052 }
01053 #endif /* LWIP_TCP */
01054 
01055 /**
01056  * Send some data on a TCP pcb contained in a netconn
01057  * Called from netconn_write
01058  *
01059  * @param msg the api_msg_msg pointing to the connection
01060  */
01061 void
01062 do_write(struct api_msg_msg *msg)
01063 {
01064   if (!ERR_IS_FATAL(msg->conn->err)) {
01065     if ((msg->conn->pcb.tcp != NULL) && (msg->conn->type == NETCONN_TCP)) {
01066 #if LWIP_TCP
01067       msg->conn->state = NETCONN_WRITE;
01068       /* set all the variables used by do_writemore */
01069       LWIP_ASSERT("already writing", msg->conn->write_msg == NULL &&
01070         msg->conn->write_offset == 0);
01071       msg->conn->write_msg = msg;
01072       msg->conn->write_offset = 0;
01073 #if LWIP_TCPIP_CORE_LOCKING
01074       msg->conn->write_delayed = 0;
01075       if (do_writemore(msg->conn) != ERR_OK) {
01076         LWIP_ASSERT("state!", msg->conn->state == NETCONN_WRITE);
01077         UNLOCK_TCPIP_CORE();
01078         sys_arch_sem_wait(msg->conn->op_completed, 0);
01079         LOCK_TCPIP_CORE();
01080         LWIP_ASSERT("state!", msg->conn->state == NETCONN_NONE);
01081       }
01082 #else
01083       do_writemore(msg->conn);
01084 #endif
01085       /* for both cases: if do_writemore was called, don't ACK the APIMSG! */
01086       return;
01087 #endif /* LWIP_TCP */
01088 #if (LWIP_UDP || LWIP_RAW)
01089     } else {
01090       msg->conn->err = ERR_VAL;
01091 #endif /* (LWIP_UDP || LWIP_RAW) */
01092     }
01093   }
01094   TCPIP_APIMSG_ACK(msg);
01095 }
01096 
01097 /**
01098  * Return a connection's local or remote address
01099  * Called from netconn_getaddr
01100  *
01101  * @param msg the api_msg_msg pointing to the connection
01102  */
01103 void
01104 do_getaddr(struct api_msg_msg *msg)
01105 {
01106   if (msg->conn->pcb.ip != NULL) {
01107     *(msg->msg.ad.ipaddr) = (msg->msg.ad.local?msg->conn->pcb.ip->local_ip:msg->conn->pcb.ip->remote_ip);
01108     
01109     switch (NETCONNTYPE_GROUP(msg->conn->type)) {
01110 #if LWIP_RAW
01111     case NETCONN_RAW:
01112       if (msg->msg.ad.local) {
01113         *(msg->msg.ad.port) = msg->conn->pcb.raw->protocol;
01114       } else {
01115         /* return an error as connecting is only a helper for upper layers */
01116         msg->conn->err = ERR_CONN;
01117       }
01118       break;
01119 #endif /* LWIP_RAW */
01120 #if LWIP_UDP
01121     case NETCONN_UDP:
01122       if (msg->msg.ad.local) {
01123         *(msg->msg.ad.port) = msg->conn->pcb.udp->local_port;
01124       } else {
01125         if ((msg->conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0) {
01126           msg->conn->err = ERR_CONN;
01127         } else {
01128           *(msg->msg.ad.port) = msg->conn->pcb.udp->remote_port;
01129         }
01130       }
01131       break;
01132 #endif /* LWIP_UDP */
01133 #if LWIP_TCP
01134     case NETCONN_TCP:
01135       *(msg->msg.ad.port) = (msg->msg.ad.local?msg->conn->pcb.tcp->local_port:msg->conn->pcb.tcp->remote_port);
01136       break;
01137 #endif /* LWIP_TCP */
01138     }
01139   } else {
01140     msg->conn->err = ERR_CONN;
01141   }
01142   TCPIP_APIMSG_ACK(msg);
01143 }
01144 
01145 /**
01146  * Close a TCP pcb contained in a netconn
01147  * Called from netconn_close
01148  *
01149  * @param msg the api_msg_msg pointing to the connection
01150  */
01151 void
01152 do_close(struct api_msg_msg *msg)
01153 {
01154 #if LWIP_TCP
01155   if ((msg->conn->pcb.tcp != NULL) && (msg->conn->type == NETCONN_TCP)) {
01156       msg->conn->state = NETCONN_CLOSE;
01157       do_close_internal(msg->conn);
01158       /* for tcp netconns, do_close_internal ACKs the message */
01159   } else
01160 #endif /* LWIP_TCP */
01161   {
01162     msg->conn->err = ERR_VAL;
01163     sys_sem_signal(msg->conn->op_completed);
01164   }
01165 }
01166 
01167 #if LWIP_IGMP
01168 /**
01169  * Join multicast groups for UDP netconns.
01170  * Called from netconn_join_leave_group
01171  *
01172  * @param msg the api_msg_msg pointing to the connection
01173  */
01174 void
01175 do_join_leave_group(struct api_msg_msg *msg)
01176 { 
01177   if (!ERR_IS_FATAL(msg->conn->err)) {
01178     if (msg->conn->pcb.tcp != NULL) {
01179       if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
01180 #if LWIP_UDP
01181         if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
01182           msg->conn->err = igmp_joingroup(msg->msg.jl.interface, msg->msg.jl.multiaddr);
01183         } else {
01184           msg->conn->err = igmp_leavegroup(msg->msg.jl.interface, msg->msg.jl.multiaddr);
01185         }
01186 #endif /* LWIP_UDP */
01187 #if (LWIP_TCP || LWIP_RAW)
01188       } else {
01189         msg->conn->err = ERR_VAL;
01190 #endif /* (LWIP_TCP || LWIP_RAW) */
01191       }
01192     }
01193   }
01194   TCPIP_APIMSG_ACK(msg);
01195 }
01196 #endif /* LWIP_IGMP */
01197 
01198 #if LWIP_DNS
01199 /**
01200  * Callback function that is called when DNS name is resolved
01201  * (or on timeout). A waiting application thread is waked up by
01202  * signaling the semaphore.
01203  */
01204 static void
01205 do_dns_found(const char *name, struct ip_addr *ipaddr, void *arg)
01206 {
01207   struct dns_api_msg *msg = (struct dns_api_msg*)arg;
01208 
01209   LWIP_ASSERT("DNS response for wrong host name", strcmp(msg->name, name) == 0);
01210 
01211   if (ipaddr == NULL) {
01212     /* timeout or memory error */
01213     *msg->err = ERR_VAL;
01214   } else {
01215     /* address was resolved */
01216     *msg->err = ERR_OK;
01217     *msg->addr = *ipaddr;
01218   }
01219   /* wake up the application task waiting in netconn_gethostbyname */
01220   sys_sem_signal(msg->sem);
01221 }
01222 
01223 /**
01224  * Execute a DNS query
01225  * Called from netconn_gethostbyname
01226  *
01227  * @param arg the dns_api_msg pointing to the query
01228  */
01229 void
01230 do_gethostbyname(void *arg)
01231 {
01232   struct dns_api_msg *msg = (struct dns_api_msg*)arg;
01233 
01234   *msg->err = dns_gethostbyname(msg->name, msg->addr, do_dns_found, msg);
01235   if (*msg->err != ERR_INPROGRESS) {
01236     /* on error or immediate success, wake up the application
01237      * task waiting in netconn_gethostbyname */
01238     sys_sem_signal(msg->sem);
01239   }
01240 }
01241 #endif /* LWIP_DNS */
01242 
01243 #endif /* LWIP_NETCONN */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines