1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
/* ----------------------------------------------------------------------- *
*
* Copyright 2001-2008 H. Peter Anvin - All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, Inc., 53 Temple Place Ste 330,
* Boston MA 02111-1307, USA; either version 2 of the License, or
* (at your option) any later version; incorporated herein by reference.
*
* ----------------------------------------------------------------------- */
/*
* gethostip.c
*
* Small program to use gethostbyname() to print out a hostname in
* hex and/or dotted-quad notation
*/
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sysexits.h>
#define _GNU_SOURCE /* For getopt_long */
#include <getopt.h>
const struct option options[] = {
{"hexadecimal", 0, NULL, 'x'},
{"decimal", 0, NULL, 'd'},
{"dotted-quad", 0, NULL, 'd'},
{"full-output", 0, NULL, 'f'},
{"name", 0, NULL, 'n'},
{"help", 0, NULL, 'h'},
{NULL, 0, NULL, 0}
};
const char *program;
void usage(int exit_code)
{
fprintf(stderr, "Usage: %s [-dxnf] hostname/ip...\n", program);
exit(exit_code);
}
int main(int argc, char *argv[])
{
int opt;
int output = 0;
int i;
char *sep;
int err = 0;
struct hostent *host;
program = argv[0];
while ((opt = getopt_long(argc, argv, "dxfnh", options, NULL)) != -1) {
switch (opt) {
case 'd':
output |= 2; /* Decimal output */
break;
case 'x':
output |= 4; /* Hexadecimal output */
break;
case 'n':
output |= 1; /* Canonical name output */
break;
case 'f':
output = 7; /* Full output */
break;
case 'h':
usage(0);
break;
default:
usage(EX_USAGE);
break;
}
}
if (optind == argc)
usage(EX_USAGE);
if (output == 0)
output = 7; /* Default output */
for (i = optind; i < argc; i++) {
sep = "";
host = gethostbyname(argv[i]);
if (!host) {
herror(argv[i]);
err = 1;
continue;
}
if (host->h_addrtype != AF_INET || host->h_length != 4) {
fprintf(stderr, "%s: No IPv4 address associated with name\n",
argv[i]);
err = 1;
continue;
}
if (output & 1) {
printf("%s%s", sep, host->h_name);
sep = " ";
}
if (output & 2) {
printf("%s%u.%u.%u.%u", sep,
((unsigned char *)host->h_addr)[0],
((unsigned char *)host->h_addr)[1],
((unsigned char *)host->h_addr)[2],
((unsigned char *)host->h_addr)[3]);
sep = " ";
}
if (output & 4) {
printf("%s%02X%02X%02X%02X", sep,
((unsigned char *)host->h_addr)[0],
((unsigned char *)host->h_addr)[1],
((unsigned char *)host->h_addr)[2],
((unsigned char *)host->h_addr)[3]);
sep = " ";
}
putchar('\n');
}
return err;
}
|