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