2002-10-19 12:59:30 +00:00
|
|
|
/*
|
2004-02-07 00:30:03 +00:00
|
|
|
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
2002-10-19 12:59:30 +00:00
|
|
|
* 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 */
|
|
|
|
|
2003-02-21 16:43:46 +00:00
|
|
|
#include "lwip/opt.h"
|
2002-10-19 12:59:30 +00:00
|
|
|
#include "lwip/api.h"
|
|
|
|
#include "lwip/api_msg.h"
|
|
|
|
#include "lwip/memp.h"
|
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
struct
|
|
|
|
netbuf *netbuf_new(void)
|
|
|
|
{
|
|
|
|
struct netbuf *buf;
|
|
|
|
|
2003-06-27 20:46:11 +00:00
|
|
|
buf = memp_malloc(MEMP_NETBUF);
|
2003-05-01 13:24:01 +00:00
|
|
|
if (buf != NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
buf->p = NULL;
|
|
|
|
buf->ptr = NULL;
|
|
|
|
return buf;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
void
|
|
|
|
netbuf_delete(struct netbuf *buf)
|
|
|
|
{
|
2003-05-01 13:24:01 +00:00
|
|
|
if (buf != NULL) {
|
|
|
|
if (buf->p != NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
pbuf_free(buf->p);
|
|
|
|
buf->p = buf->ptr = NULL;
|
|
|
|
}
|
2003-06-27 20:46:11 +00:00
|
|
|
memp_free(MEMP_NETBUF, buf);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
void *
|
|
|
|
netbuf_alloc(struct netbuf *buf, u16_t size)
|
|
|
|
{
|
|
|
|
/* Deallocate any previously allocated memory. */
|
2003-05-01 13:24:01 +00:00
|
|
|
if (buf->p != NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
pbuf_free(buf->p);
|
|
|
|
}
|
|
|
|
buf->p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);
|
2003-05-01 13:24:01 +00:00
|
|
|
if (buf->p == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
buf->ptr = buf->p;
|
|
|
|
return buf->p->payload;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
void
|
|
|
|
netbuf_free(struct netbuf *buf)
|
|
|
|
{
|
2003-05-01 13:24:01 +00:00
|
|
|
if (buf->p != NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
pbuf_free(buf->p);
|
|
|
|
}
|
|
|
|
buf->p = buf->ptr = NULL;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
void
|
|
|
|
netbuf_ref(struct netbuf *buf, void *dataptr, u16_t size)
|
|
|
|
{
|
2003-05-01 13:24:01 +00:00
|
|
|
if (buf->p != NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
pbuf_free(buf->p);
|
|
|
|
}
|
2003-03-19 22:14:49 +00:00
|
|
|
buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF);
|
2002-10-19 12:59:30 +00:00
|
|
|
buf->p->payload = dataptr;
|
|
|
|
buf->p->len = buf->p->tot_len = size;
|
|
|
|
buf->ptr = buf->p;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
void
|
|
|
|
netbuf_chain(struct netbuf *head, struct netbuf *tail)
|
|
|
|
{
|
|
|
|
pbuf_chain(head->p, tail->p);
|
|
|
|
head->ptr = head->p;
|
2003-06-27 20:46:11 +00:00
|
|
|
memp_free(MEMP_NETBUF, tail);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
u16_t
|
|
|
|
netbuf_len(struct netbuf *buf)
|
|
|
|
{
|
|
|
|
return buf->p->tot_len;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
|
|
|
netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len)
|
|
|
|
{
|
2003-05-01 13:24:01 +00:00
|
|
|
if (buf->ptr == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_BUF;
|
|
|
|
}
|
|
|
|
*dataptr = buf->ptr->payload;
|
|
|
|
*len = buf->ptr->len;
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
s8_t
|
|
|
|
netbuf_next(struct netbuf *buf)
|
|
|
|
{
|
2003-05-01 13:24:01 +00:00
|
|
|
if (buf->ptr->next == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
buf->ptr = buf->ptr->next;
|
2003-05-01 13:24:01 +00:00
|
|
|
if (buf->ptr->next == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
void
|
|
|
|
netbuf_first(struct netbuf *buf)
|
|
|
|
{
|
|
|
|
buf->ptr = buf->p;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
void
|
|
|
|
netbuf_copy_partial(struct netbuf *buf, void *dataptr, u16_t len, u16_t offset)
|
|
|
|
{
|
|
|
|
struct pbuf *p;
|
2007-02-26 19:49:49 +00:00
|
|
|
u16_t left;
|
|
|
|
u16_t buf_copy_len;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
|
|
|
left = 0;
|
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
if(buf == NULL || dataptr == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-26 19:49:49 +00:00
|
|
|
/* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
|
|
|
|
for(p = buf->p; len != 0 && p != NULL; p = p->next) {
|
2003-05-01 13:24:01 +00:00
|
|
|
if (offset != 0 && offset >= p->len) {
|
2007-02-26 19:49:49 +00:00
|
|
|
/* don't copy from this buffer -> on to the next */
|
2002-10-19 12:59:30 +00:00
|
|
|
offset -= p->len;
|
2007-02-26 19:49:49 +00:00
|
|
|
} else {
|
|
|
|
/* copy from this buffer. maybe only partially. */
|
|
|
|
buf_copy_len = p->len - offset;
|
|
|
|
if (buf_copy_len > len)
|
|
|
|
buf_copy_len = len;
|
|
|
|
/* copy the necessary parts of the buffer */
|
|
|
|
memcpy(&((char*)dataptr)[left], &((char*)p->payload)[offset], buf_copy_len);
|
|
|
|
left += buf_copy_len;
|
|
|
|
len -= buf_copy_len;
|
2002-10-19 12:59:30 +00:00
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
void
|
|
|
|
netbuf_copy(struct netbuf *buf, void *dataptr, u16_t len)
|
|
|
|
{
|
|
|
|
netbuf_copy_partial(buf, dataptr, len, 0);
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
struct ip_addr *
|
|
|
|
netbuf_fromaddr(struct netbuf *buf)
|
|
|
|
{
|
|
|
|
return buf->fromaddr;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
u16_t
|
|
|
|
netbuf_fromport(struct netbuf *buf)
|
|
|
|
{
|
|
|
|
return buf->fromport;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
struct
|
2003-11-14 13:17:23 +00:00
|
|
|
netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u16_t proto,
|
|
|
|
void (*callback)(struct netconn *, enum netconn_evt, u16_t len))
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
|
|
|
struct netconn *conn;
|
2007-02-28 14:15:43 +00:00
|
|
|
struct api_msg _msg;
|
|
|
|
struct api_msg *msg = &_msg;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2003-06-27 20:46:11 +00:00
|
|
|
conn = memp_malloc(MEMP_NETCONN);
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
|
|
|
conn->err = ERR_OK;
|
2002-10-19 12:59:30 +00:00
|
|
|
conn->type = t;
|
|
|
|
conn->pcb.tcp = NULL;
|
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if ((conn->mbox = sys_mbox_new()) == SYS_MBOX_NULL) {
|
2003-06-27 20:46:11 +00:00
|
|
|
memp_free(MEMP_NETCONN, conn);
|
2002-10-19 12:59:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
conn->recvmbox = SYS_MBOX_NULL;
|
|
|
|
conn->acceptmbox = SYS_MBOX_NULL;
|
2006-05-26 07:39:39 +00:00
|
|
|
conn->sem = sys_sem_new(0);
|
|
|
|
if (conn->sem == SYS_SEM_NULL) {
|
|
|
|
memp_free(MEMP_NETCONN, conn);
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
conn->state = NETCONN_NONE;
|
2003-02-06 22:18:56 +00:00
|
|
|
conn->socket = 0;
|
2003-11-14 13:17:23 +00:00
|
|
|
conn->callback = callback;
|
2003-02-06 22:18:56 +00:00
|
|
|
conn->recv_avail = 0;
|
2003-11-14 13:17:23 +00:00
|
|
|
|
|
|
|
msg->type = API_MSG_NEWCONN;
|
|
|
|
msg->msg.msg.bc.port = proto; /* misusing the port field */
|
|
|
|
msg->msg.conn = conn;
|
|
|
|
api_msg_post(msg);
|
|
|
|
sys_mbox_fetch(conn->mbox, NULL);
|
|
|
|
|
|
|
|
if ( conn->err != ERR_OK ) {
|
|
|
|
memp_free(MEMP_NETCONN, conn);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
return conn;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct
|
|
|
|
netconn *netconn_new(enum netconn_type t)
|
|
|
|
{
|
|
|
|
return netconn_new_with_proto_and_callback(t,0,NULL);
|
|
|
|
}
|
|
|
|
|
2003-02-06 22:18:56 +00:00
|
|
|
struct
|
|
|
|
netconn *netconn_new_with_callback(enum netconn_type t,
|
|
|
|
void (*callback)(struct netconn *, enum netconn_evt, u16_t len))
|
|
|
|
{
|
2003-11-14 13:17:23 +00:00
|
|
|
return netconn_new_with_proto_and_callback(t,0,callback);
|
2003-02-06 22:18:56 +00:00
|
|
|
}
|
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
|
|
|
netconn_delete(struct netconn *conn)
|
|
|
|
{
|
2007-02-28 14:15:43 +00:00
|
|
|
struct api_msg _msg;
|
|
|
|
struct api_msg *msg = &_msg;
|
2002-10-19 12:59:30 +00:00
|
|
|
void *mem;
|
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg->type = API_MSG_DELCONN;
|
|
|
|
msg->msg.conn = conn;
|
|
|
|
api_msg_post(msg);
|
|
|
|
sys_mbox_fetch(conn->mbox, NULL);
|
|
|
|
|
|
|
|
/* Drain the recvmbox. */
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn->recvmbox != SYS_MBOX_NULL) {
|
|
|
|
while (sys_arch_mbox_fetch(conn->recvmbox, &mem, 1) != SYS_ARCH_TIMEOUT) {
|
|
|
|
if (conn->type == NETCONN_TCP) {
|
2004-09-20 16:58:01 +00:00
|
|
|
if(mem != NULL)
|
|
|
|
pbuf_free((struct pbuf *)mem);
|
2002-10-19 12:59:30 +00:00
|
|
|
} else {
|
2004-09-20 16:58:01 +00:00
|
|
|
netbuf_delete((struct netbuf *)mem);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sys_mbox_free(conn->recvmbox);
|
|
|
|
conn->recvmbox = SYS_MBOX_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Drain the acceptmbox. */
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn->acceptmbox != SYS_MBOX_NULL) {
|
|
|
|
while (sys_arch_mbox_fetch(conn->acceptmbox, &mem, 1) != SYS_ARCH_TIMEOUT) {
|
2002-10-19 12:59:30 +00:00
|
|
|
netconn_delete((struct netconn *)mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
sys_mbox_free(conn->acceptmbox);
|
|
|
|
conn->acceptmbox = SYS_MBOX_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
sys_mbox_free(conn->mbox);
|
|
|
|
conn->mbox = SYS_MBOX_NULL;
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn->sem != SYS_SEM_NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
sys_sem_free(conn->sem);
|
|
|
|
}
|
|
|
|
/* conn->sem = SYS_SEM_NULL;*/
|
|
|
|
memp_free(MEMP_NETCONN, conn);
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
enum netconn_type
|
|
|
|
netconn_type(struct netconn *conn)
|
|
|
|
{
|
|
|
|
return conn->type;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
2003-01-24 09:24:44 +00:00
|
|
|
netconn_peer(struct netconn *conn, struct ip_addr *addr,
|
2003-06-09 21:14:47 +00:00
|
|
|
u16_t *port)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2003-05-01 13:24:01 +00:00
|
|
|
switch (conn->type) {
|
2003-11-14 13:17:23 +00:00
|
|
|
case NETCONN_RAW:
|
|
|
|
/* return an error as connecting is only a helper for upper layers */
|
|
|
|
return ERR_CONN;
|
2002-10-19 12:59:30 +00:00
|
|
|
case NETCONN_UDPLITE:
|
|
|
|
case NETCONN_UDPNOCHKSUM:
|
|
|
|
case NETCONN_UDP:
|
2003-03-31 09:34:56 +00:00
|
|
|
if (conn->pcb.udp == NULL ||
|
2003-06-09 21:14:47 +00:00
|
|
|
((conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0))
|
2003-03-31 09:34:56 +00:00
|
|
|
return ERR_CONN;
|
2003-01-24 09:24:44 +00:00
|
|
|
*addr = (conn->pcb.udp->remote_ip);
|
2002-10-19 12:59:30 +00:00
|
|
|
*port = conn->pcb.udp->remote_port;
|
|
|
|
break;
|
|
|
|
case NETCONN_TCP:
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn->pcb.tcp == NULL)
|
2003-03-31 09:34:56 +00:00
|
|
|
return ERR_CONN;
|
2003-01-24 09:24:44 +00:00
|
|
|
*addr = (conn->pcb.tcp->remote_ip);
|
2002-10-19 12:59:30 +00:00
|
|
|
*port = conn->pcb.tcp->remote_port;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (conn->err = ERR_OK);
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
|
|
|
netconn_addr(struct netconn *conn, struct ip_addr **addr,
|
2003-06-09 21:14:47 +00:00
|
|
|
u16_t *port)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2003-05-01 13:24:01 +00:00
|
|
|
switch (conn->type) {
|
2003-11-14 13:17:23 +00:00
|
|
|
case NETCONN_RAW:
|
|
|
|
*addr = &(conn->pcb.raw->local_ip);
|
|
|
|
*port = conn->pcb.raw->protocol;
|
|
|
|
break;
|
2002-10-19 12:59:30 +00:00
|
|
|
case NETCONN_UDPLITE:
|
|
|
|
case NETCONN_UDPNOCHKSUM:
|
|
|
|
case NETCONN_UDP:
|
|
|
|
*addr = &(conn->pcb.udp->local_ip);
|
|
|
|
*port = conn->pcb.udp->local_port;
|
|
|
|
break;
|
|
|
|
case NETCONN_TCP:
|
|
|
|
*addr = &(conn->pcb.tcp->local_ip);
|
|
|
|
*port = conn->pcb.tcp->local_port;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (conn->err = ERR_OK);
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
|
|
|
netconn_bind(struct netconn *conn, struct ip_addr *addr,
|
2003-06-09 21:14:47 +00:00
|
|
|
u16_t port)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2007-02-28 14:15:43 +00:00
|
|
|
struct api_msg _msg;
|
|
|
|
struct api_msg *msg = &_msg;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_VAL;
|
|
|
|
}
|
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn->type != NETCONN_TCP &&
|
2002-10-19 12:59:30 +00:00
|
|
|
conn->recvmbox == SYS_MBOX_NULL) {
|
2003-05-01 13:24:01 +00:00
|
|
|
if ((conn->recvmbox = sys_mbox_new()) == SYS_MBOX_NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msg->type = API_MSG_BIND;
|
|
|
|
msg->msg.conn = conn;
|
|
|
|
msg->msg.msg.bc.ipaddr = addr;
|
|
|
|
msg->msg.msg.bc.port = port;
|
|
|
|
api_msg_post(msg);
|
|
|
|
sys_mbox_fetch(conn->mbox, NULL);
|
|
|
|
return conn->err;
|
|
|
|
}
|
2003-01-22 16:18:05 +00:00
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
|
|
|
netconn_connect(struct netconn *conn, struct ip_addr *addr,
|
2003-06-09 21:14:47 +00:00
|
|
|
u16_t port)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2007-02-28 14:15:43 +00:00
|
|
|
struct api_msg _msg;
|
|
|
|
struct api_msg *msg = &_msg;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_VAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn->recvmbox == SYS_MBOX_NULL) {
|
|
|
|
if ((conn->recvmbox = sys_mbox_new()) == SYS_MBOX_NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msg->type = API_MSG_CONNECT;
|
|
|
|
msg->msg.conn = conn;
|
|
|
|
msg->msg.msg.bc.ipaddr = addr;
|
|
|
|
msg->msg.msg.bc.port = port;
|
|
|
|
api_msg_post(msg);
|
|
|
|
sys_mbox_fetch(conn->mbox, NULL);
|
|
|
|
return conn->err;
|
|
|
|
}
|
2003-01-22 16:18:05 +00:00
|
|
|
|
|
|
|
err_t
|
|
|
|
netconn_disconnect(struct netconn *conn)
|
|
|
|
{
|
2007-02-28 14:15:43 +00:00
|
|
|
struct api_msg _msg;
|
|
|
|
struct api_msg *msg = &_msg;
|
2003-01-22 16:18:05 +00:00
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn == NULL) {
|
2003-01-22 16:18:05 +00:00
|
|
|
return ERR_VAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg->type = API_MSG_DISCONNECT;
|
|
|
|
msg->msg.conn = conn;
|
|
|
|
api_msg_post(msg);
|
2003-01-24 09:24:44 +00:00
|
|
|
sys_mbox_fetch(conn->mbox, NULL);
|
2003-01-22 16:18:05 +00:00
|
|
|
return conn->err;
|
|
|
|
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
|
|
|
netconn_listen(struct netconn *conn)
|
|
|
|
{
|
2007-02-28 14:15:43 +00:00
|
|
|
struct api_msg _msg;
|
|
|
|
struct api_msg *msg = &_msg;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_VAL;
|
|
|
|
}
|
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn->acceptmbox == SYS_MBOX_NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
conn->acceptmbox = sys_mbox_new();
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn->acceptmbox == SYS_MBOX_NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msg->type = API_MSG_LISTEN;
|
|
|
|
msg->msg.conn = conn;
|
|
|
|
api_msg_post(msg);
|
|
|
|
sys_mbox_fetch(conn->mbox, NULL);
|
|
|
|
return conn->err;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
struct netconn *
|
|
|
|
netconn_accept(struct netconn *conn)
|
|
|
|
{
|
|
|
|
struct netconn *newconn;
|
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-10-11 12:04:04 +00:00
|
|
|
sys_mbox_fetch(conn->acceptmbox, (void *)&newconn);
|
2003-02-06 22:18:56 +00:00
|
|
|
/* Register event with callback */
|
|
|
|
if (conn->callback)
|
|
|
|
(*conn->callback)(conn, NETCONN_EVT_RCVMINUS, 0);
|
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
return newconn;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
struct netbuf *
|
|
|
|
netconn_recv(struct netconn *conn)
|
|
|
|
{
|
2007-02-28 14:15:43 +00:00
|
|
|
struct api_msg _msg;
|
|
|
|
struct api_msg *msg = &_msg;
|
2002-10-19 12:59:30 +00:00
|
|
|
struct netbuf *buf;
|
|
|
|
struct pbuf *p;
|
2003-02-11 21:00:14 +00:00
|
|
|
u16_t len;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn->recvmbox == SYS_MBOX_NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
conn->err = ERR_CONN;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn->err != ERR_OK) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn->type == NETCONN_TCP) {
|
|
|
|
if (conn->pcb.tcp->state == LISTEN) {
|
2002-10-19 12:59:30 +00:00
|
|
|
conn->err = ERR_CONN;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-27 20:46:11 +00:00
|
|
|
buf = memp_malloc(MEMP_NETBUF);
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (buf == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
conn->err = ERR_MEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-10-11 12:04:04 +00:00
|
|
|
sys_mbox_fetch(conn->recvmbox, (void *)&p);
|
2003-02-11 16:33:02 +00:00
|
|
|
|
2003-02-11 21:00:14 +00:00
|
|
|
if (p != NULL)
|
|
|
|
{
|
|
|
|
len = p->tot_len;
|
|
|
|
conn->recv_avail -= len;
|
2003-02-11 16:33:02 +00:00
|
|
|
}
|
2003-02-11 21:00:14 +00:00
|
|
|
else
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
/* Register event with callback */
|
|
|
|
if (conn->callback)
|
|
|
|
(*conn->callback)(conn, NETCONN_EVT_RCVMINUS, len);
|
2003-02-06 22:18:56 +00:00
|
|
|
|
2003-05-19 14:41:54 +00:00
|
|
|
/* If we are closed, we indicate that we no longer wish to receive
|
2002-10-19 12:59:30 +00:00
|
|
|
data by setting conn->recvmbox to SYS_MBOX_NULL. */
|
2003-05-01 13:24:01 +00:00
|
|
|
if (p == NULL) {
|
2003-06-27 20:46:11 +00:00
|
|
|
memp_free(MEMP_NETBUF, buf);
|
2002-10-19 12:59:30 +00:00
|
|
|
sys_mbox_free(conn->recvmbox);
|
|
|
|
conn->recvmbox = SYS_MBOX_NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf->p = p;
|
|
|
|
buf->ptr = p;
|
|
|
|
buf->fromport = 0;
|
|
|
|
buf->fromaddr = NULL;
|
|
|
|
|
|
|
|
/* Let the stack know that we have taken the data. */
|
|
|
|
msg->type = API_MSG_RECV;
|
|
|
|
msg->msg.conn = conn;
|
2003-05-01 13:24:01 +00:00
|
|
|
if (buf != NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
msg->msg.msg.len = buf->p->tot_len;
|
|
|
|
} else {
|
|
|
|
msg->msg.msg.len = 1;
|
|
|
|
}
|
|
|
|
api_msg_post(msg);
|
|
|
|
|
|
|
|
sys_mbox_fetch(conn->mbox, NULL);
|
|
|
|
} else {
|
2006-10-11 12:04:04 +00:00
|
|
|
sys_mbox_fetch(conn->recvmbox, (void *)&buf);
|
2003-06-09 21:14:47 +00:00
|
|
|
conn->recv_avail -= buf->p->tot_len;
|
2003-02-06 22:18:56 +00:00
|
|
|
/* Register event with callback */
|
|
|
|
if (conn->callback)
|
|
|
|
(*conn->callback)(conn, NETCONN_EVT_RCVMINUS, buf->p->tot_len);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_recv: received %p (err %d)\n", (void *)buf, conn->err));
|
2002-10-19 12:59:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
|
|
|
netconn_send(struct netconn *conn, struct netbuf *buf)
|
|
|
|
{
|
2007-02-28 14:15:43 +00:00
|
|
|
struct api_msg _msg;
|
|
|
|
struct api_msg *msg = &_msg;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_VAL;
|
|
|
|
}
|
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn->err != ERR_OK) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return conn->err;
|
|
|
|
}
|
|
|
|
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %d bytes\n", buf->p->tot_len));
|
2002-10-19 12:59:30 +00:00
|
|
|
msg->type = API_MSG_SEND;
|
|
|
|
msg->msg.conn = conn;
|
|
|
|
msg->msg.msg.p = buf->p;
|
|
|
|
api_msg_post(msg);
|
|
|
|
|
|
|
|
sys_mbox_fetch(conn->mbox, NULL);
|
|
|
|
return conn->err;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
|
|
|
netconn_write(struct netconn *conn, void *dataptr, u16_t size, u8_t copy)
|
|
|
|
{
|
2007-02-28 14:15:43 +00:00
|
|
|
struct api_msg _msg;
|
|
|
|
struct api_msg *msg = &_msg;
|
2002-10-19 12:59:30 +00:00
|
|
|
u16_t len;
|
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_VAL;
|
|
|
|
}
|
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn->err != ERR_OK) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return conn->err;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg->type = API_MSG_WRITE;
|
|
|
|
msg->msg.conn = conn;
|
|
|
|
|
|
|
|
|
|
|
|
conn->state = NETCONN_WRITE;
|
2003-05-01 13:24:01 +00:00
|
|
|
while (conn->err == ERR_OK && size > 0) {
|
2002-10-19 12:59:30 +00:00
|
|
|
msg->msg.msg.w.dataptr = dataptr;
|
|
|
|
msg->msg.msg.w.copy = copy;
|
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn->type == NETCONN_TCP) {
|
|
|
|
if (tcp_sndbuf(conn->pcb.tcp) == 0) {
|
2003-06-09 21:14:47 +00:00
|
|
|
sys_sem_wait(conn->sem);
|
|
|
|
if (conn->err != ERR_OK) {
|
|
|
|
goto ret;
|
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-05-01 13:24:01 +00:00
|
|
|
if (size > tcp_sndbuf(conn->pcb.tcp)) {
|
2003-06-09 21:14:47 +00:00
|
|
|
/* We cannot send more than one send buffer's worth of data at a
|
|
|
|
time. */
|
|
|
|
len = tcp_sndbuf(conn->pcb.tcp);
|
2002-10-19 12:59:30 +00:00
|
|
|
} else {
|
2003-06-09 21:14:47 +00:00
|
|
|
len = size;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
len = size;
|
|
|
|
}
|
|
|
|
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_write: writing %d bytes (%d)\n", len, copy));
|
2002-10-19 12:59:30 +00:00
|
|
|
msg->msg.msg.w.len = len;
|
|
|
|
api_msg_post(msg);
|
|
|
|
sys_mbox_fetch(conn->mbox, NULL);
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn->err == ERR_OK) {
|
2005-11-25 12:03:38 +00:00
|
|
|
dataptr = (void *)((u8_t *)dataptr + len);
|
2002-10-19 12:59:30 +00:00
|
|
|
size -= len;
|
2003-05-01 13:24:01 +00:00
|
|
|
} else if (conn->err == ERR_MEM) {
|
2002-10-19 12:59:30 +00:00
|
|
|
conn->err = ERR_OK;
|
|
|
|
sys_sem_wait(conn->sem);
|
|
|
|
} else {
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret:
|
|
|
|
conn->state = NETCONN_NONE;
|
2003-02-06 22:18:56 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
return conn->err;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
|
|
|
netconn_close(struct netconn *conn)
|
|
|
|
{
|
2007-02-28 14:15:43 +00:00
|
|
|
struct api_msg _msg;
|
|
|
|
struct api_msg *msg = &_msg;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_VAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->state = NETCONN_CLOSE;
|
|
|
|
again:
|
|
|
|
msg->type = API_MSG_CLOSE;
|
|
|
|
msg->msg.conn = conn;
|
|
|
|
api_msg_post(msg);
|
|
|
|
sys_mbox_fetch(conn->mbox, NULL);
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn->err == ERR_MEM &&
|
2002-10-19 12:59:30 +00:00
|
|
|
conn->sem != SYS_SEM_NULL) {
|
|
|
|
sys_sem_wait(conn->sem);
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
conn->state = NETCONN_NONE;
|
|
|
|
return conn->err;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
|
|
|
netconn_err(struct netconn *conn)
|
|
|
|
{
|
|
|
|
return conn->err;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|