ONC+ Developer's Guide

RPC Language Reference

The RPC language is an extension of the XDR language. The sole extension is the addition of the program and version types.

For a description of the RPC extensions to the XDR language, see Appendix B, RPC Protocol and Language Specification.

The RPC language is similar to C. This section describes the syntax of the RPC language, showing a few examples along the way. It also shows how RPC and XDR type definitions get compiled into C type definitions in the output header file.

An RPC language file consists of a series of definitions.

definition-list:
 	definition;
 	definition; definition-list 

It recognizes six types of definitions.

definition:
 	enum-definition
 	const-definition
 	typedef-definition
 	struct-definition
 	union-definition
 	program-definition 

Definitions are not the same as declarations. No space is allocated by a definition - only the type definition of a single or series of data elements. This means that variables still must be declared.

Enumerations

RPC/XDR enumerations have similar syntax as C enumerations.

enum-definition:
   "enum" enum-ident "{"
 		enum-value-list
   "}"

enum-value-list:
   enum-value
   enum-value "," enum-value-list

enum-value:
   enum-value-ident
   enum-value-ident "=" value 
Here is an example of an XDR enum and the C enum to which it gets compiled.
enum colortype {               enum colortype {
 	RED = 0,                       RED = 0,
 	GREEN = 1,       -->           GREEN = 1,
 	BLUE = 2                       BLUE = 2,
};                             };
                               typedef enum colortype colortype; 

Constants

XDR symbolic constants may be used wherever an integer constant is used. For example, in array size specifications:

const-definition:
 	const const-ident = integer 

The following example defines a constant, DOZEN as equal to 12:

const DOZEN = 12; --> #define DOZEN 12 

Type Definitions

XDR typedefs have the same syntax as C typedefs.

typedef-definition:
   typedef declaration 

This example defines an fname_type used for declaring file name strings that have a maximum length of 255 characters.

typedef string fname_type<255>; --> typedef char *fname_type;

Declarations

In XDR, there are four kinds of declarations. These declarations must be a part of a struct or a typedef; they cannot stand alone:

declaration:
 	simple-declaration
 	fixed-array-declaration
 	variable-array-declaration
 	pointer-declaration

Simple Declarations

Simple declarations are just like simple C declarations:

simple-declaration:
 	type-ident variable-ident 

Example:

colortype color; --> colortype color;

Fixed-Length Array Declarations

Fixed-length array declarations are just like C array declarations:

fixed-array-declaration:
 	type-ident variable-ident [value] 

Example:

colortype palette[8]; --> colortype palette[8];

Many programmers confuse variable declarations with type declarations. It is important to note that rpcgen does not support variable declarations. This example is a program that will not compile:

int data[10];
program P {
   version V {
      int PROC(data) = 1;
 	} = 1;
} = 0x200000;

The example above will not compile because of the variable declaration:

int data[10]

Instead, use:

typedef int data[10];

or

struct data {int dummy [10]};

Variable-Length Array Declarations

Variable-length array declarations have no explicit syntax in C. The XDR language does have a syntax, using angle brackets:

variable-array-declaration:
 	type-ident variable-ident <value>
 	type-ident variable-ident < > 

The maximum size is specified between the angle brackets. The size may be omitted, indicating that the array may be of any size:

int heights<12>; /* at most 12 items */
int widths<>; /* any number of items */

Because variable-length arrays have no explicit syntax in C, these declarations are compiled into struct declarations. For example, the heights declaration compiled into the following struct:

struct {
   u_int heights_len;    /* # of items in array */
 	int *heights_val;     /* pointer to array */
} heights;

The number of items in the array is stored in the _len component and the pointer to the array is stored in the _val component. The first part of each component name is the same as the name of the declared XDR variable (heights).

Pointer Declarations

Pointer declarations are made in XDR exactly as they are in C. Address pointers are not really sent over the network; instead, XDR pointers are useful for sending recursive data types such as lists and trees. The type is called "optional-data," not "pointer," in XDR language:

pointer-declaration:
 	type-ident *variable-ident 

Example:

listitem *next; --> listitem *next;

Structures

An RPC/XDR struct is declared almost exactly like its C counterpart. It looks like the following:

struct-definition:
   struct struct-ident "{"
      declaration-list
 	"}"

declaration-list:
   declaration ";"
 	declaration ";" declaration-list

The following XDR structure is an example of a two-dimensional coordinate and the C structure that it compiles into:

struct coord {                 struct coord {
   int x;            -->           int x;
 	int y;                          int y;
};                             };
                               typedef struct coord coord;

The output is identical to the input, except for the added typedef at the end of the output. This enables one to use coord instead of struct coord when declaring items.

Unions

XDR unions are discriminated unions, and do not look like C unions - they are more similar to Pascal variant records:

union-definition:

"union" union-ident "switch" "("simple declaration")" "{"
       case-list
   "}"

case-list:
   "case" value ":" declaration ";"
 	"case" value ":" declaration ";" case-list
 	"default" ":" declaration ";" 

The following is an example of a type returned as the result of a "read data" operation: If there is no error, return a block of data; otherwise, don't return anything.

union read_result switch (int errno) {
 	case 0:
      opaque data[1024];
 	default:
 		void;
 	};

It compiles into the following:

struct read_result {
 	int errno;
 	union {
      char data[1024];
 	} read_result_u;
};
typedef struct read_result read_result;

Notice that the union component of the output struct has the same name as the type name, except for the trailing _u.

Programs

RPC programs are declared using the following syntax:

program-definition:
 	"program" program-ident "{"
 		version-list
 	"}" "=" value; 
version-list:
 	version ";"
 	version ";" version-list
version:
 	"version" version-ident "{"
 		procedure-list
 	"}" "=" value;  
procedure-list:
 	procedure ";"
 	procedure ";" procedure-list
procedure:
   type-ident procedure-ident "(" type-ident ")" "=" value;  

When the -N option is specified, rpcgen also recognizes the following syntax:

procedure:
 	type-ident procedure-ident "(" type-ident-list ")" "=" value;
type-ident-list:
 	type-ident
 	type-ident "," type-ident-list 

For example:

/*
 * time.x: Get or set the time. Time is represented as seconds
 * since 0:00, January 1, 1970.
 */
program TIMEPROG {
   version TIMEVERS {
      unsigned int TIMEGET(void) = 1;
 		void TIMESET(unsigned) = 2;
 	} = 1;
} = 0x20000044;

Note that the void argument type means that no argument is passed.

This file compiles into these #define statements in the output header file:

#define TIMEPROG 0x20000044
#define TIMEVERS 1
#define TIMEGET 1
#define TIMESET 2

Special Cases

There are several exceptions to the RPC language rules.

C-style Mode

In the new features section we talked about the features of the C-style mode of rpcgen. These features have implications with regard to the passing of void arguments. No arguments need be passed if their value is void.

Booleans

C has no built-in boolean type. However, the RPC library uses a boolean type called bool_t that is either TRUE or FALSE. Parameters declared as type bool in XDR language are compiled into bool_t in the output header file.

Example:

bool married; --> bool_t married;

Strings

The C language has no built-in string type, but instead uses the null-terminated char * convention. In C, strings are usually treated as null- terminated single-dimensional arrays.

In XDR language, strings are declared using the string keyword, and compiled into type char * in the output header file. The maximum size contained in the angle brackets specifies the maximum number of characters allowed in the strings (not counting the NULL character). The maximum size may be omitted, indicating a string of arbitrary length.

Examples:

string name<32>;   --> char *name;
string longname<>; --> char *longname;

Note -

NULL strings cannot be passed; however, a zero-length string (that is, just the terminator or NULL byte) can be passed.


Opaque Data

Opaque data is used in XDR to describe untyped data, that is, sequences of arbitrary bytes. It may be declared either as a fixed length or variable length array. Examples:

opaque diskblock[512]; --> char diskblock[512];
opaque filedata<1024>; --> struct {
                           u_int filedata_len;
                           char *filedata_val;
                     } filedata;

Voids

In a void declaration, the variable is not named. The declaration is just void and nothing else. Void declarations can only occur in two places: union definitions and program definitions (as the argument or result of a remote procedure, for example no arguments are passed.)