Ethernet test for ECE 4180 and others to find your IP address and do a simple HTTP GET request over port 80.

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Committer:
mkersh3
Date:
Thu Apr 04 05:26:09 2013 +0000
Revision:
0:e7ca326e76ee
Ethernet Test for ECE4180 and others to find their IP Address and do a simple HTTP GET request over port 80.

Who changed what in which revision?

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