[comp.sys.sgi] What do I have to do to get gethostbyaddr to work?

pplacewa@bbn.com (Paul W Placeway) (03/30/91)

I'm stumped.  I can't seem to make gethostbyaddr to work right.  The
host is in /etc/hosts (which shouldn't even exist, but that's another
issue), AND it's in the YP version of hosts ("ypmatch <num> hosts.byaddr"
works), AND the DNS knows about	it.  I've tried compiling this tidbit
normally, with -D_BSD_COMPAT and -lbsd, and with -D_BSD_COMPAT, -lsun
-lbsd (to get YP stuff).  None of them work.  It works fine on our
Suns.

This is on a 4d/240 running Irix 3.3.2.

		-- Paul Placeway <pplaceway@bbn.com>

================ cut here ================
#! /bin/sh
# To extract, remove mail header lines and type "sh filename"
echo x - FOO.c
sed -e 's/^X//' > FOO.c << '!FaR!OuT!'
X#include <stdio.h>
X#include	<netdb.h>
X#include	<sys/types.h>
X#include	<sys/socket.h>
X
Xmain(argc, argv)
Xint argc;
Xchar *argv[];
X{
X    struct	hostent	*h;
X    char	*host_addr;
X    long l;
X    
X    if (sscanf(argv[1], "%ld", &l) != 1)
X	exit (1);
X
X    host_addr = (char *) &l;
X    h = gethostbyaddr (host_addr, sizeof (long), 0);
X    if (h == (struct hostent *) 0) {
X#ifdef sun
X	printf("h_errno == %d\n", h_errno);
X#else
X	herror("gethostbyaddr");
X#endif
X    } else {
X	printf("%s\n", h->h_name);
X    }
X
X    exit (0);
X}
X
!FaR!OuT!
exit

arc@kaibab.wpd.sgi.com (Andrew Cherenson) (04/04/91)

In article <63465@bbn.BBN.COM> pplacewa@bbn.com (Paul W Placeway) writes:
>I'm stumped.  I can't seem to make gethostbyaddr to work right.  The
>host is in /etc/hosts (which shouldn't even exist, but that's another
>issue), AND it's in the YP version of hosts ("ypmatch <num> hosts.byaddr"
>works), AND the DNS knows about	it.  I've tried compiling this tidbit
>normally, with -D_BSD_COMPAT and -lbsd, and with -D_BSD_COMPAT, -lsun
>-lbsd (to get YP stuff).  None of them work.  It works fine on our
>Suns.
>
>This is on a 4d/240 running Irix 3.3.2.
>
>		-- Paul Placeway <pplaceway@bbn.com>
>
>================ cut here ================
>#! /bin/sh
># To extract, remove mail header lines and type "sh filename"
>echo x - FOO.c
>sed -e 's/^X//' > FOO.c << '!FaR!OuT!'
>X#include <stdio.h>
>X#include	<netdb.h>
>X#include	<sys/types.h>
>X#include	<sys/socket.h>
>X
>Xmain(argc, argv)
>Xint argc;
>Xchar *argv[];
>X{
>X    struct	hostent	*h;
>X    char	*host_addr;
>X    long l;
>X    
>X    if (sscanf(argv[1], "%ld", &l) != 1)
>X	exit (1);
>X
>X    host_addr = (char *) &l;
>X    h = gethostbyaddr (host_addr, sizeof (long), 0);
>X    if (h == (struct hostent *) 0) {
>X#ifdef sun
>X	printf("h_errno == %d\n", h_errno);
>X#else
>X	herror("gethostbyaddr");
>X#endif
>X    } else {
>X	printf("%s\n", h->h_name);
>X    }
>X
>X    exit (0);
>X}
>X
>!FaR!OuT!
>exit

Change the trailing 0 in the gethostbyaddr call to AF_INET.