2

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.

Rohit
  • 604
  • 1
  • 10
  • 25

1 Answers1

3

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

Rohit
  • 604
  • 1
  • 10
  • 25
kofemann
  • 4,217
  • 1
  • 34
  • 39
  • The link seems to be useful but this doesn't answer my question. My question is **gm_protocol.x** doesn't contain any specification as `program STLEN {` but still rpcgen can create **gm_protocol_xdr.c** file which contain functions. According to "Unix Network Programming Vol 2 by Richard Stevens": **The RPC specification (RFC 1831) says that the RPC language, sometimes called RPCL, is identical to the XDR language (which is defined in RFC 1832), except for the addition of a program definition (which describes the program, versions, and procedures).** So, How are these functions created here? – Rohit Oct 29 '14 at 08:47
  • 1
    rpcgen will generate an encoder/decoder function for any struct and enum defined in the .x file.The XDR encoded message is an array of bytes and you need to convert them into corresponding types. – kofemann Oct 29 '14 at 08:56
  • 1
    This was what I thought earlier. So, this means that there is no need of putting `program STRLEN {` in the **.x** file to generate encoder/decoder functions. +1 for this confirmation. I'd edit your answer and add what makes the answer as complete. Thanks. – Rohit Oct 29 '14 at 09:31