xref: /netbsd/usr.sbin/bootp/common/trygetif.c (revision bf9ec67e)
1 /*	$NetBSD: trygetif.c,v 1.4 1998/03/14 04:39:55 lukem Exp $	*/
2 
3 #include <sys/cdefs.h>
4 #ifndef lint
5 __RCSID("$NetBSD: trygetif.c,v 1.4 1998/03/14 04:39:55 lukem 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 <ctype.h>
26 #include <errno.h>
27 
28 #include "getif.h"
29 
30 int debug = 0;
31 char *progname;
32 
33 main(argc, argv)
34 	char **argv;
35 {
36 	struct hostent *hep;
37 	struct sockaddr ea;			/* Ethernet address */
38 	struct sockaddr_in *sip;	/* Interface address */
39 	struct ifreq *ifr;
40 	struct in_addr dst_addr;
41 	struct in_addr *dap;
42 	int i, s;
43 
44 	progname = argv[0];			/* for report */
45 
46 	dap = NULL;
47 	if (argc > 1) {
48 		dap = &dst_addr;
49 		if (inet_aton(argv[1], &dst_addr) == 0) {
50 			hep = gethostbyname(argv[1]);
51 			if (!hep) {
52 				printf("gethostbyname(%s)\n", argv[1]);
53 				exit(1);
54 			}
55 			memcpy(&dst_addr, hep->h_addr, sizeof(dst_addr));
56 		}
57 	}
58 	s = socket(AF_INET, SOCK_DGRAM, 0);
59 	if (s < 0) {
60 		perror("socket open");
61 		exit(1);
62 	}
63 	ifr = getif(s, dap);
64 	if (!ifr) {
65 		printf("no interface for address\n");
66 		exit(1);
67 	}
68 	printf("Intf-name:%s\n", ifr->ifr_name);
69 	sip = (struct sockaddr_in *) &(ifr->ifr_addr);
70 	printf("Intf-addr:%s\n", inet_ntoa(sip->sin_addr));
71 
72 	exit(0);
73 }
74