Just as there was a need to describe the XDR data types in a formal language, there is also need to describe the procedures that operate on these XDR data types in a formal language as well. The RPC Language, an extension to the XDR language, serves this purpose. The following example is used to describe the essence of the language.
Example B-4 shows the specification of a simple ping program.
/* * Simple ping program */ program PING_PROG { version PING_VERS_PINGBACK { void PINGPROC_NULL(void) = 0; /* * ping the caller, return the round-trip time * in milliseconds. Return a minus one (-1) if * operation times-out */ int PINGPROC_PINGBACK(void) = 1; /* void - above is an argument to the call */ } = 2; /* * Original version */ version PING_VERS_ORIG { void PINGPROC_NULL(void) = 0; } = 1; } = 200000; const PING_VERS = 2; /* latest version */ |
The first version described is PING_VERS_PINGBACK with two procedures, PINGPROC_NULL and PINGPROC_PINGBACK.
PINGPROC_NULL takes no arguments and returns no results, but it is useful for such things as computing round-trip times from the client to the server and back again. By convention, procedure 0 of any RPC program should have the same semantics, and never require authentication.
The second procedure returns the amount of time (in microseconds) that the operation used.
The next version, PING_VERS_ORIG, is the original version of the protocol and it does not contain PINGPROC_PINGBACK procedure. It is useful for compatibility with old client programs, and as this program matures it may be dropped from the protocol entirely.
The RPC language (RPCL) 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.
The RPC language is identical to the XDR language, except for the added definitions described in Table B-2.
Table B-2 RPC Language Definitions
Term |
Definition |
---|---|
program program-ident {version-list} = value |
|
version; version; version-list |
|
version version-ident {procedure-list} = value |
|
procedure; procedure; procedure-list |
|
type-ident procedure-ident (type-ident) = value |
The following keywords are added and cannot be used as identifiers: program version.
Neither version name nor a version number can occur more than once within the scope of a program definition.
Neither a procedure name nor a procedure number can occur more than once within the scope of a version definition.
Program identifiers are in the same name space as constant and type identifiers.
Only unsigned constants can be assigned to programs, versions, and procedures.
RPC/XDR enumerations have the same 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;
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
XDR typedef
s have the same syntax as C typedef
s.
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;
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 are just like simple C declarations:
simple-declaration: type-ident variable-ident
Example:
colortype color; --> colortype color;
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 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 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;
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.
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
.
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
There are several exceptions to the RPC language rules.
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.
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;
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;
NULL strings cannot be passed; however, a zero-length string (that is, just the terminator or NULL byte) can be passed.
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;
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.)
rpcbind maps RPC program and version numbers to universal addresses, thus making dynamic binding of remote programs possible.
rpcbind is bound to a well-known address of each supported transport, and other programs register their dynamically allocated transport addresses with it. rpcbind then makes those addresses publicly available. Universal addresses are string representations of the transport-dependent address. They are defined by the addressing authority of the given transport.
rpcbind also aids in broadcast RPC. RPC programs will have different addresses on different machines, so there is no way to broadcast directly to all these programs. rpcbind, however, has a well-known address. So, to broadcast to a given program, the client actually sends its message to the rpcbind process on the machine it chooses to reach. rpcbind picks up the broadcast and calls the local service specified by the client. When rpcbind gets a reply from the local service, it passes the reply on to the client. )
rpcbind is contacted by way of an assigned address specific to the transport being used. For TCP/IP and UDP/IP, for example, it is port number 111. Each transport has such an assigned well known address. The following is a description of each of the procedures supported by rpcbind.
This procedure does no work. By convention, procedure zero of any program takes no parameters and returns no results.
When a program first becomes available on a machine, it registers itself with the rpcbind program running on the same machine. The program passes its program number prog; version number vers; network identifier netid; and the universal address uaddr; on which it awaits service requests.
The procedure returns a Boolean response with the value TRUE if the procedure successfully established the mapping and FALSE otherwise. The procedure refuses to establish a mapping if one already exists for the ordered set (prog, vers, netid).
Note that neither netid nor uaddr can be NULL, and that netid should be a valid network identifier on the machine making the call.
When a program becomes unavailable, it should unregister itself with the rpcbind program on the same machine.
The parameters and results have meanings identical to those of RPCBPROC_SET. The mapping of the (prog, vers, netid) tuple with uaddr is deleted.
If netid is NULL, all mappings specified by the ordered set (prog, vers, *) and the corresponding universal addresses are deleted. Only the owner of the service or the super-user is allowed to unset a service.
Given a program number prog, version number vers, and network identifier netid, this procedure returns the universal address on which the program is awaiting call requests.
The netid field of the argument is ignored and the netid is inferred from the netid of the transport on which the request came in.
This procedure lists all entries in rpcbind's database.
The procedure takes no parameters and returns a list of program, version, netid, and universal addresses. Call this procedure using a stream rather than a datagram transport to avoid the return of a large amount of data.
This procedure allows a caller to call another remote procedure on the same machine without knowing the remote procedure's universal address. It is intended for supporting broadcasts to arbitrary remote programs via rpcbind's universal address.
The parameters prog, vers, proc, and the args_ptr are the program number, version number, procedure number, and parameters of the remote procedure.
This procedure sends a response only if the procedure was successfully executed, and is silent (no response) otherwise.
The procedure returns the remote program's universal address, and the results of the remote procedure.
This procedure returns the local time on its own machine in seconds since midnight of January 1, 1970.
This procedure converts universal addresses to transport (netbuf) addresses. RPCBPROC_UADDR2TADDR is equivalent to uaddr2taddr(). See thenetdir(3NSL) man page. Only processes that cannot link to the name-to-address library modules should use RPCBPROC_UADDR2TADDR.
This procedure converts transport (netbuf) addresses to universal addresses. RPCBPROC_TADDR2UADDR is equivalent to taddr2uaddr(). See thenetdir(3NSL) man page. Only processes that can not link to the name-to-address library modules should use RPCBPROC_TADDR2UADDR.
Version 4 of the rpcbind protocol includes all of the above procedures, and adds several others.
This procedure is identical to the version 3 RPCBPROC_CALLIT procedure. The new name indicates that the procedure should be used for broadcast RPCs only. RPCBPROC_INDIRECT, defined in the following text, should be used for indirect RPC calls.
This procedure is similar to RPCBPROC_GETADDR. The difference is the r_vers field of the rpcb structure can be used to specify the version of interest. If that version is not registered, no address is returned.
This procedure is similar to RPCBPROC_CALLIT. Instead of being silent about errors (such as the program not being registered on the system), this procedure returns an indication of the error. This procedure should not be used for broadcast RPC. It is intended to be used with indirect RPC calls only.
This procedure returns a list of addresses for the given rpcb entry. The client may be able use the results to determine alternate transports that it can use to communicate with the server.
This procedure returns statistics on the activity of the rpcbind server. The information lists the number and kind of requests the server has received.
All procedures except RPCBPROC_SET and RPCBPROC_UNSET can be called by clients running on a machine other than a machine on which rpcbind is running. rpcbind accepts only RPCPROC_SET and RPCPROC_UNSET requests on the loopback transport.