home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / fingercl.zip / tcp.c < prev    next >
Text File  |  1997-08-29  |  6KB  |  190 lines

  1. /* tcp.c -- Functions for talking to other machines via TCP services. */
  2.  
  3. /* Copyright (C) 1988, 1990, 1992  Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Finger.
  6.  
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2, or (at your option)
  10.    any later version.
  11.  
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include <stdio.h>
  22. #include <config.h>
  23. #include <stdlib.h>
  24. #include <sys/types.h>
  25. #include <netdb.h>
  26. #include <netinet/in.h>
  27.  
  28. #if !defined (hpux)
  29. #include <arpa/inet.h>
  30. #endif
  31.  
  32. #include <signal.h>
  33.  
  34. #include <tcp.h>
  35. #include <general.h>
  36.  
  37. /* Because Unix is too stupid to make this a define.  This is
  38.    worse than the ptrace stuff.  Value signifies Internet Protocol. */
  39. #define IP 0
  40.  
  41. /* **************************************************************** */
  42. /*                                                                  */
  43. /*                      TCP Stream Functions                        */
  44. /*                                                                  */
  45. /* **************************************************************** */
  46.  
  47. /* How come these don't come in a library somewhere?  Everyone could
  48.    use one.  Like to open a SMTP connection, or talk, or anything. */
  49.  
  50. /* Default number of seconds before timing out on connect call. */
  51. #define TIME_OUT 4
  52.  
  53. /* Number of seconds before timing out on connect call. */
  54. int connection_timeout_counter = TIME_OUT;
  55.  
  56. /* Non-zero means only allow TIME_OUT seconds for a connect () to
  57.    succeed, instead of whatever the infernal network code allows. */
  58. int allow_time_outs = 0;
  59.  
  60. /* Open a filedes to SERVICE at ADDRESS.  If SERVICE is the name of a
  61.    service, then it must exist on the local machine.  SERVICE can also
  62.    be the ASCII representation of a decimal number, in which case it is
  63.    interpreted as the port number to connect to.  Returns a valid file
  64.    descriptor if successful, or -1 if not. */
  65. int
  66. tcp_to_service (service, address)
  67.   char *service;
  68.   char *address;
  69. {
  70.   struct servent *server;
  71.   struct sockaddr_in name;
  72.   int connection;
  73.  
  74.   /* Prepare the socket name for binding. */
  75.   bzero (&name, sizeof (name));
  76.  
  77.   name.sin_family = AF_INET;
  78.   bcopy (address, &name.sin_addr.s_addr, 4);
  79.  
  80.   /* Find the port to use for the requested service. */
  81.   if (strcmp (service, "cfinger") == 0)
  82.     name.sin_port = htons (2003);
  83.   else if (digit (*service))
  84.     name.sin_port = htons (atoi (service));
  85.   else
  86.     {
  87.       server = getservbyname (service, "tcp");
  88.       if (!server)
  89.         return (-1);
  90.       name.sin_port = server->s_port;
  91.     }
  92.  
  93.   /* Make a new socket. */
  94.   connection = socket (PF_INET, SOCK_STREAM, IP);
  95.  
  96.   if (connection < 0)
  97.     return (-1);
  98.  
  99.   /* Connect to the desired port.  We have a shorter timeout than
  100.      the connect call uses by default. */
  101.   {
  102.     int error;
  103.     void connect_timed_out ();
  104.  
  105.     if (allow_time_outs)
  106.       {
  107.         signal (SIGALRM, connect_timed_out);
  108.         alarm (TIME_OUT);
  109.         error = connect (connection, &name, sizeof (name));
  110.         alarm (0);
  111.         signal (SIGALRM, SIG_DFL);
  112.       }
  113.     else
  114.       error = connect (connection, &name, sizeof (name));
  115.  
  116.     if (error < 0)
  117.       {
  118.         close (connection);
  119.         return (-1);
  120.       }
  121.   }
  122.  
  123.  
  124.   return (connection);
  125. }
  126.  
  127. void
  128. connect_timed_out ()
  129. {
  130.   alarm (0);
  131. }
  132.  
  133.  
  134. /* Compare hosts for equality: returns non-zero if HOST1 is the same
  135.    as HOST2.  They are considered the same either if they have
  136.    matching names, or translate to the same IP address. The latter
  137.    requires that the two addresses are comparable, i.e. are of the
  138.    same type and size. The IP address comparison has been added to
  139.    avoid problems where hosts are sometimes specified with a FQDN and
  140.    sometimes by host name alone.  In the case where one host name is a
  141.    FQDN and the other a local network hostname, comparing the host
  142.    name to the host name portion of the FQDN would be boobtytrapped,
  143.    since hosts in different networks often have the same name. This
  144.    would, in fact, be a considerable security hole, since any host
  145.    could pretend to be any other, possibly trusted, host. */
  146.  
  147. #ifndef __EMX__
  148. int
  149. host_cmp (host1, host2)
  150.   char *host1, *host2;
  151. {
  152.   struct hostent *ht1, *ht2;
  153.   int type1, len1, retval;
  154.   char *addr1, ulongbuf[sizeof (unsigned long)];
  155.  
  156.  
  157.   /* Guard */
  158.   if (!host1 || !host2)
  159.     return 0;
  160.  
  161.   /* First just compare names */
  162.   if (!xstricmp (host1, host2))
  163.     return 1;
  164.  
  165.   /* Names didn't match; check if they translate
  166.      to the same IP address. */
  167.  
  168.   if (!(ht1 = gethostbyname (host1)))
  169.     return 0;
  170.  
  171.   if (ht1->h_length <= sizeof (unsigned long))
  172.     addr1 = ulongbuf;
  173.   else
  174.     addr1 = xmalloc (ht1->h_length);
  175.  
  176.   len1 = ht1->h_length;
  177.   type1 = ht1->h_addrtype;
  178.   bcopy (*ht1->h_addr_list, addr1, len1);
  179.  
  180.   retval = ((ht2 = gethostbyname (host2))
  181.             && type1 == ht2->h_addrtype
  182.             && len1 == ht2->h_length
  183.             && !bcmp (addr1, *ht2->h_addr_list, ht1->h_length));
  184.  
  185.   if (addr1 != ulongbuf)
  186.     free (addr1);
  187.  
  188.   return retval;
  189. }
  190. #endif (__EMX__