blob: 591073712b08dd45c91e6dc80520807eafcaeba6 [file] [log] [blame]
adamdunkels1e45c6d2003-09-02 21:47:27 +00001/**
adamdunkels0170b082003-10-01 11:25:37 +00002 * \addtogroup uip
3 * @{
4 */
5
6/**
adamdunkels1e45c6d2003-09-02 21:47:27 +00007 * \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
adamdunkels0170b082003-10-01 11:25:37 +000017
adamdunkelsca9ddcb2003-03-19 14:13:31 +000018/*
adamdunkels1e45c6d2003-09-02 21:47:27 +000019 * Copyright (c) 2001-2003, Adam Dunkels.
adamdunkelsca9ddcb2003-03-19 14:13:31 +000020 * 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.
adamdunkels1e45c6d2003-09-02 21:47:27 +000030 * 3. The name of the author may not be used to endorse or promote
adamdunkelsca9ddcb2003-03-19 14:13:31 +000031 * 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 *
adamdunkelsb380a3e2004-09-17 20:59:23 +000048 * $Id: uip.h,v 1.13 2004/09/17 20:59:23 adamdunkels Exp $
adamdunkelsca9ddcb2003-03-19 14:13:31 +000049 *
50 */
51
52#ifndef __UIP_H__
53#define __UIP_H__
54
55#include "uipopt.h"
56
adamdunkelsca9ddcb2003-03-19 14:13:31 +000057/*-----------------------------------------------------------------------------------*/
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
adamdunkels1e45c6d2003-09-02 21:47:27 +000063/**
adamdunkels0170b082003-10-01 11:25:37 +000064 * \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/**
adamdunkels1e45c6d2003-09-02 21:47:27 +000072 * Set the IP address of this host.
adamdunkelsca9ddcb2003-03-19 14:13:31 +000073 *
adamdunkels1e45c6d2003-09-02 21:47:27 +000074 * 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.
adamdunkelsca9ddcb2003-03-19 14:13:31 +000077 *
adamdunkels1e45c6d2003-09-02 21:47:27 +000078 * \param addr A pointer to a 4-byte representation of the IP address.
adamdunkelsb489e7a2003-10-14 11:12:50 +000079 *
80 * \hideinitializer
adamdunkelsca9ddcb2003-03-19 14:13:31 +000081 */
adamdunkels18076c62004-06-06 06:16:03 +000082#define uip_sethostaddr(addr) do { uip_hostaddr[0] = ((u16_t *)(addr))[0]; \
83 uip_hostaddr[1] = ((u16_t *)(addr))[1]; } while(0)
adamdunkelsca9ddcb2003-03-19 14:13:31 +000084
adamdunkels1e45c6d2003-09-02 21:47:27 +000085/**
86 * Get the IP address of this host.
adamdunkels66c6af62003-04-16 18:28:16 +000087 *
adamdunkels1e45c6d2003-09-02 21:47:27 +000088 * 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.
adamdunkelsb489e7a2003-10-14 11:12:50 +000094 *
95 * \hideinitializer
adamdunkels66c6af62003-04-16 18:28:16 +000096 */
adamdunkels18076c62004-06-06 06:16:03 +000097#define uip_gethostaddr(addr) do { ((u16_t *)(addr))[0] = uip_hostaddr[0]; \
98 ((u16_t *)(addr))[1] = uip_hostaddr[1]; } while(0)
99
100/**
101 * Set the default router's IP address.
102 *
103 * \param addr A pointer to a 4-byte array containing the IP address
104 * of the default router.
105 *
106 * \hideinitializer
107 */
108#define uip_setdraddr(addr) do { uip_draddr[0] = ((u16_t *)(addr))[0]; \
109 uip_draddr[1] = ((u16_t *)(addr))[1]; } while(0)
110
111/**
112 * Set the netmask.
113 *
114 * \param addr A pointer to a 4-byte array containing the IP address
115 * of the netmask.
116 *
117 * \hideinitializer
118 */
119#define uip_setnetmask(addr) do { uip_netmask[0] = ((u16_t *)(addr))[0]; \
120 uip_netmask[1] = ((u16_t *)(addr))[1]; } while(0)
121
122
123/**
124 * Get the default router's IP address.
125 *
126 * \param addr A pointer to a 4-byte array that will be filled in with
127 * the IP address of the default router.
128 *
129 * \hideinitializer
130 */
131#define uip_getdraddr(addr) do { ((u16_t *)(addr))[0] = uip_draddr[0]; \
132 ((u16_t *)(addr))[1] = uip_draddr[1]; } while(0)
133
134/**
135 * Get the netmask.
136 *
137 * \param addr A pointer to a 4-byte array that will be filled in with
138 * the value of the netmask.
139 *
140 * \hideinitializer
141 */
142#define uip_getnetmask(addr) do { ((u16_t *)(addr))[0] = uip_netmask[0]; \
143 ((u16_t *)(addr))[1] = uip_netmask[1]; } while(0)
adamdunkels66c6af62003-04-16 18:28:16 +0000144
adamdunkels0170b082003-10-01 11:25:37 +0000145/** @} */
146
adamdunkelsb489e7a2003-10-14 11:12:50 +0000147/**
148 * \defgroup uipinit uIP initialization functions
149 * @{
150 *
151 * The uIP initialization functions are used for booting uIP.
152 */
adamdunkels0170b082003-10-01 11:25:37 +0000153
154/**
155 * uIP initialization function.
156 *
157 * This function should be called at boot up to initilize the uIP
158 * TCP/IP stack.
159 */
160void uip_init(void);
161
adamdunkelsb489e7a2003-10-14 11:12:50 +0000162/** @} */
163
adamdunkels0170b082003-10-01 11:25:37 +0000164/**
165 * \defgroup uipdevfunc uIP device driver functions
166 * @{
167 *
168 * These functions are used by a network device driver for interacting
169 * with uIP.
170 */
171
adamdunkels1e45c6d2003-09-02 21:47:27 +0000172/**
173 * Process an incoming packet.
174 *
175 * This function should be called when the device driver has received
176 * a packet from the network. The packet from the device driver must
177 * be present in the uip_buf buffer, and the length of the packet
178 * should be placed in the uip_len variable.
179 *
180 * When the function returns, there may be an outbound packet placed
181 * in the uip_buf packet buffer. If so, the uip_len variable is set to
182 * the length of the packet. If no packet is to be sent out, the
183 * uip_len variable is set to 0.
184 *
185 * The usual way of calling the function is presented by the source
186 * code below.
187 \code
188 uip_len = devicedriver_poll();
189 if(uip_len > 0) {
190 uip_input();
191 if(uip_len > 0) {
192 devicedriver_send();
193 }
194 }
195 \endcode
196 *
197 * \note If you are writing a uIP device driver that needs ARP
198 * (Address Resolution Protocol), e.g., when running uIP over
199 * Ethernet, you will need to call the uIP ARP code before calling
200 * this function:
201 \code
202 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
203 uip_len = ethernet_devicedrver_poll();
204 if(uip_len > 0) {
205 if(BUF->type == HTONS(UIP_ETHTYPE_IP)) {
206 uip_arp_ipin();
adamdunkels1e45c6d2003-09-02 21:47:27 +0000207 uip_input();
208 if(uip_len > 0) {
209 uip_arp_out();
210 ethernet_devicedriver_send();
211 }
212 } else if(BUF->type == HTONS(UIP_ETHTYPE_ARP)) {
213 uip_arp_arpin();
214 if(uip_len > 0) {
215 ethernet_devicedriver_send();
216 }
217 }
218 \endcode
adamdunkelsb489e7a2003-10-14 11:12:50 +0000219 *
220 * \hideinitializer
adamdunkels1e45c6d2003-09-02 21:47:27 +0000221 */
222#define uip_input() uip_process(UIP_DATA)
223
224/**
adamdunkels1e45c6d2003-09-02 21:47:27 +0000225 * Periodic processing for a connection identified by its number.
226 *
227 * This function does the necessary periodic processing (timers,
228 * polling) for a uIP TCP conneciton, and should be called when the
229 * periodic uIP timer goes off. It should be called for every
230 * connection, regardless of whether they are open of closed.
231 *
232 * When the function returns, it may have an outbound packet waiting
233 * for service in the uIP packet buffer, and if so the uip_len
234 * variable is set to a value larger than zero. The device driver
235 * should be called to send out the packet.
236 *
237 * The ususal way of calling the function is through a for() loop like
238 * this:
239 \code
240 for(i = 0; i < UIP_CONNS; ++i) {
241 uip_periodic(i);
242 if(uip_len > 0) {
243 devicedriver_send();
244 }
245 }
246 \endcode
247 *
248 * \note If you are writing a uIP device driver that needs ARP
249 * (Address Resolution Protocol), e.g., when running uIP over
250 * Ethernet, you will need to call the uip_arp_out() function before
251 * calling the device driver:
252 \code
253 for(i = 0; i < UIP_CONNS; ++i) {
254 uip_periodic(i);
255 if(uip_len > 0) {
256 uip_arp_out();
257 ethernet_devicedriver_send();
258 }
259 }
260 \endcode
261 *
262 * \param conn The number of the connection which is to be periodically polled.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000263 *
264 * \hideinitializer
adamdunkels1e45c6d2003-09-02 21:47:27 +0000265 */
266#define uip_periodic(conn) do { uip_conn = &uip_conns[conn]; \
267 uip_process(UIP_TIMER); } while (0)
268
269/**
270 * Periodic processing for a connection identified by a pointer to its structure.
271 *
272 * Same as uip_periodic() but takes a pointer to the actual uip_conn
273 * struct instead of an integer as its argument. This function can be
274 * used to force periodic processing of a specific connection.
275 *
276 * \param conn A pointer to the uip_conn struct for the connection to
277 * be processed.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000278 *
279 * \hideinitializer
adamdunkels1e45c6d2003-09-02 21:47:27 +0000280 */
281#define uip_periodic_conn(conn) do { uip_conn = conn; \
282 uip_process(UIP_TIMER); } while (0)
283
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000284#if UIP_UDP
adamdunkels1e45c6d2003-09-02 21:47:27 +0000285/**
286 * Periodic processing for a UDP connection identified by its number.
287 *
288 * This function is essentially the same as uip_prerioic(), but for
289 * UDP connections. It is called in a similar fashion as the
290 * uip_periodic() function:
291 \code
292 for(i = 0; i < UIP_UDP_CONNS; i++) {
293 uip_udp_periodic(i);
294 if(uip_len > 0) {
295 devicedriver_send();
296 }
297 }
298 \endcode
299 *
300 * \note As for the uip_periodic() function, special care has to be
301 * taken when using uIP together with ARP and Ethernet:
302 \code
303 for(i = 0; i < UIP_UDP_CONNS; i++) {
304 uip_udp_periodic(i);
305 if(uip_len > 0) {
306 uip_arp_out();
307 ethernet_devicedriver_send();
308 }
309 }
310 \endcode
311 *
312 * \param conn The number of the UDP connection to be processed.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000313 *
314 * \hideinitializer
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000315 */
316#define uip_udp_periodic(conn) do { uip_udp_conn = &uip_udp_conns[conn]; \
317 uip_process(UIP_UDP_TIMER); } while (0)
adamdunkelseb91d5f2003-08-20 20:58:08 +0000318
adamdunkels1e45c6d2003-09-02 21:47:27 +0000319/**
320 * Periodic processing for a UDP connection identified by a pointer to
321 * its structure.
322 *
323 * Same as uip_udp_periodic() but takes a pointer to the actual
324 * uip_conn struct instead of an integer as its argument. This
325 * function can be used to force periodic processing of a specific
326 * connection.
327 *
328 * \param conn A pointer to the uip_udp_conn struct for the connection
329 * to be processed.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000330 *
331 * \hideinitializer
adamdunkelseb91d5f2003-08-20 20:58:08 +0000332 */
333#define uip_udp_periodic_conn(conn) do { uip_udp_conn = conn; \
334 uip_process(UIP_UDP_TIMER); } while (0)
adamdunkelsb489e7a2003-10-14 11:12:50 +0000335
336
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000337#endif /* UIP_UDP */
adamdunkels0170b082003-10-01 11:25:37 +0000338
339/**
340 * The uIP packet buffer.
341 *
342 * The uip_buf array is used to hold incoming and outgoing
343 * packets. The device driver should place incoming data into this
344 * buffer. When sending data, the device driver should read the link
345 * level headers and the TCP/IP headers from this buffer. The size of
346 * the link level headers is configured by the UIP_LLH_LEN define.
347 *
348 * \note The application data need not be placed in this buffer, so
349 * the device driver must read it from the place pointed to by the
350 * uip_appdata pointer as illustrated by the following example:
351 \code
352 void
353 devicedriver_send(void)
354 {
355 hwsend(&uip_buf[0], UIP_LLH_LEN);
356 hwsend(&uip_buf[UIP_LLH_LEN], 40);
357 hwsend(uip_appdata, uip_len - 40 - UIP_LLH_LEN);
358 }
359 \endcode
360 */
361extern u8_t uip_buf[UIP_BUFSIZE+2];
362
363/** @} */
364
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000365/*-----------------------------------------------------------------------------------*/
366/* Functions that are used by the uIP application program. Opening and
367 * closing connections, sending and receiving data, etc. is all
368 * handled by the functions below.
369*/
adamdunkels0170b082003-10-01 11:25:37 +0000370/**
371 * \defgroup uipappfunc uIP application functions
372 * @{
373 *
374 * Functions used by an application running of top of uIP.
375 */
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000376
adamdunkels1e45c6d2003-09-02 21:47:27 +0000377/**
378 * Start listening to the specified port.
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000379 *
adamdunkels1e45c6d2003-09-02 21:47:27 +0000380 * \note Since this function expects the port number in network byte
381 * order, a conversion using HTONS() or htons() is necessary.
382 *
383 \code
384 uip_listen(HTONS(80));
385 \endcode
386 *
387 * \param port A 16-bit port number in network byte order.
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000388 */
389void uip_listen(u16_t port);
390
adamdunkels1e45c6d2003-09-02 21:47:27 +0000391/**
392 * Stop listening to the specified port.
adamdunkelscd8c3a22003-08-13 22:52:48 +0000393 *
adamdunkels1e45c6d2003-09-02 21:47:27 +0000394 * \note Since this function expects the port number in network byte
395 * order, a conversion using HTONS() or htons() is necessary.
396 *
397 \code
398 uip_unlisten(HTONS(80));
399 \endcode
400 *
401 * \param port A 16-bit port number in network byte order.
adamdunkelscd8c3a22003-08-13 22:52:48 +0000402 */
403void uip_unlisten(u16_t port);
404
adamdunkels1e45c6d2003-09-02 21:47:27 +0000405/**
406 * Connect to a remote host using TCP.
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000407 *
adamdunkels1e45c6d2003-09-02 21:47:27 +0000408 * This function is used to start a new connection to the specified
409 * port on the specied host. It allocates a new connection identifier,
410 * sets the connection to the SYN_SENT state and sets the
411 * retransmission timer to 0. This will cause a TCP SYN segment to be
412 * sent out the next time this connection is periodically processed,
413 * which usually is done within 0.5 seconds after the call to
414 * uip_connect().
415 *
416 * \note This function is avaliable only if support for active open
417 * has been configured by defining UIP_ACTIVE_OPEN to 1 in uipopt.h.
418 *
419 * \note Since this function requires the port number to be in network
420 * byte order, a convertion using HTONS() or htons() is necessary.
421 *
422 \code
423 u16_t ipaddr[2];
424
425 uip_ipaddr(ipaddr, 192,168,1,2);
426 uip_connect(ipaddr, HTONS(80));
427 \endcode
428 *
429 * \param ripaddr A pointer to a 4-byte array representing the IP
430 * address of the remote hot.
431 *
432 * \param port A 16-bit port number in network byte order.
433 *
434 * \return A pointer to the uIP connection identifier for the new connection,
435 * or NULL if no connection could be allocated.
436 *
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000437 */
438struct uip_conn *uip_connect(u16_t *ripaddr, u16_t port);
439
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000440
441
adamdunkels0170b082003-10-01 11:25:37 +0000442/**
adamdunkelsb489e7a2003-10-14 11:12:50 +0000443 * \internal
444 *
adamdunkels0170b082003-10-01 11:25:37 +0000445 * Check if a connection has outstanding (i.e., unacknowledged) data.
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000446 *
adamdunkels0170b082003-10-01 11:25:37 +0000447 * \param conn A pointer to the uip_conn structure for the connection.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000448 *
449 * \hideinitializer
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000450 */
451#define uip_outstanding(conn) ((conn)->len)
452
adamdunkels0170b082003-10-01 11:25:37 +0000453/**
454 * Send data on the current connection.
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000455 *
adamdunkels0170b082003-10-01 11:25:37 +0000456 * This function is used to send out a single segment of TCP
457 * data. Only applications that have been invoked by uIP for event
458 * processing can send data.
459 *
adamdunkelsb489e7a2003-10-14 11:12:50 +0000460 * The amount of data that actually is sent out after a call to this
461 * funcion is determined by the maximum amount of data TCP allows. uIP
462 * will automatically crop the data so that only the appropriate
463 * amount of data is sent. The function uip_mss() can be used to query
464 * uIP for the amount of data that actually will be sent.
465 *
adamdunkels0170b082003-10-01 11:25:37 +0000466 * \note This function does not guarantee that the sent data will
467 * arrive at the destination. If the data is lost in the network, the
468 * application will be invoked with the uip_rexmit() event being
469 * set. The application will then have to resend the data using this
470 * function.
471 *
472 * \param data A pointer to the data which is to be sent.
473 *
adamdunkelsb489e7a2003-10-14 11:12:50 +0000474 * \param len The maximum amount of data bytes to be sent.
475 *
476 * \hideinitializer
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000477 */
adamdunkels18076c62004-06-06 06:16:03 +0000478#define uip_send(data, len) do { uip_sappdata = (void *)(data); uip_slen = (len);} while(0)
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000479
adamdunkels0170b082003-10-01 11:25:37 +0000480/**
481 * The length of any incoming data that is currently avaliable (if avaliable)
482 * in the uip_appdata buffer.
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000483 *
adamdunkels0170b082003-10-01 11:25:37 +0000484 * The test function uip_data() must first be used to check if there
485 * is any data available at all.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000486 *
487 * \hideinitializer
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000488 */
489#define uip_datalen() uip_len
490
adamdunkels0170b082003-10-01 11:25:37 +0000491/**
492 * The length of any out-of-band data (urgent data) that has arrived
493 * on the connection.
494 *
495 * \note The configuration parameter UIP_URGDATA must be set for this
496 * function to be enabled.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000497 *
498 * \hideinitializer
adamdunkels0170b082003-10-01 11:25:37 +0000499 */
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000500#define uip_urgdatalen() uip_urglen
501
adamdunkels0170b082003-10-01 11:25:37 +0000502/**
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000503 * Close the current connection.
adamdunkels0170b082003-10-01 11:25:37 +0000504 *
505 * This function will close the current connection in a nice way.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000506 *
507 * \hideinitializer
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000508 */
509#define uip_close() (uip_flags = UIP_CLOSE)
510
adamdunkels0170b082003-10-01 11:25:37 +0000511/**
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000512 * Abort the current connection.
adamdunkels0170b082003-10-01 11:25:37 +0000513 *
514 * This function will abort (reset) the current connection, and is
515 * usually used when an error has occured that prevents using the
516 * uip_close() function.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000517 *
518 * \hideinitializer
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000519 */
520#define uip_abort() (uip_flags = UIP_ABORT)
521
adamdunkels0170b082003-10-01 11:25:37 +0000522/**
523 * Tell the sending host to stop sending data.
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000524 *
adamdunkels0170b082003-10-01 11:25:37 +0000525 * This function will close our receiver's window so that we stop
526 * receiving data for the current connection.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000527 *
528 * \hideinitializer
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000529 */
530#define uip_stop() (uip_conn->tcpstateflags |= UIP_STOPPED)
531
adamdunkels0170b082003-10-01 11:25:37 +0000532/**
533 * Find out if the current connection has been previously stopped with
534 * uip_stop().
adamdunkelsb489e7a2003-10-14 11:12:50 +0000535 *
536 * \hideinitializer
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000537 */
538#define uip_stopped(conn) ((conn)->tcpstateflags & UIP_STOPPED)
539
adamdunkels0170b082003-10-01 11:25:37 +0000540/**
541 * Restart the current connection, if is has previously been stopped
542 * with uip_stop().
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000543 *
adamdunkels0170b082003-10-01 11:25:37 +0000544 * This function will open the receiver's window again so that we
545 * start receiving data for the current connection.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000546 *
547 * \hideinitializer
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000548 */
549#define uip_restart() do { uip_flags |= UIP_NEWDATA; \
550 uip_conn->tcpstateflags &= ~UIP_STOPPED; \
551 } while(0)
552
553
554/* uIP tests that can be made to determine in what state the current
555 connection is, and what the application function should do. */
556
adamdunkels0170b082003-10-01 11:25:37 +0000557/**
adamdunkels18076c62004-06-06 06:16:03 +0000558 * Is the current connection a UDP connection?
559 *
560 * This function checks whether the current connection is a UDP connection.
561 *
562 * \hideinitializer
563 *
564 */
565#define uip_udpconnection() (uip_conn == NULL)
566
567/**
adamdunkels0170b082003-10-01 11:25:37 +0000568 * Is new incoming data available?
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000569 *
570 * Will reduce to non-zero if there is new data for the application
571 * present at the uip_appdata pointer. The size of the data is
572 * avaliable through the uip_len variable.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000573 *
574 * \hideinitializer
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000575 */
576#define uip_newdata() (uip_flags & UIP_NEWDATA)
577
adamdunkels0170b082003-10-01 11:25:37 +0000578/**
579 * Has previously sent data been acknowledged?
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000580 *
581 * Will reduce to non-zero if the previously sent data has been
582 * acknowledged by the remote host. This means that the application
adamdunkels0170b082003-10-01 11:25:37 +0000583 * can send new data.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000584 *
585 * \hideinitializer
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000586 */
587#define uip_acked() (uip_flags & UIP_ACKDATA)
adamdunkels0170b082003-10-01 11:25:37 +0000588
589/**
adamdunkels0170b082003-10-01 11:25:37 +0000590 * Has the connection just been connected?
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000591 *
592 * Reduces to non-zero if the current connection has been connected to
593 * a remote host. This will happen both if the connection has been
594 * actively opened (with uip_connect()) or passively opened (with
595 * uip_listen()).
adamdunkelsb489e7a2003-10-14 11:12:50 +0000596 *
597 * \hideinitializer
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000598 */
599#define uip_connected() (uip_flags & UIP_CONNECTED)
600
adamdunkels0170b082003-10-01 11:25:37 +0000601/**
602 * Has the connection been closed by the other end?
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000603 *
604 * Is non-zero if the connection has been closed by the remote
adamdunkels0170b082003-10-01 11:25:37 +0000605 * host. The application may then do the necessary clean-ups.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000606 *
607 * \hideinitializer
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000608 */
609#define uip_closed() (uip_flags & UIP_CLOSE)
610
adamdunkels0170b082003-10-01 11:25:37 +0000611/**
612 * Has the connection been aborted by the other end?
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000613 *
614 * Non-zero if the current connection has been aborted (reset) by the
615 * remote host.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000616 *
617 * \hideinitializer
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000618 */
619#define uip_aborted() (uip_flags & UIP_ABORT)
620
adamdunkels0170b082003-10-01 11:25:37 +0000621/**
622 * Has the connection timed out?
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000623 *
624 * Non-zero if the current connection has been aborted due to too many
625 * retransmissions.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000626 *
627 * \hideinitializer
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000628 */
adamdunkels399a0782004-02-16 20:52:07 +0000629#define uip_timedout() (uip_flags & UIP_TIMEDOUT)
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000630
adamdunkels0170b082003-10-01 11:25:37 +0000631/**
632 * Do we need to retransmit previously data?
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000633 *
634 * Reduces to non-zero if the previously sent data has been lost in
635 * the network, and the application should retransmit it. The
adamdunkels0170b082003-10-01 11:25:37 +0000636 * application should send the exact same data as it did the last
637 * time, using the uip_send() function.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000638 *
639 * \hideinitializer
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000640 */
641#define uip_rexmit() (uip_flags & UIP_REXMIT)
642
adamdunkels0170b082003-10-01 11:25:37 +0000643/**
644 * Is the connection being polled by uIP?
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000645 *
646 * Is non-zero if the reason the application is invoked is that the
647 * current connection has been idle for a while and should be
648 * polled.
adamdunkels0170b082003-10-01 11:25:37 +0000649 *
650 * The polling event can be used for sending data without having to
651 * wait for the remote host to send data.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000652 *
653 * \hideinitializer
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000654 */
655#define uip_poll() (uip_flags & UIP_POLL)
656
adamdunkels0170b082003-10-01 11:25:37 +0000657/**
658 * Get the initial maxium segment size (MSS) of the current
659 * connection.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000660 *
661 * \hideinitializer
adamdunkels0170b082003-10-01 11:25:37 +0000662 */
663#define uip_initialmss() (uip_conn->initialmss)
664
665/**
adamdunkelsb489e7a2003-10-14 11:12:50 +0000666 * Get the current maxium segment size that can be sent on the current
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000667 * connection.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000668 *
669 * The current maxiumum segment size that can be sent on the
670 * connection is computed from the receiver's window and the MSS of
671 * the connection (which also is available by calling
672 * uip_initialmss()).
673 *
674 * \hideinitializer
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000675 */
676#define uip_mss() (uip_conn->mss)
677
adamdunkelsb489e7a2003-10-14 11:12:50 +0000678/**
679 * Set up a new UDP connection.
680 *
681 * \param ripaddr A pointer to a 4-byte structure representing the IP
682 * address of the remote host.
683 *
684 * \param rport The remote port number in network byte order.
685 *
686 * \return The uip_udp_conn structure for the new connection or NULL
687 * if no connection could be allocated.
688 */
689struct uip_udp_conn *uip_udp_new(u16_t *ripaddr, u16_t rport);
690
691/**
692 * Removed a UDP connection.
693 *
694 * \param conn A pointer to the uip_udp_conn structure for the connection.
695 *
696 * \hideinitializer
697 */
698#define uip_udp_remove(conn) (conn)->lport = 0
699
700/**
adamdunkels37f0b8d2004-07-04 17:00:50 +0000701 * Bind a UDP connection to a local port.
702 *
703 * \param conn A pointer to the uip_udp_conn structure for the
704 * connection.
705 *
706 * \param port The local port number, in network byte order.
707 *
708 * \hideinitializer
709 */
710#define uip_udp_bind(conn, port) (conn)->lport = port
711
712/**
adamdunkelsb489e7a2003-10-14 11:12:50 +0000713 * Send a UDP datagram of length len on the current connection.
714 *
715 * This function can only be called in response to a UDP event (poll
716 * or newdata). The data must be present in the uip_buf buffer, at the
717 * place pointed to by the uip_appdata pointer.
718 *
719 * \param len The length of the data in the uip_buf buffer.
720 *
721 * \hideinitializer
722 */
723#define uip_udp_send(len) uip_slen = (len)
724
adamdunkels0170b082003-10-01 11:25:37 +0000725/** @} */
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000726
727/* uIP convenience and converting functions. */
728
adamdunkels0170b082003-10-01 11:25:37 +0000729/**
730 * \defgroup uipconvfunc uIP conversion functions
731 * @{
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000732 *
adamdunkels0170b082003-10-01 11:25:37 +0000733 * These functions can be used for converting between different data
734 * formats used by uIP.
735 */
736
737/**
738 * Pack an IP address into a 4-byte array which is used by uIP to
739 * represent IP addresses.
740 *
741 * Example:
742 \code
743 u16_t ipaddr[2];
744
745 uip_ipaddr(&ipaddr, 192,168,1,2);
746 \endcode
747 *
748 * \param addr A pointer to a 4-byte array that will be filled in with
749 * the IP addres.
750 * \param addr0 The first octet of the IP address.
751 * \param addr1 The second octet of the IP address.
752 * \param addr2 The third octet of the IP address.
753 * \param addr3 The forth octet of the IP address.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000754 *
755 * \hideinitializer
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000756 */
757#define uip_ipaddr(addr, addr0,addr1,addr2,addr3) do { \
adamdunkels18076c62004-06-06 06:16:03 +0000758 ((u16_t *)(addr))[0] = HTONS(((addr0) << 8) | (addr1)); \
759 ((u16_t *)(addr))[1] = HTONS(((addr2) << 8) | (addr3)); \
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000760 } while(0)
761
adamdunkels0170b082003-10-01 11:25:37 +0000762/**
adamdunkels18076c62004-06-06 06:16:03 +0000763 * Copy an IP address to another IP address.
764 *
765 * Copies an IP address from one place to another.
766 *
767 * Example:
768 \code
769 u16_t ipaddr1[2], ipaddr2[2];
770
771 uip_ipaddr(ipaddr1, 192,16,1,2);
772 uip_ipaddr_copy(ipaddr2, ipaddr1);
773 \endcode
774 *
775 * \param dest The destination for the copy.
776 * \param src The source from where to copy.
777 *
778 * \hideinitializer
779 */
780#define uip_ipaddr_copy(dest, src) do { \
781 ((u16_t *)dest)[0] = ((u16_t *)src)[0]; \
782 ((u16_t *)dest)[1] = ((u16_t *)src)[1]; \
783 } while(0)
784/**
785 * Compare two IP addresses
786 *
787 * Compares two IP addresses.
788 *
789 * Example:
790 \code
791 u16_t ipaddr1[2], ipaddr2[2];
792
793 uip_ipaddr(ipaddr1, 192,16,1,2);
794 if(uip_ipaddr_cmp(ipaddr2, ipaddr1)) {
795 printf("They are the same");
796 }
797 \endcode
798 *
799 * \param addr1 The first IP address.
800 * \param addr2 The second IP address.
801 *
802 * \hideinitializer
803 */
804#define uip_ipaddr_cmp(addr1, addr2) (((u16_t *)addr1)[0] == ((u16_t *)addr2)[0] && \
805 ((u16_t *)addr1)[1] == ((u16_t *)addr2)[1])
806
807/**
808 * Compare two IP addresses with netmasks
809 *
810 * Compares two IP addresses with netmasks. The masks are used to mask
811 * out the bits that are to be compared.
812 *
813 * Example:
814 \code
815 u16_t ipaddr1[2], ipaddr2[2], mask[2];
816
817 uip_ipaddr(mask, 255,255,255,0);
818 uip_ipaddr(ipaddr1, 192,16,1,2);
819 uip_ipaddr(ipaddr2, 192,16,1,3);
820 if(uip_ipaddr_maskcmp(ipaddr1, ipaddr2, mask)) {
821 printf("They are the same");
822 }
823 \endcode
824 *
825 * \param addr1 The first IP address.
826 * \param addr2 The second IP address.
827 * \param mask The netmask.
828 *
829 * \hideinitializer
830 */
831#define uip_ipaddr_maskcmp(addr1, addr2, mask) \
832 (((((u16_t *)addr1)[0] & ((u16_t *)mask)[0]) == \
833 (((u16_t *)addr2)[0] & ((u16_t *)mask)[0])) && \
834 ((((u16_t *)addr1)[1] & ((u16_t *)mask)[1]) == \
835 (((u16_t *)addr2)[1] & ((u16_t *)mask)[1])))
836
837
838/**
839 * Mask out the network part of an IP address.
840 *
841 * Masks out the network part of an IP address, given the address and
842 * the netmask.
843 *
844 * Example:
845 \code
846 u16_t ipaddr1[2], ipaddr2[2], netmask[2];
847
848 uip_ipaddr(ipaddr1, 192,16,1,2);
849 uip_ipaddr(netmask, 255,255,255,0);
850 uip_ipaddr_mask(ipaddr2, ipaddr1, netmask);
851 \endcode
852 *
853 * In the example above, the variable "ipaddr2" will contain the IP
854 * address 192.168.1.0.
855 *
856 * \param dest Where the result is to be placed.
857 * \param src The IP address.
858 * \param mask The netmask.
859 *
860 * \hideinitializer
861 */
862#define uip_ipaddr_mask(dest, src, mask) do { \
863 ((u16_t *)dest)[0] = ((u16_t *)src)[0] & ((u16_t *)mask)[0]; \
864 ((u16_t *)dest)[1] = ((u16_t *)src)[1] & ((u16_t *)mask)[1]; \
865 } while(0)
866
867/**
868 * Pick the first octet of an IP address.
869 *
870 * Picks out the first octet of an IP address.
871 *
872 * Example:
873 \code
874 u16_t ipaddr[2];
875 u8_t octet;
876
877 uip_ipaddr(ipaddr, 1,2,3,4);
878 octet = uip_ipaddr1(ipaddr);
879 \endcode
880 *
881 * In the example above, the variable "octet" will contain the value 1.
882 *
883 * \hideinitializer
884 */
885#define uip_ipaddr1(addr) (htons(((u16_t *)(addr))[0]) >> 8)
886
887/**
888 * Pick the second octet of an IP address.
889 *
890 * Picks out the second octet of an IP address.
891 *
892 * Example:
893 \code
894 u16_t ipaddr[2];
895 u8_t octet;
896
897 uip_ipaddr(ipaddr, 1,2,3,4);
898 octet = uip_ipaddr2(ipaddr);
899 \endcode
900 *
901 * In the example above, the variable "octet" will contain the value 2.
902 *
903 * \hideinitializer
904 */
905#define uip_ipaddr2(addr) (htons(((u16_t *)(addr))[0]) & 0xff)
906
907/**
908 * Pick the third octet of an IP address.
909 *
910 * Picks out the third octet of an IP address.
911 *
912 * Example:
913 \code
914 u16_t ipaddr[2];
915 u8_t octet;
916
917 uip_ipaddr(ipaddr, 1,2,3,4);
918 octet = uip_ipaddr3(ipaddr);
919 \endcode
920 *
921 * In the example above, the variable "octet" will contain the value 3.
922 *
923 * \hideinitializer
924 */
925#define uip_ipaddr3(addr) (htons(((u16_t *)(addr))[1]) >> 8)
926
927/**
928 * Pick the fourth octet of an IP address.
929 *
930 * Picks out the fourth octet of an IP address.
931 *
932 * Example:
933 \code
934 u16_t ipaddr[2];
935 u8_t octet;
936
937 uip_ipaddr(ipaddr, 1,2,3,4);
938 octet = uip_ipaddr4(ipaddr);
939 \endcode
940 *
941 * In the example above, the variable "octet" will contain the value 4.
942 *
943 * \hideinitializer
944 */
945#define uip_ipaddr4(addr) (htons(((u16_t *)(addr))[1]) & 0xff)
946
947/**
adamdunkels0170b082003-10-01 11:25:37 +0000948 * Convert 16-bit quantity from host byte order to network byte order.
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000949 *
adamdunkels0170b082003-10-01 11:25:37 +0000950 * This macro is primarily used for converting constants from host
951 * byte order to network byte order. For converting variables to
952 * network byte order, use the htons() function instead.
adamdunkelsb489e7a2003-10-14 11:12:50 +0000953 *
954 * \hideinitializer
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000955 */
adamdunkels47ec7fa2003-03-28 12:11:17 +0000956#ifndef HTONS
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000957# if BYTE_ORDER == BIG_ENDIAN
adamdunkels47ec7fa2003-03-28 12:11:17 +0000958# define HTONS(n) (n)
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000959# else /* BYTE_ORDER == BIG_ENDIAN */
adamdunkels47ec7fa2003-03-28 12:11:17 +0000960# define HTONS(n) ((((u16_t)((n) & 0xff)) << 8) | (((n) & 0xff00) >> 8))
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000961# endif /* BYTE_ORDER == BIG_ENDIAN */
adamdunkels47ec7fa2003-03-28 12:11:17 +0000962#endif /* HTONS */
963
adamdunkels0170b082003-10-01 11:25:37 +0000964/**
965 * Convert 16-bit quantity from host byte order to network byte order.
966 *
967 * This function is primarily used for converting variables from host
968 * byte order to network byte order. For converting constants to
969 * network byte order, use the HTONS() macro instead.
970 */
adamdunkels47ec7fa2003-03-28 12:11:17 +0000971#ifndef htons
972u16_t htons(u16_t val);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000973#endif /* htons */
974
adamdunkels0170b082003-10-01 11:25:37 +0000975/** @} */
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000976
adamdunkels0170b082003-10-01 11:25:37 +0000977/**
978 * Pointer to the application data in the packet buffer.
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000979 *
980 * This pointer points to the application data when the application is
adamdunkels0170b082003-10-01 11:25:37 +0000981 * called. If the application wishes to send data, the application may
982 * use this space to write the data into before calling uip_send().
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000983 */
adamdunkels18076c62004-06-06 06:16:03 +0000984extern u8_t *uip_appdata;
985extern u8_t *uip_sappdata;
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000986
987#if UIP_URGDATA > 0
988/* u8_t *uip_urgdata:
989 *
990 * This pointer points to any urgent data that has been received. Only
991 * present if compiled with support for urgent data (UIP_URGDATA).
992 */
adamdunkels18076c62004-06-06 06:16:03 +0000993extern u8_t *uip_urgdata;
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000994#endif /* UIP_URGDATA > 0 */
995
996
997/* u[8|16]_t uip_len:
998 *
999 * When the application is called, uip_len contains the length of any
1000 * new data that has been received from the remote host. The
1001 * application should set this variable to the size of any data that
1002 * the application wishes to send. When the network device driver
1003 * output function is called, uip_len should contain the length of the
1004 * outgoing packet.
1005 */
adamdunkels18076c62004-06-06 06:16:03 +00001006extern u16_t uip_len, uip_slen;
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001007
1008#if UIP_URGDATA > 0
adamdunkels18076c62004-06-06 06:16:03 +00001009extern u8_t uip_urglen, uip_surglen;
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001010#endif /* UIP_URGDATA > 0 */
1011
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001012
adamdunkels0170b082003-10-01 11:25:37 +00001013/**
1014 * Representation of a uIP TCP connection.
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001015 *
1016 * The uip_conn structure is used for identifying a connection. All
1017 * but one field in the structure are to be considered read-only by an
1018 * application. The only exception is the appstate field whos purpose
1019 * is to let the application store application-specific state (e.g.,
1020 * file pointers) for the connection. The size of this field is
1021 * configured in the "uipopt.h" header file.
1022 */
1023struct uip_conn {
adamdunkels0170b082003-10-01 11:25:37 +00001024 u16_t ripaddr[2]; /**< The IP address of the remote host. */
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001025
adamdunkels0170b082003-10-01 11:25:37 +00001026 u16_t lport; /**< The local TCP port, in network byte order. */
1027 u16_t rport; /**< The local remote TCP port, in network byte
1028 order. */
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001029
adamdunkels0170b082003-10-01 11:25:37 +00001030 u8_t rcv_nxt[4]; /**< The sequence number that we expect to
1031 receive next. */
1032 u8_t snd_nxt[4]; /**< The sequence number that was last sent by
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001033 us. */
adamdunkels0170b082003-10-01 11:25:37 +00001034 u16_t len; /**< Length of the data that was previously sent. */
1035 u16_t mss; /**< Current maximum segment size for the
1036 connection. */
1037 u16_t initialmss; /**< Initial maximum segment size for the
1038 connection. */
adamdunkelsb489e7a2003-10-14 11:12:50 +00001039 u8_t sa; /**< Retransmission time-out calculation state
1040 variable. */
1041 u8_t sv; /**< Retransmission time-out calculation state
1042 variable. */
1043 u8_t rto; /**< Retransmission time-out. */
adamdunkels0170b082003-10-01 11:25:37 +00001044 u8_t tcpstateflags; /**< TCP state and flags. */
1045 u8_t timer; /**< The retransmission timer. */
1046 u8_t nrtx; /**< The number of retransmissions for the last
1047 segment sent. */
1048
1049 /** The application state. */
1050 u8_t appstate[UIP_APPSTATE_SIZE];
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001051};
1052
adamdunkels0170b082003-10-01 11:25:37 +00001053
1054/* Pointer to the current connection. */
1055extern struct uip_conn *uip_conn;
1056/* The array containing all uIP connections. */
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001057extern struct uip_conn uip_conns[UIP_CONNS];
adamdunkels0170b082003-10-01 11:25:37 +00001058/**
1059 * \addtogroup uiparch
1060 * @{
1061 */
1062
1063/**
1064 * 4-byte array used for the 32-bit sequence number calculations.
1065 */
adamdunkels18076c62004-06-06 06:16:03 +00001066extern u8_t uip_acc32[4];
adamdunkels0170b082003-10-01 11:25:37 +00001067
1068/** @} */
1069
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001070
1071#if UIP_UDP
adamdunkels1e45c6d2003-09-02 21:47:27 +00001072/**
1073 * Representation of a uIP UDP connection.
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001074 */
1075struct uip_udp_conn {
adamdunkelsb489e7a2003-10-14 11:12:50 +00001076 u16_t ripaddr[2]; /**< The IP address of the remote peer. */
1077 u16_t lport; /**< The local port number in network byte order. */
1078 u16_t rport; /**< The remote port number in network byte order. */
adamdunkels399a0782004-02-16 20:52:07 +00001079
1080 /** The application state. */
1081 u8_t appstate[UIP_APPSTATE_SIZE];
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001082};
1083
1084extern struct uip_udp_conn *uip_udp_conn;
1085extern struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];
1086#endif /* UIP_UDP */
1087
adamdunkelsb489e7a2003-10-14 11:12:50 +00001088/**
1089 * The structure holding the TCP/IP statistics that are gathered if
1090 * UIP_STATISTICS is set to 1.
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001091 *
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001092 */
1093struct uip_stats {
1094 struct {
adamdunkelsb489e7a2003-10-14 11:12:50 +00001095 uip_stats_t drop; /**< Number of dropped packets at the IP
1096 layer. */
1097 uip_stats_t recv; /**< Number of received packets at the IP
1098 layer. */
1099 uip_stats_t sent; /**< Number of sent packets at the IP
1100 layer. */
1101 uip_stats_t vhlerr; /**< Number of packets dropped due to wrong
1102 IP version or header length. */
1103 uip_stats_t hblenerr; /**< Number of packets dropped due to wrong
1104 IP length, high byte. */
1105 uip_stats_t lblenerr; /**< Number of packets dropped due to wrong
1106 IP length, low byte. */
1107 uip_stats_t fragerr; /**< Number of packets dropped since they
1108 were IP fragments. */
1109 uip_stats_t chkerr; /**< Number of packets dropped due to IP
1110 checksum errors. */
1111 uip_stats_t protoerr; /**< Number of packets dropped since they
1112 were neither ICMP, UDP nor TCP. */
1113 } ip; /**< IP statistics. */
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001114 struct {
adamdunkelsb489e7a2003-10-14 11:12:50 +00001115 uip_stats_t drop; /**< Number of dropped ICMP packets. */
1116 uip_stats_t recv; /**< Number of received ICMP packets. */
1117 uip_stats_t sent; /**< Number of sent ICMP packets. */
1118 uip_stats_t typeerr; /**< Number of ICMP packets with a wrong
1119 type. */
1120 } icmp; /**< ICMP statistics. */
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001121 struct {
adamdunkelsb489e7a2003-10-14 11:12:50 +00001122 uip_stats_t drop; /**< Number of dropped TCP segments. */
1123 uip_stats_t recv; /**< Number of recived TCP segments. */
1124 uip_stats_t sent; /**< Number of sent TCP segments. */
1125 uip_stats_t chkerr; /**< Number of TCP segments with a bad
1126 checksum. */
1127 uip_stats_t ackerr; /**< Number of TCP segments with a bad ACK
1128 number. */
1129 uip_stats_t rst; /**< Number of recevied TCP RST (reset) segments. */
1130 uip_stats_t rexmit; /**< Number of retransmitted TCP segments. */
1131 uip_stats_t syndrop; /**< Number of dropped SYNs due to too few
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001132 connections was avaliable. */
adamdunkelsb489e7a2003-10-14 11:12:50 +00001133 uip_stats_t synrst; /**< Number of SYNs for closed ports,
1134 triggering a RST. */
1135 } tcp; /**< TCP statistics. */
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001136};
1137
adamdunkelsb489e7a2003-10-14 11:12:50 +00001138/**
1139 * The uIP TCP/IP statistics.
1140 *
1141 * This is the variable in which the uIP TCP/IP statistics are gathered.
1142 */
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001143extern struct uip_stats uip_stat;
1144
1145
1146/*-----------------------------------------------------------------------------------*/
1147/* All the stuff below this point is internal to uIP and should not be
1148 * used directly by an application or by a device driver.
1149 */
1150/*-----------------------------------------------------------------------------------*/
1151/* u8_t uip_flags:
1152 *
1153 * When the application is called, uip_flags will contain the flags
1154 * that are defined in this file. Please read below for more
1155 * infomation.
1156 */
adamdunkels18076c62004-06-06 06:16:03 +00001157extern u8_t uip_flags;
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001158
1159/* The following flags may be set in the global variable uip_flags
1160 before calling the application callback. The UIP_ACKDATA and
1161 UIP_NEWDATA flags may both be set at the same time, whereas the
1162 others are mutualy exclusive. Note that these flags should *NOT* be
1163 accessed directly, but through the uIP functions/macros. */
1164
1165#define UIP_ACKDATA 1 /* Signifies that the outstanding data was
1166 acked and the application should send
1167 out new data instead of retransmitting
1168 the last data. */
1169#define UIP_NEWDATA 2 /* Flags the fact that the peer has sent
1170 us new data. */
1171#define UIP_REXMIT 4 /* Tells the application to retransmit the
1172 data that was last sent. */
1173#define UIP_POLL 8 /* Used for polling the application, to
1174 check if the application has data that
1175 it wants to send. */
1176#define UIP_CLOSE 16 /* The remote host has closed the
1177 connection, thus the connection has
1178 gone away. Or the application signals
1179 that it wants to close the
1180 connection. */
1181#define UIP_ABORT 32 /* The remote host has aborted the
1182 connection, thus the connection has
1183 gone away. Or the application signals
1184 that it wants to abort the
1185 connection. */
1186#define UIP_CONNECTED 64 /* We have got a connection from a remote
1187 host and have set up a new connection
1188 for it, or an active connection has
1189 been successfully established. */
1190
1191#define UIP_TIMEDOUT 128 /* The connection has been aborted due to
1192 too many retransmissions. */
1193
1194
1195/* uip_process(flag):
1196 *
1197 * The actual uIP function which does all the work.
1198 */
1199void uip_process(u8_t flag);
1200
1201/* The following flags are passed as an argument to the uip_process()
1202 function. They are used to distinguish between the two cases where
1203 uip_process() is called. It can be called either because we have
1204 incoming data that should be processed, or because the periodic
1205 timer has fired. */
1206
1207#define UIP_DATA 1 /* Tells uIP that there is incoming data in
1208 the uip_buf buffer. The length of the
1209 data is stored in the global variable
1210 uip_len. */
1211#define UIP_TIMER 2 /* Tells uIP that the periodic timer has
1212 fired. */
1213#if UIP_UDP
1214#define UIP_UDP_TIMER 3
1215#endif /* UIP_UDP */
1216
1217/* The TCP states used in the uip_conn->tcpstateflags. */
1218#define CLOSED 0
1219#define SYN_RCVD 1
1220#define SYN_SENT 2
1221#define ESTABLISHED 3
1222#define FIN_WAIT_1 4
1223#define FIN_WAIT_2 5
1224#define CLOSING 6
1225#define TIME_WAIT 7
1226#define LAST_ACK 8
1227#define TS_MASK 15
1228
1229#define UIP_STOPPED 16
1230
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001231#define UIP_TCPIP_HLEN 40
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001232
1233/* The TCP and IP headers. */
1234typedef struct {
1235 /* IP header. */
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001236 u8_t vhl,
1237 tos,
1238 len[2],
1239 ipid[2],
1240 ipoffset[2],
1241 ttl,
1242 proto;
1243 u16_t ipchksum;
1244 u16_t srcipaddr[2],
1245 destipaddr[2];
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001246
1247 /* TCP header. */
1248 u16_t srcport,
1249 destport;
1250 u8_t seqno[4],
1251 ackno[4],
1252 tcpoffset,
1253 flags,
1254 wnd[2];
1255 u16_t tcpchksum;
1256 u8_t urgp[2];
1257 u8_t optdata[4];
1258} uip_tcpip_hdr;
1259
1260/* The ICMP and IP headers. */
1261typedef struct {
1262 /* IP header. */
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001263 u8_t vhl,
1264 tos,
1265 len[2],
1266 ipid[2],
1267 ipoffset[2],
1268 ttl,
1269 proto;
1270 u16_t ipchksum;
1271 u16_t srcipaddr[2],
1272 destipaddr[2];
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001273 /* ICMP (echo) header. */
1274 u8_t type, icode;
1275 u16_t icmpchksum;
1276 u16_t id, seqno;
1277} uip_icmpip_hdr;
1278
1279
1280/* The UDP and IP headers. */
1281typedef struct {
1282 /* IP header. */
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001283 u8_t vhl,
1284 tos,
1285 len[2],
1286 ipid[2],
1287 ipoffset[2],
1288 ttl,
1289 proto;
1290 u16_t ipchksum;
1291 u16_t srcipaddr[2],
1292 destipaddr[2];
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001293
1294 /* UDP header. */
1295 u16_t srcport,
1296 destport;
1297 u16_t udplen;
1298 u16_t udpchksum;
1299} uip_udpip_hdr;
1300
1301#define UIP_PROTO_ICMP 1
1302#define UIP_PROTO_TCP 6
1303#define UIP_PROTO_UDP 17
1304
adamdunkels18076c62004-06-06 06:16:03 +00001305
1306
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001307#if UIP_FIXEDADDR
adamdunkels18076c62004-06-06 06:16:03 +00001308extern const u16_t uip_hostaddr[2], uip_netmask[2], uip_draddr[2];
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001309#else /* UIP_FIXEDADDR */
adamdunkels18076c62004-06-06 06:16:03 +00001310extern u16_t uip_hostaddr[2], uip_netmask[2], uip_draddr[2];
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001311#endif /* UIP_FIXEDADDR */
1312
adamdunkels18076c62004-06-06 06:16:03 +00001313
1314
adamdunkelsb380a3e2004-09-17 20:59:23 +00001315/**
1316 * Representation of a 48-bit Ethernet address.
1317 */
1318struct uip_eth_addr {
1319 u8_t addr[6];
1320};
1321
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001322#endif /* __UIP_H__ */
1323
1324
adamdunkels0170b082003-10-01 11:25:37 +00001325/** @} */
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001326