add example of broadcast context and broadcasting an RPC call
[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 <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>
28 #include <netdb.h>
29 #include "libnfs.h"
30 #include "libnfs-raw.h"
31 #include "libnfs-raw-mount.h"
32 #include "libnfs-raw-portmap.h"
33 #include "libnfs-private.h"
34
35 void pm_cb(struct rpc_context *rpc _U_, int status, void *data, void *private_data _U_)
36 {
37 pmap_call_result *res = (pmap_call_result *)data;
38 struct sockaddr *sin;
39 char hostdd[16];
40
41 if (status != 0) {
42 printf("callback for CALLIT failed\n");
43 exit(10);
44 }
45
46 sin = rpc_get_recv_sockaddr(rpc);
47 if (sin == NULL) {
48 printf("failed to get sockaddr for received pdu\n");
49 exit(10);
50 }
51
52 if (getnameinfo(sin, sizeof(struct sockaddr_in), &hostdd[0], sizeof(hostdd), NULL, 0, NI_NUMERICHOST) < 0) {
53 printf("getnameinfo failed\n");
54 exit(10);
55 }
56
57 printf("NFS server at %s\n", hostdd);
58 }
59
60 int main(int argc _U_, char *argv[] _U_)
61 {
62 struct rpc_context *rpc;
63 struct pollfd pfd;
64
65 rpc = rpc_init_udp_context();
66 if (rpc == NULL) {
67 printf("failed to init context\n");
68 exit(10);
69 }
70
71 if (rpc_bind_udp(rpc, "0.0.0.0", 0) < 0) {
72 printf("failed to bind to udp %s\n", rpc_get_error(rpc));
73 exit(10);
74 }
75
76 if (rpc_set_udp_destination(rpc, "10.1.1.255", 111, 1) < 0) {
77 printf("failed to set udp destination %s\n", rpc_get_error(rpc));
78 exit(10);
79 }
80
81 if (rpc_pmap_callit_async(rpc, 100005, 2, 0, NULL, 0, pm_cb, NULL) < 0) {
82 printf("Failed to set up callit function\n");
83 exit(10);
84 }
85 if (rpc_set_udp_destination(rpc, "10.9.2.255", 111, 1) < 0) {
86 printf("failed to set udp destination %s\n", rpc_get_error(rpc));
87 exit(10);
88 }
89
90 if (rpc_pmap_callit_async(rpc, 100005, 2, 0, NULL, 0, pm_cb, NULL) < 0) {
91 printf("Failed to set up callit function\n");
92 exit(10);
93 }
94
95 alarm(3);
96
97 for(;;) {
98 pfd.fd = rpc_get_fd(rpc);
99 pfd.events = rpc_which_events(rpc);
100
101 if (poll(&pfd, 1, -1) < 0) {
102 printf("Poll failed");
103 exit(10);
104 }
105 if (rpc_service(rpc, pfd.revents) < 0) {
106 printf("rpc_service failed with %s\n", rpc_get_error(rpc));
107 break;
108 }
109 }
110
111 rpc_destroy_context(rpc);
112 rpc=NULL;
113 printf("nfsclient finished\n");
114 return 0;
115 }