home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / MSDOS / WATTCP / MSWATTCP.ZIP / INCLUDE / TCP.H < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-27  |  15.3 KB  |  515 lines

  1. /***********************************************************************
  2.  *
  3.  * File: tcp.h
  4.  *
  5.  * last revised: 06-Jul-92 lr
  6.  *
  7.  * 06-Sep-93 lr,fr
  8.  *    final cleanup, version becomes 0301
  9.  * 19-Jun-92 lr
  10.  *      cleaned up, merged from wattcp.h and tcp.h
  11.  *
  12.  * Waterloo TCP - TCP/IP library routines
  13.  * 
  14.  * Copyright 1992 Universita` di Pisa
  15.  *
  16.  * This network library was originally created by Erick Engelke,
  17.  * of the University of Waterloo, Waterloo, Ontario, Canada.
  18.  * Porting to Microsoft C, and thorough rewriting was done by
  19.  * Luigi Rizzo of the Dip. di Ingegneria dell'Informazione of the
  20.  * Universita` di Pisa, Pisa, ITALY. Also, some parts are taken
  21.  * by previous implementations of TCP-IP.
  22.  *
  23.  *
  24.  * Portions Copyright (C) 1990, 1991, University of Waterloo
  25.  *
  26.  * Portions Copyright (C) 1990, National Center for Supercomputer Applications
  27.  * Portions Copyright (C) 1990, Clarkson University
  28.  * Portions Copyright (C) 1983, 1986, Imagen Corporation
  29.  *
  30.  * This software is distributed in the hope that it will be useful,
  31.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  33.  *
  34.  * You may freely use this library to produce programs which you may give
  35.  * away or sell in source format, or as compiled executables, or both.
  36.  *
  37.  * You may not sell this library or a portion thereof or distribute modified
  38.  * versions the library code in either source or OBJect or LIBrary format
  39.  * without written permission from the authors.
  40.  *
  41.  * Name resolution services were adapted from sources made available by
  42.  * the National Centre for Supercomputer Applications (NCSA) and Clarkson
  43.  * University.
  44.  *
  45.  *   A programmer's reference for WATTCP is available from:
  46.  *
  47.  *       WATTCP Programmer's Reference
  48.  *       c/o SNSI
  49.  *       P.O. Box 18,
  50.  *       Warsaw, Ont.
  51.  *       Canada
  52.  *       K0L 3A0
  53.  *
  54.  *    or phone 705-652-1572 (does not need a country code from U.S.A.
  55.  *    or Canada)
  56.  *
  57.  *    Credit cards, purchase orders and other forms of payment accepted.
  58.  */
  59.  
  60. #define WATTCP_C "Copyright 1992, Universita` di Pisa\n"
  61.  
  62. #include <stdio.h>
  63. #include <io.h>         /* read/write/open/close */
  64. #include <stdlib.h>     /* for malloc(), free() */
  65. #include <string.h>
  66. #include <dos.h>
  67. #include <conio.h>      /* for kbhit() */
  68. #include <socket.h>     /* fr */
  69.  
  70.  
  71. #define FDB stderr      /* debug file */
  72.  
  73. #define DB4(x)    fprintf x
  74. #define DB3(x)    if (debug_on) DB4(x)
  75. #ifdef debug
  76. #    define DB2(x)    DB4(x)
  77. #else
  78. #    define DB2(x)    /* never print anything */
  79. #endif
  80. /* Kernel version (major major minor minor) */
  81. #define WTCP_VER 0x0301
  82.  
  83. /*
  84.  * Typedefs and constants
  85.  */
  86.  
  87. #ifndef byte
  88. #define byte unsigned char
  89. #endif  byte
  90.  
  91. #ifndef word
  92. #define word unsigned short
  93. #endif  word
  94.  
  95. #ifndef longword
  96. #define longword unsigned long
  97. #endif  longword
  98.  
  99.  
  100. typedef int (*procref)();
  101. typedef byte eth_address[6]; /* check this! */
  102.  
  103. #define WATTCPH
  104.  
  105. #define MAX_GATE_DATA 12        /* number of gateways */
  106. #define MAX_STRING 50           /* most strings are limited */
  107.  
  108. #define MAXBUFS         5       /* maximum number of Ethernet buffers */
  109. #define BUFSIZE         1500    /* size of ethernet buffers */
  110.  
  111. #define MAXVJSA         1440    /* 10 s */
  112. #define MAXVJSD         360     /* 10 s */
  113. #define SAFETYTCP       0x538f25a3L
  114. #define SAFETYUDP       0x3e45e154L
  115.  
  116. #define TRUE            1
  117. #define FALSE           0
  118. #define TICKS_SEC       18
  119.  
  120.  
  121. #define PD_ETHER        1       /* packet driver class */
  122. #define PD_SLIP         6       /* packet driver class */
  123.  
  124. /* The Ethernet header */
  125. typedef struct {
  126.     eth_address     destination;
  127.     eth_address     source;
  128.     word            type;
  129. } eth_Header;
  130.  
  131. /* The Internet Header: */
  132. typedef struct {
  133.     unsigned        hdrlen  : 4, ver : 4, tos : 8;
  134.     word            length;
  135.     word            identification;
  136.     word            frag;
  137.     byte            ttl;
  138.     byte            proto;
  139.     word            checksum;
  140.     longword        source;
  141.     longword        destination;
  142. } in_Header;
  143.  
  144. typedef struct {
  145.     word            srcPort;
  146.     word            dstPort;
  147.     word            length;
  148.     word            checksum;
  149. } udp_Header;
  150.  
  151. #define UDP_LENGTH ( sizeof( udp_Header ))
  152.  
  153. typedef struct {
  154.     word            srcPort;
  155.     word            dstPort;
  156.     longword        seqnum;
  157.     longword        acknum;
  158.     word            flags;
  159.     word            window;
  160.     word            checksum;
  161.     word            urgentPointer;
  162. } tcp_Header;
  163.  
  164. /*** These are Intelled values ***/
  165. #define tcp_FlagFIN     0x0001
  166. #define tcp_FlagSYN     0x0002
  167. #define tcp_FlagRST     0x0004
  168. #define tcp_FlagPUSH    0x0008
  169. #define tcp_FlagACK     0x0010
  170. #define tcp_FlagURG     0x0020
  171. #define tcp_FlagDO      0xF000
  172. #define tcp_GetDataOffset(tp) (intel16((tp)->flags) >> 12)
  173.  
  174. /*** The TCP/UDP Pseudo Header ***/
  175. typedef struct {
  176.     longword    src;
  177.     longword    dst;
  178.     byte        mbz;
  179.     byte        protocol;
  180.     word        length;
  181.     word        checksum;
  182. } tcp_PseudoHeader;
  183.  
  184. /*
  185.  * TCP states, from tcp manual.
  186.  * Note: close-wait state is bypassed by automatically closing a connection
  187.  *       when a FIN is received.  This is easy to undo.
  188.  */
  189. #define tcp_StateLISTEN  0
  190.     /* listening for connection
  191.      */
  192. #define tcp_StateSYNSENT 1
  193.     /* active open, syn sent, waiting for SYN
  194.      */
  195. #define tcp_StateSYNREC  2
  196.     /* active open, syn received, synack+syn sent., waiting ack
  197.      */
  198. #define tcp_StateSYNRECLIS  3
  199.     /* passive open, syn received, synack+syn sent., waiting ack
  200.      */
  201. #define tcp_StateESTAB   4
  202.     /* established, can transmit data.
  203.      */
  204. #define tcp_StateESTCL   5
  205.     /* established+close (will FIN after flushing the retransmit
  206.      * queue).
  207.      */
  208. #define tcp_StateFINWT1  6
  209.     /* outgoing data flushed, sent FIN. Can still receive incoming
  210.      * data from the other party.
  211.      */
  212. #define tcp_StateFINWT2  7
  213.     /* sent FIN, received FINACK. Can still receive incoming data
  214.      * from the other party (he might be in CLOSEWT).
  215.      */
  216. #define tcp_StateCLOSWT  8
  217.     /* was in ESTAB, received FIN, waiting for close. Must still
  218.      * flush the outgoing queue, while no more data can arrive.
  219.      */
  220. #define tcp_StateCLOSWTCL 9
  221.     /* was in CLOSWT (received FIN), received close. Must still
  222.      * flush the outgoing queue but no new data can arrive.
  223.      */
  224. #define tcp_StateCLOSING 10
  225.     /* sent FIN, received FIN (waiting for FINACK). No more
  226.      * data can travel in both directions.
  227.      */
  228. #define tcp_StateLASTACK 11
  229.     /* FIN received, FINack+FIN sent. No more data can
  230.      * travel in both directions.
  231.      */
  232. #define tcp_StateTIMEWT  12
  233.     /* (from CLOSING or FINWT2). Delay after sending final FINACK
  234.      * which the other party might have lost.
  235.      */
  236. #define tcp_StateCLOSEMSL 13
  237.     /* Don't know what's this for.
  238.      */
  239. #define tcp_StateCLOSED  14
  240.     /* FINack received. The connection is down.
  241.      */
  242.  
  243. #define DEFAULT_BUFSIZE 2048    /* default buffer size for sockets */
  244.  
  245. /*** 1-apr-92 gm Moving the buffers outside the socket structure ***/
  246. /*
  247.  * UDP socket definition. Here, there is only a receive buffer
  248.  * because send is done on the fly.
  249.  */
  250.  
  251. typedef struct _udp_socket {
  252.     struct _udp_socket *next;
  253.     word            ip_type;            /* always set to UDP_PROTO */
  254.     char           *err_msg;            /* null when all is ok */
  255.     void          (*usr_yield)();
  256.     word            sock_mode;          /* a logical OR of bits */
  257.     longword        usertimer;          /* ip_timer_set, ip_timer_timeout */
  258.     procref         dataHandler;
  259.     eth_address     hisethaddr;         /* peer's ethernet address */
  260.     longword        hisaddr;            /* peer's internet address */
  261.     word            hisport;            /* peer's UDP port */
  262.     word            myport;
  263.  
  264.     short           rxbufsize;
  265.     short           rdatalen;           /* must be signed */
  266.     byte            *rdata;             /* if dataHandler==0,len=512 */
  267.     longword        safetysig;
  268. } udp_Socket;
  269.  
  270.  
  271. /*
  272.  * TCP Socket definition. We have variable buffer size.
  273.  * Buffers are allocated by new_socket() with default size. Space is
  274.  * only deallocated when necessary for a change in size.
  275.  * become 
  276.  *
  277.  */
  278.  
  279. typedef struct _tcp_socket {
  280.     struct _tcp_socket *next;       /* link field */
  281.     word            ip_type;        /* always set to TCP_PROTO */
  282.     char           *err_msg;        /* error string, null if OK */
  283.     void          (*usr_yield)();   /* whom to give control when blocked */
  284.     word            sock_mode;      /* a logical OR of bits */
  285.  
  286.     longword        usertimer;      /* ip_timer_set, ip_timer_timeout */
  287.     eth_address     hisethaddr;     /* ethernet address of peer */
  288.     longword        hisaddr;        /* internet address of peer */
  289.     word            hisport;        /* tcp ports for this connection */
  290.     word            myport;         /* my port of connection */
  291.  
  292.     word            state;          /* connection state */
  293.  
  294.  
  295.     short           rxbufsize;      /* normally defaultbufsize */
  296.     short           rdatalen;       /* must be signed, -1 means error */
  297.     byte            *rdata;         /* received data */
  298.     longword        safetysig;      /* a magic number */
  299.     procref         dataHandler;    /* called with incoming data */
  300.  
  301.     longword        acknum;         /* last ack sent */
  302.     longword        seqnum;         /* data ack'd and sequence num */
  303.     long            timeout;        /* timeout, in ticks */
  304.     byte            unhappy;        /* flag, indicates retransmitting segt's */
  305.     word            flags;          /* tcp flags word for last packet sent */
  306.  
  307.     word            window;         /* other guy's window */
  308.     short           txbufsize;      /* normally DEFAULT_BUFSIZE */
  309.     short           datalen;        /* number of bytes of data to send */
  310.     byte            *data;          /* data to send */
  311.                     /* must be signed */
  312.     short           unacked;        /* unacked data */
  313.  
  314.     /* 6-mar-92 added by lr-gm */
  315.     longword        irs;            /* Initial Receive Sequential number */
  316.     longword        iss;            /* Initial Send Sequential number    */
  317.     word            UP;             /* Urgent Pointer                    */
  318.     /* *********************** */
  319.  
  320.     word            vj_sa;          /* VJ's alg, standard average */
  321.     word            vj_sd;          /* VJ's alg, standard deviation */
  322.     longword        vj_last;        /* last transmit time */
  323.     word            rto;
  324.  
  325.     /* retransmission timeout proceedure */
  326.     /* these are in clock ticks */
  327.     longword        rtt_lasttran;       /* last transmission time */
  328.     longword        rtt_smooth;         /* smoothed round trip time */
  329.     longword        rtt_delay;          /* delay for next transmission */
  330.     longword        rtt_time;           /* time of next transmission */
  331.  
  332.     word            mss;
  333.     longword        inactive_to;        /* for the inactive flag */
  334.  
  335.     struct _tcp_socket   *father;      /* pointer to sock-list-descriptor*/
  336.     struct _tcp_socket   *brother;     /* link field for multi_listen*/
  337.     word           inlist;             /* 1=LAZY 2=BUSY*/ 
  338. } tcp_Socket;
  339.  
  340. /* sock_type used for socket io */
  341. typedef union {
  342.     udp_Socket udp;
  343.     tcp_Socket tcp;
  344. } sock_type;
  345.  
  346. /*
  347.  * ARP definitions
  348.  */
  349. #ifdef WATTCPH
  350. #define arp_TypeEther  0x100    /* ARP type of Eth addr (net format)*/
  351. #else
  352. #define arp_TypeEther  0x1      wrong.../* ARP type of Ethernet address */
  353. #endif /* WATTCPH */
  354.  
  355. /* harp op codes */
  356. #ifdef WATTCPH
  357. #define ARP_REQUEST 0x0100 /* net format */
  358. #define ARP_REPLY   0x0200 /* net format */
  359. #else
  360. #define ARP_REQUEST 1   wrong ... /* on purpose */
  361. #define ARP_REPLY   2   wrong ... /* on purpose */
  362. #endif  /* WATTCPH */
  363.  
  364. /*
  365.  * Arp header
  366.  */
  367. typedef struct {
  368.     word            hwType;
  369.     word            protType;
  370.     word            hwProtAddrLen;  /* hw and prot addr len */
  371.     word            opcode;
  372.     eth_address     srcEthAddr;
  373.     longword        srcIPAddr;
  374.     eth_address     dstEthAddr;
  375.     longword        dstIPAddr;
  376. } arp_Header;
  377.  
  378. #define ETH_MSS 1400  /* MSS for Ethernet */
  379.  
  380.  
  381. /**** MS C compatibility ****/
  382.  
  383. #include <memory.h>
  384.  
  385. #define movmem(src,dest,len)    memmove((void *)(dest),(void *)(src),(size_t)(len))
  386.  
  387. struct REGPACK {
  388.     struct WORDREGS x;
  389.     struct SREGS    s;
  390. #define         r_ax    x.ax
  391. #define         r_bx    x.bx
  392. #define         r_cx    x.cx
  393. #define         r_dx    x.dx
  394. #define         r_si    x.si
  395. #define         r_di    x.di
  396. #define         r_flags x.cflag
  397. #define         r_es    s.es
  398. #define         r_cs    s.cs
  399. #define         r_ds    s.ds
  400. #define         r_ss    s.ss
  401. };
  402.  
  403. #define getvect(x) _dos_getvect(x)
  404. #define intr(intno,r)   int86x(intno,(union REGS *)&((r)->x), \
  405.             (union REGS *)&((r)->x), &((r)->s))
  406. /**** end MS C compatibility ****/
  407. struct wat_sockaddr {
  408.     word        s_type;
  409.     word        s_port;
  410.     longword    s_ip;
  411.     byte        s_spares[6];    /* unused in TCP realm */
  412. };
  413.  
  414. typedef struct sockdesc{
  415.     int        valid;
  416.     int        type;
  417.     int         my_port;
  418.     sock_type    *sockp;
  419.     } sock_desc;
  420.  
  421. /** those are in socket.h
  422.  
  423. #define u_short    unsigned short
  424. #define u_long    unsigned long
  425. #define u_char    unsigned char
  426. #define u_int    unsigned int
  427.  
  428. #define AF_INET      2
  429. #define INADDR_ANY   0L
  430. #define SOCK_STREAM  1
  431. #define SOCK_DGRAM   2
  432. #define IPPROTO_IP   0
  433. #define IPPROTO_ICMP 1
  434. #define IPPROTO_UDP  17
  435. #define IPPROTO_TCP  6
  436. #define MAXSOCK      32
  437. #define MAXFILE      30
  438.  
  439.  
  440. struct sockaddr{
  441.     u_short        sa_family;
  442.     char        sa_data[14];
  443.     };
  444.  
  445. struct in_addr{
  446.     u_long        s_addr;
  447.     };
  448.  
  449. struct sockaddr_in {
  450.     short        sin_family;
  451.     u_short        sin_port;
  452.     struct in_addr    sin_addr;
  453.     char        sin_zero[8];
  454.     };
  455.  
  456.  ***** end socket.h */
  457.  
  458.  
  459. #define MAX_COOKIES     10
  460. #define MAX_NAMESERVERS 10
  461.  
  462. #define UDP_PROTO  0x11
  463. #define TCP_PROTO  0x06
  464. #define ICMP_PROTO 0x01
  465.  
  466. #define TCP_MODE_BINARY 0
  467. #define TCP_MODE_ASCII  1
  468. #define UDP_MODE_CHK    0       /*default to checksum */
  469. #define UDP_MODE_NOCHK  2
  470. #define TCP_MODE_NAGLE  0       /* Nagle algorithm */
  471. #define TCP_MODE_NONAGLE 4
  472.  
  473.  
  474. /*
  475.  * sock_wait_... macros
  476.  *
  477.  * sock_wait_established()
  478.  *      - waits then aborts if timeout on s connection
  479.  * sock_wait_input()
  480.  *      - waits for received input on s
  481.  *      - may not be valid input for sock_Gets... check returned length
  482.  * sock_tick()
  483.  *      - do tick and jump on abort
  484.  * sock_wait_closed();
  485.  *      - discards all received data
  486.  *
  487.  * jump to sock_err with contents of *statusptr set to
  488.  *       1 on closed
  489.  *      -1 on timeout
  490.  *
  491.  */
  492.  
  493.  
  494. #define sock_wait_established( s, seconds, fn, statusptr ) \
  495.     if (_ip_delay0( s, seconds, fn, statusptr )) goto sock_err;
  496. #define sock_wait_input( s, seconds, fn , statusptr ) \
  497.     if (_ip_delay1( s, seconds, fn, statusptr )) goto sock_err;
  498. #define sock_tick( s, statusptr ) \
  499.     if ( !tcp_tick(s)) { if (statusptr) *statusptr = 1 ; goto sock_err; }
  500. #define sock_wait_closed(s, seconds, fn, statusptr )\
  501.     if (_ip_delay2( s, seconds, fn, statusptr )) goto sock_err;
  502.  
  503.  
  504. #define ntohs(x) intel16(x)
  505. #define htons(x) intel16(x)
  506. #define ntohl(x) intel(x)
  507. #define htonl(x) intel(x)
  508.  
  509. extern char *itoa(int, char *, int);
  510. extern char *ltoa(long, char *, int);
  511.  
  512. #include <proto.h>      /* function prototypes */
  513.  
  514. /*** end of file tcp.h ***/
  515.