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