Getting the Domain Suffix Selected by DHCP in Linux C

I am trying to get the local domain suffix of my host programmatically in ANSI C (Linux). For example: my machine is ironside.0ffnet.net, I would like to get "0ffnet.net".

I saw several messages accessing this with getnameinfo () and getaddrinfo (), however, it seems that these functions extract information from the / etc / hosts file for any local interface addresses on the computer.

If my machine is assigned an address (and the corresponding domain suffix) via DHCP, the / etc / hosts file is not updated, instead this information is stored in the /etc/resolv.conf file, for example:

dfex@ironside :~/hush$cat /etc/resolv.conf domain 0ffnet.net search 0ffnet.net nameserver 139.130.4.4 

both getnameinfo () and getaddrinfo () simply return the hostname of the machine without a suffix using the information / etc / hosts, which looks like this:

 dfex@ironside :~/hush$ cat /etc/hosts ::1 ironside localhost6.localdomain6 localhost6 127.0.1.1 ironside 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback 

Does anyone know of a function that will retrieve this information without resorting to system () calls? I combed through the Bay management without much success.

+7
source share
3 answers

I will have to understand this question and answer the parts separately. First title assignment.

DHCP Domain

The only way to find out what the DHCP client received from the DHCP server is to read the files left by the client in /var/lib/dhcp . The search domain proposed by DHCP may not be used at all if something else has taken control of resolv.conf .

"my host local domain suffix"

A host can belong to multiple domains or to anyone, which makes this complex concept to define. resolv.conf specifies the domains that will look for hostnames that you resolve; there is no fundamental guarantee that the search will succeed when applied to your own hostname.

Whatever! A recognizer search list is what I really want. How to get it?

Call res_init , and then look at _res.dnsrch and / or _res.defdname . Or analyze resolv.conf yourself; This is a fairly simple and stable format.

Then what is getdomainname () for?

This is for NIS (YP), which you probably don't want.

+2
source

You can try:

 int getdomainname(char *name, size_t len); 

Try running this program:

 #include <unistd.h> #include <stdio.h> int main() { char buf[255]; getdomainname(buf, 255); printf("Domain: %s\n", buf); return 0; } 

EDIT:

No, after many attempts, I suspect you'll need to use syscall and an ugly C-parser (using AWK inside popen would make this code a little shorter).

This code works for me:

 #include <stdio.h> #include <string.h> int main() { char buf[255]; const char reqhostname[255] = "ENTER_YOUR_HOSTNAME_HERE"; FILE *fd; char readbuf[255]; char *pch; int token_counter = 0; memset(buf, 0, 255); strcat(buf, "host "); strcat(buf, reqhostname); fd = popen(buf, "r"); fgets(readbuf, 255, fd); printf("Host returned: %s\n", readbuf); pclose(fd); pch = strtok(readbuf, " "); while (pch != NULL) { strcpy(buf, pch); break; } memset(buf2, 0, 255); pch = strtok(buf, "."); while (pch != NULL) { pch = strtok(NULL, "."); if (pch == NULL) { memset(buf, 0, 255); strncpy(buf, buf2, strlen(buf2) - 1); break; } token_counter++; strcat(buf2, pch); strcat(buf2, "."); } printf("Domain: %s\n", buf); return 0; } 
+1
source

Thanks @ alan-curry for definitely pointing me in the right direction. For anyone struggling with this, res_init is definitely the way to go. Here is a quick way to get the local domain suffix:

 #include <stdio.h> #include <resolv.h> int main (int argc, char *argv[]) { res_init(); printf ("Default domain: %s\n", _res.defdname); return 0; } 
+1
source

All Articles