home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / msr313src.zip / msntcp.h < prev    next >
C/C++ Source or Header  |  1993-07-12  |  14KB  |  430 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) 1991,1993, Trustees of Columbia University in the
  6.  *  City of New York.  Permission is granted to any individual or
  7.  *  institution to use, copy, or redistribute this software as long as
  8.  *  it is not sold for profit and this copyright notice is retained.
  9.  *
  10.  * Original version created by Erick Engelke of the University of
  11.  *  Waterloo, Waterloo, Ontario, Canada.
  12.  *       Erick Engelke                Erick@development.watstar.uwaterloo.ca
  13.  *       Faculty of Engineering
  14.  *       University of Waterloo       (519) 885-1211 Ext. 2965
  15.  *       200 University Ave.,
  16.  *       Waterloo, Ont., Canada
  17.  *       N2L 3G1
  18.  * Adapted and modified for MS-DOS Kermit by Joe R. Doupnik, 
  19.  *  Utah State University, jrd@cc.usu.edu, jrd@usu.Bitnet,
  20.  *  and by Frank da Cruz, Columbia University, fdc@watsun.cc.columbia.edu.
  21.  *
  22.  * Name resolution services were adapted from sources made available by
  23.  * the National Centre for Supercomputer Applications (NCSA) and Clarkson
  24.  * University.
  25.  *
  26.  * The C code is designed for the small memory model of Microsoft C versions
  27.  * 5.1 and later, with structures packed on one byte boundaries. No other
  28.  * options are expected.
  29.  *
  30.  * Last edit:
  31.  * 18 June 1993
  32.  */
  33.  
  34. /* Kernel version (major major minor minor) */
  35. #define WTCP_VER 0x0311
  36. #define KERMIT
  37.  
  38. /*
  39.  * Typedefs and constants
  40.  */
  41.  
  42. #ifndef byte
  43. typedef unsigned char byte;
  44. #endif  /* byte */
  45. #ifndef word
  46. typedef unsigned int word;
  47. #endif  /* word */
  48. #ifndef longword
  49. typedef unsigned long longword;
  50. #endif /* longword */
  51.  
  52. typedef int (*procref)(void *, void *, int);
  53. typedef byte eth_address[6];
  54.  
  55.  
  56. /* MSC v6.0 & v7.0 definition of Far as _far */
  57. #define FAR _far
  58.  
  59. /*
  60. #define DEBUG
  61. */
  62.  
  63. #ifndef NULL
  64. #define NULL 0
  65. #endif
  66.  
  67. #define TRUE        1
  68. #define FALSE       0
  69.  
  70. #ifdef KERMIT
  71. #define ETH_MSS 1046             /* MSS for Ethernet >= 536 */
  72. #define TCP_SBUFSIZE (2*ETH_MSS)    /* max bytes to buffer in a tcp socket */
  73. #define TCP_RBUFSIZE 4096
  74. #define UDP_BUFSIZE ETH_MSS        /* max bytes to buffer in a udp socket */
  75. #define MAX_GATE_DATA 2
  76. #define MAX_NAMESERVERS 3
  77. #define MAX_COOKIES 0
  78. #else
  79. #define ETH_MSS 1500            /* MSS for Ethernet */
  80. #define tcp_MaxBufSize 2048    /* maximum bytes to buffer in a socket */
  81. #define udp_MaxBufSize 2048    /* max bytes to buffer in a udp socket */
  82. #define MAX_GATE_DATA 12
  83. #define MAX_NAMESERVERS 10
  84. #define MAX_COOKIES 10
  85. #endif
  86.  
  87. #define MAX_STRING 50    
  88. #define TICKS_SEC 18
  89. #define PD_ETHER 1
  90. #define PD_SLIP  6
  91.  
  92. /* Ethernet frame TYPE's in network order, reverse for host order */
  93. #define TYPE_IP        0x0008
  94. #define TYPE_ARP    0x0608
  95. #define TYPE_RARP    0x3580
  96.  
  97. /* Ethernet protocol identifications */
  98. #define UDP_PROTO  0x11
  99. #define TCP_PROTO  0x06
  100. #define ICMP_PROTO 0x01
  101.  
  102. #define TCP_MODE_BINARY  0       /* default mode */
  103. #define TCP_MODE_ASCII   1
  104. #define TCP_MODE_NAGLE   0       /* Nagle algorithm */
  105. #define TCP_MODE_NONAGLE 4
  106. #define UDP_MODE_CHK     0       /* default to having checksums */
  107. #define UDP_MODE_NOCHK   2       /* turn off checksums */
  108.  
  109. /* The Ethernet header */
  110. typedef struct {
  111.     eth_address     destination;
  112.     eth_address     source;
  113.     word            type;
  114. } eth_Header;
  115.  
  116. /* The Internet Header: */
  117. typedef struct {
  118.     byte        hdrlen_ver;        /* both in one byte */
  119.     byte        tos;
  120.     word            length;
  121.     word            identification;
  122.     word            frag;
  123.     byte        ttl;
  124.     byte        proto;
  125.     word            checksum;
  126.     longword        source;
  127.     longword        destination;
  128. } in_Header;
  129.  
  130.  
  131. #define in_GetVersion(ip) ((ip)->hdrlen_ver >> 4)
  132. #define in_GetHdrlen(ip)  ((ip)->hdrlen_ver & 0xf)  /* 32 bit word size */
  133. #define in_GetHdrlenBytes(ip)  (in_GetHdrlen(ip) << 2) /* 8 bit byte size */
  134. #define in_GetTos(ip)      ((ip)->tos)
  135. #define in_GetTTL(ip)      ((ip)->ttl)
  136. #define in_GetProtocol(ip) ((ip)->proto)
  137.  
  138. typedef struct {
  139.     word        srcPort;
  140.     word        dstPort;
  141.     word        length;
  142.     word        checksum;
  143. } udp_Header;
  144.  
  145. #define UDP_LENGTH (sizeof(udp_Header))
  146.  
  147. typedef struct {
  148.     word            srcPort;
  149.     word            dstPort;
  150.     longword        seqnum;
  151.     longword        acknum;
  152.     word            flags;
  153.     word            window;
  154.     word            checksum;
  155.     word            urgentPointer;
  156. } tcp_Header;
  157.  
  158. /* The TCP/UDP Pseudo Header */
  159. typedef struct {
  160.     longword    src;
  161.     longword    dst;
  162.     byte        mbz;
  163.     byte        protocol;
  164.     word        length;
  165.     word        checksum;
  166. } tcp_PseudoHeader;
  167.  
  168.  
  169. #define tcp_FlagFIN     0x0001
  170. #define tcp_FlagSYN     0x0002
  171. #define tcp_FlagRST     0x0004
  172. #define tcp_FlagPUSH    0x0008
  173. #define tcp_FlagACK     0x0010
  174. #define tcp_FlagURG     0x0020
  175. #define tcp_FlagDO      0xF000
  176. #define tcp_GetDataOffset(tp) (ntohs((tp)->flags) >> 12)
  177.  
  178. /*
  179.  * TCP states, from tcp manual.
  180.  * Note: close-wait state is bypassed by automatically closing a connection
  181.  *       when a FIN is received.  This is easy to undo.
  182.  */
  183. #define tcp_StateLISTEN  0      /* listening for connection */
  184. #define tcp_StateSYNSENT 1      /* syn sent, active open */
  185. #define tcp_StateSYNREC  2      /* syn received, synack+syn sent. */
  186. #define tcp_StateESTAB   3      /* established */
  187. #define tcp_StateFINWT1  4      /* sent FIN */
  188. #define tcp_StateFINWT2  5      /* sent FIN, received FINACK */
  189. #define tcp_StateCLOSWT  6      /* received FIN waiting for close */
  190. #define tcp_StateCLOSING 7      /* sent FIN, recvd FIN (waiting for FINACK) */
  191. #define tcp_StateLASTACK 8      /* fin received, finack+fin sent */
  192. #define tcp_StateTIMEWT  9      /* dally after sending final FINACK */
  193. #define tcp_StateCLOSEMSL 10
  194. #define tcp_StateCLOSED  11     /* finack received */
  195.  
  196. #define SOCKET_OPEN    1
  197. #define SOCKET_CLOSED    0
  198.  
  199. /*
  200.  * UDP socket definition, must match start of tcp_socket.
  201.  */
  202. typedef struct udp_socket {
  203.     struct udp_socket *next;
  204.     word        ip_type;        /* always set to UDP_PROTO */
  205.     byte        sisopen;        /* non-zero if socket is open */
  206.     byte       *err_msg;        /* null when all is ok */
  207.     word        soc_mode;            /* a logical OR of bits */
  208.     longword        usertimer;        /* ip_timer_set, ip_timer_timeout */
  209.     eth_address     hisethaddr;        /* peer's Ethernet address */
  210.     longword        hisaddr;        /* peer's Internet address */
  211.     word        hisport;        /* peer's UDP port */
  212.     word        myport;
  213.     int             rdatalen;           /* must be signed */
  214.     byte        FAR * rdata;    /* Far ptr to data buffer */
  215. } udp_Socket;
  216.  
  217. /*
  218.  * TCP Socket definition
  219.  */
  220.  
  221. typedef struct tcp_socket {
  222.     struct tcp_socket *next;
  223.     word        ip_type;        /* always set to TCP_PROTO */
  224.     byte        sisopen;        /* non-zero if socket is open */
  225.     byte        *err_msg;
  226.     word        soc_mode;        /* a logical OR of bits */
  227.     longword        usertimer;        /* ip_timer_set, ip_timer_timeout */
  228.     eth_address     hisethaddr;     /* Ethernet address of peer */
  229.     longword        hisaddr;        /* Internet address of peer */
  230.     word            hisport;        /* tcp ports for this connection */
  231.     word        myport;
  232.     int             rdatalen;       /* signed, bytes in receive buffer */
  233.     byte        FAR * rdata;    /* Far ptr to receive data buffer */
  234.                     /* above must also match udp_socket */
  235.     int            rmaxdatalen;    /* normally TCP_MAXRBUFSIZE */
  236.     byte            FAR * sdata;    /* Far ptr to send data buffer */
  237.     int         sdatalen;        /* signed number of bytes of data to send*/
  238.     word        state;          /* connection state */
  239.     longword        acknum;
  240.     longword        seqnum;         /* data ack'd and sequence num */
  241.     long            timeout;        /* timeout, in milliseconds */
  242.     word            flags;          /* tcp flags word for last packet sent */
  243.     word            mss;        /* active Max Segment Size */
  244.     word        window;        /* other guy's window */
  245.     byte        cwindow;        /* Van Jacobson's algorithm */
  246.     byte        wwindow;
  247.     word        vj_sa;        /* VJ's alg, standard average */
  248.     word        vj_sd;        /* VJ's alg, standard deviation */
  249.     longword        vj_last;        /* last transmit time */
  250.     word        rto;
  251.     byte        karn_count;        /* count of packets */
  252.     byte            rptxmit;        /* flag, to rpt transmission on timeout */
  253.  
  254.     /* retransmission timeout proceedure, these are in PC clock ticks */
  255.     longword        rtt_lasttran;       /* last transmission time */
  256.     longword        rtt_smooth;         /* smoothed round trip time */
  257.     longword        rtt_delay;          /* delay for next transmission */
  258.     longword        rtt_time;           /* time of next transmission */
  259. } tcp_Socket;
  260.  
  261. /* sock_type used for socket io */
  262. typedef union {
  263.     udp_Socket udp;
  264.     tcp_Socket tcp;
  265. } sock_type;
  266.  
  267. /* similar to UNIX */
  268. typedef struct sockaddr {
  269.     word        s_type;
  270.     word        s_port;
  271.     longword    s_ip;
  272.     byte        s_spares[6];    /* unused in TCP realm */
  273. };
  274.  
  275. /*
  276.  * socket macros
  277.  */
  278.  
  279. /*
  280.  * sock_wait_established()
  281.  *    - waits then aborts if timeout on s connection
  282.  * sock_wait_input()
  283.  *    - waits for received input on s
  284.  *    - may not be valid input for sock_Gets... check returned length
  285.  * sock_tick()
  286.  *    - do tick and jump on abort
  287.  * sock_wait_closed();
  288.  *    - discards all received data
  289.  *
  290.  * jump to sock_err with contents of *statusptr set to
  291.  *     1 on closed
  292.  *    -1 on timeout
  293.  *
  294.  */
  295. #define sock_wait_established(s, seconds, fn, statusptr) \
  296.     if (ip_delay0(s, seconds, fn, statusptr)) goto sock_err;
  297. #define sock_wait_input(s, seconds, fn , statusptr) \
  298.     if (ip_delay1(s, seconds, fn, statusptr)) goto sock_err;
  299. #define sock_tick(s, statusptr) \
  300.     if (!tcp_tick(s)) { *statusptr = 1 ; goto sock_err; }
  301. #define sock_wait_closed(s, seconds, fn, statusptr)\
  302.     if (ip_delay2(s, seconds, fn, statusptr)) goto sock_err;
  303.  
  304. longword resolve();
  305. int    add_server(int *, int, longword *, longword);
  306. longword inet_addr(byte *);
  307. byte *    sockerr(tcp_Socket *);
  308. byte *    sockstate(tcp_Socket *);
  309. byte *    getdomainname(byte *, int);
  310. byte *    setdomainname(byte *);
  311.  
  312. sock_init(void);
  313. /* s is a pointer to a udp or tcp socket */
  314. int    sock_read(void *, byte FAR *, int);
  315. int    sock_fastread(void *, byte FAR *, int);
  316. int    sock_write(void *, byte FAR *, int);
  317. int    sock_fastwrite(void *, byte FAR *, int);
  318. int    sock_flush(void *);
  319. int    sock_flushnext(void *);
  320. int    sock_puts(void *, byte *);
  321. word     sock_gets(void *, byte *, int);
  322. int    sock_putc(void *, byte);
  323. int    sock_getc(void *);
  324. word    sock_dataready(void *);
  325. int    sock_established(void *);
  326. int    sock_close(void *);
  327. void    sock_abort(void *);
  328. int    sock_mode(void *, word);        /* see TCP_MODE_... */
  329.  
  330. /*
  331.  * TCP or UDP specific material, must be used for open's and listens, but
  332.  * sock calls are used for everything else.
  333.  */
  334. int    udp_open(void *, word, longword, word);
  335. int    tcp_open(void *, word, longword, word);
  336. int    tcp_listen(void *, word, longword, word, word);
  337. int    tcp_established(void *);
  338.  
  339. /* less general functions */
  340. longword resolve(byte *);
  341. int    isaddr(byte *);
  342.  
  343. /* timers */
  344. void    ip_timer_init(void *, int);
  345. int    ip_timer_expired(void *);
  346. int    ip_delay0(void *, int, procref, int *);
  347. int    ip_delay1(void *, int, procref, int *);
  348. int    ip_delay2(void *, int, procref, int *);
  349.  
  350. /* tcp_init/tcp_shutdown, init/kill all tcp and lower services.
  351.    Call if sock_init is not used, else not recommended.
  352. */
  353. int    tcp_init(void);
  354. void    tcp_shutdown(void);
  355. int    tcp_abort(tcp_Socket *);
  356. /* tcp_tick - called periodically by user application in sock_wait.
  357.   returns 0 when our socket closes
  358. */
  359. int    tcp_tick(void *);
  360. /* tcp_set_debug_state - set 1 or reset 0, development tool */
  361. void    tcp_set_debug_state(word);
  362.  
  363. int    tcp_cancel(void *);
  364. int    udp_cancel(void *);
  365.  
  366. int    ping(longword, longword);
  367. longword chk_ping(longword, longword);
  368.  
  369. int    eth_init();
  370. byte *    eth_formatpacket(void *, word);
  371. int    eth_send(word);
  372. void    eth_free(void *);
  373. byte *    eth_arrived(word *);
  374. void    eth_release(void);
  375. void *    eth_hardware(void *);
  376. int    dobootp(void);
  377. void    arp_add_gateway(byte *, longword);
  378. int    do_rarp(void);
  379. int    do_ping(byte *, longword);
  380.  
  381. /* bsd-similar stuff */
  382.  
  383. int    sock_rbsize(void *);
  384. int    sock_rbused(void *);
  385. int    sock_rbleft(void *);
  386. int    sock_tbsize(void *);
  387. int    sock_tbused(void *);
  388. int    sock_tbleft(void *);
  389. getpeername(tcp_Socket *, void *, int *);
  390. getsockname(tcp_Socket *, void *, int *);
  391.  
  392. chk_socket(tcp_Socket *);
  393. byte *    psocket(tcp_Socket *);
  394. byte *    sockerr(tcp_Socket *);
  395. byte *    sockstate(tcp_Socket *);
  396. byte *    getdomainname(byte *, int);
  397. byte *    setdomainname(byte *);
  398. byte *    gethostname(byte *, int);
  399. byte *    sethostname(byte *);
  400. longword gethostid();
  401. longword sethostid(longword);
  402. word     ntohs(word);
  403. word     htons(word);
  404. longword ntohl(longword);
  405. longword htonl(longword);
  406. longword realclock(void);
  407. longword inet_addr(byte *);
  408. void *    movmem(void *, void *, int);
  409.  
  410. void    arp_register(longword, longword);
  411. int    arp_resolve(longword, void *);
  412. void    arp_init(void);
  413. longword arp_rpt_gateway(int);
  414. int    pkt_rarp_init(void);
  415.  
  416. extern    survivebootp;
  417. extern    word pktdevclass;
  418. extern    word mss;
  419. extern    word bootptimeout;
  420. extern    longword bootphost;
  421. extern    word bootpon;
  422. extern    longword my_ip_addr;
  423. extern    eth_address eth_addr;
  424. extern    eth_address eth_brdcast;
  425. extern    longword sin_mask;
  426. extern    word sock_delay;
  427. extern    int last_nameserver;
  428. extern    longword def_nameservers[];
  429. extern    word debug_on;
  430.