2075f09371e2e98f7981522e36d0d8d7d13f4432
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.
27 #include <sys/socket.h>
28 #include <sys/ioctl.h>
33 #include "libnfs-raw.h"
34 #include "libnfs-raw-mount.h"
35 #include "libnfs-raw-portmap.h"
36 #include "libnfs-private.h"
38 struct nfs_server_list
{
39 struct nfs_server_list
*next
;
43 struct nfs_list_data
{
45 struct nfs_server_list
*srvrs
;
48 void free_nfs_srvr_list(struct nfs_server_list
*srv
)
51 struct nfs_server_list
*next
= srv
->next
;
59 void pm_cb(struct rpc_context
*rpc
, int status
, void *data
, void *private_data _U_
)
61 pmap_call_result
*res
= (pmap_call_result
*)data
;
62 struct nfs_list_data
*srv_data
= private_data
;
65 struct nfs_server_list
*srvr
;
67 if (status
== RPC_STATUS_CANCEL
) {
71 srv_data
->status
= -1;
75 sin
= rpc_get_recv_sockaddr(rpc
);
77 rpc_set_error(rpc
, "failed to get sockaddr in CALLIT callback");
78 srv_data
->status
= -1;
82 if (getnameinfo(sin
, sizeof(struct sockaddr_in
), &hostdd
[0], sizeof(hostdd
), NULL
, 0, NI_NUMERICHOST
) < 0) {
83 rpc_set_error(rpc
, "getnameinfo failed in CALLIT callback");
84 srv_data
->status
= -1;
89 srvr
= malloc(sizeof(struct nfs_server_list
));
91 rpc_set_error(rpc
, "Malloc failed when allocating server structure");
92 srv_data
->status
= -1;
96 srvr
->addr
= strdup(hostdd
);
97 if (srvr
->addr
== NULL
) {
98 rpc_set_error(rpc
, "Strdup failed when allocating server structure");
100 srv_data
->status
= -1;
104 srvr
->next
= srv_data
->srvrs
;
105 srv_data
->srvrs
= srvr
;
108 int main(int argc _U_
, char *argv
[] _U_
)
110 struct rpc_context
*rpc
;
114 struct timeval tv_start
, tv_current
;
115 struct nfs_list_data data
= {0, NULL
};
116 struct nfs_server_list
*srvr
;
118 rpc
= rpc_init_udp_context();
120 printf("failed to init context\n");
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
));
130 /* get list of all interfaces */
131 size
= sizeof(struct ifreq
);
135 while (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");
147 for (i
=0; i
<ifc
.ifc_len
/ sizeof(struct ifconf
); i
++) {
150 if (ifc
.ifc_req
[i
].ifr_addr
.sa_family
!= AF_INET
) {
153 if (ioctl(rpc_get_fd(rpc
), SIOCGIFFLAGS
, &ifc
.ifc_req
[i
]) < 0) {
154 printf("ioctl DRBADDR failed\n");
157 if (!(ifc
.ifc_req
[i
].ifr_flags
& IFF_UP
)) {
160 if (ifc
.ifc_req
[i
].ifr_flags
& IFF_LOOPBACK
) {
163 if (!(ifc
.ifc_req
[i
].ifr_flags
& IFF_BROADCAST
)) {
166 if (ioctl(rpc_get_fd(rpc
), SIOCGIFBRDADDR
, &ifc
.ifc_req
[i
]) < 0) {
167 printf("ioctl DRBADDR failed\n");
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");
174 if (rpc_set_udp_destination(rpc
, bcdd
, 111, 1) < 0) {
175 printf("failed to set udp destination %s\n", rpc_get_error(rpc
));
179 if (rpc_pmap_callit_async(rpc
, MOUNT_PROGRAM
, 2, 0, NULL
, 0, pm_cb
, &data
) < 0) {
180 printf("Failed to set up callit function\n");
186 gettimeofday(&tv_start
, NULL
);
190 pfd
.fd
= rpc_get_fd(rpc
);
191 pfd
.events
= rpc_which_events(rpc
);
193 gettimeofday(&tv_current
, NULL
);
195 - (tv_current
.tv_sec
*1000 + tv_current
.tv_usec
/ 1000)
196 + (tv_start
.tv_sec
*1000 + tv_start
.tv_usec
/ 1000);
198 if (poll(&pfd
, 1, mpt
) < 0) {
199 printf("Poll failed");
202 if (pfd
.revents
== 0) {
206 if (rpc_service(rpc
, pfd
.revents
) < 0) {
207 printf("rpc_service failed with %s\n", rpc_get_error(rpc
));
212 for (srvr
=data
.srvrs
; srvr
; srvr
= srvr
->next
) {
213 printf("NFS SERVER @ %s\n", srvr
->addr
);
215 free_nfs_srvr_list(data
.srvrs
);
217 rpc_destroy_context(rpc
);