e73cfaff98d92fdb2d257f8e2ed4aa904385e886
[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
21 #include "config.h"
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>
29 #include <sys/ioctl.h>
30 #include <sys/time.h>
31 #include <net/if.h>
32 #include <netdb.h>
33 #include "libnfs-zdr.h"
34 #include "libnfs.h"
35 #include "libnfs-raw.h"
36 #include "libnfs-private.h"
37 #include "libnfs-raw-mount.h"
38 #include "libnfs-raw-portmap.h"
39
40 struct nfs_list_data {
41 int status;
42 struct nfs_server_list *srvrs;
43 };
44
45 void 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
56 void pm_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data)
57 {
58 struct nfs_list_data *srv_data = private_data;
59 struct sockaddr *sin;
60 char hostdd[16];
61 struct nfs_server_list *srvr;
62
63 if (status == RPC_STATUS_CANCEL) {
64 return;
65 }
66 if (status != 0) {
67 srv_data->status = -1;
68 return;
69 }
70
71 sin = rpc_get_recv_sockaddr(rpc);
72 if (sin == NULL) {
73 rpc_set_error(rpc, "failed to get sockaddr in CALLIT callback");
74 srv_data->status = -1;
75 return;
76 }
77
78 if (getnameinfo(sin, sizeof(struct sockaddr_in), &hostdd[0], sizeof(hostdd), NULL, 0, NI_NUMERICHOST) < 0) {
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;
90 }
91
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;
102 }
103
104 int main(int argc _U_, char *argv[] _U_)
105 {
106 struct rpc_context *rpc;
107 struct pollfd pfd;
108 struct ifconf ifc;
109 int size;
110 struct timeval tv_start, tv_current;
111 struct nfs_list_data data = {0, NULL};
112 struct nfs_server_list *srvr;
113 char *ptr;
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
126
127 /* get list of all interfaces */
128 size = sizeof(struct ifreq);
129 ifc.ifc_buf = NULL;
130 ifc.ifc_len = size;
131
132 while(ifc.ifc_len > (size - sizeof(struct ifreq))) {
133 size *= 2;
134
135 free(ifc.ifc_buf);
136 ifc.ifc_len = size;
137 ifc.ifc_buf = malloc(size);
138 memset(ifc.ifc_buf, 0, size);
139 if (ioctl(rpc_get_fd(rpc), SIOCGIFCONF, (caddr_t)&ifc) < 0) {
140 printf("ioctl SIOCGIFCONF failed\n");
141 exit(10);
142 }
143 }
144
145 for (ptr =(char *)ifc.ifc_buf; ptr < ((char *)ifc.ifc_buf) + ifc.ifc_len; ) {
146 struct ifreq *ifr;
147 char bcdd[16];
148
149 ifr = (struct ifreq *)ptr;
150 #ifdef HAVE_SOCKADDR_LEN
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) {
161 continue;
162 }
163 if (ioctl(rpc_get_fd(rpc), SIOCGIFFLAGS, ifr) < 0) {
164 printf("ioctl DRBADDR failed\n");
165 exit(10);
166 }
167 if (!(ifr->ifr_flags & IFF_UP)) {
168 continue;
169 }
170 if (ifr->ifr_flags & IFF_LOOPBACK) {
171 continue;
172 }
173 if (!(ifr->ifr_flags & IFF_BROADCAST)) {
174 continue;
175 }
176 if (ioctl(rpc_get_fd(rpc), SIOCGIFBRDADDR, ifr) < 0) {
177 continue;
178 }
179 if (getnameinfo(&ifr->ifr_broadaddr, sizeof(struct sockaddr_in), &bcdd[0], sizeof(bcdd), NULL, 0, NI_NUMERICHOST) < 0) {
180 continue;
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
187 if (rpc_pmap_callit_async(rpc, MOUNT_PROGRAM, 2, 0, NULL, 0, pm_cb, &data) < 0) {
188 printf("Failed to set up callit function\n");
189 exit(10);
190 }
191 }
192 free(ifc.ifc_buf);
193
194 gettimeofday(&tv_start, NULL);
195 for(;;) {
196 int mpt;
197
198 pfd.fd = rpc_get_fd(rpc);
199 pfd.events = rpc_which_events(rpc);
200
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) {
207 printf("Poll failed");
208 exit(10);
209 }
210 if (pfd.revents == 0) {
211 break;
212 }
213
214 if (rpc_service(rpc, pfd.revents) < 0) {
215 printf("rpc_service failed with %s\n", rpc_get_error(rpc));
216 break;
217 }
218 }
219
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
225 rpc_destroy_context(rpc);
226 rpc=NULL;
227 return 0;
228 }