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