Configure check for sys/time.h
[deb_libnfs.git] / examples / nfsclient-bcast.c
CommitLineData
28f7bd66
RS
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 */
00748f36 20#ifdef HAVE_CONFIG_H
9a96dd46 21#include "config.h"
00748f36
RS
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
28f7bd66 33#include <unistd.h>
00748f36
RS
34#endif
35
36#include <stdio.h>
28f7bd66
RS
37#include <stdlib.h>
38#include <string.h>
28f7bd66 39#include <errno.h>
23844203
RS
40
41#ifdef HAVE_SYS_TIME_H
cb46c8fb 42#include <sys/time.h>
23844203 43#endif
bff8fe46
RS
44
45#ifdef HAVE_NET_IF_H
92e787fe 46#include <net/if.h>
bff8fe46
RS
47#endif
48
49#ifdef HAVE_NETDB_H
28f7bd66 50#include <netdb.h>
bff8fe46 51#endif
00748f36
RS
52
53#ifdef HAVE_SYS_IOCTL_H
54#include <sys/ioctl.h>
55#endif
56
7057e733
RS
57#ifdef HAVE_SYS_SOCKET_H
58#include <sys/socket.h>
59#endif
60
763cd6e3 61#include "libnfs-zdr.h"
28f7bd66
RS
62#include "libnfs.h"
63#include "libnfs-raw.h"
763cd6e3 64#include "libnfs-private.h"
28f7bd66
RS
65#include "libnfs-raw-mount.h"
66#include "libnfs-raw-portmap.h"
28f7bd66 67
5e9910f0
RS
68struct nfs_list_data {
69 int status;
70 struct nfs_server_list *srvrs;
71};
72
73void free_nfs_srvr_list(struct nfs_server_list *srv)
74{
75 while (srv != NULL) {
76 struct nfs_server_list *next = srv->next;
77
78 free(srv->addr);
79 free(srv);
80 srv = next;
81 }
82}
83
552c7665 84void pm_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data)
28f7bd66 85{
5e9910f0 86 struct nfs_list_data *srv_data = private_data;
28f7bd66
RS
87 struct sockaddr *sin;
88 char hostdd[16];
5e9910f0 89 struct nfs_server_list *srvr;
28f7bd66 90
cb46c8fb
RS
91 if (status == RPC_STATUS_CANCEL) {
92 return;
93 }
28f7bd66 94 if (status != 0) {
5e9910f0
RS
95 srv_data->status = -1;
96 return;
28f7bd66
RS
97 }
98
99 sin = rpc_get_recv_sockaddr(rpc);
100 if (sin == NULL) {
5e9910f0
RS
101 rpc_set_error(rpc, "failed to get sockaddr in CALLIT callback");
102 srv_data->status = -1;
103 return;
28f7bd66
RS
104 }
105
106 if (getnameinfo(sin, sizeof(struct sockaddr_in), &hostdd[0], sizeof(hostdd), NULL, 0, NI_NUMERICHOST) < 0) {
5e9910f0
RS
107 rpc_set_error(rpc, "getnameinfo failed in CALLIT callback");
108 srv_data->status = -1;
109 return;
110 }
111
112
113 srvr = malloc(sizeof(struct nfs_server_list));
114 if (srvr == NULL) {
115 rpc_set_error(rpc, "Malloc failed when allocating server structure");
116 srv_data->status = -1;
117 return;
28f7bd66
RS
118 }
119
5e9910f0
RS
120 srvr->addr = strdup(hostdd);
121 if (srvr->addr == NULL) {
122 rpc_set_error(rpc, "Strdup failed when allocating server structure");
123 free(srvr);
124 srv_data->status = -1;
125 return;
126 }
127
128 srvr->next = srv_data->srvrs;
129 srv_data->srvrs = srvr;
28f7bd66
RS
130}
131
132int main(int argc _U_, char *argv[] _U_)
133{
134 struct rpc_context *rpc;
135 struct pollfd pfd;
92e787fe 136 struct ifconf ifc;
1be803ce 137 int size;
cb46c8fb 138 struct timeval tv_start, tv_current;
5e9910f0
RS
139 struct nfs_list_data data = {0, NULL};
140 struct nfs_server_list *srvr;
1be803ce 141 char *ptr;
28f7bd66
RS
142
143 rpc = rpc_init_udp_context();
144 if (rpc == NULL) {
145 printf("failed to init context\n");
146 exit(10);
147 }
148
149 if (rpc_bind_udp(rpc, "0.0.0.0", 0) < 0) {
150 printf("failed to bind to udp %s\n", rpc_get_error(rpc));
151 exit(10);
152 }
153
28f7bd66 154
92e787fe
RS
155 /* get list of all interfaces */
156 size = sizeof(struct ifreq);
157 ifc.ifc_buf = NULL;
158 ifc.ifc_len = size;
28f7bd66 159
41a6209b 160 while(ifc.ifc_len > (size - sizeof(struct ifreq))) {
92e787fe
RS
161 size *= 2;
162
163 free(ifc.ifc_buf);
164 ifc.ifc_len = size;
165 ifc.ifc_buf = malloc(size);
1be803ce 166 memset(ifc.ifc_buf, 0, size);
92e787fe
RS
167 if (ioctl(rpc_get_fd(rpc), SIOCGIFCONF, (caddr_t)&ifc) < 0) {
168 printf("ioctl SIOCGIFCONF failed\n");
169 exit(10);
170 }
171 }
172
1be803ce
RS
173 for (ptr =(char *)ifc.ifc_buf; ptr < ((char *)ifc.ifc_buf) + ifc.ifc_len; ) {
174 struct ifreq *ifr;
92e787fe
RS
175 char bcdd[16];
176
1be803ce 177 ifr = (struct ifreq *)ptr;
9a96dd46 178#ifdef HAVE_SOCKADDR_LEN
1be803ce
RS
179 if (ifr->ifr_addr.sa_len > sizeof(struct sockaddr)) {
180 ptr += sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
181 } else {
182 ptr += sizeof(ifr->ifr_name) + sizeof(struct sockaddr);
183 }
184#else
185 ptr += sizeof(struct ifreq);
186#endif
187
188 if (ifr->ifr_addr.sa_family != AF_INET) {
92e787fe
RS
189 continue;
190 }
1be803ce 191 if (ioctl(rpc_get_fd(rpc), SIOCGIFFLAGS, ifr) < 0) {
92e787fe
RS
192 printf("ioctl DRBADDR failed\n");
193 exit(10);
194 }
1be803ce 195 if (!(ifr->ifr_flags & IFF_UP)) {
92e787fe
RS
196 continue;
197 }
1be803ce 198 if (ifr->ifr_flags & IFF_LOOPBACK) {
92e787fe
RS
199 continue;
200 }
1be803ce 201 if (!(ifr->ifr_flags & IFF_BROADCAST)) {
92e787fe
RS
202 continue;
203 }
1be803ce 204 if (ioctl(rpc_get_fd(rpc), SIOCGIFBRDADDR, ifr) < 0) {
1ad6f931 205 continue;
92e787fe 206 }
1be803ce 207 if (getnameinfo(&ifr->ifr_broadaddr, sizeof(struct sockaddr_in), &bcdd[0], sizeof(bcdd), NULL, 0, NI_NUMERICHOST) < 0) {
1ad6f931 208 continue;
92e787fe
RS
209 }
210 if (rpc_set_udp_destination(rpc, bcdd, 111, 1) < 0) {
211 printf("failed to set udp destination %s\n", rpc_get_error(rpc));
212 exit(10);
213 }
214
5e9910f0 215 if (rpc_pmap_callit_async(rpc, MOUNT_PROGRAM, 2, 0, NULL, 0, pm_cb, &data) < 0) {
92e787fe
RS
216 printf("Failed to set up callit function\n");
217 exit(10);
218 }
28f7bd66 219 }
92e787fe
RS
220 free(ifc.ifc_buf);
221
cb46c8fb 222 gettimeofday(&tv_start, NULL);
28f7bd66 223 for(;;) {
cb46c8fb
RS
224 int mpt;
225
28f7bd66
RS
226 pfd.fd = rpc_get_fd(rpc);
227 pfd.events = rpc_which_events(rpc);
228
cb46c8fb
RS
229 gettimeofday(&tv_current, NULL);
230 mpt = 1000
231 - (tv_current.tv_sec *1000 + tv_current.tv_usec / 1000)
232 + (tv_start.tv_sec *1000 + tv_start.tv_usec / 1000);
233
234 if (poll(&pfd, 1, mpt) < 0) {
28f7bd66
RS
235 printf("Poll failed");
236 exit(10);
237 }
cb46c8fb
RS
238 if (pfd.revents == 0) {
239 break;
240 }
241
28f7bd66
RS
242 if (rpc_service(rpc, pfd.revents) < 0) {
243 printf("rpc_service failed with %s\n", rpc_get_error(rpc));
244 break;
245 }
246 }
92e787fe 247
5e9910f0
RS
248 for (srvr=data.srvrs; srvr; srvr = srvr->next) {
249 printf("NFS SERVER @ %s\n", srvr->addr);
250 }
251 free_nfs_srvr_list(data.srvrs);
252
28f7bd66
RS
253 rpc_destroy_context(rpc);
254 rpc=NULL;
28f7bd66
RS
255 return 0;
256}