]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/net/lwip_tcpip/v2_0/src/api/api_lib.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / net / lwip_tcpip / v2_0 / src / api / api_lib.c
1 /*
2  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3  * All rights reserved. 
4  * 
5  * Redistribution and use in source and binary forms, with or without modification, 
6  * are permitted provided that the following conditions are met:
7  *
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. 
15  *
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 
25  * OF SUCH DAMAGE.
26  *
27  * This file is part of the lwIP TCP/IP stack.
28  * 
29  * Author: Adam Dunkels <adam@sics.se>
30  *
31  */
32
33 /* This is the part of the API that is linked with
34    the application */
35
36 #include "lwip/opt.h"
37 #include "lwip/api.h"
38 #include "lwip/api_msg.h"
39 #include "lwip/memp.h"
40
41
42 struct
43 netbuf *netbuf_new(void)
44 {
45   struct netbuf *buf;
46
47   buf = memp_malloc(MEMP_NETBUF);
48   if (buf != NULL) {
49     buf->p = NULL;
50     buf->ptr = NULL;
51     return buf;
52   } else {
53     return NULL;
54   }
55 }
56
57 void
58 netbuf_delete(struct netbuf *buf)
59 {
60   if (buf != NULL) {
61     if (buf->p != NULL) {
62       pbuf_free(buf->p);
63       buf->p = buf->ptr = NULL;
64     }
65     memp_free(MEMP_NETBUF, buf);
66   }
67 }
68
69 void *
70 netbuf_alloc(struct netbuf *buf, u16_t size)
71 {
72   /* Deallocate any previously allocated memory. */
73   if (buf->p != NULL) {
74     pbuf_free(buf->p);
75   }
76   buf->p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);
77   if (buf->p == NULL) {
78      return NULL;
79   }
80   buf->ptr = buf->p;
81   return buf->p->payload;
82 }
83
84 void
85 netbuf_free(struct netbuf *buf)
86 {
87   if (buf->p != NULL) {
88     pbuf_free(buf->p);
89   }
90   buf->p = buf->ptr = NULL;
91 }
92
93 void
94 netbuf_ref(struct netbuf *buf, void *dataptr, u16_t size)
95 {
96   if (buf->p != NULL) {
97     pbuf_free(buf->p);
98   }
99   buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF);
100   buf->p->payload = dataptr;
101   buf->p->len = buf->p->tot_len = size;
102   buf->ptr = buf->p;
103 }
104
105 void
106 netbuf_chain(struct netbuf *head, struct netbuf *tail)
107 {
108   pbuf_chain(head->p, tail->p);
109   head->ptr = head->p;
110   memp_free(MEMP_NETBUF, tail);
111 }
112
113 u16_t
114 netbuf_len(struct netbuf *buf)
115 {
116   return buf->p->tot_len;
117 }
118
119 err_t
120 netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len)
121 {
122   if (buf->ptr == NULL) {
123     return ERR_BUF;
124   }
125   *dataptr = buf->ptr->payload;
126   *len = buf->ptr->len;
127   return ERR_OK;
128 }
129
130 s8_t
131 netbuf_next(struct netbuf *buf)
132 {
133   if (buf->ptr->next == NULL) {
134     return -1;
135   }
136   buf->ptr = buf->ptr->next;
137   if (buf->ptr->next == NULL) {
138     return 1;
139   }
140   return 0;
141 }
142
143 void
144 netbuf_first(struct netbuf *buf)
145 {
146   buf->ptr = buf->p;
147 }
148
149 void
150 netbuf_copy_partial(struct netbuf *buf, void *dataptr, u16_t len, u16_t offset)
151 {
152   struct pbuf *p;
153   u16_t i, left;
154
155   left = 0;
156
157   if(buf == NULL || dataptr == NULL) {
158     return;
159   }
160   
161   /* This implementation is bad. It should use bcopy
162      instead. */
163   for(p = buf->p; left < len && p != NULL; p = p->next) {
164     if (offset != 0 && offset >= p->len) {
165       offset -= p->len;
166     } else {    
167       for(i = offset; i < p->len; ++i) {
168   ((u8_t *)dataptr)[left] = ((u8_t *)p->payload)[i];
169   if (++left >= len) {
170     return;
171   }
172       }
173       offset = 0;
174     }
175   }
176 }
177
178 void
179 netbuf_copy(struct netbuf *buf, void *dataptr, u16_t len)
180 {
181   netbuf_copy_partial(buf, dataptr, len, 0);
182 }
183
184 struct ip_addr *
185 netbuf_fromaddr(struct netbuf *buf)
186 {
187   return buf->fromaddr;
188 }
189
190 u16_t
191 netbuf_fromport(struct netbuf *buf)
192 {
193   return buf->fromport;
194 }
195
196 struct
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))
199 {
200   struct netconn *conn;
201   struct api_msg *msg;
202
203   conn = memp_malloc(MEMP_NETCONN);
204   if (conn == NULL) {
205     return NULL;
206   }
207   
208   conn->err = ERR_OK;
209   conn->type = t;
210   conn->pcb.tcp = NULL;
211
212   if ((conn->mbox = sys_mbox_new()) == SYS_MBOX_NULL) {
213     memp_free(MEMP_NETCONN, conn);
214     return NULL;
215   }
216   conn->recvmbox = SYS_MBOX_NULL;
217   conn->acceptmbox = SYS_MBOX_NULL;
218   conn->sem = SYS_SEM_NULL;
219   conn->state = NETCONN_NONE;
220   conn->socket = 0;
221   conn->callback = callback;
222   conn->recv_avail = 0;
223
224   if((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
225     memp_free(MEMP_NETCONN, conn);
226     return NULL;
227   }
228   
229   msg->type = API_MSG_NEWCONN;
230   msg->msg.msg.bc.port = proto; /* misusing the port field */
231   msg->msg.conn = conn;
232   api_msg_post(msg);  
233   sys_mbox_fetch(conn->mbox, NULL);
234   memp_free(MEMP_API_MSG, msg);
235
236   if ( conn->err != ERR_OK ) {
237     memp_free(MEMP_NETCONN, conn);
238     return NULL;
239   }
240
241   return conn;
242 }
243
244
245 struct
246 netconn *netconn_new(enum netconn_type t)
247 {
248   return netconn_new_with_proto_and_callback(t,0,NULL);
249 }
250
251 struct
252 netconn *netconn_new_with_callback(enum netconn_type t,
253                                    void (*callback)(struct netconn *, enum netconn_evt, u16_t len))
254 {
255   return netconn_new_with_proto_and_callback(t,0,callback);
256 }
257
258
259 err_t
260 netconn_delete(struct netconn *conn)
261 {
262   struct api_msg *msg;
263   void *mem;
264   
265   if (conn == NULL) {
266     return ERR_OK;
267   }
268   
269   if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
270     return ERR_MEM;
271   }
272   
273   msg->type = API_MSG_DELCONN;
274   msg->msg.conn = conn;
275   api_msg_post(msg);  
276   sys_mbox_fetch(conn->mbox, NULL);
277   memp_free(MEMP_API_MSG, msg);
278
279   /* Drain the recvmbox. */
280   if (conn->recvmbox != SYS_MBOX_NULL) {
281     while (sys_arch_mbox_fetch(conn->recvmbox, &mem, 1) != SYS_ARCH_TIMEOUT) {
282       if (conn->type == NETCONN_TCP) {
283         if(mem != NULL)
284           pbuf_free((struct pbuf *)mem);
285       } else {
286         netbuf_delete((struct netbuf *)mem);
287       }
288     }
289     sys_mbox_free(conn->recvmbox);
290     conn->recvmbox = SYS_MBOX_NULL;
291   }
292  
293
294   /* Drain the acceptmbox. */
295   if (conn->acceptmbox != SYS_MBOX_NULL) {
296     while (sys_arch_mbox_fetch(conn->acceptmbox, &mem, 1) != SYS_ARCH_TIMEOUT) {
297       netconn_delete((struct netconn *)mem);
298     }
299     
300     sys_mbox_free(conn->acceptmbox);
301     conn->acceptmbox = SYS_MBOX_NULL;
302   }
303
304   sys_mbox_free(conn->mbox);
305   conn->mbox = SYS_MBOX_NULL;
306   if (conn->sem != SYS_SEM_NULL) {
307     sys_sem_free(conn->sem);
308   }
309   /*  conn->sem = SYS_SEM_NULL;*/
310   memp_free(MEMP_NETCONN, conn);
311   return ERR_OK;
312 }
313
314 enum netconn_type
315 netconn_type(struct netconn *conn)
316 {
317   return conn->type;
318 }
319
320 err_t
321 netconn_peer(struct netconn *conn, struct ip_addr *addr,
322        u16_t *port)
323 {
324   switch (conn->type) {
325   case NETCONN_RAW:
326     /* return an error as connecting is only a helper for upper layers */
327     return ERR_CONN;
328   case NETCONN_UDPLITE:
329   case NETCONN_UDPNOCHKSUM:
330   case NETCONN_UDP:
331 #if LWIP_UDP
332     if (conn->pcb.udp == NULL ||
333   ((conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0))
334      return ERR_CONN;
335     *addr = (conn->pcb.udp->remote_ip);
336     *port = conn->pcb.udp->remote_port;
337 #endif
338     break;
339     
340   case NETCONN_TCP:
341 #if LWIP_TCP
342     if (conn->pcb.tcp == NULL)
343       return ERR_CONN;
344     *addr = (conn->pcb.tcp->remote_ip);
345     *port = conn->pcb.tcp->remote_port;
346 #endif
347     break;
348   }
349   return (conn->err = ERR_OK);
350 }
351
352 err_t
353 netconn_addr(struct netconn *conn, struct ip_addr **addr,
354        u16_t *port)
355 {
356   switch (conn->type) {
357   case NETCONN_RAW:
358 #if LWIP_RAW
359     *addr = &(conn->pcb.raw->local_ip);
360     *port = conn->pcb.raw->protocol;
361 #endif
362     break;
363   case NETCONN_UDPLITE:
364   case NETCONN_UDPNOCHKSUM:
365   case NETCONN_UDP:
366 #if LWIP_UDP
367     *addr = &(conn->pcb.udp->local_ip);
368     *port = conn->pcb.udp->local_port;
369 #endif
370     break;
371     
372   case NETCONN_TCP:
373 #if LWIP_TCP
374     *addr = &(conn->pcb.tcp->local_ip);
375     *port = conn->pcb.tcp->local_port;
376 #endif
377     break;
378   }
379   return (conn->err = ERR_OK);
380 }
381
382 err_t
383 netconn_bind(struct netconn *conn, struct ip_addr *addr,
384       u16_t port)
385 {
386   struct api_msg *msg;
387
388   if (conn == NULL) {
389     return ERR_VAL;
390   }
391
392   if (conn->type != NETCONN_TCP &&
393      conn->recvmbox == SYS_MBOX_NULL) {
394     if ((conn->recvmbox = sys_mbox_new()) == SYS_MBOX_NULL) {
395       return ERR_MEM;
396     }
397   }
398   
399   if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
400     return (conn->err = ERR_MEM);
401   }
402   msg->type = API_MSG_BIND;
403   msg->msg.conn = conn;
404   msg->msg.msg.bc.ipaddr = addr;
405   msg->msg.msg.bc.port = port;
406   api_msg_post(msg);
407   sys_mbox_fetch(conn->mbox, NULL);
408   memp_free(MEMP_API_MSG, msg);
409   return conn->err;
410 }
411
412
413 err_t
414 netconn_connect(struct netconn *conn, struct ip_addr *addr,
415        u16_t port)
416 {
417   struct api_msg *msg;
418   
419   if (conn == NULL) {
420     return ERR_VAL;
421   }
422
423
424   if (conn->recvmbox == SYS_MBOX_NULL) {
425     if ((conn->recvmbox = sys_mbox_new()) == SYS_MBOX_NULL) {
426       return ERR_MEM;
427     }
428   }
429   
430   if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
431     return ERR_MEM;
432   }
433   msg->type = API_MSG_CONNECT;
434   msg->msg.conn = conn;  
435   msg->msg.msg.bc.ipaddr = addr;
436   msg->msg.msg.bc.port = port;
437   api_msg_post(msg);
438   sys_mbox_fetch(conn->mbox, NULL);
439   memp_free(MEMP_API_MSG, msg);
440   return conn->err;
441 }
442
443 err_t
444 netconn_disconnect(struct netconn *conn)
445 {
446   struct api_msg *msg;
447   
448   if (conn == NULL) {
449     return ERR_VAL;
450   }
451
452   if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
453     return ERR_MEM;
454   }
455   msg->type = API_MSG_DISCONNECT;
456   msg->msg.conn = conn;  
457   api_msg_post(msg);
458   sys_mbox_fetch(conn->mbox, NULL);
459   memp_free(MEMP_API_MSG, msg);
460   return conn->err;
461
462 }
463
464 err_t
465 netconn_listen(struct netconn *conn)
466 {
467   struct api_msg *msg;
468
469   if (conn == NULL) {
470     return ERR_VAL;
471   }
472
473   if (conn->acceptmbox == SYS_MBOX_NULL) {
474     conn->acceptmbox = sys_mbox_new();
475     if (conn->acceptmbox == SYS_MBOX_NULL) {
476       return ERR_MEM;
477     }
478   }
479   
480   if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
481     return (conn->err = ERR_MEM);
482   }
483   msg->type = API_MSG_LISTEN;
484   msg->msg.conn = conn;
485   api_msg_post(msg);
486   sys_mbox_fetch(conn->mbox, NULL);
487   memp_free(MEMP_API_MSG, msg);
488   return conn->err;
489 }
490
491 struct netconn *
492 netconn_accept(struct netconn *conn)
493 {
494   struct netconn *newconn;
495   
496   if (conn == NULL) {
497     return NULL;
498   }
499   
500   sys_mbox_fetch(conn->acceptmbox, (void **)&newconn);
501   /* Register event with callback */
502   if (conn->callback)
503       (*conn->callback)(conn, NETCONN_EVT_RCVMINUS, 0);
504   
505   return newconn;
506 }
507
508 struct netbuf *
509 netconn_recv(struct netconn *conn)
510 {
511   struct api_msg *msg;
512   struct netbuf *buf;
513   struct pbuf *p;
514   u16_t len;
515     
516   if (conn == NULL) {
517     return NULL;
518   }
519   
520   if (conn->recvmbox == SYS_MBOX_NULL) {
521     conn->err = ERR_CONN;
522     return NULL;
523   }
524
525   if (conn->err != ERR_OK) {
526     return NULL;
527   }
528
529   if (conn->type == NETCONN_TCP) {
530     if (conn->pcb.tcp->state == LISTEN) {
531       conn->err = ERR_CONN;
532       return NULL;
533     }
534
535
536     buf = memp_malloc(MEMP_NETBUF);
537
538     if (buf == NULL) {
539       conn->err = ERR_MEM;
540       return NULL;
541     }
542     
543     sys_mbox_fetch(conn->recvmbox, (void **)&p);
544
545     if (p != NULL)
546     {
547         len = p->tot_len;
548         conn->recv_avail -= len;
549     }
550     else
551         len = 0;
552     
553     /* Register event with callback */
554       if (conn->callback)
555         (*conn->callback)(conn, NETCONN_EVT_RCVMINUS, len);
556
557     /* If we are closed, we indicate that we no longer wish to receive
558        data by setting conn->recvmbox to SYS_MBOX_NULL. */
559     if (p == NULL) {
560       memp_free(MEMP_NETBUF, buf);
561       sys_mbox_free(conn->recvmbox);
562       conn->recvmbox = SYS_MBOX_NULL;
563       return NULL;
564     }
565
566     buf->p = p;
567     buf->ptr = p;
568     buf->fromport = 0;
569     buf->fromaddr = NULL;
570
571     /* Let the stack know that we have taken the data. */
572     if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
573       conn->err = ERR_MEM;
574       return buf;
575     }
576     msg->type = API_MSG_RECV;
577     msg->msg.conn = conn;
578     if (buf != NULL) {
579       msg->msg.msg.len = buf->p->tot_len;
580     } else {
581       msg->msg.msg.len = 1;
582     }
583     api_msg_post(msg);
584
585     sys_mbox_fetch(conn->mbox, NULL);
586     memp_free(MEMP_API_MSG, msg);
587   } else {
588     sys_mbox_fetch(conn->recvmbox, (void **)&buf);
589   conn->recv_avail -= buf->p->tot_len;
590     /* Register event with callback */
591     if (conn->callback)
592         (*conn->callback)(conn, NETCONN_EVT_RCVMINUS, buf->p->tot_len);
593   }
594
595   
596
597     
598   LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_recv: received %p (err %d)\n", (void *)buf, conn->err));
599
600
601   return buf;
602 }
603
604 err_t
605 netconn_send(struct netconn *conn, struct netbuf *buf)
606 {
607   struct api_msg *msg;
608
609   if (conn == NULL) {
610     return ERR_VAL;
611   }
612
613   if (conn->err != ERR_OK) {
614     return conn->err;
615   }
616
617   if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
618     return (conn->err = ERR_MEM);
619   }
620
621   LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %d bytes\n", buf->p->tot_len));
622   msg->type = API_MSG_SEND;
623   msg->msg.conn = conn;
624   msg->msg.msg.p = buf->p;
625   api_msg_post(msg);
626
627   sys_mbox_fetch(conn->mbox, NULL);
628   memp_free(MEMP_API_MSG, msg);
629   return conn->err;
630 }
631
632 err_t
633 netconn_write(struct netconn *conn, void *dataptr, u16_t size, u8_t copy)
634 {
635   struct api_msg *msg;
636   u16_t len;
637   
638   if (conn == NULL) {
639     return ERR_VAL;
640   }
641
642   if (conn->err != ERR_OK) {
643     return conn->err;
644   }
645   
646   if (conn->sem == SYS_SEM_NULL) {
647     conn->sem = sys_sem_new(0);
648     if (conn->sem == SYS_SEM_NULL) {
649       return ERR_MEM;
650     }
651   }
652
653   if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
654     return (conn->err = ERR_MEM);
655   }
656   msg->type = API_MSG_WRITE;
657   msg->msg.conn = conn;
658         
659
660   conn->state = NETCONN_WRITE;
661   while (conn->err == ERR_OK && size > 0) {
662     msg->msg.msg.w.dataptr = dataptr;
663     msg->msg.msg.w.copy = copy;
664     
665     if (conn->type == NETCONN_TCP) {
666       if (tcp_sndbuf(conn->pcb.tcp) == 0) {
667   sys_sem_wait(conn->sem);
668   if (conn->err != ERR_OK) {
669     goto ret;
670   }
671       }
672       if (size > tcp_sndbuf(conn->pcb.tcp)) {
673   /* We cannot send more than one send buffer's worth of data at a
674      time. */
675   len = tcp_sndbuf(conn->pcb.tcp);
676       } else {
677   len = size;
678       }
679     } else {
680       len = size;
681     }
682     
683     LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_write: writing %d bytes (%d)\n", len, copy));
684     msg->msg.msg.w.len = len;
685     api_msg_post(msg);
686     sys_mbox_fetch(conn->mbox, NULL);    
687     if (conn->err == ERR_OK) {
688       dataptr = (void *)((u8_t *)dataptr + len);
689       size -= len;
690     } else if (conn->err == ERR_MEM) {
691       conn->err = ERR_OK;
692       sys_sem_wait(conn->sem);
693     } else {
694       goto ret;
695     }
696   }
697  ret:
698   memp_free(MEMP_API_MSG, msg);
699   conn->state = NETCONN_NONE;
700   if (conn->sem != SYS_SEM_NULL) {
701     sys_sem_free(conn->sem);
702     conn->sem = SYS_SEM_NULL;
703   }
704   
705   return conn->err;
706 }
707
708 err_t
709 netconn_close(struct netconn *conn)
710 {
711   struct api_msg *msg;
712
713   if (conn == NULL) {
714     return ERR_VAL;
715   }
716   if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
717     return (conn->err = ERR_MEM);
718   }
719
720   conn->state = NETCONN_CLOSE;
721  again:
722   msg->type = API_MSG_CLOSE;
723   msg->msg.conn = conn;
724   api_msg_post(msg);
725   sys_mbox_fetch(conn->mbox, NULL);
726   if (conn->err == ERR_MEM &&
727      conn->sem != SYS_SEM_NULL) {
728     sys_sem_wait(conn->sem);
729     goto again;
730   }
731   conn->state = NETCONN_NONE;
732   memp_free(MEMP_API_MSG, msg);
733   return conn->err;
734 }
735
736 err_t
737 netconn_err(struct netconn *conn)
738 {
739   return conn->err;
740 }
741