diff options
Diffstat (limited to 'auxiliary/name-addr-test')
-rw-r--r-- | auxiliary/name-addr-test/getaddrinfo.c | 64 | ||||
-rw-r--r-- | auxiliary/name-addr-test/gethostbyaddr.c | 46 | ||||
-rw-r--r-- | auxiliary/name-addr-test/gethostbyname.c | 44 | ||||
-rw-r--r-- | auxiliary/name-addr-test/getnameinfo.c | 79 |
4 files changed, 233 insertions, 0 deletions
diff --git a/auxiliary/name-addr-test/getaddrinfo.c b/auxiliary/name-addr-test/getaddrinfo.c new file mode 100644 index 0000000..275bf59 --- /dev/null +++ b/auxiliary/name-addr-test/getaddrinfo.c @@ -0,0 +1,64 @@ + /* + * getaddrinfo(3) (name->address lookup) tester. + * + * Compile with: + * + * cc -o getaddrinfo getaddrinfo.c (BSD, Linux) + * + * cc -o getaddrinfo getaddrinfo.c -lsocket -lnsl (SunOS 5.x) + * + * Run as: getaddrinfo hostname + * + * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands. + * + * Author: Wietse Venema, IBM T.J. Watson Research, USA. + */ +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <netdb.h> +#include <stdio.h> +#include <unistd.h> +#include <string.h> +#include <stdlib.h> + +int main(int argc, char **argv) +{ + char hostbuf[NI_MAXHOST]; /* XXX */ + struct addrinfo hints; + struct addrinfo *res0; + struct addrinfo *res; + const char *addr; + int err; + +#define NO_SERVICE ((char *) 0) + + if (argc != 2) { + fprintf(stderr, "usage: %s hostname\n", argv[0]); + exit(1); + } + memset((char *) &hints, 0, sizeof(hints)); + hints.ai_family = PF_UNSPEC; + hints.ai_flags = AI_CANONNAME; + hints.ai_socktype = SOCK_STREAM; + if ((err = getaddrinfo(argv[1], NO_SERVICE, &hints, &res0)) != 0) { + fprintf(stderr, "host %s not found: %s\n", argv[1], gai_strerror(err)); + exit(1); + } + printf("Hostname:\t%s\n", res0->ai_canonname); + printf("Addresses:\t"); + for (res = res0; res != 0; res = res->ai_next) { + addr = (res->ai_family == AF_INET ? + (char *) &((struct sockaddr_in *) res->ai_addr)->sin_addr : + (char *) &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr); + if (inet_ntop(res->ai_family, addr, hostbuf, sizeof(hostbuf)) == 0) { + perror("inet_ntop:"); + exit(1); + } + printf("%s ", hostbuf); + } + printf("\n"); + freeaddrinfo(res0); + exit(0); +} diff --git a/auxiliary/name-addr-test/gethostbyaddr.c b/auxiliary/name-addr-test/gethostbyaddr.c new file mode 100644 index 0000000..e58db9e --- /dev/null +++ b/auxiliary/name-addr-test/gethostbyaddr.c @@ -0,0 +1,46 @@ + /* + * gethostbyaddr tester. compile with: + * + * cc -o gethostbyaddr gethostbyaddr.c (SunOS 4.x) + * + * cc -o gethostbyaddr gethostbyaddr.c -lnsl (SunOS 5.x) + * + * run as: gethostbyaddr address + * + * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands. + */ + +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <netdb.h> +#include <stdio.h> + +main(argc, argv) +int argc; +char **argv; +{ + struct hostent *hp; + long addr; + + if (argc != 2) { + fprintf(stderr, "usage: %s i.p.addres\n", argv[0]); + exit(1); + } + addr = inet_addr(argv[1]); + if (hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET)) { + printf("Hostname:\t%s\n", hp->h_name); + printf("Aliases:\t"); + while (hp->h_aliases[0]) + printf("%s ", *hp->h_aliases++); + printf("\n"); + printf("Addresses:\t"); + while (hp->h_addr_list[0]) + printf("%s ", inet_ntoa(*(struct in_addr *) * hp->h_addr_list++)); + printf("\n"); + exit(0); + } + fprintf(stderr, "host %s not found\n", argv[1]); + exit(1); +} diff --git a/auxiliary/name-addr-test/gethostbyname.c b/auxiliary/name-addr-test/gethostbyname.c new file mode 100644 index 0000000..d8079dd --- /dev/null +++ b/auxiliary/name-addr-test/gethostbyname.c @@ -0,0 +1,44 @@ + /* + * gethostbyname tester. compile with: + * + * cc -o gethostbyname gethostbyname.c (SunOS 4.x) + * + * cc -o gethostbyname gethostbyname.c -lnsl (SunOS 5.x) + * + * run as: gethostbyname hostname + * + * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands. + */ +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <netdb.h> +#include <stdio.h> + +main(argc, argv) +int argc; +char **argv; +{ + struct hostent *hp; + + if (argc != 2) { + fprintf(stderr, "usage: %s hostname\n", argv[0]); + exit(1); + } + if (hp = gethostbyname(argv[1])) { + printf("Hostname:\t%s\n", hp->h_name); + printf("Aliases:\t"); + while (hp->h_aliases[0]) + printf("%s ", *hp->h_aliases++); + printf("\n"); + printf("Addresses:\t"); + while (hp->h_addr_list[0]) + printf("%s ", inet_ntoa(*(struct in_addr *) * hp->h_addr_list++)); + printf("\n"); + exit(0); + } else { + fprintf(stderr, "host %s not found\n", argv[1]); + exit(1); + } +} diff --git a/auxiliary/name-addr-test/getnameinfo.c b/auxiliary/name-addr-test/getnameinfo.c new file mode 100644 index 0000000..a270a06 --- /dev/null +++ b/auxiliary/name-addr-test/getnameinfo.c @@ -0,0 +1,79 @@ + /* + * getnameinfo(3) (address->name lookup) tester. + * + * Compile with: + * + * cc -o getnameinfo getnameinfo.c (BSD, Linux) + * + * cc -o getnameinfo getnameinfo.c -lsocket -lnsl (SunOS 5.x) + * + * Run as: getnameinfo address + * + * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands. + * + * Author: Wietse Venema, IBM T.J. Watson Research, USA. + */ +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <netdb.h> +#include <stdio.h> +#include <unistd.h> +#include <string.h> +#include <stdlib.h> + +int main(int argc, char **argv) +{ + char hostbuf[NI_MAXHOST]; /* XXX */ + struct addrinfo hints; + struct addrinfo *res0; + struct addrinfo *res; + const char *host; + const char *addr; + int err; + +#define NO_SERVICE ((char *) 0) + + if (argc != 2) { + fprintf(stderr, "usage: %s ipaddres\n", argv[0]); + exit(1); + } + + /* + * Convert address to internal form. + */ + host = argv[1]; + memset((char *) &hints, 0, sizeof(hints)); + hints.ai_family = (strchr(host, ':') ? AF_INET6 : AF_INET); + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags |= AI_NUMERICHOST; + if ((err = getaddrinfo(host, NO_SERVICE, &hints, &res0)) != 0) { + fprintf(stderr, "getaddrinfo %s: %s\n", host, gai_strerror(err)); + exit(1); + } + + /* + * Convert host address to name. + */ + for (res = res0; res != 0; res = res->ai_next) { + err = getnameinfo(res->ai_addr, res->ai_addrlen, + hostbuf, sizeof(hostbuf), + NO_SERVICE, 0, NI_NAMEREQD); + if (err) { + fprintf(stderr, "getnameinfo %s: %s\n", host, gai_strerror(err)); + exit(1); + } + printf("Hostname:\t%s\n", hostbuf); + addr = (res->ai_family == AF_INET ? + (char *) &((struct sockaddr_in *) res->ai_addr)->sin_addr : + (char *) &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr); + if (inet_ntop(res->ai_family, addr, hostbuf, sizeof(hostbuf)) == 0) { + perror("inet_ntop:"); + exit(1); + } + printf("Address:\t%s\n", hostbuf); + } + freeaddrinfo(res0); + exit(0); +} |