gethostbyname_r returns NULL when hostname has dash


 
Thread Tools Search this Thread
Special Forums IP Networking gethostbyname_r returns NULL when hostname has dash
# 1  
Old 05-15-2008
gethostbyname_r returns NULL when hostname has dash

We have a code to find the DNS entry of a host that has a trailing '-' in its url (format example: mysite-.watch.com):

Code:
if(gethostbyname_r(host,host_ent,host_buffer,host_buffer_size,&host_error)==NULL) 
{
//failed
}

But when remove the '-' from the host name the code does not return failure. IE and nslookup works fine.

What we need to do in such a case?

Last edited by uunniixx; 05-15-2008 at 10:20 PM..
# 2  
Old 05-21-2008
Below are my personal findings:


Example 1:

Code:
> host nyustern-.collegemailer.com
nyustern-.collegemailer.com has address 209.200.118.155

> host nyustern.collegemailer.com
nyustern.collegemailer.com has address 209.200.118.155

Example 2:

Code:
> nslookup nyustern-.collegemailer.com
Note:  nslookup is deprecated and may be removed from future releases.
Consider using the `dig' or `host' programs instead.  Run nslookup with
the `-sil[ent]' option to prevent this message from appearing.
Server:         xx.xxx.xx.xxx
Address:       xx.xxx.xx.xxx#xx
 
Non-authoritative answer:
Name:   nyustern-.collegemailer.com
Address: 209.200.118.155
 
> nslookup nyustern.collegemailer.com
Note:  nslookup is deprecated and may be removed from future releases.
Consider using the `dig' or `host' programs instead.  Run nslookup with
the `-sil[ent]' option to prevent this message from appearing.
Server:         xx.xxx.xx.xxx
Address:       xx.xxx.xx.xxx#xx
 
Non-authoritative answer:
Name:   nyustern.collegemailer.com
Address: 209.200.118.155

Example 3:

This code uses gethostbyname_r method to determine the address:-

Code:
> a.out nyuster-n.collegemailer.com
addresses:   209.200.118.155
> a.out ny-uster-n.collegemailer.com
addresses:   209.200.118.155
> a.out ny-uster-n-.collegemailer.com
unknown host `ny-uster-n-.collegemailer.com'
> a.out ny---uster-n.collegemailer.com
addresses:   209.200.118.155
> a.out nyustern.collegemailer.com
addresses:   209.200.118.155
>

I am unsure whether '-' in hostname ignored or does not has a functionality. In such a case having a trailing '-' is just a corner off case.

Last edited by uunniixx; 05-25-2008 at 06:16 PM..
# 3  
Old 05-21-2008
Looks like that particular domain is running some funky resolver which resolves almost anything. I'd guess their resolver is configured to resolve everything, but has a bug.

Code:
vnix$ host fnordityfnord.collegemailer.com
fnordityfnord.collegemailer.com has address 209.200.118.155
vnix$ host thisabsurdhostnamecouldnotpossiblyexist.collegemailer.com
thisabsurdhostnamecouldnotpossiblyexist.collegemailer.com has address 209.200.118.155
vnix$ host '!*?'.collegemailer.com
!*?.collegemailer.com has address 209.200.118.155

Punycode - Wikipedia, the free encyclopedia has some special cases relating to hyphens in host names.
# 4  
Old 05-21-2008
I am quiet unsure now of what extra we need to code here. Is there any library we need to include? How can we resolve this issue?
# 5  
Old 05-22-2008
What does h_errno say when you get the failure? The man page seems to suggest to use getnameinfo() instead, have you tried that? My version of gethostbyname_r returns an int, do you have a different implementation?
# 6  
Old 05-23-2008
I did a sample code:

Code:

Code:
#include <netdb.h>
#include <stdio.h>
#include <alloca.h>

int
main (void)
{
  struct hostent hostbuf, *hp;
  size_t hstbuflen;
  char *tmphstbuf;
  int res;
  int herr;

  hstbuflen = 1024;
  tmphstbuf = (char *)alloca (hstbuflen);

res = gethostbyname_r ("nyustern-.collegemailer.com", &hostbuf, tmphstbuf, hstbuflen, &hp, &herr);
        herror("Error");
        printf("Hsterror [%s]\n" , hstrerror (h_errno ));

  printf ("gethostbyname_r returned: %d\n", res);
  printf ("herr: %d\n", herr);
  printf ("hp is %s\n", hp == NULL ? "null" : "set");
}

Output:

cc test_host.cpp && a.out

Error: Unknown host
Hsterror [Unknown host]
gethostbyname_r returned: 0
herr: 3
hp is null


The getnameinfo() function takes a socket address as input and returns the corresponding node name and service location.
Can you be more specific regards to my requirement?
# 7  
Old 05-23-2008
This appears to be illegal according to rfc 952.
RFC 952 (rfc952) - DoD Internet host table specification

Code:
<name>  ::= <let>[*[<let-or-digit-or-hyphen>]<let-or-digit>]

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

A dash to GOTO or a dash from GOTO, that is the question...

Well, guys I saw a question about GOTO for Python. So this gave me the inspiration to attempt a GOTO function for 'dash', (bash and ksh too). Machine: MBP OSX 10.14.3, default bash terminal, calling '#!/usr/local/bin/dash'... This is purely a fun project to see if it is possible in PURE... (3 Replies)
Discussion started by: wisecracker
3 Replies

2. UNIX for Advanced & Expert Users

Hostname -f hostname: Unknown host

deleted (0 Replies)
Discussion started by: hce
0 Replies

3. Red Hat

hostname command prints nothing a null value

Hi , On my box everything works fine. But whenever I run command It returns nothing as you see @(none) too. Its very strange issue I have never noticed on any other system yet. Any one have any idea about this. Thank you (2 Replies)
Discussion started by: pratapsingh
2 Replies

4. Emergency UNIX and Linux Support

HP UX - ILO Console hostname different than Machine Hostname...

Hi All, So we added a new HP-UX 11.31 machine. Copied OS via Ignite-UX (DVD)over from this machine called machine_a. It was supposed to be named machine_c. And it is when you log in...however when I'm in the ILO console before logging in, it says: It should say: What gives? And how do... (4 Replies)
Discussion started by: zixzix01
4 Replies

5. Shell Programming and Scripting

Insert string 'NULL' where there is a null value

I have an input file having 7 fields delimited by , eg : 1,ABC,hg,1,2,34,3 2,hj,YU,2,3,4, 3,JU,kl,4,5,7, 4,JK,KJ,3,56,4,5 The seventh field here in some lines is empty, whereas the other lines there is a value. How do I insert string NULL at this location (7th loc) for these lines where... (8 Replies)
Discussion started by: zilch
8 Replies

6. Shell Programming and Scripting

awk returns null?

hi i try to check if awk returns null and i dont know how it's works this is the command set EndByC = `ls -l $base | awk '/.c$/ {print $9}'` if ($EndByC=="") then #check if ther is XXX.c directory echo Sorry ther is no XXX.c folder "in" $base path echo the XXX.c folder is necessary... (6 Replies)
Discussion started by: frenkelor
6 Replies

7. UNIX for Dummies Questions & Answers

echo statement when find returns null

Hi, How do you echo something once when a find statement returns null results? This is when using mutiple locations and mutiple arguments. The below find command the inner loop of a nested for loop where the outter loop holds the $args and the inner loop holds the locations. find... (2 Replies)
Discussion started by: tchoruma
2 Replies

8. Shell Programming and Scripting

remove dash

hi I am using ksh #A="abc-def" #typeset -u B="$A" #echo $B ABC-DEF how to remove the dash? i.e. ABCDEF? thank you (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

9. Programming

PEM_read_RSAPublicKey returns NULL

Hi all, I am trying to write a program in C which will generate private and public keys using openssl RSA and use these for encryption and decryption. I am able to generate the keys successfully and write these to files. I am able to read the private key successfully. I can encrypt and decrypt... (1 Reply)
Discussion started by: Treasa
1 Replies

10. UNIX for Dummies Questions & Answers

Solaris - unknown hostname - how can I change hostname?

Hello, I am new to Solaris. I am using stand alone Solaris 10.0 for test/study purpose and connecting to internet via an ADSL modem which has DHCP server. My Solaris is working on VMWare within winXP. My WinXP and Solaris connects to internet by the same ADSL modem via its DHCP at the same... (1 Reply)
Discussion started by: XNOR
1 Replies
Login or Register to Ask a Question