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