adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 1 | /** |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 2 | * \addtogroup uip |
| 3 | * @{ |
| 4 | */ |
| 5 | |
| 6 | /** |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 7 | * \file |
| 8 | * Header file for the uIP TCP/IP stack. |
| 9 | * \author Adam Dunkels <adam@dunkels.com> |
| 10 | * |
| 11 | * The uIP TCP/IP stack header file contains definitions for a number |
| 12 | * of C macros that are used by uIP programs as well as internal uIP |
| 13 | * structures, TCP/IP header structures and function declarations. |
| 14 | * |
| 15 | */ |
| 16 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 17 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 18 | /* |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 19 | * Copyright (c) 2001-2003, Adam Dunkels. |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 20 | * All rights reserved. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the above copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 30 | * 3. The name of the author may not be used to endorse or promote |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 31 | * products derived from this software without specific prior |
| 32 | * written permission. |
| 33 | * |
| 34 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 35 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 36 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 37 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 38 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 39 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 40 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 41 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 42 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 43 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 44 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 45 | * |
| 46 | * This file is part of the uIP TCP/IP stack. |
| 47 | * |
adamdunkels | 399a078 | 2004-02-16 20:52:07 +0000 | [diff] [blame^] | 48 | * $Id: uip.h,v 1.10 2004/02/16 20:52:07 adamdunkels Exp $ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 49 | * |
| 50 | */ |
| 51 | |
| 52 | #ifndef __UIP_H__ |
| 53 | #define __UIP_H__ |
| 54 | |
| 55 | #include "uipopt.h" |
| 56 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 57 | /*-----------------------------------------------------------------------------------*/ |
| 58 | /* First, the functions that should be called from the |
| 59 | * system. Initialization, the periodic timer and incoming packets are |
| 60 | * handled by the following three functions. |
| 61 | */ |
| 62 | |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 63 | /** |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 64 | * \defgroup uipconffunc uIP configuration functions |
| 65 | * @{ |
| 66 | * |
| 67 | * The uIP configuration functions are used for setting run-time |
| 68 | * parameters in uIP such as IP addresses. |
| 69 | */ |
| 70 | |
| 71 | /** |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 72 | * Set the IP address of this host. |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 73 | * |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 74 | * The IP address is represented as a 4-byte array where the first |
| 75 | * octet of the IP address is put in the first member of the 4-byte |
| 76 | * array. |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 77 | * |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 78 | * \param addr A pointer to a 4-byte representation of the IP address. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 79 | * |
| 80 | * \hideinitializer |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 81 | */ |
| 82 | #define uip_sethostaddr(addr) do { uip_hostaddr[0] = addr[0]; \ |
| 83 | uip_hostaddr[1] = addr[1]; } while(0) |
| 84 | |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 85 | /** |
| 86 | * Get the IP address of this host. |
adamdunkels | 66c6af6 | 2003-04-16 18:28:16 +0000 | [diff] [blame] | 87 | * |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 88 | * The IP address is represented as a 4-byte array where the first |
| 89 | * octet of the IP address is put in the first member of the 4-byte |
| 90 | * array. |
| 91 | * |
| 92 | * \param addr A pointer to a 4-byte array that will be filled in with |
| 93 | * the currently configured IP address. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 94 | * |
| 95 | * \hideinitializer |
adamdunkels | 66c6af6 | 2003-04-16 18:28:16 +0000 | [diff] [blame] | 96 | */ |
| 97 | #define uip_gethostaddr(addr) do { addr[0] = uip_hostaddr[0]; \ |
| 98 | addr[1] = uip_hostaddr[1]; } while(0) |
| 99 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 100 | /** @} */ |
| 101 | |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 102 | /** |
| 103 | * \defgroup uipinit uIP initialization functions |
| 104 | * @{ |
| 105 | * |
| 106 | * The uIP initialization functions are used for booting uIP. |
| 107 | */ |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * uIP initialization function. |
| 111 | * |
| 112 | * This function should be called at boot up to initilize the uIP |
| 113 | * TCP/IP stack. |
| 114 | */ |
| 115 | void uip_init(void); |
| 116 | |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 117 | /** @} */ |
| 118 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 119 | /** |
| 120 | * \defgroup uipdevfunc uIP device driver functions |
| 121 | * @{ |
| 122 | * |
| 123 | * These functions are used by a network device driver for interacting |
| 124 | * with uIP. |
| 125 | */ |
| 126 | |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 127 | /** |
| 128 | * Process an incoming packet. |
| 129 | * |
| 130 | * This function should be called when the device driver has received |
| 131 | * a packet from the network. The packet from the device driver must |
| 132 | * be present in the uip_buf buffer, and the length of the packet |
| 133 | * should be placed in the uip_len variable. |
| 134 | * |
| 135 | * When the function returns, there may be an outbound packet placed |
| 136 | * in the uip_buf packet buffer. If so, the uip_len variable is set to |
| 137 | * the length of the packet. If no packet is to be sent out, the |
| 138 | * uip_len variable is set to 0. |
| 139 | * |
| 140 | * The usual way of calling the function is presented by the source |
| 141 | * code below. |
| 142 | \code |
| 143 | uip_len = devicedriver_poll(); |
| 144 | if(uip_len > 0) { |
| 145 | uip_input(); |
| 146 | if(uip_len > 0) { |
| 147 | devicedriver_send(); |
| 148 | } |
| 149 | } |
| 150 | \endcode |
| 151 | * |
| 152 | * \note If you are writing a uIP device driver that needs ARP |
| 153 | * (Address Resolution Protocol), e.g., when running uIP over |
| 154 | * Ethernet, you will need to call the uIP ARP code before calling |
| 155 | * this function: |
| 156 | \code |
| 157 | #define BUF ((struct uip_eth_hdr *)&uip_buf[0]) |
| 158 | uip_len = ethernet_devicedrver_poll(); |
| 159 | if(uip_len > 0) { |
| 160 | if(BUF->type == HTONS(UIP_ETHTYPE_IP)) { |
| 161 | uip_arp_ipin(); |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 162 | uip_input(); |
| 163 | if(uip_len > 0) { |
| 164 | uip_arp_out(); |
| 165 | ethernet_devicedriver_send(); |
| 166 | } |
| 167 | } else if(BUF->type == HTONS(UIP_ETHTYPE_ARP)) { |
| 168 | uip_arp_arpin(); |
| 169 | if(uip_len > 0) { |
| 170 | ethernet_devicedriver_send(); |
| 171 | } |
| 172 | } |
| 173 | \endcode |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 174 | * |
| 175 | * \hideinitializer |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 176 | */ |
| 177 | #define uip_input() uip_process(UIP_DATA) |
| 178 | |
| 179 | /** |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 180 | * Periodic processing for a connection identified by its number. |
| 181 | * |
| 182 | * This function does the necessary periodic processing (timers, |
| 183 | * polling) for a uIP TCP conneciton, and should be called when the |
| 184 | * periodic uIP timer goes off. It should be called for every |
| 185 | * connection, regardless of whether they are open of closed. |
| 186 | * |
| 187 | * When the function returns, it may have an outbound packet waiting |
| 188 | * for service in the uIP packet buffer, and if so the uip_len |
| 189 | * variable is set to a value larger than zero. The device driver |
| 190 | * should be called to send out the packet. |
| 191 | * |
| 192 | * The ususal way of calling the function is through a for() loop like |
| 193 | * this: |
| 194 | \code |
| 195 | for(i = 0; i < UIP_CONNS; ++i) { |
| 196 | uip_periodic(i); |
| 197 | if(uip_len > 0) { |
| 198 | devicedriver_send(); |
| 199 | } |
| 200 | } |
| 201 | \endcode |
| 202 | * |
| 203 | * \note If you are writing a uIP device driver that needs ARP |
| 204 | * (Address Resolution Protocol), e.g., when running uIP over |
| 205 | * Ethernet, you will need to call the uip_arp_out() function before |
| 206 | * calling the device driver: |
| 207 | \code |
| 208 | for(i = 0; i < UIP_CONNS; ++i) { |
| 209 | uip_periodic(i); |
| 210 | if(uip_len > 0) { |
| 211 | uip_arp_out(); |
| 212 | ethernet_devicedriver_send(); |
| 213 | } |
| 214 | } |
| 215 | \endcode |
| 216 | * |
| 217 | * \param conn The number of the connection which is to be periodically polled. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 218 | * |
| 219 | * \hideinitializer |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 220 | */ |
| 221 | #define uip_periodic(conn) do { uip_conn = &uip_conns[conn]; \ |
| 222 | uip_process(UIP_TIMER); } while (0) |
| 223 | |
| 224 | /** |
| 225 | * Periodic processing for a connection identified by a pointer to its structure. |
| 226 | * |
| 227 | * Same as uip_periodic() but takes a pointer to the actual uip_conn |
| 228 | * struct instead of an integer as its argument. This function can be |
| 229 | * used to force periodic processing of a specific connection. |
| 230 | * |
| 231 | * \param conn A pointer to the uip_conn struct for the connection to |
| 232 | * be processed. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 233 | * |
| 234 | * \hideinitializer |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 235 | */ |
| 236 | #define uip_periodic_conn(conn) do { uip_conn = conn; \ |
| 237 | uip_process(UIP_TIMER); } while (0) |
| 238 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 239 | #if UIP_UDP |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 240 | /** |
| 241 | * Periodic processing for a UDP connection identified by its number. |
| 242 | * |
| 243 | * This function is essentially the same as uip_prerioic(), but for |
| 244 | * UDP connections. It is called in a similar fashion as the |
| 245 | * uip_periodic() function: |
| 246 | \code |
| 247 | for(i = 0; i < UIP_UDP_CONNS; i++) { |
| 248 | uip_udp_periodic(i); |
| 249 | if(uip_len > 0) { |
| 250 | devicedriver_send(); |
| 251 | } |
| 252 | } |
| 253 | \endcode |
| 254 | * |
| 255 | * \note As for the uip_periodic() function, special care has to be |
| 256 | * taken when using uIP together with ARP and Ethernet: |
| 257 | \code |
| 258 | for(i = 0; i < UIP_UDP_CONNS; i++) { |
| 259 | uip_udp_periodic(i); |
| 260 | if(uip_len > 0) { |
| 261 | uip_arp_out(); |
| 262 | ethernet_devicedriver_send(); |
| 263 | } |
| 264 | } |
| 265 | \endcode |
| 266 | * |
| 267 | * \param conn The number of the UDP connection to be processed. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 268 | * |
| 269 | * \hideinitializer |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 270 | */ |
| 271 | #define uip_udp_periodic(conn) do { uip_udp_conn = &uip_udp_conns[conn]; \ |
| 272 | uip_process(UIP_UDP_TIMER); } while (0) |
adamdunkels | eb91d5f | 2003-08-20 20:58:08 +0000 | [diff] [blame] | 273 | |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 274 | /** |
| 275 | * Periodic processing for a UDP connection identified by a pointer to |
| 276 | * its structure. |
| 277 | * |
| 278 | * Same as uip_udp_periodic() but takes a pointer to the actual |
| 279 | * uip_conn struct instead of an integer as its argument. This |
| 280 | * function can be used to force periodic processing of a specific |
| 281 | * connection. |
| 282 | * |
| 283 | * \param conn A pointer to the uip_udp_conn struct for the connection |
| 284 | * to be processed. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 285 | * |
| 286 | * \hideinitializer |
adamdunkels | eb91d5f | 2003-08-20 20:58:08 +0000 | [diff] [blame] | 287 | */ |
| 288 | #define uip_udp_periodic_conn(conn) do { uip_udp_conn = conn; \ |
| 289 | uip_process(UIP_UDP_TIMER); } while (0) |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 290 | |
| 291 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 292 | #endif /* UIP_UDP */ |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 293 | |
| 294 | /** |
| 295 | * The uIP packet buffer. |
| 296 | * |
| 297 | * The uip_buf array is used to hold incoming and outgoing |
| 298 | * packets. The device driver should place incoming data into this |
| 299 | * buffer. When sending data, the device driver should read the link |
| 300 | * level headers and the TCP/IP headers from this buffer. The size of |
| 301 | * the link level headers is configured by the UIP_LLH_LEN define. |
| 302 | * |
| 303 | * \note The application data need not be placed in this buffer, so |
| 304 | * the device driver must read it from the place pointed to by the |
| 305 | * uip_appdata pointer as illustrated by the following example: |
| 306 | \code |
| 307 | void |
| 308 | devicedriver_send(void) |
| 309 | { |
| 310 | hwsend(&uip_buf[0], UIP_LLH_LEN); |
| 311 | hwsend(&uip_buf[UIP_LLH_LEN], 40); |
| 312 | hwsend(uip_appdata, uip_len - 40 - UIP_LLH_LEN); |
| 313 | } |
| 314 | \endcode |
| 315 | */ |
| 316 | extern u8_t uip_buf[UIP_BUFSIZE+2]; |
| 317 | |
| 318 | /** @} */ |
| 319 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 320 | /*-----------------------------------------------------------------------------------*/ |
| 321 | /* Functions that are used by the uIP application program. Opening and |
| 322 | * closing connections, sending and receiving data, etc. is all |
| 323 | * handled by the functions below. |
| 324 | */ |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 325 | /** |
| 326 | * \defgroup uipappfunc uIP application functions |
| 327 | * @{ |
| 328 | * |
| 329 | * Functions used by an application running of top of uIP. |
| 330 | */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 331 | |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 332 | /** |
| 333 | * Start listening to the specified port. |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 334 | * |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 335 | * \note Since this function expects the port number in network byte |
| 336 | * order, a conversion using HTONS() or htons() is necessary. |
| 337 | * |
| 338 | \code |
| 339 | uip_listen(HTONS(80)); |
| 340 | \endcode |
| 341 | * |
| 342 | * \param port A 16-bit port number in network byte order. |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 343 | */ |
| 344 | void uip_listen(u16_t port); |
| 345 | |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 346 | /** |
| 347 | * Stop listening to the specified port. |
adamdunkels | cd8c3a2 | 2003-08-13 22:52:48 +0000 | [diff] [blame] | 348 | * |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 349 | * \note Since this function expects the port number in network byte |
| 350 | * order, a conversion using HTONS() or htons() is necessary. |
| 351 | * |
| 352 | \code |
| 353 | uip_unlisten(HTONS(80)); |
| 354 | \endcode |
| 355 | * |
| 356 | * \param port A 16-bit port number in network byte order. |
adamdunkels | cd8c3a2 | 2003-08-13 22:52:48 +0000 | [diff] [blame] | 357 | */ |
| 358 | void uip_unlisten(u16_t port); |
| 359 | |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 360 | /** |
| 361 | * Connect to a remote host using TCP. |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 362 | * |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 363 | * This function is used to start a new connection to the specified |
| 364 | * port on the specied host. It allocates a new connection identifier, |
| 365 | * sets the connection to the SYN_SENT state and sets the |
| 366 | * retransmission timer to 0. This will cause a TCP SYN segment to be |
| 367 | * sent out the next time this connection is periodically processed, |
| 368 | * which usually is done within 0.5 seconds after the call to |
| 369 | * uip_connect(). |
| 370 | * |
| 371 | * \note This function is avaliable only if support for active open |
| 372 | * has been configured by defining UIP_ACTIVE_OPEN to 1 in uipopt.h. |
| 373 | * |
| 374 | * \note Since this function requires the port number to be in network |
| 375 | * byte order, a convertion using HTONS() or htons() is necessary. |
| 376 | * |
| 377 | \code |
| 378 | u16_t ipaddr[2]; |
| 379 | |
| 380 | uip_ipaddr(ipaddr, 192,168,1,2); |
| 381 | uip_connect(ipaddr, HTONS(80)); |
| 382 | \endcode |
| 383 | * |
| 384 | * \param ripaddr A pointer to a 4-byte array representing the IP |
| 385 | * address of the remote hot. |
| 386 | * |
| 387 | * \param port A 16-bit port number in network byte order. |
| 388 | * |
| 389 | * \return A pointer to the uIP connection identifier for the new connection, |
| 390 | * or NULL if no connection could be allocated. |
| 391 | * |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 392 | */ |
| 393 | struct uip_conn *uip_connect(u16_t *ripaddr, u16_t port); |
| 394 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 395 | |
| 396 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 397 | /** |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 398 | * \internal |
| 399 | * |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 400 | * Check if a connection has outstanding (i.e., unacknowledged) data. |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 401 | * |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 402 | * \param conn A pointer to the uip_conn structure for the connection. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 403 | * |
| 404 | * \hideinitializer |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 405 | */ |
| 406 | #define uip_outstanding(conn) ((conn)->len) |
| 407 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 408 | /** |
| 409 | * Send data on the current connection. |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 410 | * |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 411 | * This function is used to send out a single segment of TCP |
| 412 | * data. Only applications that have been invoked by uIP for event |
| 413 | * processing can send data. |
| 414 | * |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 415 | * The amount of data that actually is sent out after a call to this |
| 416 | * funcion is determined by the maximum amount of data TCP allows. uIP |
| 417 | * will automatically crop the data so that only the appropriate |
| 418 | * amount of data is sent. The function uip_mss() can be used to query |
| 419 | * uIP for the amount of data that actually will be sent. |
| 420 | * |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 421 | * \note This function does not guarantee that the sent data will |
| 422 | * arrive at the destination. If the data is lost in the network, the |
| 423 | * application will be invoked with the uip_rexmit() event being |
| 424 | * set. The application will then have to resend the data using this |
| 425 | * function. |
| 426 | * |
| 427 | * \param data A pointer to the data which is to be sent. |
| 428 | * |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 429 | * \param len The maximum amount of data bytes to be sent. |
| 430 | * |
| 431 | * \hideinitializer |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 432 | */ |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 433 | #define uip_send(data, len) do { uip_sappdata = (data); uip_slen = (len);} while(0) |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 434 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 435 | /** |
| 436 | * The length of any incoming data that is currently avaliable (if avaliable) |
| 437 | * in the uip_appdata buffer. |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 438 | * |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 439 | * The test function uip_data() must first be used to check if there |
| 440 | * is any data available at all. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 441 | * |
| 442 | * \hideinitializer |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 443 | */ |
| 444 | #define uip_datalen() uip_len |
| 445 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 446 | /** |
| 447 | * The length of any out-of-band data (urgent data) that has arrived |
| 448 | * on the connection. |
| 449 | * |
| 450 | * \note The configuration parameter UIP_URGDATA must be set for this |
| 451 | * function to be enabled. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 452 | * |
| 453 | * \hideinitializer |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 454 | */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 455 | #define uip_urgdatalen() uip_urglen |
| 456 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 457 | /** |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 458 | * Close the current connection. |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 459 | * |
| 460 | * This function will close the current connection in a nice way. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 461 | * |
| 462 | * \hideinitializer |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 463 | */ |
| 464 | #define uip_close() (uip_flags = UIP_CLOSE) |
| 465 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 466 | /** |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 467 | * Abort the current connection. |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 468 | * |
| 469 | * This function will abort (reset) the current connection, and is |
| 470 | * usually used when an error has occured that prevents using the |
| 471 | * uip_close() function. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 472 | * |
| 473 | * \hideinitializer |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 474 | */ |
| 475 | #define uip_abort() (uip_flags = UIP_ABORT) |
| 476 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 477 | /** |
| 478 | * Tell the sending host to stop sending data. |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 479 | * |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 480 | * This function will close our receiver's window so that we stop |
| 481 | * receiving data for the current connection. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 482 | * |
| 483 | * \hideinitializer |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 484 | */ |
| 485 | #define uip_stop() (uip_conn->tcpstateflags |= UIP_STOPPED) |
| 486 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 487 | /** |
| 488 | * Find out if the current connection has been previously stopped with |
| 489 | * uip_stop(). |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 490 | * |
| 491 | * \hideinitializer |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 492 | */ |
| 493 | #define uip_stopped(conn) ((conn)->tcpstateflags & UIP_STOPPED) |
| 494 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 495 | /** |
| 496 | * Restart the current connection, if is has previously been stopped |
| 497 | * with uip_stop(). |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 498 | * |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 499 | * This function will open the receiver's window again so that we |
| 500 | * start receiving data for the current connection. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 501 | * |
| 502 | * \hideinitializer |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 503 | */ |
| 504 | #define uip_restart() do { uip_flags |= UIP_NEWDATA; \ |
| 505 | uip_conn->tcpstateflags &= ~UIP_STOPPED; \ |
| 506 | } while(0) |
| 507 | |
| 508 | |
| 509 | /* uIP tests that can be made to determine in what state the current |
| 510 | connection is, and what the application function should do. */ |
| 511 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 512 | /** |
| 513 | * Is new incoming data available? |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 514 | * |
| 515 | * Will reduce to non-zero if there is new data for the application |
| 516 | * present at the uip_appdata pointer. The size of the data is |
| 517 | * avaliable through the uip_len variable. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 518 | * |
| 519 | * \hideinitializer |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 520 | */ |
| 521 | #define uip_newdata() (uip_flags & UIP_NEWDATA) |
| 522 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 523 | /** |
| 524 | * Has previously sent data been acknowledged? |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 525 | * |
| 526 | * Will reduce to non-zero if the previously sent data has been |
| 527 | * acknowledged by the remote host. This means that the application |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 528 | * can send new data. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 529 | * |
| 530 | * \hideinitializer |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 531 | */ |
| 532 | #define uip_acked() (uip_flags & UIP_ACKDATA) |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 533 | |
| 534 | /** |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 535 | * Has the connection just been connected? |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 536 | * |
| 537 | * Reduces to non-zero if the current connection has been connected to |
| 538 | * a remote host. This will happen both if the connection has been |
| 539 | * actively opened (with uip_connect()) or passively opened (with |
| 540 | * uip_listen()). |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 541 | * |
| 542 | * \hideinitializer |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 543 | */ |
| 544 | #define uip_connected() (uip_flags & UIP_CONNECTED) |
| 545 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 546 | /** |
| 547 | * Has the connection been closed by the other end? |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 548 | * |
| 549 | * Is non-zero if the connection has been closed by the remote |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 550 | * host. The application may then do the necessary clean-ups. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 551 | * |
| 552 | * \hideinitializer |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 553 | */ |
| 554 | #define uip_closed() (uip_flags & UIP_CLOSE) |
| 555 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 556 | /** |
| 557 | * Has the connection been aborted by the other end? |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 558 | * |
| 559 | * Non-zero if the current connection has been aborted (reset) by the |
| 560 | * remote host. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 561 | * |
| 562 | * \hideinitializer |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 563 | */ |
| 564 | #define uip_aborted() (uip_flags & UIP_ABORT) |
| 565 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 566 | /** |
| 567 | * Has the connection timed out? |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 568 | * |
| 569 | * Non-zero if the current connection has been aborted due to too many |
| 570 | * retransmissions. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 571 | * |
| 572 | * \hideinitializer |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 573 | */ |
adamdunkels | 399a078 | 2004-02-16 20:52:07 +0000 | [diff] [blame^] | 574 | #define uip_timedout() (uip_flags & UIP_TIMEDOUT) |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 575 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 576 | /** |
| 577 | * Do we need to retransmit previously data? |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 578 | * |
| 579 | * Reduces to non-zero if the previously sent data has been lost in |
| 580 | * the network, and the application should retransmit it. The |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 581 | * application should send the exact same data as it did the last |
| 582 | * time, using the uip_send() function. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 583 | * |
| 584 | * \hideinitializer |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 585 | */ |
| 586 | #define uip_rexmit() (uip_flags & UIP_REXMIT) |
| 587 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 588 | /** |
| 589 | * Is the connection being polled by uIP? |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 590 | * |
| 591 | * Is non-zero if the reason the application is invoked is that the |
| 592 | * current connection has been idle for a while and should be |
| 593 | * polled. |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 594 | * |
| 595 | * The polling event can be used for sending data without having to |
| 596 | * wait for the remote host to send data. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 597 | * |
| 598 | * \hideinitializer |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 599 | */ |
| 600 | #define uip_poll() (uip_flags & UIP_POLL) |
| 601 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 602 | /** |
| 603 | * Get the initial maxium segment size (MSS) of the current |
| 604 | * connection. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 605 | * |
| 606 | * \hideinitializer |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 607 | */ |
| 608 | #define uip_initialmss() (uip_conn->initialmss) |
| 609 | |
| 610 | /** |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 611 | * Get the current maxium segment size that can be sent on the current |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 612 | * connection. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 613 | * |
| 614 | * The current maxiumum segment size that can be sent on the |
| 615 | * connection is computed from the receiver's window and the MSS of |
| 616 | * the connection (which also is available by calling |
| 617 | * uip_initialmss()). |
| 618 | * |
| 619 | * \hideinitializer |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 620 | */ |
| 621 | #define uip_mss() (uip_conn->mss) |
| 622 | |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 623 | /** |
| 624 | * Set up a new UDP connection. |
| 625 | * |
| 626 | * \param ripaddr A pointer to a 4-byte structure representing the IP |
| 627 | * address of the remote host. |
| 628 | * |
| 629 | * \param rport The remote port number in network byte order. |
| 630 | * |
| 631 | * \return The uip_udp_conn structure for the new connection or NULL |
| 632 | * if no connection could be allocated. |
| 633 | */ |
| 634 | struct uip_udp_conn *uip_udp_new(u16_t *ripaddr, u16_t rport); |
| 635 | |
| 636 | /** |
| 637 | * Removed a UDP connection. |
| 638 | * |
| 639 | * \param conn A pointer to the uip_udp_conn structure for the connection. |
| 640 | * |
| 641 | * \hideinitializer |
| 642 | */ |
| 643 | #define uip_udp_remove(conn) (conn)->lport = 0 |
| 644 | |
| 645 | /** |
| 646 | * Send a UDP datagram of length len on the current connection. |
| 647 | * |
| 648 | * This function can only be called in response to a UDP event (poll |
| 649 | * or newdata). The data must be present in the uip_buf buffer, at the |
| 650 | * place pointed to by the uip_appdata pointer. |
| 651 | * |
| 652 | * \param len The length of the data in the uip_buf buffer. |
| 653 | * |
| 654 | * \hideinitializer |
| 655 | */ |
| 656 | #define uip_udp_send(len) uip_slen = (len) |
| 657 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 658 | /** @} */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 659 | |
| 660 | /* uIP convenience and converting functions. */ |
| 661 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 662 | /** |
| 663 | * \defgroup uipconvfunc uIP conversion functions |
| 664 | * @{ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 665 | * |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 666 | * These functions can be used for converting between different data |
| 667 | * formats used by uIP. |
| 668 | */ |
| 669 | |
| 670 | /** |
| 671 | * Pack an IP address into a 4-byte array which is used by uIP to |
| 672 | * represent IP addresses. |
| 673 | * |
| 674 | * Example: |
| 675 | \code |
| 676 | u16_t ipaddr[2]; |
| 677 | |
| 678 | uip_ipaddr(&ipaddr, 192,168,1,2); |
| 679 | \endcode |
| 680 | * |
| 681 | * \param addr A pointer to a 4-byte array that will be filled in with |
| 682 | * the IP addres. |
| 683 | * \param addr0 The first octet of the IP address. |
| 684 | * \param addr1 The second octet of the IP address. |
| 685 | * \param addr2 The third octet of the IP address. |
| 686 | * \param addr3 The forth octet of the IP address. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 687 | * |
| 688 | * \hideinitializer |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 689 | */ |
| 690 | #define uip_ipaddr(addr, addr0,addr1,addr2,addr3) do { \ |
adamdunkels | 47ec7fa | 2003-03-28 12:11:17 +0000 | [diff] [blame] | 691 | (addr)[0] = HTONS(((addr0) << 8) | (addr1)); \ |
| 692 | (addr)[1] = HTONS(((addr2) << 8) | (addr3)); \ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 693 | } while(0) |
| 694 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 695 | /** |
| 696 | * Convert 16-bit quantity from host byte order to network byte order. |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 697 | * |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 698 | * This macro is primarily used for converting constants from host |
| 699 | * byte order to network byte order. For converting variables to |
| 700 | * network byte order, use the htons() function instead. |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 701 | * |
| 702 | * \hideinitializer |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 703 | */ |
adamdunkels | 47ec7fa | 2003-03-28 12:11:17 +0000 | [diff] [blame] | 704 | #ifndef HTONS |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 705 | # if BYTE_ORDER == BIG_ENDIAN |
adamdunkels | 47ec7fa | 2003-03-28 12:11:17 +0000 | [diff] [blame] | 706 | # define HTONS(n) (n) |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 707 | # else /* BYTE_ORDER == BIG_ENDIAN */ |
adamdunkels | 47ec7fa | 2003-03-28 12:11:17 +0000 | [diff] [blame] | 708 | # define HTONS(n) ((((u16_t)((n) & 0xff)) << 8) | (((n) & 0xff00) >> 8)) |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 709 | # endif /* BYTE_ORDER == BIG_ENDIAN */ |
adamdunkels | 47ec7fa | 2003-03-28 12:11:17 +0000 | [diff] [blame] | 710 | #endif /* HTONS */ |
| 711 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 712 | /** |
| 713 | * Convert 16-bit quantity from host byte order to network byte order. |
| 714 | * |
| 715 | * This function is primarily used for converting variables from host |
| 716 | * byte order to network byte order. For converting constants to |
| 717 | * network byte order, use the HTONS() macro instead. |
| 718 | */ |
adamdunkels | 47ec7fa | 2003-03-28 12:11:17 +0000 | [diff] [blame] | 719 | #ifndef htons |
| 720 | u16_t htons(u16_t val); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 721 | #endif /* htons */ |
| 722 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 723 | /** @} */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 724 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 725 | /** |
| 726 | * Pointer to the application data in the packet buffer. |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 727 | * |
| 728 | * This pointer points to the application data when the application is |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 729 | * called. If the application wishes to send data, the application may |
| 730 | * use this space to write the data into before calling uip_send(). |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 731 | */ |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 732 | extern volatile u8_t *uip_appdata; |
| 733 | extern volatile u8_t *uip_sappdata; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 734 | |
| 735 | #if UIP_URGDATA > 0 |
| 736 | /* u8_t *uip_urgdata: |
| 737 | * |
| 738 | * This pointer points to any urgent data that has been received. Only |
| 739 | * present if compiled with support for urgent data (UIP_URGDATA). |
| 740 | */ |
| 741 | extern volatile u8_t *uip_urgdata; |
| 742 | #endif /* UIP_URGDATA > 0 */ |
| 743 | |
| 744 | |
| 745 | /* u[8|16]_t uip_len: |
| 746 | * |
| 747 | * When the application is called, uip_len contains the length of any |
| 748 | * new data that has been received from the remote host. The |
| 749 | * application should set this variable to the size of any data that |
| 750 | * the application wishes to send. When the network device driver |
| 751 | * output function is called, uip_len should contain the length of the |
| 752 | * outgoing packet. |
| 753 | */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 754 | extern volatile u16_t uip_len, uip_slen; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 755 | |
| 756 | #if UIP_URGDATA > 0 |
| 757 | extern volatile u8_t uip_urglen, uip_surglen; |
| 758 | #endif /* UIP_URGDATA > 0 */ |
| 759 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 760 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 761 | /** |
| 762 | * Representation of a uIP TCP connection. |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 763 | * |
| 764 | * The uip_conn structure is used for identifying a connection. All |
| 765 | * but one field in the structure are to be considered read-only by an |
| 766 | * application. The only exception is the appstate field whos purpose |
| 767 | * is to let the application store application-specific state (e.g., |
| 768 | * file pointers) for the connection. The size of this field is |
| 769 | * configured in the "uipopt.h" header file. |
| 770 | */ |
| 771 | struct uip_conn { |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 772 | u16_t ripaddr[2]; /**< The IP address of the remote host. */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 773 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 774 | u16_t lport; /**< The local TCP port, in network byte order. */ |
| 775 | u16_t rport; /**< The local remote TCP port, in network byte |
| 776 | order. */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 777 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 778 | u8_t rcv_nxt[4]; /**< The sequence number that we expect to |
| 779 | receive next. */ |
| 780 | u8_t snd_nxt[4]; /**< The sequence number that was last sent by |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 781 | us. */ |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 782 | u16_t len; /**< Length of the data that was previously sent. */ |
| 783 | u16_t mss; /**< Current maximum segment size for the |
| 784 | connection. */ |
| 785 | u16_t initialmss; /**< Initial maximum segment size for the |
| 786 | connection. */ |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 787 | u8_t sa; /**< Retransmission time-out calculation state |
| 788 | variable. */ |
| 789 | u8_t sv; /**< Retransmission time-out calculation state |
| 790 | variable. */ |
| 791 | u8_t rto; /**< Retransmission time-out. */ |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 792 | u8_t tcpstateflags; /**< TCP state and flags. */ |
| 793 | u8_t timer; /**< The retransmission timer. */ |
| 794 | u8_t nrtx; /**< The number of retransmissions for the last |
| 795 | segment sent. */ |
| 796 | |
| 797 | /** The application state. */ |
| 798 | u8_t appstate[UIP_APPSTATE_SIZE]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 799 | }; |
| 800 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 801 | |
| 802 | /* Pointer to the current connection. */ |
| 803 | extern struct uip_conn *uip_conn; |
| 804 | /* The array containing all uIP connections. */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 805 | extern struct uip_conn uip_conns[UIP_CONNS]; |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 806 | /** |
| 807 | * \addtogroup uiparch |
| 808 | * @{ |
| 809 | */ |
| 810 | |
| 811 | /** |
| 812 | * 4-byte array used for the 32-bit sequence number calculations. |
| 813 | */ |
| 814 | extern volatile u8_t uip_acc32[4]; |
| 815 | |
| 816 | /** @} */ |
| 817 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 818 | |
| 819 | #if UIP_UDP |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 820 | /** |
| 821 | * Representation of a uIP UDP connection. |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 822 | */ |
| 823 | struct uip_udp_conn { |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 824 | u16_t ripaddr[2]; /**< The IP address of the remote peer. */ |
| 825 | u16_t lport; /**< The local port number in network byte order. */ |
| 826 | u16_t rport; /**< The remote port number in network byte order. */ |
adamdunkels | 399a078 | 2004-02-16 20:52:07 +0000 | [diff] [blame^] | 827 | |
| 828 | /** The application state. */ |
| 829 | u8_t appstate[UIP_APPSTATE_SIZE]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 830 | }; |
| 831 | |
| 832 | extern struct uip_udp_conn *uip_udp_conn; |
| 833 | extern struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS]; |
| 834 | #endif /* UIP_UDP */ |
| 835 | |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 836 | /** |
| 837 | * The structure holding the TCP/IP statistics that are gathered if |
| 838 | * UIP_STATISTICS is set to 1. |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 839 | * |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 840 | */ |
| 841 | struct uip_stats { |
| 842 | struct { |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 843 | uip_stats_t drop; /**< Number of dropped packets at the IP |
| 844 | layer. */ |
| 845 | uip_stats_t recv; /**< Number of received packets at the IP |
| 846 | layer. */ |
| 847 | uip_stats_t sent; /**< Number of sent packets at the IP |
| 848 | layer. */ |
| 849 | uip_stats_t vhlerr; /**< Number of packets dropped due to wrong |
| 850 | IP version or header length. */ |
| 851 | uip_stats_t hblenerr; /**< Number of packets dropped due to wrong |
| 852 | IP length, high byte. */ |
| 853 | uip_stats_t lblenerr; /**< Number of packets dropped due to wrong |
| 854 | IP length, low byte. */ |
| 855 | uip_stats_t fragerr; /**< Number of packets dropped since they |
| 856 | were IP fragments. */ |
| 857 | uip_stats_t chkerr; /**< Number of packets dropped due to IP |
| 858 | checksum errors. */ |
| 859 | uip_stats_t protoerr; /**< Number of packets dropped since they |
| 860 | were neither ICMP, UDP nor TCP. */ |
| 861 | } ip; /**< IP statistics. */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 862 | struct { |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 863 | uip_stats_t drop; /**< Number of dropped ICMP packets. */ |
| 864 | uip_stats_t recv; /**< Number of received ICMP packets. */ |
| 865 | uip_stats_t sent; /**< Number of sent ICMP packets. */ |
| 866 | uip_stats_t typeerr; /**< Number of ICMP packets with a wrong |
| 867 | type. */ |
| 868 | } icmp; /**< ICMP statistics. */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 869 | struct { |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 870 | uip_stats_t drop; /**< Number of dropped TCP segments. */ |
| 871 | uip_stats_t recv; /**< Number of recived TCP segments. */ |
| 872 | uip_stats_t sent; /**< Number of sent TCP segments. */ |
| 873 | uip_stats_t chkerr; /**< Number of TCP segments with a bad |
| 874 | checksum. */ |
| 875 | uip_stats_t ackerr; /**< Number of TCP segments with a bad ACK |
| 876 | number. */ |
| 877 | uip_stats_t rst; /**< Number of recevied TCP RST (reset) segments. */ |
| 878 | uip_stats_t rexmit; /**< Number of retransmitted TCP segments. */ |
| 879 | uip_stats_t syndrop; /**< Number of dropped SYNs due to too few |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 880 | connections was avaliable. */ |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 881 | uip_stats_t synrst; /**< Number of SYNs for closed ports, |
| 882 | triggering a RST. */ |
| 883 | } tcp; /**< TCP statistics. */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 884 | }; |
| 885 | |
adamdunkels | b489e7a | 2003-10-14 11:12:50 +0000 | [diff] [blame] | 886 | /** |
| 887 | * The uIP TCP/IP statistics. |
| 888 | * |
| 889 | * This is the variable in which the uIP TCP/IP statistics are gathered. |
| 890 | */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 891 | extern struct uip_stats uip_stat; |
| 892 | |
| 893 | |
| 894 | /*-----------------------------------------------------------------------------------*/ |
| 895 | /* All the stuff below this point is internal to uIP and should not be |
| 896 | * used directly by an application or by a device driver. |
| 897 | */ |
| 898 | /*-----------------------------------------------------------------------------------*/ |
| 899 | /* u8_t uip_flags: |
| 900 | * |
| 901 | * When the application is called, uip_flags will contain the flags |
| 902 | * that are defined in this file. Please read below for more |
| 903 | * infomation. |
| 904 | */ |
| 905 | extern volatile u8_t uip_flags; |
| 906 | |
| 907 | /* The following flags may be set in the global variable uip_flags |
| 908 | before calling the application callback. The UIP_ACKDATA and |
| 909 | UIP_NEWDATA flags may both be set at the same time, whereas the |
| 910 | others are mutualy exclusive. Note that these flags should *NOT* be |
| 911 | accessed directly, but through the uIP functions/macros. */ |
| 912 | |
| 913 | #define UIP_ACKDATA 1 /* Signifies that the outstanding data was |
| 914 | acked and the application should send |
| 915 | out new data instead of retransmitting |
| 916 | the last data. */ |
| 917 | #define UIP_NEWDATA 2 /* Flags the fact that the peer has sent |
| 918 | us new data. */ |
| 919 | #define UIP_REXMIT 4 /* Tells the application to retransmit the |
| 920 | data that was last sent. */ |
| 921 | #define UIP_POLL 8 /* Used for polling the application, to |
| 922 | check if the application has data that |
| 923 | it wants to send. */ |
| 924 | #define UIP_CLOSE 16 /* The remote host has closed the |
| 925 | connection, thus the connection has |
| 926 | gone away. Or the application signals |
| 927 | that it wants to close the |
| 928 | connection. */ |
| 929 | #define UIP_ABORT 32 /* The remote host has aborted the |
| 930 | connection, thus the connection has |
| 931 | gone away. Or the application signals |
| 932 | that it wants to abort the |
| 933 | connection. */ |
| 934 | #define UIP_CONNECTED 64 /* We have got a connection from a remote |
| 935 | host and have set up a new connection |
| 936 | for it, or an active connection has |
| 937 | been successfully established. */ |
| 938 | |
| 939 | #define UIP_TIMEDOUT 128 /* The connection has been aborted due to |
| 940 | too many retransmissions. */ |
| 941 | |
| 942 | |
| 943 | /* uip_process(flag): |
| 944 | * |
| 945 | * The actual uIP function which does all the work. |
| 946 | */ |
| 947 | void uip_process(u8_t flag); |
| 948 | |
| 949 | /* The following flags are passed as an argument to the uip_process() |
| 950 | function. They are used to distinguish between the two cases where |
| 951 | uip_process() is called. It can be called either because we have |
| 952 | incoming data that should be processed, or because the periodic |
| 953 | timer has fired. */ |
| 954 | |
| 955 | #define UIP_DATA 1 /* Tells uIP that there is incoming data in |
| 956 | the uip_buf buffer. The length of the |
| 957 | data is stored in the global variable |
| 958 | uip_len. */ |
| 959 | #define UIP_TIMER 2 /* Tells uIP that the periodic timer has |
| 960 | fired. */ |
| 961 | #if UIP_UDP |
| 962 | #define UIP_UDP_TIMER 3 |
| 963 | #endif /* UIP_UDP */ |
| 964 | |
| 965 | /* The TCP states used in the uip_conn->tcpstateflags. */ |
| 966 | #define CLOSED 0 |
| 967 | #define SYN_RCVD 1 |
| 968 | #define SYN_SENT 2 |
| 969 | #define ESTABLISHED 3 |
| 970 | #define FIN_WAIT_1 4 |
| 971 | #define FIN_WAIT_2 5 |
| 972 | #define CLOSING 6 |
| 973 | #define TIME_WAIT 7 |
| 974 | #define LAST_ACK 8 |
| 975 | #define TS_MASK 15 |
| 976 | |
| 977 | #define UIP_STOPPED 16 |
| 978 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 979 | #define UIP_TCPIP_HLEN 40 |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 980 | |
| 981 | /* The TCP and IP headers. */ |
| 982 | typedef struct { |
| 983 | /* IP header. */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 984 | u8_t vhl, |
| 985 | tos, |
| 986 | len[2], |
| 987 | ipid[2], |
| 988 | ipoffset[2], |
| 989 | ttl, |
| 990 | proto; |
| 991 | u16_t ipchksum; |
| 992 | u16_t srcipaddr[2], |
| 993 | destipaddr[2]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 994 | |
| 995 | /* TCP header. */ |
| 996 | u16_t srcport, |
| 997 | destport; |
| 998 | u8_t seqno[4], |
| 999 | ackno[4], |
| 1000 | tcpoffset, |
| 1001 | flags, |
| 1002 | wnd[2]; |
| 1003 | u16_t tcpchksum; |
| 1004 | u8_t urgp[2]; |
| 1005 | u8_t optdata[4]; |
| 1006 | } uip_tcpip_hdr; |
| 1007 | |
| 1008 | /* The ICMP and IP headers. */ |
| 1009 | typedef struct { |
| 1010 | /* IP header. */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 1011 | u8_t vhl, |
| 1012 | tos, |
| 1013 | len[2], |
| 1014 | ipid[2], |
| 1015 | ipoffset[2], |
| 1016 | ttl, |
| 1017 | proto; |
| 1018 | u16_t ipchksum; |
| 1019 | u16_t srcipaddr[2], |
| 1020 | destipaddr[2]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 1021 | /* ICMP (echo) header. */ |
| 1022 | u8_t type, icode; |
| 1023 | u16_t icmpchksum; |
| 1024 | u16_t id, seqno; |
| 1025 | } uip_icmpip_hdr; |
| 1026 | |
| 1027 | |
| 1028 | /* The UDP and IP headers. */ |
| 1029 | typedef struct { |
| 1030 | /* IP header. */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 1031 | u8_t vhl, |
| 1032 | tos, |
| 1033 | len[2], |
| 1034 | ipid[2], |
| 1035 | ipoffset[2], |
| 1036 | ttl, |
| 1037 | proto; |
| 1038 | u16_t ipchksum; |
| 1039 | u16_t srcipaddr[2], |
| 1040 | destipaddr[2]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 1041 | |
| 1042 | /* UDP header. */ |
| 1043 | u16_t srcport, |
| 1044 | destport; |
| 1045 | u16_t udplen; |
| 1046 | u16_t udpchksum; |
| 1047 | } uip_udpip_hdr; |
| 1048 | |
| 1049 | #define UIP_PROTO_ICMP 1 |
| 1050 | #define UIP_PROTO_TCP 6 |
| 1051 | #define UIP_PROTO_UDP 17 |
| 1052 | |
| 1053 | #if UIP_FIXEDADDR |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 1054 | extern const u16_t uip_hostaddr[2]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 1055 | #else /* UIP_FIXEDADDR */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 1056 | extern u16_t uip_hostaddr[2]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 1057 | #endif /* UIP_FIXEDADDR */ |
| 1058 | |
| 1059 | #endif /* __UIP_H__ */ |
| 1060 | |
| 1061 | |
adamdunkels | 0170b08 | 2003-10-01 11:25:37 +0000 | [diff] [blame] | 1062 | /** @} */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 1063 | |