blob: b86816667e133be40b71151b6e7c45a273a9456c [file] [log] [blame]
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001/*
2 * Copyright (c) 2001-2002, Adam Dunkels.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Adam Dunkels.
16 * 4. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior
18 * written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
21 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * This file is part of the uIP TCP/IP stack.
33 *
adamdunkelscd8c3a22003-08-13 22:52:48 +000034 * $Id: uip.h,v 1.4 2003/08/13 22:52:48 adamdunkels Exp $
adamdunkelsca9ddcb2003-03-19 14:13:31 +000035 *
36 */
37
38#ifndef __UIP_H__
39#define __UIP_H__
40
41#include "uipopt.h"
42
43#ifndef UIP_IPV6
44#define UIP_IPV6 0
45#endif
46
47/*-----------------------------------------------------------------------------------*/
48/* First, the functions that should be called from the
49 * system. Initialization, the periodic timer and incoming packets are
50 * handled by the following three functions.
51 */
52
53/* uip_init(void):
54 *
55 * Must be called at boot up to configure the uIP data structures.
56 */
57void uip_init(void);
58
59/* uip_periodic(conn):
60 *
61 * Should be called when the periodic timer has fired. Should be
62 * called once per connection (0 - UIP_CONNS).
63 */
64#define uip_periodic(conn) do { uip_conn = &uip_conns[conn]; \
65 uip_process(UIP_TIMER); } while (0)
66
67/* uip_input(void):
68 *
69 * Is called when the network device driver has received new data.
70 */
71#define uip_input() uip_process(UIP_DATA)
72
73/* uip_sethostaddr(addr):
74 *
75 * Is used to set the IP address.
76 */
77#define uip_sethostaddr(addr) do { uip_hostaddr[0] = addr[0]; \
78 uip_hostaddr[1] = addr[1]; } while(0)
79
adamdunkels66c6af62003-04-16 18:28:16 +000080/* uip_gethostaddr(&addr):
81 *
82 * Obtains the IP address.
83 */
84#define uip_gethostaddr(addr) do { addr[0] = uip_hostaddr[0]; \
85 addr[1] = uip_hostaddr[1]; } while(0)
86
adamdunkelsca9ddcb2003-03-19 14:13:31 +000087#if UIP_UDP
88/* uip_udp_periodic(conn):
89 */
90#define uip_udp_periodic(conn) do { uip_udp_conn = &uip_udp_conns[conn]; \
91 uip_process(UIP_UDP_TIMER); } while (0)
92#endif /* UIP_UDP */
93/*-----------------------------------------------------------------------------------*/
94/* Functions that are used by the uIP application program. Opening and
95 * closing connections, sending and receiving data, etc. is all
96 * handled by the functions below.
97*/
98
99/* uip_listen(port):
100 *
101 * Starts listening to the specified port.
102 */
103void uip_listen(u16_t port);
104
adamdunkelscd8c3a22003-08-13 22:52:48 +0000105/* uip_unlisten(port):
106 *
107 * Stops listening to the specified port.
108 */
109void uip_unlisten(u16_t port);
110
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000111/* uip_connect(ripaddr, port):
112 *
113 * Returns a connection identifier that connects to a port on the
114 * specified host (given in ripaddr). If no connections are avaliable,
115 * the function returns NULL. This function is avaliable only if
116 * support for active open has been configured (#define
117 * UIP_ACTIVE_OPEN 1 in uipopt.h)
118 */
119struct uip_conn *uip_connect(u16_t *ripaddr, u16_t port);
120
121#if UIP_UDP
122/* uip_udp_new(ripaddr, rport):
123 *
124 * Sets up a new UDP "connection" with the specified parameters.
125 */
126struct uip_udp_conn *uip_udp_new(u16_t *ripaddr, u16_t rport);
127
128/* uip_udp_remove(conn):
129 *
130 * Removes the UDP "connection".
131 */
132#define uip_udp_remove(conn) (conn)->lport = 0
133
134/* uip_udp_send(len):
135 *
136 * Sends a UDP datagram of length len. The data must be present in the
137 * uip_buf buffer (pointed to by uip_appdata).
138 */
139#define uip_udp_send(len) uip_slen = (len)
140#endif /* UIP_UDP */
141
142
143/* uip_outstanding(conn):
144 *
145 * Checks whether a connection has outstanding (i.e., unacknowledged)
146 * data.
147 */
148#define uip_outstanding(conn) ((conn)->len)
149
150/* uip_send(data, len):
151 *
152 * Send data on the current connection. The length of the data must
153 * not exceed the maxium segment size (MSS) for the connection.
154 */
155#define uip_send(data, len) do { uip_appdata = (data); uip_slen = (len);} while(0)
156
157/* uip_datalen():
158 *
159 * The length of the data that is currently avaliable (if avaliable)
160 * in the uip_appdata buffer. The test function uip_data() is
161 * used to check if data is avaliable.
162 */
163#define uip_datalen() uip_len
164
165#define uip_urgdatalen() uip_urglen
166
167/* uip_close():
168 *
169 * Close the current connection.
170 */
171#define uip_close() (uip_flags = UIP_CLOSE)
172
173/* uip_abort():
174 *
175 * Abort the current connection.
176 */
177#define uip_abort() (uip_flags = UIP_ABORT)
178
179/* uip_stop():
180 *
181 * Close our receiver's window so that we stop receiving data for the
182 * current connection.
183 */
184#define uip_stop() (uip_conn->tcpstateflags |= UIP_STOPPED)
185
186/* uip_stopped():
187 *
188 * Find out if the current connection has been previously stopped.
189 */
190#define uip_stopped(conn) ((conn)->tcpstateflags & UIP_STOPPED)
191
192/* uip_restart():
193 *
194 * Open the window again so that we start receiving data for the
195 * current connection.
196 */
197#define uip_restart() do { uip_flags |= UIP_NEWDATA; \
198 uip_conn->tcpstateflags &= ~UIP_STOPPED; \
199 } while(0)
200
201
202/* uIP tests that can be made to determine in what state the current
203 connection is, and what the application function should do. */
204
205/* uip_newdata():
206 *
207 * Will reduce to non-zero if there is new data for the application
208 * present at the uip_appdata pointer. The size of the data is
209 * avaliable through the uip_len variable.
210 */
211#define uip_newdata() (uip_flags & UIP_NEWDATA)
212
213/* uip_acked():
214 *
215 * Will reduce to non-zero if the previously sent data has been
216 * acknowledged by the remote host. This means that the application
217 * can send new data. uip_reset_acked() can be used to reset the acked
218 * flag.
219 */
220#define uip_acked() (uip_flags & UIP_ACKDATA)
221#define uip_reset_acked() (uip_flags &= ~UIP_ACKDATA)
222
223/* uip_connected():
224 *
225 * Reduces to non-zero if the current connection has been connected to
226 * a remote host. This will happen both if the connection has been
227 * actively opened (with uip_connect()) or passively opened (with
228 * uip_listen()).
229 */
230#define uip_connected() (uip_flags & UIP_CONNECTED)
231
232/* uip_closed():
233 *
234 * Is non-zero if the connection has been closed by the remote
235 * host. The application may do the necessary clean-ups.
236 */
237#define uip_closed() (uip_flags & UIP_CLOSE)
238
239/* uip_aborted():
240 *
241 * Non-zero if the current connection has been aborted (reset) by the
242 * remote host.
243 */
244#define uip_aborted() (uip_flags & UIP_ABORT)
245
246/* uip_timedout():
247 *
248 * Non-zero if the current connection has been aborted due to too many
249 * retransmissions.
250 */
251#define uip_timedout() (uip_flags & UIP_TIMEDOUT)
252
253/* uip_rexmit():
254 *
255 * Reduces to non-zero if the previously sent data has been lost in
256 * the network, and the application should retransmit it. The
257 * application should set the uip_appdata buffer and the uip_len
258 * variable just as it did the last time this data was to be
259 * transmitted.
260 */
261#define uip_rexmit() (uip_flags & UIP_REXMIT)
262
263/* uip_poll():
264 *
265 * Is non-zero if the reason the application is invoked is that the
266 * current connection has been idle for a while and should be
267 * polled.
268 */
269#define uip_poll() (uip_flags & UIP_POLL)
270
271/* uip_mss():
272 *
273 * Gives the current maxium segment size (MSS) of the current
274 * connection.
275 */
276#define uip_mss() (uip_conn->mss)
277
278
279/* uIP convenience and converting functions. */
280
281/* uip_ipaddr(&ipaddr, addr0,addr1,addr2,addr3):
282 *
283 * Packs an IP address into a two element 16-bit array. Such arrays
284 * are used to represent IP addresses in uIP.
285 */
286#define uip_ipaddr(addr, addr0,addr1,addr2,addr3) do { \
adamdunkels47ec7fa2003-03-28 12:11:17 +0000287 (addr)[0] = HTONS(((addr0) << 8) | (addr1)); \
288 (addr)[1] = HTONS(((addr2) << 8) | (addr3)); \
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000289 } while(0)
290
adamdunkels47ec7fa2003-03-28 12:11:17 +0000291/* HTONS():
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000292 *
293 * Macros for converting 16-bit quantities between host and network
294 * byte order.
295 */
adamdunkels47ec7fa2003-03-28 12:11:17 +0000296#ifndef HTONS
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000297# if BYTE_ORDER == BIG_ENDIAN
adamdunkels47ec7fa2003-03-28 12:11:17 +0000298# define HTONS(n) (n)
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000299# else /* BYTE_ORDER == BIG_ENDIAN */
adamdunkels47ec7fa2003-03-28 12:11:17 +0000300# define HTONS(n) ((((u16_t)((n) & 0xff)) << 8) | (((n) & 0xff00) >> 8))
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000301# endif /* BYTE_ORDER == BIG_ENDIAN */
adamdunkels47ec7fa2003-03-28 12:11:17 +0000302#endif /* HTONS */
303
304#ifndef htons
305u16_t htons(u16_t val);
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000306#endif /* htons */
307
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000308/*-----------------------------------------------------------------------------------*/
309/* The following global variables are used for passing parameters
310 * between uIP, the network device driver and the application. */
311/*-----------------------------------------------------------------------------------*/
312
313/* u8_t uip_buf[UIP_BUFSIZE]:
314 *
315 * The uip_buf array is used to hold incoming and outgoing
316 * packets. The device driver fills this with incoming packets.
317 */
318extern u8_t uip_buf[UIP_BUFSIZE];
319
320/* u8_t *uip_appdata:
321 *
322 * This pointer points to the application data when the application is
323 * called. If the application wishes to send data, this is where the
324 * application should write it. The application can also point this to
325 * another location.
326 */
327extern volatile u8_t *uip_appdata;
328
329#if UIP_URGDATA > 0
330/* u8_t *uip_urgdata:
331 *
332 * This pointer points to any urgent data that has been received. Only
333 * present if compiled with support for urgent data (UIP_URGDATA).
334 */
335extern volatile u8_t *uip_urgdata;
336#endif /* UIP_URGDATA > 0 */
337
338
339/* u[8|16]_t uip_len:
340 *
341 * When the application is called, uip_len contains the length of any
342 * new data that has been received from the remote host. The
343 * application should set this variable to the size of any data that
344 * the application wishes to send. When the network device driver
345 * output function is called, uip_len should contain the length of the
346 * outgoing packet.
347 */
348#if UIP_BUFSIZE > 255
349extern volatile u16_t uip_len, uip_slen;
350#else
351extern volatile u8_t uip_len, uip_slen;
352#endif
353
354#if UIP_URGDATA > 0
355extern volatile u8_t uip_urglen, uip_surglen;
356#endif /* UIP_URGDATA > 0 */
357
358extern volatile u8_t uip_acc32[4];
359
360/* struct uip_conn:
361 *
362 * The uip_conn structure is used for identifying a connection. All
363 * but one field in the structure are to be considered read-only by an
364 * application. The only exception is the appstate field whos purpose
365 * is to let the application store application-specific state (e.g.,
366 * file pointers) for the connection. The size of this field is
367 * configured in the "uipopt.h" header file.
368 */
369struct uip_conn {
370#if UIP_IPV6
371 u16_t ripaddr[8]; /* The IP address of the remote peer. */
372#else /* UIP_IPV6 */
373 u16_t ripaddr[2]; /* The IP address of the remote peer. */
374#endif /* UIP_IPV6 */
375
376 u16_t lport, rport; /* The local and the remote port. */
377
378 u8_t rcv_nxt[4]; /* The sequence number that we expect to receive
379 next. */
380 u8_t snd_nxt[4]; /* The sequence number that was last sent by
381 us. */
382#if UIP_TCP_MSS > 255
383 u16_t len;
384 u16_t mss; /* Maximum segment size for the connection. */
385#else
386 u8_t len;
387 u8_t mss;
388#endif /* UIP_TCP_MSS */
389 u8_t sa, sv, rto;
390 u8_t tcpstateflags; /* TCP state and flags. */
391 u8_t timer; /* The retransmission timer. */
392 u8_t nrtx; /* Counts the number of retransmissions for a
393 particular segment. */
394
395 u8_t appstate[UIP_APPSTATE_SIZE];
396};
397
398/* struct uip_conn *uip_conn:
399 *
400 * When the application is called, uip_conn will point to the current
401 * conntection, the one that should be processed by the
402 * application. The uip_conns[] array is a list containing all
403 * connections.
404 */
405extern struct uip_conn *uip_conn;
406extern struct uip_conn uip_conns[UIP_CONNS];
407
408#if UIP_UDP
409/* struct uip_udp_conn:
410 *
411 * The uip_udp_conn structure is used for identifying UDP
412 * "connections".
413 */
414struct uip_udp_conn {
415#if UIP_IPV6
416 u16_t ripaddr[8]; /* The IP address of the remote peer. */
417#else /* UIP_IPV6 */
418 u16_t ripaddr[2]; /* The IP address of the remote peer. */
419#endif /* UIP_IPV6 */
420 u16_t lport, rport;
421};
422
423extern struct uip_udp_conn *uip_udp_conn;
424extern struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];
425#endif /* UIP_UDP */
426
427/* struct uip_stats:
428 *
429 * Contains statistics about the TCP/IP stack.
430 */
431struct uip_stats {
432 struct {
433 uip_stats_t drop;
434 uip_stats_t recv;
435 uip_stats_t sent;
436 uip_stats_t vhlerr; /* Number of packets dropped due to wrong IP version
437 or header length. */
438 uip_stats_t hblenerr; /* Number of packets dropped due to wrong IP length,
439 high byte. */
440 uip_stats_t lblenerr; /* Number of packets dropped due to wrong IP length,
441 low byte. */
442 uip_stats_t fragerr; /* Number of packets dropped since they were IP
443 fragments. */
444 uip_stats_t chkerr; /* Number of packets dropped due to IP checksum errors. */
445 uip_stats_t protoerr; /* Number of packets dropped since they were neither
446 ICMP nor TCP. */
447 } ip;
448 struct {
449 uip_stats_t drop;
450 uip_stats_t recv;
451 uip_stats_t sent;
452 uip_stats_t typeerr;
453 } icmp;
454 struct {
455 uip_stats_t drop;
456 uip_stats_t recv;
457 uip_stats_t sent;
458 uip_stats_t chkerr;
459 uip_stats_t ackerr;
460 uip_stats_t rst;
461 uip_stats_t rexmit;
462 uip_stats_t syndrop; /* Number of dropped SYNs due to too few
463 connections was avaliable. */
464 uip_stats_t synrst; /* Number of SYNs for closed ports, triggering a
465 RST. */
466 } tcp;
467};
468
469extern struct uip_stats uip_stat;
470
471
472/*-----------------------------------------------------------------------------------*/
473/* All the stuff below this point is internal to uIP and should not be
474 * used directly by an application or by a device driver.
475 */
476/*-----------------------------------------------------------------------------------*/
477/* u8_t uip_flags:
478 *
479 * When the application is called, uip_flags will contain the flags
480 * that are defined in this file. Please read below for more
481 * infomation.
482 */
483extern volatile u8_t uip_flags;
484
485/* The following flags may be set in the global variable uip_flags
486 before calling the application callback. The UIP_ACKDATA and
487 UIP_NEWDATA flags may both be set at the same time, whereas the
488 others are mutualy exclusive. Note that these flags should *NOT* be
489 accessed directly, but through the uIP functions/macros. */
490
491#define UIP_ACKDATA 1 /* Signifies that the outstanding data was
492 acked and the application should send
493 out new data instead of retransmitting
494 the last data. */
495#define UIP_NEWDATA 2 /* Flags the fact that the peer has sent
496 us new data. */
497#define UIP_REXMIT 4 /* Tells the application to retransmit the
498 data that was last sent. */
499#define UIP_POLL 8 /* Used for polling the application, to
500 check if the application has data that
501 it wants to send. */
502#define UIP_CLOSE 16 /* The remote host has closed the
503 connection, thus the connection has
504 gone away. Or the application signals
505 that it wants to close the
506 connection. */
507#define UIP_ABORT 32 /* The remote host has aborted the
508 connection, thus the connection has
509 gone away. Or the application signals
510 that it wants to abort the
511 connection. */
512#define UIP_CONNECTED 64 /* We have got a connection from a remote
513 host and have set up a new connection
514 for it, or an active connection has
515 been successfully established. */
516
517#define UIP_TIMEDOUT 128 /* The connection has been aborted due to
518 too many retransmissions. */
519
520
521/* uip_process(flag):
522 *
523 * The actual uIP function which does all the work.
524 */
525void uip_process(u8_t flag);
526
527/* The following flags are passed as an argument to the uip_process()
528 function. They are used to distinguish between the two cases where
529 uip_process() is called. It can be called either because we have
530 incoming data that should be processed, or because the periodic
531 timer has fired. */
532
533#define UIP_DATA 1 /* Tells uIP that there is incoming data in
534 the uip_buf buffer. The length of the
535 data is stored in the global variable
536 uip_len. */
537#define UIP_TIMER 2 /* Tells uIP that the periodic timer has
538 fired. */
539#if UIP_UDP
540#define UIP_UDP_TIMER 3
541#endif /* UIP_UDP */
542
543/* The TCP states used in the uip_conn->tcpstateflags. */
544#define CLOSED 0
545#define SYN_RCVD 1
546#define SYN_SENT 2
547#define ESTABLISHED 3
548#define FIN_WAIT_1 4
549#define FIN_WAIT_2 5
550#define CLOSING 6
551#define TIME_WAIT 7
552#define LAST_ACK 8
553#define TS_MASK 15
554
555#define UIP_STOPPED 16
556
557#if UIP_IPV6
558#define UIP_TCPIP_HLEN 60
559#else /* UIP_IPV6 */
560#define UIP_TCPIP_HLEN 40
561#endif /* UIP_IPV6 */
562
563/* The TCP and IP headers. */
564typedef struct {
565 /* IP header. */
566#if UIP_IPV6
567 u8_t vtc,
568 tcfl;
569 u16_t fl;
570 u8_t len[2];
571 u8_t nxthdr, hoplim;
572 u16_t srcipaddr[8],
573 destipaddr[8];
574#else /* UIP_IPV6 */
575 u8_t vhl,
576 tos,
577 len[2],
578 ipid[2],
579 ipoffset[2],
580 ttl,
581 proto;
582 u16_t ipchksum;
583 u16_t srcipaddr[2],
584 destipaddr[2];
585#endif /* UIP_IPV6 */
586
587 /* TCP header. */
588 u16_t srcport,
589 destport;
590 u8_t seqno[4],
591 ackno[4],
592 tcpoffset,
593 flags,
594 wnd[2];
595 u16_t tcpchksum;
596 u8_t urgp[2];
597 u8_t optdata[4];
598} uip_tcpip_hdr;
599
600/* The ICMP and IP headers. */
601typedef struct {
602 /* IP header. */
603#if UIP_IPV6
604 u16_t vtcfl;
605 u16_t fl;
606 u8_t len[2];
607 u8_t nxthdr, hoplim;
608 u16_t srcipaddr[8],
609 destipaddr[8];
610#else /* UIP_IPV6 */
611 u8_t vhl,
612 tos,
613 len[2],
614 ipid[2],
615 ipoffset[2],
616 ttl,
617 proto;
618 u16_t ipchksum;
619 u16_t srcipaddr[2],
620 destipaddr[2];
621#endif /* UIP_IPV6 */
622 /* ICMP (echo) header. */
623 u8_t type, icode;
624 u16_t icmpchksum;
625 u16_t id, seqno;
626} uip_icmpip_hdr;
627
628
629/* The UDP and IP headers. */
630typedef struct {
631 /* IP header. */
632#if UIP_IPV6
633 u8_t vtc,
634 tcfl;
635 u16_t fl;
636 u8_t len[2];
637 u8_t nxthdr, hoplim;
638 u16_t srcipaddr[8],
639 destipaddr[8];
640#else /* UIP_IPV6 */
641 u8_t vhl,
642 tos,
643 len[2],
644 ipid[2],
645 ipoffset[2],
646 ttl,
647 proto;
648 u16_t ipchksum;
649 u16_t srcipaddr[2],
650 destipaddr[2];
651#endif /* UIP_IPV6 */
652
653 /* UDP header. */
654 u16_t srcport,
655 destport;
656 u16_t udplen;
657 u16_t udpchksum;
658} uip_udpip_hdr;
659
660#define UIP_PROTO_ICMP 1
661#define UIP_PROTO_TCP 6
662#define UIP_PROTO_UDP 17
663
664#if UIP_FIXEDADDR
665#if UIP_IPV6
666extern const u16_t uip_hostaddr[8];
667#else /* UIP_IPV6 */
668extern const u16_t uip_hostaddr[2];
669#endif /* UIP_IPV6 */
670#else /* UIP_FIXEDADDR */
671#if UIP_IPV6
672extern u16_t uip_hostaddr[8];
673#else /* UIP_IPV6 */
674extern u16_t uip_hostaddr[2];
675#endif /* UIP_IPV6 */
676#endif /* UIP_FIXEDADDR */
677
678#endif /* __UIP_H__ */
679
680
681
682
683
684
685