home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / t / tel2305s.zip / ENGINE / NETUTILS.C < prev    next >
C/C++ Source or Header  |  1991-12-15  |  4KB  |  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 "netevent.h"
  13. #include "hostform.h"
  14. #include "externs.h"
  15.  
  16. #include "netutils.h"
  17. /*
  18. extern struct machinfo *gethostinfo(char *hostinfo);
  19. extern int connect_sock(struct *machine, int source_port, int dest_port);
  20. */
  21.  
  22. extern int debug;  /* debug variable */
  23.  
  24. /* gethostinfo() is designed to take either a hostname, or a
  25.    hostip (dotted form), and return a machine structure */
  26.  
  27. struct machinfo *gethostinfo(char *hostinfo)
  28. {
  29.    struct machinfo *mach_info;   /* temp location for the machine info */
  30.    int mach_number;     /* unique machine number in domain lookup */
  31.    int theclass, dat;   /* for use with Sgetevent() */
  32.  
  33.     if(debug)
  34.         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.       } /* end if */
  45.     if(mach_info)
  46.         return (mach_info); /* We found it in cache */
  47.  
  48. /* failing lookup up in local cache, do a domain query */
  49. /* domain queries in NCSA have to be done with events, so, set it up
  50.    and wait for the event to happen (Good or Bad) */
  51.  
  52.    if((mach_number=Sdomain(hostinfo)) < 0) {    /* no nameservers */
  53.       printf("No NameServer(s) defined in network startup file\n");
  54.       return((struct machinfo *)NULL);
  55.    }
  56.  
  57. /* keep looping until the event occurs */ 
  58.     while (!mach_info) {
  59.         switch(Sgetevent(USERCLASS,&theclass,&dat)) {
  60.             case DOMFAIL:
  61.                 return((struct machinfo *)NULL);   /* not found */
  62.             case DOMOK:
  63.                 mach_info=Slooknum(mach_number);    /* got it */
  64.           } /* end switch */
  65.       } /* end while */
  66.  
  67.    if (debug) {
  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. #ifndef NET14
  77. /* connect_sock() is designed to open a connection (~socket) to
  78.    a machine that has been looked up with gethostinfo.
  79. */
  80. int connect_sock(struct machinfo *machine, int source_port, int dest_port)
  81. {
  82.     int connect_id;      /* connection id for netread, netwrite /(return value) */
  83.     int theclass,dat;   /* for Sgetevent */
  84.     int event;
  85.  
  86.    /* set source port as required by NCSA (so it's not Telnet port ?) */
  87.     netfromport(source_port);
  88.     if(debug)
  89.         printf("connect_sock: from_port set to %d\n",source_port);
  90.  
  91. /* Try to connect */
  92.     if((connect_id=Snetopen(machine,dest_port))<0) { /* assuming id's should be greater than 0, like U*x? */
  93.         printf("connect_sock: Snetopen return value %d", connect_id);
  94.         return(-1);
  95.      }
  96.  
  97.     if(debug)
  98.         printf("connect_sock: Snetopen(machine, %d) ok\n",dest_port);
  99.  
  100. /*   while (Sgetevent(CONCLASS, &theclass, &dat) != (CONOK)) */
  101.     while(1) {
  102.         if((event=Sgetevent(CONCLASS, &theclass, &dat))!=0) {
  103.             if(connect_id!=dat) {
  104.             /* put it back, it's not our problem */
  105. /* do I need to do this */
  106. /*            netputevent(theclass, event, dat); */
  107.               }
  108.             else {
  109.                 if(event==CONOPEN) {
  110.                     if(debug)
  111.                         printf("connect_sock: CONOPEN\n");
  112.                     break;   /* how far does break break out? */
  113.                   } /* end if */
  114.                 else {
  115.                     printf("received event [%d]\n",event);
  116.                     return(-1);
  117.                   } /* end else */
  118.               } /* end else */
  119.           } /* end if */
  120.       } /* end while */
  121.  
  122.     if(debug)
  123.         printf("connect_sock: Connection opened [%d]\n",connect_id);
  124.     return(connect_id);  /* send back connection id, that's why we're here */
  125. }
  126. #endif
  127.  
  128.