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