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