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