xref: /dragonfly/libexec/bootpd/trygetif.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1 /*
2  * trygetif.c - test program for getif.c
3  *
4  * $FreeBSD: src/libexec/bootpd/trygetif.c,v 1.5 1999/08/28 00:09:20 peter Exp $
5  * $DragonFly: src/libexec/bootpd/trygetif.c,v 1.2 2003/06/17 04:27:07 dillon Exp $
6  */
7 
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 
11 #if defined(SUNOS) || defined(SVR4)
12 #include <sys/sockio.h>
13 #endif
14 
15 #ifdef _AIX32
16 #include <sys/time.h>         /* for struct timeval in net/if.h */
17 #endif
18 #include <net/if.h>                               /* for struct ifreq */
19 #include <netinet/in.h>
20 #include <arpa/inet.h>                            /* inet_ntoa */
21 
22 #include <netdb.h>
23 #include <stdio.h>
24 #include <ctype.h>
25 #include <errno.h>
26 
27 #include "getif.h"
28 
29 int debug = 0;
30 char *progname;
31 
32 void
main(argc,argv)33 main(argc, argv)
34           int argc;
35           char **argv;
36 {
37           struct hostent *hep;
38           struct sockaddr_in *sip;      /* Interface address */
39           struct ifreq *ifr;
40           struct in_addr dst_addr;
41           struct in_addr *dap;
42           int s;
43 
44           progname = argv[0];                     /* for report */
45 
46           dap = NULL;
47           if (argc > 1) {
48                     dap = &dst_addr;
49                     if (isdigit(argv[1][0]))
50                               dst_addr.s_addr = inet_addr(argv[1]);
51                     else {
52                               hep = gethostbyname(argv[1]);
53                               if (!hep) {
54                                         printf("gethostbyname(%s)\n", argv[1]);
55                                         exit(1);
56                               }
57                               memcpy(&dst_addr, hep->h_addr, sizeof(dst_addr));
58                     }
59           }
60           s = socket(AF_INET, SOCK_DGRAM, 0);
61           if (s < 0) {
62                     perror("socket open");
63                     exit(1);
64           }
65           ifr = getif(s, dap);
66           if (!ifr) {
67                     printf("no interface for address\n");
68                     exit(1);
69           }
70           printf("Intf-name:%s\n", ifr->ifr_name);
71           sip = (struct sockaddr_in *) &(ifr->ifr_addr);
72           printf("Intf-addr:%s\n", inet_ntoa(sip->sin_addr));
73 
74           exit(0);
75 }
76