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