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