Some more configure checks for headers
[deb_libnfs.git] / examples / nfsclient-bcast.c
1 /*
2 Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2011
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /* Example program using the lowlevel raw broadcast interface.
19 */
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #ifdef WIN32
25 #include "win32_compat.h"
26 #endif
27
28 #ifdef HAVE_POLL_H
29 #include <poll.h>
30 #endif
31
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <sys/time.h>
41
42 #ifdef HAVE_NET_IF_H
43 #include <net/if.h>
44 #endif
45
46 #ifdef HAVE_NETDB_H
47 #include <netdb.h>
48 #endif
49
50 #ifdef HAVE_SYS_IOCTL_H
51 #include <sys/ioctl.h>
52 #endif
53
54 #ifdef HAVE_SYS_SOCKET_H
55 #include <sys/socket.h>
56 #endif
57
58 #include "libnfs-zdr.h"
59 #include "libnfs.h"
60 #include "libnfs-raw.h"
61 #include "libnfs-private.h"
62 #include "libnfs-raw-mount.h"
63 #include "libnfs-raw-portmap.h"
64
65 struct nfs_list_data {
66 int status;
67 struct nfs_server_list *srvrs;
68 };
69
70 void free_nfs_srvr_list(struct nfs_server_list *srv)
71 {
72 while (srv != NULL) {
73 struct nfs_server_list *next = srv->next;
74
75 free(srv->addr);
76 free(srv);
77 srv = next;
78 }
79 }
80
81 void pm_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data)
82 {
83 struct nfs_list_data *srv_data = private_data;
84 struct sockaddr *sin;
85 char hostdd[16];
86 struct nfs_server_list *srvr;
87
88 if (status == RPC_STATUS_CANCEL) {
89 return;
90 }
91 if (status != 0) {
92 srv_data->status = -1;
93 return;
94 }
95
96 sin = rpc_get_recv_sockaddr(rpc);
97 if (sin == NULL) {
98 rpc_set_error(rpc, "failed to get sockaddr in CALLIT callback");
99 srv_data->status = -1;
100 return;
101 }
102
103 if (getnameinfo(sin, sizeof(struct sockaddr_in), &hostdd[0], sizeof(hostdd), NULL, 0, NI_NUMERICHOST) < 0) {
104 rpc_set_error(rpc, "getnameinfo failed in CALLIT callback");
105 srv_data->status = -1;
106 return;
107 }
108
109
110 srvr = malloc(sizeof(struct nfs_server_list));
111 if (srvr == NULL) {
112 rpc_set_error(rpc, "Malloc failed when allocating server structure");
113 srv_data->status = -1;
114 return;
115 }
116
117 srvr->addr = strdup(hostdd);
118 if (srvr->addr == NULL) {
119 rpc_set_error(rpc, "Strdup failed when allocating server structure");
120 free(srvr);
121 srv_data->status = -1;
122 return;
123 }
124
125 srvr->next = srv_data->srvrs;
126 srv_data->srvrs = srvr;
127 }
128
129 int main(int argc _U_, char *argv[] _U_)
130 {
131 struct rpc_context *rpc;
132 struct pollfd pfd;
133 struct ifconf ifc;
134 int size;
135 struct timeval tv_start, tv_current;
136 struct nfs_list_data data = {0, NULL};
137 struct nfs_server_list *srvr;
138 char *ptr;
139
140 rpc = rpc_init_udp_context();
141 if (rpc == NULL) {
142 printf("failed to init context\n");
143 exit(10);
144 }
145
146 if (rpc_bind_udp(rpc, "0.0.0.0", 0) < 0) {
147 printf("failed to bind to udp %s\n", rpc_get_error(rpc));
148 exit(10);
149 }
150
151
152 /* get list of all interfaces */
153 size = sizeof(struct ifreq);
154 ifc.ifc_buf = NULL;
155 ifc.ifc_len = size;
156
157 while(ifc.ifc_len > (size - sizeof(struct ifreq))) {
158 size *= 2;
159
160 free(ifc.ifc_buf);
161 ifc.ifc_len = size;
162 ifc.ifc_buf = malloc(size);
163 memset(ifc.ifc_buf, 0, size);
164 if (ioctl(rpc_get_fd(rpc), SIOCGIFCONF, (caddr_t)&ifc) < 0) {
165 printf("ioctl SIOCGIFCONF failed\n");
166 exit(10);
167 }
168 }
169
170 for (ptr =(char *)ifc.ifc_buf; ptr < ((char *)ifc.ifc_buf) + ifc.ifc_len; ) {
171 struct ifreq *ifr;
172 char bcdd[16];
173
174 ifr = (struct ifreq *)ptr;
175 #ifdef HAVE_SOCKADDR_LEN
176 if (ifr->ifr_addr.sa_len > sizeof(struct sockaddr)) {
177 ptr += sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
178 } else {
179 ptr += sizeof(ifr->ifr_name) + sizeof(struct sockaddr);
180 }
181 #else
182 ptr += sizeof(struct ifreq);
183 #endif
184
185 if (ifr->ifr_addr.sa_family != AF_INET) {
186 continue;
187 }
188 if (ioctl(rpc_get_fd(rpc), SIOCGIFFLAGS, ifr) < 0) {
189 printf("ioctl DRBADDR failed\n");
190 exit(10);
191 }
192 if (!(ifr->ifr_flags & IFF_UP)) {
193 continue;
194 }
195 if (ifr->ifr_flags & IFF_LOOPBACK) {
196 continue;
197 }
198 if (!(ifr->ifr_flags & IFF_BROADCAST)) {
199 continue;
200 }
201 if (ioctl(rpc_get_fd(rpc), SIOCGIFBRDADDR, ifr) < 0) {
202 continue;
203 }
204 if (getnameinfo(&ifr->ifr_broadaddr, sizeof(struct sockaddr_in), &bcdd[0], sizeof(bcdd), NULL, 0, NI_NUMERICHOST) < 0) {
205 continue;
206 }
207 if (rpc_set_udp_destination(rpc, bcdd, 111, 1) < 0) {
208 printf("failed to set udp destination %s\n", rpc_get_error(rpc));
209 exit(10);
210 }
211
212 if (rpc_pmap_callit_async(rpc, MOUNT_PROGRAM, 2, 0, NULL, 0, pm_cb, &data) < 0) {
213 printf("Failed to set up callit function\n");
214 exit(10);
215 }
216 }
217 free(ifc.ifc_buf);
218
219 gettimeofday(&tv_start, NULL);
220 for(;;) {
221 int mpt;
222
223 pfd.fd = rpc_get_fd(rpc);
224 pfd.events = rpc_which_events(rpc);
225
226 gettimeofday(&tv_current, NULL);
227 mpt = 1000
228 - (tv_current.tv_sec *1000 + tv_current.tv_usec / 1000)
229 + (tv_start.tv_sec *1000 + tv_start.tv_usec / 1000);
230
231 if (poll(&pfd, 1, mpt) < 0) {
232 printf("Poll failed");
233 exit(10);
234 }
235 if (pfd.revents == 0) {
236 break;
237 }
238
239 if (rpc_service(rpc, pfd.revents) < 0) {
240 printf("rpc_service failed with %s\n", rpc_get_error(rpc));
241 break;
242 }
243 }
244
245 for (srvr=data.srvrs; srvr; srvr = srvr->next) {
246 printf("NFS SERVER @ %s\n", srvr->addr);
247 }
248 free_nfs_srvr_list(data.srvrs);
249
250 rpc_destroy_context(rpc);
251 rpc=NULL;
252 return 0;
253 }