2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
27 * This file is part of the lwIP TCP/IP stack.
29 * Author: Adam Dunkels <adam@sics.se>
33 /* This is the part of the API that is linked with
38 #include "lwip/api_msg.h"
39 #include "lwip/memp.h"
43 netbuf *netbuf_new(void)
47 buf = memp_malloc(MEMP_NETBUF);
58 netbuf_delete(struct netbuf *buf)
63 buf->p = buf->ptr = NULL;
65 memp_free(MEMP_NETBUF, buf);
70 netbuf_alloc(struct netbuf *buf, u16_t size)
72 /* Deallocate any previously allocated memory. */
76 buf->p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);
81 return buf->p->payload;
85 netbuf_free(struct netbuf *buf)
90 buf->p = buf->ptr = NULL;
94 netbuf_ref(struct netbuf *buf, void *dataptr, u16_t size)
99 buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF);
100 buf->p->payload = dataptr;
101 buf->p->len = buf->p->tot_len = size;
106 netbuf_chain(struct netbuf *head, struct netbuf *tail)
108 pbuf_chain(head->p, tail->p);
110 memp_free(MEMP_NETBUF, tail);
114 netbuf_len(struct netbuf *buf)
116 return buf->p->tot_len;
120 netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len)
122 if (buf->ptr == NULL) {
125 *dataptr = buf->ptr->payload;
126 *len = buf->ptr->len;
131 netbuf_next(struct netbuf *buf)
133 if (buf->ptr->next == NULL) {
136 buf->ptr = buf->ptr->next;
137 if (buf->ptr->next == NULL) {
144 netbuf_first(struct netbuf *buf)
150 netbuf_copy_partial(struct netbuf *buf, void *dataptr, u16_t len, u16_t offset)
157 if(buf == NULL || dataptr == NULL) {
161 /* This implementation is bad. It should use bcopy
163 for(p = buf->p; left < len && p != NULL; p = p->next) {
164 if (offset != 0 && offset >= p->len) {
167 for(i = offset; i < p->len; ++i) {
168 ((u8_t *)dataptr)[left] = ((u8_t *)p->payload)[i];
179 netbuf_copy(struct netbuf *buf, void *dataptr, u16_t len)
181 netbuf_copy_partial(buf, dataptr, len, 0);
185 netbuf_fromaddr(struct netbuf *buf)
187 return buf->fromaddr;
191 netbuf_fromport(struct netbuf *buf)
193 return buf->fromport;
197 netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u16_t proto,
198 void (*callback)(struct netconn *, enum netconn_evt, u16_t len))
200 struct netconn *conn;
203 conn = memp_malloc(MEMP_NETCONN);
210 conn->pcb.tcp = NULL;
212 if ((conn->mbox = sys_mbox_new()) == SYS_MBOX_NULL) {
213 memp_free(MEMP_NETCONN, conn);
216 conn->recvmbox = SYS_MBOX_NULL;
217 conn->acceptmbox = SYS_MBOX_NULL;
218 conn->sem = sys_sem_new(0);
219 if (conn->sem == SYS_SEM_NULL) {
220 memp_free(MEMP_NETCONN, conn);
223 conn->state = NETCONN_NONE;
225 conn->callback = callback;
226 conn->recv_avail = 0;
228 if((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
229 memp_free(MEMP_NETCONN, conn);
233 msg->type = API_MSG_NEWCONN;
234 msg->msg.msg.bc.port = proto; /* misusing the port field */
235 msg->msg.conn = conn;
237 sys_mbox_fetch(conn->mbox, NULL);
238 memp_free(MEMP_API_MSG, msg);
240 if ( conn->err != ERR_OK ) {
241 memp_free(MEMP_NETCONN, conn);
250 netconn *netconn_new(enum netconn_type t)
252 return netconn_new_with_proto_and_callback(t,0,NULL);
256 netconn *netconn_new_with_callback(enum netconn_type t,
257 void (*callback)(struct netconn *, enum netconn_evt, u16_t len))
259 return netconn_new_with_proto_and_callback(t,0,callback);
264 netconn_delete(struct netconn *conn)
273 if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
277 msg->type = API_MSG_DELCONN;
278 msg->msg.conn = conn;
280 sys_mbox_fetch(conn->mbox, NULL);
281 memp_free(MEMP_API_MSG, msg);
283 /* Drain the recvmbox. */
284 if (conn->recvmbox != SYS_MBOX_NULL) {
285 while (sys_arch_mbox_fetch(conn->recvmbox, &mem, 1) != SYS_ARCH_TIMEOUT) {
286 if (conn->type == NETCONN_TCP) {
288 pbuf_free((struct pbuf *)mem);
290 netbuf_delete((struct netbuf *)mem);
293 sys_mbox_free(conn->recvmbox);
294 conn->recvmbox = SYS_MBOX_NULL;
298 /* Drain the acceptmbox. */
299 if (conn->acceptmbox != SYS_MBOX_NULL) {
300 while (sys_arch_mbox_fetch(conn->acceptmbox, &mem, 1) != SYS_ARCH_TIMEOUT) {
301 netconn_delete((struct netconn *)mem);
304 sys_mbox_free(conn->acceptmbox);
305 conn->acceptmbox = SYS_MBOX_NULL;
308 sys_mbox_free(conn->mbox);
309 conn->mbox = SYS_MBOX_NULL;
310 if (conn->sem != SYS_SEM_NULL) {
311 sys_sem_free(conn->sem);
313 /* conn->sem = SYS_SEM_NULL;*/
314 memp_free(MEMP_NETCONN, conn);
319 netconn_type(struct netconn *conn)
325 netconn_peer(struct netconn *conn, struct ip_addr *addr,
328 switch (conn->type) {
330 /* return an error as connecting is only a helper for upper layers */
332 case NETCONN_UDPLITE:
333 case NETCONN_UDPNOCHKSUM:
335 if (conn->pcb.udp == NULL ||
336 ((conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0))
338 *addr = (conn->pcb.udp->remote_ip);
339 *port = conn->pcb.udp->remote_port;
342 if (conn->pcb.tcp == NULL)
344 *addr = (conn->pcb.tcp->remote_ip);
345 *port = conn->pcb.tcp->remote_port;
348 return (conn->err = ERR_OK);
352 netconn_addr(struct netconn *conn, struct ip_addr **addr,
355 switch (conn->type) {
357 *addr = &(conn->pcb.raw->local_ip);
358 *port = conn->pcb.raw->protocol;
360 case NETCONN_UDPLITE:
361 case NETCONN_UDPNOCHKSUM:
363 *addr = &(conn->pcb.udp->local_ip);
364 *port = conn->pcb.udp->local_port;
367 *addr = &(conn->pcb.tcp->local_ip);
368 *port = conn->pcb.tcp->local_port;
371 return (conn->err = ERR_OK);
375 netconn_bind(struct netconn *conn, struct ip_addr *addr,
384 if (conn->type != NETCONN_TCP &&
385 conn->recvmbox == SYS_MBOX_NULL) {
386 if ((conn->recvmbox = sys_mbox_new()) == SYS_MBOX_NULL) {
391 if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
392 return (conn->err = ERR_MEM);
394 msg->type = API_MSG_BIND;
395 msg->msg.conn = conn;
396 msg->msg.msg.bc.ipaddr = addr;
397 msg->msg.msg.bc.port = port;
399 sys_mbox_fetch(conn->mbox, NULL);
400 memp_free(MEMP_API_MSG, msg);
406 netconn_connect(struct netconn *conn, struct ip_addr *addr,
416 if (conn->recvmbox == SYS_MBOX_NULL) {
417 if ((conn->recvmbox = sys_mbox_new()) == SYS_MBOX_NULL) {
422 if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
425 msg->type = API_MSG_CONNECT;
426 msg->msg.conn = conn;
427 msg->msg.msg.bc.ipaddr = addr;
428 msg->msg.msg.bc.port = port;
430 sys_mbox_fetch(conn->mbox, NULL);
431 memp_free(MEMP_API_MSG, msg);
436 netconn_disconnect(struct netconn *conn)
444 if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
447 msg->type = API_MSG_DISCONNECT;
448 msg->msg.conn = conn;
450 sys_mbox_fetch(conn->mbox, NULL);
451 memp_free(MEMP_API_MSG, msg);
457 netconn_listen(struct netconn *conn)
465 if (conn->acceptmbox == SYS_MBOX_NULL) {
466 conn->acceptmbox = sys_mbox_new();
467 if (conn->acceptmbox == SYS_MBOX_NULL) {
472 if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
473 return (conn->err = ERR_MEM);
475 msg->type = API_MSG_LISTEN;
476 msg->msg.conn = conn;
478 sys_mbox_fetch(conn->mbox, NULL);
479 memp_free(MEMP_API_MSG, msg);
484 netconn_accept(struct netconn *conn)
486 struct netconn *newconn;
492 sys_mbox_fetch(conn->acceptmbox, (void *)&newconn);
493 /* Register event with callback */
495 (*conn->callback)(conn, NETCONN_EVT_RCVMINUS, 0);
501 netconn_recv(struct netconn *conn)
512 if (conn->recvmbox == SYS_MBOX_NULL) {
513 conn->err = ERR_CONN;
517 if (conn->err != ERR_OK) {
521 if (conn->type == NETCONN_TCP) {
522 if (conn->pcb.tcp->state == LISTEN) {
523 conn->err = ERR_CONN;
528 buf = memp_malloc(MEMP_NETBUF);
535 sys_mbox_fetch(conn->recvmbox, (void *)&p);
540 conn->recv_avail -= len;
545 /* Register event with callback */
547 (*conn->callback)(conn, NETCONN_EVT_RCVMINUS, len);
549 /* If we are closed, we indicate that we no longer wish to receive
550 data by setting conn->recvmbox to SYS_MBOX_NULL. */
552 memp_free(MEMP_NETBUF, buf);
553 sys_mbox_free(conn->recvmbox);
554 conn->recvmbox = SYS_MBOX_NULL;
561 buf->fromaddr = NULL;
563 /* Let the stack know that we have taken the data. */
564 if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
568 msg->type = API_MSG_RECV;
569 msg->msg.conn = conn;
571 msg->msg.msg.len = buf->p->tot_len;
573 msg->msg.msg.len = 1;
577 sys_mbox_fetch(conn->mbox, NULL);
578 memp_free(MEMP_API_MSG, msg);
580 sys_mbox_fetch(conn->recvmbox, (void *)&buf);
581 conn->recv_avail -= buf->p->tot_len;
582 /* Register event with callback */
584 (*conn->callback)(conn, NETCONN_EVT_RCVMINUS, buf->p->tot_len);
590 LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_recv: received %p (err %d)\n", (void *)buf, conn->err));
597 netconn_send(struct netconn *conn, struct netbuf *buf)
605 if (conn->err != ERR_OK) {
609 if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
610 return (conn->err = ERR_MEM);
613 LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %d bytes\n", buf->p->tot_len));
614 msg->type = API_MSG_SEND;
615 msg->msg.conn = conn;
616 msg->msg.msg.p = buf->p;
619 sys_mbox_fetch(conn->mbox, NULL);
620 memp_free(MEMP_API_MSG, msg);
625 netconn_write(struct netconn *conn, void *dataptr, u16_t size, u8_t copy)
634 if (conn->err != ERR_OK) {
638 if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
639 return (conn->err = ERR_MEM);
641 msg->type = API_MSG_WRITE;
642 msg->msg.conn = conn;
645 conn->state = NETCONN_WRITE;
646 while (conn->err == ERR_OK && size > 0) {
647 msg->msg.msg.w.dataptr = dataptr;
648 msg->msg.msg.w.copy = copy;
650 if (conn->type == NETCONN_TCP) {
651 if (tcp_sndbuf(conn->pcb.tcp) == 0) {
652 sys_sem_wait(conn->sem);
653 if (conn->err != ERR_OK) {
657 if (size > tcp_sndbuf(conn->pcb.tcp)) {
658 /* We cannot send more than one send buffer's worth of data at a
660 len = tcp_sndbuf(conn->pcb.tcp);
668 LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_write: writing %d bytes (%d)\n", len, copy));
669 msg->msg.msg.w.len = len;
671 sys_mbox_fetch(conn->mbox, NULL);
672 if (conn->err == ERR_OK) {
673 dataptr = (void *)((u8_t *)dataptr + len);
675 } else if (conn->err == ERR_MEM) {
677 sys_sem_wait(conn->sem);
683 memp_free(MEMP_API_MSG, msg);
684 conn->state = NETCONN_NONE;
690 netconn_close(struct netconn *conn)
697 if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
698 return (conn->err = ERR_MEM);
701 conn->state = NETCONN_CLOSE;
703 msg->type = API_MSG_CLOSE;
704 msg->msg.conn = conn;
706 sys_mbox_fetch(conn->mbox, NULL);
707 if (conn->err == ERR_MEM &&
708 conn->sem != SYS_SEM_NULL) {
709 sys_sem_wait(conn->sem);
712 conn->state = NETCONN_NONE;
713 memp_free(MEMP_API_MSG, msg);
718 netconn_err(struct netconn *conn)