trygetif.c revision 272461
198184Sgordon/*
298184Sgordon * trygetif.c - test program for getif.c
398184Sgordon *
498184Sgordon * $FreeBSD: releng/10.1/libexec/bootpd/trygetif.c 50476 1999-08-28 00:22:10Z peter $
598184Sgordon */
698184Sgordon
7153298Sdougb#include <sys/types.h>
8136803Smtm#include <sys/socket.h>
9136224Smtm
1098184Sgordon#if defined(SUNOS) || defined(SVR4)
1198184Sgordon#include <sys/sockio.h>
1298184Sgordon#endif
1398184Sgordon
14119166Smtm#ifdef _AIX32
15114769Sdougb#include <sys/time.h>	/* for struct timeval in net/if.h */
1698184Sgordon#endif
17119166Smtm#include <net/if.h>				/* for struct ifreq */
18119166Smtm#include <netinet/in.h>
19231194Sdougb#include <arpa/inet.h>			/* inet_ntoa */
20231194Sdougb
21119166Smtm#include <netdb.h>
22127222Sgreen#include <stdio.h>
23127222Sgreen#include <ctype.h>
24127222Sgreen#include <errno.h>
25127222Sgreen
26127222Sgreen#include "getif.h"
27127222Sgreen
28127222Sgreenint debug = 0;
29127222Sgreenchar *progname;
30127222Sgreen
31127222Sgreenvoid
32127222Sgreenmain(argc, argv)
33127222Sgreen	int argc;
34127222Sgreen	char **argv;
35127222Sgreen{
36127222Sgreen	struct hostent *hep;
37119166Smtm	struct sockaddr_in *sip;	/* Interface address */
38119166Smtm	struct ifreq *ifr;
39119166Smtm	struct in_addr dst_addr;
40119166Smtm	struct in_addr *dap;
41114769Sdougb	int s;
42114769Sdougb
43114769Sdougb	progname = argv[0];			/* for report */
44114769Sdougb
45154637Smatteo	dap = NULL;
46114769Sdougb	if (argc > 1) {
47154637Smatteo		dap = &dst_addr;
48202880Skib		if (isdigit(argv[1][0]))
49154637Smatteo			dst_addr.s_addr = inet_addr(argv[1]);
50154637Smatteo		else {
51154637Smatteo			hep = gethostbyname(argv[1]);
52114769Sdougb			if (!hep) {
53154637Smatteo				printf("gethostbyname(%s)\n", argv[1]);
54154637Smatteo				exit(1);
55154637Smatteo			}
56154637Smatteo			memcpy(&dst_addr, hep->h_addr, sizeof(dst_addr));
57154637Smatteo		}
58114769Sdougb	}
59154637Smatteo	s = socket(AF_INET, SOCK_DGRAM, 0);
60154637Smatteo	if (s < 0) {
61154637Smatteo		perror("socket open");
62154637Smatteo		exit(1);
63154637Smatteo	}
64114769Sdougb	ifr = getif(s, dap);
65114769Sdougb	if (!ifr) {
66114769Sdougb		printf("no interface for address\n");
67114769Sdougb		exit(1);
68114769Sdougb	}
69114769Sdougb	printf("Intf-name:%s\n", ifr->ifr_name);
7098184Sgordon	sip = (struct sockaddr_in *) &(ifr->ifr_addr);
71114769Sdougb	printf("Intf-addr:%s\n", inet_ntoa(sip->sin_addr));
72
73	exit(0);
74}
75