2 Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2011
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.
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.
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/>.
18 /* Example program using the lowlevel raw broadcast interface.
25 #include "win32_compat.h"
41 #ifdef HAVE_SYS_TIME_H
53 #ifdef HAVE_SYS_IOCTL_H
54 #include <sys/ioctl.h>
57 #ifdef HAVE_SYS_SOCKET_H
58 #include <sys/socket.h>
62 #include "libnfs-raw.h"
63 #include "libnfs-private.h"
64 #include "libnfs-raw-mount.h"
65 #include "libnfs-raw-portmap.h"
67 struct nfs_list_data
{
69 struct nfs_server_list
*srvrs
;
72 void free_nfs_srvr_list(struct nfs_server_list
*srv
)
75 struct nfs_server_list
*next
= srv
->next
;
83 void pm_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
85 struct nfs_list_data
*srv_data
= private_data
;
88 struct nfs_server_list
*srvr
;
90 if (status
== RPC_STATUS_CANCEL
) {
94 srv_data
->status
= -1;
98 sin
= rpc_get_recv_sockaddr(rpc
);
100 rpc_set_error(rpc
, "failed to get sockaddr in CALLIT callback");
101 srv_data
->status
= -1;
105 if (getnameinfo(sin
, sizeof(struct sockaddr_in
), &hostdd
[0], sizeof(hostdd
), NULL
, 0, NI_NUMERICHOST
) < 0) {
106 rpc_set_error(rpc
, "getnameinfo failed in CALLIT callback");
107 srv_data
->status
= -1;
112 srvr
= malloc(sizeof(struct nfs_server_list
));
114 rpc_set_error(rpc
, "Malloc failed when allocating server structure");
115 srv_data
->status
= -1;
119 srvr
->addr
= strdup(hostdd
);
120 if (srvr
->addr
== NULL
) {
121 rpc_set_error(rpc
, "Strdup failed when allocating server structure");
123 srv_data
->status
= -1;
127 srvr
->next
= srv_data
->srvrs
;
128 srv_data
->srvrs
= srvr
;
131 int main(int argc _U_
, char *argv
[] _U_
)
133 struct rpc_context
*rpc
;
137 struct timeval tv_start
, tv_current
;
138 struct nfs_list_data data
= {0, NULL
};
139 struct nfs_server_list
*srvr
;
142 rpc
= rpc_init_udp_context();
144 printf("failed to init context\n");
148 if (rpc_bind_udp(rpc
, "0.0.0.0", 0) < 0) {
149 printf("failed to bind to udp %s\n", rpc_get_error(rpc
));
154 /* get list of all interfaces */
155 size
= sizeof(struct ifreq
);
159 while(ifc
.ifc_len
> (size
- sizeof(struct ifreq
))) {
164 ifc
.ifc_buf
= malloc(size
);
165 memset(ifc
.ifc_buf
, 0, size
);
166 if (ioctl(rpc_get_fd(rpc
), SIOCGIFCONF
, (caddr_t
)&ifc
) < 0) {
167 printf("ioctl SIOCGIFCONF failed\n");
172 for (ptr
=(char *)ifc
.ifc_buf
; ptr
< ((char *)ifc
.ifc_buf
) + ifc
.ifc_len
; ) {
176 ifr
= (struct ifreq
*)ptr
;
177 #ifdef HAVE_SOCKADDR_LEN
178 if (ifr
->ifr_addr
.sa_len
> sizeof(struct sockaddr
)) {
179 ptr
+= sizeof(ifr
->ifr_name
) + ifr
->ifr_addr
.sa_len
;
181 ptr
+= sizeof(ifr
->ifr_name
) + sizeof(struct sockaddr
);
184 ptr
+= sizeof(struct ifreq
);
187 if (ifr
->ifr_addr
.sa_family
!= AF_INET
) {
190 if (ioctl(rpc_get_fd(rpc
), SIOCGIFFLAGS
, ifr
) < 0) {
191 printf("ioctl DRBADDR failed\n");
194 if (!(ifr
->ifr_flags
& IFF_UP
)) {
197 if (ifr
->ifr_flags
& IFF_LOOPBACK
) {
200 if (!(ifr
->ifr_flags
& IFF_BROADCAST
)) {
203 if (ioctl(rpc_get_fd(rpc
), SIOCGIFBRDADDR
, ifr
) < 0) {
206 if (getnameinfo(&ifr
->ifr_broadaddr
, sizeof(struct sockaddr_in
), &bcdd
[0], sizeof(bcdd
), NULL
, 0, NI_NUMERICHOST
) < 0) {
209 if (rpc_set_udp_destination(rpc
, bcdd
, 111, 1) < 0) {
210 printf("failed to set udp destination %s\n", rpc_get_error(rpc
));
214 if (rpc_pmap2_callit_async(rpc
, MOUNT_PROGRAM
, 2, 0, NULL
, 0, pm_cb
, &data
) < 0) {
215 printf("Failed to set up callit function\n");
221 gettimeofday(&tv_start
, NULL
);
225 pfd
.fd
= rpc_get_fd(rpc
);
226 pfd
.events
= rpc_which_events(rpc
);
228 gettimeofday(&tv_current
, NULL
);
230 - (tv_current
.tv_sec
*1000 + tv_current
.tv_usec
/ 1000)
231 + (tv_start
.tv_sec
*1000 + tv_start
.tv_usec
/ 1000);
233 if (poll(&pfd
, 1, mpt
) < 0) {
234 printf("Poll failed");
237 if (pfd
.revents
== 0) {
241 if (rpc_service(rpc
, pfd
.revents
) < 0) {
242 printf("rpc_service failed with %s\n", rpc_get_error(rpc
));
247 for (srvr
=data
.srvrs
; srvr
; srvr
= srvr
->next
) {
248 printf("NFS SERVER @ %s\n", srvr
->addr
);
250 free_nfs_srvr_list(data
.srvrs
);
252 rpc_destroy_context(rpc
);