]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/net/lwip_tcpip/v2_0/src/api/api_lib.c
93c0e453885638a87954db65ce25fc77c33f8864
[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   ((char *)dataptr)[left] = ((char *)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   pbuf_free((struct pbuf *)mem);
284       } else {
285   netbuf_delete((struct netbuf *)mem);
286       }
287     }
288     sys_mbox_free(conn->recvmbox);
289     conn->recvmbox = SYS_MBOX_NULL;
290   }
291  
292
293   /* Drain the acceptmbox. */
294   if (conn->acceptmbox != SYS_MBOX_NULL) {
295     while (sys_arch_mbox_fetch(conn->acceptmbox, &mem, 1) != SYS_ARCH_TIMEOUT) {
296       netconn_delete((struct netconn *)mem);
297     }
298     
299     sys_mbox_free(conn->acceptmbox);
300     conn->acceptmbox = SYS_MBOX_NULL;
301   }
302
303   sys_mbox_free(conn->mbox);
304   conn->mbox = SYS_MBOX_NULL;
305   if (conn->sem != SYS_SEM_NULL) {
306     sys_sem_free(conn->sem);
307   }
308   /*  conn->sem = SYS_SEM_NULL;*/
309   memp_free(MEMP_NETCONN, conn);
310   return ERR_OK;
311 }
312
313 enum netconn_type
314 netconn_type(struct netconn *conn)
315 {
316   return conn->type;
317 }
318
319 err_t
320 netconn_peer(struct netconn *conn, struct ip_addr *addr,
321        u16_t *port)
322 {
323   switch (conn->type) {
324   case NETCONN_RAW:
325     /* return an error as connecting is only a helper for upper layers */
326     return ERR_CONN;
327   case NETCONN_UDPLITE:
328   case NETCONN_UDPNOCHKSUM:
329   case NETCONN_UDP:
330     if (conn->pcb.udp == NULL ||
331   ((conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0))
332      return ERR_CONN;
333     *addr = (conn->pcb.udp->remote_ip);
334     *port = conn->pcb.udp->remote_port;
335     break;
336   case NETCONN_TCP:
337     if (conn->pcb.tcp == NULL)
338       return ERR_CONN;
339     *addr = (conn->pcb.tcp->remote_ip);
340     *port = conn->pcb.tcp->remote_port;
341     break;
342   }
343   return (conn->err = ERR_OK);
344 }
345
346 err_t
347 netconn_addr(struct netconn *conn, struct ip_addr **addr,
348        u16_t *port)
349 {
350   switch (conn->type) {
351   case NETCONN_RAW:
352     *addr = &(conn->pcb.raw->local_ip);
353     *port = conn->pcb.raw->protocol;
354     break;
355   case NETCONN_UDPLITE:
356   case NETCONN_UDPNOCHKSUM:
357   case NETCONN_UDP:
358     *addr = &(conn->pcb.udp->local_ip);
359     *port = conn->pcb.udp->local_port;
360     break;
361   case NETCONN_TCP:
362     *addr = &(conn->pcb.tcp->local_ip);
363     *port = conn->pcb.tcp->local_port;
364     break;
365   }
366   return (conn->err = ERR_OK);
367 }
368
369 err_t
370 netconn_bind(struct netconn *conn, struct ip_addr *addr,
371       u16_t port)
372 {
373   struct api_msg *msg;
374
375   if (conn == NULL) {
376     return ERR_VAL;
377   }
378
379   if (conn->type != NETCONN_TCP &&
380      conn->recvmbox == SYS_MBOX_NULL) {
381     if ((conn->recvmbox = sys_mbox_new()) == SYS_MBOX_NULL) {
382       return ERR_MEM;
383     }
384   }
385   
386   if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
387     return (conn->err = ERR_MEM);
388   }
389   msg->type = API_MSG_BIND;
390   msg->msg.conn = conn;
391   msg->msg.msg.bc.ipaddr = addr;
392   msg->msg.msg.bc.port = port;
393   api_msg_post(msg);
394   sys_mbox_fetch(conn->mbox, NULL);
395   memp_free(MEMP_API_MSG, msg);
396   return conn->err;
397 }
398
399
400 err_t
401 netconn_connect(struct netconn *conn, struct ip_addr *addr,
402        u16_t port)
403 {
404   struct api_msg *msg;
405   
406   if (conn == NULL) {
407     return ERR_VAL;
408   }
409
410
411   if (conn->recvmbox == SYS_MBOX_NULL) {
412     if ((conn->recvmbox = sys_mbox_new()) == SYS_MBOX_NULL) {
413       return ERR_MEM;
414     }
415   }
416   
417   if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
418     return ERR_MEM;
419   }
420   msg->type = API_MSG_CONNECT;
421   msg->msg.conn = conn;  
422   msg->msg.msg.bc.ipaddr = addr;
423   msg->msg.msg.bc.port = port;
424   api_msg_post(msg);
425   sys_mbox_fetch(conn->mbox, NULL);
426   memp_free(MEMP_API_MSG, msg);
427   return conn->err;
428 }
429
430 err_t
431 netconn_disconnect(struct netconn *conn)
432 {
433   struct api_msg *msg;
434   
435   if (conn == NULL) {
436     return ERR_VAL;
437   }
438
439   if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
440     return ERR_MEM;
441   }
442   msg->type = API_MSG_DISCONNECT;
443   msg->msg.conn = conn;  
444   api_msg_post(msg);
445   sys_mbox_fetch(conn->mbox, NULL);
446   memp_free(MEMP_API_MSG, msg);
447   return conn->err;
448
449 }
450
451 err_t
452 netconn_listen(struct netconn *conn)
453 {
454   struct api_msg *msg;
455
456   if (conn == NULL) {
457     return ERR_VAL;
458   }
459
460   if (conn->acceptmbox == SYS_MBOX_NULL) {
461     conn->acceptmbox = sys_mbox_new();
462     if (conn->acceptmbox == SYS_MBOX_NULL) {
463       return ERR_MEM;
464     }
465   }
466   
467   if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
468     return (conn->err = ERR_MEM);
469   }
470   msg->type = API_MSG_LISTEN;
471   msg->msg.conn = conn;
472   api_msg_post(msg);
473   sys_mbox_fetch(conn->mbox, NULL);
474   memp_free(MEMP_API_MSG, msg);
475   return conn->err;
476 }
477
478 struct netconn *
479 netconn_accept(struct netconn *conn)
480 {
481   struct netconn *newconn;
482   
483   if (conn == NULL) {
484     return NULL;
485   }
486   
487   sys_mbox_fetch(conn->acceptmbox, (void **)&newconn);
488   /* Register event with callback */
489   if (conn->callback)
490       (*conn->callback)(conn, NETCONN_EVT_RCVMINUS, 0);
491   
492   return newconn;
493 }
494
495 struct netbuf *
496 netconn_recv(struct netconn *conn)
497 {
498   struct api_msg *msg;
499   struct netbuf *buf;
500   struct pbuf *p;
501   u16_t len;
502     
503   if (conn == NULL) {
504     return NULL;
505   }
506   
507   if (conn->recvmbox == SYS_MBOX_NULL) {
508     conn->err = ERR_CONN;
509     return NULL;
510   }
511
512   if (conn->err != ERR_OK) {
513     return NULL;
514   }
515
516   if (conn->type == NETCONN_TCP) {
517     if (conn->pcb.tcp->state == LISTEN) {
518       conn->err = ERR_CONN;
519       return NULL;
520     }
521
522
523     buf = memp_malloc(MEMP_NETBUF);
524
525     if (buf == NULL) {
526       conn->err = ERR_MEM;
527       return NULL;
528     }
529     
530     sys_mbox_fetch(conn->recvmbox, (void **)&p);
531
532     if (p != NULL)
533     {
534         len = p->tot_len;
535         conn->recv_avail -= len;
536     }
537     else
538         len = 0;
539     
540     /* Register event with callback */
541       if (conn->callback)
542         (*conn->callback)(conn, NETCONN_EVT_RCVMINUS, len);
543
544     /* If we are closed, we indicate that we no longer wish to receive
545        data by setting conn->recvmbox to SYS_MBOX_NULL. */
546     if (p == NULL) {
547       memp_free(MEMP_NETBUF, buf);
548       sys_mbox_free(conn->recvmbox);
549       conn->recvmbox = SYS_MBOX_NULL;
550       return NULL;
551     }
552
553     buf->p = p;
554     buf->ptr = p;
555     buf->fromport = 0;
556     buf->fromaddr = NULL;
557
558     /* Let the stack know that we have taken the data. */
559     if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
560       conn->err = ERR_MEM;
561       return buf;
562     }
563     msg->type = API_MSG_RECV;
564     msg->msg.conn = conn;
565     if (buf != NULL) {
566       msg->msg.msg.len = buf->p->tot_len;
567     } else {
568       msg->msg.msg.len = 1;
569     }
570     api_msg_post(msg);
571
572     sys_mbox_fetch(conn->mbox, NULL);
573     memp_free(MEMP_API_MSG, msg);
574   } else {
575     sys_mbox_fetch(conn->recvmbox, (void **)&buf);
576   conn->recv_avail -= buf->p->tot_len;
577     /* Register event with callback */
578     if (conn->callback)
579         (*conn->callback)(conn, NETCONN_EVT_RCVMINUS, buf->p->tot_len);
580   }
581
582   
583
584     
585   LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_recv: received %p (err %d)\n", (void *)buf, conn->err));
586
587
588   return buf;
589 }
590
591 err_t
592 netconn_send(struct netconn *conn, struct netbuf *buf)
593 {
594   struct api_msg *msg;
595
596   if (conn == NULL) {
597     return ERR_VAL;
598   }
599
600   if (conn->err != ERR_OK) {
601     return conn->err;
602   }
603
604   if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
605     return (conn->err = ERR_MEM);
606   }
607
608   LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %d bytes\n", buf->p->tot_len));
609   msg->type = API_MSG_SEND;
610   msg->msg.conn = conn;
611   msg->msg.msg.p = buf->p;
612   api_msg_post(msg);
613
614   sys_mbox_fetch(conn->mbox, NULL);
615   memp_free(MEMP_API_MSG, msg);
616   return conn->err;
617 }
618
619 err_t
620 netconn_write(struct netconn *conn, void *dataptr, u16_t size, u8_t copy)
621 {
622   struct api_msg *msg;
623   u16_t len;
624   
625   if (conn == NULL) {
626     return ERR_VAL;
627   }
628
629   if (conn->err != ERR_OK) {
630     return conn->err;
631   }
632   
633   if (conn->sem == SYS_SEM_NULL) {
634     conn->sem = sys_sem_new(0);
635     if (conn->sem == SYS_SEM_NULL) {
636       return ERR_MEM;
637     }
638   }
639
640   if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
641     return (conn->err = ERR_MEM);
642   }
643   msg->type = API_MSG_WRITE;
644   msg->msg.conn = conn;
645         
646
647   conn->state = NETCONN_WRITE;
648   while (conn->err == ERR_OK && size > 0) {
649     msg->msg.msg.w.dataptr = dataptr;
650     msg->msg.msg.w.copy = copy;
651     
652     if (conn->type == NETCONN_TCP) {
653       if (tcp_sndbuf(conn->pcb.tcp) == 0) {
654   sys_sem_wait(conn->sem);
655   if (conn->err != ERR_OK) {
656     goto ret;
657   }
658       }
659       if (size > tcp_sndbuf(conn->pcb.tcp)) {
660   /* We cannot send more than one send buffer's worth of data at a
661      time. */
662   len = tcp_sndbuf(conn->pcb.tcp);
663       } else {
664   len = size;
665       }
666     } else {
667       len = size;
668     }
669     
670     LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_write: writing %d bytes (%d)\n", len, copy));
671     msg->msg.msg.w.len = len;
672     api_msg_post(msg);
673     sys_mbox_fetch(conn->mbox, NULL);    
674     if (conn->err == ERR_OK) {
675       dataptr = (void *)((char *)dataptr + len);
676       size -= len;
677     } else if (conn->err == ERR_MEM) {
678       conn->err = ERR_OK;
679       sys_sem_wait(conn->sem);
680     } else {
681       goto ret;
682     }
683   }
684  ret:
685   memp_free(MEMP_API_MSG, msg);
686   conn->state = NETCONN_NONE;
687   if (conn->sem != SYS_SEM_NULL) {
688     sys_sem_free(conn->sem);
689     conn->sem = SYS_SEM_NULL;
690   }
691   
692   return conn->err;
693 }
694
695 err_t
696 netconn_close(struct netconn *conn)
697 {
698   struct api_msg *msg;
699
700   if (conn == NULL) {
701     return ERR_VAL;
702   }
703   if ((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
704     return (conn->err = ERR_MEM);
705   }
706
707   conn->state = NETCONN_CLOSE;
708  again:
709   msg->type = API_MSG_CLOSE;
710   msg->msg.conn = conn;
711   api_msg_post(msg);
712   sys_mbox_fetch(conn->mbox, NULL);
713   if (conn->err == ERR_MEM &&
714      conn->sem != SYS_SEM_NULL) {
715     sys_sem_wait(conn->sem);
716     goto again;
717   }
718   conn->state = NETCONN_NONE;
719   memp_free(MEMP_API_MSG, msg);
720   return conn->err;
721 }
722
723 err_t
724 netconn_err(struct netconn *conn)
725 {
726   return conn->err;
727 }
728