Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


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