home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / c / ck5a188s.zip / CKONET.C < prev    next >
C/C++ Source or Header  |  1992-11-23  |  6KB  |  275 lines

  1. char *cknetv = "OS/2 Network support, 5A(008) 10 Jul 91";
  2.  
  3. /*  C K O N E T  --  Network support  */
  4. /*
  5.   Copyright (C) 1985, 1992, Trustees of Columbia University in the City of New
  6.   York.  Permission is granted to any individual or institution to use this
  7.   software as long as it is not sold for profit.  This copyright notice must be
  8.   retained.  This software may not be included in commercial products without
  9.   written permission of Columbia University.
  10. */
  11.  
  12. /* Currently supported network services:
  13.  
  14.  - DECnet LAT
  15.  
  16.  */
  17.  
  18. #include "ckcdeb.h"
  19. #include "ckcker.h"
  20. #include "ckcnet.h"
  21.  
  22. #include <io.h>
  23. #include <fcntl.h>
  24. #include <string.h>
  25.  
  26. int ttnproto = NP_NONE;            /* Network virtual terminal protocol */
  27.  
  28. #ifndef NETCONN
  29. /*
  30.   Network support not defined.
  31.   Dummy functions here in case #ifdef's forgotten elsewhere.
  32. */
  33. int                    /* Open network connection */
  34. netopen(name, lcl, nett) char *name; int *lcl, nett; {
  35.     return(-1);
  36. }
  37. int                    /* Close network connection */
  38. netclos() {
  39.     return(-1);
  40. }
  41. int                    /* Check network input buffer */
  42. nettchk() {
  43.     return(-1);
  44. }
  45. int                    /* Flush network input buffer */
  46. netflui() {
  47.     return(-1);
  48. }
  49. int                    /* Send network BREAK */
  50. netbreak() {
  51.     return(-1);
  52. }
  53. int                    /* Input character from network */
  54. netinc(timo) int timo; {
  55. }
  56. int                    /* Output character to network */
  57. #ifdef CK_ANSIC
  58. nettoc(char c)
  59. #else
  60. nettoc(c) char c;
  61. #endif /* CK_ANSIC */
  62. /* nettoc */ {
  63.     return(-1);
  64. }
  65. int
  66. nettol(s,n) char *s; int n; {
  67.     return(-1);
  68. }
  69.  
  70. #else /* NETCONN is defined (rest of this module...) */
  71.  
  72. #ifndef __32BIT__
  73. #define far _far
  74. #define near _near
  75. #define pascal _pascal
  76. #endif
  77. #define    INCL_NOPM
  78. #define    INCL_DOSPROCESS
  79. #define    INCL_DOSMODULEMGR
  80. #include <os2.h>
  81.  
  82. #include "ckolat.h"
  83.  
  84. #ifdef __32BIT__
  85. short (* _Far16 _Pascal LATENTRY)(struct lat_cb *);
  86. #else
  87. int (_far _pascal *LATENTRY)(struct lat_cb *);
  88. #endif
  89.  
  90. extern int duplex, debses, seslog, ttyfd, quiet; /* External variables */
  91. extern int nettype;
  92.  
  93. int tn_init = 0;            /* Telnet protocol initialized flag */
  94. int telnetfd;
  95.  
  96. char ipaddr[20];
  97.  
  98. static struct lat_cb lcb;
  99.  
  100. /*  N E T O P E N  --  Open a network connection.  */
  101. /*  Returns 0 on success, -1 on failure.  */
  102.  
  103. /*
  104.   Calling conventions same as ttopen(), except third argument is network
  105.   type rather than modem type.  Designed to be called from within ttopen.
  106. */
  107.  
  108. int
  109. netopen(name, lcl, nett) char *name; int *lcl, nett; {
  110.  
  111.     char failname[256];
  112.     HMODULE hLatCalls;
  113.  
  114.     if ( LATENTRY == NULL )
  115.     {
  116.       if ( DosLoadModule(failname, sizeof(failname), "LATCALLS", &hLatCalls) )
  117.         return -1;
  118. #ifdef __32BIT__
  119.       if ( DosQueryProcAddr(hLatCalls, 0, "LATENTRY", (PFN *) &LATENTRY) )
  120.         return -1;
  121. #else
  122.       if ( DosGetProcAddr(hLatCalls, "LATENTRY", &LATENTRY) )
  123.         return -1;
  124. #endif
  125.     }
  126.  
  127.     ttnproto = NP_LAT;
  128.  
  129.     printf("Trying connection to %s ... ", name);
  130.  
  131.     lcb.LatFunction = START_SESSION;
  132.     lcb.BufferSize = strlen(name);
  133.     lcb.BufferPtr = (void *) name;
  134.  
  135.     LATENTRY(&lcb);
  136.  
  137.     ttyfd = lcb.SessionHandle;
  138.     ipaddr[0] = 0;
  139.  
  140.     printf(lcb.LatStatus ? "failed.\n" : "OK.\n");
  141.  
  142.     return (lcb.LatStatus == 0) ? 0 : -1;
  143. }
  144.  
  145. /*  N E T C L O S  --  Close current network connection.  */
  146.  
  147. int
  148. netclos() {
  149.     int x = 0;
  150.  
  151.     if (ttyfd < 0)            /* Was open? */
  152.       return(0);            /* Wasn't. */
  153.  
  154.     if (ttyfd > -1)            /* Was. */
  155.     {
  156.       lcb.LatFunction = STOP_SESSION;
  157.       lcb.SessionHandle = ttyfd;
  158.  
  159.       LATENTRY(&lcb);
  160.  
  161.       x = (lcb.LatStatus == 0) ? 0 : -1;
  162.     }
  163.  
  164.     ttyfd = -1;                /* Mark it as closed. */
  165.     ipaddr[0] = 0;
  166.  
  167.     return(x);
  168. }
  169.  
  170. /*  N E T T C H K  --  Check if network up, and how many bytes can be read */
  171. /*
  172.   Returns number of bytes waiting, or -1 if connection has been dropped.
  173. */
  174. int                    /* Check how many bytes are ready */
  175. nettchk() {                /* for reading from network */
  176.     return(0);
  177. }
  178.  
  179. /*  N E T T I N C --  Input character from network */
  180.  
  181. int
  182. netinc(timo) int timo; {
  183.  
  184.     static char buffer[256];
  185.     static int size = 0, pos = 0;
  186.     int chr;
  187.  
  188.     if ( pos < size )
  189.       return buffer[pos++];
  190.  
  191.     lcb.LatFunction = GET_CHAR_BLK;
  192.     lcb.SessionHandle = ttyfd;
  193.     lcb.BufferSize = sizeof(buffer);
  194.     lcb.BufferPtr = (void *) buffer;
  195.     lcb.WaitTime = timo < 0 ? 10L * -timo : 1000L * timo;
  196.  
  197.     LATENTRY(&lcb);
  198.  
  199.     if ( (lcb.SessionStatus & 0xFF) == SS_Stopped )
  200.       return -2;
  201.     if ( lcb.LatStatus )
  202.       return -1;
  203.  
  204.     pos = 0;
  205.     size = lcb.BufferSize;
  206.  
  207.     chr = buffer[pos++];
  208.  
  209.     return chr;
  210. }
  211.  
  212. /*  N E T T O C  --   Output character to network */
  213. /*
  214.   Call with character to be transmitted.
  215.   Returns 0 if transmission was successful, or
  216.   -1 upon i/o error, or -2 if called improperly.
  217. */
  218. int
  219. #ifdef CK_ANSIC
  220. nettoc(char c)
  221. #else
  222. nettoc(c) char c;
  223. #endif /* CK_ANSIC */
  224. /* nettoc */ {
  225.  
  226.     lcb.LatFunction = SEND_CHAR;
  227.     lcb.SessionHandle = ttyfd;
  228.     lcb.CharByte = c;
  229.  
  230.     LATENTRY(&lcb);
  231.  
  232.     return (lcb.LatStatus == 0) ? 0 : -1;
  233. }
  234.  
  235. /*  N E T T O L  --  Output a string of bytes to the network  */
  236. /*
  237.   Call with s = pointer to string, n = length.
  238.   Returns number of bytes actuall written on success, or
  239.   -1 on i/o error, -2 if called improperly.
  240. */
  241. int
  242. nettol(s,n) char *s; int n; {
  243.  
  244.     int b;
  245.  
  246.     for ( b = 0; b < n; b++, s++ )
  247.       if ( nettoc(*s) )
  248.         break;
  249.  
  250.     return(b);
  251. }
  252.  
  253. /*  N E T F L U I  --  Flush network input buffer  */
  254.  
  255. int
  256. netflui() {
  257.     return(0);
  258. }
  259.  
  260. /* Send network BREAK */
  261. /*
  262.   Returns -1 on error, 0 if nothing happens, 1 if BREAK sent successfully.
  263. */
  264. int
  265. netbreak() {
  266.     lcb.LatFunction = SEND_BREAK;
  267.     lcb.SessionHandle = ttyfd;
  268.  
  269.     LATENTRY(&lcb);
  270.  
  271.     return (lcb.LatStatus == 0) ? 0 : -1;
  272. }
  273.  
  274. #endif /* NETCONN */
  275.