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 23 int debug = 0; 24 char *progname; 25 26 main(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 (isdigit(argv[1][0])) 43 dst_addr.s_addr = inet_addr(argv[1]); 44 else { 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