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 / SERVICES.C < prev    next >
C/C++ Source or Header  |  1992-04-12  |  3KB  |  115 lines

  1. /************************************************************************
  2.  *    services.c
  3.  *
  4.  *    part of NCSA Telnet
  5.  *
  6.  *        This file contains routines for figuring what service a tcp port 
  7.  *        normally is used for.  It is used in telnet when a packet for an 
  8.  *        invalid port is received to let the user know what service was 
  9.  *        requested by what machine.
  10.  *
  11.  *        New 6/91    Jeff Wiedemeier
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include "externs.h"
  17.  
  18. static char *get_name(int port);
  19. static char *find_port(int port);
  20. static char *services={"services"};            /* Path to the services file */
  21.  
  22.  
  23. /************************************************************************
  24.  *    void inv_port_err(int service, int port, uint8 *ip)
  25.  *
  26.  *        This function has two purposes - 
  27.  *            0 :        Display an error string based on information that
  28.  *                    was previously set - the port and ip arguments
  29.  *                    should be 0 when this is called in case of future
  30.  *                    use of these arguments for this service
  31.  *
  32.  *            1 :        set the port and source ip number for future service
  33.  *                    0 calls 
  34. int service;         0 - display error string , 1 - set message info
  35. int port;            should be 0 when service 0 is requested
  36. uint8 *ip;           should be NULL when service 0 is requested
  37.  */
  38. void inv_port_err(int service,int port,uint8 *ip)
  39. {
  40.     static int portnum;
  41.     static uint8 sourceip[4];
  42.     char msg[81];
  43.  
  44.     if (!service) {        /* service 0 */
  45.         sprintf(msg, "\t-Destination port %d (%s) - From host %d.%d.%d.%d\r\n",
  46.           portnum, get_name(portnum), sourceip[0], sourceip[1],
  47.           sourceip[2],sourceip[3]);
  48.         tprintf(console->vs, msg);
  49.     } else {            /* service 1 */
  50.         int i;
  51.  
  52.         portnum = port;
  53.         for(i = 0; i < 4; i++)
  54.             sourceip[i] = *(ip + i);
  55.     }
  56. }
  57.  
  58. static char *get_name(int port)
  59. {
  60.     char *pname;
  61.  
  62.     if ((pname = find_port(port)) != NULL)
  63.         return(pname);
  64.     
  65.     return ("");   /* just in case we get a NULL (should NEVER happen) */
  66. }
  67.  
  68. static int is_service=1;
  69.  
  70. static char *find_port(int port)
  71. {
  72.     FILE *servfile;
  73.     char line[101];
  74.     static char name[30];
  75.     int found = FALSE;
  76.     int num;
  77.  
  78.     if (is_service == 0)
  79.         return("unknown");
  80.  
  81.     if ((servfile = fopen(service_file(NULL), "r")) != NULL) {
  82.         while((fgets(line, 100, servfile) != NULL) && !found) {
  83.             sscanf(line,"%s %d/tcp", &name[0], &num);
  84.             if ((line[0] != '#') && (num == port)) 
  85.                 found = TRUE;
  86.         }
  87.  
  88.         fclose(servfile);
  89.         if(found)
  90.             return(name);
  91.     } else {
  92.         is_service = 0;
  93.     }
  94.  
  95.     return ("unknown");
  96. }
  97.  
  98. /****************************************************************************
  99.  *    char *service_file(char *path)
  100.  *
  101.  *        This routine is used to maintain the path to the services file.
  102.  *        If called with a path == NULL, it will return a (char *) to the
  103.  *        current pathname of the services file.  Otherwise it will set 
  104.  *        the pathname to the services file to path and return a pointer 
  105.  *         to the new name of the services file.
  106.  */
  107.  
  108. char *service_file(char *path)
  109. {
  110.     if (path) 
  111.         services = strdup(path);
  112.     return(services);
  113. }
  114.  
  115.