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