gethostbyaddr example

Dominic Mitchell dom at myrddin.demon.co.uk
Wed Jan 26 23:09:24 GMT 2000


On Wed, Jan 26, 2000 at 03:29:46PM +0000, Gary Brewer wrote:
> Does anyone have an example of gethosybyaddr working in FreeBSD ? I am
> unable to get this function to work and always get 'dereferencing pointer
> for cast integer'.

You may want to have a look at /usr/local/share/doc/21.ipc/paper.ascii.gz,
as it has an example for gethostbyname, which should be similar.

It would be easier to work out where the problem lied if you posted
some source code, along with compiler output.

FWIW, here's a function I have in one of my programs that uses it. This
does a bit more than is needed, though.

------------------------------------------------------------------------
/***********************************************************************
 * getrname: return remote host name.  also fills in addr.
 */
static char *
getrname(int fd, struct sockaddr *addr, int *addrlen)
{
    struct hostent *hp;
    struct sockaddr_in *so = (struct sockaddr_in *)addr;

    getpeername (fd, addr, addrlen);
    hp = gethostbyaddr ((char *)&so->sin_addr.s_addr,
                        sizeof so->sin_addr.s_addr, AF_INET);
    return estrdup (hp ? hp->h_name : inet_ntoa(so->sin_addr));
}
------------------------------------------------------------------------

Actually, you may as well use the code under /usr/src to see what other
programs use it correctly.  Let's have a quick peek... (ideally, I would
use glimpse to index the source, but I haven't gotten around to it yet).

% find /usr/src -type f -name '*.c' | xargs egrep -l 'gethostbyaddr'
/usr/src/contrib/amd/fixmount/fixmount.c
/usr/src/contrib/amd/libamu/wire.c
/usr/src/contrib/bind/bin/irpd/irpd.c
/usr/src/contrib/bind/lib/irs/gethostent.c
/usr/src/contrib/bind/lib/irs/gethostent_r.c
/usr/src/contrib/bind/lib/irs/getnameinfo.c
/usr/src/contrib/bind/lib/irs/irp_ho.c
/usr/src/contrib/bind/tests/irs_testirpd.c
/usr/src/contrib/ipfilter/ipmon.c
/usr/src/contrib/ipfilter/ipsend/arp.c
/usr/src/contrib/isc-dhcp/common/memory.c
/usr/src/contrib/opie/opieftpd.c
/usr/src/contrib/perl5/pp_sys.c
/usr/src/contrib/perl5/toke.c
/usr/src/contrib/sendmail/src/conf.c
/usr/src/contrib/sendmail/src/daemon.c
/usr/src/contrib/tcp_wrappers/socket.c
/usr/src/contrib/tcpdump/addrtoname.c
/usr/src/contrib/traceroute/traceroute.c
/usr/src/lib/libatm/ip_addr.c
/usr/src/lib/libc/net/gethostbydns.c
/usr/src/lib/libc/net/gethostnamadr.c
/usr/src/lib/libc/net/rcmd.c
/usr/src/lib/libskey/skeyaccess.c
/usr/src/lib/libutil/realhostname.c
/usr/src/libexec/ftpd/ftpd.c
/usr/src/libexec/talkd/process.c
/usr/src/release/picobsd/tinyware/simple_httpd/simple_httpd.c
/usr/src/sbin/ipfw/ipfw.c
/usr/src/sbin/mount_nfs/mount_nfs.c
/usr/src/sbin/mountd/mountd.c
/usr/src/sbin/ping/ping.c
/usr/src/sbin/route/route.c
/usr/src/sbin/routed/rtquery/rtquery.c
/usr/src/usr.bin/netstat/inet.c
/usr/src/usr.bin/netstat/route.c
/usr/src/usr.bin/rpcinfo/rpcinfo.c
/usr/src/usr.bin/rup/rup.c
/usr/src/usr.bin/rusers/rusers.c
/usr/src/usr.bin/systat/netcmds.c
/usr/src/usr.bin/systat/netstat.c
/usr/src/usr.bin/telnet/commands.c
/usr/src/usr.bin/w/w.c
/usr/src/usr.bin/ypwhich/ypwhich.c
/usr/src/usr.sbin/arp/arp.c
/usr/src/usr.sbin/bootparamd/bootparamd/bootparamd.c
/usr/src/usr.sbin/bootparamd/callbootd/callbootd.c
/usr/src/usr.sbin/keyadmin/keyadmin.c
/usr/src/usr.sbin/lpr/common_source/recvjob.c
/usr/src/usr.sbin/lpr/lpd/lpd.c
/usr/src/usr.sbin/lpr/lpd/recvjob.c
/usr/src/usr.sbin/mrouted/mapper.c
/usr/src/usr.sbin/mrouted/mrinfo.c
/usr/src/usr.sbin/mrouted/mtrace.c
/usr/src/usr.sbin/rpc.lockd/procs.c
/usr/src/usr.sbin/syslogd/syslogd.c
/usr/src/usr.sbin/xntpd/lib/numtohost.c
/usr/src/crypto/kerberosIV/appl/bsd/iruserok.c
/usr/src/crypto/kerberosIV/lib/kafs/afskrb.c
/usr/src/crypto/kerberosIV/lib/krb/k_getsockinst.c
/usr/src/crypto/kerberosIV/lib/krb/name2name.c
/usr/src/crypto/kerberosIV/lib/roken/inaddr2str.c
/usr/src/crypto/telnet/telnet/commands.c

Looking through that list, /usr/src/lib/libutil/realhostname.c seems to
provide a fairly clear example of it's use.  Not only that, but
realhostname(3) seems to be a useful library function in it's own right.

If you don't have the source installed on your system, try looking at
http://www.freebsd.org/cgi-bin/cvsweb.cgi/src/lib/libutil/realhostname.c
for a copy, as well as a changelog for that file.

By golly, I like having source to my operating system.  Not only can I
find out how it works, I can steal it too!

-Dom





More information about the Ukfreebsd mailing list