make the bcast example use timers and wait for up to 1 second for all
[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"
36#include "libnfs-private.h"
37
38void pm_cb(struct rpc_context *rpc _U_, int status, void *data, void *private_data _U_)
39{
40 pmap_call_result *res = (pmap_call_result *)data;
41 struct sockaddr *sin;
42 char hostdd[16];
43
cb46c8fb
RS
44 if (status == RPC_STATUS_CANCEL) {
45 return;
46 }
28f7bd66
RS
47 if (status != 0) {
48 printf("callback for CALLIT failed\n");
49 exit(10);
50 }
51
52 sin = rpc_get_recv_sockaddr(rpc);
53 if (sin == NULL) {
54 printf("failed to get sockaddr for received pdu\n");
55 exit(10);
56 }
57
58 if (getnameinfo(sin, sizeof(struct sockaddr_in), &hostdd[0], sizeof(hostdd), NULL, 0, NI_NUMERICHOST) < 0) {
59 printf("getnameinfo failed\n");
60 exit(10);
61 }
62
63 printf("NFS server at %s\n", hostdd);
64}
65
66int main(int argc _U_, char *argv[] _U_)
67{
68 struct rpc_context *rpc;
69 struct pollfd pfd;
92e787fe
RS
70 struct ifconf ifc;
71 int i, size;
cb46c8fb 72 struct timeval tv_start, tv_current;
28f7bd66
RS
73
74 rpc = rpc_init_udp_context();
75 if (rpc == NULL) {
76 printf("failed to init context\n");
77 exit(10);
78 }
79
80 if (rpc_bind_udp(rpc, "0.0.0.0", 0) < 0) {
81 printf("failed to bind to udp %s\n", rpc_get_error(rpc));
82 exit(10);
83 }
84
28f7bd66 85
92e787fe
RS
86 /* get list of all interfaces */
87 size = sizeof(struct ifreq);
88 ifc.ifc_buf = NULL;
89 ifc.ifc_len = size;
28f7bd66 90
92e787fe
RS
91 while (ifc.ifc_len == size) {
92 size *= 2;
93
94 free(ifc.ifc_buf);
95 ifc.ifc_len = size;
96 ifc.ifc_buf = malloc(size);
97 if (ioctl(rpc_get_fd(rpc), SIOCGIFCONF, (caddr_t)&ifc) < 0) {
98 printf("ioctl SIOCGIFCONF failed\n");
99 exit(10);
100 }
101 }
102
103 for (i=0; i<ifc.ifc_len / sizeof(struct ifconf); i++) {
104 char bcdd[16];
105
106 if (ifc.ifc_req[i].ifr_addr.sa_family != AF_INET) {
107 continue;
108 }
109 if (ioctl(rpc_get_fd(rpc), SIOCGIFFLAGS, &ifc.ifc_req[i]) < 0) {
110 printf("ioctl DRBADDR failed\n");
111 exit(10);
112 }
113 if (!(ifc.ifc_req[i].ifr_flags & IFF_UP)) {
114 continue;
115 }
116 if (ifc.ifc_req[i].ifr_flags & IFF_LOOPBACK) {
117 continue;
118 }
119 if (!(ifc.ifc_req[i].ifr_flags & IFF_BROADCAST)) {
120 continue;
121 }
122 if (ioctl(rpc_get_fd(rpc), SIOCGIFBRDADDR, &ifc.ifc_req[i]) < 0) {
123 printf("ioctl DRBADDR failed\n");
124 exit(10);
125 }
126 if (getnameinfo(&ifc.ifc_req[i].ifr_broadaddr, sizeof(struct sockaddr_in), &bcdd[0], sizeof(bcdd), NULL, 0, NI_NUMERICHOST) < 0) {
127 printf("getnameinfo failed\n");
128 exit(10);
129 }
130 if (rpc_set_udp_destination(rpc, bcdd, 111, 1) < 0) {
131 printf("failed to set udp destination %s\n", rpc_get_error(rpc));
132 exit(10);
133 }
134
135 if (rpc_pmap_callit_async(rpc, MOUNT_PROGRAM, 2, 0, NULL, 0, pm_cb, NULL) < 0) {
136 printf("Failed to set up callit function\n");
137 exit(10);
138 }
28f7bd66 139 }
92e787fe
RS
140 free(ifc.ifc_buf);
141
cb46c8fb 142 gettimeofday(&tv_start, NULL);
28f7bd66 143 for(;;) {
cb46c8fb
RS
144 int mpt;
145
28f7bd66
RS
146 pfd.fd = rpc_get_fd(rpc);
147 pfd.events = rpc_which_events(rpc);
148
cb46c8fb
RS
149 gettimeofday(&tv_current, NULL);
150 mpt = 1000
151 - (tv_current.tv_sec *1000 + tv_current.tv_usec / 1000)
152 + (tv_start.tv_sec *1000 + tv_start.tv_usec / 1000);
153
154 if (poll(&pfd, 1, mpt) < 0) {
28f7bd66
RS
155 printf("Poll failed");
156 exit(10);
157 }
cb46c8fb
RS
158 if (pfd.revents == 0) {
159 break;
160 }
161
28f7bd66
RS
162 if (rpc_service(rpc, pfd.revents) < 0) {
163 printf("rpc_service failed with %s\n", rpc_get_error(rpc));
164 break;
165 }
166 }
92e787fe 167
28f7bd66
RS
168 rpc_destroy_context(rpc);
169 rpc=NULL;
28f7bd66
RS
170 return 0;
171}