Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Thu Sep 26 00:44:20 2013 -0500
Revision:
5:3f93dd1d4cb3
Exported program and replaced contents of the repo with the source
to build and debug using keil mdk. Libs NOT upto date are lwip, lwip-sys
and socket. these have newer versions under mbed_official but were starting
from a know working point

Who changed what in which revision?

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