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