13229Spst/*
23229Spst * trygetif.c - test program for getif.c
318471Swosch *
450476Speter * $FreeBSD: releng/10.3/libexec/bootpd/trygetif.c 50476 1999-08-28 00:22:10Z peter $
53229Spst */
63229Spst
73229Spst#include <sys/types.h>
83229Spst#include <sys/socket.h>
93229Spst
103229Spst#if defined(SUNOS) || defined(SVR4)
113229Spst#include <sys/sockio.h>
123229Spst#endif
133229Spst
1413572Spst#ifdef _AIX32
1513572Spst#include <sys/time.h>	/* for struct timeval in net/if.h */
1613572Spst#endif
173229Spst#include <net/if.h>				/* for struct ifreq */
183229Spst#include <netinet/in.h>
193229Spst#include <arpa/inet.h>			/* inet_ntoa */
203229Spst
213229Spst#include <netdb.h>
223229Spst#include <stdio.h>
233229Spst#include <ctype.h>
243229Spst#include <errno.h>
253229Spst
263229Spst#include "getif.h"
273229Spst
283229Spstint debug = 0;
293229Spstchar *progname;
303229Spst
3113572Spstvoid
323229Spstmain(argc, argv)
3313572Spst	int argc;
343229Spst	char **argv;
353229Spst{
363229Spst	struct hostent *hep;
373229Spst	struct sockaddr_in *sip;	/* Interface address */
383229Spst	struct ifreq *ifr;
393229Spst	struct in_addr dst_addr;
403229Spst	struct in_addr *dap;
4113572Spst	int s;
423229Spst
433229Spst	progname = argv[0];			/* for report */
443229Spst
453229Spst	dap = NULL;
463229Spst	if (argc > 1) {
473229Spst		dap = &dst_addr;
4813572Spst		if (isdigit(argv[1][0]))
4913572Spst			dst_addr.s_addr = inet_addr(argv[1]);
5013572Spst		else {
513229Spst			hep = gethostbyname(argv[1]);
523229Spst			if (!hep) {
533229Spst				printf("gethostbyname(%s)\n", argv[1]);
543229Spst				exit(1);
553229Spst			}
563229Spst			memcpy(&dst_addr, hep->h_addr, sizeof(dst_addr));
573229Spst		}
583229Spst	}
593229Spst	s = socket(AF_INET, SOCK_DGRAM, 0);
603229Spst	if (s < 0) {
613229Spst		perror("socket open");
623229Spst		exit(1);
633229Spst	}
643229Spst	ifr = getif(s, dap);
653229Spst	if (!ifr) {
663229Spst		printf("no interface for address\n");
673229Spst		exit(1);
683229Spst	}
693229Spst	printf("Intf-name:%s\n", ifr->ifr_name);
703229Spst	sip = (struct sockaddr_in *) &(ifr->ifr_addr);
713229Spst	printf("Intf-addr:%s\n", inet_ntoa(sip->sin_addr));
723229Spst
733229Spst	exit(0);
743229Spst}
75