home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / NETWORK / TEL23SRC.ZIP / ENGINE / SERVICES.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-22  |  3.0 KB  |  117 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.  */
  35. void inv_port_err(service, port, ip)
  36. int service;        /* 0 - display error string , 1 - set message info     */
  37. int port;            /* should be 0 when service 0 is requested            */
  38. uint8 *ip;            /* should be NULL when service 0 is requested        */
  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.         vprint(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(port)
  59. int port;
  60. {
  61.     char *pname;
  62.  
  63.     if ((pname = find_port(port)) != NULL)
  64.         return(pname);
  65.     
  66.     return ("");   /* just in case we get a NULL (should NEVER happen) */
  67. }
  68.  
  69. static int is_service=1;
  70.  
  71. static char *find_port(port)
  72. int port;
  73. {
  74.     FILE *servfile;
  75.     char line[101];
  76.     static char name[30];
  77.     int found = FALSE;
  78.     int num;
  79.  
  80.     if (is_service == 0)
  81.         return("unknown");
  82.  
  83.     if ((servfile = fopen(service_file(NULL), "r")) != NULL) {
  84.         while((fgets(line, 100, servfile) != NULL) && !found) {
  85.                 sscanf(line,"%s %d/tcp", &name, &num);
  86.             if ((line[0] != '#') && (num == port)) 
  87.                 found = TRUE;
  88.         }
  89.  
  90.         fclose(servfile);
  91.         if (found)
  92.             return(name);
  93.     } else {
  94.         is_service = 0;
  95.     }
  96.  
  97.     return ("unknown");
  98. }
  99.  
  100. /****************************************************************************
  101.  *    char *service_file(char *path)
  102.  *
  103.  *        This routine is used to maintain the path to the services file.
  104.  *        If called with a path == NULL, it will return a (char *) to the
  105.  *        current pathname of the services file.  Otherwise it will set 
  106.  *        the pathname to the services file to path and return a pointer 
  107.  *         to the new name of the services file.
  108.  */
  109.  
  110. char *service_file(path)
  111. char *path;
  112. {
  113.     if (path) 
  114.         services = strdup(path);
  115.     return(services);
  116. }
  117.