Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-04

Portable (or at least working) version of GetDomainName()?

17 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mark Morgan Lloyd

How best to get the (internet-style) domain name of a system? GetDomainName() appears to be returning "(none)" here on x86 Linux.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
waldo kitty
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote
How best to get the (internet-style) domain name of a system? GetDomainName() appears to be returning "(none)" here on x86 Linux.

do you mean the FQDN (Fully Qualified Domain Name)??

Mark Morgan Lloyd
to waldo kitty
waldo kitty wrote 6 lines
On 4/8/2013 11:03, Mark Morgan Lloyd wrote: > How best to get the (internet-style) domain name of a system? > GetDomainName() > appears to be returning "(none)" here on x86 Linux. do you mean the FQDN (Fully Qualified Domain Name)??

I mean that on the machine I'm working on the hostname is pye-dev-01 and the domain name is telemetry.co.uk. I was under the impression that GetDomainName() should return the latter, but apart from anything else it's unix-only and whilst Windows is not somewhere I want to go today I'd like a portable solution if possible.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
waldo kitty
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 12 lines
waldo kitty wrote: > On 4/8/2013 11:03, Mark Morgan Lloyd wrote: >> How best to get the (internet-style) domain name of a system? GetDomainName() >> appears to be returning "(none)" here on x86 Linux. > > do you mean the FQDN (Fully Qualified Domain Name)?? I mean that on the machine I'm working on the hostname is pye-dev-01 and the domain name is telemetry.co.uk. I was under the impression that GetDomainName() should return the latter, but apart from anything else it's unix-only and whilst Windows is not somewhere I want to go today I'd like a portable solution if possible.

ok... you are looking for the FQDN, then... the question is how is this machine referenced and its FQDN created and maintained... it would seem that you would have a DNS server that would return this information... it sounds like your machine only has its host name defined and there is nothing else DNS-wise that returns the rest of the domain name...

from the machine you are trying to perform this look up from, what do you get if you do the following?

nslookup pye-dev-01

nslookup pye-dev-01.telemetry.co.uk

using the last one, i get the message that that machine doesn't exist... of course, i'm on the outside and access to this machine may not be allowed from outside... not even finding its address...

i don't see this as linux or windows centric... what i'm seeing seems to point to a DNS configuration situation... you might be able to handle this with a local entry in your HOSTS file...

[crystalball] get nslookup working first. your code should work. [/crystalball]

Mark Morgan Lloyd
to waldo kitty
waldo kitty wrote 30 lines
On 4/8/2013 14:54, Mark Morgan Lloyd wrote: > waldo kitty wrote: >> On 4/8/2013 11:03, Mark Morgan Lloyd wrote: >>> How best to get the (internet-style) domain name of a system? >>> GetDomainName() >>> appears to be returning "(none)" here on x86 Linux. >> >> do you mean the FQDN (Fully Qualified Domain Name)?? > > I mean that on the machine I'm working on the hostname is pye-dev-01 > and the > domain name is telemetry.co.uk. I was under the impression that > GetDomainName() > should return the latter, but apart from anything else it's unix-only > and whilst > Windows is not somewhere I want to go today I'd like a portable > solution if > possible. ok... you are looking for the FQDN, then... the question is how is this machine referenced and its FQDN created and maintained... it would seem that you would have a DNS server that would return this information... it sounds like your machine only has its host name defined and there is nothing else DNS-wise that returns the rest of the domain name... from the machine you are trying to perform this look up from, what do you get if you do the following? nslookup pye-dev-01

Server: 192.168.1.1
Address: 192.168.1.1#53

Name: pye-dev-01.telemetry.co.uk
Address: 192.168.1.16

nslookup pye-dev-01.telemetry.co.uk

Server: 192.168.1.1
Address: 192.168.1.1#53

Name: pye-dev-01.telemetry.co.uk
Address: 192.168.1.16

using the last one, i get the message that that machine doesn't exist... of course, i'm on the outside and access to this machine may not be allowed from outside... not even finding its address...

As you can see, that's an internal (RFC 1918 IIRC) address.

Show quoted text 6 lines
i don't see this as linux or windows centric... what i'm seeing seems to point to a DNS configuration situation... you might be able to handle this with a local entry in your HOSTS file... [crystalball] get nslookup working first. your code should work. [/crystalball]

OK, that's no big deal. But it looks like GetDomainName() doesn't quite do what it says on the can :-)

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
waldo kitty
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 20 lines
waldo kitty wrote: > from the machine you are trying to perform this look up from, what do you get > if you do the following? > > > nslookup pye-dev-01 Server: 192.168.1.1 Address: 192.168.1.1#53 Name: pye-dev-01.telemetry.co.uk Address: 192.168.1.16 > nslookup pye-dev-01.telemetry.co.uk Server: 192.168.1.1 Address: 192.168.1.1#53 Name: pye-dev-01.telemetry.co.uk Address: 192.168.1.16

i like those outputs :)

Show quoted text 5 lines
> using the last one, i get the message that that machine doesn't exist... of > course, i'm on the outside and access to this machine may not be allowed from > outside... not even finding its address... As you can see, that's an internal (RFC 1918 IIRC) address.

yup... i kinda figured that was the case...

Show quoted text 8 lines
> i don't see this as linux or windows centric... what i'm seeing seems to point > to a DNS configuration situation... you might be able to handle this with a > local entry in your HOSTS file... > > [crystalball] get nslookup working first. your code should work. [/crystalball] OK, that's no big deal. But it looks like GetDomainName() doesn't quite do what it says on the can :-)

agreed... i was just looking around on my FPC 2.6.0 install and note the following from rtl/unix/unix.pp

Function  GetDomainName:String; deprecated; // because linux only.

diggin in, i note that it seems to use the system's uname function... but i think that is different than the command line "uname" or "uname -a" because none of my linux machine return their FQDN in this output... i note that it also seems to be pulling this function from a/the libc library...

i note that both, GetDomainName and GetHostName both use the same var (Sysn : utsname) but just different fields in what is apparently a record of some type...

can't trace further... the grill and a pork loin are calling me... not to mention other mouths in the location wanting some eats soon-ish...

Mark Morgan Lloyd
to waldo kitty
waldo kitty wrote 11 lines
>> [crystalball] get nslookup working first. your code should work. >> [/crystalball] > > OK, that's no big deal. But it looks like GetDomainName() doesn't > quite do what > it says on the can :-) agreed... i was just looking around on my FPC 2.6.0 install and note the following from rtl/unix/unix.pp Function GetDomainName:String; deprecated; // because linux only.

I saw that as well, and wouldn't have raised it if I'd not been working on x86 Linux. It might turn out to depend on the distro: I use Debian.

Show quoted text 12 lines
diggin in, i note that it seems to use the system's uname function... but i think that is different than the command line "uname" or "uname -a" because none of my linux machine return their FQDN in this output... i note that it also seems to be pulling this function from a/the libc library... i note that both, GetDomainName and GetHostName both use the same var (Sysn : utsname) but just different fields in what is apparently a record of some type... can't trace further... the grill and a pork loin are calling me... not to mention other mouths in the location wanting some eats soon-ish...

It might be notable that Debian doesn't volunteer a domain name unless it's able to contact DNS. I'll get onto nslookup, or just use temporary text (it's only salt for a password hash, and is stored).

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Tomas Hajny
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote
waldo kitty wrote:

.
.

Show quoted text 16 lines
> diggin in, i note that it seems to use the system's uname function... > but i think that is different than the command line "uname" or "uname > -a" because none of my linux machine return their FQDN in this output... > i note that it also seems to be pulling this function from a/the libc > library... > > i note that both, GetDomainName and GetHostName both use the same var > (Sysn : utsname) but just different fields in what is apparently a > record of some type... > > can't trace further... the grill and a pork loin are calling me... not > to mention other mouths in the location wanting some eats soon-ish... It might be notable that Debian doesn't volunteer a domain name unless it's able to contact DNS. I'll get onto nslookup, or just use temporary text (it's only salt for a password hash, and is stored).

Have you tried using unit netdb from package fcl-net? It doesn't support Windows (and some other platforms yet), but it should work for Unix targets. I believe that the two approaches for finding out the domain are either getting it from the DNS or having it specified in /etc/resolv (and both are used by unit netdb - search for DefaultDomainList).

Tomas

Mark Morgan Lloyd
to Tomas Hajny
Tomas Hajny wrote 25 lines
On Tue, April 9, 2013 10:14, Mark Morgan Lloyd wrote: > waldo kitty wrote: . . >> diggin in, i note that it seems to use the system's uname function... >> but i think that is different than the command line "uname" or "uname >> -a" because none of my linux machine return their FQDN in this output... >> i note that it also seems to be pulling this function from a/the libc >> library... >> >> i note that both, GetDomainName and GetHostName both use the same var >> (Sysn : utsname) but just different fields in what is apparently a >> record of some type... >> >> can't trace further... the grill and a pork loin are calling me... not >> to mention other mouths in the location wanting some eats soon-ish... > It might be notable that Debian doesn't volunteer a domain name unless > it's able to contact DNS. I'll get onto nslookup, or just use temporary > text (it's only salt for a password hash, and is stored). Have you tried using unit netdb from package fcl-net? It doesn't support Windows (and some other platforms yet), but it should work for Unix targets. I believe that the two approaches for finding out the domain are either getting it from the DNS or having it specified in /etc/resolv (and both are used by unit netdb - search for DefaultDomainList).

Thanks for the additional pointer, will investigate.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Ludo Brands
to Mark Morgan Lloyd: I saw that as well, and wouldn't have raised it if I'd not been working on x86 Linux. It…
Mark Morgan Lloyd wrote 5 lines
It might be notable that Debian doesn't volunteer a domain name unless it's able to contact DNS. I'll get onto nslookup, or just use temporary text (it's only salt for a password hash, and is stored).

One of the problems with uname is that the kernel doesn't have a clue on how you are connected to the network. Your computer could have 10 network names if it had 10 addresses (not even 10 NIC's required).

Looking up the IP you want the domain name for in a DNS is the only reliable way. And it is portable.

Ludo

Mark Morgan Lloyd
to Ludo Brands
Ludo Brands wrote 13 lines
On 04/09/2013 10:14 AM, Mark Morgan Lloyd wrote: > It might be notable that Debian doesn't volunteer a domain name unless > it's able to contact DNS. I'll get onto nslookup, or just use temporary > text (it's only salt for a password hash, and is stored). > One of the problems with uname is that the kernel doesn't have a clue on how you are connected to the network. Your computer could have 10 network names if it had 10 addresses (not even 10 NIC's required). Looking up the IP you want the domain name for in a DNS is the only reliable way. And it is portable.

Thanks Ludo, I'd got there. I think there's still the possibility of ambiguity if there are multiple DNS servers listed in resolv.conf, but I agree that it's probably the best way available.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
waldo kitty
to Mark Morgan Lloyd: I saw that as well, and wouldn't have raised it if I'd not been working on x86 Linux. It…
Mark Morgan Lloyd wrote
It might be notable that Debian doesn't volunteer a domain name unless it's able to contact DNS. I'll get onto nslookup, or just use temporary text (it's only salt for a password hash, and is stored).

understood... your nslookup output looked fine to me... it is kinda of different in what mine returns but that's likely the DNS package and the configuration differences... (see the link below for an example of my results)

i would attempt to go portable by performing a DNS lookup on the address and or the given host name... i don't think i would try to use external programs for this but would compare my results with theirs for the same lookups...

FWIW: i also just stumbled over our very similar conversation of a year ago...

13 April 2012 - Resolving a local hostnames to an IP address

http://free-pascal-general.1045716.n5.nabble.com/Resolving-a-local-hostnames-to-an-IP-address-td5637709.html

(sorry for the wrap)

but this time, you are trying to go the other way i guess... that or just trying to get all of the FQDN for the host your app is running on...

waldo kitty
to Tomas Hajny: . . Have you tried using unit netdb from package fcl-net? It doesn't support Windows (and…
Tomas Hajny wrote 6 lines
On Tue, April 9, 2013 10:14, Mark Morgan Lloyd wrote: > waldo kitty wrote: >> i note that both, GetDomainName and GetHostName both use the same var >> (Sysn : utsname) but just different fields in what is apparently a >> record of some type... >>

[TRIM]

Show quoted text 10 lines
> > It might be notable that Debian doesn't volunteer a domain name unless > it's able to contact DNS. I'll get onto nslookup, or just use temporary > text (it's only salt for a password hash, and is stored). Have you tried using unit netdb from package fcl-net? It doesn't support Windows (and some other platforms yet), but it should work for Unix targets. I believe that the two approaches for finding out the domain are either getting it from the DNS or having it specified in /etc/resolv (and both are used by unit netdb - search for DefaultDomainList).

shouldn't the OS' lookup routines handle whether the value from HOSTS or DNS is used automatically? i know that on some systems, one can specify to use HOSTS first (ie: USE_HOSTS_FIRST=1 [OS2] or /etc/host.conf [*nix])... i've seen it in winwhatever once upon a long time ago...

or am i misunderstanding this aspect and the apps used to perform DNS lookups are reacting to this setting and using one value or the other depending on the results returned from the initial query?

waldo kitty
to Mark Morgan Lloyd: Thanks Ludo, I'd got there. I think there's still the possibility of ambiguity if there…
Mark Morgan Lloyd wrote 18 lines
Ludo Brands wrote: > On 04/09/2013 10:14 AM, Mark Morgan Lloyd wrote: > >> It might be notable that Debian doesn't volunteer a domain name unless >> it's able to contact DNS. I'll get onto nslookup, or just use temporary >> text (it's only salt for a password hash, and is stored). >> > > One of the problems with uname is that the kernel doesn't have a clue on > how you are connected to the network. Your computer could have 10 > network names if it had 10 addresses (not even 10 NIC's required). > > Looking up the IP you want the domain name for in a DNS is the only > reliable way. And it is portable. Thanks Ludo, I'd got there. I think there's still the possibility of ambiguity if there are multiple DNS servers listed in resolv.conf, but I agree that it's probably the best way available.

the first DNS server is the one used... if it doesn't respond in X amount of time, then it may be retried Y times before the next DNS server listed in resolv is tried... then you loop through the time period and retries again for each until you get NXDOMAIN or a positive result or have exhausted the list of DNS servers in resolv... at that point, the app may return an error code about not being able to reach a DNS server...

in some cases, the non-responding DNS server(s) may be blocked out from further usage... this may be for some time period or it may require a restart of the local DNS server app (DNS Proxy on some systems) or reboot of the system... i have at least one system that acts this way (locking out unresponsive DNS server entries and requiring a restart of the DNS proxy app)...

my understanding is that this is pretty much all handled by the OS' library code for performing the lookups...

Tomas Hajny
to waldo kitty: [TRIM] shouldn't the OS' lookup routines handle whether the value from HOSTS or DNS is…
waldo kitty wrote
On 4/9/2013 04:24, Tomas Hajny wrote:

.
.

Show quoted text 15 lines
> Have you tried using unit netdb from package fcl-net? It doesn't support > Windows (and some other platforms yet), but it should work for Unix > targets. I believe that the two approaches for finding out the domain > are > either getting it from the DNS or having it specified in /etc/resolv > (and > both are used by unit netdb - search for DefaultDomainList). shouldn't the OS' lookup routines handle whether the value from HOSTS or DNS is used automatically? i know that on some systems, one can specify to use HOSTS first (ie: USE_HOSTS_FIRST=1 [OS2] or /etc/host.conf [*nix])... i've seen it in winwhatever once upon a long time ago...

That depends on what API provided by the respective OS. As far as I understand it correctly, Unix does not provide such functionality in the basic API (syscalls) but only e.g. in libc (which is in turn used by nslookup, etc.), so Pascal code (for Unix) not using libc needs to implement this logic (that's what unit netdb does).

Tomas

waldo kitty
to Tomas Hajny
Tomas Hajny wrote 20 lines
On Tue, April 9, 2013 19:51, waldo kitty wrote: > On 4/9/2013 04:24, Tomas Hajny wrote: . . >> Have you tried using unit netdb from package fcl-net? It doesn't support >> Windows (and some other platforms yet), but it should work for Unix >> targets. I believe that the two approaches for finding out the domain >> are either getting it from the DNS or having it specified in /etc/resolv >> (and both are used by unit netdb - search for DefaultDomainList). > > shouldn't the OS' lookup routines handle whether the value from HOSTS or > DNS is used automatically? i know that on some systems, one can specify > to use HOSTS first (ie: USE_HOSTS_FIRST=1 [OS2] or /etc/host.conf > [*nix])... i've seen it in winwhatever once upon a long time ago... That depends on what API provided by the respective OS. As far as I understand it correctly, Unix does not provide such functionality in the basic API (syscalls) but only e.g. in libc (which is in turn used by nslookup, etc.), so Pascal code (for Unix) not using libc needs to implement this logic (that's what unit netdb does).

ahhhhh... ok... i hadn't yet had a chance to go looking in that unit... trying to first complete the email wading for the day ;)

FWIW: if i were to just be hunting and digging, the name of the unit would throw me off because it seems to indicate network database functions whereas i wouldn't think that DNS lookups and the like are database oriented... not like SQL stuff... but i guess the DNS is a form of database, isn't it ;)

Mark Morgan Lloyd
to waldo kitty: understood... your nslookup output looked fine to me... it is kinda of different in what…
waldo kitty wrote
i would attempt to go portable by performing a DNS lookup on the address and or the given host name... i don't think i would try to use external programs for this but would compare my results with theirs for the same lookups...

Agreed. I rarely use external programs since there's too much risk they're misplaced or of an unexpected version

Show quoted text 6 lines
FWIW: i also just stumbled over our very similar conversation of a year ago... 13 April 2012 - Resolving a local hostnames to an IP address http://free-pascal-general.1045716.n5.nabble.com/Resolving-a-local-hostnames-to-an-IP-address-td5637709.html

So did I. As a quick hack I've parsed /etc/resolv.conf (which covers at least the Linux and Solaris cases), falling back to GetDomainName() if that fails. That will do for the moment, since it will normally be overridden by a .ini file or by something explicitly entered by the user.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]