home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / NETWORK / TEL23SRC.ZIP / ENGINE / NETUTILS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-29  |  3.8 KB  |  128 lines

  1. /*
  2.     netutils.c
  3.     Networking utilities for use with NCSA 2.3
  4.  
  5.     By James Nau, College of Engineering,
  6.     University of Nebraska--Lincoln
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. #include "memdebug.h"
  13. #include "netevent.h"
  14. #include "hostform.h"
  15. #include "externs.h"
  16.  
  17. #include "netutils.h"
  18. /*
  19. extern struct machinfo *gethostinfo(char *hostinfo);
  20. extern int connect_sock(struct *machine, int source_port, int dest_port);
  21. */
  22.  
  23. extern int debug;  /* debug variable */
  24.  
  25. /* gethostinfo() is designed to take either a hostname, or a
  26.    hostip (dotted form), and return a machine structure */
  27.  
  28. struct machinfo *gethostinfo(char *hostinfo)
  29. {
  30.    struct machinfo *mach_info;   /* temp location for the machine info */
  31.    int mach_number;     /* unique machine number in domain lookup */
  32.    int theclass, dat;   /* for use with Sgetevent() */
  33.  
  34.    if (debug) printf("gethostinfo: gethostinfo(%s)\n", hostinfo);
  35.  
  36. /* first, try to look up the name in the predefined host cache */
  37.    mach_info = Sgethost(hostinfo);
  38.  
  39.    if (debug)
  40.       if (mach_info)
  41.          printf("gethostinfo: Sgethost(%s) was successful\n",hostinfo);
  42.       else
  43.          printf("gethostinfo: Sgethost(%s) was NOT sucessful\n",hostinfo);
  44.    if (mach_info)  return (mach_info); /* We found it in cache */
  45.  
  46. /* failing lookup up in local cache, do a domain query */
  47. /* domain queries in NCSA have to be done with events, so, set it up
  48.    and wait for the event to happen (Good or Bad) */
  49.  
  50.    if ((mach_number = Sdomain(hostinfo)) < 0)    /* no nameservers */
  51.    {
  52.       printf("No NameServer(s) defined in network startup file\n");
  53.       return ((struct machinfo *)NULL);
  54.    }
  55.  
  56. /* keep looping until the event occurs */ 
  57.   while (!mach_info)
  58.    {
  59.       switch(Sgetevent(USERCLASS,&theclass,&dat))
  60.       {
  61.          case DOMFAIL: return ((struct machinfo *)NULL);   /* not found */
  62.          case DOMOK: mach_info = Slooknum(mach_number);    /* got it */
  63.       }
  64.    }
  65.  
  66.    if (debug)
  67.    {
  68.       printf("gethostinfo: domain lookup successful\n");
  69.       printf("gethostinfo: theclass [%d], dat [%d]\n", theclass, dat);
  70.    }
  71.  
  72.    return (mach_info);  /* send the structure back */
  73. }
  74.  
  75.  
  76. int connect_sock(struct machinfo *machine, int source_port, int dest_port)
  77. /* connect_sock() is designed to open a connection (~socket) to
  78.    a machine that has been looked up with gethostinfo.
  79. */
  80. {
  81.    int connect_id; /* connection id for netread, netwrite /(return value) */
  82.    int theclass, dat;   /* for Sgetevent */
  83.    int event;
  84.    /* set source port as required by NCSA (so it's not Telnet port ?) */
  85.    netfromport(source_port);
  86.    if (debug) printf("connect_sock: from_port set to %d\n",source_port);
  87.  
  88. /* Try to connect */
  89.    if ( (connect_id = Snetopen(machine, dest_port)) < 0)
  90.    {
  91.       /* assuming id's should be greater than 0, like U*x? */
  92.       printf("connect_sock: Snetopen return value %d", connect_id);
  93.       return (-1);
  94.    }
  95.  
  96.    if (debug) printf("connect_sock: Snetopen(machine, %d) ok\n",dest_port);
  97.  
  98. /*   while (Sgetevent(CONCLASS, &theclass, &dat) != (CONOK)) */
  99.    while (1)
  100.    {
  101.       if ((event = Sgetevent(CONCLASS, &theclass, &dat)) != 0)
  102.       {
  103.          if (connect_id != dat)
  104.          {
  105.             /* put it back, it's not our problem */
  106. /* do I need to do this */
  107. /*              netputevent(theclass, event, dat); */
  108.          }
  109.          else
  110.          {
  111.             if (event == CONOPEN)
  112.             {
  113.                if (debug) printf("connect_sock: CONOPEN\n");
  114.                break;   /* how far does break break out? */
  115.             }
  116.             else
  117.             {
  118.                printf("received event [%d]\n",event);
  119.                return (-1);
  120.             }
  121.          }
  122.       }
  123.    }
  124.  
  125.    if (debug) printf("connect_sock: Connection opened [%d]\n",connect_id);
  126.    return(connect_id);  /* send back connection id, that's why we're here */
  127. }
  128.