blob: 1d5a70a268162ae7291be7413b1dab8ee3515039 [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 *
oliverschmidtd2fed3b2005-01-26 23:36:23 +000034 * $Id: uip.h,v 1.2 2005/01/26 23:36:23 oliverschmidt Exp $
adamdunkels2f5291c2003-04-09 12:55:06 +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
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 */
adamdunkels2f5291c2003-04-09 12:55:06 +0000335extern volatile u16_t uip_len, uip_slen;
adamdunkels2f5291c2003-04-09 12:55:06 +0000336
337#if UIP_URGDATA > 0
338extern volatile u8_t uip_urglen, uip_surglen;
339#endif /* UIP_URGDATA > 0 */
340
341extern volatile u8_t uip_acc32[4];
342
343/* struct uip_conn:
344 *
345 * The uip_conn structure is used for identifying a connection. All
346 * but one field in the structure are to be considered read-only by an
347 * application. The only exception is the appstate field whos purpose
348 * is to let the application store application-specific state (e.g.,
349 * file pointers) for the connection. The size of this field is
350 * configured in the "uipopt.h" header file.
351 */
352struct uip_conn {
353#if UIP_IPV6
354 u16_t ripaddr[8]; /* The IP address of the remote peer. */
355#else /* UIP_IPV6 */
356 u16_t ripaddr[2]; /* The IP address of the remote peer. */
357#endif /* UIP_IPV6 */
358
359 u16_t lport, rport; /* The local and the remote port. */
360
361 u8_t rcv_nxt[4]; /* The sequence number that we expect to receive
362 next. */
363 u8_t snd_nxt[4]; /* The sequence number that was last sent by
364 us. */
365#if UIP_TCP_MSS > 255
366 u16_t len;
367 u16_t mss; /* Maximum segment size for the connection. */
368#else
369 u8_t len;
370 u8_t mss;
371#endif /* UIP_TCP_MSS */
372 u8_t sa, sv, rto;
373 u8_t tcpstateflags; /* TCP state and flags. */
374 u8_t timer; /* The retransmission timer. */
375 u8_t nrtx; /* Counts the number of retransmissions for a
376 particular segment. */
377
378 u8_t appstate[UIP_APPSTATE_SIZE];
379};
380
381/* struct uip_conn *uip_conn:
382 *
383 * When the application is called, uip_conn will point to the current
384 * conntection, the one that should be processed by the
385 * application. The uip_conns[] array is a list containing all
386 * connections.
387 */
388extern struct uip_conn *uip_conn;
389extern struct uip_conn uip_conns[UIP_CONNS];
390
391#if UIP_UDP
392/* struct uip_udp_conn:
393 *
394 * The uip_udp_conn structure is used for identifying UDP
395 * "connections".
396 */
397struct uip_udp_conn {
398#if UIP_IPV6
399 u16_t ripaddr[8]; /* The IP address of the remote peer. */
400#else /* UIP_IPV6 */
401 u16_t ripaddr[2]; /* The IP address of the remote peer. */
402#endif /* UIP_IPV6 */
403 u16_t lport, rport;
404};
405
406extern struct uip_udp_conn *uip_udp_conn;
407extern struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];
408#endif /* UIP_UDP */
409
410/* struct uip_stats:
411 *
412 * Contains statistics about the TCP/IP stack.
413 */
414struct uip_stats {
415 struct {
416 uip_stats_t drop;
417 uip_stats_t recv;
418 uip_stats_t sent;
419 uip_stats_t vhlerr; /* Number of packets dropped due to wrong IP version
420 or header length. */
421 uip_stats_t hblenerr; /* Number of packets dropped due to wrong IP length,
422 high byte. */
423 uip_stats_t lblenerr; /* Number of packets dropped due to wrong IP length,
424 low byte. */
425 uip_stats_t fragerr; /* Number of packets dropped since they were IP
426 fragments. */
427 uip_stats_t chkerr; /* Number of packets dropped due to IP checksum errors. */
428 uip_stats_t protoerr; /* Number of packets dropped since they were neither
429 ICMP nor TCP. */
430 } ip;
431 struct {
432 uip_stats_t drop;
433 uip_stats_t recv;
434 uip_stats_t sent;
435 uip_stats_t typeerr;
436 } icmp;
437 struct {
438 uip_stats_t drop;
439 uip_stats_t recv;
440 uip_stats_t sent;
441 uip_stats_t chkerr;
442 uip_stats_t ackerr;
443 uip_stats_t rst;
444 uip_stats_t rexmit;
445 uip_stats_t syndrop; /* Number of dropped SYNs due to too few
446 connections was avaliable. */
447 uip_stats_t synrst; /* Number of SYNs for closed ports, triggering a
448 RST. */
449 } tcp;
450};
451
452extern struct uip_stats uip_stat;
453
454
455/*-----------------------------------------------------------------------------------*/
456/* All the stuff below this point is internal to uIP and should not be
457 * used directly by an application or by a device driver.
458 */
459/*-----------------------------------------------------------------------------------*/
460/* u8_t uip_flags:
461 *
462 * When the application is called, uip_flags will contain the flags
463 * that are defined in this file. Please read below for more
464 * infomation.
465 */
466extern volatile u8_t uip_flags;
467
468/* The following flags may be set in the global variable uip_flags
469 before calling the application callback. The UIP_ACKDATA and
470 UIP_NEWDATA flags may both be set at the same time, whereas the
471 others are mutualy exclusive. Note that these flags should *NOT* be
472 accessed directly, but through the uIP functions/macros. */
473
474#define UIP_ACKDATA 1 /* Signifies that the outstanding data was
475 acked and the application should send
476 out new data instead of retransmitting
477 the last data. */
478#define UIP_NEWDATA 2 /* Flags the fact that the peer has sent
479 us new data. */
480#define UIP_REXMIT 4 /* Tells the application to retransmit the
481 data that was last sent. */
482#define UIP_POLL 8 /* Used for polling the application, to
483 check if the application has data that
484 it wants to send. */
485#define UIP_CLOSE 16 /* The remote host has closed the
486 connection, thus the connection has
487 gone away. Or the application signals
488 that it wants to close the
489 connection. */
490#define UIP_ABORT 32 /* The remote host has aborted the
491 connection, thus the connection has
492 gone away. Or the application signals
493 that it wants to abort the
494 connection. */
495#define UIP_CONNECTED 64 /* We have got a connection from a remote
496 host and have set up a new connection
497 for it, or an active connection has
498 been successfully established. */
499
500#define UIP_TIMEDOUT 128 /* The connection has been aborted due to
501 too many retransmissions. */
502
503
504/* uip_process(flag):
505 *
506 * The actual uIP function which does all the work.
507 */
508void uip_process(u8_t flag);
509
510/* The following flags are passed as an argument to the uip_process()
511 function. They are used to distinguish between the two cases where
512 uip_process() is called. It can be called either because we have
513 incoming data that should be processed, or because the periodic
514 timer has fired. */
515
516#define UIP_DATA 1 /* Tells uIP that there is incoming data in
517 the uip_buf buffer. The length of the
518 data is stored in the global variable
519 uip_len. */
520#define UIP_TIMER 2 /* Tells uIP that the periodic timer has
521 fired. */
522#if UIP_UDP
523#define UIP_UDP_TIMER 3
524#endif /* UIP_UDP */
525
526/* The TCP states used in the uip_conn->tcpstateflags. */
527#define CLOSED 0
528#define SYN_RCVD 1
529#define SYN_SENT 2
530#define ESTABLISHED 3
531#define FIN_WAIT_1 4
532#define FIN_WAIT_2 5
533#define CLOSING 6
534#define TIME_WAIT 7
535#define LAST_ACK 8
536#define TS_MASK 15
537
538#define UIP_STOPPED 16
539
540#if UIP_IPV6
541#define UIP_TCPIP_HLEN 60
542#else /* UIP_IPV6 */
543#define UIP_TCPIP_HLEN 40
544#endif /* UIP_IPV6 */
545
546/* The TCP and IP headers. */
547typedef struct {
548 /* IP header. */
549#if UIP_IPV6
550 u8_t vtc,
551 tcfl;
552 u16_t fl;
553 u8_t len[2];
554 u8_t nxthdr, hoplim;
555 u16_t srcipaddr[8],
556 destipaddr[8];
557#else /* UIP_IPV6 */
558 u8_t vhl,
559 tos,
560 len[2],
561 ipid[2],
562 ipoffset[2],
563 ttl,
564 proto;
565 u16_t ipchksum;
566 u16_t srcipaddr[2],
567 destipaddr[2];
568#endif /* UIP_IPV6 */
569
570 /* TCP header. */
571 u16_t srcport,
572 destport;
573 u8_t seqno[4],
574 ackno[4],
575 tcpoffset,
576 flags,
577 wnd[2];
578 u16_t tcpchksum;
579 u8_t urgp[2];
580 u8_t optdata[4];
581} uip_tcpip_hdr;
582
583/* The ICMP and IP headers. */
584typedef struct {
585 /* IP header. */
586#if UIP_IPV6
587 u16_t vtcfl;
588 u16_t fl;
589 u8_t len[2];
590 u8_t nxthdr, hoplim;
591 u16_t srcipaddr[8],
592 destipaddr[8];
593#else /* UIP_IPV6 */
594 u8_t vhl,
595 tos,
596 len[2],
597 ipid[2],
598 ipoffset[2],
599 ttl,
600 proto;
601 u16_t ipchksum;
602 u16_t srcipaddr[2],
603 destipaddr[2];
604#endif /* UIP_IPV6 */
605 /* ICMP (echo) header. */
606 u8_t type, icode;
607 u16_t icmpchksum;
608 u16_t id, seqno;
609} uip_icmpip_hdr;
610
611
612/* The UDP and IP headers. */
613typedef struct {
614 /* IP header. */
615#if UIP_IPV6
616 u8_t vtc,
617 tcfl;
618 u16_t fl;
619 u8_t len[2];
620 u8_t nxthdr, hoplim;
621 u16_t srcipaddr[8],
622 destipaddr[8];
623#else /* UIP_IPV6 */
624 u8_t vhl,
625 tos,
626 len[2],
627 ipid[2],
628 ipoffset[2],
629 ttl,
630 proto;
631 u16_t ipchksum;
632 u16_t srcipaddr[2],
633 destipaddr[2];
634#endif /* UIP_IPV6 */
635
636 /* UDP header. */
637 u16_t srcport,
638 destport;
639 u16_t udplen;
640 u16_t udpchksum;
641} uip_udpip_hdr;
642
643#define UIP_PROTO_ICMP 1
644#define UIP_PROTO_TCP 6
645#define UIP_PROTO_UDP 17
646
647#if UIP_FIXEDADDR
648#if UIP_IPV6
649extern const u16_t uip_hostaddr[8];
650#else /* UIP_IPV6 */
651extern const u16_t uip_hostaddr[2];
652#endif /* UIP_IPV6 */
653#else /* UIP_FIXEDADDR */
654#if UIP_IPV6
655extern u16_t uip_hostaddr[8];
656#else /* UIP_IPV6 */
657extern u16_t uip_hostaddr[2];
658#endif /* UIP_IPV6 */
659#endif /* UIP_FIXEDADDR */
660
661#endif /* __UIP_H__ */
662
663
664
665
666
667
668