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