How do I find the current machine's full hostname in C (hostname and domain information)?

后端 未结 5 1627
无人共我
无人共我 2020-11-30 23:33

In a C project (POSIX), how do I get the fully qualified name for the current system?

For example, I can get just the hostname of my machine by doing gethostna

相关标签:
5条回答
  • 2020-12-01 00:12

    gethostname() is POSIX way to get local host name. Check out man.

    BSD function getdomainname() can give you domain name so you can build fully qualified hostname. There is no POSIX way to get a domain I'm afraid.

    0 讨论(0)
  • 2020-12-01 00:20

    To get a fully qualified name for a machine, we must first get the local hostname, and then lookup the canonical name.

    The easiest way to do this is by first getting the local hostname using uname() or gethostname() and then performing a lookup with gethostbyname() and looking at the h_name member of the struct it returns. If you are using ANSI c, you must use uname() instead of gethostname().

    Example:

    char hostname[1024];
    hostname[1023] = '\0';
    gethostname(hostname, 1023);
    printf("Hostname: %s\n", hostname);
    struct hostent* h;
    h = gethostbyname(hostname);
    printf("h_name: %s\n", h->h_name);
    

    Unfortunately, gethostbyname() is deprecated in the current POSIX specification, as it doesn't play well with IPv6. A more modern version of this code would use getaddrinfo().

    Example:

    struct addrinfo hints, *info, *p;
    int gai_result;
    
    char hostname[1024];
    hostname[1023] = '\0';
    gethostname(hostname, 1023);
    
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; /*either IPV4 or IPV6*/
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_CANONNAME;
    
    if ((gai_result = getaddrinfo(hostname, "http", &hints, &info)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(gai_result));
        exit(1);
    }
    
    for(p = info; p != NULL; p = p->ai_next) {
        printf("hostname: %s\n", p->ai_canonname);
    }
    
    freeaddrinfo(info);
    

    Of course, this will only work if the machine has a FQDN to give - if not, the result of the getaddrinfo() ends up being the same as the unqualified hostname.

    0 讨论(0)
  • 2020-12-01 00:20

    The easy way, try uname()

    If that does not work, use gethostname() then gethostbyname() and finally gethostbyaddr()

    The h_name of hostent{} should be your FQDN

    0 讨论(0)
  • 2020-12-01 00:22

    My solution:

    #ifdef WIN32
        #include <Windows.h>
        #include <tchar.h>
    #else
        #include <unistd.h>
    #endif
    
    void GetMachineName(char machineName[150])
    {
        char Name[150];
        int i=0;
    
        #ifdef WIN32
            TCHAR infoBuf[150];
            DWORD bufCharCount = 150;
            memset(Name, 0, 150);
            if( GetComputerName( infoBuf, &bufCharCount ) )
            {
                for(i=0; i<150; i++)
                {
                    Name[i] = infoBuf[i];
                }
            }
            else
            {
                strcpy(Name, "Unknown_Host_Name");
            }
        #else
            memset(Name, 0, 150);
            gethostname(Name, 150);
        #endif
        strncpy(machineName,Name, 150);
    }
    
    0 讨论(0)
  • 2020-12-01 00:24

    I believe you are looking for:

    gethostbyaddress

    Just pass it the localhost IP.

    There is also a gethostbyname function, that is also usefull.

    0 讨论(0)
提交回复
热议问题