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