]> git.sur5r.net Git - freertos/blob - Demo/uIP_Demo_Rowley_ARM7/uip/uip.h
Renamed the CORTEX_M4_ATSAM4S_AVR_Studio directory to the correct CORTEX_M4_ATSAM4S_A...
[freertos] / Demo / uIP_Demo_Rowley_ARM7 / uip / uip.h
1 /**\r
2  * \addtogroup uip\r
3  * @{\r
4  */\r
5 \r
6 /**\r
7  * \file\r
8  * Header file for the uIP TCP/IP stack.\r
9  * \author Adam Dunkels <adam@dunkels.com>\r
10  *\r
11  * The uIP TCP/IP stack header file contains definitions for a number\r
12  * of C macros that are used by uIP programs as well as internal uIP\r
13  * structures, TCP/IP header structures and function declarations.\r
14  *\r
15  */\r
16 \r
17 \r
18 /*\r
19  * Copyright (c) 2001-2003, Adam Dunkels.\r
20  * All rights reserved. \r
21  *\r
22  * Redistribution and use in source and binary forms, with or without \r
23  * modification, are permitted provided that the following conditions \r
24  * are met: \r
25  * 1. Redistributions of source code must retain the above copyright \r
26  *    notice, this list of conditions and the following disclaimer. \r
27  * 2. Redistributions in binary form must reproduce the above copyright \r
28  *    notice, this list of conditions and the following disclaimer in the \r
29  *    documentation and/or other materials provided with the distribution. \r
30  * 3. The name of the author may not be used to endorse or promote\r
31  *    products derived from this software without specific prior\r
32  *    written permission.  \r
33  *\r
34  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS\r
35  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
36  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
37  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY\r
38  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
39  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE\r
40  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
41  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\r
42  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
43  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
44  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  \r
45  *\r
46  * This file is part of the uIP TCP/IP stack.\r
47  *\r
48  * $Id: uip.h,v 1.36.2.7 2003/10/07 13:47:51 adam Exp $\r
49  *\r
50  */\r
51 \r
52 #ifndef __UIP_H__\r
53 #define __UIP_H__\r
54 \r
55 #include "uipopt.h"\r
56 \r
57 /*-----------------------------------------------------------------------------------*/\r
58 /* First, the functions that should be called from the\r
59  * system. Initialization, the periodic timer and incoming packets are\r
60  * handled by the following three functions.\r
61  */\r
62 \r
63 /**\r
64  * \defgroup uipconffunc uIP configuration functions\r
65  * @{\r
66  *\r
67  * The uIP configuration functions are used for setting run-time\r
68  * parameters in uIP such as IP addresses. \r
69  */\r
70 \r
71 /**\r
72  * Set the IP address of this host.\r
73  *\r
74  * The IP address is represented as a 4-byte array where the first\r
75  * octet of the IP address is put in the first member of the 4-byte\r
76  * array.\r
77  *\r
78  * \param addr A pointer to a 4-byte representation of the IP address.\r
79  *\r
80  * \hideinitializer\r
81  */\r
82 #define uip_sethostaddr(addr) do { uip_hostaddr[0] = addr[0]; \\r
83                               uip_hostaddr[1] = addr[1]; } while(0)\r
84 \r
85 /**\r
86  * Get the IP address of this host.\r
87  *\r
88  * The IP address is represented as a 4-byte array where the first\r
89  * octet of the IP address is put in the first member of the 4-byte\r
90  * array.\r
91  *\r
92  * \param addr A pointer to a 4-byte array that will be filled in with\r
93  * the currently configured IP address.\r
94  *\r
95  * \hideinitializer\r
96  */\r
97 #define uip_gethostaddr(addr) do { addr[0] = uip_hostaddr[0]; \\r
98                               addr[1] = uip_hostaddr[1]; } while(0)\r
99 \r
100 /** @} */\r
101 \r
102 /**\r
103  * \defgroup uipinit uIP initialization functions\r
104  * @{\r
105  *\r
106  * The uIP initialization functions are used for booting uIP.\r
107  */\r
108 \r
109 /**\r
110  * uIP initialization function.\r
111  *\r
112  * This function should be called at boot up to initilize the uIP\r
113  * TCP/IP stack.\r
114  */\r
115 void uip_init(void);\r
116 \r
117 /** @} */\r
118 \r
119 /**\r
120  * \defgroup uipdevfunc uIP device driver functions\r
121  * @{\r
122  *\r
123  * These functions are used by a network device driver for interacting\r
124  * with uIP.\r
125  */\r
126 \r
127 /**\r
128  * Process an incoming packet.\r
129  *\r
130  * This function should be called when the device driver has received\r
131  * a packet from the network. The packet from the device driver must\r
132  * be present in the uip_buf buffer, and the length of the packet\r
133  * should be placed in the uip_len variable.\r
134  *\r
135  * When the function returns, there may be an outbound packet placed\r
136  * in the uip_buf packet buffer. If so, the uip_len variable is set to\r
137  * the length of the packet. If no packet is to be sent out, the\r
138  * uip_len variable is set to 0.\r
139  *\r
140  * The usual way of calling the function is presented by the source\r
141  * code below.\r
142  \code\r
143   uip_len = devicedriver_poll();\r
144   if(uip_len > 0) {\r
145     uip_input();\r
146     if(uip_len > 0) {\r
147       devicedriver_send();\r
148     }\r
149   }\r
150  \endcode\r
151  *\r
152  * \note If you are writing a uIP device driver that needs ARP\r
153  * (Address Resolution Protocol), e.g., when running uIP over\r
154  * Ethernet, you will need to call the uIP ARP code before calling\r
155  * this function:\r
156  \code\r
157   #define BUF ((struct uip_eth_hdr *)&uip_buf[0])\r
158   uip_len = ethernet_devicedrver_poll();\r
159   if(uip_len > 0) {\r
160     if(BUF->type == HTONS(UIP_ETHTYPE_IP)) {\r
161       uip_arp_ipin();\r
162       uip_input();\r
163       if(uip_len > 0) {\r
164         uip_arp_out();\r
165         ethernet_devicedriver_send();\r
166       }\r
167     } else if(BUF->type == HTONS(UIP_ETHTYPE_ARP)) {\r
168       uip_arp_arpin();\r
169       if(uip_len > 0) {\r
170         ethernet_devicedriver_send();\r
171       }\r
172     }\r
173  \endcode\r
174  *\r
175  * \hideinitializer\r
176  */\r
177 #define uip_input()        uip_process(UIP_DATA)\r
178 \r
179 /**\r
180  * Periodic processing for a connection identified by its number.\r
181  * \r
182  * This function does the necessary periodic processing (timers,\r
183  * polling) for a uIP TCP conneciton, and should be called when the\r
184  * periodic uIP timer goes off. It should be called for every\r
185  * connection, regardless of whether they are open of closed.\r
186  *\r
187  * When the function returns, it may have an outbound packet waiting\r
188  * for service in the uIP packet buffer, and if so the uip_len\r
189  * variable is set to a value larger than zero. The device driver\r
190  * should be called to send out the packet.\r
191  *\r
192  * The ususal way of calling the function is through a for() loop like\r
193  * this:\r
194  \code\r
195   for(i = 0; i < UIP_CONNS; ++i) {\r
196     uip_periodic(i);\r
197     if(uip_len > 0) {\r
198       devicedriver_send();\r
199     }\r
200   }\r
201  \endcode\r
202  *\r
203  * \note If you are writing a uIP device driver that needs ARP\r
204  * (Address Resolution Protocol), e.g., when running uIP over\r
205  * Ethernet, you will need to call the uip_arp_out() function before\r
206  * calling the device driver:\r
207  \code\r
208   for(i = 0; i < UIP_CONNS; ++i) {\r
209     uip_periodic(i);\r
210     if(uip_len > 0) {\r
211       uip_arp_out();\r
212       ethernet_devicedriver_send();\r
213     }\r
214   }\r
215  \endcode \r
216  *\r
217  * \param conn The number of the connection which is to be periodically polled.\r
218  *\r
219  * \hideinitializer\r
220  */\r
221 #define uip_periodic(conn) do { uip_conn = &uip_conns[conn]; \\r
222                                 uip_process(UIP_TIMER); } while (0)\r
223 \r
224 /**\r
225  * Periodic processing for a connection identified by a pointer to its structure.\r
226  *\r
227  * Same as uip_periodic() but takes a pointer to the actual uip_conn\r
228  * struct instead of an integer as its argument. This function can be\r
229  * used to force periodic processing of a specific connection.\r
230  *\r
231  * \param conn A pointer to the uip_conn struct for the connection to\r
232  * be processed.\r
233  *\r
234  * \hideinitializer\r
235  */\r
236 #define uip_periodic_conn(conn) do { uip_conn = conn; \\r
237                                      uip_process(UIP_TIMER); } while (0)\r
238 \r
239 #if UIP_UDP\r
240 /**\r
241  * Periodic processing for a UDP connection identified by its number.\r
242  *\r
243  * This function is essentially the same as uip_prerioic(), but for\r
244  * UDP connections. It is called in a similar fashion as the\r
245  * uip_periodic() function:\r
246  \code\r
247   for(i = 0; i < UIP_UDP_CONNS; i++) {\r
248     uip_udp_periodic(i);\r
249     if(uip_len > 0) {\r
250       devicedriver_send();\r
251     }\r
252   }   \r
253  \endcode\r
254  *\r
255  * \note As for the uip_periodic() function, special care has to be\r
256  * taken when using uIP together with ARP and Ethernet:\r
257  \code\r
258   for(i = 0; i < UIP_UDP_CONNS; i++) {\r
259     uip_udp_periodic(i);\r
260     if(uip_len > 0) {\r
261       uip_arp_out();\r
262       ethernet_devicedriver_send();\r
263     }\r
264   }   \r
265  \endcode\r
266  *\r
267  * \param conn The number of the UDP connection to be processed.\r
268  *\r
269  * \hideinitializer\r
270  */\r
271 #define uip_udp_periodic(conn) do { uip_udp_conn = &uip_udp_conns[conn]; \\r
272                                 uip_process(UIP_UDP_TIMER); } while (0)\r
273 \r
274 /**\r
275  * Periodic processing for a UDP connection identified by a pointer to\r
276  * its structure.\r
277  *\r
278  * Same as uip_udp_periodic() but takes a pointer to the actual\r
279  * uip_conn struct instead of an integer as its argument. This\r
280  * function can be used to force periodic processing of a specific\r
281  * connection.\r
282  *\r
283  * \param conn A pointer to the uip_udp_conn struct for the connection\r
284  * to be processed.\r
285  *\r
286  * \hideinitializer\r
287  */\r
288 #define uip_udp_periodic_conn(conn) do { uip_udp_conn = conn; \\r
289                                          uip_process(UIP_UDP_TIMER); } while (0)\r
290 \r
291 \r
292 #endif /* UIP_UDP */\r
293 \r
294 /**\r
295  * The uIP packet buffer.\r
296  *\r
297  * The uip_buf array is used to hold incoming and outgoing\r
298  * packets. The device driver should place incoming data into this\r
299  * buffer. When sending data, the device driver should read the link\r
300  * level headers and the TCP/IP headers from this buffer. The size of\r
301  * the link level headers is configured by the UIP_LLH_LEN define.\r
302  *\r
303  * \note The application data need not be placed in this buffer, so\r
304  * the device driver must read it from the place pointed to by the\r
305  * uip_appdata pointer as illustrated by the following example:\r
306  \code\r
307  void\r
308  devicedriver_send(void)\r
309  {\r
310     hwsend(&uip_buf[0], UIP_LLH_LEN);\r
311     hwsend(&uip_buf[UIP_LLH_LEN], 40);\r
312     hwsend(uip_appdata, uip_len - 40 - UIP_LLH_LEN);\r
313  }\r
314  \endcode\r
315  */\r
316 extern u8_t uip_buf[UIP_BUFSIZE+2] __attribute__ ((aligned (4)));\r
317 \r
318 /** @} */\r
319 \r
320 /*-----------------------------------------------------------------------------------*/\r
321 /* Functions that are used by the uIP application program. Opening and\r
322  * closing connections, sending and receiving data, etc. is all\r
323  * handled by the functions below.\r
324 */\r
325 /**\r
326  * \defgroup uipappfunc uIP application functions\r
327  * @{\r
328  *\r
329  * Functions used by an application running of top of uIP.\r
330  */\r
331 \r
332 /**\r
333  * Start listening to the specified port.\r
334  *\r
335  * \note Since this function expects the port number in network byte\r
336  * order, a conversion using HTONS() or htons() is necessary.\r
337  *\r
338  \code\r
339  uip_listen(HTONS(80)); \r
340  \endcode\r
341  *\r
342  * \param port A 16-bit port number in network byte order.\r
343  */\r
344 void uip_listen(u16_t port);\r
345 \r
346 /**\r
347  * Stop listening to the specified port.\r
348  *\r
349  * \note Since this function expects the port number in network byte\r
350  * order, a conversion using HTONS() or htons() is necessary.\r
351  *\r
352  \code\r
353  uip_unlisten(HTONS(80)); \r
354  \endcode\r
355  *\r
356  * \param port A 16-bit port number in network byte order.\r
357  */\r
358 void uip_unlisten(u16_t port);\r
359 \r
360 /**\r
361  * Connect to a remote host using TCP.\r
362  *\r
363  * This function is used to start a new connection to the specified\r
364  * port on the specied host. It allocates a new connection identifier,\r
365  * sets the connection to the SYN_SENT state and sets the\r
366  * retransmission timer to 0. This will cause a TCP SYN segment to be\r
367  * sent out the next time this connection is periodically processed,\r
368  * which usually is done within 0.5 seconds after the call to\r
369  * uip_connect().\r
370  *\r
371  * \note This function is avaliable only if support for active open\r
372  * has been configured by defining UIP_ACTIVE_OPEN to 1 in uipopt.h.\r
373  *\r
374  * \note Since this function requires the port number to be in network\r
375  * byte order, a convertion using HTONS() or htons() is necessary.\r
376  *\r
377  \code\r
378  u16_t ipaddr[2];\r
379 \r
380  uip_ipaddr(ipaddr, 192,168,1,2);\r
381  uip_connect(ipaddr, HTONS(80)); \r
382  \endcode\r
383  * \r
384  * \param ripaddr A pointer to a 4-byte array representing the IP\r
385  * address of the remote hot.\r
386  *\r
387  * \param port A 16-bit port number in network byte order.\r
388  *\r
389  * \return A pointer to the uIP connection identifier for the new connection,\r
390  * or NULL if no connection could be allocated.   \r
391  *\r
392  */\r
393 struct uip_conn *uip_connect(u16_t *ripaddr, u16_t port);\r
394 \r
395 \r
396 \r
397 /**\r
398  * \internal\r
399  *\r
400  * Check if a connection has outstanding (i.e., unacknowledged) data.\r
401  *\r
402  * \param conn A pointer to the uip_conn structure for the connection.\r
403  *\r
404  * \hideinitializer\r
405  */\r
406 #define uip_outstanding(conn) ((conn)->len)\r
407 \r
408 /**\r
409  * Send data on the current connection.\r
410  *\r
411  * This function is used to send out a single segment of TCP\r
412  * data. Only applications that have been invoked by uIP for event\r
413  * processing can send data. \r
414  *\r
415  * The amount of data that actually is sent out after a call to this\r
416  * funcion is determined by the maximum amount of data TCP allows. uIP\r
417  * will automatically crop the data so that only the appropriate\r
418  * amount of data is sent. The function uip_mss() can be used to query\r
419  * uIP for the amount of data that actually will be sent.\r
420  * \r
421  * \note This function does not guarantee that the sent data will\r
422  * arrive at the destination. If the data is lost in the network, the\r
423  * application will be invoked with the uip_rexmit() event being\r
424  * set. The application will then have to resend the data using this\r
425  * function.\r
426  * \r
427  * \param data A pointer to the data which is to be sent.\r
428  *\r
429  * \param len The maximum amount of data bytes to be sent.\r
430  *\r
431  * \hideinitializer\r
432  */\r
433 #define uip_send(data, len) do { uip_sappdata = (data); uip_slen = (len);} while(0)   \r
434 \r
435 /**\r
436  * The length of any incoming data that is currently avaliable (if avaliable)\r
437  * in the uip_appdata buffer.\r
438  *\r
439  * The test function uip_data() must first be used to check if there\r
440  * is any data available at all.\r
441  *\r
442  * \hideinitializer\r
443  */\r
444 #define uip_datalen()       uip_len\r
445 \r
446 /**\r
447  * The length of any out-of-band data (urgent data) that has arrived\r
448  * on the connection.\r
449  *\r
450  * \note The configuration parameter UIP_URGDATA must be set for this\r
451  * function to be enabled.\r
452  *\r
453  * \hideinitializer\r
454  */\r
455 #define uip_urgdatalen()    uip_urglen\r
456 \r
457 /**\r
458  * Close the current connection.\r
459  *\r
460  * This function will close the current connection in a nice way.\r
461  *\r
462  * \hideinitializer\r
463  */\r
464 #define uip_close()         (uip_flags = UIP_CLOSE)\r
465 \r
466 /**\r
467  * Abort the current connection.\r
468  *\r
469  * This function will abort (reset) the current connection, and is\r
470  * usually used when an error has occured that prevents using the\r
471  * uip_close() function.\r
472  *\r
473  * \hideinitializer\r
474  */\r
475 #define uip_abort()         (uip_flags = UIP_ABORT)\r
476 \r
477 /**\r
478  * Tell the sending host to stop sending data.\r
479  *\r
480  * This function will close our receiver's window so that we stop\r
481  * receiving data for the current connection.\r
482  *\r
483  * \hideinitializer\r
484  */\r
485 #define uip_stop()          (uip_conn->tcpstateflags |= UIP_STOPPED)\r
486 \r
487 /**\r
488  * Find out if the current connection has been previously stopped with\r
489  * uip_stop().\r
490  *\r
491  * \hideinitializer\r
492  */\r
493 #define uip_stopped(conn)   ((conn)->tcpstateflags & UIP_STOPPED)\r
494 \r
495 /**\r
496  * Restart the current connection, if is has previously been stopped\r
497  * with uip_stop().\r
498  *\r
499  * This function will open the receiver's window again so that we\r
500  * start receiving data for the current connection.\r
501  *\r
502  * \hideinitializer\r
503  */\r
504 #define uip_restart()         do { uip_flags |= UIP_NEWDATA; \\r
505                                    uip_conn->tcpstateflags &= ~UIP_STOPPED; \\r
506                               } while(0)\r
507 \r
508 \r
509 /* uIP tests that can be made to determine in what state the current\r
510    connection is, and what the application function should do. */\r
511 \r
512 /**\r
513  * Is new incoming data available?\r
514  *\r
515  * Will reduce to non-zero if there is new data for the application\r
516  * present at the uip_appdata pointer. The size of the data is\r
517  * avaliable through the uip_len variable.\r
518  *\r
519  * \hideinitializer\r
520  */\r
521 #define uip_newdata()   (uip_flags & UIP_NEWDATA)\r
522 \r
523 /**\r
524  * Has previously sent data been acknowledged?\r
525  *\r
526  * Will reduce to non-zero if the previously sent data has been\r
527  * acknowledged by the remote host. This means that the application\r
528  * can send new data. \r
529  *\r
530  * \hideinitializer\r
531  */\r
532 #define uip_acked()   (uip_flags & UIP_ACKDATA)\r
533 \r
534 /**\r
535  * Has the connection just been connected?  \r
536  *\r
537  * Reduces to non-zero if the current connection has been connected to\r
538  * a remote host. This will happen both if the connection has been\r
539  * actively opened (with uip_connect()) or passively opened (with\r
540  * uip_listen()).\r
541  *\r
542  * \hideinitializer\r
543  */\r
544 #define uip_connected() (uip_flags & UIP_CONNECTED)\r
545 \r
546 /**\r
547  * Has the connection been closed by the other end?\r
548  *\r
549  * Is non-zero if the connection has been closed by the remote\r
550  * host. The application may then do the necessary clean-ups.\r
551  *\r
552  * \hideinitializer\r
553  */\r
554 #define uip_closed()    (uip_flags & UIP_CLOSE)\r
555 \r
556 /**\r
557  * Has the connection been aborted by the other end?\r
558  *\r
559  * Non-zero if the current connection has been aborted (reset) by the\r
560  * remote host.\r
561  *\r
562  * \hideinitializer\r
563  */\r
564 #define uip_aborted()    (uip_flags & UIP_ABORT)\r
565 \r
566 /**\r
567  * Has the connection timed out?\r
568  *\r
569  * Non-zero if the current connection has been aborted due to too many\r
570  * retransmissions.\r
571  *\r
572  * \hideinitializer\r
573  */\r
574 #define uip_timedout()    (uip_flags & UIP_TIMEDOUT)\r
575 \r
576 /**\r
577  * Do we need to retransmit previously data?\r
578  *\r
579  * Reduces to non-zero if the previously sent data has been lost in\r
580  * the network, and the application should retransmit it. The\r
581  * application should send the exact same data as it did the last\r
582  * time, using the uip_send() function.\r
583  *\r
584  * \hideinitializer\r
585  */\r
586 #define uip_rexmit()     (uip_flags & UIP_REXMIT)\r
587 \r
588 /**\r
589  * Is the connection being polled by uIP?\r
590  *\r
591  * Is non-zero if the reason the application is invoked is that the\r
592  * current connection has been idle for a while and should be\r
593  * polled.\r
594  *\r
595  * The polling event can be used for sending data without having to\r
596  * wait for the remote host to send data.\r
597  *\r
598  * \hideinitializer\r
599  */ \r
600 #define uip_poll()       (uip_flags & UIP_POLL)\r
601 \r
602 /**\r
603  * Get the initial maxium segment size (MSS) of the current\r
604  * connection.\r
605  *\r
606  * \hideinitializer\r
607  */\r
608 #define uip_initialmss()             (uip_conn->initialmss)\r
609 \r
610 /**\r
611  * Get the current maxium segment size that can be sent on the current\r
612  * connection.\r
613  *\r
614  * The current maxiumum segment size that can be sent on the\r
615  * connection is computed from the receiver's window and the MSS of\r
616  * the connection (which also is available by calling\r
617  * uip_initialmss()).\r
618  *\r
619  * \hideinitializer\r
620  */\r
621 #define uip_mss()             (uip_conn->mss)\r
622 \r
623 /**\r
624  * Set up a new UDP connection.\r
625  *\r
626  * \param ripaddr A pointer to a 4-byte structure representing the IP\r
627  * address of the remote host.\r
628  *\r
629  * \param rport The remote port number in network byte order.\r
630  *\r
631  * \return The uip_udp_conn structure for the new connection or NULL\r
632  * if no connection could be allocated.\r
633  */\r
634 struct uip_udp_conn *uip_udp_new(u16_t *ripaddr, u16_t rport);\r
635 \r
636 /**\r
637  * Removed a UDP connection.\r
638  *\r
639  * \param conn A pointer to the uip_udp_conn structure for the connection.\r
640  *\r
641  * \hideinitializer\r
642  */\r
643 #define uip_udp_remove(conn) (conn)->lport = 0\r
644 \r
645 /**\r
646  * Send a UDP datagram of length len on the current connection.\r
647  *\r
648  * This function can only be called in response to a UDP event (poll\r
649  * or newdata). The data must be present in the uip_buf buffer, at the\r
650  * place pointed to by the uip_appdata pointer.\r
651  *\r
652  * \param len The length of the data in the uip_buf buffer.\r
653  *\r
654  * \hideinitializer\r
655  */\r
656 #define uip_udp_send(len) uip_slen = (len)\r
657 \r
658 /** @} */\r
659 \r
660 /* uIP convenience and converting functions. */\r
661 \r
662 /**\r
663  * \defgroup uipconvfunc uIP conversion functions\r
664  * @{\r
665  *\r
666  * These functions can be used for converting between different data\r
667  * formats used by uIP.\r
668  */\r
669  \r
670 /**\r
671  * Pack an IP address into a 4-byte array which is used by uIP to\r
672  * represent IP addresses.\r
673  *\r
674  * Example:\r
675  \code\r
676  u16_t ipaddr[2];\r
677 \r
678  uip_ipaddr(&ipaddr, 192,168,1,2); \r
679  \endcode\r
680  *\r
681  * \param addr A pointer to a 4-byte array that will be filled in with\r
682  * the IP addres.\r
683  * \param addr0 The first octet of the IP address.\r
684  * \param addr1 The second octet of the IP address.\r
685  * \param addr2 The third octet of the IP address.\r
686  * \param addr3 The forth octet of the IP address. \r
687  *\r
688  * \hideinitializer\r
689  */\r
690 #define uip_ipaddr(addr, addr0,addr1,addr2,addr3) do { \\r
691                      (addr)[0] = HTONS(((addr0) << 8) | (addr1)); \\r
692                      (addr)[1] = HTONS(((addr2) << 8) | (addr3)); \\r
693                   } while(0)\r
694 \r
695 /**\r
696  * Convert 16-bit quantity from host byte order to network byte order.\r
697  *\r
698  * This macro is primarily used for converting constants from host\r
699  * byte order to network byte order. For converting variables to\r
700  * network byte order, use the htons() function instead.\r
701  *\r
702  * \hideinitializer\r
703  */\r
704 #ifndef HTONS\r
705 #   if BYTE_ORDER == BIG_ENDIAN\r
706 #      define HTONS(n) (n)\r
707 #   else /* BYTE_ORDER == BIG_ENDIAN */\r
708 #      define HTONS(n) ((((u16_t)((n) & 0xff)) << 8) | (((n) & 0xff00) >> 8))\r
709 #   endif /* BYTE_ORDER == BIG_ENDIAN */\r
710 #endif /* HTONS */\r
711 \r
712 /**\r
713  * Convert 16-bit quantity from host byte order to network byte order.\r
714  *\r
715  * This function is primarily used for converting variables from host\r
716  * byte order to network byte order. For converting constants to\r
717  * network byte order, use the HTONS() macro instead.\r
718  */\r
719 #ifndef htons\r
720 u16_t htons(u16_t val);\r
721 #endif /* htons */\r
722 \r
723 /** @} */\r
724 \r
725 /**\r
726  * Pointer to the application data in the packet buffer.\r
727  *\r
728  * This pointer points to the application data when the application is\r
729  * called. If the application wishes to send data, the application may\r
730  * use this space to write the data into before calling uip_send().\r
731  */\r
732 extern volatile u8_t *uip_appdata;\r
733 extern volatile u8_t *uip_sappdata; \r
734 \r
735 #if UIP_URGDATA > 0 \r
736 /* u8_t *uip_urgdata:\r
737  *\r
738  * This pointer points to any urgent data that has been received. Only\r
739  * present if compiled with support for urgent data (UIP_URGDATA).\r
740  */\r
741 extern volatile u8_t *uip_urgdata; \r
742 #endif /* UIP_URGDATA > 0 */\r
743 \r
744 \r
745 /* u[8|16]_t uip_len:\r
746  *\r
747  * When the application is called, uip_len contains the length of any\r
748  * new data that has been received from the remote host. The\r
749  * application should set this variable to the size of any data that\r
750  * the application wishes to send. When the network device driver\r
751  * output function is called, uip_len should contain the length of the\r
752  * outgoing packet.\r
753  */\r
754 extern volatile u16_t uip_len, uip_slen;\r
755 \r
756 #if UIP_URGDATA > 0 \r
757 extern volatile u8_t uip_urglen, uip_surglen;\r
758 #endif /* UIP_URGDATA > 0 */\r
759 \r
760 \r
761 /**\r
762  * Representation of a uIP TCP connection.\r
763  *\r
764  * The uip_conn structure is used for identifying a connection. All\r
765  * but one field in the structure are to be considered read-only by an\r
766  * application. The only exception is the appstate field whos purpose\r
767  * is to let the application store application-specific state (e.g.,\r
768  * file pointers) for the connection. The size of this field is\r
769  * configured in the "uipopt.h" header file.\r
770  */\r
771 struct uip_conn {\r
772   u16_t ripaddr[2];   /**< The IP address of the remote host. */\r
773   \r
774   u16_t lport;        /**< The local TCP port, in network byte order. */\r
775   u16_t rport;        /**< The local remote TCP port, in network byte\r
776                          order. */  \r
777   \r
778   u8_t rcv_nxt[4];    /**< The sequence number that we expect to\r
779                          receive next. */\r
780   u8_t snd_nxt[4];    /**< The sequence number that was last sent by\r
781                          us. */\r
782   u16_t len;          /**< Length of the data that was previously sent. */\r
783   u16_t mss;          /**< Current maximum segment size for the\r
784                          connection. */\r
785   u16_t initialmss;   /**< Initial maximum segment size for the\r
786                          connection. */  \r
787   u8_t sa;            /**< Retransmission time-out calculation state\r
788                          variable. */\r
789   u8_t sv;            /**< Retransmission time-out calculation state\r
790                          variable. */\r
791   u8_t rto;           /**< Retransmission time-out. */\r
792   u8_t tcpstateflags; /**< TCP state and flags. */\r
793   u8_t timer;         /**< The retransmission timer. */\r
794   u8_t nrtx;          /**< The number of retransmissions for the last\r
795                          segment sent. */\r
796 \r
797   /** The application state. */\r
798   u8_t appstate[UIP_APPSTATE_SIZE];  \r
799 };\r
800 \r
801 \r
802 /* Pointer to the current connection. */\r
803 extern struct uip_conn *uip_conn;\r
804 /* The array containing all uIP connections. */\r
805 extern struct uip_conn uip_conns[UIP_CONNS];\r
806 /**\r
807  * \addtogroup uiparch\r
808  * @{\r
809  */\r
810 \r
811 /**\r
812  * 4-byte array used for the 32-bit sequence number calculations.\r
813  */\r
814 extern volatile u8_t uip_acc32[4];\r
815 \r
816 /** @} */\r
817 \r
818 \r
819 #if UIP_UDP\r
820 /**\r
821  * Representation of a uIP UDP connection.\r
822  */\r
823 struct uip_udp_conn {\r
824   u16_t ripaddr[2];   /**< The IP address of the remote peer. */\r
825   u16_t lport;        /**< The local port number in network byte order. */\r
826   u16_t rport;        /**< The remote port number in network byte order. */\r
827 };\r
828 \r
829 extern struct uip_udp_conn *uip_udp_conn;\r
830 extern struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];\r
831 #endif /* UIP_UDP */\r
832 \r
833 /**\r
834  * The structure holding the TCP/IP statistics that are gathered if\r
835  * UIP_STATISTICS is set to 1.\r
836  *\r
837  */\r
838 struct uip_stats {\r
839   struct {\r
840     uip_stats_t drop;     /**< Number of dropped packets at the IP\r
841                              layer. */\r
842     uip_stats_t recv;     /**< Number of received packets at the IP\r
843                              layer. */\r
844     uip_stats_t sent;     /**< Number of sent packets at the IP\r
845                              layer. */\r
846     uip_stats_t vhlerr;   /**< Number of packets dropped due to wrong\r
847                              IP version or header length. */\r
848     uip_stats_t hblenerr; /**< Number of packets dropped due to wrong\r
849                              IP length, high byte. */\r
850     uip_stats_t lblenerr; /**< Number of packets dropped due to wrong\r
851                              IP length, low byte. */\r
852     uip_stats_t fragerr;  /**< Number of packets dropped since they\r
853                              were IP fragments. */\r
854     uip_stats_t chkerr;   /**< Number of packets dropped due to IP\r
855                              checksum errors. */\r
856     uip_stats_t protoerr; /**< Number of packets dropped since they\r
857                              were neither ICMP, UDP nor TCP. */\r
858   } ip;                   /**< IP statistics. */\r
859   struct {\r
860     uip_stats_t drop;     /**< Number of dropped ICMP packets. */\r
861     uip_stats_t recv;     /**< Number of received ICMP packets. */\r
862     uip_stats_t sent;     /**< Number of sent ICMP packets. */\r
863     uip_stats_t typeerr;  /**< Number of ICMP packets with a wrong\r
864                              type. */\r
865   } icmp;                 /**< ICMP statistics. */\r
866   struct {\r
867     uip_stats_t drop;     /**< Number of dropped TCP segments. */\r
868     uip_stats_t recv;     /**< Number of recived TCP segments. */\r
869     uip_stats_t sent;     /**< Number of sent TCP segments. */\r
870     uip_stats_t chkerr;   /**< Number of TCP segments with a bad\r
871                              checksum. */\r
872     uip_stats_t ackerr;   /**< Number of TCP segments with a bad ACK\r
873                              number. */\r
874     uip_stats_t rst;      /**< Number of recevied TCP RST (reset) segments. */\r
875     uip_stats_t rexmit;   /**< Number of retransmitted TCP segments. */\r
876     uip_stats_t syndrop;  /**< Number of dropped SYNs due to too few\r
877                              connections was avaliable. */\r
878     uip_stats_t synrst;   /**< Number of SYNs for closed ports,\r
879                              triggering a RST. */\r
880   } tcp;                  /**< TCP statistics. */\r
881 };\r
882 \r
883 /**\r
884  * The uIP TCP/IP statistics.\r
885  *\r
886  * This is the variable in which the uIP TCP/IP statistics are gathered.\r
887  */\r
888 extern struct uip_stats uip_stat;\r
889 \r
890 \r
891 /*-----------------------------------------------------------------------------------*/\r
892 /* All the stuff below this point is internal to uIP and should not be\r
893  * used directly by an application or by a device driver.\r
894  */\r
895 /*-----------------------------------------------------------------------------------*/\r
896 /* u8_t uip_flags:\r
897  *\r
898  * When the application is called, uip_flags will contain the flags\r
899  * that are defined in this file. Please read below for more\r
900  * infomation.\r
901  */\r
902 extern volatile u8_t uip_flags;\r
903 \r
904 /* The following flags may be set in the global variable uip_flags\r
905    before calling the application callback. The UIP_ACKDATA and\r
906    UIP_NEWDATA flags may both be set at the same time, whereas the\r
907    others are mutualy exclusive. Note that these flags should *NOT* be\r
908    accessed directly, but through the uIP functions/macros. */\r
909 \r
910 #define UIP_ACKDATA   1     /* Signifies that the outstanding data was\r
911                                acked and the application should send\r
912                                out new data instead of retransmitting\r
913                                the last data. */\r
914 #define UIP_NEWDATA   2     /* Flags the fact that the peer has sent\r
915                                us new data. */\r
916 #define UIP_REXMIT    4     /* Tells the application to retransmit the\r
917                                data that was last sent. */\r
918 #define UIP_POLL      8     /* Used for polling the application, to\r
919                                check if the application has data that\r
920                                it wants to send. */\r
921 #define UIP_CLOSE     16    /* The remote host has closed the\r
922                                connection, thus the connection has\r
923                                gone away. Or the application signals\r
924                                that it wants to close the\r
925                                connection. */\r
926 #define UIP_ABORT     32    /* The remote host has aborted the\r
927                                connection, thus the connection has\r
928                                gone away. Or the application signals\r
929                                that it wants to abort the\r
930                                connection. */\r
931 #define UIP_CONNECTED 64    /* We have got a connection from a remote\r
932                                host and have set up a new connection\r
933                                for it, or an active connection has\r
934                                been successfully established. */\r
935 \r
936 #define UIP_TIMEDOUT  128   /* The connection has been aborted due to\r
937                                too many retransmissions. */\r
938 \r
939 \r
940 /* uip_process(flag):\r
941  *\r
942  * The actual uIP function which does all the work.\r
943  */\r
944 void uip_process(u8_t flag);\r
945 \r
946 /* The following flags are passed as an argument to the uip_process()\r
947    function. They are used to distinguish between the two cases where\r
948    uip_process() is called. It can be called either because we have\r
949    incoming data that should be processed, or because the periodic\r
950    timer has fired. */\r
951 \r
952 #define UIP_DATA    1     /* Tells uIP that there is incoming data in\r
953                              the uip_buf buffer. The length of the\r
954                              data is stored in the global variable\r
955                              uip_len. */\r
956 #define UIP_TIMER   2     /* Tells uIP that the periodic timer has\r
957                              fired. */\r
958 #if UIP_UDP\r
959 #define UIP_UDP_TIMER 3\r
960 #endif /* UIP_UDP */\r
961 \r
962 /* The TCP states used in the uip_conn->tcpstateflags. */\r
963 #define CLOSED      0\r
964 #define SYN_RCVD    1\r
965 #define SYN_SENT    2\r
966 #define ESTABLISHED 3\r
967 #define FIN_WAIT_1  4\r
968 #define FIN_WAIT_2  5\r
969 #define CLOSING     6\r
970 #define TIME_WAIT   7\r
971 #define LAST_ACK    8\r
972 #define TS_MASK     15\r
973   \r
974 #define UIP_STOPPED      16\r
975 \r
976 #define UIP_TCPIP_HLEN 40\r
977 \r
978 /* The TCP and IP headers. */\r
979 typedef struct {\r
980   /* IP header. */\r
981   u8_t vhl,\r
982     tos,          \r
983     len[2],       \r
984     ipid[2],        \r
985     ipoffset[2],  \r
986     ttl,          \r
987     proto;     \r
988   u16_t ipchksum;\r
989   u16_t srcipaddr[2], \r
990     destipaddr[2];\r
991   \r
992   /* TCP header. */\r
993   u16_t srcport,\r
994     destport;\r
995   u8_t seqno[4],  \r
996     ackno[4],\r
997     tcpoffset,\r
998     flags,\r
999     wnd[2];     \r
1000   u16_t tcpchksum;\r
1001   u8_t urgp[2];\r
1002   u8_t optdata[4];\r
1003 } uip_tcpip_hdr;\r
1004 \r
1005 /* The ICMP and IP headers. */\r
1006 typedef struct {\r
1007   /* IP header. */\r
1008   u8_t vhl,\r
1009     tos,          \r
1010     len[2],       \r
1011     ipid[2],        \r
1012     ipoffset[2],  \r
1013     ttl,          \r
1014     proto;     \r
1015   u16_t ipchksum;\r
1016   u16_t srcipaddr[2], \r
1017     destipaddr[2];\r
1018   /* ICMP (echo) header. */\r
1019   u8_t type, icode;\r
1020   u16_t icmpchksum;\r
1021   u16_t id, seqno;  \r
1022 } uip_icmpip_hdr;\r
1023 \r
1024 \r
1025 /* The UDP and IP headers. */\r
1026 typedef struct {\r
1027   /* IP header. */\r
1028   u8_t vhl,\r
1029     tos,          \r
1030     len[2],       \r
1031     ipid[2],        \r
1032     ipoffset[2],  \r
1033     ttl,          \r
1034     proto;     \r
1035   u16_t ipchksum;\r
1036   u16_t srcipaddr[2], \r
1037     destipaddr[2];\r
1038   \r
1039   /* UDP header. */\r
1040   u16_t srcport,\r
1041     destport;\r
1042   u16_t udplen;\r
1043   u16_t udpchksum;\r
1044 } uip_udpip_hdr;\r
1045 \r
1046 #define UIP_PROTO_ICMP  1\r
1047 #define UIP_PROTO_TCP   6\r
1048 #define UIP_PROTO_UDP   17\r
1049 \r
1050 #if UIP_FIXEDADDR\r
1051 extern const u16_t uip_hostaddr[2];\r
1052 #else /* UIP_FIXEDADDR */\r
1053 extern u16_t uip_hostaddr[2];\r
1054 #endif /* UIP_FIXEDADDR */\r
1055 \r
1056 #endif /* __UIP_H__ */\r
1057 \r
1058 \r
1059 /** @} */\r
1060 \r