uIP 1.0: uip/uip.h Source File

- Main Page
- Modules
- Data Structures
- Files
- Examples

- File List
- Globals

# uip/uip.h

Go to the documentation of this file.

    00001
    00002 /**
    00003  * \addtogroup uip
    00004  * @{
    00005  */
    00006
    00007 /**
    00008  * \file
    00009  * Header file for the uIP TCP/IP stack.
    00010  * \author Adam Dunkels <adam@dunkels.com>
    00011  *
    00012  * The uIP TCP/IP stack header file contains definitions for a number
    00013  * of C macros that are used by uIP programs as well as internal uIP
    00014  * structures, TCP/IP header structures and function declarations.
    00015  *
    00016  */
    00017
    00018
    00019 /*
    00020  * Copyright (c) 2001-2003, Adam Dunkels.
    00021  * All rights reserved.
    00022  *
    00023  * Redistribution and use in source and binary forms, with or without
    00024  * modification, are permitted provided that the following conditions
    00025  * are met:
    00026  * 1. Redistributions of source code must retain the above copyright
    00027  *    notice, this list of conditions and the following disclaimer.
    00028  * 2. Redistributions in binary form must reproduce the above copyright
    00029  *    notice, this list of conditions and the following disclaimer in the
    00030  *    documentation and/or other materials provided with the distribution.
    00031  * 3. The name of the author may not be used to endorse or promote
    00032  *    products derived from this software without specific prior
    00033  *    written permission.
    00034  *
    00035  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
    00036  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    00037  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    00038  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
    00039  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    00040  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
    00041  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    00042  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    00043  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    00044  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    00045  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00046  *
    00047  * This file is part of the uIP TCP/IP stack.
    00048  *
    00049  * $Id: uip.h,v 1.40 2006/06/08 07:12:07 adam Exp $
    00050  *
    00051  */
    00052
    00053 #ifndef __UIP_H__
    00054 #define __UIP_H__
    00055
    00056 #include "uipopt.h"
    00057
    00058 /**
    00059  * Repressentation of an IP address.
    00060  *
    00061  */
    00062 typedef u16_t uip_ip4addr_t[2];
    00063 typedef u16_t uip_ip6addr_t[8];
    00064 #if UIP_CONF_IPV6
    00065 typedef uip_ip6addr_t uip_ipaddr_t;
    00066 #else /* UIP_CONF_IPV6 */
    00067 typedef uip_ip4addr_t uip_ipaddr_t;
    00068 #endif /* UIP_CONF_IPV6 */
    00069
    00070 /*---------------------------------------------------------------------------*/
    00071 /* First, the functions that should be called from the
    00072  * system. Initialization, the periodic timer and incoming packets are
    00073  * handled by the following three functions.
    00074  */
    00075
    00076 /**
    00077  * \defgroup uipconffunc uIP configuration functions
    00078  * @{
    00079  *
    00080  * The uIP configuration functions are used for setting run-time
    00081  * parameters in uIP such as IP addresses.
    00082  */
    00083
    00084 /**
    00085  * Set the IP address of this host.
    00086  *
    00087  * The IP address is represented as a 4-byte array where the first
    00088  * octet of the IP address is put in the first member of the 4-byte
    00089  * array.
    00090  *
    00091  * Example:
    00092  \code
    00093
    00094  uip_ipaddr_t addr;
    00095
    00096  uip_ipaddr(&addr, 192,168,1,2);
    00097  uip_sethostaddr(&addr);
    00098
    00099  \endcode
    00100  * \param addr A pointer to an IP address of type uip_ipaddr_t;
    00101  *
    00102  * \sa uip_ipaddr()
    00103  *
    00104  * \hideinitializer
    00105  */
    00106 #define uip_sethostaddr(addr) uip_ipaddr_copy(uip_hostaddr, (addr))
    00107
    00108 /**
    00109  * Get the IP address of this host.
    00110  *
    00111  * The IP address is represented as a 4-byte array where the first
    00112  * octet of the IP address is put in the first member of the 4-byte
    00113  * array.
    00114  *
    00115  * Example:
    00116  \code
    00117  uip_ipaddr_t hostaddr;
    00118
    00119  uip_gethostaddr(&hostaddr);
    00120  \endcode
    00121  * \param addr A pointer to a uip_ipaddr_t variable that will be
    00122  * filled in with the currently configured IP address.
    00123  *
    00124  * \hideinitializer
    00125  */
    00126 #define uip_gethostaddr(addr) uip_ipaddr_copy((addr), uip_hostaddr)
    00127
    00128 /**
    00129  * Set the default router's IP address.
    00130  *
    00131  * \param addr A pointer to a uip_ipaddr_t variable containing the IP
    00132  * address of the default router.
    00133  *
    00134  * \sa uip_ipaddr()
    00135  *
    00136  * \hideinitializer
    00137  */
    00138 #define uip_setdraddr(addr) uip_ipaddr_copy(uip_draddr, (addr))
    00139
    00140 /**
    00141  * Set the netmask.
    00142  *
    00143  * \param addr A pointer to a uip_ipaddr_t variable containing the IP
    00144  * address of the netmask.
    00145  *
    00146  * \sa uip_ipaddr()
    00147  *
    00148  * \hideinitializer
    00149  */
    00150 #define uip_setnetmask(addr) uip_ipaddr_copy(uip_netmask, (addr))
    00151
    00152
    00153 /**
    00154  * Get the default router's IP address.
    00155  *
    00156  * \param addr A pointer to a uip_ipaddr_t variable that will be
    00157  * filled in with the IP address of the default router.
    00158  *
    00159  * \hideinitializer
    00160  */
    00161 #define uip_getdraddr(addr) uip_ipaddr_copy((addr), uip_draddr)
    00162
    00163 /**
    00164  * Get the netmask.
    00165  *
    00166  * \param addr A pointer to a uip_ipaddr_t variable that will be
    00167  * filled in with the value of the netmask.
    00168  *
    00169  * \hideinitializer
    00170  */
    00171 #define uip_getnetmask(addr) uip_ipaddr_copy((addr), uip_netmask)
    00172
    00173 /** @} */
    00174
    00175 /**
    00176  * \defgroup uipinit uIP initialization functions
    00177  * @{
    00178  *
    00179  * The uIP initialization functions are used for booting uIP.
    00180  */
    00181
    00182 /**
    00183  * uIP initialization function.
    00184  *
    00185  * This function should be called at boot up to initilize the uIP
    00186  * TCP/IP stack.
    00187  */
    00188 void uip_init(void);
    00189
    00190 /**
    00191  * uIP initialization function.
    00192  *
    00193  * This function may be used at boot time to set the initial ip_id.
    00194  */
    00195 void uip_setipid(u16_t id);
    00196
    00197 /** @} */
    00198
    00199 /**
    00200  * \defgroup uipdevfunc uIP device driver functions
    00201  * @{
    00202  *
    00203  * These functions are used by a network device driver for interacting
    00204  * with uIP.
    00205  */
    00206
    00207 /**
    00208  * Process an incoming packet.
    00209  *
    00210  * This function should be called when the device driver has received
    00211  * a packet from the network. The packet from the device driver must
    00212  * be present in the uip_buf buffer, and the length of the packet
    00213  * should be placed in the uip_len variable.
    00214  *
    00215  * When the function returns, there may be an outbound packet placed
    00216  * in the uip_buf packet buffer. If so, the uip_len variable is set to
    00217  * the length of the packet. If no packet is to be sent out, the
    00218  * uip_len variable is set to 0.
    00219  *
    00220  * The usual way of calling the function is presented by the source
    00221  * code below.
    00222  \code
    00223   uip_len = devicedriver_poll();
    00224   if(uip_len > 0) {
    00225     uip_input();
    00226     if(uip_len > 0) {
    00227       devicedriver_send();
    00228     }
    00229   }
    00230  \endcode
    00231  *
    00232  * \note If you are writing a uIP device driver that needs ARP
    00233  * (Address Resolution Protocol), e.g., when running uIP over
    00234  * Ethernet, you will need to call the uIP ARP code before calling
    00235  * this function:
    00236  \code
    00237   #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
    00238   uip_len = ethernet_devicedrver_poll();
    00239   if(uip_len > 0) {
    00240     if(BUF->type == HTONS(UIP_ETHTYPE_IP)) {
    00241       uip_arp_ipin();
    00242       uip_input();
    00243       if(uip_len > 0) {
    00244         uip_arp_out();
    00245         ethernet_devicedriver_send();
    00246       }
    00247     } else if(BUF->type == HTONS(UIP_ETHTYPE_ARP)) {
    00248       uip_arp_arpin();
    00249       if(uip_len > 0) {
    00250         ethernet_devicedriver_send();
    00251       }
    00252     }
    00253  \endcode
    00254  *
    00255  * \hideinitializer
    00256  */
    00257 #define uip_input()        uip_process(UIP_DATA)
    00258
    00259 /**
    00260  * Periodic processing for a connection identified by its number.
    00261  *
    00262  * This function does the necessary periodic processing (timers,
    00263  * polling) for a uIP TCP conneciton, and should be called when the
    00264  * periodic uIP timer goes off. It should be called for every
    00265  * connection, regardless of whether they are open of closed.
    00266  *
    00267  * When the function returns, it may have an outbound packet waiting
    00268  * for service in the uIP packet buffer, and if so the uip_len
    00269  * variable is set to a value larger than zero. The device driver
    00270  * should be called to send out the packet.
    00271  *
    00272  * The ususal way of calling the function is through a for() loop like
    00273  * this:
    00274  \code
    00275   for(i = 0; i < UIP_CONNS; ++i) {
    00276     uip_periodic(i);
    00277     if(uip_len > 0) {
    00278       devicedriver_send();
    00279     }
    00280   }
    00281  \endcode
    00282  *
    00283  * \note If you are writing a uIP device driver that needs ARP
    00284  * (Address Resolution Protocol), e.g., when running uIP over
    00285  * Ethernet, you will need to call the uip_arp_out() function before
    00286  * calling the device driver:
    00287  \code
    00288   for(i = 0; i < UIP_CONNS; ++i) {
    00289     uip_periodic(i);
    00290     if(uip_len > 0) {
    00291       uip_arp_out();
    00292       ethernet_devicedriver_send();
    00293     }
    00294   }
    00295  \endcode
    00296  *
    00297  * \param conn The number of the connection which is to be periodically polled.
    00298  *
    00299  * \hideinitializer
    00300  */
    00301 #define uip_periodic(conn) do { uip_conn = &uip_conns[conn]; \
    00302                                 uip_process(UIP_TIMER); } while (0)
    00303
    00304 /**
    00305  *
    00306  *
    00307  */
    00308 #define uip_conn_active(conn) (uip_conns[conn].tcpstateflags != UIP_CLOSED)
    00309
    00310 /**
    00311  * Perform periodic processing for a connection identified by a pointer
    00312  * to its structure.
    00313  *
    00314  * Same as uip_periodic() but takes a pointer to the actual uip_conn
    00315  * struct instead of an integer as its argument. This function can be
    00316  * used to force periodic processing of a specific connection.
    00317  *
    00318  * \param conn A pointer to the uip_conn struct for the connection to
    00319  * be processed.
    00320  *
    00321  * \hideinitializer
    00322  */
    00323 #define uip_periodic_conn(conn) do { uip_conn = conn; \
    00324                                      uip_process(UIP_TIMER); } while (0)
    00325
    00326 /**
    00327  * Reuqest that a particular connection should be polled.
    00328  *
    00329  * Similar to uip_periodic_conn() but does not perform any timer
    00330  * processing. The application is polled for new data.
    00331  *
    00332  * \param conn A pointer to the uip_conn struct for the connection to
    00333  * be processed.
    00334  *
    00335  * \hideinitializer
    00336  */
    00337 #define uip_poll_conn(conn) do { uip_conn = conn; \
    00338                                  uip_process(UIP_POLL_REQUEST); } while (0)
    00339
    00340
    00341 #if UIP_UDP
    00342 /**
    00343  * Periodic processing for a UDP connection identified by its number.
    00344  *
    00345  * This function is essentially the same as uip_periodic(), but for
    00346  * UDP connections. It is called in a similar fashion as the
    00347  * uip_periodic() function:
    00348  \code
    00349   for(i = 0; i < UIP_UDP_CONNS; i++) {
    00350     uip_udp_periodic(i);
    00351     if(uip_len > 0) {
    00352       devicedriver_send();
    00353     }
    00354   }
    00355  \endcode
    00356  *
    00357  * \note As for the uip_periodic() function, special care has to be
    00358  * taken when using uIP together with ARP and Ethernet:
    00359  \code
    00360   for(i = 0; i < UIP_UDP_CONNS; i++) {
    00361     uip_udp_periodic(i);
    00362     if(uip_len > 0) {
    00363       uip_arp_out();
    00364       ethernet_devicedriver_send();
    00365     }
    00366   }
    00367  \endcode
    00368  *
    00369  * \param conn The number of the UDP connection to be processed.
    00370  *
    00371  * \hideinitializer
    00372  */
    00373 #define uip_udp_periodic(conn) do { uip_udp_conn = &uip_udp_conns[conn]; \
    00374                                 uip_process(UIP_UDP_TIMER); } while (0)
    00375
    00376 /**
    00377  * Periodic processing for a UDP connection identified by a pointer to
    00378  * its structure.
    00379  *
    00380  * Same as uip_udp_periodic() but takes a pointer to the actual
    00381  * uip_conn struct instead of an integer as its argument. This
    00382  * function can be used to force periodic processing of a specific
    00383  * connection.
    00384  *
    00385  * \param conn A pointer to the uip_udp_conn struct for the connection
    00386  * to be processed.
    00387  *
    00388  * \hideinitializer
    00389  */
    00390 #define uip_udp_periodic_conn(conn) do { uip_udp_conn = conn; \
    00391                                          uip_process(UIP_UDP_TIMER); } while (0)
    00392
    00393
    00394 #endif /* UIP_UDP */
    00395
    00396 /**
    00397  * The uIP packet buffer.
    00398  *
    00399  * The uip_buf array is used to hold incoming and outgoing
    00400  * packets. The device driver should place incoming data into this
    00401  * buffer. When sending data, the device driver should read the link
    00402  * level headers and the TCP/IP headers from this buffer. The size of
    00403  * the link level headers is configured by the UIP_LLH_LEN define.
    00404  *
    00405  * \note The application data need not be placed in this buffer, so
    00406  * the device driver must read it from the place pointed to by the
    00407  * uip_appdata pointer as illustrated by the following example:
    00408  \code
    00409  void
    00410  devicedriver_send(void)
    00411  {
    00412     hwsend(&uip_buf[0], UIP_LLH_LEN);
    00413     if(uip_len <= UIP_LLH_LEN + UIP_TCPIP_HLEN) {
    00414       hwsend(&uip_buf[UIP_LLH_LEN], uip_len - UIP_LLH_LEN);
    00415     } else {
    00416       hwsend(&uip_buf[UIP_LLH_LEN], UIP_TCPIP_HLEN);
    00417       hwsend(uip_appdata, uip_len - UIP_TCPIP_HLEN - UIP_LLH_LEN);
    00418     }
    00419  }
    00420  \endcode
    00421  */
    00422 extern u8_t uip_buf[UIP_BUFSIZE+2];
    00423
    00424 /** @} */
    00425
    00426 /*---------------------------------------------------------------------------*/
    00427 /* Functions that are used by the uIP application program. Opening and
    00428  * closing connections, sending and receiving data, etc. is all
    00429  * handled by the functions below.
    00430 */
    00431 /**
    00432  * \defgroup uipappfunc uIP application functions
    00433  * @{
    00434  *
    00435  * Functions used by an application running of top of uIP.
    00436  */
    00437
    00438 /**
    00439  * Start listening to the specified port.
    00440  *
    00441  * \note Since this function expects the port number in network byte
    00442  * order, a conversion using HTONS() or htons() is necessary.
    00443  *
    00444  \code
    00445  uip_listen(HTONS(80));
    00446  \endcode
    00447  *
    00448  * \param port A 16-bit port number in network byte order.
    00449  */
    00450 void uip_listen(u16_t port);
    00451
    00452 /**
    00453  * Stop listening to the specified port.
    00454  *
    00455  * \note Since this function expects the port number in network byte
    00456  * order, a conversion using HTONS() or htons() is necessary.
    00457  *
    00458  \code
    00459  uip_unlisten(HTONS(80));
    00460  \endcode
    00461  *
    00462  * \param port A 16-bit port number in network byte order.
    00463  */
    00464 void uip_unlisten(u16_t port);
    00465
    00466 /**
    00467  * Connect to a remote host using TCP.
    00468  *
    00469  * This function is used to start a new connection to the specified
    00470  * port on the specied host. It allocates a new connection identifier,
    00471  * sets the connection to the SYN_SENT state and sets the
    00472  * retransmission timer to 0. This will cause a TCP SYN segment to be
    00473  * sent out the next time this connection is periodically processed,
    00474  * which usually is done within 0.5 seconds after the call to
    00475  * uip_connect().
    00476  *
    00477  * \note This function is avaliable only if support for active open
    00478  * has been configured by defining UIP_ACTIVE_OPEN to 1 in uipopt.h.
    00479  *
    00480  * \note Since this function requires the port number to be in network
    00481  * byte order, a conversion using HTONS() or htons() is necessary.
    00482  *
    00483  \code
    00484  uip_ipaddr_t ipaddr;
    00485
    00486  uip_ipaddr(&ipaddr, 192,168,1,2);
    00487  uip_connect(&ipaddr, HTONS(80));
    00488  \endcode
    00489  *
    00490  * \param ripaddr The IP address of the remote hot.
    00491  *
    00492  * \param port A 16-bit port number in network byte order.
    00493  *
    00494  * \return A pointer to the uIP connection identifier for the new connection,
    00495  * or NULL if no connection could be allocated.
    00496  *
    00497  */
    00498 struct uip_conn *uip_connect(uip_ipaddr_t *ripaddr, u16_t port);
    00499
    00500
    00501
    00502 /**
    00503  * \internal
    00504  *
    00505  * Check if a connection has outstanding (i.e., unacknowledged) data.
    00506  *
    00507  * \param conn A pointer to the uip_conn structure for the connection.
    00508  *
    00509  * \hideinitializer
    00510  */
    00511 #define uip_outstanding(conn) ((conn)->len)
    00512
    00513 /**
    00514  * Send data on the current connection.
    00515  *
    00516  * This function is used to send out a single segment of TCP
    00517  * data. Only applications that have been invoked by uIP for event
    00518  * processing can send data.
    00519  *
    00520  * The amount of data that actually is sent out after a call to this
    00521  * funcion is determined by the maximum amount of data TCP allows. uIP
    00522  * will automatically crop the data so that only the appropriate
    00523  * amount of data is sent. The function uip_mss() can be used to query
    00524  * uIP for the amount of data that actually will be sent.
    00525  *
    00526  * \note This function does not guarantee that the sent data will
    00527  * arrive at the destination. If the data is lost in the network, the
    00528  * application will be invoked with the uip_rexmit() event being
    00529  * set. The application will then have to resend the data using this
    00530  * function.
    00531  *
    00532  * \param data A pointer to the data which is to be sent.
    00533  *
    00534  * \param len The maximum amount of data bytes to be sent.
    00535  *
    00536  * \hideinitializer
    00537  */
    00538 void uip_send(const void *data, int len);
    00539
    00540 /**
    00541  * The length of any incoming data that is currently avaliable (if avaliable)
    00542  * in the uip_appdata buffer.
    00543  *
    00544  * The test function uip_data() must first be used to check if there
    00545  * is any data available at all.
    00546  *
    00547  * \hideinitializer
    00548  */
    00549 /*void uip_datalen(void);*/
    00550 #define uip_datalen()       uip_len
    00551
    00552 /**
    00553  * The length of any out-of-band data (urgent data) that has arrived
    00554  * on the connection.
    00555  *
    00556  * \note The configuration parameter UIP_URGDATA must be set for this
    00557  * function to be enabled.
    00558  *
    00559  * \hideinitializer
    00560  */
    00561 #define uip_urgdatalen()    uip_urglen
    00562
    00563 /**
    00564  * Close the current connection.
    00565  *
    00566  * This function will close the current connection in a nice way.
    00567  *
    00568  * \hideinitializer
    00569  */
    00570 #define uip_close()         (uip_flags = UIP_CLOSE)
    00571
    00572 /**
    00573  * Abort the current connection.
    00574  *
    00575  * This function will abort (reset) the current connection, and is
    00576  * usually used when an error has occured that prevents using the
    00577  * uip_close() function.
    00578  *
    00579  * \hideinitializer
    00580  */
    00581 #define uip_abort()         (uip_flags = UIP_ABORT)
    00582
    00583 /**
    00584  * Tell the sending host to stop sending data.
    00585  *
    00586  * This function will close our receiver's window so that we stop
    00587  * receiving data for the current connection.
    00588  *
    00589  * \hideinitializer
    00590  */
    00591 #define uip_stop()          (uip_conn->tcpstateflags |= UIP_STOPPED)
    00592
    00593 /**
    00594  * Find out if the current connection has been previously stopped with
    00595  * uip_stop().
    00596  *
    00597  * \hideinitializer
    00598  */
    00599 #define uip_stopped(conn)   ((conn)->tcpstateflags & UIP_STOPPED)
    00600
    00601 /**
    00602  * Restart the current connection, if is has previously been stopped
    00603  * with uip_stop().
    00604  *
    00605  * This function will open the receiver's window again so that we
    00606  * start receiving data for the current connection.
    00607  *
    00608  * \hideinitializer
    00609  */
    00610 #define uip_restart()         do { uip_flags |= UIP_NEWDATA; \
    00611                                    uip_conn->tcpstateflags &= ~UIP_STOPPED; \
    00612                               } while(0)
    00613
    00614
    00615 /* uIP tests that can be made to determine in what state the current
    00616    connection is, and what the application function should do. */
    00617
    00618 /**
    00619  * Is the current connection a UDP connection?
    00620  *
    00621  * This function checks whether the current connection is a UDP connection.
    00622  *
    00623  * \hideinitializer
    00624  *
    00625  */
    00626 #define uip_udpconnection() (uip_conn == NULL)
    00627
    00628 /**
    00629  * Is new incoming data available?
    00630  *
    00631  * Will reduce to non-zero if there is new data for the application
    00632  * present at the uip_appdata pointer. The size of the data is
    00633  * avaliable through the uip_len variable.
    00634  *
    00635  * \hideinitializer
    00636  */
    00637 #define uip_newdata()   (uip_flags & UIP_NEWDATA)
    00638
    00639 /**
    00640  * Has previously sent data been acknowledged?
    00641  *
    00642  * Will reduce to non-zero if the previously sent data has been
    00643  * acknowledged by the remote host. This means that the application
    00644  * can send new data.
    00645  *
    00646  * \hideinitializer
    00647  */
    00648 #define uip_acked()   (uip_flags & UIP_ACKDATA)
    00649
    00650 /**
    00651  * Has the connection just been connected?
    00652  *
    00653  * Reduces to non-zero if the current connection has been connected to
    00654  * a remote host. This will happen both if the connection has been
    00655  * actively opened (with uip_connect()) or passively opened (with
    00656  * uip_listen()).
    00657  *
    00658  * \hideinitializer
    00659  */
    00660 #define uip_connected() (uip_flags & UIP_CONNECTED)
    00661
    00662 /**
    00663  * Has the connection been closed by the other end?
    00664  *
    00665  * Is non-zero if the connection has been closed by the remote
    00666  * host. The application may then do the necessary clean-ups.
    00667  *
    00668  * \hideinitializer
    00669  */
    00670 #define uip_closed()    (uip_flags & UIP_CLOSE)
    00671
    00672 /**
    00673  * Has the connection been aborted by the other end?
    00674  *
    00675  * Non-zero if the current connection has been aborted (reset) by the
    00676  * remote host.
    00677  *
    00678  * \hideinitializer
    00679  */
    00680 #define uip_aborted()    (uip_flags & UIP_ABORT)
    00681
    00682 /**
    00683  * Has the connection timed out?
    00684  *
    00685  * Non-zero if the current connection has been aborted due to too many
    00686  * retransmissions.
    00687  *
    00688  * \hideinitializer
    00689  */
    00690 #define uip_timedout()    (uip_flags & UIP_TIMEDOUT)
    00691
    00692 /**
    00693  * Do we need to retransmit previously data?
    00694  *
    00695  * Reduces to non-zero if the previously sent data has been lost in
    00696  * the network, and the application should retransmit it. The
    00697  * application should send the exact same data as it did the last
    00698  * time, using the uip_send() function.
    00699  *
    00700  * \hideinitializer
    00701  */
    00702 #define uip_rexmit()     (uip_flags & UIP_REXMIT)
    00703
    00704 /**
    00705  * Is the connection being polled by uIP?
    00706  *
    00707  * Is non-zero if the reason the application is invoked is that the
    00708  * current connection has been idle for a while and should be
    00709  * polled.
    00710  *
    00711  * The polling event can be used for sending data without having to
    00712  * wait for the remote host to send data.
    00713  *
    00714  * \hideinitializer
    00715  */
    00716 #define uip_poll()       (uip_flags & UIP_POLL)
    00717
    00718 /**
    00719  * Get the initial maxium segment size (MSS) of the current
    00720  * connection.
    00721  *
    00722  * \hideinitializer
    00723  */
    00724 #define uip_initialmss()             (uip_conn->initialmss)
    00725
    00726 /**
    00727  * Get the current maxium segment size that can be sent on the current
    00728  * connection.
    00729  *
    00730  * The current maxiumum segment size that can be sent on the
    00731  * connection is computed from the receiver's window and the MSS of
    00732  * the connection (which also is available by calling
    00733  * uip_initialmss()).
    00734  *
    00735  * \hideinitializer
    00736  */
    00737 #define uip_mss()             (uip_conn->mss)
    00738
    00739 /**
    00740  * Set up a new UDP connection.
    00741  *
    00742  * This function sets up a new UDP connection. The function will
    00743  * automatically allocate an unused local port for the new
    00744  * connection. However, another port can be chosen by using the
    00745  * uip_udp_bind() call, after the uip_udp_new() function has been
    00746  * called.
    00747  *
    00748  * Example:
    00749  \code
    00750  uip_ipaddr_t addr;
    00751  struct uip_udp_conn *c;
    00752
    00753  uip_ipaddr(&addr, 192,168,2,1);
    00754  c = uip_udp_new(&addr, HTONS(12345));
    00755  if(c != NULL) {
    00756    uip_udp_bind(c, HTONS(12344));
    00757  }
    00758  \endcode
    00759  * \param ripaddr The IP address of the remote host.
    00760  *
    00761  * \param rport The remote port number in network byte order.
    00762  *
    00763  * \return The uip_udp_conn structure for the new connection or NULL
    00764  * if no connection could be allocated.
    00765  */
    00766 struct uip_udp_conn *uip_udp_new(uip_ipaddr_t *ripaddr, u16_t rport);
    00767
    00768 /**
    00769  * Removed a UDP connection.
    00770  *
    00771  * \param conn A pointer to the uip_udp_conn structure for the connection.
    00772  *
    00773  * \hideinitializer
    00774  */
    00775 #define uip_udp_remove(conn) (conn)->lport = 0
    00776
    00777 /**
    00778  * Bind a UDP connection to a local port.
    00779  *
    00780  * \param conn A pointer to the uip_udp_conn structure for the
    00781  * connection.
    00782  *
    00783  * \param port The local port number, in network byte order.
    00784  *
    00785  * \hideinitializer
    00786  */
    00787 #define uip_udp_bind(conn, port) (conn)->lport = port
    00788
    00789 /**
    00790  * Send a UDP datagram of length len on the current connection.
    00791  *
    00792  * This function can only be called in response to a UDP event (poll
    00793  * or newdata). The data must be present in the uip_buf buffer, at the
    00794  * place pointed to by the uip_appdata pointer.
    00795  *
    00796  * \param len The length of the data in the uip_buf buffer.
    00797  *
    00798  * \hideinitializer
    00799  */
    00800 #define uip_udp_send(len) uip_send((char *)uip_appdata, len)
    00801
    00802 /** @} */
    00803
    00804 /* uIP convenience and converting functions. */
    00805
    00806 /**
    00807  * \defgroup uipconvfunc uIP conversion functions
    00808  * @{
    00809  *
    00810  * These functions can be used for converting between different data
    00811  * formats used by uIP.
    00812  */
    00813
    00814 /**
    00815  * Construct an IP address from four bytes.
    00816  *
    00817  * This function constructs an IP address of the type that uIP handles
    00818  * internally from four bytes. The function is handy for specifying IP
    00819  * addresses to use with e.g. the uip_connect() function.
    00820  *
    00821  * Example:
    00822  \code
    00823  uip_ipaddr_t ipaddr;
    00824  struct uip_conn *c;
    00825
    00826  uip_ipaddr(&ipaddr, 192,168,1,2);
    00827  c = uip_connect(&ipaddr, HTONS(80));
    00828  \endcode
    00829  *
    00830  * \param addr A pointer to a uip_ipaddr_t variable that will be
    00831  * filled in with the IP address.
    00832  *
    00833  * \param addr0 The first octet of the IP address.
    00834  * \param addr1 The second octet of the IP address.
    00835  * \param addr2 The third octet of the IP address.
    00836  * \param addr3 The forth octet of the IP address.
    00837  *
    00838  * \hideinitializer
    00839  */
    00840 #define uip_ipaddr(addr, addr0,addr1,addr2,addr3) do { \
    00841                      ((u16_t *)(addr))[0] = HTONS(((addr0) << 8) | (addr1)); \
    00842                      ((u16_t *)(addr))[1] = HTONS(((addr2) << 8) | (addr3)); \
    00843                   } while(0)
    00844
    00845 /**
    00846  * Construct an IPv6 address from eight 16-bit words.
    00847  *
    00848  * This function constructs an IPv6 address.
    00849  *
    00850  * \hideinitializer
    00851  */
    00852 #define uip_ip6addr(addr, addr0,addr1,addr2,addr3,addr4,addr5,addr6,addr7) do { \
    00853                      ((u16_t *)(addr))[0] = HTONS((addr0)); \
    00854                      ((u16_t *)(addr))[1] = HTONS((addr1)); \
    00855                      ((u16_t *)(addr))[2] = HTONS((addr2)); \
    00856                      ((u16_t *)(addr))[3] = HTONS((addr3)); \
    00857                      ((u16_t *)(addr))[4] = HTONS((addr4)); \
    00858                      ((u16_t *)(addr))[5] = HTONS((addr5)); \
    00859                      ((u16_t *)(addr))[6] = HTONS((addr6)); \
    00860                      ((u16_t *)(addr))[7] = HTONS((addr7)); \
    00861                   } while(0)
    00862
    00863 /**
    00864  * Copy an IP address to another IP address.
    00865  *
    00866  * Copies an IP address from one place to another.
    00867  *
    00868  * Example:
    00869  \code
    00870  uip_ipaddr_t ipaddr1, ipaddr2;
    00871
    00872  uip_ipaddr(&ipaddr1, 192,16,1,2);
    00873  uip_ipaddr_copy(&ipaddr2, &ipaddr1);
    00874  \endcode
    00875  *
    00876  * \param dest The destination for the copy.
    00877  * \param src The source from where to copy.
    00878  *
    00879  * \hideinitializer
    00880  */
    00881 #if !UIP_CONF_IPV6
    00882 #define uip_ipaddr_copy(dest, src) do { \
    00883                      ((u16_t *)dest)[0] = ((u16_t *)src)[0]; \
    00884                      ((u16_t *)dest)[1] = ((u16_t *)src)[1]; \
    00885                   } while(0)
    00886 #else /* !UIP_CONF_IPV6 */
    00887 #define uip_ipaddr_copy(dest, src) memcpy(dest, src, sizeof(uip_ip6addr_t))
    00888 #endif /* !UIP_CONF_IPV6 */
    00889
    00890 /**
    00891  * Compare two IP addresses
    00892  *
    00893  * Compares two IP addresses.
    00894  *
    00895  * Example:
    00896  \code
    00897  uip_ipaddr_t ipaddr1, ipaddr2;
    00898
    00899  uip_ipaddr(&ipaddr1, 192,16,1,2);
    00900  if(uip_ipaddr_cmp(&ipaddr2, &ipaddr1)) {
    00901     printf("They are the same");
    00902  }
    00903  \endcode
    00904  *
    00905  * \param addr1 The first IP address.
    00906  * \param addr2 The second IP address.
    00907  *
    00908  * \hideinitializer
    00909  */
    00910 #if !UIP_CONF_IPV6
    00911 #define uip_ipaddr_cmp(addr1, addr2) (((u16_t *)addr1)[0] == ((u16_t *)addr2)[0] && \
    00912                                       ((u16_t *)addr1)[1] == ((u16_t *)addr2)[1])
    00913 #else /* !UIP_CONF_IPV6 */
    00914 #define uip_ipaddr_cmp(addr1, addr2) (memcmp(addr1, addr2, sizeof(uip_ip6addr_t)) == 0)
    00915 #endif /* !UIP_CONF_IPV6 */
    00916
    00917 /**
    00918  * Compare two IP addresses with netmasks
    00919  *
    00920  * Compares two IP addresses with netmasks. The masks are used to mask
    00921  * out the bits that are to be compared.
    00922  *
    00923  * Example:
    00924  \code
    00925  uip_ipaddr_t ipaddr1, ipaddr2, mask;
    00926
    00927  uip_ipaddr(&mask, 255,255,255,0);
    00928  uip_ipaddr(&ipaddr1, 192,16,1,2);
    00929  uip_ipaddr(&ipaddr2, 192,16,1,3);
    00930  if(uip_ipaddr_maskcmp(&ipaddr1, &ipaddr2, &mask)) {
    00931     printf("They are the same");
    00932  }
    00933  \endcode
    00934  *
    00935  * \param addr1 The first IP address.
    00936  * \param addr2 The second IP address.
    00937  * \param mask The netmask.
    00938  *
    00939  * \hideinitializer
    00940  */
    00941 #define uip_ipaddr_maskcmp(addr1, addr2, mask) \
    00942                           (((((u16_t *)addr1)[0] & ((u16_t *)mask)[0]) == \
    00943                             (((u16_t *)addr2)[0] & ((u16_t *)mask)[0])) && \
    00944                            ((((u16_t *)addr1)[1] & ((u16_t *)mask)[1]) == \
    00945                             (((u16_t *)addr2)[1] & ((u16_t *)mask)[1])))
    00946
    00947
    00948 /**
    00949  * Mask out the network part of an IP address.
    00950  *
    00951  * Masks out the network part of an IP address, given the address and
    00952  * the netmask.
    00953  *
    00954  * Example:
    00955  \code
    00956  uip_ipaddr_t ipaddr1, ipaddr2, netmask;
    00957
    00958  uip_ipaddr(&ipaddr1, 192,16,1,2);
    00959  uip_ipaddr(&netmask, 255,255,255,0);
    00960  uip_ipaddr_mask(&ipaddr2, &ipaddr1, &netmask);
    00961  \endcode
    00962  *
    00963  * In the example above, the variable "ipaddr2" will contain the IP
    00964  * address 192.168.1.0.
    00965  *
    00966  * \param dest Where the result is to be placed.
    00967  * \param src The IP address.
    00968  * \param mask The netmask.
    00969  *
    00970  * \hideinitializer
    00971  */
    00972 #define uip_ipaddr_mask(dest, src, mask) do { \
    00973                      ((u16_t *)dest)[0] = ((u16_t *)src)[0] & ((u16_t *)mask)[0]; \
    00974                      ((u16_t *)dest)[1] = ((u16_t *)src)[1] & ((u16_t *)mask)[1]; \
    00975                   } while(0)
    00976
    00977 /**
    00978  * Pick the first octet of an IP address.
    00979  *
    00980  * Picks out the first octet of an IP address.
    00981  *
    00982  * Example:
    00983  \code
    00984  uip_ipaddr_t ipaddr;
    00985  u8_t octet;
    00986
    00987  uip_ipaddr(&ipaddr, 1,2,3,4);
    00988  octet = uip_ipaddr1(&ipaddr);
    00989  \endcode
    00990  *
    00991  * In the example above, the variable "octet" will contain the value 1.
    00992  *
    00993  * \hideinitializer
    00994  */
    00995 #define uip_ipaddr1(addr) (htons(((u16_t *)(addr))[0]) >> 8)
    00996
    00997 /**
    00998  * Pick the second octet of an IP address.
    00999  *
    01000  * Picks out the second octet of an IP address.
    01001  *
    01002  * Example:
    01003  \code
    01004  uip_ipaddr_t ipaddr;
    01005  u8_t octet;
    01006
    01007  uip_ipaddr(&ipaddr, 1,2,3,4);
    01008  octet = uip_ipaddr2(&ipaddr);
    01009  \endcode
    01010  *
    01011  * In the example above, the variable "octet" will contain the value 2.
    01012  *
    01013  * \hideinitializer
    01014  */
    01015 #define uip_ipaddr2(addr) (htons(((u16_t *)(addr))[0]) & 0xff)
    01016
    01017 /**
    01018  * Pick the third octet of an IP address.
    01019  *
    01020  * Picks out the third octet of an IP address.
    01021  *
    01022  * Example:
    01023  \code
    01024  uip_ipaddr_t ipaddr;
    01025  u8_t octet;
    01026
    01027  uip_ipaddr(&ipaddr, 1,2,3,4);
    01028  octet = uip_ipaddr3(&ipaddr);
    01029  \endcode
    01030  *
    01031  * In the example above, the variable "octet" will contain the value 3.
    01032  *
    01033  * \hideinitializer
    01034  */
    01035 #define uip_ipaddr3(addr) (htons(((u16_t *)(addr))[1]) >> 8)
    01036
    01037 /**
    01038  * Pick the fourth octet of an IP address.
    01039  *
    01040  * Picks out the fourth octet of an IP address.
    01041  *
    01042  * Example:
    01043  \code
    01044  uip_ipaddr_t ipaddr;
    01045  u8_t octet;
    01046
    01047  uip_ipaddr(&ipaddr, 1,2,3,4);
    01048  octet = uip_ipaddr4(&ipaddr);
    01049  \endcode
    01050  *
    01051  * In the example above, the variable "octet" will contain the value 4.
    01052  *
    01053  * \hideinitializer
    01054  */
    01055 #define uip_ipaddr4(addr) (htons(((u16_t *)(addr))[1]) & 0xff)
    01056
    01057 /**
    01058  * Convert 16-bit quantity from host byte order to network byte order.
    01059  *
    01060  * This macro is primarily used for converting constants from host
    01061  * byte order to network byte order. For converting variables to
    01062  * network byte order, use the htons() function instead.
    01063  *
    01064  * \hideinitializer
    01065  */
    01066 #ifndef HTONS
    01067 #   if UIP_BYTE_ORDER == UIP_BIG_ENDIAN
    01068 #      define HTONS(n) (n)
    01069 #   else /* UIP_BYTE_ORDER == UIP_BIG_ENDIAN */
    01070 #      define HTONS(n) (u16_t)((((u16_t) (n)) << 8) | (((u16_t) (n)) >> 8))
    01071 #   endif /* UIP_BYTE_ORDER == UIP_BIG_ENDIAN */
    01072 #else
    01073 #error "HTONS already defined!"
    01074 #endif /* HTONS */
    01075
    01076 /**
    01077  * Convert 16-bit quantity from host byte order to network byte order.
    01078  *
    01079  * This function is primarily used for converting variables from host
    01080  * byte order to network byte order. For converting constants to
    01081  * network byte order, use the HTONS() macro instead.
    01082  */
    01083 #ifndef htons
    01084 u16_t htons(u16_t val);
    01085 #endif /* htons */
    01086 #ifndef ntohs
    01087 #define ntohs htons
    01088 #endif
    01089
    01090 /** @} */
    01091
    01092 /**
    01093  * Pointer to the application data in the packet buffer.
    01094  *
    01095  * This pointer points to the application data when the application is
    01096  * called. If the application wishes to send data, the application may
    01097  * use this space to write the data into before calling uip_send().
    01098  */
    01099 extern void *uip_appdata;
    01100
    01101 #if UIP_URGDATA > 0
    01102 /* u8_t *uip_urgdata:
    01103  *
    01104  * This pointer points to any urgent data that has been received. Only
    01105  * present if compiled with support for urgent data (UIP_URGDATA).
    01106  */
    01107 extern void *uip_urgdata;
    01108 #endif /* UIP_URGDATA > 0 */
    01109
    01110
    01111 /**
    01112  * \defgroup uipdrivervars Variables used in uIP device drivers
    01113  * @{
    01114  *
    01115  * uIP has a few global variables that are used in device drivers for
    01116  * uIP.
    01117  */
    01118
    01119 /**
    01120  * The length of the packet in the uip_buf buffer.
    01121  *
    01122  * The global variable uip_len holds the length of the packet in the
    01123  * uip_buf buffer.
    01124  *
    01125  * When the network device driver calls the uIP input function,
    01126  * uip_len should be set to the length of the packet in the uip_buf
    01127  * buffer.
    01128  *
    01129  * When sending packets, the device driver should use the contents of
    01130  * the uip_len variable to determine the length of the outgoing
    01131  * packet.
    01132  *
    01133  */
    01134 extern u16_t uip_len;
    01135
    01136 /** @} */
    01137
    01138 #if UIP_URGDATA > 0
    01139 extern u16_t uip_urglen, uip_surglen;
    01140 #endif /* UIP_URGDATA > 0 */
    01141
    01142
    01143 /**
    01144  * Representation of a uIP TCP connection.
    01145  *
    01146  * The uip_conn structure is used for identifying a connection. All
    01147  * but one field in the structure are to be considered read-only by an
    01148  * application. The only exception is the appstate field whos purpose
    01149  * is to let the application store application-specific state (e.g.,
    01150  * file pointers) for the connection. The type of this field is
    01151  * configured in the "uipopt.h" header file.
    01152  */
    01153 struct uip_conn {
    01154   uip_ipaddr_t ripaddr;   /**< The IP address of the remote host. */
    01155
    01156   u16_t lport;        /**< The local TCP port, in network byte order. */
    01157   u16_t rport;        /**< The local remote TCP port, in network byte
    01158                          order. */
    01159
    01160   u8_t rcv_nxt[4];    /**< The sequence number that we expect to
    01161                          receive next. */
    01162   u8_t snd_nxt[4];    /**< The sequence number that was last sent by
    01163                          us. */
    01164   u16_t len;          /**< Length of the data that was previously sent. */
    01165   u16_t mss;          /**< Current maximum segment size for the
    01166                          connection. */
    01167   u16_t initialmss;   /**< Initial maximum segment size for the
    01168                          connection. */
    01169   u8_t sa;            /**< Retransmission time-out calculation state
    01170                          variable. */
    01171   u8_t sv;            /**< Retransmission time-out calculation state
    01172                          variable. */
    01173   u8_t rto;           /**< Retransmission time-out. */
    01174   u8_t tcpstateflags; /**< TCP state and flags. */
    01175   u8_t timer;         /**< The retransmission timer. */
    01176   u8_t nrtx;          /**< The number of retransmissions for the last
    01177                          segment sent. */
    01178
    01179   /** The application state. */
    01180   uip_tcp_appstate_t appstate;
    01181 };
    01182
    01183
    01184 /**
    01185  * Pointer to the current TCP connection.
    01186  *
    01187  * The uip_conn pointer can be used to access the current TCP
    01188  * connection.
    01189  */
    01190 extern struct uip_conn *uip_conn;
    01191 /* The array containing all uIP connections. */
    01192 extern struct uip_conn uip_conns[UIP_CONNS];
    01193 /**
    01194  * \addtogroup uiparch
    01195  * @{
    01196  */
    01197
    01198 /**
    01199  * 4-byte array used for the 32-bit sequence number calculations.
    01200  */
    01201 extern u8_t uip_acc32[4];
    01202
    01203 /** @} */
    01204
    01205
    01206 #if UIP_UDP
    01207 /**
    01208  * Representation of a uIP UDP connection.
    01209  */
    01210 struct uip_udp_conn {
    01211   uip_ipaddr_t ripaddr;   /**< The IP address of the remote peer. */
    01212   u16_t lport;        /**< The local port number in network byte order. */
    01213   u16_t rport;        /**< The remote port number in network byte order. */
    01214   u8_t  ttl;          /**< Default time-to-live. */
    01215
    01216   /** The application state. */
    01217   uip_udp_appstate_t appstate;
    01218 };
    01219
    01220 /**
    01221  * The current UDP connection.
    01222  */
    01223 extern struct uip_udp_conn *uip_udp_conn;
    01224 extern struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];
    01225 #endif /* UIP_UDP */
    01226
    01227 /**
    01228  * The structure holding the TCP/IP statistics that are gathered if
    01229  * UIP_STATISTICS is set to 1.
    01230  *
    01231  */
    01232 struct uip_stats {
    01233   struct {
    01234     uip_stats_t drop;     /**< Number of dropped packets at the IP
    01235                              layer. */
    01236     uip_stats_t recv;     /**< Number of received packets at the IP
    01237                              layer. */
    01238     uip_stats_t sent;     /**< Number of sent packets at the IP
    01239                              layer. */
    01240     uip_stats_t vhlerr;   /**< Number of packets dropped due to wrong
    01241                              IP version or header length. */
    01242     uip_stats_t hblenerr; /**< Number of packets dropped due to wrong
    01243                              IP length, high byte. */
    01244     uip_stats_t lblenerr; /**< Number of packets dropped due to wrong
    01245                              IP length, low byte. */
    01246     uip_stats_t fragerr;  /**< Number of packets dropped since they
    01247                              were IP fragments. */
    01248     uip_stats_t chkerr;   /**< Number of packets dropped due to IP
    01249                              checksum errors. */
    01250     uip_stats_t protoerr; /**< Number of packets dropped since they
    01251                              were neither ICMP, UDP nor TCP. */
    01252   } ip;                   /**< IP statistics. */
    01253   struct {
    01254     uip_stats_t drop;     /**< Number of dropped ICMP packets. */
    01255     uip_stats_t recv;     /**< Number of received ICMP packets. */
    01256     uip_stats_t sent;     /**< Number of sent ICMP packets. */
    01257     uip_stats_t typeerr;  /**< Number of ICMP packets with a wrong
    01258                              type. */
    01259   } icmp;                 /**< ICMP statistics. */
    01260   struct {
    01261     uip_stats_t drop;     /**< Number of dropped TCP segments. */
    01262     uip_stats_t recv;     /**< Number of recived TCP segments. */
    01263     uip_stats_t sent;     /**< Number of sent TCP segments. */
    01264     uip_stats_t chkerr;   /**< Number of TCP segments with a bad
    01265                              checksum. */
    01266     uip_stats_t ackerr;   /**< Number of TCP segments with a bad ACK
    01267                              number. */
    01268     uip_stats_t rst;      /**< Number of recevied TCP RST (reset) segments. */
    01269     uip_stats_t rexmit;   /**< Number of retransmitted TCP segments. */
    01270     uip_stats_t syndrop;  /**< Number of dropped SYNs due to too few
    01271                              connections was avaliable. */
    01272     uip_stats_t synrst;   /**< Number of SYNs for closed ports,
    01273                              triggering a RST. */
    01274   } tcp;                  /**< TCP statistics. */
    01275 #if UIP_UDP
    01276   struct {
    01277     uip_stats_t drop;     /**< Number of dropped UDP segments. */
    01278     uip_stats_t recv;     /**< Number of recived UDP segments. */
    01279     uip_stats_t sent;     /**< Number of sent UDP segments. */
    01280     uip_stats_t chkerr;   /**< Number of UDP segments with a bad
    01281                              checksum. */
    01282   } udp;                  /**< UDP statistics. */
    01283 #endif /* UIP_UDP */
    01284 };
    01285
    01286 /**
    01287  * The uIP TCP/IP statistics.
    01288  *
    01289  * This is the variable in which the uIP TCP/IP statistics are gathered.
    01290  */
    01291 extern struct uip_stats uip_stat;
    01292
    01293
    01294 /*---------------------------------------------------------------------------*/
    01295 /* All the stuff below this point is internal to uIP and should not be
    01296  * used directly by an application or by a device driver.
    01297  */
    01298 /*---------------------------------------------------------------------------*/
    01299 /* u8_t uip_flags:
    01300  *
    01301  * When the application is called, uip_flags will contain the flags
    01302  * that are defined in this file. Please read below for more
    01303  * infomation.
    01304  */
    01305 extern u8_t uip_flags;
    01306
    01307 /* The following flags may be set in the global variable uip_flags
    01308    before calling the application callback. The UIP_ACKDATA,
    01309    UIP_NEWDATA, and UIP_CLOSE flags may both be set at the same time,
    01310    whereas the others are mutualy exclusive. Note that these flags
    01311    should *NOT* be accessed directly, but only through the uIP
    01312    functions/macros. */
    01313
    01314 #define UIP_ACKDATA   1     /* Signifies that the outstanding data was
    01315                                acked and the application should send
    01316                                out new data instead of retransmitting
    01317                                the last data. */
    01318 #define UIP_NEWDATA   2     /* Flags the fact that the peer has sent
    01319                                us new data. */
    01320 #define UIP_REXMIT    4     /* Tells the application to retransmit the
    01321                                data that was last sent. */
    01322 #define UIP_POLL      8     /* Used for polling the application, to
    01323                                check if the application has data that
    01324                                it wants to send. */
    01325 #define UIP_CLOSE     16    /* The remote host has closed the
    01326                                connection, thus the connection has
    01327                                gone away. Or the application signals
    01328                                that it wants to close the
    01329                                connection. */
    01330 #define UIP_ABORT     32    /* The remote host has aborted the
    01331                                connection, thus the connection has
    01332                                gone away. Or the application signals
    01333                                that it wants to abort the
    01334                                connection. */
    01335 #define UIP_CONNECTED 64    /* We have got a connection from a remote
    01336                                host and have set up a new connection
    01337                                for it, or an active connection has
    01338                                been successfully established. */
    01339
    01340 #define UIP_TIMEDOUT  128   /* The connection has been aborted due to
    01341                                too many retransmissions. */
    01342
    01343 /* uip_process(flag):
    01344  *
    01345  * The actual uIP function which does all the work.
    01346  */
    01347 void uip_process(u8_t flag);
    01348
    01349 /* The following flags are passed as an argument to the uip_process()
    01350    function. They are used to distinguish between the two cases where
    01351    uip_process() is called. It can be called either because we have
    01352    incoming data that should be processed, or because the periodic
    01353    timer has fired. These values are never used directly, but only in
    01354    the macrose defined in this file. */
    01355
    01356 #define UIP_DATA          1     /* Tells uIP that there is incoming
    01357                                    data in the uip_buf buffer. The
    01358                                    length of the data is stored in the
    01359                                    global variable uip_len. */
    01360 #define UIP_TIMER         2     /* Tells uIP that the periodic timer
    01361                                    has fired. */
    01362 #define UIP_POLL_REQUEST  3     /* Tells uIP that a connection should
    01363                                    be polled. */
    01364 #define UIP_UDP_SEND_CONN 4     /* Tells uIP that a UDP datagram
    01365                                    should be constructed in the
    01366                                    uip_buf buffer. */
    01367 #if UIP_UDP
    01368 #define UIP_UDP_TIMER     5
    01369 #endif /* UIP_UDP */
    01370
    01371 /* The TCP states used in the uip_conn->tcpstateflags. */
    01372 #define UIP_CLOSED      0
    01373 #define UIP_SYN_RCVD    1
    01374 #define UIP_SYN_SENT    2
    01375 #define UIP_ESTABLISHED 3
    01376 #define UIP_FIN_WAIT_1  4
    01377 #define UIP_FIN_WAIT_2  5
    01378 #define UIP_CLOSING     6
    01379 #define UIP_TIME_WAIT   7
    01380 #define UIP_LAST_ACK    8
    01381 #define UIP_TS_MASK     15
    01382
    01383 #define UIP_STOPPED      16
    01384
    01385 /* The TCP and IP headers. */
    01386 struct uip_tcpip_hdr {
    01387 #if UIP_CONF_IPV6
    01388   /* IPv6 header. */
    01389   u8_t vtc,
    01390     tcflow;
    01391   u16_t flow;
    01392   u8_t len[2];
    01393   u8_t proto, ttl;
    01394   uip_ip6addr_t srcipaddr, destipaddr;
    01395 #else /* UIP_CONF_IPV6 */
    01396   /* IPv4 header. */
    01397   u8_t vhl,
    01398     tos,
    01399     len[2],
    01400     ipid[2],
    01401     ipoffset[2],
    01402     ttl,
    01403     proto;
    01404   u16_t ipchksum;
    01405   u16_t srcipaddr[2],
    01406     destipaddr[2];
    01407 #endif /* UIP_CONF_IPV6 */
    01408
    01409   /* TCP header. */
    01410   u16_t srcport,
    01411     destport;
    01412   u8_t seqno[4],
    01413     ackno[4],
    01414     tcpoffset,
    01415     flags,
    01416     wnd[2];
    01417   u16_t tcpchksum;
    01418   u8_t urgp[2];
    01419   u8_t optdata[4];
    01420 };
    01421
    01422 /* The ICMP and IP headers. */
    01423 struct uip_icmpip_hdr {
    01424 #if UIP_CONF_IPV6
    01425   /* IPv6 header. */
    01426   u8_t vtc,
    01427     tcf;
    01428   u16_t flow;
    01429   u8_t len[2];
    01430   u8_t proto, ttl;
    01431   uip_ip6addr_t srcipaddr, destipaddr;
    01432 #else /* UIP_CONF_IPV6 */
    01433   /* IPv4 header. */
    01434   u8_t vhl,
    01435     tos,
    01436     len[2],
    01437     ipid[2],
    01438     ipoffset[2],
    01439     ttl,
    01440     proto;
    01441   u16_t ipchksum;
    01442   u16_t srcipaddr[2],
    01443     destipaddr[2];
    01444 #endif /* UIP_CONF_IPV6 */
    01445
    01446   /* ICMP (echo) header. */
    01447   u8_t type, icode;
    01448   u16_t icmpchksum;
    01449 #if !UIP_CONF_IPV6
    01450   u16_t id, seqno;
    01451 #else /* !UIP_CONF_IPV6 */
    01452   u8_t flags, reserved1, reserved2, reserved3;
    01453   u8_t icmp6data[16];
    01454   u8_t options[1];
    01455 #endif /* !UIP_CONF_IPV6 */
    01456 };
    01457
    01458
    01459 /* The UDP and IP headers. */
    01460 struct uip_udpip_hdr {
    01461 #if UIP_CONF_IPV6
    01462   /* IPv6 header. */
    01463   u8_t vtc,
    01464     tcf;
    01465   u16_t flow;
    01466   u8_t len[2];
    01467   u8_t proto, ttl;
    01468   uip_ip6addr_t srcipaddr, destipaddr;
    01469 #else /* UIP_CONF_IPV6 */
    01470   /* IP header. */
    01471   u8_t vhl,
    01472     tos,
    01473     len[2],
    01474     ipid[2],
    01475     ipoffset[2],
    01476     ttl,
    01477     proto;
    01478   u16_t ipchksum;
    01479   u16_t srcipaddr[2],
    01480     destipaddr[2];
    01481 #endif /* UIP_CONF_IPV6 */
    01482
    01483   /* UDP header. */
    01484   u16_t srcport,
    01485     destport;
    01486   u16_t udplen;
    01487   u16_t udpchksum;
    01488 };
    01489
    01490
    01491
    01492 /**
    01493  * The buffer size available for user data in the \ref uip_buf buffer.
    01494  *
    01495  * This macro holds the available size for user data in the \ref
    01496  * uip_buf buffer. The macro is intended to be used for checking
    01497  * bounds of available user data.
    01498  *
    01499  * Example:
    01500  \code
    01501  snprintf(uip_appdata, UIP_APPDATA_SIZE, "%u\n", i);
    01502  \endcode
    01503  *
    01504  * \hideinitializer
    01505  */
    01506 #define UIP_APPDATA_SIZE (UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN)
    01507
    01508
    01509 #define UIP_PROTO_ICMP  1
    01510 #define UIP_PROTO_TCP   6
    01511 #define UIP_PROTO_UDP   17
    01512 #define UIP_PROTO_ICMP6 58
    01513
    01514 /* Header sizes. */
    01515 #if UIP_CONF_IPV6
    01516 #define UIP_IPH_LEN    40
    01517 #else /* UIP_CONF_IPV6 */
    01518 #define UIP_IPH_LEN    20    /* Size of IP header */
    01519 #endif /* UIP_CONF_IPV6 */
    01520 #define UIP_UDPH_LEN    8    /* Size of UDP header */
    01521 #define UIP_TCPH_LEN   20    /* Size of TCP header */
    01522 #define UIP_IPUDPH_LEN (UIP_UDPH_LEN + UIP_IPH_LEN)    /* Size of IP +
    01523                                                           UDP
    01524                                                           header */
    01525 #define UIP_IPTCPH_LEN (UIP_TCPH_LEN + UIP_IPH_LEN)    /* Size of IP +
    01526                                                           TCP
    01527                                                           header */
    01528 #define UIP_TCPIP_HLEN UIP_IPTCPH_LEN
    01529
    01530
    01531 #if UIP_FIXEDADDR
    01532 extern const uip_ipaddr_t uip_hostaddr, uip_netmask, uip_draddr;
    01533 #else /* UIP_FIXEDADDR */
    01534 extern uip_ipaddr_t uip_hostaddr, uip_netmask, uip_draddr;
    01535 #endif /* UIP_FIXEDADDR */
    01536
    01537
    01538
    01539 /**
    01540  * Representation of a 48-bit Ethernet address.
    01541  */
    01542 struct uip_eth_addr {
    01543   u8_t addr[6];
    01544 };
    01545
    01546 /**
    01547  * Calculate the Internet checksum over a buffer.
    01548  *
    01549  * The Internet checksum is the one's complement of the one's
    01550  * complement sum of all 16-bit words in the buffer.
    01551  *
    01552  * See RFC1071.
    01553  *
    01554  * \param buf A pointer to the buffer over which the checksum is to be
    01555  * computed.
    01556  *
    01557  * \param len The length of the buffer over which the checksum is to
    01558  * be computed.
    01559  *
    01560  * \return The Internet checksum of the buffer.
    01561  */
    01562 u16_t uip_chksum(u16_t *buf, u16_t len);
    01563
    01564 /**
    01565  * Calculate the IP header checksum of the packet header in uip_buf.
    01566  *
    01567  * The IP header checksum is the Internet checksum of the 20 bytes of
    01568  * the IP header.
    01569  *
    01570  * \return The IP header checksum of the IP header in the uip_buf
    01571  * buffer.
    01572  */
    01573 u16_t uip_ipchksum(void);
    01574
    01575 /**
    01576  * Calculate the TCP checksum of the packet in uip_buf and uip_appdata.
    01577  *
    01578  * The TCP checksum is the Internet checksum of data contents of the
    01579  * TCP segment, and a pseudo-header as defined in RFC793.
    01580  *
    01581  * \return The TCP checksum of the TCP segment in uip_buf and pointed
    01582  * to by uip_appdata.
    01583  */
    01584 u16_t uip_tcpchksum(void);
    01585
    01586 /**
    01587  * Calculate the UDP checksum of the packet in uip_buf and uip_appdata.
    01588  *
    01589  * The UDP checksum is the Internet checksum of data contents of the
    01590  * UDP segment, and a pseudo-header as defined in RFC768.
    01591  *
    01592  * \return The UDP checksum of the UDP segment in uip_buf and pointed
    01593  * to by uip_appdata.
    01594  */
    01595 u16_t uip_udpchksum(void);
    01596
    01597
    01598 #endif /* __UIP_H__ */
    01599
    01600
    01601 /** @} */