1 /*
2 * trygetif.c - test program for getif.c
3 */
4
5 #include <sys/types.h>
6 #include <sys/socket.h>
7
8 #if defined(SUNOS) || defined(SVR4)
9 #include <sys/sockio.h>
10 #endif
11
12 #ifdef _AIX32
13 #include <sys/time.h> /* for struct timeval in net/if.h */
14 #endif
15 #include <net/if.h> /* for struct ifreq */
16 #include <netinet/in.h>
17 #include <arpa/inet.h> /* inet_ntoa */
18
19 #include <netdb.h>
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <errno.h>
23
24 #include "getif.h"
25
26 int debug = 0;
27 char *progname;
28
29 void
main(argc,argv)30 main(argc, argv)
31 int argc;
32 char **argv;
33 {
34 struct hostent *hep;
35 struct sockaddr_in *sip; /* Interface address */
36 struct ifreq *ifr;
37 struct in_addr dst_addr;
38 struct in_addr *dap;
39 int s;
40
41 progname = argv[0]; /* for report */
42
43 dap = NULL;
44 if (argc > 1) {
45 dap = &dst_addr;
46 if (isdigit(argv[1][0]))
47 dst_addr.s_addr = inet_addr(argv[1]);
48 else {
49 hep = gethostbyname(argv[1]);
50 if (!hep) {
51 printf("gethostbyname(%s)\n", argv[1]);
52 exit(1);
53 }
54 memcpy(&dst_addr, hep->h_addr, sizeof(dst_addr));
55 }
56 }
57 s = socket(AF_INET, SOCK_DGRAM, 0);
58 if (s < 0) {
59 perror("socket open");
60 exit(1);
61 }
62 ifr = getif(s, dap);
63 if (!ifr) {
64 printf("no interface for address\n");
65 exit(1);
66 }
67 printf("Intf-name:%s\n", ifr->ifr_name);
68 sip = (struct sockaddr_in *) &(ifr->ifr_addr);
69 printf("Intf-addr:%s\n", inet_ntoa(sip->sin_addr));
70
71 exit(0);
72 }
73