nullnullRPC ProgrammingRPC ProgrammingIntroduction to Remote Procedure Call
The Programming's Interface to RPC
Building Application with rpcgen
Exmaple source code & referencesI. Introduction to Remote Procedure CallI. Introduction to Remote Procedure CallThe basic concepts of RPC
The Issues in RPC
Program, Version and Procedure
rpcbind & rpcinfoLocal and Remote Procedure CallLocal and Remote Procedure CallLocal Procedure Call
The called and calling procedures locate in the same address space (in more common word, in the same process).
Remote Procedure Call
The called and calling procedures locate in the different address space ( can be in same system, or different systems which are connected by network).Issues in RPCIssues in RPCHow are parameters and result passed.
How is a binding carried out(How to register and find a remote procedure).
Transport protocol.
Call semantics.
Data representation.Parameter PassingParameter PassingOnly at most single one parameter is allowed.
Multiple pieces of information can be combined into a structure.
The information from server to client should be passed as the function's return value.
It is not possible to pass information back from server to client through the parameter list.BindingBindingFind out which host a server is on.
Just the machine's name
Find out the service you want in a server
Program, Version and Procedure naming schema is used to locate a service in a server.Transport ProtocolTransport ProtocolSpecifies how the call message and reply message are transmitted between client and server. In common words, which transport service is used in a RPC.
Tranditionly, TCP and UDP are used.
Other TLI transport protocols can also be used. Call SemanticsCall Semantics"At least Once" Semantics is provided.
"idempotent" Semantics is guaranteed.
A rpc service should return the same result each time, even if this is serverl times.Data RepresentationData RepresentationThe format used for parameters and result as they are passed between the processes.
XDR is used.
A function is provided for every C language primitive datatype, such as char, int, long, etc.
The function name conversion is, for primitive xxx, the function name is xdr_xxx.
Think where to get the functions for user defined data types?Program, Version & ProcedureProgram, Version & ProcedureA Program can contain multiple Versions.
A Version can contain multiple Procedures.
Programs, Versions and Procedures are all represented as numbers.
The Procedures with the same Program and Version locates in the same process.
The triple (Program, Version, Procedure) identifies an RPC in a system. Program, Version & Procedure(Cont.)Program, Version & Procedure(Cont.)Program use conversion:
0x0000, 0000 to 0x1fff, ffff Defined by Sun
0x2000, 0000 to 0x3fff, ffff Available for local use
0x4000, 0000 to 0x5fff, ffff Reserved for transient use
0x6000, 0000 to 0xffff, ffff Reserved for future userpcbind & rpcinforpcbind & rpcinfoClientrpcbindClient SystemServer System111Server
ProgramAProg,Ver, Noaddressaddressrpcbind & rpcinfo(cont.)rpcbind & rpcinfo(cont.)rpcinfo is used to list the rpc service information in a system:
# rpcinfo
program version netid address service owner
100000 4 ticots gemini.rpc rpcbind superuser
100000 3 ticots gemini.rpc rpcbind superuser
....II. The Programming's Interface to RPCII. The Programming's Interface to RPCSimplified Interface
Standard InterfaceSimplified InterfaceSimplified InterfaceServer side
int rpc_gen (
u_long prognum, Program number
u_long vernum, Version number
u_long procnum, Procedure number
char *(*proc)( ), Actual function to call
xdrproc_t inproc, XDR filter to decode agument
xdrproc_t outproc, XDR filter to encode result
char *nettype For transport selection );
void svc_run(); Only return if a fatal error occurs Simplified Interface(cont.)Simplified Interface(cont.)Client Side
int rpc_call (
char host, Name of server host
u_long prognum, Program number
u_long vernum, Version number
u_long procnum, Procedure number
xdrproc_t inproc, XDR filter to decode argument
char *in, Pointer to argument
xdrproc_t outproc, XDR filter to encode result
char *nettype For transport selection );
char *clnt_sperrno ( enum clnt_stat stat);Some explanations for the Simplified InterfaceSome explanations for the Simplified Interfacerpc_gen
This function registers a rpc service to rpcbind and gets a transport address.
This function binds the server to the transport address.
A XDR file should be provided if a user defined data type tranfered in rpc.
The return value should be "static" storage type.nullExampleServer side:
...
#include
#include "icode.h"
....
int *geticode_1();
if ( rpc_reg(ICODE_PROC, ICODE_VERS, ICODE_PROC,
geticode_1, xdr_char, xdr_int,
"netpath") == (-1) ) {
fprintf(stderr, "Couldn't register server\n");
exit(1);
}
svc_run();
}
Example (cont.)Example (cont.)int *geticode_1 ( ch )
char *ch;
{
static int ival;
ival = (int) *ch;
return(&ival);
}
Example(cont.)Example(cont.)client side:
#include
...
ret = rpc_call( argv[1], ICODE_PROG, ICODE_VERS,
ICODE_PROC, xdr_char, &c,
xdr_int, (char *)&icode, "netpath");
...Standard RPC InterfacesStandard RPC InterfacesServer Side:
Set up a transport handle
Register service with rpcbind
Call svc_run function
Include RPC dispatcher functionStandard RPC InterfacesStandard RPC InterfacesClient
Set up a client handle to server program
Call remote procedure
Destroy the client handleStandard Server Side(Top level)Standard Server Side(Top level)int svc_create ( the transport handle created
void (* dispatch) (), Dispatch function
u_long prognum, Program number
u_long versnum, Version number
char *nettype For transport selection ); Standard Server Side
(Top level)(cont.)Standard Server Side
(Top level)(cont.)int svc_run( );Dispatch FunctionDispatch Functionvoid dispatch (
struct svc_req * req,
SVCXRT *xprt Transport handle
Utility Rountines for the Dispatch Function
bool svc_getargs(
SVCXPRT * xprt, Transport handle
xdrproc_t inproc, XDR filter to decode argument
char * in) Where to store decode argumentDispatch Function(cont.)Dispatch Function(cont.)bool_t svc_freeargs (
SVCXPRT *xprt, Transport handle
xdrproc_t inproc, XDR filter for argument
char *in); Pointer to argument
bool_t svc_sendreply (
SVCXPRT * xprt,
xdrproc_t outproc,
char *out); Pointer to result. If NULL, no reply is sent.Example: Server main()Example: Server main()server side:
...
main()
{...
int n_transp;
void dispatch();
n_transp = svc_create(dispatch, PROG, VERS, "netpath");
...
svc_run();
...
} Standard Client Side
(Top level)Standard Client Side
(Top level)CLIENT * clnt_create ( Client handle
char *host, hostname of server
u_long prog, Program number
u_long vers, Version number
char *nettype For transport selectionStandard Client Side
(Top level)(cont.)Standard Client Side
(Top level)(cont.)enum clnt_stat clnt_call (
CLIENT *clnt_p, Client handle
u_long proc, Procudure number
xdrproc_t inproc, XDR filter for argument
char *in, Pointer to argument
xdrproc *outproc, XDR filter for result
char *out, Address to store result
struct timeval timeout Timeout value );Example: Dispatch routineExample: Dispatch routinedispatch rountine:
void dispatch( rqstp, transp)
struct svc_req *rqstp; SVCXPRT *transp;
{
char in_char; int *outval; int *geticode_1();
if( rqstp -> rq_proc == NULLPROC ) {
svc_sendreply(transp, xdr_void, NULL);
return;
}
switch ( rqstp -> rq_proc ) {
case: ICODE _PROC:
svc_getargs(transp, xdr_char, &in_char);
outval = geticode_1(&in_char);
svc_sendreply(transp, xdr_int, outval);
...
Example: clientExample: clientclient side:
...
struct timeval timeout = {20, 0};
main(argc, argv)
int argc; char* argv[];
{
CLIENT * clnt_p; char c; int icode, ret;
...
clnt_p = clnt_create(argv[1], PROG, VERS "netpath");
status = clnt_call(clnt_p, PROC, xdr_char, &c, xdr_int, &icode, timeout);
...
clnt_destroy(clnt_p);
...
}
III. Building Applicatoin with rpcgenIII. Building Applicatoin with rpcgenWhy rpcgen(What does rpcgen create)
The files created by rpcgen
The files should be provided by programers
Two main RPC Language types
A typical procedure for pragraming when rpcgen used.Why rpcgenWhy rpcgenrpcgen is used to create stub functions for client and server.
rpcgen is used to create XDR filter subroutines for extended data types.
By using rpcgen, programers are free from RPC handle creating, registering a RPC, but focus on the business functions.The files created by rpcgenThe files created by rpcgenxxx.h, Header files:
constants such as program numbers, version numbers, and procedure numbers.
extened data type.
should be included the files involved in RPC
xxx_clnt.c, Client stub
should be linked in client program
xxx_svc.c, Server stub
should be linked in server program
main function is included. The files created by rpcgen(cont.)The files created by rpcgen(cont.)xxx_xdr.c, XDR filters:
All the extended data type filters are included
Should be linked both in server and client.The files provided by programsThe files provided by programs(for client side) The client main function.
(for server side)The actual RPC subroutines.Two main RPC Language typesTwo main RPC Language typesThe PRC declaration type
is used to declare remote procedures:
program id {
version id{
type_spec id ( type_spec ) = constant; /* procdurce number */
....
} = constant; /*version number */
....
} = constant; /* program number */
The extended data type definition type
extended data types are defined and mapped to C languageExample for RPCLExample for RPCLconst LABELSZ = 10;
const VALSZ = 5;
typedef string lab_t;
struct datum {
int d_type;
lab_t d_label;
int d_values[VALSZ];
struct datum *d_next;
};
struct point {
int x_coord;
int y_coord;
};Example for RPCL(cont.)Example for RPCL(cont.)program ICODE_PROG {
version ICODE_VERS {
int geticode(point) = 1;
} = 1;
} = 0x20000299;
A typical procedure when rpcgen is usedA typical procedure when rpcgen is usedProcess RPC Language source code file xxx.rpc
rpcgen xxx.rpc : xxx.h, xxx_clnt.c xxx_svc.c xxx_xdr.c are created.
Make server program by:
cc -o server xxx_svc.c rpc_funcs.c xxx_xdr.c -lnsl
All the "actual" remote procedures are coded in rpc_funcs.c
Make client program by:
cc -o client client.c xxx_svc.c xxx_xdr.c -lnslIV. Exmaples & referencesIV. Exmaples & referencesExample source code can be gotten http://sunlibrary
References:
Solaris Answer Book, Software Developing Collection, ONC_Developer_Guide & Network_Interface_Guide. All these documents can be gotten http://sunlibrary.