• Antelope Release 5.9 Mac OS X 10.13.2 2019-05-01

 

NAME

ip2name, name2ip - convert hostname to ip address, or ip to hostname

SYNOPSIS

#include "stock.h"

int ip2name(int addr, char *name)
int name2ip(char *name, struct in_addr *addr, char *ipc)

DESCRIPTION

ip2name takes an integer ip address and converts it to some ascii string. It first checks for 127.0.0.1 (localhost), then uses gethostbyaddr(3). If gethostbyaddr fails, it converts the ip address to a string representing the ip address.

name2ip performs the inverse operation, calling gethostbyname(3) to find the ip address corresponding to a hostname. It returns both the ip address in addr, and the ascii representation of the inet address in ipc.

Both routines are thread safe because they employ mutexes to ensure that calls to the thread-unsafe gethostbyaddr and gethostbyname are serial.

EXAMPLE



if ( ip2name ( aclient->address, hostname) ) {
    complain ( 0, "Can't find address '%s'", hostname ) ;
}


if ( name2ip ( server, &serv_addr.sin_addr.s_addr, ipc ) ) {
    register_error ( 1, "Can't find host '%s'\n", server ) ;
    return -1 ;
}

RETURN VALUES

ip2name and name2ip return 0 for success, -1 if the name or ip address could not be looked up.

LIBRARY

$(STOCKLIBS)

SEE ALSO

gethostbyaddr(3)
gethostbyname(3)

BUGS AND CAVEATS

Both ip2name and name2ip expect the ip address to be in network byte order, not (necessarily) the natural byte order of the computer. I.e., on arm or x86 machines, the ip address is in reversed byte order.

AUTHOR

Daniel Quinlan
Printer icon