From 28f7bd66967e39c8f2290f32c9b3dee1306cb11c Mon Sep 17 00:00:00 2001 From: Ronnie Sahlberg Date: Sat, 2 Jul 2011 07:36:09 +1000 Subject: [PATCH] add example of broadcast context and broadcasting an RPC call --- Makefile.in | 6 +- examples/nfsclient-bcast.c | 115 +++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 examples/nfsclient-bcast.c diff --git a/Makefile.in b/Makefile.in index d86a1b7..d2d0c83 100644 --- a/Makefile.in +++ b/Makefile.in @@ -35,7 +35,7 @@ nfs/libnfs-raw-nfs.o nfs/nfs.o \ rquota/libnfs-raw-rquota.o rquota/rquota.o ifeq ("$(ENABLE_EXAMPLES)","yes") - EXAMPLES=bin/nfsclient-raw bin/nfsclient-async bin/nfsclient-sync + EXAMPLES=bin/nfsclient-raw bin/nfsclient-async bin/nfsclient-sync bin/nfsclient-bcast endif all: $(LIBNFS_SO) $(EXAMPLES) @@ -52,6 +52,10 @@ bin/nfsclient-raw: examples/nfsclient-raw.c $(LIBNFS_A) mkdir -p bin $(CC) $(CFLAGS) -o $@ examples/nfsclient-raw.c $(LIBNFS_A) $(LIBS) +bin/nfsclient-bcast: examples/nfsclient-bcast.c $(LIBNFS_A) + mkdir -p bin + $(CC) $(CFLAGS) -o $@ examples/nfsclient-bcast.c $(LIBNFS_A) $(LIBS) + $(LIBNFS_A): $(LIBNFS_OBJS) $(LIB_OBJS) @echo Creating library $@ ar r $(LIBNFS_A) $(LIBNFS_OBJS) $(LIB_OBJS) diff --git a/examples/nfsclient-bcast.c b/examples/nfsclient-bcast.c new file mode 100644 index 0000000..c74c5b3 --- /dev/null +++ b/examples/nfsclient-bcast.c @@ -0,0 +1,115 @@ +/* + Copyright (C) by Ronnie Sahlberg 2011 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +/* Example program using the lowlevel raw broadcast interface. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "libnfs.h" +#include "libnfs-raw.h" +#include "libnfs-raw-mount.h" +#include "libnfs-raw-portmap.h" +#include "libnfs-private.h" + +void pm_cb(struct rpc_context *rpc _U_, int status, void *data, void *private_data _U_) +{ + pmap_call_result *res = (pmap_call_result *)data; + struct sockaddr *sin; + char hostdd[16]; + + if (status != 0) { + printf("callback for CALLIT failed\n"); + exit(10); + } + + sin = rpc_get_recv_sockaddr(rpc); + if (sin == NULL) { + printf("failed to get sockaddr for received pdu\n"); + exit(10); + } + + if (getnameinfo(sin, sizeof(struct sockaddr_in), &hostdd[0], sizeof(hostdd), NULL, 0, NI_NUMERICHOST) < 0) { + printf("getnameinfo failed\n"); + exit(10); + } + + printf("NFS server at %s\n", hostdd); +} + +int main(int argc _U_, char *argv[] _U_) +{ + struct rpc_context *rpc; + struct pollfd pfd; + + rpc = rpc_init_udp_context(); + if (rpc == NULL) { + printf("failed to init context\n"); + exit(10); + } + + if (rpc_bind_udp(rpc, "0.0.0.0", 0) < 0) { + printf("failed to bind to udp %s\n", rpc_get_error(rpc)); + exit(10); + } + + if (rpc_set_udp_destination(rpc, "10.1.1.255", 111, 1) < 0) { + printf("failed to set udp destination %s\n", rpc_get_error(rpc)); + exit(10); + } + + if (rpc_pmap_callit_async(rpc, 100005, 2, 0, NULL, 0, pm_cb, NULL) < 0) { + printf("Failed to set up callit function\n"); + exit(10); + } + if (rpc_set_udp_destination(rpc, "10.9.2.255", 111, 1) < 0) { + printf("failed to set udp destination %s\n", rpc_get_error(rpc)); + exit(10); + } + + if (rpc_pmap_callit_async(rpc, 100005, 2, 0, NULL, 0, pm_cb, NULL) < 0) { + printf("Failed to set up callit function\n"); + exit(10); + } + + alarm(3); + + for(;;) { + pfd.fd = rpc_get_fd(rpc); + pfd.events = rpc_which_events(rpc); + + if (poll(&pfd, 1, -1) < 0) { + printf("Poll failed"); + exit(10); + } + if (rpc_service(rpc, pfd.revents) < 0) { + printf("rpc_service failed with %s\n", rpc_get_error(rpc)); + break; + } + } + + rpc_destroy_context(rpc); + rpc=NULL; + printf("nfsclient finished\n"); + return 0; +} -- 2.34.1