'gethostbyname' fails Please Help

C

cpptutor2000

Could some C guru please help ? I am writing a Web server on RH 9.0
box. In the section of the code where the Web server has to be
initiialized, I have:

char host[128];
struct hostent *hp = NULL;
..............................
...............................
if(gethostname(host, sizeof(host)) < 0)
{
/* Print error message and return -1 */
}

if((hp = gethostbyname(host)) == NULL)
{
/* Print another message and return -1 */
}

The second guard is always failing. I have tried 'nslookup' from the
command prompt and it works fine, by returning the name of the DNS
server.
Any hints or suggestions would be greatly appreciated.
 
K

Keith Thompson

Could some C guru please help ? I am writing a Web server on RH 9.0
box. In the section of the code where the Web server has to be
initiialized, I have:

char host[128];
struct hostent *hp = NULL;
.............................
..............................
if(gethostname(host, sizeof(host)) < 0)
[...]

Try comp.unix.programmer.
 
S

soscpd

That fails too?

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

int main(int argc, char *argv[])
{
struct hostent *server;
if (argc < 2)
{
fprintf(stderr,"usage %s hostname\n", argv[0]);
exit(0);
}

server = gethostbyname(argv[1]);

if (server == NULL)
{
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}

/*You can put something here to print positive results, just in
case.*/

return 0;
}

Regards
Rafael
 
K

Kaz Kylheku

Could some C guru please help ? I am writing a Web server on RH 9.0
box. In the section of the code where the Web server has to be
initiialized, I have:

char host[128];
struct hostent *hp = NULL;
.............................
..............................
if(gethostname(host, sizeof(host)) < 0)
{
/* Print error message and return -1 */
}

This just retrieves the machine's own name, as it is known within
your machine. It's largely a useless piece of information.
if((hp = gethostbyname(host)) == NULL)
{
/* Print another message and return -1 */
}

DNS might know nothing about your machine.

If you set up a new Linux box, and call it ``mybox'', do you think
that your DNS servers automatically knows about the name ``mybox''?

The gethostbyname function might resolve it properly through /etc/hosts,
rather than through DNS, if your /etc/nsswitch.conf is set up to look in
/etc/hosts, and if the contents of your /etc/hosts are sane. Otherwise,
all bets are off.

What do you want to achieve by discovering the host's name and address?

What if the host has more than one address?

As a client, to talk to some service on the local machine, you can always use
INADDR_LOOPBACK, without any name resolution.

To discover all of the external-facing IP addresses of your machine,
well, that is more complicated. You have to do something like
enumerate all of the interfaces (in a very system specific way)
and query all of their configured addresses.

But you don't need to do this in a simple Web server. Here is a simpler
solution. Part of an HTTP request is the domain name. So multi-hosting can be
handled entirely within your Web server.

Just bind your socket to INADDR_ANY, so that you catch connection
requests from all attached networks. Then match the domain names
in the requests against supported domains.

You tell your web server (e.g. through a configuration file or whatever) to
serve some pages for "www.foo.com". Clients will request the pages that way,
and you simply have to parse the name out of the request and compare strings;
if a request is for other than "www.foo.com" you reject it with a 404.
As a bonus, you can handle multiple domains at the same time, so you can serve
up a different file for "www.foo.com/index.html" and "www.bar.com/index.html".
The second guard is always failing. I have tried 'nslookup' from the
command prompt and it works fine, by returning the name of the DNS
server.

nslookup (or at least some of its implementations) prints the name of the DNS
server that it's using, whether or not the lookup succeeds or fails. The one I
have here also looks up a successful termination status if the lookup.
 
V

vippstar

<snip off-topic posix reply>

Why not set up follow-ups to comp.unix.programmer instead of here?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top