📄 api_lib.c
字号:
/** * @file * Sequential API External module * */ /* * Copyright (c) 2001-2004 Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. * * This file is part of the lwIP TCP/IP stack. * * Author: Adam Dunkels <adam@sics.se> * *//* This is the part of the API that is linked with the application */#include "lwip/opt.h"#if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */#include "lwip/api.h"#include "lwip/tcpip.h"#include "lwip/memp.h"#include "lwip/ip.h"#include "lwip/raw.h"#include "lwip/udp.h"#include "lwip/tcp.h"#include <string.h>/** * Create a new netconn (of a specific type) that has a callback function. * The corresponding pcb is also created. * * @param t the type of 'connection' to create (@see enum netconn_type) * @param proto the IP protocol for RAW IP pcbs * @param callback a function to call on status changes (RX available, TX'ed) * @return a newly allocated struct netconn or * NULL on memory error */struct netconn*netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto, netconn_callback callback){ struct netconn *conn; struct api_msg msg; conn = netconn_alloc(t, callback); if (conn != NULL) { msg.function = do_newconn; msg.msg.msg.n.proto = proto; msg.msg.conn = conn; if (TCPIP_APIMSG(&msg) != ERR_OK) { LWIP_ASSERT("freeing conn without freeing pcb", conn->pcb.tcp == NULL); LWIP_ASSERT("conn has no op_completed", sys_sem_valid(&conn->op_completed)); LWIP_ASSERT("conn has no recvmbox", sys_mbox_valid(&conn->recvmbox));#if LWIP_TCP LWIP_ASSERT("conn->acceptmbox shouldn't exist", !sys_mbox_valid(&conn->acceptmbox));#endif /* LWIP_TCP */ sys_sem_free(&conn->op_completed); sys_mbox_free(&conn->recvmbox); memp_free(MEMP_NETCONN, conn); return NULL; } } return conn;}/** * Close a netconn 'connection' and free its resources. * UDP and RAW connection are completely closed, TCP pcbs might still be in a waitstate * after this returns. * * @param conn the netconn to delete * @return ERR_OK if the connection was deleted */err_tnetconn_delete(struct netconn *conn){ struct api_msg msg; /* No ASSERT here because possible to get a (conn == NULL) if we got an accept error */ if (conn == NULL) { return ERR_OK; } msg.function = do_delconn; msg.msg.conn = conn; tcpip_apimsg(&msg); netconn_free(conn); /* don't care for return value of do_delconn since it only calls void functions */ return ERR_OK;}/** * Get the local or remote IP address and port of a netconn. * For RAW netconns, this returns the protocol instead of a port! * * @param conn the netconn to query * @param addr a pointer to which to save the IP address * @param port a pointer to which to save the port (or protocol for RAW) * @param local 1 to get the local IP address, 0 to get the remote one * @return ERR_CONN for invalid connections * ERR_OK if the information was retrieved */err_tnetconn_getaddr(struct netconn *conn, ip_addr_t *addr, u16_t *port, u8_t local){ struct api_msg msg; err_t err; LWIP_ERROR("netconn_getaddr: invalid conn", (conn != NULL), return ERR_ARG;); LWIP_ERROR("netconn_getaddr: invalid addr", (addr != NULL), return ERR_ARG;); LWIP_ERROR("netconn_getaddr: invalid port", (port != NULL), return ERR_ARG;); msg.function = do_getaddr; msg.msg.conn = conn; msg.msg.msg.ad.ipaddr = addr; msg.msg.msg.ad.port = port; msg.msg.msg.ad.local = local; err = TCPIP_APIMSG(&msg); NETCONN_SET_SAFE_ERR(conn, err); return err;}/** * Bind a netconn to a specific local IP address and port. * Binding one netconn twice might not always be checked correctly! * * @param conn the netconn to bind * @param addr the local IP address to bind the netconn to (use IP_ADDR_ANY * to bind to all addresses) * @param port the local port to bind the netconn to (not used for RAW) * @return ERR_OK if bound, any other err_t on failure */err_tnetconn_bind(struct netconn *conn, ip_addr_t *addr, u16_t port){ struct api_msg msg; err_t err; LWIP_ERROR("netconn_bind: invalid conn", (conn != NULL), return ERR_ARG;); msg.function = do_bind; msg.msg.conn = conn; msg.msg.msg.bc.ipaddr = addr; msg.msg.msg.bc.port = port; err = TCPIP_APIMSG(&msg); NETCONN_SET_SAFE_ERR(conn, err); return err;}/** * Connect a netconn to a specific remote IP address and port. * * @param conn the netconn to connect * @param addr the remote IP address to connect to * @param port the remote port to connect to (no used for RAW) * @return ERR_OK if connected, return value of tcp_/udp_/raw_connect otherwise */err_tnetconn_connect(struct netconn *conn, ip_addr_t *addr, u16_t port){ struct api_msg msg; err_t err; LWIP_ERROR("netconn_connect: invalid conn", (conn != NULL), return ERR_ARG;); msg.function = do_connect; msg.msg.conn = conn; msg.msg.msg.bc.ipaddr = addr; msg.msg.msg.bc.port = port; /* This is the only function which need to not block tcpip_thread */ err = tcpip_apimsg(&msg); NETCONN_SET_SAFE_ERR(conn, err); return err;}/** * Disconnect a netconn from its current peer (only valid for UDP netconns). * * @param conn the netconn to disconnect * @return TODO: return value is not set here... */err_tnetconn_disconnect(struct netconn *conn){ struct api_msg msg; err_t err; LWIP_ERROR("netconn_disconnect: invalid conn", (conn != NULL), return ERR_ARG;); msg.function = do_disconnect; msg.msg.conn = conn; err = TCPIP_APIMSG(&msg); NETCONN_SET_SAFE_ERR(conn, err); return err;}/** * Set a TCP netconn into listen mode * * @param conn the tcp netconn to set to listen mode * @param backlog the listen backlog, only used if TCP_LISTEN_BACKLOG==1 * @return ERR_OK if the netconn was set to listen (UDP and RAW netconns * don't return any error (yet?)) */err_tnetconn_listen_with_backlog(struct netconn *conn, u8_t backlog){#if LWIP_TCP struct api_msg msg; err_t err; /* This does no harm. If TCP_LISTEN_BACKLOG is off, backlog is unused. */ LWIP_UNUSED_ARG(backlog); LWIP_ERROR("netconn_listen: invalid conn", (conn != NULL), return ERR_ARG;); msg.function = do_listen; msg.msg.conn = conn;#if TCP_LISTEN_BACKLOG msg.msg.msg.lb.backlog = backlog;#endif /* TCP_LISTEN_BACKLOG */ err = TCPIP_APIMSG(&msg); NETCONN_SET_SAFE_ERR(conn, err); return err;#else /* LWIP_TCP */ LWIP_UNUSED_ARG(conn); LWIP_UNUSED_ARG(backlog); return ERR_ARG;#endif /* LWIP_TCP */}/** * Accept a new connection on a TCP listening netconn. * * @param conn the TCP listen netconn * @param new_conn pointer where the new connection is stored * @return ERR_OK if a new connection has been received or an error * code otherwise */err_tnetconn_accept(struct netconn *conn, struct netconn **new_conn){#if LWIP_TCP struct netconn *newconn; err_t err;#if TCP_LISTEN_BACKLOG struct api_msg msg;#endif /* TCP_LISTEN_BACKLOG */ LWIP_ERROR("netconn_accept: invalid pointer", (new_conn != NULL), return ERR_ARG;); *new_conn = NULL; LWIP_ERROR("netconn_accept: invalid conn", (conn != NULL), return ERR_ARG;); LWIP_ERROR("netconn_accept: invalid acceptmbox", sys_mbox_valid(&conn->acceptmbox), return ERR_ARG;); err = conn->last_err; if (ERR_IS_FATAL(err)) { /* don't recv on fatal errors: this might block the application task waiting on acceptmbox forever! */ return err; }#if LWIP_SO_RCVTIMEO if (sys_arch_mbox_fetch(&conn->acceptmbox, (void **)&newconn, conn->recv_timeout) == SYS_ARCH_TIMEOUT) { NETCONN_SET_SAFE_ERR(conn, ERR_TIMEOUT); return ERR_TIMEOUT; }#else sys_arch_mbox_fetch(&conn->acceptmbox, (void **)&newconn, 0);#endif /* LWIP_SO_RCVTIMEO*/ /* Register event with callback */ API_EVENT(conn, NETCONN_EVT_RCVMINUS, 0); if (newconn == NULL) { /* connection has been aborted */ NETCONN_SET_SAFE_ERR(conn, ERR_ABRT); return ERR_ABRT; }#if TCP_LISTEN_BACKLOG /* Let the stack know that we have accepted the connection. */ msg.function = do_recv; msg.msg.conn = conn; /* don't care for the return value of do_recv */ TCPIP_APIMSG(&msg);#endif /* TCP_LISTEN_BACKLOG */ *new_conn = newconn; /* don't set conn->last_err: it's only ERR_OK, anyway */ return ERR_OK;#else /* LWIP_TCP */ LWIP_UNUSED_ARG(conn); LWIP_UNUSED_ARG(new_conn); return ERR_ARG;#endif /* LWIP_TCP */}/** * Receive data: actual implementation that doesn't care whether pbuf or netbuf * is received * * @param conn the netconn from which to receive data * @param new_buf pointer where a new pbuf/netbuf is stored when received data * @return ERR_OK if data has been received, an error code otherwise (timeout, * memory error or another error) */static err_tnetconn_recv_data(struct netconn *conn, void **new_buf){ void *buf = NULL; u16_t len; err_t err;#if LWIP_TCP struct api_msg msg;#endif /* LWIP_TCP */ LWIP_ERROR("netconn_recv: invalid pointer", (new_buf != NULL), return ERR_ARG;); *new_buf = NULL; LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL), return ERR_ARG;); LWIP_ERROR("netconn_accept: invalid recvmbox", sys_mbox_valid(&conn->recvmbox), return ERR_CONN;); err = conn->last_err; if (ERR_IS_FATAL(err)) { /* don't recv on fatal errors: this might block the application task waiting on recvmbox forever! */ /* @todo: this does not allow us to fetch data that has been put into recvmbox before the fatal error occurred - is that a problem? */ return err; }#if LWIP_SO_RCVTIMEO if (sys_arch_mbox_fetch(&conn->recvmbox, &buf, conn->recv_timeout) == SYS_ARCH_TIMEOUT) { NETCONN_SET_SAFE_ERR(conn, ERR_TIMEOUT); return ERR_TIMEOUT; }#else sys_arch_mbox_fetch(&conn->recvmbox, &buf, 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -