]> git.sur5r.net Git - freertos/blob - Demo/Common/ethernet/uIP/uip-1.0/uip/uip.h
8ff9a240c9cbbd01cee6cfd490c107d733f221dd
[freertos] / Demo / Common / ethernet / uIP / uip-1.0 / uip / uip.h
1 \r
2 /**\r
3  * \addtogroup uip\r
4  * @{\r
5  */\r
6 \r
7 /**\r
8  * \file\r
9  * Header file for the uIP TCP/IP stack.\r
10  * \author Adam Dunkels <adam@dunkels.com>\r
11  *\r
12  * The uIP TCP/IP stack header file contains definitions for a number\r
13  * of C macros that are used by uIP programs as well as internal uIP\r
14  * structures, TCP/IP header structures and function declarations.\r
15  *\r
16  */\r
17 \r
18 \r
19 /*\r
20  * Copyright (c) 2001-2003, Adam Dunkels.\r
21  * All rights reserved.\r
22  *\r
23  * Redistribution and use in source and binary forms, with or without\r
24  * modification, are permitted provided that the following conditions\r
25  * are met:\r
26  * 1. Redistributions of source code must retain the above copyright\r
27  *    notice, this list of conditions and the following disclaimer.\r
28  * 2. Redistributions in binary form must reproduce the above copyright\r
29  *    notice, this list of conditions and the following disclaimer in the\r
30  *    documentation and/or other materials provided with the distribution.\r
31  * 3. The name of the author may not be used to endorse or promote\r
32  *    products derived from this software without specific prior\r
33  *    written permission.\r
34  *\r
35  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS\r
36  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
37  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
38  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY\r
39  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
40  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE\r
41  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
42  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\r
43  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
44  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
45  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
46  *\r
47  * This file is part of the uIP TCP/IP stack.\r
48  *\r
49  * $Id: uip.h,v 1.40 2006/06/08 07:12:07 adam Exp $\r
50  *\r
51  */\r
52 \r
53 #ifndef __UIP_H__\r
54 #define __UIP_H__\r
55 \r
56 #include "uipopt.h"\r
57 \r
58 /**\r
59  * Repressentation of an IP address.\r
60  *\r
61  */\r
62 typedef u16_t uip_ip4addr_t[2];\r
63 typedef u16_t uip_ip6addr_t[8];\r
64 #if UIP_CONF_IPV6\r
65 typedef uip_ip6addr_t uip_ipaddr_t;\r
66 #else /* UIP_CONF_IPV6 */\r
67 typedef uip_ip4addr_t uip_ipaddr_t;\r
68 #endif /* UIP_CONF_IPV6 */\r
69 \r
70 /*---------------------------------------------------------------------------*/\r
71 /* First, the functions that should be called from the\r
72  * system. Initialization, the periodic timer and incoming packets are\r
73  * handled by the following three functions.\r
74  */\r
75 \r
76 /**\r
77  * \defgroup uipconffunc uIP configuration functions\r
78  * @{\r
79  *\r
80  * The uIP configuration functions are used for setting run-time\r
81  * parameters in uIP such as IP addresses.\r
82  */\r
83 \r
84 /**\r
85  * Set the IP address of this host.\r
86  *\r
87  * The IP address is represented as a 4-byte array where the first\r
88  * octet of the IP address is put in the first member of the 4-byte\r
89  * array.\r
90  *\r
91  * Example:\r
92  \code\r
93 \r
94  uip_ipaddr_t addr;\r
95 \r
96  uip_ipaddr(&addr, 192,168,1,2);\r
97  uip_sethostaddr(&addr);\r
98  \r
99  \endcode\r
100  * \param addr A pointer to an IP address of type uip_ipaddr_t;\r
101  *\r
102  * \sa uip_ipaddr()\r
103  *\r
104  * \hideinitializer\r
105  */\r
106 #define uip_sethostaddr(addr) uip_ipaddr_copy(uip_hostaddr, (addr))\r
107 \r
108 /**\r
109  * Get the IP address of this host.\r
110  *\r
111  * The IP address is represented as a 4-byte array where the first\r
112  * octet of the IP address is put in the first member of the 4-byte\r
113  * array.\r
114  *\r
115  * Example:\r
116  \code\r
117  uip_ipaddr_t hostaddr;\r
118 \r
119  uip_gethostaddr(&hostaddr);\r
120  \endcode\r
121  * \param addr A pointer to a uip_ipaddr_t variable that will be\r
122  * filled in with the currently configured IP address.\r
123  *\r
124  * \hideinitializer\r
125  */\r
126 #define uip_gethostaddr(addr) uip_ipaddr_copy((addr), uip_hostaddr)\r
127 \r
128 /**\r
129  * Set the default router's IP address.\r
130  *\r
131  * \param addr A pointer to a uip_ipaddr_t variable containing the IP\r
132  * address of the default router.\r
133  *\r
134  * \sa uip_ipaddr()\r
135  *\r
136  * \hideinitializer\r
137  */\r
138 #define uip_setdraddr(addr) uip_ipaddr_copy(uip_draddr, (addr))\r
139 \r
140 /**\r
141  * Set the netmask.\r
142  *\r
143  * \param addr A pointer to a uip_ipaddr_t variable containing the IP\r
144  * address of the netmask.\r
145  *\r
146  * \sa uip_ipaddr()\r
147  *\r
148  * \hideinitializer\r
149  */\r
150 #define uip_setnetmask(addr) uip_ipaddr_copy(uip_netmask, (addr))\r
151 \r
152 \r
153 /**\r
154  * Get the default router's IP address.\r
155  *\r
156  * \param addr A pointer to a uip_ipaddr_t variable that will be\r
157  * filled in with the IP address of the default router.\r
158  *\r
159  * \hideinitializer\r
160  */\r
161 #define uip_getdraddr(addr) uip_ipaddr_copy((addr), uip_draddr)\r
162 \r
163 /**\r
164  * Get the netmask.\r
165  *\r
166  * \param addr A pointer to a uip_ipaddr_t variable that will be\r
167  * filled in with the value of the netmask.\r
168  *\r
169  * \hideinitializer\r
170  */\r
171 #define uip_getnetmask(addr) uip_ipaddr_copy((addr), uip_netmask)\r
172 \r
173 /** @} */\r
174 \r
175 /**\r
176  * \defgroup uipinit uIP initialization functions\r
177  * @{\r
178  *\r
179  * The uIP initialization functions are used for booting uIP.\r
180  */\r
181 \r
182 /**\r
183  * uIP initialization function.\r
184  *\r
185  * This function should be called at boot up to initilize the uIP\r
186  * TCP/IP stack.\r
187  */\r
188 void uip_init(void);\r
189 \r
190 /**\r
191  * uIP initialization function.\r
192  *\r
193  * This function may be used at boot time to set the initial ip_id.\r
194  */\r
195 void uip_setipid(u16_t id);\r
196 \r
197 /** @} */\r
198 \r
199 /**\r
200  * \defgroup uipdevfunc uIP device driver functions\r
201  * @{\r
202  *\r
203  * These functions are used by a network device driver for interacting\r
204  * with uIP.\r
205  */\r
206 \r
207 /**\r
208  * Process an incoming packet.\r
209  *\r
210  * This function should be called when the device driver has received\r
211  * a packet from the network. The packet from the device driver must\r
212  * be present in the uip_buf buffer, and the length of the packet\r
213  * should be placed in the uip_len variable.\r
214  *\r
215  * When the function returns, there may be an outbound packet placed\r
216  * in the uip_buf packet buffer. If so, the uip_len variable is set to\r
217  * the length of the packet. If no packet is to be sent out, the\r
218  * uip_len variable is set to 0.\r
219  *\r
220  * The usual way of calling the function is presented by the source\r
221  * code below.\r
222  \code\r
223   uip_len = devicedriver_poll();\r
224   if(uip_len > 0) {\r
225     uip_input();\r
226     if(uip_len > 0) {\r
227       devicedriver_send();\r
228     }\r
229   }\r
230  \endcode\r
231  *\r
232  * \note If you are writing a uIP device driver that needs ARP\r
233  * (Address Resolution Protocol), e.g., when running uIP over\r
234  * Ethernet, you will need to call the uIP ARP code before calling\r
235  * this function:\r
236  \code\r
237   #define BUF ((struct uip_eth_hdr *)&uip_buf[0])\r
238   uip_len = ethernet_devicedrver_poll();\r
239   if(uip_len > 0) {\r
240     if(BUF->type == HTONS(UIP_ETHTYPE_IP)) {\r
241       uip_arp_ipin();\r
242       uip_input();\r
243       if(uip_len > 0) {\r
244         uip_arp_out();\r
245         ethernet_devicedriver_send();\r
246       }\r
247     } else if(BUF->type == HTONS(UIP_ETHTYPE_ARP)) {\r
248       uip_arp_arpin();\r
249       if(uip_len > 0) {\r
250         ethernet_devicedriver_send();\r
251       }\r
252     }\r
253  \endcode\r
254  *\r
255  * \hideinitializer\r
256  */\r
257 #define uip_input()        uip_process(UIP_DATA)\r
258 \r
259 /**\r
260  * Periodic processing for a connection identified by its number.\r
261  *\r
262  * This function does the necessary periodic processing (timers,\r
263  * polling) for a uIP TCP conneciton, and should be called when the\r
264  * periodic uIP timer goes off. It should be called for every\r
265  * connection, regardless of whether they are open of closed.\r
266  *\r
267  * When the function returns, it may have an outbound packet waiting\r
268  * for service in the uIP packet buffer, and if so the uip_len\r
269  * variable is set to a value larger than zero. The device driver\r
270  * should be called to send out the packet.\r
271  *\r
272  * The ususal way of calling the function is through a for() loop like\r
273  * this:\r
274  \code\r
275   for(i = 0; i < UIP_CONNS; ++i) {\r
276     uip_periodic(i);\r
277     if(uip_len > 0) {\r
278       devicedriver_send();\r
279     }\r
280   }\r
281  \endcode\r
282  *\r
283  * \note If you are writing a uIP device driver that needs ARP\r
284  * (Address Resolution Protocol), e.g., when running uIP over\r
285  * Ethernet, you will need to call the uip_arp_out() function before\r
286  * calling the device driver:\r
287  \code\r
288   for(i = 0; i < UIP_CONNS; ++i) {\r
289     uip_periodic(i);\r
290     if(uip_len > 0) {\r
291       uip_arp_out();\r
292       ethernet_devicedriver_send();\r
293     }\r
294   }\r
295  \endcode\r
296  *\r
297  * \param conn The number of the connection which is to be periodically polled.\r
298  *\r
299  * \hideinitializer\r
300  */\r
301 #define uip_periodic(conn) do { uip_conn = &uip_conns[conn]; \\r
302                                 uip_process(UIP_TIMER); } while (0)\r
303 \r
304 /**\r
305  *\r
306  *\r
307  */\r
308 #define uip_conn_active(conn) (uip_conns[conn].tcpstateflags != UIP_CLOSED)\r
309 \r
310 /**\r
311  * Perform periodic processing for a connection identified by a pointer\r
312  * to its structure.\r
313  *\r
314  * Same as uip_periodic() but takes a pointer to the actual uip_conn\r
315  * struct instead of an integer as its argument. This function can be\r
316  * used to force periodic processing of a specific connection.\r
317  *\r
318  * \param conn A pointer to the uip_conn struct for the connection to\r
319  * be processed.\r
320  *\r
321  * \hideinitializer\r
322  */\r
323 #define uip_periodic_conn(conn) do { uip_conn = conn; \\r
324                                      uip_process(UIP_TIMER); } while (0)\r
325 \r
326 /**\r
327  * Reuqest that a particular connection should be polled.\r
328  *\r
329  * Similar to uip_periodic_conn() but does not perform any timer\r
330  * processing. The application is polled for new data.\r
331  *\r
332  * \param conn A pointer to the uip_conn struct for the connection to\r
333  * be processed.\r
334  *\r
335  * \hideinitializer\r
336  */\r
337 #define uip_poll_conn(conn) do { uip_conn = conn; \\r
338                                  uip_process(UIP_POLL_REQUEST); } while (0)\r
339 \r
340 \r
341 #if UIP_UDP\r
342 /**\r
343  * Periodic processing for a UDP connection identified by its number.\r
344  *\r
345  * This function is essentially the same as uip_periodic(), but for\r
346  * UDP connections. It is called in a similar fashion as the\r
347  * uip_periodic() function:\r
348  \code\r
349   for(i = 0; i < UIP_UDP_CONNS; i++) {\r
350     uip_udp_periodic(i);\r
351     if(uip_len > 0) {\r
352       devicedriver_send();\r
353     }\r
354   }\r
355  \endcode\r
356  *\r
357  * \note As for the uip_periodic() function, special care has to be\r
358  * taken when using uIP together with ARP and Ethernet:\r
359  \code\r
360   for(i = 0; i < UIP_UDP_CONNS; i++) {\r
361     uip_udp_periodic(i);\r
362     if(uip_len > 0) {\r
363       uip_arp_out();\r
364       ethernet_devicedriver_send();\r
365     }\r
366   }\r
367  \endcode\r
368  *\r
369  * \param conn The number of the UDP connection to be processed.\r
370  *\r
371  * \hideinitializer\r
372  */\r
373 #define uip_udp_periodic(conn) do { uip_udp_conn = &uip_udp_conns[conn]; \\r
374                                 uip_process(UIP_UDP_TIMER); } while (0)\r
375 \r
376 /**\r
377  * Periodic processing for a UDP connection identified by a pointer to\r
378  * its structure.\r
379  *\r
380  * Same as uip_udp_periodic() but takes a pointer to the actual\r
381  * uip_conn struct instead of an integer as its argument. This\r
382  * function can be used to force periodic processing of a specific\r
383  * connection.\r
384  *\r
385  * \param conn A pointer to the uip_udp_conn struct for the connection\r
386  * to be processed.\r
387  *\r
388  * \hideinitializer\r
389  */\r
390 #define uip_udp_periodic_conn(conn) do { uip_udp_conn = conn; \\r
391                                          uip_process(UIP_UDP_TIMER); } while (0)\r
392 \r
393 \r
394 #endif /* UIP_UDP */\r
395 \r
396 /**\r
397  * The uIP packet buffer.\r
398  *\r
399  * The uip_buf array is used to hold incoming and outgoing\r
400  * packets. The device driver should place incoming data into this\r
401  * buffer. When sending data, the device driver should read the link\r
402  * level headers and the TCP/IP headers from this buffer. The size of\r
403  * the link level headers is configured by the UIP_LLH_LEN define.\r
404  *\r
405  * \note The application data need not be placed in this buffer, so\r
406  * the device driver must read it from the place pointed to by the\r
407  * uip_appdata pointer as illustrated by the following example:\r
408  \code\r
409  void\r
410  devicedriver_send(void)\r
411  {\r
412     hwsend(&uip_buf[0], UIP_LLH_LEN);\r
413     if(uip_len <= UIP_LLH_LEN + UIP_TCPIP_HLEN) {\r
414       hwsend(&uip_buf[UIP_LLH_LEN], uip_len - UIP_LLH_LEN);\r
415     } else {\r
416       hwsend(&uip_buf[UIP_LLH_LEN], UIP_TCPIP_HLEN);\r
417       hwsend(uip_appdata, uip_len - UIP_TCPIP_HLEN - UIP_LLH_LEN);\r
418     }\r
419  }\r
420  \endcode\r
421  */\r
422 extern u8_t uip_buf[UIP_BUFSIZE+2];\r
423 \r
424 /** @} */\r
425 \r
426 /*---------------------------------------------------------------------------*/\r
427 /* Functions that are used by the uIP application program. Opening and\r
428  * closing connections, sending and receiving data, etc. is all\r
429  * handled by the functions below.\r
430 */\r
431 /**\r
432  * \defgroup uipappfunc uIP application functions\r
433  * @{\r
434  *\r
435  * Functions used by an application running of top of uIP.\r
436  */\r
437 \r
438 /**\r
439  * Start listening to the specified port.\r
440  *\r
441  * \note Since this function expects the port number in network byte\r
442  * order, a conversion using HTONS() or htons() is necessary.\r
443  *\r
444  \code\r
445  uip_listen(HTONS(80));\r
446  \endcode\r
447  *\r
448  * \param port A 16-bit port number in network byte order.\r
449  */\r
450 void uip_listen(u16_t port);\r
451 \r
452 /**\r
453  * Stop listening to the specified port.\r
454  *\r
455  * \note Since this function expects the port number in network byte\r
456  * order, a conversion using HTONS() or htons() is necessary.\r
457  *\r
458  \code\r
459  uip_unlisten(HTONS(80));\r
460  \endcode\r
461  *\r
462  * \param port A 16-bit port number in network byte order.\r
463  */\r
464 void uip_unlisten(u16_t port);\r
465 \r
466 /**\r
467  * Connect to a remote host using TCP.\r
468  *\r
469  * This function is used to start a new connection to the specified\r
470  * port on the specied host. It allocates a new connection identifier,\r
471  * sets the connection to the SYN_SENT state and sets the\r
472  * retransmission timer to 0. This will cause a TCP SYN segment to be\r
473  * sent out the next time this connection is periodically processed,\r
474  * which usually is done within 0.5 seconds after the call to\r
475  * uip_connect().\r
476  *\r
477  * \note This function is avaliable only if support for active open\r
478  * has been configured by defining UIP_ACTIVE_OPEN to 1 in uipopt.h.\r
479  *\r
480  * \note Since this function requires the port number to be in network\r
481  * byte order, a conversion using HTONS() or htons() is necessary.\r
482  *\r
483  \code\r
484  uip_ipaddr_t ipaddr;\r
485 \r
486  uip_ipaddr(&ipaddr, 192,168,1,2);\r
487  uip_connect(&ipaddr, HTONS(80));\r
488  \endcode\r
489  *\r
490  * \param ripaddr The IP address of the remote hot.\r
491  *\r
492  * \param port A 16-bit port number in network byte order.\r
493  *\r
494  * \return A pointer to the uIP connection identifier for the new connection,\r
495  * or NULL if no connection could be allocated.\r
496  *\r
497  */\r
498 struct uip_conn *uip_connect(uip_ipaddr_t *ripaddr, u16_t port);\r
499 \r
500 \r
501 \r
502 /**\r
503  * \internal\r
504  *\r
505  * Check if a connection has outstanding (i.e., unacknowledged) data.\r
506  *\r
507  * \param conn A pointer to the uip_conn structure for the connection.\r
508  *\r
509  * \hideinitializer\r
510  */\r
511 #define uip_outstanding(conn) ((conn)->len)\r
512 \r
513 /**\r
514  * Send data on the current connection.\r
515  *\r
516  * This function is used to send out a single segment of TCP\r
517  * data. Only applications that have been invoked by uIP for event\r
518  * processing can send data.\r
519  *\r
520  * The amount of data that actually is sent out after a call to this\r
521  * funcion is determined by the maximum amount of data TCP allows. uIP\r
522  * will automatically crop the data so that only the appropriate\r
523  * amount of data is sent. The function uip_mss() can be used to query\r
524  * uIP for the amount of data that actually will be sent.\r
525  *\r
526  * \note This function does not guarantee that the sent data will\r
527  * arrive at the destination. If the data is lost in the network, the\r
528  * application will be invoked with the uip_rexmit() event being\r
529  * set. The application will then have to resend the data using this\r
530  * function.\r
531  *\r
532  * \param data A pointer to the data which is to be sent.\r
533  *\r
534  * \param len The maximum amount of data bytes to be sent.\r
535  *\r
536  * \hideinitializer\r
537  */\r
538 void uip_send(const void *data, int len);\r
539 \r
540 /**\r
541  * The length of any incoming data that is currently avaliable (if avaliable)\r
542  * in the uip_appdata buffer.\r
543  *\r
544  * The test function uip_data() must first be used to check if there\r
545  * is any data available at all.\r
546  *\r
547  * \hideinitializer\r
548  */\r
549 /*void uip_datalen(void);*/\r
550 #define uip_datalen()       uip_len\r
551 \r
552 /**\r
553  * The length of any out-of-band data (urgent data) that has arrived\r
554  * on the connection.\r
555  *\r
556  * \note The configuration parameter UIP_URGDATA must be set for this\r
557  * function to be enabled.\r
558  *\r
559  * \hideinitializer\r
560  */\r
561 #define uip_urgdatalen()    uip_urglen\r
562 \r
563 /**\r
564  * Close the current connection.\r
565  *\r
566  * This function will close the current connection in a nice way.\r
567  *\r
568  * \hideinitializer\r
569  */\r
570 #define uip_close()         (uip_flags = UIP_CLOSE)\r
571 \r
572 /**\r
573  * Abort the current connection.\r
574  *\r
575  * This function will abort (reset) the current connection, and is\r
576  * usually used when an error has occured that prevents using the\r
577  * uip_close() function.\r
578  *\r
579  * \hideinitializer\r
580  */\r
581 #define uip_abort()         (uip_flags = UIP_ABORT)\r
582 \r
583 /**\r
584  * Tell the sending host to stop sending data.\r
585  *\r
586  * This function will close our receiver's window so that we stop\r
587  * receiving data for the current connection.\r
588  *\r
589  * \hideinitializer\r
590  */\r
591 #define uip_stop()          (uip_conn->tcpstateflags |= UIP_STOPPED)\r
592 \r
593 /**\r
594  * Find out if the current connection has been previously stopped with\r
595  * uip_stop().\r
596  *\r
597  * \hideinitializer\r
598  */\r
599 #define uip_stopped(conn)   ((conn)->tcpstateflags & UIP_STOPPED)\r
600 \r
601 /**\r
602  * Restart the current connection, if is has previously been stopped\r
603  * with uip_stop().\r
604  *\r
605  * This function will open the receiver's window again so that we\r
606  * start receiving data for the current connection.\r
607  *\r
608  * \hideinitializer\r
609  */\r
610 #define uip_restart()         do { uip_flags |= UIP_NEWDATA; \\r
611                                    uip_conn->tcpstateflags &= ~UIP_STOPPED; \\r
612                               } while(0)\r
613 \r
614 \r
615 /* uIP tests that can be made to determine in what state the current\r
616    connection is, and what the application function should do. */\r
617 \r
618 /**\r
619  * Is the current connection a UDP connection?\r
620  *\r
621  * This function checks whether the current connection is a UDP connection.\r
622  *\r
623  * \hideinitializer\r
624  *\r
625  */\r
626 #define uip_udpconnection() (uip_conn == NULL)\r
627 \r
628 /**\r
629  * Is new incoming data available?\r
630  *\r
631  * Will reduce to non-zero if there is new data for the application\r
632  * present at the uip_appdata pointer. The size of the data is\r
633  * avaliable through the uip_len variable.\r
634  *\r
635  * \hideinitializer\r
636  */\r
637 #define uip_newdata()   (uip_flags & UIP_NEWDATA)\r
638 \r
639 /**\r
640  * Has previously sent data been acknowledged?\r
641  *\r
642  * Will reduce to non-zero if the previously sent data has been\r
643  * acknowledged by the remote host. This means that the application\r
644  * can send new data.\r
645  *\r
646  * \hideinitializer\r
647  */\r
648 #define uip_acked()   (uip_flags & UIP_ACKDATA)\r
649 \r
650 /**\r
651  * Has the connection just been connected?\r
652  *\r
653  * Reduces to non-zero if the current connection has been connected to\r
654  * a remote host. This will happen both if the connection has been\r
655  * actively opened (with uip_connect()) or passively opened (with\r
656  * uip_listen()).\r
657  *\r
658  * \hideinitializer\r
659  */\r
660 #define uip_connected() (uip_flags & UIP_CONNECTED)\r
661 \r
662 /**\r
663  * Has the connection been closed by the other end?\r
664  *\r
665  * Is non-zero if the connection has been closed by the remote\r
666  * host. The application may then do the necessary clean-ups.\r
667  *\r
668  * \hideinitializer\r
669  */\r
670 #define uip_closed()    (uip_flags & UIP_CLOSE)\r
671 \r
672 /**\r
673  * Has the connection been aborted by the other end?\r
674  *\r
675  * Non-zero if the current connection has been aborted (reset) by the\r
676  * remote host.\r
677  *\r
678  * \hideinitializer\r
679  */\r
680 #define uip_aborted()    (uip_flags & UIP_ABORT)\r
681 \r
682 /**\r
683  * Has the connection timed out?\r
684  *\r
685  * Non-zero if the current connection has been aborted due to too many\r
686  * retransmissions.\r
687  *\r
688  * \hideinitializer\r
689  */\r
690 #define uip_timedout()    (uip_flags & UIP_TIMEDOUT)\r
691 \r
692 /**\r
693  * Do we need to retransmit previously data?\r
694  *\r
695  * Reduces to non-zero if the previously sent data has been lost in\r
696  * the network, and the application should retransmit it. The\r
697  * application should send the exact same data as it did the last\r
698  * time, using the uip_send() function.\r
699  *\r
700  * \hideinitializer\r
701  */\r
702 #define uip_rexmit()     (uip_flags & UIP_REXMIT)\r
703 \r
704 /**\r
705  * Is the connection being polled by uIP?\r
706  *\r
707  * Is non-zero if the reason the application is invoked is that the\r
708  * current connection has been idle for a while and should be\r
709  * polled.\r
710  *\r
711  * The polling event can be used for sending data without having to\r
712  * wait for the remote host to send data.\r
713  *\r
714  * \hideinitializer\r
715  */\r
716 #define uip_poll()       (uip_flags & UIP_POLL)\r
717 \r
718 /**\r
719  * Get the initial maxium segment size (MSS) of the current\r
720  * connection.\r
721  *\r
722  * \hideinitializer\r
723  */\r
724 #define uip_initialmss()             (uip_conn->initialmss)\r
725 \r
726 /**\r
727  * Get the current maxium segment size that can be sent on the current\r
728  * connection.\r
729  *\r
730  * The current maxiumum segment size that can be sent on the\r
731  * connection is computed from the receiver's window and the MSS of\r
732  * the connection (which also is available by calling\r
733  * uip_initialmss()).\r
734  *\r
735  * \hideinitializer\r
736  */\r
737 #define uip_mss()             (uip_conn->mss)\r
738 \r
739 /**\r
740  * Set up a new UDP connection.\r
741  *\r
742  * This function sets up a new UDP connection. The function will\r
743  * automatically allocate an unused local port for the new\r
744  * connection. However, another port can be chosen by using the\r
745  * uip_udp_bind() call, after the uip_udp_new() function has been\r
746  * called.\r
747  *\r
748  * Example:\r
749  \code\r
750  uip_ipaddr_t addr;\r
751  struct uip_udp_conn *c;\r
752  \r
753  uip_ipaddr(&addr, 192,168,2,1);\r
754  c = uip_udp_new(&addr, HTONS(12345));\r
755  if(c != NULL) {\r
756    uip_udp_bind(c, HTONS(12344));\r
757  }\r
758  \endcode\r
759  * \param ripaddr The IP address of the remote host.\r
760  *\r
761  * \param rport The remote port number in network byte order.\r
762  *\r
763  * \return The uip_udp_conn structure for the new connection or NULL\r
764  * if no connection could be allocated.\r
765  */\r
766 struct uip_udp_conn *uip_udp_new(uip_ipaddr_t *ripaddr, u16_t rport);\r
767 \r
768 /**\r
769  * Removed a UDP connection.\r
770  *\r
771  * \param conn A pointer to the uip_udp_conn structure for the connection.\r
772  *\r
773  * \hideinitializer\r
774  */\r
775 #define uip_udp_remove(conn) (conn)->lport = 0\r
776 \r
777 /**\r
778  * Bind a UDP connection to a local port.\r
779  *\r
780  * \param conn A pointer to the uip_udp_conn structure for the\r
781  * connection.\r
782  *\r
783  * \param port The local port number, in network byte order.\r
784  *\r
785  * \hideinitializer\r
786  */\r
787 #define uip_udp_bind(conn, port) (conn)->lport = port\r
788 \r
789 /**\r
790  * Send a UDP datagram of length len on the current connection.\r
791  *\r
792  * This function can only be called in response to a UDP event (poll\r
793  * or newdata). The data must be present in the uip_buf buffer, at the\r
794  * place pointed to by the uip_appdata pointer.\r
795  *\r
796  * \param len The length of the data in the uip_buf buffer.\r
797  *\r
798  * \hideinitializer\r
799  */\r
800 #define uip_udp_send(len) uip_send((char *)uip_appdata, len)\r
801 \r
802 /** @} */\r
803 \r
804 /* uIP convenience and converting functions. */\r
805 \r
806 /**\r
807  * \defgroup uipconvfunc uIP conversion functions\r
808  * @{\r
809  *\r
810  * These functions can be used for converting between different data\r
811  * formats used by uIP.\r
812  */\r
813  \r
814 /**\r
815  * Construct an IP address from four bytes.\r
816  *\r
817  * This function constructs an IP address of the type that uIP handles\r
818  * internally from four bytes. The function is handy for specifying IP\r
819  * addresses to use with e.g. the uip_connect() function.\r
820  *\r
821  * Example:\r
822  \code\r
823  uip_ipaddr_t ipaddr;\r
824  struct uip_conn *c;\r
825  \r
826  uip_ipaddr(&ipaddr, 192,168,1,2);\r
827  c = uip_connect(&ipaddr, HTONS(80));\r
828  \endcode\r
829  *\r
830  * \param addr A pointer to a uip_ipaddr_t variable that will be\r
831  * filled in with the IP address.\r
832  *\r
833  * \param addr0 The first octet of the IP address.\r
834  * \param addr1 The second octet of the IP address.\r
835  * \param addr2 The third octet of the IP address.\r
836  * \param addr3 The forth octet of the IP address.\r
837  *\r
838  * \hideinitializer\r
839  */\r
840 #define uip_ipaddr(addr, addr0,addr1,addr2,addr3) do { \\r
841                      ((u16_t *)(addr))[0] = HTONS(((addr0) << 8) | (addr1)); \\r
842                      ((u16_t *)(addr))[1] = HTONS(((addr2) << 8) | (addr3)); \\r
843                   } while(0)\r
844 \r
845 /**\r
846  * Construct an IPv6 address from eight 16-bit words.\r
847  *\r
848  * This function constructs an IPv6 address.\r
849  *\r
850  * \hideinitializer\r
851  */\r
852 #define uip_ip6addr(addr, addr0,addr1,addr2,addr3,addr4,addr5,addr6,addr7) do { \\r
853                      ((u16_t *)(addr))[0] = HTONS((addr0)); \\r
854                      ((u16_t *)(addr))[1] = HTONS((addr1)); \\r
855                      ((u16_t *)(addr))[2] = HTONS((addr2)); \\r
856                      ((u16_t *)(addr))[3] = HTONS((addr3)); \\r
857                      ((u16_t *)(addr))[4] = HTONS((addr4)); \\r
858                      ((u16_t *)(addr))[5] = HTONS((addr5)); \\r
859                      ((u16_t *)(addr))[6] = HTONS((addr6)); \\r
860                      ((u16_t *)(addr))[7] = HTONS((addr7)); \\r
861                   } while(0)\r
862 \r
863 /**\r
864  * Copy an IP address to another IP address.\r
865  *\r
866  * Copies an IP address from one place to another.\r
867  *\r
868  * Example:\r
869  \code\r
870  uip_ipaddr_t ipaddr1, ipaddr2;\r
871 \r
872  uip_ipaddr(&ipaddr1, 192,16,1,2);\r
873  uip_ipaddr_copy(&ipaddr2, &ipaddr1);\r
874  \endcode\r
875  *\r
876  * \param dest The destination for the copy.\r
877  * \param src The source from where to copy.\r
878  *\r
879  * \hideinitializer\r
880  */\r
881 #if !UIP_CONF_IPV6\r
882 #define uip_ipaddr_copy(dest, src) do { \\r
883                      ((u16_t *)dest)[0] = ((u16_t *)src)[0]; \\r
884                      ((u16_t *)dest)[1] = ((u16_t *)src)[1]; \\r
885                   } while(0)\r
886 #else /* !UIP_CONF_IPV6 */\r
887 #define uip_ipaddr_copy(dest, src) memcpy(dest, src, sizeof(uip_ip6addr_t))\r
888 #endif /* !UIP_CONF_IPV6 */\r
889 \r
890 /**\r
891  * Compare two IP addresses\r
892  *\r
893  * Compares two IP addresses.\r
894  *\r
895  * Example:\r
896  \code\r
897  uip_ipaddr_t ipaddr1, ipaddr2;\r
898 \r
899  uip_ipaddr(&ipaddr1, 192,16,1,2);\r
900  if(uip_ipaddr_cmp(&ipaddr2, &ipaddr1)) {\r
901     printf("They are the same");\r
902  }\r
903  \endcode\r
904  *\r
905  * \param addr1 The first IP address.\r
906  * \param addr2 The second IP address.\r
907  *\r
908  * \hideinitializer\r
909  */\r
910 #if !UIP_CONF_IPV6\r
911 #define uip_ipaddr_cmp(addr1, addr2) (((u16_t *)addr1)[0] == ((u16_t *)addr2)[0] && \\r
912                                       ((u16_t *)addr1)[1] == ((u16_t *)addr2)[1])\r
913 #else /* !UIP_CONF_IPV6 */\r
914 #define uip_ipaddr_cmp(addr1, addr2) (memcmp(addr1, addr2, sizeof(uip_ip6addr_t)) == 0)\r
915 #endif /* !UIP_CONF_IPV6 */\r
916 \r
917 /**\r
918  * Compare two IP addresses with netmasks\r
919  *\r
920  * Compares two IP addresses with netmasks. The masks are used to mask\r
921  * out the bits that are to be compared.\r
922  *\r
923  * Example:\r
924  \code\r
925  uip_ipaddr_t ipaddr1, ipaddr2, mask;\r
926 \r
927  uip_ipaddr(&mask, 255,255,255,0);\r
928  uip_ipaddr(&ipaddr1, 192,16,1,2);\r
929  uip_ipaddr(&ipaddr2, 192,16,1,3);\r
930  if(uip_ipaddr_maskcmp(&ipaddr1, &ipaddr2, &mask)) {\r
931     printf("They are the same");\r
932  }\r
933  \endcode\r
934  *\r
935  * \param addr1 The first IP address.\r
936  * \param addr2 The second IP address.\r
937  * \param mask The netmask.\r
938  *\r
939  * \hideinitializer\r
940  */\r
941 #define uip_ipaddr_maskcmp(addr1, addr2, mask) \\r
942                           (((((u16_t *)addr1)[0] & ((u16_t *)mask)[0]) == \\r
943                             (((u16_t *)addr2)[0] & ((u16_t *)mask)[0])) && \\r
944                            ((((u16_t *)addr1)[1] & ((u16_t *)mask)[1]) == \\r
945                             (((u16_t *)addr2)[1] & ((u16_t *)mask)[1])))\r
946 \r
947 \r
948 /**\r
949  * Mask out the network part of an IP address.\r
950  *\r
951  * Masks out the network part of an IP address, given the address and\r
952  * the netmask.\r
953  *\r
954  * Example:\r
955  \code\r
956  uip_ipaddr_t ipaddr1, ipaddr2, netmask;\r
957 \r
958  uip_ipaddr(&ipaddr1, 192,16,1,2);\r
959  uip_ipaddr(&netmask, 255,255,255,0);\r
960  uip_ipaddr_mask(&ipaddr2, &ipaddr1, &netmask);\r
961  \endcode\r
962  *\r
963  * In the example above, the variable "ipaddr2" will contain the IP\r
964  * address 192.168.1.0.\r
965  *\r
966  * \param dest Where the result is to be placed.\r
967  * \param src The IP address.\r
968  * \param mask The netmask.\r
969  *\r
970  * \hideinitializer\r
971  */\r
972 #define uip_ipaddr_mask(dest, src, mask) do { \\r
973                      ((u16_t *)dest)[0] = ((u16_t *)src)[0] & ((u16_t *)mask)[0]; \\r
974                      ((u16_t *)dest)[1] = ((u16_t *)src)[1] & ((u16_t *)mask)[1]; \\r
975                   } while(0)\r
976 \r
977 /**\r
978  * Pick the first octet of an IP address.\r
979  *\r
980  * Picks out the first octet of an IP address.\r
981  *\r
982  * Example:\r
983  \code\r
984  uip_ipaddr_t ipaddr;\r
985  u8_t octet;\r
986 \r
987  uip_ipaddr(&ipaddr, 1,2,3,4);\r
988  octet = uip_ipaddr1(&ipaddr);\r
989  \endcode\r
990  *\r
991  * In the example above, the variable "octet" will contain the value 1.\r
992  *\r
993  * \hideinitializer\r
994  */\r
995 #define uip_ipaddr1(addr) (htons(((u16_t *)(addr))[0]) >> 8)\r
996 \r
997 /**\r
998  * Pick the second octet of an IP address.\r
999  *\r
1000  * Picks out the second octet of an IP address.\r
1001  *\r
1002  * Example:\r
1003  \code\r
1004  uip_ipaddr_t ipaddr;\r
1005  u8_t octet;\r
1006 \r
1007  uip_ipaddr(&ipaddr, 1,2,3,4);\r
1008  octet = uip_ipaddr2(&ipaddr);\r
1009  \endcode\r
1010  *\r
1011  * In the example above, the variable "octet" will contain the value 2.\r
1012  *\r
1013  * \hideinitializer\r
1014  */\r
1015 #define uip_ipaddr2(addr) (htons(((u16_t *)(addr))[0]) & 0xff)\r
1016 \r
1017 /**\r
1018  * Pick the third octet of an IP address.\r
1019  *\r
1020  * Picks out the third octet of an IP address.\r
1021  *\r
1022  * Example:\r
1023  \code\r
1024  uip_ipaddr_t ipaddr;\r
1025  u8_t octet;\r
1026 \r
1027  uip_ipaddr(&ipaddr, 1,2,3,4);\r
1028  octet = uip_ipaddr3(&ipaddr);\r
1029  \endcode\r
1030  *\r
1031  * In the example above, the variable "octet" will contain the value 3.\r
1032  *\r
1033  * \hideinitializer\r
1034  */\r
1035 #define uip_ipaddr3(addr) (htons(((u16_t *)(addr))[1]) >> 8)\r
1036 \r
1037 /**\r
1038  * Pick the fourth octet of an IP address.\r
1039  *\r
1040  * Picks out the fourth octet of an IP address.\r
1041  *\r
1042  * Example:\r
1043  \code\r
1044  uip_ipaddr_t ipaddr;\r
1045  u8_t octet;\r
1046 \r
1047  uip_ipaddr(&ipaddr, 1,2,3,4);\r
1048  octet = uip_ipaddr4(&ipaddr);\r
1049  \endcode\r
1050  *\r
1051  * In the example above, the variable "octet" will contain the value 4.\r
1052  *\r
1053  * \hideinitializer\r
1054  */\r
1055 #define uip_ipaddr4(addr) (htons(((u16_t *)(addr))[1]) & 0xff)\r
1056 \r
1057 /**\r
1058  * Convert 16-bit quantity from host byte order to network byte order.\r
1059  *\r
1060  * This macro is primarily used for converting constants from host\r
1061  * byte order to network byte order. For converting variables to\r
1062  * network byte order, use the htons() function instead.\r
1063  *\r
1064  * \hideinitializer\r
1065  */\r
1066 #ifndef HTONS\r
1067 #   if UIP_BYTE_ORDER == UIP_BIG_ENDIAN\r
1068 #      define HTONS(n) (n)\r
1069 #   else /* UIP_BYTE_ORDER == UIP_BIG_ENDIAN */\r
1070 #      define HTONS(n) (u16_t)((((u16_t) (n)) << 8) | (((u16_t) (n)) >> 8))\r
1071 #   endif /* UIP_BYTE_ORDER == UIP_BIG_ENDIAN */\r
1072 #else\r
1073 #error "HTONS already defined!"\r
1074 #endif /* HTONS */\r
1075 \r
1076 /**\r
1077  * Convert 16-bit quantity from host byte order to network byte order.\r
1078  *\r
1079  * This function is primarily used for converting variables from host\r
1080  * byte order to network byte order. For converting constants to\r
1081  * network byte order, use the HTONS() macro instead.\r
1082  */\r
1083 #ifndef htons\r
1084 u16_t htons(u16_t val);\r
1085 #endif /* htons */\r
1086 #ifndef ntohs\r
1087 #define ntohs htons\r
1088 #endif\r
1089 \r
1090 /** @} */\r
1091 \r
1092 /**\r
1093  * Pointer to the application data in the packet buffer.\r
1094  *\r
1095  * This pointer points to the application data when the application is\r
1096  * called. If the application wishes to send data, the application may\r
1097  * use this space to write the data into before calling uip_send().\r
1098  */\r
1099 extern void *uip_appdata;\r
1100 \r
1101 #if UIP_URGDATA > 0\r
1102 /* u8_t *uip_urgdata:\r
1103  *\r
1104  * This pointer points to any urgent data that has been received. Only\r
1105  * present if compiled with support for urgent data (UIP_URGDATA).\r
1106  */\r
1107 extern void *uip_urgdata;\r
1108 #endif /* UIP_URGDATA > 0 */\r
1109 \r
1110 \r
1111 /**\r
1112  * \defgroup uipdrivervars Variables used in uIP device drivers\r
1113  * @{\r
1114  *\r
1115  * uIP has a few global variables that are used in device drivers for\r
1116  * uIP.\r
1117  */\r
1118 \r
1119 /**\r
1120  * The length of the packet in the uip_buf buffer.\r
1121  *\r
1122  * The global variable uip_len holds the length of the packet in the\r
1123  * uip_buf buffer.\r
1124  *\r
1125  * When the network device driver calls the uIP input function,\r
1126  * uip_len should be set to the length of the packet in the uip_buf\r
1127  * buffer.\r
1128  *\r
1129  * When sending packets, the device driver should use the contents of\r
1130  * the uip_len variable to determine the length of the outgoing\r
1131  * packet.\r
1132  *\r
1133  */\r
1134 extern u16_t uip_len;\r
1135 \r
1136 /** @} */\r
1137 \r
1138 #if UIP_URGDATA > 0\r
1139 extern u16_t uip_urglen, uip_surglen;\r
1140 #endif /* UIP_URGDATA > 0 */\r
1141 \r
1142 \r
1143 /**\r
1144  * Representation of a uIP TCP connection.\r
1145  *\r
1146  * The uip_conn structure is used for identifying a connection. All\r
1147  * but one field in the structure are to be considered read-only by an\r
1148  * application. The only exception is the appstate field whos purpose\r
1149  * is to let the application store application-specific state (e.g.,\r
1150  * file pointers) for the connection. The type of this field is\r
1151  * configured in the "uipopt.h" header file.\r
1152  */\r
1153 struct uip_conn {\r
1154   uip_ipaddr_t ripaddr;   /**< The IP address of the remote host. */\r
1155   \r
1156   u16_t lport;        /**< The local TCP port, in network byte order. */\r
1157   u16_t rport;        /**< The local remote TCP port, in network byte\r
1158                          order. */\r
1159   \r
1160   u8_t rcv_nxt[4];    /**< The sequence number that we expect to\r
1161                          receive next. */\r
1162   u8_t snd_nxt[4];    /**< The sequence number that was last sent by\r
1163                          us. */\r
1164   u16_t len;          /**< Length of the data that was previously sent. */\r
1165   u16_t mss;          /**< Current maximum segment size for the\r
1166                          connection. */\r
1167   u16_t initialmss;   /**< Initial maximum segment size for the\r
1168                          connection. */\r
1169   u8_t sa;            /**< Retransmission time-out calculation state\r
1170                          variable. */\r
1171   u8_t sv;            /**< Retransmission time-out calculation state\r
1172                          variable. */\r
1173   u8_t rto;           /**< Retransmission time-out. */\r
1174   u8_t tcpstateflags; /**< TCP state and flags. */\r
1175   u8_t timer;         /**< The retransmission timer. */\r
1176   u8_t nrtx;          /**< The number of retransmissions for the last\r
1177                          segment sent. */\r
1178 \r
1179   /** The application state. */\r
1180   uip_tcp_appstate_t appstate;\r
1181 };\r
1182 \r
1183 \r
1184 /**\r
1185  * Pointer to the current TCP connection.\r
1186  *\r
1187  * The uip_conn pointer can be used to access the current TCP\r
1188  * connection.\r
1189  */\r
1190 extern struct uip_conn *uip_conn;\r
1191 /* The array containing all uIP connections. */\r
1192 extern struct uip_conn uip_conns[UIP_CONNS];\r
1193 /**\r
1194  * \addtogroup uiparch\r
1195  * @{\r
1196  */\r
1197 \r
1198 /**\r
1199  * 4-byte array used for the 32-bit sequence number calculations.\r
1200  */\r
1201 extern u8_t uip_acc32[4];\r
1202 \r
1203 /** @} */\r
1204 \r
1205 \r
1206 #if UIP_UDP\r
1207 /**\r
1208  * Representation of a uIP UDP connection.\r
1209  */\r
1210 struct uip_udp_conn {\r
1211   uip_ipaddr_t ripaddr;   /**< The IP address of the remote peer. */\r
1212   u16_t lport;        /**< The local port number in network byte order. */\r
1213   u16_t rport;        /**< The remote port number in network byte order. */\r
1214   u8_t  ttl;          /**< Default time-to-live. */\r
1215 \r
1216   /** The application state. */\r
1217   uip_udp_appstate_t appstate;\r
1218 };\r
1219 \r
1220 /**\r
1221  * The current UDP connection.\r
1222  */\r
1223 extern struct uip_udp_conn *uip_udp_conn;\r
1224 extern struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];\r
1225 #endif /* UIP_UDP */\r
1226 \r
1227 /**\r
1228  * The structure holding the TCP/IP statistics that are gathered if\r
1229  * UIP_STATISTICS is set to 1.\r
1230  *\r
1231  */\r
1232 struct uip_stats {\r
1233   struct {\r
1234     uip_stats_t drop;     /**< Number of dropped packets at the IP\r
1235                              layer. */\r
1236     uip_stats_t recv;     /**< Number of received packets at the IP\r
1237                              layer. */\r
1238     uip_stats_t sent;     /**< Number of sent packets at the IP\r
1239                              layer. */\r
1240     uip_stats_t vhlerr;   /**< Number of packets dropped due to wrong\r
1241                              IP version or header length. */\r
1242     uip_stats_t hblenerr; /**< Number of packets dropped due to wrong\r
1243                              IP length, high byte. */\r
1244     uip_stats_t lblenerr; /**< Number of packets dropped due to wrong\r
1245                              IP length, low byte. */\r
1246     uip_stats_t fragerr;  /**< Number of packets dropped since they\r
1247                              were IP fragments. */\r
1248     uip_stats_t chkerr;   /**< Number of packets dropped due to IP\r
1249                              checksum errors. */\r
1250     uip_stats_t protoerr; /**< Number of packets dropped since they\r
1251                              were neither ICMP, UDP nor TCP. */\r
1252   } ip;                   /**< IP statistics. */\r
1253   struct {\r
1254     uip_stats_t drop;     /**< Number of dropped ICMP packets. */\r
1255     uip_stats_t recv;     /**< Number of received ICMP packets. */\r
1256     uip_stats_t sent;     /**< Number of sent ICMP packets. */\r
1257     uip_stats_t typeerr;  /**< Number of ICMP packets with a wrong\r
1258                              type. */\r
1259   } icmp;                 /**< ICMP statistics. */\r
1260   struct {\r
1261     uip_stats_t drop;     /**< Number of dropped TCP segments. */\r
1262     uip_stats_t recv;     /**< Number of recived TCP segments. */\r
1263     uip_stats_t sent;     /**< Number of sent TCP segments. */\r
1264     uip_stats_t chkerr;   /**< Number of TCP segments with a bad\r
1265                              checksum. */\r
1266     uip_stats_t ackerr;   /**< Number of TCP segments with a bad ACK\r
1267                              number. */\r
1268     uip_stats_t rst;      /**< Number of recevied TCP RST (reset) segments. */\r
1269     uip_stats_t rexmit;   /**< Number of retransmitted TCP segments. */\r
1270     uip_stats_t syndrop;  /**< Number of dropped SYNs due to too few\r
1271                              connections was avaliable. */\r
1272     uip_stats_t synrst;   /**< Number of SYNs for closed ports,\r
1273                              triggering a RST. */\r
1274   } tcp;                  /**< TCP statistics. */\r
1275 #if UIP_UDP\r
1276   struct {\r
1277     uip_stats_t drop;     /**< Number of dropped UDP segments. */\r
1278     uip_stats_t recv;     /**< Number of recived UDP segments. */\r
1279     uip_stats_t sent;     /**< Number of sent UDP segments. */\r
1280     uip_stats_t chkerr;   /**< Number of UDP segments with a bad\r
1281                              checksum. */\r
1282   } udp;                  /**< UDP statistics. */\r
1283 #endif /* UIP_UDP */\r
1284 };\r
1285 \r
1286 /**\r
1287  * The uIP TCP/IP statistics.\r
1288  *\r
1289  * This is the variable in which the uIP TCP/IP statistics are gathered.\r
1290  */\r
1291 extern struct uip_stats uip_stat;\r
1292 \r
1293 \r
1294 /*---------------------------------------------------------------------------*/\r
1295 /* All the stuff below this point is internal to uIP and should not be\r
1296  * used directly by an application or by a device driver.\r
1297  */\r
1298 /*---------------------------------------------------------------------------*/\r
1299 /* u8_t uip_flags:\r
1300  *\r
1301  * When the application is called, uip_flags will contain the flags\r
1302  * that are defined in this file. Please read below for more\r
1303  * infomation.\r
1304  */\r
1305 extern u8_t uip_flags;\r
1306 \r
1307 /* The following flags may be set in the global variable uip_flags\r
1308    before calling the application callback. The UIP_ACKDATA,\r
1309    UIP_NEWDATA, and UIP_CLOSE flags may both be set at the same time,\r
1310    whereas the others are mutualy exclusive. Note that these flags\r
1311    should *NOT* be accessed directly, but only through the uIP\r
1312    functions/macros. */\r
1313 \r
1314 #define UIP_ACKDATA   1     /* Signifies that the outstanding data was\r
1315                                acked and the application should send\r
1316                                out new data instead of retransmitting\r
1317                                the last data. */\r
1318 #define UIP_NEWDATA   2     /* Flags the fact that the peer has sent\r
1319                                us new data. */\r
1320 #define UIP_REXMIT    4     /* Tells the application to retransmit the\r
1321                                data that was last sent. */\r
1322 #define UIP_POLL      8     /* Used for polling the application, to\r
1323                                check if the application has data that\r
1324                                it wants to send. */\r
1325 #define UIP_CLOSE     16    /* The remote host has closed the\r
1326                                connection, thus the connection has\r
1327                                gone away. Or the application signals\r
1328                                that it wants to close the\r
1329                                connection. */\r
1330 #define UIP_ABORT     32    /* The remote host has aborted the\r
1331                                connection, thus the connection has\r
1332                                gone away. Or the application signals\r
1333                                that it wants to abort the\r
1334                                connection. */\r
1335 #define UIP_CONNECTED 64    /* We have got a connection from a remote\r
1336                                host and have set up a new connection\r
1337                                for it, or an active connection has\r
1338                                been successfully established. */\r
1339 \r
1340 #define UIP_TIMEDOUT  128   /* The connection has been aborted due to\r
1341                                too many retransmissions. */\r
1342 \r
1343 /* uip_process(flag):\r
1344  *\r
1345  * The actual uIP function which does all the work.\r
1346  */\r
1347 void uip_process(u8_t flag);\r
1348 \r
1349 /* The following flags are passed as an argument to the uip_process()\r
1350    function. They are used to distinguish between the two cases where\r
1351    uip_process() is called. It can be called either because we have\r
1352    incoming data that should be processed, or because the periodic\r
1353    timer has fired. These values are never used directly, but only in\r
1354    the macrose defined in this file. */\r
1355  \r
1356 #define UIP_DATA          1     /* Tells uIP that there is incoming\r
1357                                    data in the uip_buf buffer. The\r
1358                                    length of the data is stored in the\r
1359                                    global variable uip_len. */\r
1360 #define UIP_TIMER         2     /* Tells uIP that the periodic timer\r
1361                                    has fired. */\r
1362 #define UIP_POLL_REQUEST  3     /* Tells uIP that a connection should\r
1363                                    be polled. */\r
1364 #define UIP_UDP_SEND_CONN 4     /* Tells uIP that a UDP datagram\r
1365                                    should be constructed in the\r
1366                                    uip_buf buffer. */\r
1367 #if UIP_UDP\r
1368 #define UIP_UDP_TIMER     5\r
1369 #endif /* UIP_UDP */\r
1370 \r
1371 /* The TCP states used in the uip_conn->tcpstateflags. */\r
1372 #define UIP_CLOSED      0\r
1373 #define UIP_SYN_RCVD    1\r
1374 #define UIP_SYN_SENT    2\r
1375 #define UIP_ESTABLISHED 3\r
1376 #define UIP_FIN_WAIT_1  4\r
1377 #define UIP_FIN_WAIT_2  5\r
1378 #define UIP_CLOSING     6\r
1379 #define UIP_TIME_WAIT   7\r
1380 #define UIP_LAST_ACK    8\r
1381 #define UIP_TS_MASK     15\r
1382   \r
1383 #define UIP_STOPPED      16\r
1384 \r
1385 /* The TCP and IP headers. */\r
1386 struct uip_tcpip_hdr {\r
1387 #if UIP_CONF_IPV6\r
1388   /* IPv6 header. */\r
1389   u8_t vtc,\r
1390     tcflow;\r
1391   u16_t flow;\r
1392   u8_t len[2];\r
1393   u8_t proto, ttl;\r
1394   uip_ip6addr_t srcipaddr, destipaddr;\r
1395 #else /* UIP_CONF_IPV6 */\r
1396   /* IPv4 header. */\r
1397   u8_t vhl,\r
1398     tos,\r
1399     len[2],\r
1400     ipid[2],\r
1401     ipoffset[2],\r
1402     ttl,\r
1403     proto;\r
1404   u16_t ipchksum;\r
1405   u16_t srcipaddr[2],\r
1406     destipaddr[2];\r
1407 #endif /* UIP_CONF_IPV6 */\r
1408   \r
1409   /* TCP header. */\r
1410   u16_t srcport,\r
1411     destport;\r
1412   u8_t seqno[4],\r
1413     ackno[4],\r
1414     tcpoffset,\r
1415     flags,\r
1416     wnd[2];\r
1417   u16_t tcpchksum;\r
1418   u8_t urgp[2];\r
1419   u8_t optdata[4];\r
1420 } PACK_STRUCT_END;\r
1421 \r
1422 /* The ICMP and IP headers. */\r
1423 struct uip_icmpip_hdr {\r
1424 #if UIP_CONF_IPV6\r
1425   /* IPv6 header. */\r
1426   u8_t vtc,\r
1427     tcf;\r
1428   u16_t flow;\r
1429   u8_t len[2];\r
1430   u8_t proto, ttl;\r
1431   uip_ip6addr_t srcipaddr, destipaddr;\r
1432 #else /* UIP_CONF_IPV6 */\r
1433   /* IPv4 header. */\r
1434   u8_t vhl,\r
1435     tos,\r
1436     len[2],\r
1437     ipid[2],\r
1438     ipoffset[2],\r
1439     ttl,\r
1440     proto;\r
1441   u16_t ipchksum;\r
1442   u16_t srcipaddr[2],\r
1443     destipaddr[2];\r
1444 #endif /* UIP_CONF_IPV6 */\r
1445   \r
1446   /* ICMP (echo) header. */\r
1447   u8_t type, icode;\r
1448   u16_t icmpchksum;\r
1449 #if !UIP_CONF_IPV6\r
1450   u16_t id, seqno;\r
1451 #else /* !UIP_CONF_IPV6 */\r
1452   u8_t flags, reserved1, reserved2, reserved3;\r
1453   u8_t icmp6data[16];\r
1454   u8_t options[1];\r
1455 #endif /* !UIP_CONF_IPV6 */\r
1456 } PACK_STRUCT_END;\r
1457 \r
1458 \r
1459 /* The UDP and IP headers. */\r
1460 struct uip_udpip_hdr {\r
1461 #if UIP_CONF_IPV6\r
1462   /* IPv6 header. */\r
1463   u8_t vtc,\r
1464     tcf;\r
1465   u16_t flow;\r
1466   u8_t len[2];\r
1467   u8_t proto, ttl;\r
1468   uip_ip6addr_t srcipaddr, destipaddr;\r
1469 #else /* UIP_CONF_IPV6 */\r
1470   /* IP header. */\r
1471   u8_t vhl,\r
1472     tos,\r
1473     len[2],\r
1474     ipid[2],\r
1475     ipoffset[2],\r
1476     ttl,\r
1477     proto;\r
1478   u16_t ipchksum;\r
1479   u16_t srcipaddr[2],\r
1480     destipaddr[2];\r
1481 #endif /* UIP_CONF_IPV6 */\r
1482   \r
1483   /* UDP header. */\r
1484   u16_t srcport,\r
1485     destport;\r
1486   u16_t udplen;\r
1487   u16_t udpchksum;\r
1488 } PACK_STRUCT_END;\r
1489 \r
1490 \r
1491 \r
1492 /**\r
1493  * The buffer size available for user data in the \ref uip_buf buffer.\r
1494  *\r
1495  * This macro holds the available size for user data in the \ref\r
1496  * uip_buf buffer. The macro is intended to be used for checking\r
1497  * bounds of available user data.\r
1498  *\r
1499  * Example:\r
1500  \code\r
1501  snprintf(uip_appdata, UIP_APPDATA_SIZE, "%u\n", i);\r
1502  \endcode\r
1503  *\r
1504  * \hideinitializer\r
1505  */\r
1506 #define UIP_APPDATA_SIZE (UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN)\r
1507 \r
1508 \r
1509 #define UIP_PROTO_ICMP  1\r
1510 #define UIP_PROTO_TCP   6\r
1511 #define UIP_PROTO_UDP   17\r
1512 #define UIP_PROTO_ICMP6 58\r
1513 \r
1514 /* Header sizes. */\r
1515 #if UIP_CONF_IPV6\r
1516 #define UIP_IPH_LEN    40\r
1517 #else /* UIP_CONF_IPV6 */\r
1518 #define UIP_IPH_LEN    20    /* Size of IP header */\r
1519 #endif /* UIP_CONF_IPV6 */\r
1520 #define UIP_UDPH_LEN    8    /* Size of UDP header */\r
1521 #define UIP_TCPH_LEN   20    /* Size of TCP header */\r
1522 #define UIP_IPUDPH_LEN (UIP_UDPH_LEN + UIP_IPH_LEN)    /* Size of IP +\r
1523                                                           UDP\r
1524                                                           header */\r
1525 #define UIP_IPTCPH_LEN (UIP_TCPH_LEN + UIP_IPH_LEN)    /* Size of IP +\r
1526                                                           TCP\r
1527                                                           header */\r
1528 #define UIP_TCPIP_HLEN UIP_IPTCPH_LEN\r
1529 \r
1530 \r
1531 #if UIP_FIXEDADDR\r
1532 extern const uip_ipaddr_t uip_hostaddr, uip_netmask, uip_draddr;\r
1533 #else /* UIP_FIXEDADDR */\r
1534 extern uip_ipaddr_t uip_hostaddr, uip_netmask, uip_draddr;\r
1535 #endif /* UIP_FIXEDADDR */\r
1536 \r
1537 \r
1538 \r
1539 /**\r
1540  * Representation of a 48-bit Ethernet address.\r
1541  */\r
1542 struct uip_eth_addr {\r
1543   u8_t addr[6];\r
1544 } PACK_STRUCT_END;\r
1545 \r
1546 /**\r
1547  * Calculate the Internet checksum over a buffer.\r
1548  *\r
1549  * The Internet checksum is the one's complement of the one's\r
1550  * complement sum of all 16-bit words in the buffer.\r
1551  *\r
1552  * See RFC1071.\r
1553  *\r
1554  * \param buf A pointer to the buffer over which the checksum is to be\r
1555  * computed.\r
1556  *\r
1557  * \param len The length of the buffer over which the checksum is to\r
1558  * be computed.\r
1559  *\r
1560  * \return The Internet checksum of the buffer.\r
1561  */\r
1562 u16_t uip_chksum(u16_t *buf, u16_t len);\r
1563 \r
1564 /**\r
1565  * Calculate the IP header checksum of the packet header in uip_buf.\r
1566  *\r
1567  * The IP header checksum is the Internet checksum of the 20 bytes of\r
1568  * the IP header.\r
1569  *\r
1570  * \return The IP header checksum of the IP header in the uip_buf\r
1571  * buffer.\r
1572  */\r
1573 u16_t uip_ipchksum(void);\r
1574 \r
1575 /**\r
1576  * Calculate the TCP checksum of the packet in uip_buf and uip_appdata.\r
1577  *\r
1578  * The TCP checksum is the Internet checksum of data contents of the\r
1579  * TCP segment, and a pseudo-header as defined in RFC793.\r
1580  *\r
1581  * \return The TCP checksum of the TCP segment in uip_buf and pointed\r
1582  * to by uip_appdata.\r
1583  */\r
1584 u16_t uip_tcpchksum(void);\r
1585 \r
1586 /**\r
1587  * Calculate the UDP checksum of the packet in uip_buf and uip_appdata.\r
1588  *\r
1589  * The UDP checksum is the Internet checksum of data contents of the\r
1590  * UDP segment, and a pseudo-header as defined in RFC768.\r
1591  *\r
1592  * \return The UDP checksum of the UDP segment in uip_buf and pointed\r
1593  * to by uip_appdata.\r
1594  */\r
1595 u16_t uip_udpchksum(void);\r
1596 \r
1597 \r
1598 #endif /* __UIP_H__ */\r
1599 \r
1600 \r
1601 /** @} */\r