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