/**
* @file
* Sequential API Internal 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
* 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.
*
*
* Author: Adam Dunkels <adam@sics.se>
*
*/
#if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
#include <string.h>
} else { \
/* forward declarations */
#if LWIP_TCP
#endif
#if LWIP_RAW
/**
* Receive callback function for RAW netconns.
* Doesn't 'eat' the packet, only references it and sends it to
* conn->recvmbox
*
* @see raw.h (struct raw_pcb.recv) for parameters and return value
*/
static u8_t
{
struct pbuf *q;
#if LWIP_SO_RCVBUF
int recv_avail;
return 0;
}
#endif /* LWIP_SO_RCVBUF */
/* copy the whole packet into new pbufs */
if(q != NULL) {
pbuf_free(q);
q = NULL;
}
}
if (q != NULL) {
pbuf_free(q);
return 0;
}
buf->p = q;
return 0;
} else {
#if LWIP_SO_RCVBUF
#endif /* LWIP_SO_RCVBUF */
/* Register event with callback */
}
}
}
return 0; /* do not eat the packet */
}
#endif /* LWIP_RAW*/
#if LWIP_UDP
/**
* Receive callback function for UDP netconns.
* Posts the packet to conn->recvmbox or deletes it on memory error.
*
* @see udp.h (struct udp_pcb.recv) for parameters
*/
static void
{
#if LWIP_SO_RCVBUF
int recv_avail;
#endif /* LWIP_SO_RCVBUF */
#if LWIP_SO_RCVBUF
#else /* LWIP_SO_RCVBUF */
#endif /* LWIP_SO_RCVBUF */
pbuf_free(p);
return;
}
pbuf_free(p);
return;
} else {
buf->p = p;
{
/* get the UDP header - always in the first pbuf, ensured by udp_input */
#endif /* LWIP_CHECKSUM_ON_COPY */
}
#endif /* LWIP_NETBUF_RECVINFO */
}
return;
} else {
#if LWIP_SO_RCVBUF
#endif /* LWIP_SO_RCVBUF */
/* Register event with callback */
}
}
#endif /* LWIP_UDP */
#if LWIP_TCP
/**
* Receive callback function for TCP netconns.
* Posts the packet to conn->recvmbox, but doesn't delete it on errors.
*
* @see tcp.h (struct tcp_pcb.recv) for parameters and return value
*/
static err_t
{
return ERR_VAL;
}
/* recvmbox already deleted */
if (p != NULL) {
pbuf_free(p);
}
return ERR_OK;
}
/* Unlike for UDP or RAW pcbs, don't check for available space
using recv_avail since that could break the connection
(data is already ACKed) */
/* don't overwrite fatal errors! */
if (p != NULL) {
} else {
len = 0;
}
/* don't deallocate p: it is presented to us later again from tcp_fasttmr! */
return ERR_MEM;
} else {
#if LWIP_SO_RCVBUF
#endif /* LWIP_SO_RCVBUF */
/* Register event with callback */
}
return ERR_OK;
}
/**
* Poll callback function for TCP netconns.
* Wakes up an application thread that waits for a connection to close
* or data to be sent. The application thread then takes the
* appropriate action to go on.
*
* Signals the conn->sem.
* netconn_close waits for conn->sem if closing failed.
*
* @see tcp.h (struct tcp_pcb.poll) for parameters and return value
*/
static err_t
{
}
/* @todo: implement connect timeout here? */
/* Did a nonblocking write fail before? Then check available write-space. */
/* If the queued byte- or pbuf-count drops below the configured low-water limit,
let select mark this pcb as writable again. */
}
}
return ERR_OK;
}
/**
* Sent callback function for TCP netconns.
* Signals the conn->sem and calls API_EVENT.
* netconn_write waits for conn->sem if send buffer is low.
*
*/
static err_t
{
}
if (conn) {
/* If the queued byte- or pbuf-count drops below the configured low-water limit,
let select mark this pcb as writable again. */
}
}
return ERR_OK;
}
/**
* Error callback function for TCP netconns.
* Signals conn->sem, posts to all conn mboxes and calls API_EVENT.
* The application thread has then to decide what to do.
*
* @see tcp.h (struct tcp_pcb.err) for parameters
*/
static void
{
/* no check since this is always fatal! */
/* reset conn->state now before waking up other threads */
/* Notify the user layer about a connection error. Used to signal
select. */
/* Try to release selects pending on 'read' or 'write', too.
They will get an error if they actually try to read or write. */
/* pass NULL-message to recvmbox to wake up pending recv */
/* use trypost to prevent deadlock */
}
/* pass NULL-message to acceptmbox to wake up pending accept */
/* use trypost to preven deadlock */
}
(old_state == NETCONN_CONNECT)) {
/* calling lwip_netconn_do_writemore/lwip_netconn_do_close_internal is not necessary
since the pcb has already been deleted! */
if (!was_nonblocking_connect) {
/* set error return code */
/* wake up the waiting task */
}
} else {
}
}
/**
* Setup a tcp_pcb with the correct callback function pointers
* and their arguments.
*
* @param conn the TCP netconn to setup
*/
static void
{
}
/**
* Accept callback function for TCP netconns.
* Allocates a new netconn and posts that to conn->acceptmbox.
*
* @see tcp.h (struct tcp_pcb_listen.accept) for parameters and return value
*/
static err_t
{
LWIP_DEBUGF(API_MSG_DEBUG, ("accept_function: newpcb->tate: %s\n", tcp_debug_state_str(newpcb->state)));
return ERR_VAL;
}
/* We have to set the callback here even though
* the new socket is unknown. conn->socket is marked as -1. */
return ERR_MEM;
}
/* no protection: when creating the pcb, the netconn is not yet known
to the application thread */
/* When returning != ERR_OK, the pcb is aborted in tcp_process(),
so do nothing here! */
/* remove all references to this netconn from the pcb */
/* remove reference from to the pcb from this netconn */
/* no need to drain since we know the recvmbox is empty. */
return ERR_MEM;
} else {
/* Register event with callback */
}
return ERR_OK;
}
#endif /* LWIP_TCP */
/**
* Create a new pcb of a specific type.
* Called from lwip_netconn_do_newconn().
*
* @param msg the api_msg_msg describing the connection type
* @return msg->conn->err, but the return value is currently ignored
*/
static void
{
/* Allocate a PCB for this connection */
#if LWIP_RAW
case NETCONN_RAW:
}
break;
#endif /* LWIP_RAW */
#if LWIP_UDP
case NETCONN_UDP:
#if LWIP_UDPLITE
}
#endif /* LWIP_UDPLITE */
}
}
break;
#endif /* LWIP_UDP */
#if LWIP_TCP
case NETCONN_TCP:
}
break;
#endif /* LWIP_TCP */
default:
/* Unsupported netconn type, e.g. protocol disabled */
return;
}
}
#if LWIP_IPV6
else {
}
}
#endif /* LWIP_IPV6 */
}
/**
* Create a new pcb of a specific type inside a netconn.
* Called from netconn_new_with_proto_and_callback.
*
* @param msg the api_msg_msg describing the connection type
*/
void
{
}
/* Else? This "new" connection already has a PCB allocated. */
/* Is this an error condition? Should it be deleted? */
/* We currently just are happy and return. */
}
/**
* Create a new netconn (of a specific type) that has a callback function.
* The corresponding pcb is NOT 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*
{
int size;
return NULL;
}
/* If all sizes are the same, every compiler should optimize this switch to nothing, */
switch(NETCONNTYPE_GROUP(t)) {
#if LWIP_RAW
case NETCONN_RAW:
break;
#endif /* LWIP_RAW */
#if LWIP_UDP
case NETCONN_UDP:
break;
#endif /* LWIP_UDP */
#if LWIP_TCP
case NETCONN_TCP:
break;
#endif /* LWIP_TCP */
default:
LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0);
goto free_and_return;
}
goto free_and_return;
}
goto free_and_return;
}
#if LWIP_TCP
#endif
#if LWIP_SOCKET
/* initialize socket to -1 since 0 is a valid socket */
#endif /* LWIP_SOCKET */
#if LWIP_TCP
conn->write_offset = 0;
#endif /* LWIP_TCP */
#if LWIP_SO_SNDTIMEO
conn->send_timeout = 0;
#endif /* LWIP_SO_SNDTIMEO */
#if LWIP_SO_RCVTIMEO
conn->recv_timeout = 0;
#endif /* LWIP_SO_RCVTIMEO */
#if LWIP_SO_RCVBUF
conn->recv_avail = 0;
#endif /* LWIP_SO_RCVBUF */
return conn;
return NULL;
}
/**
* Delete a netconn and all its resources.
* The pcb is NOT freed (since we might not be in the right thread context do this).
*
* @param conn the netconn to free
*/
void
{
LWIP_ASSERT("recvmbox must be deallocated before calling this function",
#if LWIP_TCP
LWIP_ASSERT("acceptmbox must be deallocated before calling this function",
#endif /* LWIP_TCP */
}
/**
* Delete rcvmbox and acceptmbox of a netconn and free the left-over data in
* these mboxes
*
* @param conn the netconn to free
* @bytes_drained bytes drained from recvmbox
* @accepts_drained pending connections drained from acceptmbox
*/
static void
{
void *mem;
#if LWIP_TCP
struct pbuf *p;
#endif /* LWIP_TCP */
/* This runs in tcpip_thread, so we don't need to lock against rx packets */
/* Delete and drain the recvmbox. */
#if LWIP_TCP
/* pcb might be set to NULL already by err_tcp() */
}
pbuf_free(p);
}
} else
#endif /* LWIP_TCP */
{
}
}
}
/* Delete and drain the acceptmbox. */
#if LWIP_TCP
/* Only tcp pcbs have an acceptmbox, so no need to check conn->type */
/* pcb might be set to NULL already by err_tcp() */
}
/* drain recvmbox */
}
}
}
#endif /* LWIP_TCP */
}
#if LWIP_TCP
/**
* Internal helper function to close a TCP netconn: since this sometimes
* doesn't work at the first attempt, this function is called from multiple
* places.
*
* @param conn the TCP netconn to close
*/
static void
{
/* shutting down both ends is the same as closing */
/* Set back some callback pointers */
if (close) {
}
} else {
/* some callbacks have to be reset if tcp_close is not successful */
if (shut_rx) {
}
if (shut_tx) {
}
if (close) {
}
}
/* Try to close the connection */
if (close) {
} else {
}
/* Closing succeeded */
if (close) {
/* Set back some callback pointers as conn is going away */
/* Trigger select() in socket layer. Make sure everybody notices activity
on the connection, error first! */
}
if (shut_rx) {
}
if (shut_tx) {
}
/* wake up the application task */
} else {
/* Closing failed, restore some of the callbacks */
/* Closing of listen pcb will never fail! */
/* don't restore recv callback: we don't want to receive any more data */
}
/* If closing didn't succeed, we get called again either
from poll_tcp or from sent_tcp */
}
#endif /* LWIP_TCP */
/**
* Delete the pcb inside a netconn.
* Called from netconn_delete.
*
* @param msg the api_msg_msg pointing to the connection
*/
void
{
/* this only happens for TCP netconns */
LWIP_ASSERT("NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP",
} else {
LWIP_ASSERT("blocking connect in progress",
/* Drain and delete mboxes */
#if LWIP_RAW
case NETCONN_RAW:
break;
#endif /* LWIP_RAW */
#if LWIP_UDP
case NETCONN_UDP:
break;
#endif /* LWIP_UDP */
#if LWIP_TCP
case NETCONN_TCP:
/* API_EVENT is called inside lwip_netconn_do_close_internal, before releasing
the application thread, so we can return at this point! */
return;
#endif /* LWIP_TCP */
default:
break;
}
}
/* tcp netconns don't come here! */
/* @todo: this lets select make the socket readable and writable,
which is wrong! errfd instead? */
}
}
}
/**
* Bind a pcb contained in a netconn
* Called from netconn_bind.
*
* @param msg the api_msg_msg pointing to the connection and containing
* the IP address and port to bind to
*/
void
{
} else {
#if LWIP_RAW
case NETCONN_RAW:
break;
#endif /* LWIP_RAW */
#if LWIP_UDP
case NETCONN_UDP:
break;
#endif /* LWIP_UDP */
#if LWIP_TCP
case NETCONN_TCP:
break;
#endif /* LWIP_TCP */
default:
break;
}
}
}
}
#if LWIP_TCP
/**
* TCP callback function if a connection (opened by tcp_connect/lwip_netconn_do_connect) has
* been established (or reset by the remote host).
*
*/
static err_t
{
int was_blocking;
return ERR_VAL;
}
LWIP_ASSERT("(conn->current_msg != NULL) || conn->in_non_blocking_connect",
}
}
if (!was_blocking) {
}
if (was_blocking) {
}
return ERR_OK;
}
#endif /* LWIP_TCP */
/**
* Connect a pcb contained inside a netconn
* Called from netconn_connect.
*
* @param msg the api_msg_msg pointing to the connection and containing
* the IP address and port to connect to
*/
void
{
/* This may happen when calling netconn_connect() a second time */
/* For TCP, netconn_connect() calls tcpip_apimsg(), so signal op_completed here. */
return;
}
} else {
#if LWIP_RAW
case NETCONN_RAW:
break;
#endif /* LWIP_RAW */
#if LWIP_UDP
case NETCONN_UDP:
break;
#endif /* LWIP_UDP */
#if LWIP_TCP
case NETCONN_TCP:
/* Prevent connect while doing any other action. */
} else {
if (non_blocking) {
} else {
/* sys_sem_signal() is called from lwip_netconn_do_connected (or err_tcp()),
* when the connection is established! */
return;
}
}
}
/* For TCP, netconn_connect() calls tcpip_apimsg(), so signal op_completed here. */
return;
#endif /* LWIP_TCP */
default:
break;
}
}
/* For all other protocols, netconn_connect() calls TCPIP_APIMSG(),
so use TCPIP_APIMSG_ACK() here. */
}
/**
* Connect a pcb contained inside a netconn
* Only used for UDP netconns.
* Called from netconn_disconnect.
*
* @param msg the api_msg_msg pointing to the connection to disconnect
*/
void
{
#if LWIP_UDP
} else
#endif /* LWIP_UDP */
{
}
}
#if LWIP_TCP
/**
* Set a TCP pcb contained in a netconn into listen mode
* Called from netconn_listen.
*
* @param msg the api_msg_msg pointing to the connection
*/
void
{
} else {
#if LWIP_IPV6
#else /* TCP_LISTEN_BACKLOG */
#endif /* TCP_LISTEN_BACKLOG */
} else
#endif /* LWIP_IPV6 */
{
#else /* TCP_LISTEN_BACKLOG */
#endif /* TCP_LISTEN_BACKLOG */
}
/* in this case, the old pcb is still allocated */
} else {
/* delete the recvmbox and allocate the acceptmbox */
/** @todo: should we drain the recvmbox here? */
}
}
} else {
/* since the old pcb is already deallocated, free lpcb now */
}
}
}
} else {
}
}
}
}
#endif /* LWIP_TCP */
/**
* Send some data on a RAW or UDP pcb contained in a netconn
* Called from netconn_send
*
* @param msg the api_msg_msg pointing to the connection
*/
void
{
} else {
#if LWIP_RAW
case NETCONN_RAW:
} else {
}
break;
#endif
#if LWIP_UDP
case NETCONN_UDP:
} else {
}
#else /* LWIP_CHECKSUM_ON_COPY */
} else {
msg->err = udp_sendto(msg->conn->pcb.udp, msg->msg.b->p, ipX_2_ip(&msg->msg.b->addr), msg->msg.b->port);
}
#endif /* LWIP_CHECKSUM_ON_COPY */
break;
#endif /* LWIP_UDP */
default:
break;
}
}
}
}
#if LWIP_TCP
/**
* Indicate data has been received from a TCP pcb contained in a netconn
* Called from netconn_recv
*
* @param msg the api_msg_msg pointing to the connection
*/
void
{
} else
#endif /* TCP_LISTEN_BACKLOG */
{
do {
}while(remaining != 0);
}
}
}
}
/**
* See if more data needs to be written from a previous call to netconn_write.
* Called initially from lwip_netconn_do_write. If the first call can't send all data
* (because of low memory or empty send-buffer), this function is called again
* from sent_tcp() or poll_tcp() to send more data. If all data is sent, the
* blocking application thread (waiting in netconn_write) is released.
*
* @param conn netconn (that is currently in state NETCONN_WRITE) to process
* @return ERR_OK
* ERR_MEM if LWIP_TCPIP_CORE_LOCKING=1 and sending hasn't yet finished
*/
static err_t
{
void *dataptr;
LWIP_ASSERT("conn->write_offset < conn->current_msg->msg.w.len",
#if LWIP_SO_SNDTIMEO
if ((conn->send_timeout != 0) &&
write_finished = 1;
if (conn->write_offset == 0) {
/* nothing has been written */
} else {
/* partial write */
}
} else
#endif /* LWIP_SO_SNDTIMEO */
{
len = 0xffff;
#endif
} else {
}
/* don't try to write more than sendbuf */
if (dontblock){
if (!len) {
goto err_mem;
}
} else {
#endif
}
}
LWIP_ASSERT("lwip_netconn_do_writemore: invalid length!", ((conn->write_offset + len) <= conn->current_msg->msg.w.len));
/* if OK or memory error, check available space */
/* non-blocking write did not write everything: mark the pcb non-writable
and let poll_tcp check writable space to mark the pcb writable again */
/* The queued byte- or pbuf-count exceeds the configured low-water limit,
let select mark this pcb as non-writable. */
}
}
/* return sent length */
/* everything was written */
write_finished = 1;
conn->write_offset = 0;
}
/* If ERR_MEM, we wait for sent_tcp or poll_tcp to be called
we do NOT return to the application thread, since ERR_MEM is
only a temporary error! */
/* tcp_write returned ERR_MEM, try tcp_output anyway */
#endif
} else {
/* On errors != ERR_MEM, we don't try writing any more but return
the error to the application thread. */
write_finished = 1;
}
}
if (write_finished) {
/* everything was written: set back connection state
and back to application task */
#endif
{
}
}
else
return ERR_MEM;
#endif
return ERR_OK;
}
#endif /* LWIP_TCP */
/**
* Send some data on a TCP pcb contained in a netconn
* Called from netconn_write
*
* @param msg the api_msg_msg pointing to the connection
*/
void
{
} else {
#if LWIP_TCP
/* netconn is connecting, closing or in blocking write */
/* set all the variables used by lwip_netconn_do_writemore */
}
#else /* LWIP_TCPIP_CORE_LOCKING */
#endif /* LWIP_TCPIP_CORE_LOCKING */
/* for both cases: if lwip_netconn_do_writemore was called, don't ACK the APIMSG
since lwip_netconn_do_writemore ACKs it! */
return;
} else {
}
#else /* LWIP_TCP */
#endif /* LWIP_TCP */
} else {
#endif /* (LWIP_UDP || LWIP_RAW) */
}
}
}
/**
* Return a connection's local or remote address
* Called from netconn_getaddr
*
* @param msg the api_msg_msg pointing to the connection
*/
void
{
} else {
}
#if LWIP_RAW
case NETCONN_RAW:
} else {
/* return an error as connecting is only a helper for upper layers */
}
break;
#endif /* LWIP_RAW */
#if LWIP_UDP
case NETCONN_UDP:
} else {
} else {
}
}
break;
#endif /* LWIP_UDP */
#if LWIP_TCP
case NETCONN_TCP:
*(msg->msg.ad.port) = (msg->msg.ad.local?msg->conn->pcb.tcp->local_port:msg->conn->pcb.tcp->remote_port);
break;
#endif /* LWIP_TCP */
default:
LWIP_ASSERT("invalid netconn_type", 0);
break;
}
} else {
}
}
/**
* Close a TCP pcb contained in a netconn
* Called from netconn_close
*
* @param msg the api_msg_msg pointing to the connection
*/
void
{
#if LWIP_TCP
/* this only happens for TCP netconns */
LWIP_ASSERT("NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP",
/* LISTEN doesn't support half shutdown */
} else {
/* Drain and delete mboxes */
}
/* for tcp netconns, lwip_netconn_do_close_internal ACKs the message */
return;
}
} else
#endif /* LWIP_TCP */
{
}
}
/**
* Join multicast groups for UDP netconns.
* Called from netconn_join_leave_group
*
* @param msg the api_msg_msg pointing to the connection
*/
void
{
} else {
#if LWIP_UDP
#if LWIP_IPV6 && LWIP_IPV6_MLD
} else {
}
}
else
#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
{
#if LWIP_IGMP
} else {
}
#endif /* LWIP_IGMP */
}
#endif /* LWIP_UDP */
} else {
#endif /* (LWIP_TCP || LWIP_RAW) */
}
} else {
}
}
}
#endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */
#if LWIP_DNS
/**
* Callback function that is called when DNS name is resolved
* (or on timeout). A waiting application thread is waked up by
* signaling the semaphore.
*/
static void
{
/* timeout or memory error */
} else {
/* address was resolved */
}
/* wake up the application task waiting in netconn_gethostbyname */
}
/**
* Execute a DNS query
* Called from netconn_gethostbyname
*
* @param arg the dns_api_msg pointing to the query
*/
void
{
/* on error or immediate success, wake up the application
* task waiting in netconn_gethostbyname */
}
}
#endif /* LWIP_DNS */
#endif /* LWIP_NETCONN */