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