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