uip.h

Go to the documentation of this file.
00001 
00018 /*
00019  * Copyright (c) 2001-2003, Adam Dunkels.
00020  * All rights reserved. 
00021  *
00022  * Redistribution and use in source and binary forms, with or without 
00023  * modification, are permitted provided that the following conditions 
00024  * are met: 
00025  * 1. Redistributions of source code must retain the above copyright 
00026  *    notice, this list of conditions and the following disclaimer. 
00027  * 2. Redistributions in binary form must reproduce the above copyright 
00028  *    notice, this list of conditions and the following disclaimer in the 
00029  *    documentation and/or other materials provided with the distribution. 
00030  * 3. The name of the author may not be used to endorse or promote
00031  *    products derived from this software without specific prior
00032  *    written permission.  
00033  *
00034  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00035  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00036  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00037  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00038  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00039  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00040  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00041  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00042  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00043  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00044  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
00045  *
00046  * This file is part of the uIP TCP/IP stack.
00047  *
00048  * $Id: uip.h,v 1.36.2.7 2003/10/07 13:47:51 adam Exp $
00049  *
00050  */
00051 
00052 #ifndef __UIP_H__
00053 #define __UIP_H__
00054 
00055 #include "uipopt.h"
00056 
00057 /*-----------------------------------------------------------------------------------*/
00058 /* First, the functions that should be called from the
00059  * system. Initialization, the periodic timer and incoming packets are
00060  * handled by the following three functions.
00061  */
00062 
00082 #define uip_sethostaddr(addr) do { uip_hostaddr[0] = addr[0]; \
00083                               uip_hostaddr[1] = addr[1]; } while(0)
00084 
00097 #define uip_gethostaddr(addr) do { addr[0] = uip_hostaddr[0]; \
00098                               addr[1] = uip_hostaddr[1]; } while(0)
00099 
00115 void uip_init(void);
00116 
00177 #define uip_input()        uip_process(UIP_DATA)
00178 
00221 #define uip_periodic(conn) do { uip_conn = &uip_conns[conn]; \
00222                                 uip_process(UIP_TIMER); } while (0)
00223 
00236 #define uip_periodic_conn(conn) do { uip_conn = conn; \
00237                                      uip_process(UIP_TIMER); } while (0)
00238 
00239 #if UIP_UDP
00240 
00271 #define uip_udp_periodic(conn) do { uip_udp_conn = &uip_udp_conns[conn]; \
00272                                 uip_process(UIP_UDP_TIMER); } while (0)
00273 
00288 #define uip_udp_periodic_conn(conn) do { uip_udp_conn = conn; \
00289                                          uip_process(UIP_UDP_TIMER); } while (0)
00290 
00291 
00292 #endif /* UIP_UDP */
00293 
00316 extern u8_t uip_buf[UIP_BUFSIZE+2];
00317 
00320 /*-----------------------------------------------------------------------------------*/
00321 /* Functions that are used by the uIP application program. Opening and
00322  * closing connections, sending and receiving data, etc. is all
00323  * handled by the functions below.
00324 */
00344 void uip_listen(u16_t port);
00345 
00358 void uip_unlisten(u16_t port);
00359 
00393 struct uip_conn *uip_connect(u16_t *ripaddr, u16_t port);
00394 
00395 
00396 
00406 #define uip_outstanding(conn) ((conn)->len)
00407 
00433 #define uip_send(data, len) do { uip_sappdata = (data); uip_slen = (len);} while(0)   
00434 
00444 #define uip_datalen()       uip_len
00445 
00455 #define uip_urgdatalen()    uip_urglen
00456 
00464 #define uip_close()         (uip_flags = UIP_CLOSE)
00465 
00475 #define uip_abort()         (uip_flags = UIP_ABORT)
00476 
00485 #define uip_stop()          (uip_conn->tcpstateflags |= UIP_STOPPED)
00486 
00493 #define uip_stopped(conn)   ((conn)->tcpstateflags & UIP_STOPPED)
00494 
00504 #define uip_restart()         do { uip_flags |= UIP_NEWDATA; \
00505                                    uip_conn->tcpstateflags &= ~UIP_STOPPED; \
00506                               } while(0)
00507 
00508 
00509 /* uIP tests that can be made to determine in what state the current
00510    connection is, and what the application function should do. */
00511 
00521 #define uip_newdata()   (uip_flags & UIP_NEWDATA)
00522 
00532 #define uip_acked()   (uip_flags & UIP_ACKDATA)
00533 
00544 #define uip_connected() (uip_flags & UIP_CONNECTED)
00545 
00554 #define uip_closed()    (uip_flags & UIP_CLOSE)
00555 
00564 #define uip_aborted()    (uip_flags & UIP_ABORT)
00565 
00574 #define uip_timedout()    (uip_flags & UIP_TIMEDOUT)
00575 
00586 #define uip_rexmit()     (uip_flags & UIP_REXMIT)
00587 
00600 #define uip_poll()       (uip_flags & UIP_POLL)
00601 
00608 #define uip_initialmss()             (uip_conn->initialmss)
00609 
00621 #define uip_mss()             (uip_conn->mss)
00622 
00634 struct uip_udp_conn *uip_udp_new(u16_t *ripaddr, u16_t rport);
00635 
00643 #define uip_udp_remove(conn) (conn)->lport = 0
00644 
00656 #define uip_udp_send(len) uip_slen = (len)
00657 
00660 /* uIP convenience and converting functions. */
00661 
00690 #define uip_ipaddr(addr, addr0,addr1,addr2,addr3) do { \
00691                      (addr)[0] = HTONS(((addr0) << 8) | (addr1)); \
00692                      (addr)[1] = HTONS(((addr2) << 8) | (addr3)); \
00693                   } while(0)
00694 
00704 #ifndef HTONS
00705 #   if BYTE_ORDER == BIG_ENDIAN
00706 #      define HTONS(n) (n)
00707 #   else /* BYTE_ORDER == BIG_ENDIAN */
00708 #      define HTONS(n) ((((u16_t)((n) & 0xff)) << 8) | (((n) & 0xff00) >> 8))
00709 #   endif /* BYTE_ORDER == BIG_ENDIAN */
00710 #endif /* HTONS */
00711 
00719 #ifndef htons
00720 u16_t htons(u16_t val);
00721 #endif /* htons */
00722 
00732 extern volatile u8_t *uip_appdata;
00733 extern volatile u8_t *uip_sappdata; 
00734 
00735 #if UIP_URGDATA > 0 
00736 /* u8_t *uip_urgdata:
00737  *
00738  * This pointer points to any urgent data that has been received. Only
00739  * present if compiled with support for urgent data (UIP_URGDATA).
00740  */
00741 extern volatile u8_t *uip_urgdata; 
00742 #endif /* UIP_URGDATA > 0 */
00743 
00744 
00745 /* u[8|16]_t uip_len:
00746  *
00747  * When the application is called, uip_len contains the length of any
00748  * new data that has been received from the remote host. The
00749  * application should set this variable to the size of any data that
00750  * the application wishes to send. When the network device driver
00751  * output function is called, uip_len should contain the length of the
00752  * outgoing packet.
00753  */
00754 extern volatile u16_t uip_len, uip_slen;
00755 
00756 #if UIP_URGDATA > 0 
00757 extern volatile u8_t uip_urglen, uip_surglen;
00758 #endif /* UIP_URGDATA > 0 */
00759 
00760 
00771 struct uip_conn {
00772   u16_t ripaddr[2];   
00774   u16_t lport;        
00775   u16_t rport;        
00778   u8_t rcv_nxt[4];    
00780   u8_t snd_nxt[4];    
00782   u16_t len;          
00783   u16_t mss;          
00785   u16_t initialmss;   
00787   u8_t sa;            
00789   u8_t sv;            
00791   u8_t rto;           
00792   u8_t tcpstateflags; 
00793   u8_t timer;         
00794   u8_t nrtx;          
00798   u8_t appstate[UIP_APPSTATE_SIZE];  
00799 };
00800 
00801 
00802 /* Pointer to the current connection. */
00803 extern struct uip_conn *uip_conn;
00804 /* The array containing all uIP connections. */
00805 extern struct uip_conn uip_conns[UIP_CONNS];
00814 extern volatile u8_t uip_acc32[4];
00815 
00819 #if UIP_UDP
00820 
00823 struct uip_udp_conn {
00824   u16_t ripaddr[2];   
00825   u16_t lport;        
00826   u16_t rport;        
00827 };
00828 
00829 extern struct uip_udp_conn *uip_udp_conn;
00830 extern struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];
00831 #endif /* UIP_UDP */
00832 
00838 struct uip_stats {
00839   struct {
00840     uip_stats_t drop;     
00842     uip_stats_t recv;     
00844     uip_stats_t sent;     
00846     uip_stats_t vhlerr;   
00848     uip_stats_t hblenerr; 
00850     uip_stats_t lblenerr; 
00852     uip_stats_t fragerr;  
00854     uip_stats_t chkerr;   
00856     uip_stats_t protoerr; 
00858   } ip;                   
00859   struct {
00860     uip_stats_t drop;     
00861     uip_stats_t recv;     
00862     uip_stats_t sent;     
00863     uip_stats_t typeerr;  
00865   } icmp;                 
00866   struct {
00867     uip_stats_t drop;     
00868     uip_stats_t recv;     
00869     uip_stats_t sent;     
00870     uip_stats_t chkerr;   
00872     uip_stats_t ackerr;   
00874     uip_stats_t rst;      
00875     uip_stats_t rexmit;   
00876     uip_stats_t syndrop;  
00878     uip_stats_t synrst;   
00880   } tcp;                  
00881 };
00882 
00888 extern struct uip_stats uip_stat;
00889 
00890 
00891 /*-----------------------------------------------------------------------------------*/
00892 /* All the stuff below this point is internal to uIP and should not be
00893  * used directly by an application or by a device driver.
00894  */
00895 /*-----------------------------------------------------------------------------------*/
00896 /* u8_t uip_flags:
00897  *
00898  * When the application is called, uip_flags will contain the flags
00899  * that are defined in this file. Please read below for more
00900  * infomation.
00901  */
00902 extern volatile u8_t uip_flags;
00903 
00904 /* The following flags may be set in the global variable uip_flags
00905    before calling the application callback. The UIP_ACKDATA and
00906    UIP_NEWDATA flags may both be set at the same time, whereas the
00907    others are mutualy exclusive. Note that these flags should *NOT* be
00908    accessed directly, but through the uIP functions/macros. */
00909 
00910 #define UIP_ACKDATA   1     /* Signifies that the outstanding data was
00911                                acked and the application should send
00912                                out new data instead of retransmitting
00913                                the last data. */
00914 #define UIP_NEWDATA   2     /* Flags the fact that the peer has sent
00915                                us new data. */
00916 #define UIP_REXMIT    4     /* Tells the application to retransmit the
00917                                data that was last sent. */
00918 #define UIP_POLL      8     /* Used for polling the application, to
00919                                check if the application has data that
00920                                it wants to send. */
00921 #define UIP_CLOSE     16    /* The remote host has closed the
00922                                connection, thus the connection has
00923                                gone away. Or the application signals
00924                                that it wants to close the
00925                                connection. */
00926 #define UIP_ABORT     32    /* The remote host has aborted the
00927                                connection, thus the connection has
00928                                gone away. Or the application signals
00929                                that it wants to abort the
00930                                connection. */
00931 #define UIP_CONNECTED 64    /* We have got a connection from a remote
00932                                host and have set up a new connection
00933                                for it, or an active connection has
00934                                been successfully established. */
00935 
00936 #define UIP_TIMEDOUT  128   /* The connection has been aborted due to
00937                                too many retransmissions. */
00938 
00939 
00940 /* uip_process(flag):
00941  *
00942  * The actual uIP function which does all the work.
00943  */
00944 void uip_process(u8_t flag);
00945 
00946 /* The following flags are passed as an argument to the uip_process()
00947    function. They are used to distinguish between the two cases where
00948    uip_process() is called. It can be called either because we have
00949    incoming data that should be processed, or because the periodic
00950    timer has fired. */
00951 
00952 #define UIP_DATA    1     /* Tells uIP that there is incoming data in
00953                              the uip_buf buffer. The length of the
00954                              data is stored in the global variable
00955                              uip_len. */
00956 #define UIP_TIMER   2     /* Tells uIP that the periodic timer has
00957                              fired. */
00958 #if UIP_UDP
00959 #define UIP_UDP_TIMER 3
00960 #endif /* UIP_UDP */
00961 
00962 /* The TCP states used in the uip_conn->tcpstateflags. */
00963 #define CLOSED      0
00964 #define SYN_RCVD    1
00965 #define SYN_SENT    2
00966 #define ESTABLISHED 3
00967 #define FIN_WAIT_1  4
00968 #define FIN_WAIT_2  5
00969 #define CLOSING     6
00970 #define TIME_WAIT   7
00971 #define LAST_ACK    8
00972 #define TS_MASK     15
00973   
00974 #define UIP_STOPPED      16
00975 
00976 #define UIP_TCPIP_HLEN 40
00977 
00978 /* The TCP and IP headers. */
00979 typedef struct {
00980   /* IP header. */
00981   u8_t vhl,
00982     tos,          
00983     len[2],       
00984     ipid[2],        
00985     ipoffset[2],  
00986     ttl,          
00987     proto;     
00988   u16_t ipchksum;
00989   u16_t srcipaddr[2], 
00990     destipaddr[2];
00991   
00992   /* TCP header. */
00993   u16_t srcport,
00994     destport;
00995   u8_t seqno[4],  
00996     ackno[4],
00997     tcpoffset,
00998     flags,
00999     wnd[2];     
01000   u16_t tcpchksum;
01001   u8_t urgp[2];
01002   u8_t optdata[4];
01003 } uip_tcpip_hdr;
01004 
01005 /* The ICMP and IP headers. */
01006 typedef struct {
01007   /* IP header. */
01008   u8_t vhl,
01009     tos,          
01010     len[2],       
01011     ipid[2],        
01012     ipoffset[2],  
01013     ttl,          
01014     proto;     
01015   u16_t ipchksum;
01016   u16_t srcipaddr[2], 
01017     destipaddr[2];
01018   /* ICMP (echo) header. */
01019   u8_t type, icode;
01020   u16_t icmpchksum;
01021   u16_t id, seqno;  
01022 } uip_icmpip_hdr;
01023 
01024 
01025 /* The UDP and IP headers. */
01026 typedef struct {
01027   /* IP header. */
01028   u8_t vhl,
01029     tos,          
01030     len[2],       
01031     ipid[2],        
01032     ipoffset[2],  
01033     ttl,          
01034     proto;     
01035   u16_t ipchksum;
01036   u16_t srcipaddr[2], 
01037     destipaddr[2];
01038   
01039   /* UDP header. */
01040   u16_t srcport,
01041     destport;
01042   u16_t udplen;
01043   u16_t udpchksum;
01044 } uip_udpip_hdr;
01045 
01046 #define UIP_PROTO_ICMP  1
01047 #define UIP_PROTO_TCP   6
01048 #define UIP_PROTO_UDP   17
01049 
01050 #if UIP_FIXEDADDR
01051 extern const u16_t uip_hostaddr[2];
01052 #else /* UIP_FIXEDADDR */
01053 extern u16_t uip_hostaddr[2];
01054 #endif /* UIP_FIXEDADDR */
01055 
01056 #endif /* __UIP_H__ */
01057 
01058 

Generated on Fri Jan 6 22:23:17 2006 for EtherProgs by  doxygen 1.4.5