home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / msk316src.zip / MSNTCP.H < prev    next >
C/C++ Source or Header  |  1999-06-17  |  12KB  |  348 lines

  1. /* File MSNTCP.H
  2.  * Main include file for TCP/IP, as revised and modified for MS-DOS Kermit.
  3.  *
  4.  * Copyright (C) 1991, University of Waterloo.
  5.  *    Copyright (C) 1982, 1999, Trustees of Columbia University in the 
  6.  *    City of New York.  The MS-DOS Kermit software may not be, in whole 
  7.  *    or in part, licensed or sold for profit as a software product itself,
  8.  *    nor may it be included in or distributed with commercial products
  9.  *    or otherwise distributed by commercial concerns to their clients 
  10.  *    or customers without written permission of the Office of Kermit 
  11.  *    Development and Distribution, Columbia University.  This copyright 
  12.  *    notice must not be removed, altered, or obscured.
  13.  *
  14.  * Original version created by Erick Engelke of the University of
  15.  *  Waterloo, Waterloo, Ontario, Canada.
  16.  *       Erick Engelke                Erick@development.watstar.uwaterloo.ca
  17.  *       Faculty of Engineering
  18.  *       University of Waterloo       (519) 885-1211 Ext. 2965
  19.  *       200 University Ave.,
  20.  *       Waterloo, Ont., Canada
  21.  *       N2L 3G1
  22.  * Adapted and modified for MS-DOS Kermit by Joe R. Doupnik, 
  23.  *  Utah State University, jrd@cc.usu.edu, jrd@usu.Bitnet,
  24.  *  and by Frank da Cruz, Columbia University, fdc@watsun.cc.columbia.edu.
  25.  *
  26.  * Name resolution services were adapted from sources made available by
  27.  * the National Centre for Supercomputer Applications (NCSA) and Clarkson
  28.  * University.
  29.  *
  30.  * The C code is designed for the small memory model of Microsoft C versions
  31.  * 5.1 and later, with structures packed on one byte boundaries. No other
  32.  * options are expected.
  33.  *
  34.  * Last edit:
  35.  * 12 Jan 1995 version 3.14
  36.  */
  37.  
  38. #define KERMIT
  39.  
  40. /*
  41.  * Typedefs and constants
  42.  */
  43.  
  44. #ifndef byte
  45. typedef unsigned char byte;
  46. #endif  /* byte */
  47. #ifndef word
  48. typedef unsigned int word;
  49. #endif  /* word */
  50. #ifndef longword
  51. typedef unsigned long longword;
  52. #endif /* longword */
  53.  
  54. typedef int (*procref)(void *, void *, int);
  55. typedef byte eth_address[6];
  56.  
  57.  
  58. /* MSC v6.0 & v7.0 &v8.0 definition of Far as _far */
  59. #define FAR _far
  60.  
  61. /*
  62. #define DEBUG
  63. */
  64.  
  65. #ifndef NULL
  66. #define NULL 0
  67. #endif
  68.  
  69. #define TRUE        1
  70. #define FALSE       0
  71.  
  72. #define ETH_MSS 1460             /* MSS for Ethernet >= 536 */
  73. #define MAX_GATE_DATA 3
  74. #define MAX_NAMESERVERS 3
  75. #define MAX_COOKIES 0
  76. #define MAX_STRING 64    
  77. #define TICKS_SEC 18
  78. #define PD_ETHER 1
  79. #define PD_SLIP  6
  80.  
  81. #define    BOOT_FIXED    0    /* methods of obtaining our IP address */
  82. #define    BOOT_BOOTP    1
  83. #define    BOOT_RARP    2
  84. #define BOOT_DHCP    3
  85.  
  86. /* Ethernet frame TYPE's in network order, reverse for host order */
  87. #define TYPE_IP        0x0008
  88. #define TYPE_ARP    0x0608
  89. #define TYPE_RARP    0x3580
  90.  
  91. /* Ethernet protocol identifications */
  92. #define ICMP_PROTO 0x01
  93. #define TCP_PROTO  0x06
  94. #define UDP_PROTO  0x11
  95.  
  96.                 /* sock_mode byte bit-field */
  97. #define TCP_MODE_NAGLE    0x01    /* TCP Nagle algorithm */
  98. #define TCP_MODE_PASSIVE 0x02    /* TCP, opened in passive mode */
  99. #define UDP_MODE_CHKSUM    0x08    /* UDP checksums */
  100.  
  101. /* The Ethernet header */
  102. typedef struct {
  103.     eth_address     destination;
  104.     eth_address     source;
  105.     word            type;
  106. } eth_Header;
  107.  
  108. /* The Internet Header: */
  109. typedef struct {
  110.     byte        hdrlen_ver;        /* both in one byte */
  111.     byte        tos;
  112.     word            length;
  113.     word            identification;
  114.     word            frag;
  115.     byte        ttl;
  116.     byte        proto;
  117.     word            checksum;
  118.     longword        source;
  119.     longword        destination;
  120. } in_Header;
  121.  
  122. typedef struct {
  123.     word        srcPort;
  124.     word        dstPort;
  125.     word        length;
  126.     word        checksum;
  127. } udp_Header;
  128.  
  129. typedef struct {
  130.     word            srcPort;
  131.     word            dstPort;
  132.     longword        seqnum;
  133.     longword        acknum;
  134.     word            flags;
  135.     word            window;
  136.     word            checksum;
  137.     word            urgentPointer;
  138. } tcp_Header;
  139.  
  140. /* The TCP/UDP Pseudo Header */
  141. typedef struct {
  142.     longword    src;
  143.     longword    dst;
  144.     byte        mbz;
  145.     byte        protocol;
  146.     word        length;
  147.     word        checksum;
  148. } tcp_PseudoHeader;
  149.  
  150. /* TCP states */
  151. #define tcp_StateLISTEN  0x0000      /* listening for connection */
  152. #define tcp_StateSYNSENT 0x0001      /* syn sent, active open */
  153. #define tcp_StateSYNREC  0x0002      /* syn received, synack+syn sent. */
  154. #define tcp_StateESTAB   0x0004      /* established */
  155. #define tcp_StateFINWT1  0x0008      /* sent FIN */
  156. #define tcp_StateFINWT2  0x0010      /* sent FIN, received FINACK */
  157. #define tcp_StateCLOSWT  0x0020      /* received FIN waiting for close */
  158. #define tcp_StateCLOSING 0x0040      /* sent FIN, recvd FIN (waiting for FINACK) */
  159. #define tcp_StateLASTACK 0x0080      /* fin received, finack+fin sent */
  160. #define tcp_StateTIMEWT  0x0100      /* dally after sending final FINACK */
  161. #define tcp_StateCLOSEMSL 0x0200
  162. #define tcp_StateCLOSED  0x0400       /* finack received */
  163.  
  164. #define SOCKET_OPEN    1        /* if socket structure is active */
  165. #define SOCKET_CLOSED    0
  166. #define TIMED_OUT    1        /* if a timer has expired */
  167.  
  168. #define DEBUG_STATUS    1        /* Set TCP DEBUG for general info */
  169. #define DEBUG_TIMING    2        /* for round trip timing */
  170. /*
  171.  * UDP socket definition, must match start of tcp_socket.
  172.  */
  173. typedef struct udp_socket {
  174.     struct udp_socket *next;
  175.     word        ip_type;        /* always set to UDP_PROTO */
  176.     byte        sisopen;        /* non-zero if socket is open */
  177.     byte        sock_mode;            /* a logical OR of bits */
  178.     eth_address     hisethaddr;        /* peer's Ethernet address */
  179.     longword        hisaddr;        /* peer's Internet address */
  180.     word        hisport;        /* peer's UDP port */
  181.     word        myport;        /* our UDP port */
  182.     int             rdatalen;          /* signed, bytes in receive buffer */
  183.     byte        FAR * rdata;    /* Far ptr to data buffer */
  184. } udp_Socket;
  185.  
  186. /* TCP segment send queue status per segment */
  187. typedef struct sendqstat {
  188.     longword    time_sent;    /* Bios ticks when datagram was sent*/
  189.     longword    timeout;    /* Bios ticks when dgram times out */
  190.     longword    next_seq;    /* seq number of start of next dgram */
  191. };
  192. #define NSENDQ    20            /* number of timed segments */
  193. #define DELAY_ACKS            /* define to use delayed ACKs */
  194.  
  195. /*
  196.  * TCP Socket definition
  197.  */
  198.  
  199. typedef struct tcp_socket {
  200.     struct tcp_socket *next;
  201.     word        ip_type;        /* always set to TCP_PROTO */
  202.     byte        sisopen;        /* non-zero if socket is open */
  203.     byte        sock_mode;        /* a logical OR of bits */
  204.     eth_address     hisethaddr;     /* Ethernet address of peer */
  205.     longword        hisaddr;        /* Internet address of peer */
  206.     word            hisport;        /* TCP port at peer */
  207.     word        myport;        /* our TCP port */
  208.     int             rdatalen;       /* signed, bytes in receive buffer */
  209.     byte        FAR * rdata;    /* Far ptr to receive data buffer */
  210.                     /* above must also match udp_socket */
  211.     byte            FAR * sdata;    /* Far ptr to send data buffer */
  212.     int         sdatalen;        /* signed number of bytes of data to send*/
  213.     int            sdatahwater;    /* high water mark in buffer */
  214.     word        state;          /* connection state */
  215.     longword        acknum;        /* last received byte + 1 */
  216.     longword        seqnum;         /* last sent byte + 1 */
  217.     word            flags;          /* tcp flags word for last packet sent */
  218.     word            mss;        /* active Max Segment Size */
  219.     word        window;        /* remote host's window size */
  220.     word        old_window;        /* remote host's previous window size */
  221.     word        cwindow;        /* Van Jacobson's algorithm, congest win*/
  222.     word        ssthresh;        /* VJ congestion threshold */
  223.     byte        loss_count;        /* times in row have lost pkt */
  224.     byte        last_read;        /* last byte read by tcp_read, for CR NUL*/
  225.     word        vj_sa;        /* VJ's algorithm, standard average */
  226.     word        vj_sd;        /* VJ's algorithm, standard deviation */
  227.     word        rto;        /* round trip timeout */
  228.     longword        timeout;        /* TCP state timeout, in Bios tics */
  229.     longword        notimeseq;        /* seq number below which repeats sent */
  230.     longword        idle;        /* timeout for last xmission + rto */
  231.     longword        winprobe;        /* timeout on window probes */
  232.     longword        keepalive;        /* timeout on passive open'd keepalives */
  233. #ifdef DELAY_ACKS
  234.     longword        delayed_ack;    /* timeout on pending delayed ACK */
  235. #endif
  236.     word        probe_wait;        /* ticks between window probes */
  237.     word        window_last_sent; /* our window, last sent value */
  238.     byte        send_kind;        /* flag, nosend, sendnow */
  239.     int            sendqnum;        /* number of active entries in sendq */
  240.     struct sendqstat sendq[NSENDQ]; /* send queue stats per IP datagram */
  241. } tcp_Socket;
  242.  
  243.  
  244. /* sock_type used for socket io */
  245. typedef union {
  246.     udp_Socket udp;
  247.     tcp_Socket tcp;
  248. } sock_type;
  249.  
  250. /*
  251.  * socket macros
  252.  */
  253.  
  254. /*
  255.  * sock_wait_established()
  256.  *    - waits then aborts if timeout on s connection
  257.  * sock_wait_input()
  258.  *    - waits for received input on s
  259.  *    - may not be valid input for sock_Gets... check returned length
  260.  * sock_wait_closed();
  261.  *    - discards all received data
  262.  *
  263.  * jump to sock_err with contents of *statusptr set to
  264.  *     1 on closed
  265.  *    -1 on timeout
  266.  */
  267. #define sock_wait_established(s, seconds, fn, statusptr) \
  268.     if (ip_delay0(s, seconds, fn, statusptr)) goto sock_err;
  269. #define sock_wait_input(s, seconds, fn , statusptr) \
  270.     if (ip_delay1(s, seconds, fn, statusptr)) goto sock_err;
  271. #define sock_wait_closed(s, seconds, fn, statusptr)\
  272.     if (ip_delay2(s, seconds, fn, statusptr)) goto sock_err;
  273.  
  274. /* s is a pointer to a udp or tcp socket */
  275. int    sock_read(void *, byte FAR *, int);
  276. int    sock_fastread(void *, byte FAR *, int);
  277. int    sock_write(void *, byte FAR *, int);
  278. word    sock_dataready(void *);
  279. int    sock_established(void *);
  280. int    sock_close(void *);
  281. void    sock_abort(void *);
  282. int    sock_setmode(void *, word, word);
  283.  
  284. /*
  285.  * TCP or UDP specific material, must be used for open's and listens, but
  286.  * sock calls are used for everything else.
  287.  */
  288. int    udp_open(void *, word, longword, word);
  289. int    tcp_open(void *, word, longword, word);
  290. int    tcp_listen(void *, word, longword, word, word);
  291. int    tcp_established(void *);
  292.  
  293. /* timers */
  294. int    ip_delay0(void *, int, procref, int *);
  295. int    ip_delay1(void *, int, procref, int *);
  296. int    ip_delay2(void *, int, procref, int *);
  297.  
  298. /* tcp_init/tcp_shutdown, init/kill all tcp and lower services.
  299.    Call if sock_init is not used, else not recommended.
  300. */
  301. int    tcp_init(void);
  302. void    tcp_shutdown(void);
  303. int    tcp_abort(tcp_Socket *);
  304. /* tcp_tick - called periodically by user application in sock_wait.
  305.   returns 0 when our socket closes
  306. */
  307. int    tcp_tick(void *);
  308.  
  309. int    tcp_cancel(void *);
  310. int    udp_cancel(void *);
  311.  
  312. int    eth_init();
  313. byte *    eth_formatpacket(void *, word);
  314. int    eth_send(word);
  315. void    eth_free(void *);
  316. byte *    eth_arrived(word *);
  317. void    eth_release(void);
  318. void *    eth_hardware(void *);
  319. int    do_bootp(void);
  320. int    do_rarp(void);
  321. int    do_ping(byte *, longword);
  322. longword resolve(byte *);
  323. int    add_server(int *, int, longword *, longword);
  324. void    arp_init(void);
  325. int    arp_resolve(longword, void *);
  326. void    arp_register(longword, longword);
  327. void    arp_add_gateway(byte *, longword);
  328. longword arp_rpt_gateway(int);
  329. int    pkt_rarp_init(void);
  330.  
  331. word     ntohs(word);
  332. word     htons(word);
  333. longword ntohl(longword);
  334. longword htonl(longword);
  335. void *    movmem(void *, void *, int);
  336.  
  337. extern    word pktdevclass;
  338. extern    word mss;
  339. extern    longword bootphost;
  340. extern    longword my_ip_addr;
  341. extern    eth_address eth_addr;
  342. extern    eth_address eth_brdcast;
  343. extern    longword sin_mask;
  344. extern    int last_nameserver;
  345. extern    word arp_last_gateway;
  346. extern    longword def_nameservers[];
  347. extern    word debug_on;
  348.