# Question about gethostname() and IPv6



## tomdkat (May 6, 2006)

So, I've been fighting some code that tries to use getaddrinfo() and getnameinfo() to obtain a host name. These functions are being used to support IPv4 and IPv6. The problem is, these functions tend to want or assume an actual IP address is available and in my case, I won't have one readily available.

I've been scouring the web looking for info and I'm seeing consistent recommendations that gethost*by*name() needs to be replaced with getnameinfo() or getaddrinfo() but I'm not seeing anything about _gethostname_() having any issues with IPv6.

So has anyone here done much or any IPv6 related programming and if so, have you had any problems or issues with using gethostname() to get the hostname of your machine?

Here is a description of gethostname():


> The gethostname() function returns the standard host name for the current machine. The namelen argument specifies the size of the array pointed to by the name argument. The returned name is null-terminated, except that if namelen is an insufficient length to hold the host name, then the returned name is truncated and it is unspecified whether the returned name is null-terminated.


Thanks!

Peace...


----------



## -Fabez- (Jul 28, 2008)

Which programming language are you using ?


----------



## tomdkat (May 6, 2006)

C++ on HP-UX, Windows XP, and z/OS.

Peace...


----------



## -Fabez- (Jul 28, 2008)

Instead of fighting with your code, you could try and obtain the computers name using a registry query.


----------



## tomdkat (May 6, 2006)

I'm already doing that as a "fall back" measure in the event the hostname can't be obtained using gethostname(), for whatever reason. I was mainly wondering if gethostname() is safe to use with IPv6. I'm thinking it is but I'm looking for confirmation. 

Thanks!

Peace...


----------



## -Fabez- (Jul 28, 2008)

Use both methods to obtain a name and if they match then the function works with IPv6, if not then i does not. I would personally stick with using the registry to avoid problems like this. Maybe you could ask the user which method they prefer ?


----------



## tomdkat (May 6, 2006)

-Fabez- said:


> Use both methods to obtain a name and if they match then the function works with IPv6, if not then i does not.


I guess this really gets to the heart of the issue. gethost*by*hame() wants a hostname as an argument and it will return an IP address. This function supports IPv4 only which is why getaddrinfo() and/or getnameinfo() need to be used instead (since they support IPv4 _and_ IPv6). In my case, I don't already have the hostname and I'm trying to _get_ it. The hostname won't be directly related to the version of IP being used so I guess gethostname() would be safe to use with IPv6.



> I would personally stick with using the registry to avoid problems like this. Maybe you could ask the user which method they prefer ?


Fortunately, I'm working with purely internal code so the end user won't know about any of this. They can still specify hostnames and IP addresses as necessary but what I'm working on is purely "behind the scenes" code. As for the registry, since I'm working across the above mentioned platforms there isn't any "registry" I could use across all platforms. It's funny you mention the registry since the main problem I'm having with the code right now is on z/OS. 

Thanks for your replies! 

Peace...


----------

