Intermediate Layer of RPC on the Client Side
#include <stdio.h>
#include <rpc/rpc.h>
#include <utmp.h>
#include <rpcsvc/rusers.h>
main(argc, argv)
int argc;
char **argv;
{
unsigned long nusers;
int stat;
if (argc != 2) {
fprintf(stderr, "usage: nusers hostname\n");
exit(-1);
}
if (stat = callrpc(argv[1],
RUSERSPROG, RUSERSVERS, RUSERSPROC_NUM,
xdr_void, 0, xdr_u_long, &nusers) != 0) {
clnt_perrno(stat);
exit(1);
}
printf("%d users on %s\n", nusers, argv[1]);
exit(0);
}
The callrpc subroutine has eight parameters. The first, host, specifies the name of the remote server machine. The next three parameters, prognum, versnum, and procnum, specify the program, version, and procedure numbers. The fifth and sixth parameters, inproc and in, are an XDR filter and an argument to be encoded and passed to the remote procedure. The final two parameters, outproc and out, are a filter for decoding the results returned by the remote procedure and a pointer to the place where the procedure's results are to be stored. Multiple arguments and results are handled by embedding them in structures. If the callrpc subroutine completes successfully, it returns zero. Otherwise, it returns a nonzero value.
Because data types may be represented differently on different machines, the callrpc subroutine needs both the type of the RPC argument and a pointer to the argument itself. The return value for the RUSERSPROC_NUM parameter is unsigned long, so the callrpc subroutine has xdr_u_long as its first return parameter. This parameter specifies that the result is of the unsigned long type. The second return parameter, &nusers, is a pointer to where the long result is placed. Because the RUSERSPROC_NUM parameter takes no argument, the argument parameter of the callrpc subroutine is xdr_void.