RPCGen Manual

Complete guide to Netbula's RPCGen protocol compiler for ONC RPC. Learn how to generate client and server stubs from RPC interface definitions.

RPCGen Manual

🚀 RPC Programming Quick Start

Learning Resources

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.

Sample Program Walkthrough

The following is a sample program included under the samples/MyMgSvc/ folder of the ONC RPC SDK.

1) Define the RPC Interface in a .x file

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.

2) Use RPCGen to compile the .x file

rpcgen MyRPCService.x

Type the command "rpcgen MyRPCService.x" at the command prompt. This will produce the following files:

  • MyRPCService.h - The common header file for the RPC
  • MyRPCService_xdr.c - XDR routines for struct aType (used by both client and server)
  • MyRPCService_cln.c - Generated client stub
  • MyRPCService_svc.c - Generated server stub
  • MyRPCService_imp.tmp.c - Server implementation template
  • MyRPCService.hpp - C++ class for the client
  • MyRPCService.mak - Makefile (needs editing)

3) Code the server implementation

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.

4) Build and run the server

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.

5) Write a client

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.

📖 Netbula RPCGen Usage

Modified from rpcgen(1) man page on UNIX systems

NAME

rpcgen - an RPC protocol compiler

USAGE

  1. rpcgen infile
  2. rpcgen -c|-h|-l|-m [-o outfile] infile
  3. rpcgen -s transport [-o outfile] infile

DESCRIPTION

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:

  • proto.h - Header file for data types and function prototypes
  • proto_xdr.c - XDR routines to encode/decode arguments and return values
  • proto_svc.c - Server side stub with dispatch routine
  • proto_clnt.c - Client side stub

Preprocessing

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:

  • RPC_HDR - Defined when compiling into header files
  • RPC_XDR - Defined when compiling into XDR routines
  • RPC_SVC - Defined when compiling into server-side stubs
  • RPC_CLNT - Defined when compiling into client-side stubs

Netbula RPCGen for Win32

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.

Custom XDR Routines

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.

OPTIONS

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

🔌 Portmapper Service (Windows NT/2K/XP only)

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.

Install the portmap service

pmapsvc -i

Uninstall the service

pmapsvc -u

Check status

pmapsvc -v

Get help

pmapsvc -h

To start the service, go to Control Panel, open the Services icon, find the PowerRPC Portmapper and start it.

❓ Need Help?

Contact support@netbula.com for more questions about RPCGen and ONC RPC development.