calloc - Work around gcc-8 bug (2).
[dragonfly.git] / libexec / bootpd / trygetif.c
blob78fd0bc0427f2a305960146163a61910e0cf6517
1 /*
2 * trygetif.c - test program for getif.c
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 */
8 #include <sys/types.h>
9 #include <sys/socket.h>
11 #if defined(SUNOS) || defined(SVR4)
12 #include <sys/sockio.h>
13 #endif
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 */
22 #include <netdb.h>
23 #include <stdio.h>
24 #include <ctype.h>
25 #include <errno.h>
27 #include "getif.h"
29 int debug = 0;
30 char *progname;
32 void
33 main(argc, argv)
34 int argc;
35 char **argv;
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;
44 progname = argv[0]; /* for report */
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);
57 memcpy(&dst_addr, hep->h_addr, sizeof(dst_addr));
60 s = socket(AF_INET, SOCK_DGRAM, 0);
61 if (s < 0) {
62 perror("socket open");
63 exit(1);
65 ifr = getif(s, dap);
66 if (!ifr) {
67 printf("no interface for address\n");
68 exit(1);
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));
74 exit(0);