2 * Copyright (c) 2001-2003 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"
41 /*-----------------------------------------------------------------------------------*/
43 netbuf *netbuf_new(void)
47 buf = memp_mallocp(MEMP_NETBUF);
56 /*-----------------------------------------------------------------------------------*/
58 netbuf_delete(struct netbuf *buf)
63 buf->p = buf->ptr = NULL;
65 memp_freep(MEMP_NETBUF, buf);
68 /*-----------------------------------------------------------------------------------*/
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;
83 /*-----------------------------------------------------------------------------------*/
85 netbuf_free(struct netbuf *buf)
90 buf->p = buf->ptr = NULL;
92 /*-----------------------------------------------------------------------------------*/
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;
104 /*-----------------------------------------------------------------------------------*/
106 netbuf_chain(struct netbuf *head, struct netbuf *tail)
108 pbuf_chain(head->p, tail->p);
110 memp_freep(MEMP_NETBUF, tail);
112 /*-----------------------------------------------------------------------------------*/
114 netbuf_len(struct netbuf *buf)
116 return buf->p->tot_len;
118 /*-----------------------------------------------------------------------------------*/
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;
129 /*-----------------------------------------------------------------------------------*/
131 netbuf_next(struct netbuf *buf)
133 if (buf->ptr->next == NULL) {
136 buf->ptr = buf->ptr->next;
137 if (buf->ptr->next == NULL) {
142 /*-----------------------------------------------------------------------------------*/
144 netbuf_first(struct netbuf *buf)
148 /*-----------------------------------------------------------------------------------*/
150 netbuf_copy_partial(struct netbuf *buf, void *dataptr, u16_t len, u16_t offset)
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 ((char *)dataptr)[left] = ((char *)p->payload)[i];
177 /*-----------------------------------------------------------------------------------*/
179 netbuf_copy(struct netbuf *buf, void *dataptr, u16_t len)
181 netbuf_copy_partial(buf, dataptr, len, 0);
183 /*-----------------------------------------------------------------------------------*/
185 netbuf_fromaddr(struct netbuf *buf)
187 return buf->fromaddr;
189 /*-----------------------------------------------------------------------------------*/
191 netbuf_fromport(struct netbuf *buf)
193 return buf->fromport;
195 /*-----------------------------------------------------------------------------------*/
197 netconn *netconn_new(enum netconn_type t)
199 struct netconn *conn;
201 conn = memp_mallocp(MEMP_NETCONN);
206 conn->pcb.tcp = NULL;
208 if ((conn->mbox = sys_mbox_new()) == SYS_MBOX_NULL) {
209 memp_freep(MEMP_NETCONN, conn);
212 conn->recvmbox = SYS_MBOX_NULL;
213 conn->acceptmbox = SYS_MBOX_NULL;
214 conn->sem = SYS_SEM_NULL;
215 conn->state = NETCONN_NONE;
218 conn->recv_avail = 0;
221 /*-----------------------------------------------------------------------------------*/
223 netconn *netconn_new_with_callback(enum netconn_type t,
224 void (*callback)(struct netconn *, enum netconn_evt, u16_t len))
226 struct netconn *conn;
228 /* get a netconn and then initialize callback pointer and socket */
229 conn = netconn_new(t);
231 conn->callback = callback;
235 /*-----------------------------------------------------------------------------------*/
237 netconn_delete(struct netconn *conn)
246 if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) {
250 msg->type = API_MSG_DELCONN;
251 msg->msg.conn = conn;
253 sys_mbox_fetch(conn->mbox, NULL);
254 memp_freep(MEMP_API_MSG, msg);
256 /* Drain the recvmbox. */
257 if (conn->recvmbox != SYS_MBOX_NULL) {
258 while (sys_arch_mbox_fetch(conn->recvmbox, &mem, 1) != SYS_ARCH_TIMEOUT) {
259 if (conn->type == NETCONN_TCP) {
260 pbuf_free((struct pbuf *)mem);
262 netbuf_delete((struct netbuf *)mem);
265 sys_mbox_free(conn->recvmbox);
266 conn->recvmbox = SYS_MBOX_NULL;
270 /* Drain the acceptmbox. */
271 if (conn->acceptmbox != SYS_MBOX_NULL) {
272 while (sys_arch_mbox_fetch(conn->acceptmbox, &mem, 1) != SYS_ARCH_TIMEOUT) {
273 netconn_delete((struct netconn *)mem);
276 sys_mbox_free(conn->acceptmbox);
277 conn->acceptmbox = SYS_MBOX_NULL;
280 sys_mbox_free(conn->mbox);
281 conn->mbox = SYS_MBOX_NULL;
282 if (conn->sem != SYS_SEM_NULL) {
283 sys_sem_free(conn->sem);
285 /* conn->sem = SYS_SEM_NULL;*/
286 memp_free(MEMP_NETCONN, conn);
289 /*-----------------------------------------------------------------------------------*/
291 netconn_type(struct netconn *conn)
295 /*-----------------------------------------------------------------------------------*/
297 netconn_peer(struct netconn *conn, struct ip_addr *addr,
300 switch (conn->type) {
301 case NETCONN_UDPLITE:
302 case NETCONN_UDPNOCHKSUM:
304 if (conn->pcb.udp == NULL ||
305 ((conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0))
307 *addr = (conn->pcb.udp->remote_ip);
308 *port = conn->pcb.udp->remote_port;
311 if (conn->pcb.tcp == NULL)
313 *addr = (conn->pcb.tcp->remote_ip);
314 *port = conn->pcb.tcp->remote_port;
317 return (conn->err = ERR_OK);
319 /*-----------------------------------------------------------------------------------*/
321 netconn_addr(struct netconn *conn, struct ip_addr **addr,
324 switch (conn->type) {
325 case NETCONN_UDPLITE:
326 case NETCONN_UDPNOCHKSUM:
328 *addr = &(conn->pcb.udp->local_ip);
329 *port = conn->pcb.udp->local_port;
332 *addr = &(conn->pcb.tcp->local_ip);
333 *port = conn->pcb.tcp->local_port;
336 return (conn->err = ERR_OK);
338 /*-----------------------------------------------------------------------------------*/
340 netconn_bind(struct netconn *conn, struct ip_addr *addr,
349 if (conn->type != NETCONN_TCP &&
350 conn->recvmbox == SYS_MBOX_NULL) {
351 if ((conn->recvmbox = sys_mbox_new()) == SYS_MBOX_NULL) {
356 if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) {
357 return (conn->err = ERR_MEM);
359 msg->type = API_MSG_BIND;
360 msg->msg.conn = conn;
361 msg->msg.msg.bc.ipaddr = addr;
362 msg->msg.msg.bc.port = port;
364 sys_mbox_fetch(conn->mbox, NULL);
365 memp_freep(MEMP_API_MSG, msg);
369 /*-----------------------------------------------------------------------------------*/
371 netconn_connect(struct netconn *conn, struct ip_addr *addr,
381 if (conn->recvmbox == SYS_MBOX_NULL) {
382 if ((conn->recvmbox = sys_mbox_new()) == SYS_MBOX_NULL) {
387 if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) {
390 msg->type = API_MSG_CONNECT;
391 msg->msg.conn = conn;
392 msg->msg.msg.bc.ipaddr = addr;
393 msg->msg.msg.bc.port = port;
395 sys_mbox_fetch(conn->mbox, NULL);
396 memp_freep(MEMP_API_MSG, msg);
401 netconn_disconnect(struct netconn *conn)
409 if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) {
412 msg->type = API_MSG_DISCONNECT;
413 msg->msg.conn = conn;
415 sys_mbox_fetch(conn->mbox, NULL);
416 memp_freep(MEMP_API_MSG, msg);
420 /*-----------------------------------------------------------------------------------*/
422 netconn_listen(struct netconn *conn)
430 if (conn->acceptmbox == SYS_MBOX_NULL) {
431 conn->acceptmbox = sys_mbox_new();
432 if (conn->acceptmbox == SYS_MBOX_NULL) {
437 if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) {
438 return (conn->err = ERR_MEM);
440 msg->type = API_MSG_LISTEN;
441 msg->msg.conn = conn;
443 sys_mbox_fetch(conn->mbox, NULL);
444 memp_freep(MEMP_API_MSG, msg);
447 /*-----------------------------------------------------------------------------------*/
449 netconn_accept(struct netconn *conn)
451 struct netconn *newconn;
457 sys_mbox_fetch(conn->acceptmbox, (void **)&newconn);
458 /* Register event with callback */
460 (*conn->callback)(conn, NETCONN_EVT_RCVMINUS, 0);
464 /*-----------------------------------------------------------------------------------*/
466 netconn_recv(struct netconn *conn)
477 if (conn->recvmbox == SYS_MBOX_NULL) {
478 conn->err = ERR_CONN;
482 if (conn->err != ERR_OK) {
486 if (conn->type == NETCONN_TCP) {
487 if (conn->pcb.tcp->state == LISTEN) {
488 conn->err = ERR_CONN;
493 buf = memp_mallocp(MEMP_NETBUF);
500 sys_mbox_fetch(conn->recvmbox, (void **)&p);
505 conn->recv_avail -= len;
510 /* Register event with callback */
512 (*conn->callback)(conn, NETCONN_EVT_RCVMINUS, len);
514 /* If we are closed, we indicate that we no longer wish to receive
515 data by setting conn->recvmbox to SYS_MBOX_NULL. */
517 memp_freep(MEMP_NETBUF, buf);
518 sys_mbox_free(conn->recvmbox);
519 conn->recvmbox = SYS_MBOX_NULL;
526 buf->fromaddr = NULL;
528 /* Let the stack know that we have taken the data. */
529 if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) {
533 msg->type = API_MSG_RECV;
534 msg->msg.conn = conn;
536 msg->msg.msg.len = buf->p->tot_len;
538 msg->msg.msg.len = 1;
542 sys_mbox_fetch(conn->mbox, NULL);
543 memp_freep(MEMP_API_MSG, msg);
545 sys_mbox_fetch(conn->recvmbox, (void **)&buf);
546 conn->recv_avail -= buf->p->tot_len;
547 /* Register event with callback */
549 (*conn->callback)(conn, NETCONN_EVT_RCVMINUS, buf->p->tot_len);
555 DEBUGF(API_LIB_DEBUG, ("netconn_recv: received %p (err %d)\n", (void *)buf, conn->err));
560 /*-----------------------------------------------------------------------------------*/
562 netconn_send(struct netconn *conn, struct netbuf *buf)
570 if (conn->err != ERR_OK) {
574 if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) {
575 return (conn->err = ERR_MEM);
578 DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %d bytes\n", buf->p->tot_len));
579 msg->type = API_MSG_SEND;
580 msg->msg.conn = conn;
581 msg->msg.msg.p = buf->p;
584 sys_mbox_fetch(conn->mbox, NULL);
585 memp_freep(MEMP_API_MSG, msg);
588 /*-----------------------------------------------------------------------------------*/
590 netconn_write(struct netconn *conn, void *dataptr, u16_t size, u8_t copy)
599 if (conn->err != ERR_OK) {
603 if (conn->sem == SYS_SEM_NULL) {
604 conn->sem = sys_sem_new(0);
605 if (conn->sem == SYS_SEM_NULL) {
610 if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) {
611 return (conn->err = ERR_MEM);
613 msg->type = API_MSG_WRITE;
614 msg->msg.conn = conn;
617 conn->state = NETCONN_WRITE;
618 while (conn->err == ERR_OK && size > 0) {
619 msg->msg.msg.w.dataptr = dataptr;
620 msg->msg.msg.w.copy = copy;
622 if (conn->type == NETCONN_TCP) {
623 if (tcp_sndbuf(conn->pcb.tcp) == 0) {
624 sys_sem_wait(conn->sem);
625 if (conn->err != ERR_OK) {
629 if (size > tcp_sndbuf(conn->pcb.tcp)) {
630 /* We cannot send more than one send buffer's worth of data at a
632 len = tcp_sndbuf(conn->pcb.tcp);
640 DEBUGF(API_LIB_DEBUG, ("netconn_write: writing %d bytes (%d)\n", len, copy));
641 msg->msg.msg.w.len = len;
643 sys_mbox_fetch(conn->mbox, NULL);
644 if (conn->err == ERR_OK) {
645 dataptr = (void *)((char *)dataptr + len);
647 } else if (conn->err == ERR_MEM) {
649 sys_sem_wait(conn->sem);
655 memp_freep(MEMP_API_MSG, msg);
656 conn->state = NETCONN_NONE;
657 if (conn->sem != SYS_SEM_NULL) {
658 sys_sem_free(conn->sem);
659 conn->sem = SYS_SEM_NULL;
664 /*-----------------------------------------------------------------------------------*/
666 netconn_close(struct netconn *conn)
673 if ((msg = memp_mallocp(MEMP_API_MSG)) == NULL) {
674 return (conn->err = ERR_MEM);
677 conn->state = NETCONN_CLOSE;
679 msg->type = API_MSG_CLOSE;
680 msg->msg.conn = conn;
682 sys_mbox_fetch(conn->mbox, NULL);
683 if (conn->err == ERR_MEM &&
684 conn->sem != SYS_SEM_NULL) {
685 sys_sem_wait(conn->sem);
688 conn->state = NETCONN_NONE;
689 memp_freep(MEMP_API_MSG, msg);
692 /*-----------------------------------------------------------------------------------*/
694 netconn_err(struct netconn *conn)
698 /*-----------------------------------------------------------------------------------*/