"Fossies" - the Fresh Open Source Software Archive

Member "HTTPing-2.9/res.c" (29 Oct 2022, 1788 Bytes) of package /linux/www/HTTPing-2.9.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 "res.c" see the Fossies "Dox" file reference documentation.

    1 /* Released under AGPL v3 with exception for the OpenSSL library. See license.txt */
    2 
    3 #include <sys/types.h>
    4 #include <sys/time.h>
    5 #include <sys/socket.h>
    6 #include <netinet/in.h>
    7 #include <libintl.h>
    8 #include <netdb.h>
    9 #include <errno.h>
   10 #include <stdio.h>
   11 #include <string.h>
   12 #include <unistd.h>
   13 
   14 #include "gen.h"
   15 #include "res.h"
   16 #include "error.h"
   17 
   18 int resolve_host(const char *host, struct addrinfo **ai, char use_ipv6, int portnr)
   19 {
   20     int rc = -1;
   21     char servname[10];
   22     struct addrinfo myaddr;
   23 
   24     memset(&myaddr, 0, sizeof myaddr);
   25 
   26     /* myaddr.ai_flags = AI_PASSIVE; */
   27     myaddr.ai_socktype = SOCK_STREAM;
   28     myaddr.ai_protocol = IPPROTO_TCP;
   29     myaddr.ai_family = use_ipv6 ? AF_INET6 : AF_INET;
   30     snprintf(servname, sizeof servname, "%d", portnr);
   31 
   32     rc = getaddrinfo(host, servname, &myaddr, ai);
   33 
   34     if (rc != 0)
   35         set_error(gettext("Resolving %s %sfailed: %s"), host, use_ipv6 ? gettext("(IPv6) ") : "", gai_strerror(rc));
   36 
   37     return rc;
   38 }
   39 
   40 struct addrinfo * select_resolved_host(struct addrinfo *ai, char use_ipv6)
   41 {
   42     struct addrinfo *p = ai;
   43     while(p)
   44     {
   45         if (p -> ai_family == AF_INET6 && use_ipv6)
   46             return p;
   47 
   48         if (p -> ai_family == AF_INET)
   49             return p;
   50 
   51         p = p -> ai_next;
   52     }
   53 
   54     return NULL;
   55 }
   56 
   57 void get_addr(struct addrinfo *ai_use, struct sockaddr_in6 *addr)
   58 {
   59     memcpy(addr, ai_use->ai_addr, ai_use->ai_addrlen);
   60 }
   61 
   62 #define incopy(a)       *((struct in_addr *)a)
   63 
   64 int resolve_host_ipv4(const char *host, struct sockaddr_in *addr)
   65 {
   66     struct hostent *hostdnsentries = gethostbyname(host);
   67 
   68     if (hostdnsentries == NULL)
   69     {
   70         set_error(gettext("Problem resolving %s (IPv4): %s"), host, hstrerror(h_errno));
   71 
   72         return -1;
   73     }
   74 
   75     /* create address structure */
   76     addr -> sin_family = hostdnsentries -> h_addrtype;
   77     addr -> sin_addr = incopy(hostdnsentries -> h_addr_list[0]);
   78 
   79     return 0;
   80 }