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