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
[] =
31 { "hexadecimal", 0, NULL
, 'x' },
32 { "decimal", 0, NULL
, 'd' },
33 { "dotted-quad", 0, NULL
, 'd' },
34 { "full-output", 0, NULL
, 'f' },
35 { "name", 0, NULL
, 'n' },
36 { "help", 0, NULL
, 'h' },
42 void usage(int exit_code
)
44 fprintf(stderr
, "Usage: %s [-dxnf] hostname/ip...\n", program
);
48 int main(int argc
, char *argv
[])
60 while ( (opt
= getopt_long(argc
, argv
, "dxfnh", options
, NULL
)) != -1 ) {
63 output
|= 2; /* Decimal output */
66 output
|= 4; /* Hexadecimal output */
69 output
|= 1; /* Canonical name output */
72 output
= 7; /* Full output */
87 output
= 7; /* Default output */
89 for ( i
= optind
; i
< argc
; i
++ ) {
91 host
= gethostbyname(argv
[i
]);
98 if ( host
->h_addrtype
!= AF_INET
|| host
->h_length
!= 4 ) {
99 fprintf(stderr
, "%s: No IPv4 address associated with name\n", argv
[i
]);
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]);