Home | History | Annotate | Line # | Download | only in common
trygetif.c revision 1.4
      1  1.4    lukem /*	$NetBSD: trygetif.c,v 1.4 1998/03/14 04:39:55 lukem Exp $	*/
      2  1.4    lukem 
      3  1.4    lukem #include <sys/cdefs.h>
      4  1.4    lukem #ifndef lint
      5  1.4    lukem __RCSID("$NetBSD: trygetif.c,v 1.4 1998/03/14 04:39:55 lukem 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 <ctype.h>
     26  1.1      gwr #include <errno.h>
     27  1.1      gwr 
     28  1.1      gwr #include "getif.h"
     29  1.1      gwr 
     30  1.1      gwr int debug = 0;
     31  1.1      gwr char *progname;
     32  1.1      gwr 
     33  1.1      gwr main(argc, argv)
     34  1.1      gwr 	char **argv;
     35  1.1      gwr {
     36  1.1      gwr 	struct hostent *hep;
     37  1.1      gwr 	struct sockaddr ea;			/* Ethernet address */
     38  1.1      gwr 	struct sockaddr_in *sip;	/* Interface address */
     39  1.1      gwr 	struct ifreq *ifr;
     40  1.1      gwr 	struct in_addr dst_addr;
     41  1.1      gwr 	struct in_addr *dap;
     42  1.1      gwr 	int i, s;
     43  1.1      gwr 
     44  1.1      gwr 	progname = argv[0];			/* for report */
     45  1.1      gwr 
     46  1.1      gwr 	dap = NULL;
     47  1.1      gwr 	if (argc > 1) {
     48  1.1      gwr 		dap = &dst_addr;
     49  1.2  mycroft 		if (inet_aton(argv[1], &dst_addr) == 0) {
     50  1.1      gwr 			hep = gethostbyname(argv[1]);
     51  1.1      gwr 			if (!hep) {
     52  1.1      gwr 				printf("gethostbyname(%s)\n", argv[1]);
     53  1.1      gwr 				exit(1);
     54  1.1      gwr 			}
     55  1.1      gwr 			memcpy(&dst_addr, hep->h_addr, sizeof(dst_addr));
     56  1.1      gwr 		}
     57  1.1      gwr 	}
     58  1.1      gwr 	s = socket(AF_INET, SOCK_DGRAM, 0);
     59  1.1      gwr 	if (s < 0) {
     60  1.1      gwr 		perror("socket open");
     61  1.1      gwr 		exit(1);
     62  1.1      gwr 	}
     63  1.1      gwr 	ifr = getif(s, dap);
     64  1.1      gwr 	if (!ifr) {
     65  1.1      gwr 		printf("no interface for address\n");
     66  1.1      gwr 		exit(1);
     67  1.1      gwr 	}
     68  1.1      gwr 	printf("Intf-name:%s\n", ifr->ifr_name);
     69  1.1      gwr 	sip = (struct sockaddr_in *) &(ifr->ifr_addr);
     70  1.1      gwr 	printf("Intf-addr:%s\n", inet_ntoa(sip->sin_addr));
     71  1.1      gwr 
     72  1.1      gwr 	exit(0);
     73  1.1      gwr }
     74