Ethernetwebsoc

Dependencies:   C12832_lcd LM75B WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
GordonSin
Date:
Fri May 31 04:09:54 2013 +0000
Revision:
0:0ed2a7c7190c
31/5/2013;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GordonSin 0:0ed2a7c7190c 1 /**
GordonSin 0:0ed2a7c7190c 2 * @file
GordonSin 0:0ed2a7c7190c 3 * Sequential API External module
GordonSin 0:0ed2a7c7190c 4 *
GordonSin 0:0ed2a7c7190c 5 */
GordonSin 0:0ed2a7c7190c 6
GordonSin 0:0ed2a7c7190c 7 /*
GordonSin 0:0ed2a7c7190c 8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
GordonSin 0:0ed2a7c7190c 9 * All rights reserved.
GordonSin 0:0ed2a7c7190c 10 *
GordonSin 0:0ed2a7c7190c 11 * Redistribution and use in source and binary forms, with or without modification,
GordonSin 0:0ed2a7c7190c 12 * are permitted provided that the following conditions are met:
GordonSin 0:0ed2a7c7190c 13 *
GordonSin 0:0ed2a7c7190c 14 * 1. Redistributions of source code must retain the above copyright notice,
GordonSin 0:0ed2a7c7190c 15 * this list of conditions and the following disclaimer.
GordonSin 0:0ed2a7c7190c 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
GordonSin 0:0ed2a7c7190c 17 * this list of conditions and the following disclaimer in the documentation
GordonSin 0:0ed2a7c7190c 18 * and/or other materials provided with the distribution.
GordonSin 0:0ed2a7c7190c 19 * 3. The name of the author may not be used to endorse or promote products
GordonSin 0:0ed2a7c7190c 20 * derived from this software without specific prior written permission.
GordonSin 0:0ed2a7c7190c 21 *
GordonSin 0:0ed2a7c7190c 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
GordonSin 0:0ed2a7c7190c 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
GordonSin 0:0ed2a7c7190c 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
GordonSin 0:0ed2a7c7190c 25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
GordonSin 0:0ed2a7c7190c 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
GordonSin 0:0ed2a7c7190c 27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
GordonSin 0:0ed2a7c7190c 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
GordonSin 0:0ed2a7c7190c 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
GordonSin 0:0ed2a7c7190c 30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
GordonSin 0:0ed2a7c7190c 31 * OF SUCH DAMAGE.
GordonSin 0:0ed2a7c7190c 32 *
GordonSin 0:0ed2a7c7190c 33 * This file is part of the lwIP TCP/IP stack.
GordonSin 0:0ed2a7c7190c 34 *
GordonSin 0:0ed2a7c7190c 35 * Author: Adam Dunkels <adam@sics.se>
GordonSin 0:0ed2a7c7190c 36 *
GordonSin 0:0ed2a7c7190c 37 */
GordonSin 0:0ed2a7c7190c 38
GordonSin 0:0ed2a7c7190c 39 /* This is the part of the API that is linked with
GordonSin 0:0ed2a7c7190c 40 the application */
GordonSin 0:0ed2a7c7190c 41
GordonSin 0:0ed2a7c7190c 42 #include "lwip/opt.h"
GordonSin 0:0ed2a7c7190c 43
GordonSin 0:0ed2a7c7190c 44 #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
GordonSin 0:0ed2a7c7190c 45
GordonSin 0:0ed2a7c7190c 46 #include "lwip/api.h"
GordonSin 0:0ed2a7c7190c 47 #include "lwip/tcpip.h"
GordonSin 0:0ed2a7c7190c 48 #include "lwip/memp.h"
GordonSin 0:0ed2a7c7190c 49
GordonSin 0:0ed2a7c7190c 50 #include "lwip/ip.h"
GordonSin 0:0ed2a7c7190c 51 #include "lwip/raw.h"
GordonSin 0:0ed2a7c7190c 52 #include "lwip/udp.h"
GordonSin 0:0ed2a7c7190c 53 #include "lwip/tcp.h"
GordonSin 0:0ed2a7c7190c 54
GordonSin 0:0ed2a7c7190c 55 #include <string.h>
GordonSin 0:0ed2a7c7190c 56
GordonSin 0:0ed2a7c7190c 57 /**
GordonSin 0:0ed2a7c7190c 58 * Create a new netconn (of a specific type) that has a callback function.
GordonSin 0:0ed2a7c7190c 59 * The corresponding pcb is also created.
GordonSin 0:0ed2a7c7190c 60 *
GordonSin 0:0ed2a7c7190c 61 * @param t the type of 'connection' to create (@see enum netconn_type)
GordonSin 0:0ed2a7c7190c 62 * @param proto the IP protocol for RAW IP pcbs
GordonSin 0:0ed2a7c7190c 63 * @param callback a function to call on status changes (RX available, TX'ed)
GordonSin 0:0ed2a7c7190c 64 * @return a newly allocated struct netconn or
GordonSin 0:0ed2a7c7190c 65 * NULL on memory error
GordonSin 0:0ed2a7c7190c 66 */
GordonSin 0:0ed2a7c7190c 67 struct netconn*
GordonSin 0:0ed2a7c7190c 68 netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto, netconn_callback callback)
GordonSin 0:0ed2a7c7190c 69 {
GordonSin 0:0ed2a7c7190c 70 struct netconn *conn;
GordonSin 0:0ed2a7c7190c 71 struct api_msg msg;
GordonSin 0:0ed2a7c7190c 72
GordonSin 0:0ed2a7c7190c 73 conn = netconn_alloc(t, callback);
GordonSin 0:0ed2a7c7190c 74 if (conn != NULL) {
GordonSin 0:0ed2a7c7190c 75 msg.function = do_newconn;
GordonSin 0:0ed2a7c7190c 76 msg.msg.msg.n.proto = proto;
GordonSin 0:0ed2a7c7190c 77 msg.msg.conn = conn;
GordonSin 0:0ed2a7c7190c 78 if (TCPIP_APIMSG(&msg) != ERR_OK) {
GordonSin 0:0ed2a7c7190c 79 LWIP_ASSERT("freeing conn without freeing pcb", conn->pcb.tcp == NULL);
GordonSin 0:0ed2a7c7190c 80 LWIP_ASSERT("conn has no op_completed", sys_sem_valid(&conn->op_completed));
GordonSin 0:0ed2a7c7190c 81 LWIP_ASSERT("conn has no recvmbox", sys_mbox_valid(&conn->recvmbox));
GordonSin 0:0ed2a7c7190c 82 #if LWIP_TCP
GordonSin 0:0ed2a7c7190c 83 LWIP_ASSERT("conn->acceptmbox shouldn't exist", !sys_mbox_valid(&conn->acceptmbox));
GordonSin 0:0ed2a7c7190c 84 #endif /* LWIP_TCP */
GordonSin 0:0ed2a7c7190c 85 sys_sem_free(&conn->op_completed);
GordonSin 0:0ed2a7c7190c 86 sys_mbox_free(&conn->recvmbox);
GordonSin 0:0ed2a7c7190c 87 memp_free(MEMP_NETCONN, conn);
GordonSin 0:0ed2a7c7190c 88 return NULL;
GordonSin 0:0ed2a7c7190c 89 }
GordonSin 0:0ed2a7c7190c 90 }
GordonSin 0:0ed2a7c7190c 91 return conn;
GordonSin 0:0ed2a7c7190c 92 }
GordonSin 0:0ed2a7c7190c 93
GordonSin 0:0ed2a7c7190c 94 /**
GordonSin 0:0ed2a7c7190c 95 * Close a netconn 'connection' and free its resources.
GordonSin 0:0ed2a7c7190c 96 * UDP and RAW connection are completely closed, TCP pcbs might still be in a waitstate
GordonSin 0:0ed2a7c7190c 97 * after this returns.
GordonSin 0:0ed2a7c7190c 98 *
GordonSin 0:0ed2a7c7190c 99 * @param conn the netconn to delete
GordonSin 0:0ed2a7c7190c 100 * @return ERR_OK if the connection was deleted
GordonSin 0:0ed2a7c7190c 101 */
GordonSin 0:0ed2a7c7190c 102 err_t
GordonSin 0:0ed2a7c7190c 103 netconn_delete(struct netconn *conn)
GordonSin 0:0ed2a7c7190c 104 {
GordonSin 0:0ed2a7c7190c 105 struct api_msg msg;
GordonSin 0:0ed2a7c7190c 106
GordonSin 0:0ed2a7c7190c 107 /* No ASSERT here because possible to get a (conn == NULL) if we got an accept error */
GordonSin 0:0ed2a7c7190c 108 if (conn == NULL) {
GordonSin 0:0ed2a7c7190c 109 return ERR_OK;
GordonSin 0:0ed2a7c7190c 110 }
GordonSin 0:0ed2a7c7190c 111
GordonSin 0:0ed2a7c7190c 112 msg.function = do_delconn;
GordonSin 0:0ed2a7c7190c 113 msg.msg.conn = conn;
GordonSin 0:0ed2a7c7190c 114 tcpip_apimsg(&msg);
GordonSin 0:0ed2a7c7190c 115
GordonSin 0:0ed2a7c7190c 116 netconn_free(conn);
GordonSin 0:0ed2a7c7190c 117
GordonSin 0:0ed2a7c7190c 118 /* don't care for return value of do_delconn since it only calls void functions */
GordonSin 0:0ed2a7c7190c 119
GordonSin 0:0ed2a7c7190c 120 return ERR_OK;
GordonSin 0:0ed2a7c7190c 121 }
GordonSin 0:0ed2a7c7190c 122
GordonSin 0:0ed2a7c7190c 123 /**
GordonSin 0:0ed2a7c7190c 124 * Get the local or remote IP address and port of a netconn.
GordonSin 0:0ed2a7c7190c 125 * For RAW netconns, this returns the protocol instead of a port!
GordonSin 0:0ed2a7c7190c 126 *
GordonSin 0:0ed2a7c7190c 127 * @param conn the netconn to query
GordonSin 0:0ed2a7c7190c 128 * @param addr a pointer to which to save the IP address
GordonSin 0:0ed2a7c7190c 129 * @param port a pointer to which to save the port (or protocol for RAW)
GordonSin 0:0ed2a7c7190c 130 * @param local 1 to get the local IP address, 0 to get the remote one
GordonSin 0:0ed2a7c7190c 131 * @return ERR_CONN for invalid connections
GordonSin 0:0ed2a7c7190c 132 * ERR_OK if the information was retrieved
GordonSin 0:0ed2a7c7190c 133 */
GordonSin 0:0ed2a7c7190c 134 err_t
GordonSin 0:0ed2a7c7190c 135 netconn_getaddr(struct netconn *conn, ip_addr_t *addr, u16_t *port, u8_t local)
GordonSin 0:0ed2a7c7190c 136 {
GordonSin 0:0ed2a7c7190c 137 struct api_msg msg;
GordonSin 0:0ed2a7c7190c 138 err_t err;
GordonSin 0:0ed2a7c7190c 139
GordonSin 0:0ed2a7c7190c 140 LWIP_ERROR("netconn_getaddr: invalid conn", (conn != NULL), return ERR_ARG;);
GordonSin 0:0ed2a7c7190c 141 LWIP_ERROR("netconn_getaddr: invalid addr", (addr != NULL), return ERR_ARG;);
GordonSin 0:0ed2a7c7190c 142 LWIP_ERROR("netconn_getaddr: invalid port", (port != NULL), return ERR_ARG;);
GordonSin 0:0ed2a7c7190c 143
GordonSin 0:0ed2a7c7190c 144 msg.function = do_getaddr;
GordonSin 0:0ed2a7c7190c 145 msg.msg.conn = conn;
GordonSin 0:0ed2a7c7190c 146 msg.msg.msg.ad.ipaddr = addr;
GordonSin 0:0ed2a7c7190c 147 msg.msg.msg.ad.port = port;
GordonSin 0:0ed2a7c7190c 148 msg.msg.msg.ad.local = local;
GordonSin 0:0ed2a7c7190c 149 err = TCPIP_APIMSG(&msg);
GordonSin 0:0ed2a7c7190c 150
GordonSin 0:0ed2a7c7190c 151 NETCONN_SET_SAFE_ERR(conn, err);
GordonSin 0:0ed2a7c7190c 152 return err;
GordonSin 0:0ed2a7c7190c 153 }
GordonSin 0:0ed2a7c7190c 154
GordonSin 0:0ed2a7c7190c 155 /**
GordonSin 0:0ed2a7c7190c 156 * Bind a netconn to a specific local IP address and port.
GordonSin 0:0ed2a7c7190c 157 * Binding one netconn twice might not always be checked correctly!
GordonSin 0:0ed2a7c7190c 158 *
GordonSin 0:0ed2a7c7190c 159 * @param conn the netconn to bind
GordonSin 0:0ed2a7c7190c 160 * @param addr the local IP address to bind the netconn to (use IP_ADDR_ANY
GordonSin 0:0ed2a7c7190c 161 * to bind to all addresses)
GordonSin 0:0ed2a7c7190c 162 * @param port the local port to bind the netconn to (not used for RAW)
GordonSin 0:0ed2a7c7190c 163 * @return ERR_OK if bound, any other err_t on failure
GordonSin 0:0ed2a7c7190c 164 */
GordonSin 0:0ed2a7c7190c 165 err_t
GordonSin 0:0ed2a7c7190c 166 netconn_bind(struct netconn *conn, ip_addr_t *addr, u16_t port)
GordonSin 0:0ed2a7c7190c 167 {
GordonSin 0:0ed2a7c7190c 168 struct api_msg msg;
GordonSin 0:0ed2a7c7190c 169 err_t err;
GordonSin 0:0ed2a7c7190c 170
GordonSin 0:0ed2a7c7190c 171 LWIP_ERROR("netconn_bind: invalid conn", (conn != NULL), return ERR_ARG;);
GordonSin 0:0ed2a7c7190c 172
GordonSin 0:0ed2a7c7190c 173 msg.function = do_bind;
GordonSin 0:0ed2a7c7190c 174 msg.msg.conn = conn;
GordonSin 0:0ed2a7c7190c 175 msg.msg.msg.bc.ipaddr = addr;
GordonSin 0:0ed2a7c7190c 176 msg.msg.msg.bc.port = port;
GordonSin 0:0ed2a7c7190c 177 err = TCPIP_APIMSG(&msg);
GordonSin 0:0ed2a7c7190c 178
GordonSin 0:0ed2a7c7190c 179 NETCONN_SET_SAFE_ERR(conn, err);
GordonSin 0:0ed2a7c7190c 180 return err;
GordonSin 0:0ed2a7c7190c 181 }
GordonSin 0:0ed2a7c7190c 182
GordonSin 0:0ed2a7c7190c 183 /**
GordonSin 0:0ed2a7c7190c 184 * Connect a netconn to a specific remote IP address and port.
GordonSin 0:0ed2a7c7190c 185 *
GordonSin 0:0ed2a7c7190c 186 * @param conn the netconn to connect
GordonSin 0:0ed2a7c7190c 187 * @param addr the remote IP address to connect to
GordonSin 0:0ed2a7c7190c 188 * @param port the remote port to connect to (no used for RAW)
GordonSin 0:0ed2a7c7190c 189 * @return ERR_OK if connected, return value of tcp_/udp_/raw_connect otherwise
GordonSin 0:0ed2a7c7190c 190 */
GordonSin 0:0ed2a7c7190c 191 err_t
GordonSin 0:0ed2a7c7190c 192 netconn_connect(struct netconn *conn, ip_addr_t *addr, u16_t port)
GordonSin 0:0ed2a7c7190c 193 {
GordonSin 0:0ed2a7c7190c 194 struct api_msg msg;
GordonSin 0:0ed2a7c7190c 195 err_t err;
GordonSin 0:0ed2a7c7190c 196
GordonSin 0:0ed2a7c7190c 197 LWIP_ERROR("netconn_connect: invalid conn", (conn != NULL), return ERR_ARG;);
GordonSin 0:0ed2a7c7190c 198
GordonSin 0:0ed2a7c7190c 199 msg.function = do_connect;
GordonSin 0:0ed2a7c7190c 200 msg.msg.conn = conn;
GordonSin 0:0ed2a7c7190c 201 msg.msg.msg.bc.ipaddr = addr;
GordonSin 0:0ed2a7c7190c 202 msg.msg.msg.bc.port = port;
GordonSin 0:0ed2a7c7190c 203 /* This is the only function which need to not block tcpip_thread */
GordonSin 0:0ed2a7c7190c 204 err = tcpip_apimsg(&msg);
GordonSin 0:0ed2a7c7190c 205
GordonSin 0:0ed2a7c7190c 206 NETCONN_SET_SAFE_ERR(conn, err);
GordonSin 0:0ed2a7c7190c 207 return err;
GordonSin 0:0ed2a7c7190c 208 }
GordonSin 0:0ed2a7c7190c 209
GordonSin 0:0ed2a7c7190c 210 /**
GordonSin 0:0ed2a7c7190c 211 * Disconnect a netconn from its current peer (only valid for UDP netconns).
GordonSin 0:0ed2a7c7190c 212 *
GordonSin 0:0ed2a7c7190c 213 * @param conn the netconn to disconnect
GordonSin 0:0ed2a7c7190c 214 * @return TODO: return value is not set here...
GordonSin 0:0ed2a7c7190c 215 */
GordonSin 0:0ed2a7c7190c 216 err_t
GordonSin 0:0ed2a7c7190c 217 netconn_disconnect(struct netconn *conn)
GordonSin 0:0ed2a7c7190c 218 {
GordonSin 0:0ed2a7c7190c 219 struct api_msg msg;
GordonSin 0:0ed2a7c7190c 220 err_t err;
GordonSin 0:0ed2a7c7190c 221
GordonSin 0:0ed2a7c7190c 222 LWIP_ERROR("netconn_disconnect: invalid conn", (conn != NULL), return ERR_ARG;);
GordonSin 0:0ed2a7c7190c 223
GordonSin 0:0ed2a7c7190c 224 msg.function = do_disconnect;
GordonSin 0:0ed2a7c7190c 225 msg.msg.conn = conn;
GordonSin 0:0ed2a7c7190c 226 err = TCPIP_APIMSG(&msg);
GordonSin 0:0ed2a7c7190c 227
GordonSin 0:0ed2a7c7190c 228 NETCONN_SET_SAFE_ERR(conn, err);
GordonSin 0:0ed2a7c7190c 229 return err;
GordonSin 0:0ed2a7c7190c 230 }
GordonSin 0:0ed2a7c7190c 231
GordonSin 0:0ed2a7c7190c 232 /**
GordonSin 0:0ed2a7c7190c 233 * Set a TCP netconn into listen mode
GordonSin 0:0ed2a7c7190c 234 *
GordonSin 0:0ed2a7c7190c 235 * @param conn the tcp netconn to set to listen mode
GordonSin 0:0ed2a7c7190c 236 * @param backlog the listen backlog, only used if TCP_LISTEN_BACKLOG==1
GordonSin 0:0ed2a7c7190c 237 * @return ERR_OK if the netconn was set to listen (UDP and RAW netconns
GordonSin 0:0ed2a7c7190c 238 * don't return any error (yet?))
GordonSin 0:0ed2a7c7190c 239 */
GordonSin 0:0ed2a7c7190c 240 err_t
GordonSin 0:0ed2a7c7190c 241 netconn_listen_with_backlog(struct netconn *conn, u8_t backlog)
GordonSin 0:0ed2a7c7190c 242 {
GordonSin 0:0ed2a7c7190c 243 #if LWIP_TCP
GordonSin 0:0ed2a7c7190c 244 struct api_msg msg;
GordonSin 0:0ed2a7c7190c 245 err_t err;
GordonSin 0:0ed2a7c7190c 246
GordonSin 0:0ed2a7c7190c 247 /* This does no harm. If TCP_LISTEN_BACKLOG is off, backlog is unused. */
GordonSin 0:0ed2a7c7190c 248 LWIP_UNUSED_ARG(backlog);
GordonSin 0:0ed2a7c7190c 249
GordonSin 0:0ed2a7c7190c 250 LWIP_ERROR("netconn_listen: invalid conn", (conn != NULL), return ERR_ARG;);
GordonSin 0:0ed2a7c7190c 251
GordonSin 0:0ed2a7c7190c 252 msg.function = do_listen;
GordonSin 0:0ed2a7c7190c 253 msg.msg.conn = conn;
GordonSin 0:0ed2a7c7190c 254 #if TCP_LISTEN_BACKLOG
GordonSin 0:0ed2a7c7190c 255 msg.msg.msg.lb.backlog = backlog;
GordonSin 0:0ed2a7c7190c 256 #endif /* TCP_LISTEN_BACKLOG */
GordonSin 0:0ed2a7c7190c 257 err = TCPIP_APIMSG(&msg);
GordonSin 0:0ed2a7c7190c 258
GordonSin 0:0ed2a7c7190c 259 NETCONN_SET_SAFE_ERR(conn, err);
GordonSin 0:0ed2a7c7190c 260 return err;
GordonSin 0:0ed2a7c7190c 261 #else /* LWIP_TCP */
GordonSin 0:0ed2a7c7190c 262 LWIP_UNUSED_ARG(conn);
GordonSin 0:0ed2a7c7190c 263 LWIP_UNUSED_ARG(backlog);
GordonSin 0:0ed2a7c7190c 264 return ERR_ARG;
GordonSin 0:0ed2a7c7190c 265 #endif /* LWIP_TCP */
GordonSin 0:0ed2a7c7190c 266 }
GordonSin 0:0ed2a7c7190c 267
GordonSin 0:0ed2a7c7190c 268 /**
GordonSin 0:0ed2a7c7190c 269 * Accept a new connection on a TCP listening netconn.
GordonSin 0:0ed2a7c7190c 270 *
GordonSin 0:0ed2a7c7190c 271 * @param conn the TCP listen netconn
GordonSin 0:0ed2a7c7190c 272 * @param new_conn pointer where the new connection is stored
GordonSin 0:0ed2a7c7190c 273 * @return ERR_OK if a new connection has been received or an error
GordonSin 0:0ed2a7c7190c 274 * code otherwise
GordonSin 0:0ed2a7c7190c 275 */
GordonSin 0:0ed2a7c7190c 276 err_t
GordonSin 0:0ed2a7c7190c 277 netconn_accept(struct netconn *conn, struct netconn **new_conn)
GordonSin 0:0ed2a7c7190c 278 {
GordonSin 0:0ed2a7c7190c 279 #if LWIP_TCP
GordonSin 0:0ed2a7c7190c 280 struct netconn *newconn;
GordonSin 0:0ed2a7c7190c 281 err_t err;
GordonSin 0:0ed2a7c7190c 282 #if TCP_LISTEN_BACKLOG
GordonSin 0:0ed2a7c7190c 283 struct api_msg msg;
GordonSin 0:0ed2a7c7190c 284 #endif /* TCP_LISTEN_BACKLOG */
GordonSin 0:0ed2a7c7190c 285
GordonSin 0:0ed2a7c7190c 286 LWIP_ERROR("netconn_accept: invalid pointer", (new_conn != NULL), return ERR_ARG;);
GordonSin 0:0ed2a7c7190c 287 *new_conn = NULL;
GordonSin 0:0ed2a7c7190c 288 LWIP_ERROR("netconn_accept: invalid conn", (conn != NULL), return ERR_ARG;);
GordonSin 0:0ed2a7c7190c 289 LWIP_ERROR("netconn_accept: invalid acceptmbox", sys_mbox_valid(&conn->acceptmbox), return ERR_ARG;);
GordonSin 0:0ed2a7c7190c 290
GordonSin 0:0ed2a7c7190c 291 err = conn->last_err;
GordonSin 0:0ed2a7c7190c 292 if (ERR_IS_FATAL(err)) {
GordonSin 0:0ed2a7c7190c 293 /* don't recv on fatal errors: this might block the application task
GordonSin 0:0ed2a7c7190c 294 waiting on acceptmbox forever! */
GordonSin 0:0ed2a7c7190c 295 return err;
GordonSin 0:0ed2a7c7190c 296 }
GordonSin 0:0ed2a7c7190c 297
GordonSin 0:0ed2a7c7190c 298 #if LWIP_SO_RCVTIMEO
GordonSin 0:0ed2a7c7190c 299 if (sys_arch_mbox_fetch(&conn->acceptmbox, (void **)&newconn, conn->recv_timeout) == SYS_ARCH_TIMEOUT) {
GordonSin 0:0ed2a7c7190c 300 NETCONN_SET_SAFE_ERR(conn, ERR_TIMEOUT);
GordonSin 0:0ed2a7c7190c 301 return ERR_TIMEOUT;
GordonSin 0:0ed2a7c7190c 302 }
GordonSin 0:0ed2a7c7190c 303 #else
GordonSin 0:0ed2a7c7190c 304 sys_arch_mbox_fetch(&conn->acceptmbox, (void **)&newconn, 0);
GordonSin 0:0ed2a7c7190c 305 #endif /* LWIP_SO_RCVTIMEO*/
GordonSin 0:0ed2a7c7190c 306 /* Register event with callback */
GordonSin 0:0ed2a7c7190c 307 API_EVENT(conn, NETCONN_EVT_RCVMINUS, 0);
GordonSin 0:0ed2a7c7190c 308
GordonSin 0:0ed2a7c7190c 309 if (newconn == NULL) {
GordonSin 0:0ed2a7c7190c 310 /* connection has been aborted */
GordonSin 0:0ed2a7c7190c 311 NETCONN_SET_SAFE_ERR(conn, ERR_ABRT);
GordonSin 0:0ed2a7c7190c 312 return ERR_ABRT;
GordonSin 0:0ed2a7c7190c 313 }
GordonSin 0:0ed2a7c7190c 314 #if TCP_LISTEN_BACKLOG
GordonSin 0:0ed2a7c7190c 315 /* Let the stack know that we have accepted the connection. */
GordonSin 0:0ed2a7c7190c 316 msg.function = do_recv;
GordonSin 0:0ed2a7c7190c 317 msg.msg.conn = conn;
GordonSin 0:0ed2a7c7190c 318 /* don't care for the return value of do_recv */
GordonSin 0:0ed2a7c7190c 319 TCPIP_APIMSG(&msg);
GordonSin 0:0ed2a7c7190c 320 #endif /* TCP_LISTEN_BACKLOG */
GordonSin 0:0ed2a7c7190c 321
GordonSin 0:0ed2a7c7190c 322 *new_conn = newconn;
GordonSin 0:0ed2a7c7190c 323 /* don't set conn->last_err: it's only ERR_OK, anyway */
GordonSin 0:0ed2a7c7190c 324 return ERR_OK;
GordonSin 0:0ed2a7c7190c 325 #else /* LWIP_TCP */
GordonSin 0:0ed2a7c7190c 326 LWIP_UNUSED_ARG(conn);
GordonSin 0:0ed2a7c7190c 327 LWIP_UNUSED_ARG(new_conn);
GordonSin 0:0ed2a7c7190c 328 return ERR_ARG;
GordonSin 0:0ed2a7c7190c 329 #endif /* LWIP_TCP */
GordonSin 0:0ed2a7c7190c 330 }
GordonSin 0:0ed2a7c7190c 331
GordonSin 0:0ed2a7c7190c 332 /**
GordonSin 0:0ed2a7c7190c 333 * Receive data: actual implementation that doesn't care whether pbuf or netbuf
GordonSin 0:0ed2a7c7190c 334 * is received
GordonSin 0:0ed2a7c7190c 335 *
GordonSin 0:0ed2a7c7190c 336 * @param conn the netconn from which to receive data
GordonSin 0:0ed2a7c7190c 337 * @param new_buf pointer where a new pbuf/netbuf is stored when received data
GordonSin 0:0ed2a7c7190c 338 * @return ERR_OK if data has been received, an error code otherwise (timeout,
GordonSin 0:0ed2a7c7190c 339 * memory error or another error)
GordonSin 0:0ed2a7c7190c 340 */
GordonSin 0:0ed2a7c7190c 341 static err_t
GordonSin 0:0ed2a7c7190c 342 netconn_recv_data(struct netconn *conn, void **new_buf)
GordonSin 0:0ed2a7c7190c 343 {
GordonSin 0:0ed2a7c7190c 344 void *buf = NULL;
GordonSin 0:0ed2a7c7190c 345 u16_t len;
GordonSin 0:0ed2a7c7190c 346 err_t err;
GordonSin 0:0ed2a7c7190c 347 #if LWIP_TCP
GordonSin 0:0ed2a7c7190c 348 struct api_msg msg;
GordonSin 0:0ed2a7c7190c 349 #endif /* LWIP_TCP */
GordonSin 0:0ed2a7c7190c 350
GordonSin 0:0ed2a7c7190c 351 LWIP_ERROR("netconn_recv: invalid pointer", (new_buf != NULL), return ERR_ARG;);
GordonSin 0:0ed2a7c7190c 352 *new_buf = NULL;
GordonSin 0:0ed2a7c7190c 353 LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL), return ERR_ARG;);
GordonSin 0:0ed2a7c7190c 354 LWIP_ERROR("netconn_accept: invalid recvmbox", sys_mbox_valid(&conn->recvmbox), return ERR_CONN;);
GordonSin 0:0ed2a7c7190c 355
GordonSin 0:0ed2a7c7190c 356 err = conn->last_err;
GordonSin 0:0ed2a7c7190c 357 if (ERR_IS_FATAL(err)) {
GordonSin 0:0ed2a7c7190c 358 /* don't recv on fatal errors: this might block the application task
GordonSin 0:0ed2a7c7190c 359 waiting on recvmbox forever! */
GordonSin 0:0ed2a7c7190c 360 /* @todo: this does not allow us to fetch data that has been put into recvmbox
GordonSin 0:0ed2a7c7190c 361 before the fatal error occurred - is that a problem? */
GordonSin 0:0ed2a7c7190c 362 return err;
GordonSin 0:0ed2a7c7190c 363 }
GordonSin 0:0ed2a7c7190c 364
GordonSin 0:0ed2a7c7190c 365 #if LWIP_SO_RCVTIMEO
GordonSin 0:0ed2a7c7190c 366 if (sys_arch_mbox_fetch(&conn->recvmbox, &buf, conn->recv_timeout) == SYS_ARCH_TIMEOUT) {
GordonSin 0:0ed2a7c7190c 367 NETCONN_SET_SAFE_ERR(conn, ERR_TIMEOUT);
GordonSin 0:0ed2a7c7190c 368 return ERR_TIMEOUT;
GordonSin 0:0ed2a7c7190c 369 }
GordonSin 0:0ed2a7c7190c 370 #else
GordonSin 0:0ed2a7c7190c 371 sys_arch_mbox_fetch(&conn->recvmbox, &buf, 0);
GordonSin 0:0ed2a7c7190c 372 #endif /* LWIP_SO_RCVTIMEO*/
GordonSin 0:0ed2a7c7190c 373
GordonSin 0:0ed2a7c7190c 374 #if LWIP_TCP
GordonSin 0:0ed2a7c7190c 375 if (conn->type == NETCONN_TCP) {
GordonSin 0:0ed2a7c7190c 376 if (!netconn_get_noautorecved(conn) || (buf == NULL)) {
GordonSin 0:0ed2a7c7190c 377 /* Let the stack know that we have taken the data. */
GordonSin 0:0ed2a7c7190c 378 /* TODO: Speedup: Don't block and wait for the answer here
GordonSin 0:0ed2a7c7190c 379 (to prevent multiple thread-switches). */
GordonSin 0:0ed2a7c7190c 380 msg.function = do_recv;
GordonSin 0:0ed2a7c7190c 381 msg.msg.conn = conn;
GordonSin 0:0ed2a7c7190c 382 if (buf != NULL) {
GordonSin 0:0ed2a7c7190c 383 msg.msg.msg.r.len = ((struct pbuf *)buf)->tot_len;
GordonSin 0:0ed2a7c7190c 384 } else {
GordonSin 0:0ed2a7c7190c 385 msg.msg.msg.r.len = 1;
GordonSin 0:0ed2a7c7190c 386 }
GordonSin 0:0ed2a7c7190c 387 /* don't care for the return value of do_recv */
GordonSin 0:0ed2a7c7190c 388 TCPIP_APIMSG(&msg);
GordonSin 0:0ed2a7c7190c 389 }
GordonSin 0:0ed2a7c7190c 390
GordonSin 0:0ed2a7c7190c 391 /* If we are closed, we indicate that we no longer wish to use the socket */
GordonSin 0:0ed2a7c7190c 392 if (buf == NULL) {
GordonSin 0:0ed2a7c7190c 393 API_EVENT(conn, NETCONN_EVT_RCVMINUS, 0);
GordonSin 0:0ed2a7c7190c 394 /* Avoid to lose any previous error code */
GordonSin 0:0ed2a7c7190c 395 NETCONN_SET_SAFE_ERR(conn, ERR_CLSD);
GordonSin 0:0ed2a7c7190c 396 return ERR_CLSD;
GordonSin 0:0ed2a7c7190c 397 }
GordonSin 0:0ed2a7c7190c 398 len = ((struct pbuf *)buf)->tot_len;
GordonSin 0:0ed2a7c7190c 399 }
GordonSin 0:0ed2a7c7190c 400 #endif /* LWIP_TCP */
GordonSin 0:0ed2a7c7190c 401 #if LWIP_TCP && (LWIP_UDP || LWIP_RAW)
GordonSin 0:0ed2a7c7190c 402 else
GordonSin 0:0ed2a7c7190c 403 #endif /* LWIP_TCP && (LWIP_UDP || LWIP_RAW) */
GordonSin 0:0ed2a7c7190c 404 #if (LWIP_UDP || LWIP_RAW)
GordonSin 0:0ed2a7c7190c 405 {
GordonSin 0:0ed2a7c7190c 406 LWIP_ASSERT("buf != NULL", buf != NULL);
GordonSin 0:0ed2a7c7190c 407 len = netbuf_len((struct netbuf *)buf);
GordonSin 0:0ed2a7c7190c 408 }
GordonSin 0:0ed2a7c7190c 409 #endif /* (LWIP_UDP || LWIP_RAW) */
GordonSin 0:0ed2a7c7190c 410
GordonSin 0:0ed2a7c7190c 411 #if LWIP_SO_RCVBUF
GordonSin 0:0ed2a7c7190c 412 SYS_ARCH_DEC(conn->recv_avail, len);
GordonSin 0:0ed2a7c7190c 413 #endif /* LWIP_SO_RCVBUF */
GordonSin 0:0ed2a7c7190c 414 /* Register event with callback */
GordonSin 0:0ed2a7c7190c 415 API_EVENT(conn, NETCONN_EVT_RCVMINUS, len);
GordonSin 0:0ed2a7c7190c 416
GordonSin 0:0ed2a7c7190c 417 LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_recv_data: received %p, len=%"U16_F"\n", buf, len));
GordonSin 0:0ed2a7c7190c 418
GordonSin 0:0ed2a7c7190c 419 *new_buf = buf;
GordonSin 0:0ed2a7c7190c 420 /* don't set conn->last_err: it's only ERR_OK, anyway */
GordonSin 0:0ed2a7c7190c 421 return ERR_OK;
GordonSin 0:0ed2a7c7190c 422 }
GordonSin 0:0ed2a7c7190c 423
GordonSin 0:0ed2a7c7190c 424 /**
GordonSin 0:0ed2a7c7190c 425 * Receive data (in form of a pbuf) from a TCP netconn
GordonSin 0:0ed2a7c7190c 426 *
GordonSin 0:0ed2a7c7190c 427 * @param conn the netconn from which to receive data
GordonSin 0:0ed2a7c7190c 428 * @param new_buf pointer where a new pbuf is stored when received data
GordonSin 0:0ed2a7c7190c 429 * @return ERR_OK if data has been received, an error code otherwise (timeout,
GordonSin 0:0ed2a7c7190c 430 * memory error or another error)
GordonSin 0:0ed2a7c7190c 431 * ERR_ARG if conn is not a TCP netconn
GordonSin 0:0ed2a7c7190c 432 */
GordonSin 0:0ed2a7c7190c 433 err_t
GordonSin 0:0ed2a7c7190c 434 netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf)
GordonSin 0:0ed2a7c7190c 435 {
GordonSin 0:0ed2a7c7190c 436 LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL) &&
GordonSin 0:0ed2a7c7190c 437 netconn_type(conn) == NETCONN_TCP, return ERR_ARG;);
GordonSin 0:0ed2a7c7190c 438
GordonSin 0:0ed2a7c7190c 439 return netconn_recv_data(conn, (void **)new_buf);
GordonSin 0:0ed2a7c7190c 440 }
GordonSin 0:0ed2a7c7190c 441
GordonSin 0:0ed2a7c7190c 442 /**
GordonSin 0:0ed2a7c7190c 443 * Receive data (in form of a netbuf containing a packet buffer) from a netconn
GordonSin 0:0ed2a7c7190c 444 *
GordonSin 0:0ed2a7c7190c 445 * @param conn the netconn from which to receive data
GordonSin 0:0ed2a7c7190c 446 * @param new_buf pointer where a new netbuf is stored when received data
GordonSin 0:0ed2a7c7190c 447 * @return ERR_OK if data has been received, an error code otherwise (timeout,
GordonSin 0:0ed2a7c7190c 448 * memory error or another error)
GordonSin 0:0ed2a7c7190c 449 */
GordonSin 0:0ed2a7c7190c 450 err_t
GordonSin 0:0ed2a7c7190c 451 netconn_recv(struct netconn *conn, struct netbuf **new_buf)
GordonSin 0:0ed2a7c7190c 452 {
GordonSin 0:0ed2a7c7190c 453 #if LWIP_TCP
GordonSin 0:0ed2a7c7190c 454 struct netbuf *buf = NULL;
GordonSin 0:0ed2a7c7190c 455 err_t err;
GordonSin 0:0ed2a7c7190c 456 #endif /* LWIP_TCP */
GordonSin 0:0ed2a7c7190c 457
GordonSin 0:0ed2a7c7190c 458 LWIP_ERROR("netconn_recv: invalid pointer", (new_buf != NULL), return ERR_ARG;);
GordonSin 0:0ed2a7c7190c 459 *new_buf = NULL;
GordonSin 0:0ed2a7c7190c 460 LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL), return ERR_ARG;);
GordonSin 0:0ed2a7c7190c 461 LWIP_ERROR("netconn_accept: invalid recvmbox", sys_mbox_valid(&conn->recvmbox), return ERR_CONN;);
GordonSin 0:0ed2a7c7190c 462
GordonSin 0:0ed2a7c7190c 463 #if LWIP_TCP
GordonSin 0:0ed2a7c7190c 464 if (conn->type == NETCONN_TCP) {
GordonSin 0:0ed2a7c7190c 465 struct pbuf *p = NULL;
GordonSin 0:0ed2a7c7190c 466 /* This is not a listening netconn, since recvmbox is set */
GordonSin 0:0ed2a7c7190c 467
GordonSin 0:0ed2a7c7190c 468 buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
GordonSin 0:0ed2a7c7190c 469 if (buf == NULL) {
GordonSin 0:0ed2a7c7190c 470 NETCONN_SET_SAFE_ERR(conn, ERR_MEM);
GordonSin 0:0ed2a7c7190c 471 return ERR_MEM;
GordonSin 0:0ed2a7c7190c 472 }
GordonSin 0:0ed2a7c7190c 473
GordonSin 0:0ed2a7c7190c 474 err = netconn_recv_data(conn, (void **)&p);
GordonSin 0:0ed2a7c7190c 475 if (err != ERR_OK) {
GordonSin 0:0ed2a7c7190c 476 memp_free(MEMP_NETBUF, buf);
GordonSin 0:0ed2a7c7190c 477 return err;
GordonSin 0:0ed2a7c7190c 478 }
GordonSin 0:0ed2a7c7190c 479 LWIP_ASSERT("p != NULL", p != NULL);
GordonSin 0:0ed2a7c7190c 480
GordonSin 0:0ed2a7c7190c 481 buf->p = p;
GordonSin 0:0ed2a7c7190c 482 buf->ptr = p;
GordonSin 0:0ed2a7c7190c 483 buf->port = 0;
GordonSin 0:0ed2a7c7190c 484 ip_addr_set_any(&buf->addr);
GordonSin 0:0ed2a7c7190c 485 *new_buf = buf;
GordonSin 0:0ed2a7c7190c 486 /* don't set conn->last_err: it's only ERR_OK, anyway */
GordonSin 0:0ed2a7c7190c 487 return ERR_OK;
GordonSin 0:0ed2a7c7190c 488 } else
GordonSin 0:0ed2a7c7190c 489 #endif /* LWIP_TCP */
GordonSin 0:0ed2a7c7190c 490 {
GordonSin 0:0ed2a7c7190c 491 #if (LWIP_UDP || LWIP_RAW)
GordonSin 0:0ed2a7c7190c 492 return netconn_recv_data(conn, (void **)new_buf);
GordonSin 0:0ed2a7c7190c 493 #endif /* (LWIP_UDP || LWIP_RAW) */
GordonSin 0:0ed2a7c7190c 494 }
GordonSin 0:0ed2a7c7190c 495 }
GordonSin 0:0ed2a7c7190c 496
GordonSin 0:0ed2a7c7190c 497 /**
GordonSin 0:0ed2a7c7190c 498 * TCP: update the receive window: by calling this, the application
GordonSin 0:0ed2a7c7190c 499 * tells the stack that it has processed data and is able to accept
GordonSin 0:0ed2a7c7190c 500 * new data.
GordonSin 0:0ed2a7c7190c 501 * ATTENTION: use with care, this is mainly used for sockets!
GordonSin 0:0ed2a7c7190c 502 * Can only be used when calling netconn_set_noautorecved(conn, 1) before.
GordonSin 0:0ed2a7c7190c 503 *
GordonSin 0:0ed2a7c7190c 504 * @param conn the netconn for which to update the receive window
GordonSin 0:0ed2a7c7190c 505 * @param length amount of data processed (ATTENTION: this must be accurate!)
GordonSin 0:0ed2a7c7190c 506 */
GordonSin 0:0ed2a7c7190c 507 void
GordonSin 0:0ed2a7c7190c 508 netconn_recved(struct netconn *conn, u32_t length)
GordonSin 0:0ed2a7c7190c 509 {
GordonSin 0:0ed2a7c7190c 510 #if LWIP_TCP
GordonSin 0:0ed2a7c7190c 511 if ((conn != NULL) && (conn->type == NETCONN_TCP) &&
GordonSin 0:0ed2a7c7190c 512 (netconn_get_noautorecved(conn))) {
GordonSin 0:0ed2a7c7190c 513 struct api_msg msg;
GordonSin 0:0ed2a7c7190c 514 /* Let the stack know that we have taken the data. */
GordonSin 0:0ed2a7c7190c 515 /* TODO: Speedup: Don't block and wait for the answer here
GordonSin 0:0ed2a7c7190c 516 (to prevent multiple thread-switches). */
GordonSin 0:0ed2a7c7190c 517 msg.function = do_recv;
GordonSin 0:0ed2a7c7190c 518 msg.msg.conn = conn;
GordonSin 0:0ed2a7c7190c 519 msg.msg.msg.r.len = length;
GordonSin 0:0ed2a7c7190c 520 /* don't care for the return value of do_recv */
GordonSin 0:0ed2a7c7190c 521 TCPIP_APIMSG(&msg);
GordonSin 0:0ed2a7c7190c 522 }
GordonSin 0:0ed2a7c7190c 523 #else /* LWIP_TCP */
GordonSin 0:0ed2a7c7190c 524 LWIP_UNUSED_ARG(conn);
GordonSin 0:0ed2a7c7190c 525 LWIP_UNUSED_ARG(length);
GordonSin 0:0ed2a7c7190c 526 #endif /* LWIP_TCP */
GordonSin 0:0ed2a7c7190c 527 }
GordonSin 0:0ed2a7c7190c 528
GordonSin 0:0ed2a7c7190c 529 /**
GordonSin 0:0ed2a7c7190c 530 * Send data (in form of a netbuf) to a specific remote IP address and port.
GordonSin 0:0ed2a7c7190c 531 * Only to be used for UDP and RAW netconns (not TCP).
GordonSin 0:0ed2a7c7190c 532 *
GordonSin 0:0ed2a7c7190c 533 * @param conn the netconn over which to send data
GordonSin 0:0ed2a7c7190c 534 * @param buf a netbuf containing the data to send
GordonSin 0:0ed2a7c7190c 535 * @param addr the remote IP address to which to send the data
GordonSin 0:0ed2a7c7190c 536 * @param port the remote port to which to send the data
GordonSin 0:0ed2a7c7190c 537 * @return ERR_OK if data was sent, any other err_t on error
GordonSin 0:0ed2a7c7190c 538 */
GordonSin 0:0ed2a7c7190c 539 err_t
GordonSin 0:0ed2a7c7190c 540 netconn_sendto(struct netconn *conn, struct netbuf *buf, ip_addr_t *addr, u16_t port)
GordonSin 0:0ed2a7c7190c 541 {
GordonSin 0:0ed2a7c7190c 542 if (buf != NULL) {
GordonSin 0:0ed2a7c7190c 543 ip_addr_set(&buf->addr, addr);
GordonSin 0:0ed2a7c7190c 544 buf->port = port;
GordonSin 0:0ed2a7c7190c 545 return netconn_send(conn, buf);
GordonSin 0:0ed2a7c7190c 546 }
GordonSin 0:0ed2a7c7190c 547 return ERR_VAL;
GordonSin 0:0ed2a7c7190c 548 }
GordonSin 0:0ed2a7c7190c 549
GordonSin 0:0ed2a7c7190c 550 /**
GordonSin 0:0ed2a7c7190c 551 * Send data over a UDP or RAW netconn (that is already connected).
GordonSin 0:0ed2a7c7190c 552 *
GordonSin 0:0ed2a7c7190c 553 * @param conn the UDP or RAW netconn over which to send data
GordonSin 0:0ed2a7c7190c 554 * @param buf a netbuf containing the data to send
GordonSin 0:0ed2a7c7190c 555 * @return ERR_OK if data was sent, any other err_t on error
GordonSin 0:0ed2a7c7190c 556 */
GordonSin 0:0ed2a7c7190c 557 err_t
GordonSin 0:0ed2a7c7190c 558 netconn_send(struct netconn *conn, struct netbuf *buf)
GordonSin 0:0ed2a7c7190c 559 {
GordonSin 0:0ed2a7c7190c 560 struct api_msg msg;
GordonSin 0:0ed2a7c7190c 561 err_t err;
GordonSin 0:0ed2a7c7190c 562
GordonSin 0:0ed2a7c7190c 563 LWIP_ERROR("netconn_send: invalid conn", (conn != NULL), return ERR_ARG;);
GordonSin 0:0ed2a7c7190c 564
GordonSin 0:0ed2a7c7190c 565 LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %"U16_F" bytes\n", buf->p->tot_len));
GordonSin 0:0ed2a7c7190c 566 msg.function = do_send;
GordonSin 0:0ed2a7c7190c 567 msg.msg.conn = conn;
GordonSin 0:0ed2a7c7190c 568 msg.msg.msg.b = buf;
GordonSin 0:0ed2a7c7190c 569 err = TCPIP_APIMSG(&msg);
GordonSin 0:0ed2a7c7190c 570
GordonSin 0:0ed2a7c7190c 571 NETCONN_SET_SAFE_ERR(conn, err);
GordonSin 0:0ed2a7c7190c 572 return err;
GordonSin 0:0ed2a7c7190c 573 }
GordonSin 0:0ed2a7c7190c 574
GordonSin 0:0ed2a7c7190c 575 /**
GordonSin 0:0ed2a7c7190c 576 * Send data over a TCP netconn.
GordonSin 0:0ed2a7c7190c 577 *
GordonSin 0:0ed2a7c7190c 578 * @param conn the TCP netconn over which to send data
GordonSin 0:0ed2a7c7190c 579 * @param dataptr pointer to the application buffer that contains the data to send
GordonSin 0:0ed2a7c7190c 580 * @param size size of the application data to send
GordonSin 0:0ed2a7c7190c 581 * @param apiflags combination of following flags :
GordonSin 0:0ed2a7c7190c 582 * - NETCONN_COPY: data will be copied into memory belonging to the stack
GordonSin 0:0ed2a7c7190c 583 * - NETCONN_MORE: for TCP connection, PSH flag will be set on last segment sent
GordonSin 0:0ed2a7c7190c 584 * - NETCONN_DONTBLOCK: only write the data if all dat can be written at once
GordonSin 0:0ed2a7c7190c 585 * @return ERR_OK if data was sent, any other err_t on error
GordonSin 0:0ed2a7c7190c 586 */
GordonSin 0:0ed2a7c7190c 587 err_t
GordonSin 0:0ed2a7c7190c 588 netconn_write(struct netconn *conn, const void *dataptr, size_t size, u8_t apiflags)
GordonSin 0:0ed2a7c7190c 589 {
GordonSin 0:0ed2a7c7190c 590 struct api_msg msg;
GordonSin 0:0ed2a7c7190c 591 err_t err;
GordonSin 0:0ed2a7c7190c 592
GordonSin 0:0ed2a7c7190c 593 LWIP_ERROR("netconn_write: invalid conn", (conn != NULL), return ERR_ARG;);
GordonSin 0:0ed2a7c7190c 594 LWIP_ERROR("netconn_write: invalid conn->type", (conn->type == NETCONN_TCP), return ERR_VAL;);
GordonSin 0:0ed2a7c7190c 595 if (size == 0) {
GordonSin 0:0ed2a7c7190c 596 return ERR_OK;
GordonSin 0:0ed2a7c7190c 597 }
GordonSin 0:0ed2a7c7190c 598
GordonSin 0:0ed2a7c7190c 599 /* @todo: for non-blocking write, check if 'size' would ever fit into
GordonSin 0:0ed2a7c7190c 600 snd_queue or snd_buf */
GordonSin 0:0ed2a7c7190c 601 msg.function = do_write;
GordonSin 0:0ed2a7c7190c 602 msg.msg.conn = conn;
GordonSin 0:0ed2a7c7190c 603 msg.msg.msg.w.dataptr = dataptr;
GordonSin 0:0ed2a7c7190c 604 msg.msg.msg.w.apiflags = apiflags;
GordonSin 0:0ed2a7c7190c 605 msg.msg.msg.w.len = size;
GordonSin 0:0ed2a7c7190c 606 /* For locking the core: this _can_ be delayed on low memory/low send buffer,
GordonSin 0:0ed2a7c7190c 607 but if it is, this is done inside api_msg.c:do_write(), so we can use the
GordonSin 0:0ed2a7c7190c 608 non-blocking version here. */
GordonSin 0:0ed2a7c7190c 609 err = TCPIP_APIMSG(&msg);
GordonSin 0:0ed2a7c7190c 610
GordonSin 0:0ed2a7c7190c 611 NETCONN_SET_SAFE_ERR(conn, err);
GordonSin 0:0ed2a7c7190c 612 return err;
GordonSin 0:0ed2a7c7190c 613 }
GordonSin 0:0ed2a7c7190c 614
GordonSin 0:0ed2a7c7190c 615 /**
GordonSin 0:0ed2a7c7190c 616 * Close ot shutdown a TCP netconn (doesn't delete it).
GordonSin 0:0ed2a7c7190c 617 *
GordonSin 0:0ed2a7c7190c 618 * @param conn the TCP netconn to close or shutdown
GordonSin 0:0ed2a7c7190c 619 * @param how fully close or only shutdown one side?
GordonSin 0:0ed2a7c7190c 620 * @return ERR_OK if the netconn was closed, any other err_t on error
GordonSin 0:0ed2a7c7190c 621 */
GordonSin 0:0ed2a7c7190c 622 static err_t
GordonSin 0:0ed2a7c7190c 623 netconn_close_shutdown(struct netconn *conn, u8_t how)
GordonSin 0:0ed2a7c7190c 624 {
GordonSin 0:0ed2a7c7190c 625 struct api_msg msg;
GordonSin 0:0ed2a7c7190c 626 err_t err;
GordonSin 0:0ed2a7c7190c 627
GordonSin 0:0ed2a7c7190c 628 LWIP_ERROR("netconn_close: invalid conn", (conn != NULL), return ERR_ARG;);
GordonSin 0:0ed2a7c7190c 629
GordonSin 0:0ed2a7c7190c 630 msg.function = do_close;
GordonSin 0:0ed2a7c7190c 631 msg.msg.conn = conn;
GordonSin 0:0ed2a7c7190c 632 /* shutting down both ends is the same as closing */
GordonSin 0:0ed2a7c7190c 633 msg.msg.msg.sd.shut = how;
GordonSin 0:0ed2a7c7190c 634 /* because of the LWIP_TCPIP_CORE_LOCKING implementation of do_close,
GordonSin 0:0ed2a7c7190c 635 don't use TCPIP_APIMSG here */
GordonSin 0:0ed2a7c7190c 636 err = tcpip_apimsg(&msg);
GordonSin 0:0ed2a7c7190c 637
GordonSin 0:0ed2a7c7190c 638 NETCONN_SET_SAFE_ERR(conn, err);
GordonSin 0:0ed2a7c7190c 639 return err;
GordonSin 0:0ed2a7c7190c 640 }
GordonSin 0:0ed2a7c7190c 641
GordonSin 0:0ed2a7c7190c 642 /**
GordonSin 0:0ed2a7c7190c 643 * Close a TCP netconn (doesn't delete it).
GordonSin 0:0ed2a7c7190c 644 *
GordonSin 0:0ed2a7c7190c 645 * @param conn the TCP netconn to close
GordonSin 0:0ed2a7c7190c 646 * @return ERR_OK if the netconn was closed, any other err_t on error
GordonSin 0:0ed2a7c7190c 647 */
GordonSin 0:0ed2a7c7190c 648 err_t
GordonSin 0:0ed2a7c7190c 649 netconn_close(struct netconn *conn)
GordonSin 0:0ed2a7c7190c 650 {
GordonSin 0:0ed2a7c7190c 651 /* shutting down both ends is the same as closing */
GordonSin 0:0ed2a7c7190c 652 return netconn_close_shutdown(conn, NETCONN_SHUT_RDWR);
GordonSin 0:0ed2a7c7190c 653 }
GordonSin 0:0ed2a7c7190c 654
GordonSin 0:0ed2a7c7190c 655 /**
GordonSin 0:0ed2a7c7190c 656 * Shut down one or both sides of a TCP netconn (doesn't delete it).
GordonSin 0:0ed2a7c7190c 657 *
GordonSin 0:0ed2a7c7190c 658 * @param conn the TCP netconn to shut down
GordonSin 0:0ed2a7c7190c 659 * @return ERR_OK if the netconn was closed, any other err_t on error
GordonSin 0:0ed2a7c7190c 660 */
GordonSin 0:0ed2a7c7190c 661 err_t
GordonSin 0:0ed2a7c7190c 662 netconn_shutdown(struct netconn *conn, u8_t shut_rx, u8_t shut_tx)
GordonSin 0:0ed2a7c7190c 663 {
GordonSin 0:0ed2a7c7190c 664 return netconn_close_shutdown(conn, (shut_rx ? NETCONN_SHUT_RD : 0) | (shut_tx ? NETCONN_SHUT_WR : 0));
GordonSin 0:0ed2a7c7190c 665 }
GordonSin 0:0ed2a7c7190c 666
GordonSin 0:0ed2a7c7190c 667 #if LWIP_IGMP
GordonSin 0:0ed2a7c7190c 668 /**
GordonSin 0:0ed2a7c7190c 669 * Join multicast groups for UDP netconns.
GordonSin 0:0ed2a7c7190c 670 *
GordonSin 0:0ed2a7c7190c 671 * @param conn the UDP netconn for which to change multicast addresses
GordonSin 0:0ed2a7c7190c 672 * @param multiaddr IP address of the multicast group to join or leave
GordonSin 0:0ed2a7c7190c 673 * @param netif_addr the IP address of the network interface on which to send
GordonSin 0:0ed2a7c7190c 674 * the igmp message
GordonSin 0:0ed2a7c7190c 675 * @param join_or_leave flag whether to send a join- or leave-message
GordonSin 0:0ed2a7c7190c 676 * @return ERR_OK if the action was taken, any err_t on error
GordonSin 0:0ed2a7c7190c 677 */
GordonSin 0:0ed2a7c7190c 678 err_t
GordonSin 0:0ed2a7c7190c 679 netconn_join_leave_group(struct netconn *conn,
GordonSin 0:0ed2a7c7190c 680 ip_addr_t *multiaddr,
GordonSin 0:0ed2a7c7190c 681 ip_addr_t *netif_addr,
GordonSin 0:0ed2a7c7190c 682 enum netconn_igmp join_or_leave)
GordonSin 0:0ed2a7c7190c 683 {
GordonSin 0:0ed2a7c7190c 684 struct api_msg msg;
GordonSin 0:0ed2a7c7190c 685 err_t err;
GordonSin 0:0ed2a7c7190c 686
GordonSin 0:0ed2a7c7190c 687 LWIP_ERROR("netconn_join_leave_group: invalid conn", (conn != NULL), return ERR_ARG;);
GordonSin 0:0ed2a7c7190c 688
GordonSin 0:0ed2a7c7190c 689 msg.function = do_join_leave_group;
GordonSin 0:0ed2a7c7190c 690 msg.msg.conn = conn;
GordonSin 0:0ed2a7c7190c 691 msg.msg.msg.jl.multiaddr = multiaddr;
GordonSin 0:0ed2a7c7190c 692 msg.msg.msg.jl.netif_addr = netif_addr;
GordonSin 0:0ed2a7c7190c 693 msg.msg.msg.jl.join_or_leave = join_or_leave;
GordonSin 0:0ed2a7c7190c 694 err = TCPIP_APIMSG(&msg);
GordonSin 0:0ed2a7c7190c 695
GordonSin 0:0ed2a7c7190c 696 NETCONN_SET_SAFE_ERR(conn, err);
GordonSin 0:0ed2a7c7190c 697 return err;
GordonSin 0:0ed2a7c7190c 698 }
GordonSin 0:0ed2a7c7190c 699 #endif /* LWIP_IGMP */
GordonSin 0:0ed2a7c7190c 700
GordonSin 0:0ed2a7c7190c 701 #if LWIP_DNS
GordonSin 0:0ed2a7c7190c 702 /**
GordonSin 0:0ed2a7c7190c 703 * Execute a DNS query, only one IP address is returned
GordonSin 0:0ed2a7c7190c 704 *
GordonSin 0:0ed2a7c7190c 705 * @param name a string representation of the DNS host name to query
GordonSin 0:0ed2a7c7190c 706 * @param addr a preallocated ip_addr_t where to store the resolved IP address
GordonSin 0:0ed2a7c7190c 707 * @return ERR_OK: resolving succeeded
GordonSin 0:0ed2a7c7190c 708 * ERR_MEM: memory error, try again later
GordonSin 0:0ed2a7c7190c 709 * ERR_ARG: dns client not initialized or invalid hostname
GordonSin 0:0ed2a7c7190c 710 * ERR_VAL: dns server response was invalid
GordonSin 0:0ed2a7c7190c 711 */
GordonSin 0:0ed2a7c7190c 712 err_t
GordonSin 0:0ed2a7c7190c 713 netconn_gethostbyname(const char *name, ip_addr_t *addr)
GordonSin 0:0ed2a7c7190c 714 {
GordonSin 0:0ed2a7c7190c 715 struct dns_api_msg msg;
GordonSin 0:0ed2a7c7190c 716 err_t err;
GordonSin 0:0ed2a7c7190c 717 sys_sem_t sem;
GordonSin 0:0ed2a7c7190c 718
GordonSin 0:0ed2a7c7190c 719 LWIP_ERROR("netconn_gethostbyname: invalid name", (name != NULL), return ERR_ARG;);
GordonSin 0:0ed2a7c7190c 720 LWIP_ERROR("netconn_gethostbyname: invalid addr", (addr != NULL), return ERR_ARG;);
GordonSin 0:0ed2a7c7190c 721
GordonSin 0:0ed2a7c7190c 722 err = sys_sem_new(&sem, 0);
GordonSin 0:0ed2a7c7190c 723 if (err != ERR_OK) {
GordonSin 0:0ed2a7c7190c 724 return err;
GordonSin 0:0ed2a7c7190c 725 }
GordonSin 0:0ed2a7c7190c 726
GordonSin 0:0ed2a7c7190c 727 msg.name = name;
GordonSin 0:0ed2a7c7190c 728 msg.addr = addr;
GordonSin 0:0ed2a7c7190c 729 msg.err = &err;
GordonSin 0:0ed2a7c7190c 730 msg.sem = &sem;
GordonSin 0:0ed2a7c7190c 731
GordonSin 0:0ed2a7c7190c 732 tcpip_callback(do_gethostbyname, &msg);
GordonSin 0:0ed2a7c7190c 733 sys_sem_wait(&sem);
GordonSin 0:0ed2a7c7190c 734 sys_sem_free(&sem);
GordonSin 0:0ed2a7c7190c 735
GordonSin 0:0ed2a7c7190c 736 return err;
GordonSin 0:0ed2a7c7190c 737 }
GordonSin 0:0ed2a7c7190c 738 #endif /* LWIP_DNS*/
GordonSin 0:0ed2a7c7190c 739
GordonSin 0:0ed2a7c7190c 740 #endif /* LWIP_NETCONN */