1 /* ----------------------------------------------------------------------- *
3 * Copyright 2001-2008 H. Peter Anvin - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
16 * Small program to use gethostbyname() to print out a hostname in
17 * hex and/or dotted-quad notation
23 #include <sys/socket.h>
26 #define _GNU_SOURCE /* For getopt_long */
29 const struct option options
[] = {
30 {"hexadecimal", 0, NULL
, 'x'},
31 {"decimal", 0, NULL
, 'd'},
32 {"dotted-quad", 0, NULL
, 'd'},
33 {"full-output", 0, NULL
, 'f'},
34 {"name", 0, NULL
, 'n'},
35 {"help", 0, NULL
, 'h'},
41 void usage(int exit_code
)
43 fprintf(stderr
, "Usage: %s [-dxnf] hostname/ip...\n", program
);
47 int main(int argc
, char *argv
[])
59 while ((opt
= getopt_long(argc
, argv
, "dxfnh", options
, NULL
)) != -1) {
62 output
|= 2; /* Decimal output */
65 output
|= 4; /* Hexadecimal output */
68 output
|= 1; /* Canonical name output */
71 output
= 7; /* Full output */
86 output
= 7; /* Default output */
88 for (i
= optind
; i
< argc
; i
++) {
90 host
= gethostbyname(argv
[i
]);
97 if (host
->h_addrtype
!= AF_INET
|| host
->h_length
!= 4) {
98 fprintf(stderr
, "%s: No IPv4 address associated with name\n",
105 printf("%s%s", sep
, host
->h_name
);
110 printf("%s%u.%u.%u.%u", sep
,
111 ((unsigned char *)host
->h_addr
)[0],
112 ((unsigned char *)host
->h_addr
)[1],
113 ((unsigned char *)host
->h_addr
)[2],
114 ((unsigned char *)host
->h_addr
)[3]);
119 printf("%s%02X%02X%02X%02X", sep
,
120 ((unsigned char *)host
->h_addr
)[0],
121 ((unsigned char *)host
->h_addr
)[1],
122 ((unsigned char *)host
->h_addr
)[2],
123 ((unsigned char *)host
->h_addr
)[3]);