Contiki-NG
uip.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2003, Adam Dunkels.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote
14  * products derived from this software without specific prior
15  * written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * This file is part of the uIP TCP/IP stack.
30  *
31  *
32  */
33 
34 /**
35  * \addtogroup uip
36  * @{
37  */
38 
39 /**
40  * \file
41  * Header file for the uIP TCP/IP stack.
42  * \author Adam Dunkels <adam@dunkels.com>
43  * \author Julien Abeille <jabeille@cisco.com> (IPv6 related code)
44  * \author Mathilde Durvy <mdurvy@cisco.com> (IPv6 related code)
45  *
46  * The uIP TCP/IP stack header file contains definitions for a number
47  * of C macros that are used by uIP programs as well as internal uIP
48  * structures, TCP/IP header structures and function declarations.
49  *
50  */
51 
52 #ifndef UIP_H_
53 #define UIP_H_
54 
55 /* Header sizes. */
56 #define UIP_IPH_LEN 40
57 #define UIP_FRAGH_LEN 8
58 
59 #define UIP_UDPH_LEN 8 /* Size of UDP header */
60 #define UIP_TCPH_LEN 20 /* Size of TCP header */
61 #define UIP_ICMPH_LEN 4 /* Size of ICMP header */
62 
63 #define UIP_IPUDPH_LEN (UIP_UDPH_LEN + UIP_IPH_LEN) /* Size of IP + UDP header */
64 #define UIP_IPTCPH_LEN (UIP_TCPH_LEN + UIP_IPH_LEN) /* Size of IP + TCP header */
65 
66 #define uip_l3_icmp_hdr_len (UIP_IPH_LEN + uip_ext_len + UIP_ICMPH_LEN)
67 
68 /**
69  * Direct access to IPv6 header
70  */
71 #define UIP_IP_BUF ((struct uip_ip_hdr *)uip_buf)
72 #define UIP_IP_PAYLOAD(ext) ((unsigned char *)uip_buf + UIP_IPH_LEN + (ext))
73 
74 /**
75  * Direct access to ICMP, UDP, and TCP headers and payload, with implicit ext header offset (global uip_ext_len)
76  */
77 #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)UIP_IP_PAYLOAD(uip_ext_len))
78 #define UIP_ICMP_PAYLOAD ((unsigned char *)UIP_IP_PAYLOAD(uip_ext_len) + UIP_ICMPH_LEN)
79 #define UIP_UDP_BUF ((struct uip_udp_hdr *)UIP_IP_PAYLOAD(uip_ext_len))
80 #define UIP_UDP_PAYLOAD ((unsigned char *)UIP_IP_PAYLOAD(uip_ext_len) + UIP_UDPH_LEN)
81 #define UIP_TCP_BUF ((struct uip_tcp_hdr *)UIP_IP_PAYLOAD(uip_ext_len))
82 #define UIP_TCP_PAYLOAD ((unsigned char *)UIP_IP_PAYLOAD(uip_ext_len) + UIP_TCPH_LEN)
83 
84 #include "net/ipv6/uipopt.h"
85 #include "net/ipv6/uipbuf.h"
86 #include "net/linkaddr.h"
87 
88 /* For memcmp */
89 #include <string.h>
90 
91 /**
92  * Representation of an IP address.
93  *
94  */
95 typedef union uip_ip4addr_t {
96  uint8_t u8[4]; /* Initializer, must come first. */
97  uint16_t u16[2];
99 
100 typedef union uip_ip6addr_t {
101  uint8_t u8[16]; /* Initializer, must come first. */
102  uint16_t u16[8];
103 } uip_ip6addr_t;
104 
105 typedef uip_ip6addr_t uip_ipaddr_t;
106 
107 /*---------------------------------------------------------------------------*/
108 #define UIP_802154_SHORTADDR_LEN 2
109 #define UIP_802154_LONGADDR_LEN 8
110 
111 /** \brief 16 bit 802.15.4 address */
112 typedef struct uip_802154_shortaddr {
113  uint8_t addr[UIP_802154_SHORTADDR_LEN];
115 /** \brief 64 bit 802.15.4 address */
116 typedef struct uip_802154_longaddr {
117  uint8_t addr[UIP_802154_LONGADDR_LEN];
119 
120 /** \brief 802.11 address */
121 typedef struct uip_80211_addr {
122  uint8_t addr[6];
124 
125 /** \brief 802.3 address */
126 typedef struct uip_eth_addr {
127  uint8_t addr[6];
128 } uip_eth_addr;
129 
130 
131 #ifndef UIP_CONF_LL_802154
132 #define UIP_CONF_LL_802154 1
133 #endif /* UIP_CONF_LL_802154 */
134 
135 #if UIP_CONF_LL_802154
136 /** \brief 802.15.4 address */
137 #if LINKADDR_SIZE == UIP_802154_LONGADDR_LEN
138 typedef uip_802154_longaddr uip_lladdr_t;
139 #elif LINKADDR_SIZE == UIP_802154_SHORTADDR_LEN
140 typedef uip_802154_shortaddr uip_lladdr_t;
141 #else /* LINKADDR_SIZE == 8 */
142 #error unsupported configuration of LINKADDR_SIZE
143 #endif /* LINKADDR_SIZE == 8 */
144 /** \brief Link layer address length */
145 #define UIP_LLADDR_LEN LINKADDR_SIZE
146 #else /*UIP_CONF_LL_802154*/
147 #if UIP_CONF_LL_80211
148 /** \brief 802.11 address */
149 typedef uip_80211_addr uip_lladdr_t;
150 /** \brief Link layer address length */
151 #define UIP_LLADDR_LEN 6
152 #else /*UIP_CONF_LL_80211*/
153 /** \brief Ethernet address */
154 typedef uip_eth_addr uip_lladdr_t;
155 /** \brief Link layer address length */
156 #define UIP_LLADDR_LEN 6
157 #endif /*UIP_CONF_LL_80211*/
158 #endif /*UIP_CONF_LL_802154*/
159 
160 #include "net/ipv6/tcpip.h"
161 
162 /*---------------------------------------------------------------------------*/
163 /* First, the functions that should be called from the
164  * system. Initialization, the periodic timer, and incoming packets are
165  * handled by the following three functions.
166  */
167 /**
168  * \defgroup uipconffunc uIP configuration functions
169  * @{
170  *
171  * The uIP configuration functions are used for setting run-time
172  * parameters in uIP such as IP addresses.
173  */
174 
175 /**
176  * Set the IP address of this host.
177  *
178  * The IP address is represented as a 4-byte array where the first
179  * octet of the IP address is put in the first member of the 4-byte
180  * array.
181  *
182  * Example:
183  \code
184 
185  uip_ipaddr_t addr;
186 
187  uip_ipaddr(&addr, 192,168,1,2);
188  uip_sethostaddr(&addr);
189 
190  \endcode
191  * \param addr A pointer to an IP address of type uip_ipaddr_t;
192  *
193  * \sa uip_ipaddr()
194  *
195  * \hideinitializer
196  */
197 #define uip_sethostaddr(addr) uip_ipaddr_copy(&uip_hostaddr, (addr))
198 
199 /**
200  * Get the IP address of this host.
201  *
202  * The IP address is represented as a 4-byte array where the first
203  * octet of the IP address is put in the first member of the 4-byte
204  * array.
205  *
206  * Example:
207  \code
208  uip_ipaddr_t hostaddr;
209 
210  uip_gethostaddr(&hostaddr);
211  \endcode
212  * \param addr A pointer to a uip_ipaddr_t variable that will be
213  * filled in with the currently configured IP address.
214  *
215  * \hideinitializer
216  */
217 #define uip_gethostaddr(addr) uip_ipaddr_copy((addr), &uip_hostaddr)
218 
219 /**
220  * Set the default router's IP address.
221  *
222  * \param addr A pointer to a uip_ipaddr_t variable containing the IP
223  * address of the default router.
224  *
225  * \sa uip_ipaddr()
226  *
227  * \hideinitializer
228  */
229 #define uip_setdraddr(addr) uip_ipaddr_copy(&uip_draddr, (addr))
230 
231 /**
232  * Set the netmask.
233  *
234  * \param addr A pointer to a uip_ipaddr_t variable containing the IP
235  * address of the netmask.
236  *
237  * \sa uip_ipaddr()
238  *
239  * \hideinitializer
240  */
241 #define uip_setnetmask(addr) uip_ipaddr_copy(&uip_netmask, (addr))
242 
243 
244 /**
245  * Get the default router's IP address.
246  *
247  * \param addr A pointer to a uip_ipaddr_t variable that will be
248  * filled in with the IP address of the default router.
249  *
250  * \hideinitializer
251  */
252 #define uip_getdraddr(addr) uip_ipaddr_copy((addr), &uip_draddr)
253 
254 /**
255  * Get the netmask.
256  *
257  * \param addr A pointer to a uip_ipaddr_t variable that will be
258  * filled in with the value of the netmask.
259  *
260  * \hideinitializer
261  */
262 #define uip_getnetmask(addr) uip_ipaddr_copy((addr), &uip_netmask)
263 
264 /** @} */
265 
266 /**
267  * \defgroup uipinit uIP initialization functions
268  * @{
269  *
270  * The uIP initialization functions are used for booting uIP.
271  */
272 
273 /**
274  * uIP initialization function.
275  *
276  * This function should be called at boot up to initilize the uIP
277  * TCP/IP stack.
278  */
279 void uip_init(void);
280 
281 /**
282  * uIP initialization function.
283  *
284  * This function may be used at boot time to set the initial ip_id.
285  */
286 void uip_setipid(uint16_t id);
287 
288 /** @} */
289 
290 /**
291  * \defgroup uipdevfunc uIP device driver functions
292  * @{
293  *
294  * These functions are used by a network device driver for interacting
295  * with uIP.
296  */
297 
298 /**
299  * Process an incoming packet.
300  *
301  * This function should be called when the device driver has received
302  * a packet from the network. The packet from the device driver must
303  * be present in the uip_buf buffer, and the length of the packet
304  * should be placed in the uip_len variable.
305  *
306  * When the function returns, there may be an outbound packet placed
307  * in the uip_buf packet buffer. If so, the uip_len variable is set to
308  * the length of the packet. If no packet is to be sent out, the
309  * uip_len variable is set to 0.
310  *
311  * The usual way of calling the function is presented by the source
312  * code below.
313  \code
314  uip_len = devicedriver_poll();
315  if(uip_len > 0) {
316  uip_input();
317  if(uip_len > 0) {
318  devicedriver_send();
319  }
320  }
321  \endcode
322  *
323  * \note If you are writing a uIP device driver that needs ARP
324  * (Address Resolution Protocol), e.g., when running uIP over
325  * Ethernet, you will need to call the uIP ARP code before calling
326  * this function:
327  \code
328  #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
329  uip_len = ethernet_devicedrver_poll();
330  if(uip_len > 0) {
331  if(BUF->type == UIP_HTONS(UIP_ETHTYPE_IP)) {
332  uip_arp_ipin();
333  uip_input();
334  if(uip_len > 0) {
335  uip_arp_out();
336  ethernet_devicedriver_send();
337  }
338  } else if(BUF->type == UIP_HTONS(UIP_ETHTYPE_ARP)) {
339  uip_arp_arpin();
340  if(uip_len > 0) {
341  ethernet_devicedriver_send();
342  }
343  }
344  \endcode
345  *
346  * \hideinitializer
347  */
348 #define uip_input() uip_process(UIP_DATA)
349 
350 
351 /**
352  * Periodic processing for a connection identified by its number.
353  *
354  * This function does the necessary periodic processing (timers,
355  * polling) for a uIP TCP connection, and should be called when the
356  * periodic uIP timer goes off. It should be called for every
357  * connection, regardless of whether they are open of closed.
358  *
359  * When the function returns, it may have an outbound packet waiting
360  * for service in the uIP packet buffer, and if so the uip_len
361  * variable is set to a value larger than zero. The device driver
362  * should be called to send out the packet.
363  *
364  * The usual way of calling the function is through a for() loop like
365  * this:
366  \code
367  for(i = 0; i < UIP_TCP_CONNS; ++i) {
368  uip_periodic(i);
369  if(uip_len > 0) {
370  devicedriver_send();
371  }
372  }
373  \endcode
374  *
375  * \note If you are writing a uIP device driver that needs ARP
376  * (Address Resolution Protocol), e.g., when running uIP over
377  * Ethernet, you will need to call the uip_arp_out() function before
378  * calling the device driver:
379  \code
380  for(i = 0; i < UIP_TCP_CONNS; ++i) {
381  uip_periodic(i);
382  if(uip_len > 0) {
383  uip_arp_out();
384  ethernet_devicedriver_send();
385  }
386  }
387  \endcode
388  *
389  * \param conn The number of the connection which is to be periodically polled.
390  *
391  * \hideinitializer
392  */
393 #if UIP_TCP
394 #define uip_periodic(conn) do { uip_conn = &uip_conns[conn]; \
395  uip_process(UIP_TIMER); } while (0)
396 
397 /**
398  * Macro to determine whether a specific uIP connection is active
399  *
400  * \param conn The connection's number
401  * \retval 0 Connection closed
402  */
403 #define uip_conn_active(conn) (uip_conns[conn].tcpstateflags != UIP_CLOSED)
404 
405 /**
406  * Perform periodic processing for a connection identified by a pointer
407  * to its structure.
408  *
409  * Same as uip_periodic() but takes a pointer to the actual uip_conn
410  * struct instead of an integer as its argument. This function can be
411  * used to force periodic processing of a specific connection.
412  *
413  * \param conn A pointer to the uip_conn struct for the connection to
414  * be processed.
415  *
416  * \hideinitializer
417  */
418 #define uip_periodic_conn(conn) do { uip_conn = conn; \
419  uip_process(UIP_TIMER); } while (0)
420 
421 /**
422  * Request that a particular connection should be polled.
423  *
424  * Similar to uip_periodic_conn() but does not perform any timer
425  * processing. The application is polled for new data.
426  *
427  * \param conn A pointer to the uip_conn struct for the connection to
428  * be processed.
429  *
430  * \hideinitializer
431  */
432 #define uip_poll_conn(conn) do { uip_conn = conn; \
433  uip_process(UIP_POLL_REQUEST); } while (0)
434 
435 #endif /* UIP_TCP */
436 
437 #if UIP_UDP
438 /**
439  * Periodic processing for a UDP connection identified by its number.
440  *
441  * This function is essentially the same as uip_periodic(), but for
442  * UDP connections. It is called in a similar fashion as the
443  * uip_periodic() function:
444  \code
445  for(i = 0; i < UIP_UDP_CONNS; i++) {
446  uip_udp_periodic(i);
447  if(uip_len > 0) {
448  devicedriver_send();
449  }
450  }
451  \endcode
452  *
453  * \note As for the uip_periodic() function, special care has to be
454  * taken when using uIP together with ARP and Ethernet:
455  \code
456  for(i = 0; i < UIP_UDP_CONNS; i++) {
457  uip_udp_periodic(i);
458  if(uip_len > 0) {
459  uip_arp_out();
460  ethernet_devicedriver_send();
461  }
462  }
463  \endcode
464  *
465  * \param conn The number of the UDP connection to be processed.
466  *
467  * \hideinitializer
468  */
469 #define uip_udp_periodic(conn) do { uip_udp_conn = &uip_udp_conns[conn]; \
470  uip_process(UIP_UDP_TIMER); } while(0)
471 
472 /**
473  * Periodic processing for a UDP connection identified by a pointer to
474  * its structure.
475  *
476  * Same as uip_udp_periodic() but takes a pointer to the actual
477  * uip_conn struct instead of an integer as its argument. This
478  * function can be used to force periodic processing of a specific
479  * connection.
480  *
481  * \param conn A pointer to the uip_udp_conn struct for the connection
482  * to be processed.
483  *
484  * \hideinitializer
485  */
486 #define uip_udp_periodic_conn(conn) do { uip_udp_conn = conn; \
487  uip_process(UIP_UDP_TIMER); } while(0)
488 #endif /* UIP_UDP */
489 
490 /** \brief Abandon the reassembly of the current packet */
491 void uip_reass_over(void);
492 
493 /**
494  * The uIP packet buffer.
495  *
496  * The uip_aligned_buf array is used to hold incoming and outgoing
497  * packets. The device driver should place incoming data into this
498  * buffer. When sending data, the device driver should read the
499  * outgoing data from this buffer.
500 */
501 
502 typedef union {
503  uint32_t u32[(UIP_BUFSIZE + 3) / 4];
504  uint8_t u8[UIP_BUFSIZE];
505 } uip_buf_t;
506 
508 
509 /** Macro to access uip_aligned_buf as an array of bytes */
510 #define uip_buf (uip_aligned_buf.u8)
511 
512 
513 /** @} */
514 
515 /*---------------------------------------------------------------------------*/
516 /* Functions that are used by the uIP application program. Opening and
517  * closing connections, sending and receiving data, etc. is all
518  * handled by the functions below.
519  */
520 /**
521  * \defgroup uipappfunc uIP application functions
522  * @{
523  *
524  * Functions used by an application running on top of uIP.
525  */
526 
527 /**
528  * Start listening to the specified port.
529  *
530  * \note Since this function expects the port number in network byte
531  * order, a conversion using UIP_HTONS() or uip_htons() is necessary.
532  *
533  \code
534  uip_listen(UIP_HTONS(80));
535  \endcode
536  *
537  * \param port A 16-bit port number in network byte order.
538  */
539 void uip_listen(uint16_t port);
540 
541 /**
542  * Stop listening to the specified port.
543  *
544  * \note Since this function expects the port number in network byte
545  * order, a conversion using UIP_HTONS() or uip_htons() is necessary.
546  *
547  \code
548  uip_unlisten(UIP_HTONS(80));
549  \endcode
550  *
551  * \param port A 16-bit port number in network byte order.
552  */
553 void uip_unlisten(uint16_t port);
554 
555 /**
556  * Connect to a remote host using TCP.
557  *
558  * This function is used to start a new connection to the specified
559  * port on the specified host. It allocates a new connection identifier,
560  * sets the connection to the SYN_SENT state and sets the
561  * retransmission timer to 0. This will cause a TCP SYN segment to be
562  * sent out the next time this connection is periodically processed,
563  * which usually is done within 0.5 seconds after the call to
564  * uip_connect().
565  *
566  * \note This function is available only if support for active open
567  * has been configured by defining UIP_ACTIVE_OPEN to 1 in uipopt.h.
568  *
569  * \note Since this function requires the port number to be in network
570  * byte order, a conversion using UIP_HTONS() or uip_htons() is necessary.
571  *
572  \code
573  uip_ipaddr_t ipaddr;
574 
575  uip_ipaddr(&ipaddr, 192,168,1,2);
576  uip_connect(&ipaddr, UIP_HTONS(80));
577  \endcode
578  *
579  * \param ripaddr The IP address of the remote host.
580  *
581  * \param port A 16-bit port number in network byte order.
582  *
583  * \return A pointer to the uIP connection identifier for the new connection,
584  * or NULL if no connection could be allocated.
585  *
586  */
587 struct uip_conn *uip_connect(const uip_ipaddr_t *ripaddr, uint16_t port);
588 
589 
590 
591 /**
592  * \internal
593  *
594  * Check if a connection has outstanding (i.e., unacknowledged) data.
595  *
596  * \param conn A pointer to the uip_conn structure for the connection.
597  *
598  * \hideinitializer
599  */
600 #define uip_outstanding(conn) ((conn)->len)
601 
602 /**
603  * Send data on the current connection.
604  *
605  * This function is used to send out a single segment of TCP
606  * data. Only applications that have been invoked by uIP for event
607  * processing can send data.
608  *
609  * The amount of data that actually is sent out after a call to this
610  * function is determined by the maximum amount of data TCP allows. uIP
611  * will automatically crop the data so that only the appropriate
612  * amount of data is sent. The function uip_mss() can be used to query
613  * uIP for the amount of data that actually will be sent.
614  *
615  * \note This function does not guarantee that the sent data will
616  * arrive at the destination. If the data is lost in the network, the
617  * application will be invoked with the uip_rexmit() event being
618  * set. The application will then have to resend the data using this
619  * function.
620  *
621  * \param data A pointer to the data which is to be sent.
622  *
623  * \param len The maximum amount of data bytes to be sent.
624  *
625  * \hideinitializer
626  */
627 void uip_send(const void *data, int len);
628 
629 /**
630  * The length of any incoming data that is currently available (if available)
631  * in the uip_appdata buffer.
632  *
633  * The test function uip_data() must first be used to check if there
634  * is any data available at all.
635  *
636  * \hideinitializer
637  */
638 /*void uip_datalen(void);*/
639 #define uip_datalen() uip_len
640 
641 /**
642  * The length of any out-of-band data (urgent data) that has arrived
643  * on the connection.
644  *
645  * \note The configuration parameter UIP_URGDATA must be set for this
646  * function to be enabled.
647  *
648  * \hideinitializer
649  */
650 #define uip_urgdatalen() uip_urglen
651 
652 /**
653  * Close the current connection.
654  *
655  * This function will close the current connection in a nice way.
656  *
657  * \hideinitializer
658  */
659 #define uip_close() (uip_flags = UIP_CLOSE)
660 
661 /**
662  * Abort the current connection.
663  *
664  * This function will abort (reset) the current connection, and is
665  * usually used when an error has occurred that prevents using the
666  * uip_close() function.
667  *
668  * \hideinitializer
669  */
670 #define uip_abort() (uip_flags = UIP_ABORT)
671 
672 /**
673  * Tell the sending host to stop sending data.
674  *
675  * This function will close our receiver's window so that we stop
676  * receiving data for the current connection.
677  *
678  * \hideinitializer
679  */
680 #define uip_stop() (uip_conn->tcpstateflags |= UIP_STOPPED)
681 
682 /**
683  * Find out if the current connection has been previously stopped with
684  * uip_stop().
685  *
686  * \hideinitializer
687  */
688 #define uip_stopped(conn) ((conn)->tcpstateflags & UIP_STOPPED)
689 
690 /**
691  * Restart the current connection, if is has previously been stopped
692  * with uip_stop().
693  *
694  * This function will open the receiver's window again so that we
695  * start receiving data for the current connection.
696  *
697  * \hideinitializer
698  */
699 #define uip_restart() do { uip_flags |= UIP_NEWDATA; \
700  uip_conn->tcpstateflags &= ~UIP_STOPPED; \
701  } while(0)
702 
703 
704 /* uIP tests that can be made to determine in what state the current
705  connection is, and what the application function should do. */
706 
707 /**
708  * Is the current connection a UDP connection?
709  *
710  * This function checks whether the current connection is a UDP connection.
711  *
712  * \hideinitializer
713  *
714  */
715 #define uip_udpconnection() (uip_conn == NULL)
716 
717 /**
718  * Is new incoming data available?
719  *
720  * Will reduce to non-zero if there is new data for the application
721  * present at the uip_appdata pointer. The size of the data is
722  * available through the uip_len variable.
723  *
724  * \hideinitializer
725  */
726 #define uip_newdata() (uip_flags & UIP_NEWDATA)
727 
728 /**
729  * Has previously sent data been acknowledged?
730  *
731  * Will reduce to non-zero if the previously sent data has been
732  * acknowledged by the remote host. This means that the application
733  * can send new data.
734  *
735  * \hideinitializer
736  */
737 #define uip_acked() (uip_flags & UIP_ACKDATA)
738 
739 /**
740  * Has the connection just been connected?
741  *
742  * Reduces to non-zero if the current connection has been connected to
743  * a remote host. This will happen both if the connection has been
744  * actively opened (with uip_connect()) or passively opened (with
745  * uip_listen()).
746  *
747  * \hideinitializer
748  */
749 #define uip_connected() (uip_flags & UIP_CONNECTED)
750 
751 /**
752  * Has the connection been closed by the other end?
753  *
754  * Is non-zero if the connection has been closed by the remote
755  * host. The application may then do the necessary clean-ups.
756  *
757  * \hideinitializer
758  */
759 #define uip_closed() (uip_flags & UIP_CLOSE)
760 
761 /**
762  * Has the connection been aborted by the other end?
763  *
764  * Non-zero if the current connection has been aborted (reset) by the
765  * remote host.
766  *
767  * \hideinitializer
768  */
769 #define uip_aborted() (uip_flags & UIP_ABORT)
770 
771 /**
772  * Has the connection timed out?
773  *
774  * Non-zero if the current connection has been aborted due to too many
775  * retransmissions.
776  *
777  * \hideinitializer
778  */
779 #define uip_timedout() (uip_flags & UIP_TIMEDOUT)
780 
781 /**
782  * Do we need to retransmit previously data?
783  *
784  * Reduces to non-zero if the previously sent data has been lost in
785  * the network, and the application should retransmit it. The
786  * application should send the exact same data as it did the last
787  * time, using the uip_send() function.
788  *
789  * \hideinitializer
790  */
791 #define uip_rexmit() (uip_flags & UIP_REXMIT)
792 
793 /**
794  * Is the connection being polled by uIP?
795  *
796  * Is non-zero if the reason the application is invoked is that the
797  * current connection has been idle for a while and should be
798  * polled.
799  *
800  * The polling event can be used for sending data without having to
801  * wait for the remote host to send data.
802  *
803  * \hideinitializer
804  */
805 #define uip_poll() (uip_flags & UIP_POLL)
806 
807 /**
808  * Get the initial maximum segment size (MSS) of the current
809  * connection.
810  *
811  * \hideinitializer
812  */
813 #define uip_initialmss() (uip_conn->initialmss)
814 
815 /**
816  * Get the current maximum segment size that can be sent on the current
817  * connection.
818  *
819  * The current maximum segment size that can be sent on the
820  * connection is computed from the receiver's window and the MSS of
821  * the connection (which also is available by calling
822  * uip_initialmss()).
823  *
824  * \hideinitializer
825  */
826 #define uip_mss() (uip_conn->mss)
827 
828 /**
829  * Set up a new UDP connection.
830  *
831  * This function sets up a new UDP connection. The function will
832  * automatically allocate an unused local port for the new
833  * connection. However, another port can be chosen by using the
834  * uip_udp_bind() call, after the uip_udp_new() function has been
835  * called.
836  *
837  * Example:
838  \code
839  uip_ipaddr_t addr;
840  struct uip_udp_conn *c;
841 
842  uip_ipaddr(&addr, 192,168,2,1);
843  c = uip_udp_new(&addr, UIP_HTONS(12345));
844  if(c != NULL) {
845  uip_udp_bind(c, UIP_HTONS(12344));
846  }
847  \endcode
848  * \param ripaddr The IP address of the remote host.
849  *
850  * \param rport The remote port number in network byte order.
851  *
852  * \return The uip_udp_conn structure for the new connection, or NULL
853  * if no connection could be allocated.
854  */
855 struct uip_udp_conn *uip_udp_new(const uip_ipaddr_t *ripaddr, uint16_t rport);
856 
857 /**
858  * Remove a UDP connection.
859  *
860  * \param conn A pointer to the uip_udp_conn structure for the connection.
861  *
862  * \hideinitializer
863  */
864 #define uip_udp_remove(conn) (conn)->lport = 0
865 
866 /**
867  * Bind a UDP connection to a local port.
868  *
869  * \param conn A pointer to the uip_udp_conn structure for the
870  * connection.
871  *
872  * \param port The local port number, in network byte order.
873  *
874  * \hideinitializer
875  */
876 #define uip_udp_bind(conn, port) (conn)->lport = port
877 
878 /**
879  * Send a UDP datagram of length len on the current connection.
880  *
881  * This function can only be called in response to a UDP event (poll
882  * or newdata). The data must be present in the uip_buf buffer, at the
883  * place pointed to by the uip_appdata pointer.
884  *
885  * \param len The length of the data in the uip_buf buffer.
886  *
887  * \hideinitializer
888  */
889 #define uip_udp_send(len) uip_send((char *)uip_appdata, len)
890 
891 
892 /** @} */
893 
894 /* uIP convenience and converting functions. */
895 
896 /**
897  * \defgroup uipconvfunc uIP conversion functions
898  * @{
899  *
900  * These functions can be used for converting between different data
901  * formats used by uIP.
902  */
903 
904 /**
905  * Convert an IP address to four bytes separated by commas.
906  *
907  * Example:
908  \code
909  uip_ipaddr_t ipaddr;
910  printf("ipaddr=%d.%d.%d.%d\n", uip_ipaddr_to_quad(&ipaddr));
911  \endcode
912  *
913  * \param a A pointer to a uip_ipaddr_t.
914  * \hideinitializer
915  */
916 #define uip_ipaddr_to_quad(a) (a)->u8[0],(a)->u8[1],(a)->u8[2],(a)->u8[3]
917 
918 /**
919  * Construct an IP address from four bytes.
920  *
921  * This function constructs an IP address of the type that uIP handles
922  * internally from four bytes. The function is handy for specifying IP
923  * addresses to use with e.g. the uip_connect() function.
924  *
925  * Example:
926  \code
927  uip_ipaddr_t ipaddr;
928  struct uip_conn *c;
929 
930  uip_ipaddr(&ipaddr, 192,168,1,2);
931  c = uip_connect(&ipaddr, UIP_HTONS(80));
932  \endcode
933  *
934  * \param addr A pointer to a uip_ipaddr_t variable that will be
935  * filled in with the IP address.
936  *
937  * \param addr0 The first octet of the IP address.
938  * \param addr1 The second octet of the IP address.
939  * \param addr2 The third octet of the IP address.
940  * \param addr3 The forth octet of the IP address.
941  *
942  * \hideinitializer
943  */
944 #define uip_ipaddr(addr, addr0,addr1,addr2,addr3) do { \
945  (addr)->u8[0] = addr0; \
946  (addr)->u8[1] = addr1; \
947  (addr)->u8[2] = addr2; \
948  (addr)->u8[3] = addr3; \
949  } while(0)
950 
951 /**
952  * Construct an IPv6 address from eight 16-bit words.
953  *
954  * This function constructs an IPv6 address.
955  *
956  * \hideinitializer
957  */
958 #define uip_ip6addr(addr, addr0,addr1,addr2,addr3,addr4,addr5,addr6,addr7) do { \
959  (addr)->u16[0] = UIP_HTONS(addr0); \
960  (addr)->u16[1] = UIP_HTONS(addr1); \
961  (addr)->u16[2] = UIP_HTONS(addr2); \
962  (addr)->u16[3] = UIP_HTONS(addr3); \
963  (addr)->u16[4] = UIP_HTONS(addr4); \
964  (addr)->u16[5] = UIP_HTONS(addr5); \
965  (addr)->u16[6] = UIP_HTONS(addr6); \
966  (addr)->u16[7] = UIP_HTONS(addr7); \
967  } while(0)
968 
969 /**
970  * Construct an IPv6 address from sixteen 8-bit words.
971  *
972  * This function constructs an IPv6 address.
973  *
974  * \hideinitializer
975  */
976 #define uip_ip6addr_u8(addr, addr0,addr1,addr2,addr3,addr4,addr5,addr6,addr7,addr8,addr9,addr10,addr11,addr12,addr13,addr14,addr15) do { \
977  (addr)->u8[0] = addr0; \
978  (addr)->u8[1] = addr1; \
979  (addr)->u8[2] = addr2; \
980  (addr)->u8[3] = addr3; \
981  (addr)->u8[4] = addr4; \
982  (addr)->u8[5] = addr5; \
983  (addr)->u8[6] = addr6; \
984  (addr)->u8[7] = addr7; \
985  (addr)->u8[8] = addr8; \
986  (addr)->u8[9] = addr9; \
987  (addr)->u8[10] = addr10; \
988  (addr)->u8[11] = addr11; \
989  (addr)->u8[12] = addr12; \
990  (addr)->u8[13] = addr13; \
991  (addr)->u8[14] = addr14; \
992  (addr)->u8[15] = addr15; \
993  } while(0)
994 
995 
996 /**
997  * Copy an IP address from one place to another.
998  *
999  * Copies an IP address from one place to another.
1000  *
1001  * Example:
1002  \code
1003  uip_ipaddr_t ipaddr1, ipaddr2;
1004 
1005  uip_ipaddr(&ipaddr1, 192,16,1,2);
1006  uip_ipaddr_copy(&ipaddr2, &ipaddr1);
1007  \endcode
1008  *
1009  * \param dest The destination for the copy.
1010  * \param src The source from where to copy.
1011  *
1012  * \hideinitializer
1013  */
1014 #ifndef uip_ipaddr_copy
1015 #define uip_ipaddr_copy(dest, src) (*(dest) = *(src))
1016 #endif
1017 #ifndef uip_ip4addr_copy
1018 #define uip_ip4addr_copy(dest, src) (*((uip_ip4addr_t *)dest) = *((uip_ip4addr_t *)src))
1019 #endif
1020 #ifndef uip_ip6addr_copy
1021 #define uip_ip6addr_copy(dest, src) (*((uip_ip6addr_t *)dest) = *((uip_ip6addr_t *)src))
1022 #endif
1023 
1024 /**
1025  * Compare two IP addresses
1026  *
1027  * Compares two IP addresses.
1028  *
1029  * Example:
1030  \code
1031  uip_ipaddr_t ipaddr1, ipaddr2;
1032 
1033  uip_ipaddr(&ipaddr1, 192,16,1,2);
1034  if(uip_ipaddr_cmp(&ipaddr2, &ipaddr1)) {
1035  printf("They are the same");
1036  }
1037  \endcode
1038  *
1039  * \param addr1 The first IP address.
1040  * \param addr2 The second IP address.
1041  *
1042  * \hideinitializer
1043  */
1044 #define uip_ip4addr_cmp(addr1, addr2) ((addr1)->u16[0] == (addr2)->u16[0] && \
1045  (addr1)->u16[1] == (addr2)->u16[1])
1046 #define uip_ip6addr_cmp(addr1, addr2) (memcmp(addr1, addr2, sizeof(uip_ip6addr_t)) == 0)
1047 
1048 #define uip_ipaddr_cmp(addr1, addr2) uip_ip6addr_cmp(addr1, addr2)
1049 
1050 /**
1051  * Compare two IP addresses with netmasks
1052  *
1053  * Compares two IP addresses with netmasks. The masks are used to mask
1054  * out the bits that are to be compared.
1055  *
1056  * Example:
1057  \code
1058  uip_ipaddr_t ipaddr1, ipaddr2, mask;
1059 
1060  uip_ipaddr(&mask, 255,255,255,0);
1061  uip_ipaddr(&ipaddr1, 192,16,1,2);
1062  uip_ipaddr(&ipaddr2, 192,16,1,3);
1063  if(uip_ipaddr_maskcmp(&ipaddr1, &ipaddr2, &mask)) {
1064  printf("They are the same");
1065  }
1066  \endcode
1067  *
1068  * \param addr1 The first IP address.
1069  * \param addr2 The second IP address.
1070  * \param mask The netmask.
1071  *
1072  * \hideinitializer
1073  */
1074 
1075 #define uip_ipaddr_maskcmp(addr1, addr2, mask) \
1076  (((((uint16_t *)addr1)[0] & ((uint16_t *)mask)[0]) == \
1077  (((uint16_t *)addr2)[0] & ((uint16_t *)mask)[0])) && \
1078  ((((uint16_t *)addr1)[1] & ((uint16_t *)mask)[1]) == \
1079  (((uint16_t *)addr2)[1] & ((uint16_t *)mask)[1])))
1080 
1081 #define uip_ipaddr_prefixcmp(addr1, addr2, length) (memcmp(addr1, addr2, length>>3) == 0)
1082 
1083 
1084 
1085 /*
1086  * Check if an address is a broadcast address for a network.
1087  *
1088  * Checks if an address is the broadcast address for a network. The
1089  * network is defined by an IP address that is on the network and the
1090  * network's netmask.
1091  *
1092  * \param addr The IP address.
1093  * \param netaddr The network's IP address.
1094  * \param netmask The network's netmask.
1095  *
1096  * \hideinitializer
1097  */
1098 /*#define uip_ipaddr_isbroadcast(addr, netaddr, netmask)
1099  ((uip_ipaddr_t *)(addr)).u16 & ((uip_ipaddr_t *)(addr)).u16*/
1100 
1101 
1102 
1103 /**
1104  * Mask out the network part of an IP address.
1105  *
1106  * Masks out the network part of an IP address, given the address and
1107  * the netmask.
1108  *
1109  * Example:
1110  \code
1111  uip_ipaddr_t ipaddr1, ipaddr2, netmask;
1112 
1113  uip_ipaddr(&ipaddr1, 192,16,1,2);
1114  uip_ipaddr(&netmask, 255,255,255,0);
1115  uip_ipaddr_mask(&ipaddr2, &ipaddr1, &netmask);
1116  \endcode
1117  *
1118  * In the example above, the variable "ipaddr2" will contain the IP
1119  * address 192.168.1.0.
1120  *
1121  * \param dest Where the result is to be placed.
1122  * \param src The IP address.
1123  * \param mask The netmask.
1124  *
1125  * \hideinitializer
1126  */
1127 #define uip_ipaddr_mask(dest, src, mask) do { \
1128  ((uint16_t *)dest)[0] = ((uint16_t *)src)[0] & ((uint16_t *)mask)[0]; \
1129  ((uint16_t *)dest)[1] = ((uint16_t *)src)[1] & ((uint16_t *)mask)[1]; \
1130  } while(0)
1131 
1132 /**
1133  * Pick the first octet of an IP address.
1134  *
1135  * Picks out the first octet of an IP address.
1136  *
1137  * Example:
1138  \code
1139  uip_ipaddr_t ipaddr;
1140  uint8_t octet;
1141 
1142  uip_ipaddr(&ipaddr, 1,2,3,4);
1143  octet = uip_ipaddr1(&ipaddr);
1144  \endcode
1145  *
1146  * In the example above, the variable "octet" will contain the value 1.
1147  *
1148  * \hideinitializer
1149  */
1150 #define uip_ipaddr1(addr) ((addr)->u8[0])
1151 
1152 /**
1153  * Pick the second octet of an IP address.
1154  *
1155  * Picks out the second octet of an IP address.
1156  *
1157  * Example:
1158  \code
1159  uip_ipaddr_t ipaddr;
1160  uint8_t octet;
1161 
1162  uip_ipaddr(&ipaddr, 1,2,3,4);
1163  octet = uip_ipaddr2(&ipaddr);
1164  \endcode
1165  *
1166  * In the example above, the variable "octet" will contain the value 2.
1167  *
1168  * \hideinitializer
1169  */
1170 #define uip_ipaddr2(addr) ((addr)->u8[1])
1171 
1172 /**
1173  * Pick the third octet of an IP address.
1174  *
1175  * Picks out the third octet of an IP address.
1176  *
1177  * Example:
1178  \code
1179  uip_ipaddr_t ipaddr;
1180  uint8_t octet;
1181 
1182  uip_ipaddr(&ipaddr, 1,2,3,4);
1183  octet = uip_ipaddr3(&ipaddr);
1184  \endcode
1185  *
1186  * In the example above, the variable "octet" will contain the value 3.
1187  *
1188  * \hideinitializer
1189  */
1190 #define uip_ipaddr3(addr) ((addr)->u8[2])
1191 
1192 /**
1193  * Pick the fourth octet of an IP address.
1194  *
1195  * Picks out the fourth octet of an IP address.
1196  *
1197  * Example:
1198  \code
1199  uip_ipaddr_t ipaddr;
1200  uint8_t octet;
1201 
1202  uip_ipaddr(&ipaddr, 1,2,3,4);
1203  octet = uip_ipaddr4(&ipaddr);
1204  \endcode
1205  *
1206  * In the example above, the variable "octet" will contain the value 4.
1207  *
1208  * \hideinitializer
1209  */
1210 #define uip_ipaddr4(addr) ((addr)->u8[3])
1211 
1212 /**
1213  * Convert 16-bit quantity from host byte order to network byte order.
1214  *
1215  * This macro is primarily used for converting constants from host
1216  * byte order to network byte order. For converting variables to
1217  * network byte order, use the uip_htons() function instead.
1218  *
1219  * \hideinitializer
1220  */
1221 #ifndef UIP_HTONS
1222 # if UIP_BYTE_ORDER == UIP_BIG_ENDIAN
1223 # define UIP_HTONS(n) (n)
1224 # define UIP_HTONL(n) (n)
1225 # else /* UIP_BYTE_ORDER == UIP_BIG_ENDIAN */
1226 # define UIP_HTONS(n) (uint16_t)((((uint16_t) (n)) << 8) | (((uint16_t) (n)) >> 8))
1227 # define UIP_HTONL(n) (((uint32_t)UIP_HTONS(n) << 16) | UIP_HTONS((uint32_t)(n) >> 16))
1228 # endif /* UIP_BYTE_ORDER == UIP_BIG_ENDIAN */
1229 #else
1230 #error "UIP_HTONS already defined!"
1231 #endif /* UIP_HTONS */
1232 
1233 /**
1234  * Convert a 16-bit quantity from host byte order to network byte order.
1235  *
1236  * This function is primarily used for converting variables from host
1237  * byte order to network byte order. For converting constants to
1238  * network byte order, use the UIP_HTONS() macro instead.
1239  */
1240 #ifndef uip_htons
1241 uint16_t uip_htons(uint16_t val);
1242 #endif /* uip_htons */
1243 #ifndef uip_ntohs
1244 #define uip_ntohs uip_htons
1245 #endif
1246 
1247 #ifndef uip_htonl
1248 uint32_t uip_htonl(uint32_t val);
1249 #endif /* uip_htonl */
1250 #ifndef uip_ntohl
1251 #define uip_ntohl uip_htonl
1252 #endif
1253 
1254 /** @} */
1255 
1256 /**
1257  * Pointer to the application data in the packet buffer.
1258  *
1259  * This pointer points to the application data when the application is
1260  * called. If the application wishes to send data, the application may
1261  * use this space to write the data into before calling uip_send().
1262  */
1263 extern void *uip_appdata;
1264 
1265 #if UIP_URGDATA > 0
1266 /* uint8_t *uip_urgdata:
1267  *
1268  * This pointer points to any urgent data that has been received. Only
1269  * present if compiled with support for urgent data (UIP_URGDATA).
1270  */
1271 extern void *uip_urgdata;
1272 #endif /* UIP_URGDATA > 0 */
1273 
1274 
1275 /**
1276  * \defgroup uipdrivervars Variables used in uIP device drivers
1277  * @{
1278  *
1279  * uIP has a few global variables that are used in device drivers for
1280  * uIP.
1281  */
1282 
1283 /**
1284  * The length of the packet in the uip_buf buffer.
1285  *
1286  * The global variable uip_len holds the length of the packet in the
1287  * uip_buf buffer.
1288  *
1289  * When the network device driver calls the uIP input function,
1290  * uip_len should be set to the length of the packet in the uip_buf
1291  * buffer.
1292  *
1293  * When sending packets, the device driver should use the contents of
1294  * the uip_len variable to determine the length of the outgoing
1295  * packet.
1296  *
1297  */
1298 extern uint16_t uip_len;
1299 
1300 /**
1301  * The length of the extension headers
1302  */
1303 extern uint16_t uip_ext_len;
1304 
1305 /** The final protocol after IPv6 extension headers:
1306  * UIP_PROTO_TCP, UIP_PROTO_UDP or UIP_PROTO_ICMP6 */
1307 extern uint8_t uip_last_proto;
1308 /** @} */
1309 
1310 #if UIP_URGDATA > 0
1311 extern uint16_t uip_urglen, uip_surglen;
1312 #endif /* UIP_URGDATA > 0 */
1313 
1314 /**
1315  * Representation of a uIP TCP connection.
1316  *
1317  * The uip_conn structure is used for identifying a connection. All
1318  * but one field in the structure are to be considered read-only by an
1319  * application. The only exception is the appstate field whose purpose
1320  * is to let the application store application-specific state (e.g.,
1321  * file pointers) for the connection. The type of this field is
1322  * configured in the "uipopt.h" header file.
1323  */
1324 struct uip_conn {
1325  uip_ipaddr_t ripaddr; /**< The IP address of the remote host. */
1326 
1327  uint16_t lport; /**< The local TCP port, in network byte order. */
1328  uint16_t rport; /**< The local remote TCP port, in network byte
1329  order. */
1330 
1331  uint8_t rcv_nxt[4]; /**< The sequence number that we expect to
1332  receive next. */
1333  uint8_t snd_nxt[4]; /**< The sequence number that was last sent by us. */
1334  uint16_t len; /**< Length of the data that was previously sent. */
1335  uint16_t mss; /**< Current maximum segment size for the connection. */
1336  uint16_t initialmss; /**< Initial maximum segment size for the connection. */
1337  uint8_t sa; /**< Retransmission time-out calculation state variable. */
1338  uint8_t sv; /**< Retransmission time-out calculation state variable. */
1339  uint8_t rto; /**< Retransmission time-out. */
1340  uint8_t tcpstateflags; /**< TCP state and flags. */
1341  uint8_t timer; /**< The retransmission timer. */
1342  uint8_t nrtx; /**< The number of retransmissions for the last
1343  segment sent. */
1344  uip_tcp_appstate_t appstate; /** The application state. */
1345 };
1346 
1347 
1348 /**
1349  * Pointer to the current TCP connection.
1350  *
1351  * The uip_conn pointer can be used to access the current TCP
1352  * connection.
1353  */
1354 
1355 extern struct uip_conn *uip_conn;
1356 #if UIP_TCP
1357 /* The array containing all uIP connections. */
1358 extern struct uip_conn uip_conns[UIP_TCP_CONNS];
1359 #endif
1360 
1361 /**
1362  * \addtogroup uiparch
1363  * @{
1364  */
1365 
1366 /**
1367  * 4-byte array used for the 32-bit sequence number calculations.
1368  */
1369 extern uint8_t uip_acc32[4];
1370 /** @} */
1371 
1372 /**
1373  * Representation of a uIP UDP connection.
1374  */
1376  uip_ipaddr_t ripaddr; /**< The IP address of the remote peer. */
1377  uint16_t lport; /**< The local port number in network byte order. */
1378  uint16_t rport; /**< The remote port number in network byte order. */
1379  uint8_t ttl; /**< Default time-to-live. */
1380  /** The application state. */
1382 };
1383 
1384 /**
1385  * The current UDP connection.
1386  */
1387 extern struct uip_udp_conn *uip_udp_conn;
1388 extern struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];
1389 
1390 struct uip_fallback_interface {
1391  void (*init)(void);
1392  /**
1393  * \retval >=0
1394  * in case of success
1395  * \retval <0
1396  * in case of failure
1397  */
1398  int (*output)(void);
1399 };
1400 
1401 #if UIP_CONF_ICMP6
1402 struct uip_icmp6_conn {
1403  uip_icmp6_appstate_t appstate;
1404 };
1405 extern struct uip_icmp6_conn uip_icmp6_conns;
1406 #endif /*UIP_CONF_ICMP6*/
1407 
1408 /**
1409  * The uIP TCP/IP statistics.
1410  *
1411  * This is the variable in which the uIP TCP/IP statistics are gathered.
1412  */
1413 #if UIP_STATISTICS == 1
1414 extern struct uip_stats uip_stat;
1415 #define UIP_STAT(s) s
1416 #else
1417 #define UIP_STAT(s)
1418 #endif /* UIP_STATISTICS == 1 */
1419 
1420 /**
1421  * The structure holding the TCP/IP statistics that are gathered if
1422  * UIP_STATISTICS is set to 1.
1423  *
1424  */
1425 struct uip_stats {
1426  struct {
1427  uip_stats_t recv; /**< Number of received packets at the IP layer. */
1428  uip_stats_t sent; /**< Number of sent packets at the IP layer. */
1429  uip_stats_t forwarded;/**< Number of forwarded packets at the IP layer. */
1430  uip_stats_t drop; /**< Number of dropped packets at the IP layer. */
1431  uip_stats_t vhlerr; /**< Number of packets dropped due to wrong
1432  IP version or header length. */
1433  uip_stats_t hblenerr; /**< Number of packets dropped due to wrong
1434  IP length, high byte. */
1435  uip_stats_t lblenerr; /**< Number of packets dropped due to wrong
1436  IP length, low byte. */
1437  uip_stats_t fragerr; /**< Number of packets dropped because they
1438  were IP fragments. */
1439  uip_stats_t chkerr; /**< Number of packets dropped due to IP
1440  checksum errors. */
1441  uip_stats_t protoerr; /**< Number of packets dropped because they
1442  were neither ICMP, UDP nor TCP. */
1443  } ip; /**< IP statistics. */
1444  struct {
1445  uip_stats_t recv; /**< Number of received ICMP packets. */
1446  uip_stats_t sent; /**< Number of sent ICMP packets. */
1447  uip_stats_t drop; /**< Number of dropped ICMP packets. */
1448  uip_stats_t typeerr; /**< Number of ICMP packets with a wrong type. */
1449  uip_stats_t chkerr; /**< Number of ICMP packets with a bad checksum. */
1450  } icmp; /**< ICMP statistics. */
1451 #if UIP_TCP
1452  struct {
1453  uip_stats_t recv; /**< Number of recived TCP segments. */
1454  uip_stats_t sent; /**< Number of sent TCP segments. */
1455  uip_stats_t drop; /**< Number of dropped TCP segments. */
1456  uip_stats_t chkerr; /**< Number of TCP segments with a bad checksum. */
1457  uip_stats_t ackerr; /**< Number of TCP segments with a bad ACK number. */
1458  uip_stats_t rst; /**< Number of received TCP RST (reset) segments. */
1459  uip_stats_t rexmit; /**< Number of retransmitted TCP segments. */
1460  uip_stats_t syndrop; /**< Number of dropped SYNs because too few
1461  connections were available. */
1462  uip_stats_t synrst; /**< Number of SYNs for closed ports,
1463  triggering a RST. */
1464  } tcp; /**< TCP statistics. */
1465 #endif
1466 #if UIP_UDP
1467  struct {
1468  uip_stats_t drop; /**< Number of dropped UDP segments. */
1469  uip_stats_t recv; /**< Number of recived UDP segments. */
1470  uip_stats_t sent; /**< Number of sent UDP segments. */
1471  uip_stats_t chkerr; /**< Number of UDP segments with a bad
1472  checksum. */
1473  } udp; /**< UDP statistics. */
1474 #endif /* UIP_UDP */
1475  struct {
1476  uip_stats_t drop; /**< Number of dropped ND6 packets. */
1477  uip_stats_t recv; /**< Number of recived ND6 packets */
1478  uip_stats_t sent; /**< Number of sent ND6 packets */
1479  } nd6;
1480 };
1481 
1482 
1483 /*---------------------------------------------------------------------------*/
1484 /* All the stuff below this point is internal to uIP and should not be
1485  * used directly by an application or by a device driver.
1486  */
1487 /*---------------------------------------------------------------------------*/
1488 
1489 /**
1490  * The Ethernet header.
1491  */
1492 struct uip_eth_hdr {
1493  struct uip_eth_addr dest;
1494  struct uip_eth_addr src;
1495  uint16_t type;
1496 };
1497 
1498 #define UIP_ETHTYPE_ARP 0x0806
1499 #define UIP_ETHTYPE_IP 0x0800
1500 #define UIP_ETHTYPE_IPV6 0x86dd
1501 
1502 /* uint8_t uip_flags:
1503  *
1504  * When the application is called, uip_flags will contain the flags
1505  * that are defined in this file. Please read below for more
1506  * information.
1507  */
1508 extern uint8_t uip_flags;
1509 
1510 /* The following flags may be set in the global variable uip_flags
1511  before calling the application callback. The UIP_ACKDATA,
1512  UIP_NEWDATA, and UIP_CLOSE flags may both be set at the same time,
1513  whereas the others are mutually exclusive. Note that these flags
1514  should *NOT* be accessed directly, but only through the uIP
1515  functions/macros. */
1516 
1517 #define UIP_ACKDATA 1 /* Signifies that the outstanding data was
1518  acked and the application should send
1519  out new data instead of retransmitting
1520  the last data. */
1521 #define UIP_NEWDATA 2 /* Flags the fact that the peer has sent
1522  us new data. */
1523 #define UIP_REXMIT 4 /* Tells the application to retransmit the
1524  data that was last sent. */
1525 #define UIP_POLL 8 /* Used for polling the application, to
1526  check if the application has data that
1527  it wants to send. */
1528 #define UIP_CLOSE 16 /* The remote host has closed the
1529  connection, thus the connection has
1530  gone away. Or the application signals
1531  that it wants to close the
1532  connection. */
1533 #define UIP_ABORT 32 /* The remote host has aborted the
1534  connection, thus the connection has
1535  gone away. Or the application signals
1536  that it wants to abort the
1537  connection. */
1538 #define UIP_CONNECTED 64 /* We have got a connection from a remote
1539  host and have set up a new connection
1540  for it, or an active connection has
1541  been successfully established. */
1542 
1543 #define UIP_TIMEDOUT 128 /* The connection has been aborted due to
1544  too many retransmissions. */
1545 
1546 
1547 /**
1548  * \brief process the options within a hop by hop or destination option header
1549  * \retval 0: nothing to send,
1550  * \retval 1: drop pkt
1551  * \retval 2: ICMP error message to send
1552 */
1553 /*static uint8_t
1554 uip_ext_hdr_options_process(); */
1555 
1556 /* uip_process(flag):
1557  *
1558  * The actual uIP function which does all the work.
1559  */
1560 void uip_process(uint8_t flag);
1561 
1562  /* The following flags are passed as an argument to the uip_process()
1563  function. They are used to distinguish between the two cases where
1564  uip_process() is called. It can be called either because we have
1565  incoming data that should be processed, or because the periodic
1566  timer has fired. These values are never used directly, but only in
1567  the macros defined in this file. */
1568 
1569 #define UIP_DATA 1 /* Tells uIP that there is incoming
1570  data in the uip_buf buffer. The
1571  length of the data is stored in the
1572  global variable uip_len. */
1573 #define UIP_TIMER 2 /* Tells uIP that the periodic timer
1574  has fired. */
1575 #define UIP_POLL_REQUEST 3 /* Tells uIP that a connection should
1576  be polled. */
1577 #define UIP_UDP_SEND_CONN 4 /* Tells uIP that a UDP datagram
1578  should be constructed in the
1579  uip_buf buffer. */
1580 #if UIP_UDP
1581 #define UIP_UDP_TIMER 5
1582 #endif /* UIP_UDP */
1583 
1584 /* The TCP states used in the uip_conn->tcpstateflags. */
1585 #define UIP_CLOSED 0
1586 #define UIP_SYN_RCVD 1
1587 #define UIP_SYN_SENT 2
1588 #define UIP_ESTABLISHED 3
1589 #define UIP_FIN_WAIT_1 4
1590 #define UIP_FIN_WAIT_2 5
1591 #define UIP_CLOSING 6
1592 #define UIP_TIME_WAIT 7
1593 #define UIP_LAST_ACK 8
1594 #define UIP_TS_MASK 15
1595 
1596 #define UIP_STOPPED 16
1597 
1598 /*
1599  * In IPv6 the length of the L3 headers before the transport header is
1600  * not fixed, due to the possibility to include extension option headers
1601  * after the IP header. hence we split here L3 and L4 headers
1602  */
1603 /* The IP header */
1604 
1605 struct uip_ip_hdr {
1606  /* IPV6 header */
1607  uint8_t vtc;
1608  uint8_t tcflow;
1609  uint16_t flow;
1610  uint8_t len[2];
1611  uint8_t proto, ttl;
1612  uip_ip6addr_t srcipaddr, destipaddr;
1613 };
1614 
1615 
1616 /*
1617  * IPv6 extension option headers: we are able to process
1618  * the 4 extension headers defined in RFC2460 (IPv6):
1619  * - Hop by hop option header, destination option header:
1620  * These two are not used by any core IPv6 protocol, hence
1621  * we just read them and go to the next. They convey options,
1622  * the options defined in RFC2460 are Pad1 and PadN, which do
1623  * some padding, and that we do not need to read (the length
1624  * field in the header is enough)
1625  * - Routing header: this one is most notably used by MIPv6,
1626  * which we do not implement, hence we just read it and go
1627  * to the next
1628  * - Fragmentation header: we read this header and are able to
1629  * reassemble packets
1630  *
1631  * We do not offer any means to send packets with extension headers
1632  *
1633  * We do not implement Authentication and ESP headers, which are
1634  * used in IPSec and defined in RFC4302,4303,4305,4385
1635  */
1636 /* common header part */
1637 typedef struct uip_ext_hdr {
1638  uint8_t next;
1639  uint8_t len;
1640 } uip_ext_hdr;
1641 
1642 /* Hop by Hop option header */
1643 typedef struct uip_hbho_hdr {
1644  uint8_t next;
1645  uint8_t len;
1646 } uip_hbho_hdr;
1647 
1648 /* destination option header */
1649 typedef struct uip_desto_hdr {
1650  uint8_t next;
1651  uint8_t len;
1652 } uip_desto_hdr;
1653 
1654 /* We do not define structures for PAD1 and PADN options */
1655 
1656 /*
1657  * routing header
1658  * the routing header as 4 common bytes, then routing header type
1659  * specific data there are several types of routing header. Type 0 was
1660  * deprecated as per RFC5095 most notable other type is 2, used in
1661  * RFC3775 (MIPv6) here we do not implement MIPv6, so we just need to
1662  * parse the 4 first bytes
1663  */
1664 typedef struct uip_routing_hdr {
1665  uint8_t next;
1666  uint8_t len;
1667  uint8_t routing_type;
1668  uint8_t seg_left;
1669 } uip_routing_hdr;
1670 
1671 /* RPL Source Routing Header */
1672 typedef struct uip_rpl_srh_hdr {
1673  uint8_t cmpr; /* CmprI and CmprE */
1674  uint8_t pad;
1675  uint8_t reserved[2];
1676 } uip_rpl_srh_hdr;
1677 
1678 /* fragmentation header */
1679 typedef struct uip_frag_hdr {
1680  uint8_t next;
1681  uint8_t res;
1682  uint16_t offsetresmore;
1683  uint32_t id;
1684 } uip_frag_hdr;
1685 
1686 /*
1687  * an option within the destination or hop by hop option headers
1688  * it contains type an length, which is true for all options but PAD1
1689  */
1690 typedef struct uip_ext_hdr_opt {
1691  uint8_t type;
1692  uint8_t len;
1693 } uip_ext_hdr_opt;
1694 
1695 /* PADN option */
1696 typedef struct uip_ext_hdr_opt_padn {
1697  uint8_t opt_type;
1698  uint8_t opt_len;
1699 } uip_ext_hdr_opt_padn;
1700 
1701 /* RPL option */
1702 typedef struct uip_ext_hdr_opt_rpl {
1703  uint8_t opt_type;
1704  uint8_t opt_len;
1705  uint8_t flags;
1706  uint8_t instance;
1707  uint16_t senderrank;
1708 } uip_ext_hdr_opt_rpl;
1709 
1710 /* TCP header */
1711 struct uip_tcp_hdr {
1712  uint16_t srcport;
1713  uint16_t destport;
1714  uint8_t seqno[4];
1715  uint8_t ackno[4];
1716  uint8_t tcpoffset;
1717  uint8_t flags;
1718  uint8_t wnd[2];
1719  uint16_t tcpchksum;
1720  uint8_t urgp[2];
1721  uint8_t optdata[4];
1722 };
1723 
1724 /* The ICMP headers. */
1725 struct uip_icmp_hdr {
1726  uint8_t type, icode;
1727  uint16_t icmpchksum;
1728 };
1729 
1730 
1731 /* The UDP headers. */
1732 struct uip_udp_hdr {
1733  uint16_t srcport;
1734  uint16_t destport;
1735  uint16_t udplen;
1736  uint16_t udpchksum;
1737 };
1738 
1739 
1740 /**
1741  * The buffer size available for user data in the \ref uip_buf buffer.
1742  *
1743  * This macro holds the available size for user data in the \ref
1744  * uip_buf buffer. The macro is intended to be used for checking
1745  * bounds of available user data.
1746  *
1747  * Example:
1748  \code
1749  snprintf(uip_appdata, UIP_APPDATA_SIZE, "%u\n", i);
1750  \endcode
1751  *
1752  * \hideinitializer
1753  */
1754 #define UIP_APPDATA_SIZE (UIP_BUFSIZE - UIP_IPTCPH_LEN)
1755 
1756 #define UIP_PROTO_ICMP 1
1757 #define UIP_PROTO_TCP 6
1758 #define UIP_PROTO_UDP 17
1759 #define UIP_PROTO_ICMP6 58
1760 
1761 
1762 /** @{ */
1763 /** \brief extension headers types */
1764 #define UIP_PROTO_HBHO 0
1765 #define UIP_PROTO_DESTO 60
1766 #define UIP_PROTO_ROUTING 43
1767 #define UIP_PROTO_FRAG 44
1768 #define UIP_PROTO_NONE 59
1769 /** @} */
1770 
1771 #define uip_is_proto_ext_hdr(proto) ((proto) != UIP_PROTO_TCP && (proto) != UIP_PROTO_UDP && (proto) != UIP_PROTO_ICMP6)
1772 
1773 /** @{ */
1774 /** \brief Destination and Hop By Hop extension headers option types */
1775 #define UIP_EXT_HDR_OPT_PAD1 0
1776 #define UIP_EXT_HDR_OPT_PADN 1
1777 #define UIP_EXT_HDR_OPT_RPL 0x63
1778 #define UIP_EXT_HDR_OPT_MPL 0x6D
1779 
1780 /** @} */
1781 
1782 /** @{ */
1783 /**
1784  * \brief Bitmaps for extension header processing
1785  *
1786  * When processing extension headers, we should record somehow which one we
1787  * see, because you cannot have twice the same header, except for destination
1788  * We store all this in one uint8_t bitmap one bit for each header expected. The
1789  * order in the bitmap is the order recommended in RFC2460
1790  */
1791 #define UIP_EXT_HDR_BITMAP_HBHO 0x01
1792 #define UIP_EXT_HDR_BITMAP_DESTO1 0x02
1793 #define UIP_EXT_HDR_BITMAP_ROUTING 0x04
1794 #define UIP_EXT_HDR_BITMAP_FRAG 0x08
1795 #define UIP_EXT_HDR_BITMAP_AH 0x10
1796 #define UIP_EXT_HDR_BITMAP_ESP 0x20
1797 #define UIP_EXT_HDR_BITMAP_DESTO2 0x40
1798 /** @} */
1799 
1800 
1801 #if UIP_FIXEDADDR
1802 extern const uip_ipaddr_t uip_hostaddr, uip_netmask, uip_draddr;
1803 #else /* UIP_FIXEDADDR */
1804 extern uip_ipaddr_t uip_hostaddr, uip_netmask, uip_draddr;
1805 #endif /* UIP_FIXEDADDR */
1806 extern const uip_ipaddr_t uip_broadcast_addr;
1807 extern const uip_ipaddr_t uip_all_zeroes_addr;
1808 
1809 #if UIP_FIXEDETHADDR
1810 extern const uip_lladdr_t uip_lladdr;
1811 #else
1812 extern uip_lladdr_t uip_lladdr;
1813 #endif
1814 
1815 /** Length of the link local prefix */
1816 #define UIP_LLPREF_LEN 10
1817 
1818 /**
1819  * \brief Is IPv6 address a the unspecified address
1820  * a is of type uip_ipaddr_t
1821  */
1822 #define uip_is_addr_loopback(a) \
1823  ((((a)->u16[0]) == 0) && \
1824  (((a)->u16[1]) == 0) && \
1825  (((a)->u16[2]) == 0) && \
1826  (((a)->u16[3]) == 0) && \
1827  (((a)->u16[4]) == 0) && \
1828  (((a)->u16[5]) == 0) && \
1829  (((a)->u16[6]) == 0) && \
1830  (((a)->u8[14]) == 0) && \
1831  (((a)->u8[15]) == 0x01))
1832 /**
1833  * \brief Is IPv6 address a the unspecified address
1834  * a is of type uip_ipaddr_t
1835  */
1836 #define uip_is_addr_unspecified(a) \
1837  ((((a)->u16[0]) == 0) && \
1838  (((a)->u16[1]) == 0) && \
1839  (((a)->u16[2]) == 0) && \
1840  (((a)->u16[3]) == 0) && \
1841  (((a)->u16[4]) == 0) && \
1842  (((a)->u16[5]) == 0) && \
1843  (((a)->u16[6]) == 0) && \
1844  (((a)->u16[7]) == 0))
1845 
1846 /** \brief Is IPv6 address a the link local all-nodes multicast address */
1847 #define uip_is_addr_linklocal_allnodes_mcast(a) \
1848  ((((a)->u8[0]) == 0xff) && \
1849  (((a)->u8[1]) == 0x02) && \
1850  (((a)->u16[1]) == 0) && \
1851  (((a)->u16[2]) == 0) && \
1852  (((a)->u16[3]) == 0) && \
1853  (((a)->u16[4]) == 0) && \
1854  (((a)->u16[5]) == 0) && \
1855  (((a)->u16[6]) == 0) && \
1856  (((a)->u8[14]) == 0) && \
1857  (((a)->u8[15]) == 0x01))
1858 
1859 /** \brief Is IPv6 address a the link local all-routers multicast address */
1860 #define uip_is_addr_linklocal_allrouters_mcast(a) \
1861  ((((a)->u8[0]) == 0xff) && \
1862  (((a)->u8[1]) == 0x02) && \
1863  (((a)->u16[1]) == 0) && \
1864  (((a)->u16[2]) == 0) && \
1865  (((a)->u16[3]) == 0) && \
1866  (((a)->u16[4]) == 0) && \
1867  (((a)->u16[5]) == 0) && \
1868  (((a)->u16[6]) == 0) && \
1869  (((a)->u8[14]) == 0) && \
1870  (((a)->u8[15]) == 0x02))
1871 
1872 /**
1873  * \brief is addr (a) a link local unicast address, see RFC 4291
1874  * i.e. is (a) on prefix FE80::/10
1875  * a is of type uip_ipaddr_t*
1876  */
1877 #define uip_is_addr_linklocal(a) \
1878  ((a)->u8[0] == 0xfe && \
1879  (a)->u8[1] == 0x80)
1880 
1881 /** \brief set IP address a to unspecified */
1882 #define uip_create_unspecified(a) uip_ip6addr(a, 0, 0, 0, 0, 0, 0, 0, 0)
1883 
1884 /** \brief set IP address a to the link local all-nodes multicast address */
1885 #define uip_create_linklocal_allnodes_mcast(a) uip_ip6addr(a, 0xff02, 0, 0, 0, 0, 0, 0, 0x0001)
1886 
1887 /** \brief set IP address a to the link local all-routers multicast address */
1888 #define uip_create_linklocal_allrouters_mcast(a) uip_ip6addr(a, 0xff02, 0, 0, 0, 0, 0, 0, 0x0002)
1889 #define uip_create_linklocal_prefix(addr) do { \
1890  (addr)->u16[0] = UIP_HTONS(0xfe80); \
1891  (addr)->u16[1] = 0; \
1892  (addr)->u16[2] = 0; \
1893  (addr)->u16[3] = 0; \
1894  } while(0)
1895 
1896 /**
1897  * \brief is addr (a) a solicited node multicast address, see RFC 4291
1898  * a is of type uip_ipaddr_t*
1899  */
1900 #define uip_is_addr_solicited_node(a) \
1901  ((((a)->u8[0]) == 0xFF) && \
1902  (((a)->u8[1]) == 0x02) && \
1903  (((a)->u16[1]) == 0x00) && \
1904  (((a)->u16[2]) == 0x00) && \
1905  (((a)->u16[3]) == 0x00) && \
1906  (((a)->u16[4]) == 0x00) && \
1907  (((a)->u8[10]) == 0x00) && \
1908  (((a)->u8[11]) == 0x01) && \
1909  (((a)->u8[12]) == 0xFF))
1910 
1911 /**
1912  * \brief put in b the solicited node address corresponding to address a
1913  * both a and b are of type uip_ipaddr_t*
1914  * */
1915 #define uip_create_solicited_node(a, b) \
1916  (((b)->u8[0]) = 0xFF); \
1917  (((b)->u8[1]) = 0x02); \
1918  (((b)->u16[1]) = 0); \
1919  (((b)->u16[2]) = 0); \
1920  (((b)->u16[3]) = 0); \
1921  (((b)->u16[4]) = 0); \
1922  (((b)->u8[10]) = 0); \
1923  (((b)->u8[11]) = 0x01); \
1924  (((b)->u8[12]) = 0xFF); \
1925  (((b)->u8[13]) = ((a)->u8[13])); \
1926  (((b)->u16[7]) = ((a)->u16[7]))
1927 
1928 /**
1929  * \brief was addr (a) forged based on the mac address m
1930  * a type is uip_ipaddr_t
1931  * m type is uiplladdr_t
1932  */
1933 #if UIP_CONF_LL_802154
1934 #define uip_is_addr_mac_addr_based(a, m) \
1935  ((((a)->u8[8]) == (((m)->addr[0]) ^ 0x02)) && \
1936  (((a)->u8[9]) == (m)->addr[1]) && \
1937  (((a)->u8[10]) == (m)->addr[2]) && \
1938  (((a)->u8[11]) == (m)->addr[3]) && \
1939  (((a)->u8[12]) == (m)->addr[4]) && \
1940  (((a)->u8[13]) == (m)->addr[5]) && \
1941  (((a)->u8[14]) == (m)->addr[6]) && \
1942  (((a)->u8[15]) == (m)->addr[7]))
1943 #else
1944 
1945 #define uip_is_addr_mac_addr_based(a, m) \
1946  ((((a)->u8[8]) == (((m)->addr[0]) | 0x02)) && \
1947  (((a)->u8[9]) == (m)->addr[1]) && \
1948  (((a)->u8[10]) == (m)->addr[2]) && \
1949  (((a)->u8[11]) == 0xff) && \
1950  (((a)->u8[12]) == 0xfe) && \
1951  (((a)->u8[13]) == (m)->addr[3]) && \
1952  (((a)->u8[14]) == (m)->addr[4]) && \
1953  (((a)->u8[15]) == (m)->addr[5]))
1954 
1955 #endif /*UIP_CONF_LL_802154*/
1956 
1957 /**
1958  * \brief is address a multicast address, see RFC 4291
1959  * a is of type uip_ipaddr_t*
1960  * */
1961 #define uip_is_addr_mcast(a) \
1962  (((a)->u8[0]) == 0xFF)
1963 
1964 /**
1965  * \brief is address a global multicast address (FFxE::/16),
1966  * a is of type uip_ip6addr_t*
1967  * */
1968 #define uip_is_addr_mcast_global(a) \
1969  ((((a)->u8[0]) == 0xFF) && \
1970  (((a)->u8[1] & 0x0F) == 0x0E))
1971 
1972 /**
1973  * \brief is address a non-routable multicast address.
1974  * Scopes 1 (interface-local) and 2 (link-local) are non-routable
1975  * See RFC4291 and draft-ietf-6man-multicast-scopes
1976  * a is of type uip_ip6addr_t*
1977  * */
1978 #define uip_is_addr_mcast_non_routable(a) \
1979  ((((a)->u8[0]) == 0xFF) && \
1980  (((a)->u8[1] & 0x0F) <= 0x02))
1981 
1982 /**
1983  * \brief is address a routable multicast address.
1984  * Scope 3 (Realm-Local) or higher are routable
1985  * Realm-Local scope is defined in draft-ietf-6man-multicast-scopes
1986  * See RFC4291 and draft-ietf-6man-multicast-scopes
1987  * a is of type uip_ip6addr_t*
1988  * */
1989 #define uip_is_addr_mcast_routable(a) \
1990  ((((a)->u8[0]) == 0xFF) && \
1991  (((a)->u8[1] & 0x0F) > 0x02))
1992 
1993 /**
1994  * \brief is group-id of multicast address a
1995  * the all nodes group-id
1996  */
1997 #define uip_is_mcast_group_id_all_nodes(a) \
1998  ((((a)->u16[1]) == 0) && \
1999  (((a)->u16[2]) == 0) && \
2000  (((a)->u16[3]) == 0) && \
2001  (((a)->u16[4]) == 0) && \
2002  (((a)->u16[5]) == 0) && \
2003  (((a)->u16[6]) == 0) && \
2004  (((a)->u8[14]) == 0) && \
2005  (((a)->u8[15]) == 1))
2006 
2007 /**
2008  * \brief is group-id of multicast address a
2009  * the all routers group-id
2010  */
2011 #define uip_is_mcast_group_id_all_routers(a) \
2012  ((((a)->u16[1]) == 0) && \
2013  (((a)->u16[2]) == 0) && \
2014  (((a)->u16[3]) == 0) && \
2015  (((a)->u16[4]) == 0) && \
2016  (((a)->u16[5]) == 0) && \
2017  (((a)->u16[6]) == 0) && \
2018  (((a)->u8[14]) == 0) && \
2019  (((a)->u8[15]) == 2))
2020 
2021 
2022 /**
2023  * \brief are last three bytes of both addresses equal?
2024  * This is used to compare solicited node multicast addresses
2025  */
2026 #define uip_are_solicited_bytes_equal(a, b) \
2027  ((((a)->u8[13]) == ((b)->u8[13])) && \
2028  (((a)->u8[14]) == ((b)->u8[14])) && \
2029  (((a)->u8[15]) == ((b)->u8[15])))
2030 
2031 /**
2032  * A non-error message that indicates that a packet should be
2033  * processed locally.
2034  *
2035  * \hideinitializer
2036  */
2037 #define UIP_FW_LOCAL 0
2038 
2039 /**
2040  * A non-error message that indicates that something went OK.
2041  *
2042  * \hideinitializer
2043  */
2044 #define UIP_FW_OK 0
2045 
2046 /**
2047  * A non-error message that indicates that a packet was forwarded.
2048  *
2049  * \hideinitializer
2050  */
2051 #define UIP_FW_FORWARDED 1
2052 
2053 /**
2054  * A non-error message that indicates that a zero-length packet
2055  * transmission was attempted, and that no packet was sent.
2056  *
2057  * \hideinitializer
2058  */
2059 #define UIP_FW_ZEROLEN 2
2060 
2061 /**
2062  * An error message that indicates that a packet that was too large
2063  * for the outbound network interface was detected.
2064  *
2065  * \hideinitializer
2066  */
2067 #define UIP_FW_TOOLARGE 3
2068 
2069 /**
2070  * An error message that indicates that no suitable interface could be
2071  * found for an outbound packet.
2072  *
2073  * \hideinitializer
2074  */
2075 #define UIP_FW_NOROUTE 4
2076 
2077 /**
2078  * An error message that indicates that a packet that should be
2079  * forwarded or output was dropped.
2080  *
2081  * \hideinitializer
2082  */
2083 #define UIP_FW_DROPPED 5
2084 
2085 /**
2086  * Calculate the Internet checksum over a buffer.
2087  *
2088  * The Internet checksum is the one's complement of the one's
2089  * complement sum of all 16-bit words in the buffer.
2090  *
2091  * See RFC1071.
2092  *
2093  * \param data A pointer to the buffer over which the checksum is to be
2094  * computed.
2095  *
2096  * \param len The length of the buffer over which the checksum is to
2097  * be computed.
2098  *
2099  * \return The Internet checksum of the buffer.
2100  */
2101 uint16_t uip_chksum(uint16_t *data, uint16_t len);
2102 
2103 /**
2104  * Calculate the IP header checksum of the packet header in uip_buf.
2105  *
2106  * The IP header checksum is the Internet checksum of the 20 bytes of
2107  * the IP header.
2108  *
2109  * \return The IP header checksum of the IP header in the uip_buf
2110  * buffer.
2111  */
2112 uint16_t uip_ipchksum(void);
2113 
2114 /**
2115  * Calculate the TCP checksum of the packet in uip_buf and uip_appdata.
2116  *
2117  * The TCP checksum is the Internet checksum of data contents of the
2118  * TCP segment, and a pseudo-header as defined in RFC793.
2119  *
2120  * \return The TCP checksum of the TCP segment in uip_buf and pointed
2121  * to by uip_appdata.
2122  */
2123 uint16_t uip_tcpchksum(void);
2124 
2125 /**
2126  * Calculate the UDP checksum of the packet in uip_buf and uip_appdata.
2127  *
2128  * The UDP checksum is the Internet checksum of data contents of the
2129  * UDP segment, and a pseudo-header as defined in RFC768.
2130  *
2131  * \return The UDP checksum of the UDP segment in uip_buf and pointed
2132  * to by uip_appdata.
2133  */
2134 uint16_t uip_udpchksum(void);
2135 
2136 /**
2137  * Calculate the ICMP checksum of the packet in uip_buf.
2138  *
2139  * \return The ICMP checksum of the ICMP packet in uip_buf
2140  */
2141 uint16_t uip_icmp6chksum(void);
2142 
2143 /**
2144  * Removes all IPv6 extension headers from uip_buf, updates length fields
2145  * (uip_len and uip_ext_len)
2146  *
2147  * \return true upon success, false otherwise.
2148  */
2149 bool uip_remove_ext_hdr(void);
2150 
2151 #endif /* UIP_H_ */
2152 
2153 
2154 /** @} */
uint8_t uip_last_proto
The final protocol after IPv6 extension headers: UIP_PROTO_TCP, UIP_PROTO_UDP or UIP_PROTO_ICMP6.
Definition: uip6.c:125
uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip6.c:107
struct uip_udp_conn * uip_udp_new(const uip_ipaddr_t *ripaddr, uint16_t rport)
Set up a new UDP connection.
Definition: uip6.c:522
void uip_reass_over(void)
Abandon the reassembly of the current packet.
Definition: uip6.c:775
Header for the Contiki/uIP interface.
uip_stats_t drop
Number of dropped packets at the IP layer.
Definition: uip.h:1430
Representation of a uIP TCP connection.
Definition: uip.h:1324
uint16_t uip_len
The length of the packet in the uip_buf buffer.
Definition: uip6.c:159
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition: uip-nd6.c:107
void uip_process(uint8_t flag)
process the options within a hop by hop or destination option header
Definition: uip6.c:948
#define UIP_TCP_CONNS
The maximum number of simultaneously open TCP connections.
Definition: uipopt.h:359
Header file for the link-layer address representation
uip_stats_t sent
Number of sent packets at the IP layer.
Definition: uip.h:1428
#define UIP_BUFSIZE
The size of the uIP packet buffer.
Definition: uipopt.h:134
#define UIP_UDP_CONNS
The maximum amount of concurrent UDP connections.
Definition: uipopt.h:305
64 bit 802.15.4 address
Definition: uip.h:116
uint16_t uip_chksum(uint16_t *data, uint16_t len)
Calculate the Internet checksum over a buffer.
Definition: uip6.c:313
uip_ipaddr_t ripaddr
The IP address of the remote host.
Definition: uip.h:1325
struct uip_icmp6_conn uip_icmp6_conns
single possible icmpv6 "connection"
Definition: uip6.c:243
uip_stats_t recv
Number of received packets at the IP layer.
Definition: uip.h:1427
uip_stats_t forwarded
Number of forwarded packets at the IP layer.
Definition: uip.h:1429
void uip_unlisten(uint16_t port)
Stop listening to the specified port.
Definition: uip6.c:568
802.3 address
Definition: uip.h:126
Representation of an IP address.
Definition: uip.h:95
uint8_t sa
Retransmission time-out calculation state variable.
Definition: uip.h:1337
static uint8_t output(const linkaddr_t *localdest)
Take an IP packet and format it to be sent on an 802.15.4 network using 6lowpan.
Definition: sicslowpan.c:1576
void uip_setipid(uint16_t id)
uIP initialization function.
uip_stats_t protoerr
Number of packets dropped because they were neither ICMP, UDP nor TCP.
Definition: uip.h:1441
uip_stats_t rst
Number of received TCP RST (reset) segments.
Definition: uip.h:1458
uint16_t uip_htons(uint16_t val)
Convert a 16-bit quantity from host byte order to network byte order.
Definition: uip6.c:2347
uint16_t lport
The local port number in network byte order.
Definition: uip.h:1377
802.11 address
Definition: uip.h:121
uint16_t rport
The local remote TCP port, in network byte order.
Definition: uip.h:1328
Configuration options for uIP.
uint8_t rto
Retransmission time-out.
Definition: uip.h:1339
uint16_t uip_ipchksum(void)
Calculate the IP header checksum of the packet header in uip_buf.
Definition: uip6.c:320
void uip_init(void)
uIP initialization function.
Definition: uip6.c:387
uint8_t uip_acc32[4]
4-byte array used for the 32-bit sequence number calculations.
Definition: uip6.c:219
uint16_t uip_ext_len
The length of the extension headers.
Definition: uip6.c:122
uip_stats_t ackerr
Number of TCP segments with a bad ACK number.
Definition: uip.h:1457
The structure holding the TCP/IP statistics that are gathered if UIP_STATISTICS is set to 1...
Definition: uip.h:1425
struct tcpip_uipstate uip_udp_appstate_t
The type of the application state that is to be stored in the uip_conn structure. ...
Definition: tcpip.h:85
uint8_t tcpstateflags
TCP state and flags.
Definition: uip.h:1340
uip_stats_t rexmit
Number of retransmitted TCP segments.
Definition: uip.h:1459
uint16_t lport
The local TCP port, in network byte order.
Definition: uip.h:1327
uip_stats_t synrst
Number of SYNs for closed ports, triggering a RST.
Definition: uip.h:1462
uip_stats_t syndrop
Number of dropped SYNs because too few connections were available.
Definition: uip.h:1460
void uip_listen(uint16_t port)
Start listening to the specified port.
Definition: uip6.c:580
struct uip_eth_addr uip_eth_addr
802.3 address
union uip_ip4addr_t uip_ip4addr_t
Representation of an IP address.
uip_stats_t typeerr
Number of ICMP packets with a wrong type.
Definition: uip.h:1448
struct uip_802154_shortaddr uip_802154_shortaddr
16 bit 802.15.4 address
uint8_t nrtx
The number of retransmissions for the last segment sent.
Definition: uip.h:1342
uint16_t mss
Current maximum segment size for the connection.
Definition: uip.h:1335
uint16_t initialmss
Initial maximum segment size for the connection.
Definition: uip.h:1336
void uip_send(const void *data, int len)
Send data on the current connection.
Definition: uip6.c:2359
uip_stats_t chkerr
Number of packets dropped due to IP checksum errors.
Definition: uip.h:1439
The Ethernet header.
Definition: uip.h:1492
uip_stats_t lblenerr
Number of packets dropped due to wrong IP length, low byte.
Definition: uip.h:1435
uint8_t timer
The retransmission timer.
Definition: uip.h:1341
bool uip_remove_ext_hdr(void)
Removes all IPv6 extension headers from uip_buf, updates length fields (uip_len and uip_ext_len) ...
Definition: uip6.c:493
uint16_t uip_icmp6chksum(void)
Calculate the ICMP checksum of the packet in uip_buf.
Definition: uip6.c:363
struct uip_802154_longaddr uip_802154_longaddr
64 bit 802.15.4 address
uip_ipaddr_t ripaddr
The IP address of the remote peer.
Definition: uip.h:1376
uint16_t uip_tcpchksum(void)
Calculate the TCP checksum of the packet in uip_buf and uip_appdata.
Definition: uip6.c:371
struct uip_80211_addr uip_80211_addr
802.11 address
uip_buf_t uip_aligned_buf
Packet buffer for incoming and outgoing packets.
Definition: uip6.c:144
uip_stats_t hblenerr
Number of packets dropped due to wrong IP length, high byte.
Definition: uip.h:1433
struct uip_conn * uip_conn
Pointer to the current TCP connection.
Definition: uip6.c:174
uip_stats_t fragerr
Number of packets dropped because they were IP fragments.
Definition: uip.h:1437
struct uip_conn * uip_connect(const uip_ipaddr_t *ripaddr, uint16_t port)
Connect to a remote host using TCP.
uint8_t sv
Retransmission time-out calculation state variable.
Definition: uip.h:1338
uint8_t ttl
Default time-to-live.
Definition: uip.h:1379
uint16_t len
Length of the data that was previously sent.
Definition: uip.h:1334
16 bit 802.15.4 address
Definition: uip.h:112
uint16_t uip_udpchksum(void)
Calculate the UDP checksum of the packet in uip_buf and uip_appdata.
struct tcpip_uipstate uip_tcp_appstate_t
The type of the application state that is to be stored in the uip_conn structure. ...
Definition: tcpip.h:86
void * uip_appdata
Pointer to the application data in the packet buffer.
Definition: uip6.c:148
uint16_t rport
The remote port number in network byte order.
Definition: uip.h:1378
struct uip_udp_conn * uip_udp_conn
The current UDP connection.
Definition: uip6.c:230
uip_stats_t vhlerr
Number of packets dropped due to wrong IP version or header length.
Definition: uip.h:1431
Representation of a uIP UDP connection.
Definition: uip.h:1375
The uIP packet buffer.
Definition: uip.h:502
uip_udp_appstate_t appstate
The application state.
Definition: uip.h:1381