"Fossies" - the Fresh Open Source Software Archive

Member "hydra-3.3.2/mpl/src/sock/mpl_host.c" (12 Nov 2019, 2423 Bytes) of package /linux/misc/hydra-3.3.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "mpl_host.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.3.1_vs_3.3.2.

    1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
    2 /*
    3  *  (C) 2001 by Argonne National Laboratory.
    4  *      See COPYRIGHT in top-level directory.
    5  */
    6 
    7 #include "mpl.h"
    8 
    9 #if (!defined MAXHOSTNAMELEN) && (!defined MAX_HOSTNAME_LEN)
   10 #define MAX_HOSTNAME_LEN 256
   11 #elif !defined MAX_HOSTNAME_LEN
   12 #define MAX_HOSTNAME_LEN MAXHOSTNAMELEN
   13 #endif
   14 
   15 #define MAX_LOCAL_HOSTNAMES  (100)
   16 static char lhost[MAX_LOCAL_HOSTNAMES][MAX_HOSTNAME_LEN];
   17 static int lhost_count = 0;
   18 
   19 static void append_lhost(const char *host)
   20 {
   21     int i;
   22 
   23     for (i = 0; i < lhost_count; i++)
   24         if (!strcmp(lhost[i], host))
   25             return;
   26 
   27     MPL_strncpy(lhost[lhost_count], host, MAX_HOSTNAME_LEN);
   28     lhost_count++;
   29 }
   30 
   31 static void init_lhost_list(void)
   32 {
   33     /* if the local host list is already initialized, return */
   34     if (lhost_count)
   35         return;
   36 
   37 #if defined(MPL_HAVE_GETIFADDRS) && defined (MPL_HAVE_INET_NTOP)
   38     char tmp_lhost[MAX_HOSTNAME_LEN];
   39     int ret;
   40     MPL_sockaddr_t addr;
   41     struct ifaddrs *ifaddr, *ifa;
   42     char buf[MAX_HOSTNAME_LEN];
   43 
   44     if (gethostname(tmp_lhost, MAX_HOSTNAME_LEN) < 0) {
   45         /* we can't figure out our local hostname.  *sigh* */
   46         goto common_names;
   47     }
   48     append_lhost(tmp_lhost);
   49 
   50     /* we have our host name; try gethostbyname */
   51     ret = MPL_get_sockaddr(tmp_lhost, &addr);
   52     if (ret == 0) {
   53         /* Find the IP address of the host */
   54         ret = MPL_sockaddr_to_str(&addr, buf, MAX_HOSTNAME_LEN);
   55         if (ret == 0)
   56             append_lhost(buf);
   57     }
   58 
   59     /* try getifaddrs to see if we can get additional IPs */
   60     if (getifaddrs(&ifaddr) == -1)
   61         goto common_names;
   62 
   63     /* Find the IP addresses of all local interfaces */
   64     for (ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
   65         /* FIXME: IPv4 only local addresses */
   66         if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
   67             ret = MPL_sockaddr_to_str((MPL_sockaddr_t *) ifa->ifa_addr, buf, MAX_HOSTNAME_LEN);
   68             if (ret == 0)
   69                 append_lhost(buf);
   70         }
   71     }
   72     freeifaddrs(ifaddr);
   73 #endif
   74 
   75   common_names:
   76     /* list the common localhost names */
   77     append_lhost("localhost");
   78     append_lhost("127.0.0.1");
   79     append_lhost("127.0.1.1");
   80 }
   81 
   82 int MPL_host_is_local(const char *host)
   83 {
   84     int i;
   85 
   86     init_lhost_list();
   87 
   88     for (i = 0; i < lhost_count; i++)
   89         if (!strcmp(lhost[i], host))
   90             return 1;
   91 
   92     return 0;
   93 }