Complete guide to Netbula's RPCGen protocol compiler for ONC RPC. Learn how to generate client and server stubs from RPC interface definitions.
If you are not familiar with ONC RPC, the book "Power Programming RPC" from O'Reilly is a useful guide. Also, there is extensive online documentation for programming ONC RPC from DEC.
The following is a sample program included under the samples/MyMgSvc/ folder of the ONC RPC SDK.
struct aTypeStruct {
int i;
double d;
};
typedef struct aTypeStruct aType;
program MyRPCService {
version ThisVersion {
aType function_foo(int) = 1;
int function_bar(aType) = 2;
} = 1;
} = 34567;
Here, we defined an RPC interface with program number 34567, and two procedures function_foo and function_bar. Save this to a file named MyRPCService.x.
rpcgen MyRPCService.x
Type the command "rpcgen MyRPCService.x" at the command prompt. This will produce the following files:
The MyRPCService.h file includes declarations for two functions function_foo_svc() and function_bar_svc(). The RPC server programmer must implement these functions to do the real work.
Rpcgen generates a server implementation template file that you can copy and fill in the details.
Compile the MyRPCService_xdr.c, MyRPCService_svc.c and MyRPCService_imp.c files. Make sure the include/ directory of the Netbula ONC RPC SDK is in the include path. Link with the pwrpc32.lib import library.
When running the built RPC server/client, make sure pwrpc32.dll can be found by the application.
The ONC RPC SDK contains a makefile template which can be easily modified for this sample application.
To run the RPC server, first start the portmapper (pmapsvc or portmap.exe), then start your MyRPCService server.
The client code looks like this:
int i;
aType a, *ap;
CLIENT *cl = clnt_create("server_host_name", program_number, version_number, "tcp");
ap = function_foo_1(&i, cl);
i = function_bar_1(&a, cl);
RPCGEN also generates C++ code for the client. View C++ client example.
Modified from rpcgen(1) man page on UNIX systems
rpcgen - an RPC protocol compiler
rpcgen is a compiler that generates C source code from a protocol specification file (normally with .x suffix). When run without additional options flags, rpcgen produces the following C files from an input file named proto.x:
The C-preprocessor, CPP, is used by rpcgen to preprocess the input file. Rpcgen does multiple passes to generate each type of file, defining different CPP symbols during each pass:
Uses CL.EXE in MS Visual C++ as the default C-preprocessor (with flags /C /EP /nologo). The CL.EXE must be in the search path, or rpcgen will output a "preprocessing failed" error.
VC++ comes with a script named vcvars32.bat which does the proper environment setting.
You can let rpcgen use a different CPP by setting two environment variables: RPCGEN_CPP and RPCGEN_CPP_FLAG, for the CPP program and associated flags, respectively.
Rpcgen does a little preprocessing of its own. Any line beginning with '%' is passed directly into the output file, uninterpreted by rpcgen.
You can customize some of your XDR routines by leaving those data types undefined. For every data type that is undefined, rpcgen will assume that there exists a routine with the name xdr_ prepended to the name of the undefined type.
| Flag | Argument | Description |
|---|---|---|
| -c | none | Compile into XDR routines |
| -h | none | Compile into C data-definitions (header file) |
| -l | none | Compile into client-side stubs |
| -m | none | Compile into server-side stubs without "main" function |
| -o | output filename | Specify output filename (standard output used if not specified) |
| -s | transport name (udp or tcp) | Compile into server-side stubs using given transport |
When an RPC server starts up, it registers its program number and port with the Portmapper on its local machine. Before a client calls an RPC, it must contact the portmapper running on the server machine to find out the port number of the RPC server.
pmapsvc -i
pmapsvc -u
pmapsvc -v
pmapsvc -h
To start the service, go to Control Panel, open the Services icon, find the PowerRPC Portmapper and start it.
Contact support@netbula.com for more questions about RPCGen and ONC RPC development.