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