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