Search code examples
crpcxdrsunrpc

Understanding XDR specification to create a *.x file


I have been looking through several websites on the Internet and actually wanted to know how do we write specification in a .x file to generate equivalent functions in .c file for RPC. Every site I visited suggested to use following kind of specification in *.x file:

program ADD_PROG { 
    version ADD_VERS { 
        int ADD(intpair) = 1; 
    } = 1; 
} = 0x23451111;

So, to understand practically, I obtained gm_protocol.x from an open-source project known as ganglia and generated equivalent C source code (gm_protocol_xdr.c) and C header file (gm_protocol.h) using rpcgen.

[rohit@ganglia-server rpc]$ rpcgen -C gm_protocol.x 
[rohit@ganglia-server rpc]$ 
[rohit@ganglia-server rpc]$ ll
total 24
-rw-rw-r-- 1 rohit rohit 5786 Oct 28 17:52 gm_protocol.h
-rw-rw-r-- 1 rohit rohit 3485 Oct 28 15:04 gm_protocol.x
-rw-rw-r-- 1 rohit rohit 8213 Oct 28 17:52 gm_protocol_xdr.c

To my surprise, from what I have learned and understood, gm_protocol.x doesn't contain any such RPC specifications shown as code above but still it can generate too many functions in the file gm_protocol_xdr.c.

It is pretty much sure that I am not able to understand the XDR specifications because either I have consulted wrong sources or they are outdated. I could not find any tutorial which could explain the way to generate functions (Although I have found specifications to generate struct, enum, union, etc).

Please help to learn these specifications.


Solution

  • Typically, from .x file you generate three 'groups' of code: message xdr encoder/decoder functions, client stub and server stub ( well, you can do it by hand as well, but it's too much work to get it right ). Run rpcgen with -a option too generate client, server sthus and an example implementation. Try to use a simple example first:

    program STRLEN {
      version STRLENVERS {
        int strlen(string) = 1;
      } = 1;
    } = 117;
    

    The specification program is a part of RPCL but XDR language. If one puts above specifications in a test.x file and run it using rpcgen -C test.x, then he/she would just get

    test.h, test_svc.c, test_clnt.c

    If one doesn't need any server or client stubs and just need encoder and decoder functions of XDR then every specifications such as enum, struct, union, etc in the file gm_protocol.x would be generated into their equivalent C based declarations in gm_protocol.h and their corresponding XDR encoder and decoder functions would be generated in gm_protocol_xdr.c, which is the case specified in the question.

    Running rpcgen -a gm_protocol.x would generate gm_protocol_svc.c and gm_protocol_clnt.c without any functions.

    Below are some XDR specifications:

    enter image description here

    Check the old SUN docu http://www.shrubbery.net/solaris9ab/SUNWdev/ONCDG/toc.html