File: dns.c

package info (click to toggle)
xpa 2.1.18-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,728 kB
  • sloc: ansic: 17,223; sh: 3,705; makefile: 433; tcl: 219; python: 177; perl: 161
file content (48 lines) | stat: -rw-r--r-- 1,068 bytes parent folder | download | duplicates (8)
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
/*
 *	Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
 */

#include <unistd.h>
#include <strings.h>
#include <stdio.h>
#include <netdb.h>              /* gethostbyname() */

#define SZ_LINE 1024

int main(int argc, char **argv)
{
  int i;
  char host[SZ_LINE];
  struct hostent *hostent;

  if( argc > 1 )
    strcpy(host, argv[1]);
  else{
    fprintf(stderr, "calling gethostname() ...\n");
    if( gethostname(host, SZ_LINE) == -1 ){
      perror("gethostname");
      exit(1);
    }
    else{
      fprintf(stderr, "host name is %s\n", host);
    }
  }
  fprintf(stderr, "calling gethostbyname(host) ...\n");
  if( !(hostent = gethostbyname(host)) ){
      perror("gethostbyname");
      exit(1);
  }
  else{
    fprintf(stderr, "gethostbyname() succeeded\n");
  }
  fprintf(stderr, "printing ip address(es) for this host ...\n");
  if( hostent ){
    for(i=0; hostent->h_addr_list[i]; i++){
      fprintf(stderr, "%x\n", *(int *)hostent->h_addr_list[i]);
    }
  }
  else{
    fprintf(stderr, "ERROR: can't look up: %s\n", host);
  }
  return(0);
}