Top Banner
Netprog 2001 - RPC Prog ramming 1 RPC Programming with RPC Programming with rpcgen rpcgen Issues: Issues: Protocol Definition File Protocol Definition File Client Programming Client Programming Creating an "RPC Handle" to a Creating an "RPC Handle" to a server server Calling client stubs Calling client stubs Server Programming Server Programming Writing Remote Procedures Writing Remote Procedures
39

RPC Programming with rpcgen

Dec 30, 2015

Download

Documents

bruce-irwin

RPC Programming with rpcgen. Issues: Protocol Definition File Client Programming Creating an "RPC Handle" to a server Calling client stubs Server Programming Writing Remote Procedures. Protocol Definition File. Description of the interface of the remote procedures. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

1

RPC Programming with RPC Programming with rpcgenrpcgen

Issues:Issues:– Protocol Definition FileProtocol Definition File– Client ProgrammingClient Programming

• Creating an "RPC Handle" to a serverCreating an "RPC Handle" to a server• Calling client stubsCalling client stubs

– Server ProgrammingServer Programming• Writing Remote ProceduresWriting Remote Procedures

Page 2: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

2

Protocol Definition FileProtocol Definition File

Description of theDescription of the interface interface of the remote of the remote procedures.procedures.– Almost function prototypesAlmost function prototypes

Definition of any data structures used in Definition of any data structures used in the calls (argument types & return types)the calls (argument types & return types)

Can also include shared C code (shared Can also include shared C code (shared by client and server).by client and server).

Page 3: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

3

XDR the languageXDR the language

Remember that XDR data types are not Remember that XDR data types are not C data types!C data types!– There is a There is a mappingmapping from XDR types to C from XDR types to C

types – that's most of what rpcgen does.types – that's most of what rpcgen does.

Most of the XDR syntax is just like CMost of the XDR syntax is just like C– Arrays, strings are different.Arrays, strings are different.

Page 4: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

4

XDR ArraysXDR Arrays

Fixed LengthFixed Length arrays look just like C arrays look just like C code:code:

int foo[100]int foo[100] Variable LengthVariable Length arrays look like this: arrays look like this:

int foo<> int foo<> oror int foo<MAXSIZE> int foo<MAXSIZE>

Implicit maximum size is 232-1

Page 5: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

5

What gets sent on the networkWhat gets sent on the network

int x[n]int x[n]

x0 x1

int y<m>int y<m>

xn-1x2 . . .

y0 y1 . . .k

k is actual array sizek m

y2 yk

Page 6: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

6

XDR String TypeXDR String Type

Look like variable length arrays:Look like variable length arrays:

string s<100>string s<100> What is sent: length followed by What is sent: length followed by

sequence of ASCII chars:sequence of ASCII chars:

. . .n s0s1s2s3 Sn-1

n is actual string length (sent as int)

Page 7: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

7

Linked Lists!Linked Lists!struct foo {struct foo { int x;int x; foo *next;foo *next;}}

The generated XDR filter uses The generated XDR filter uses xdr_pointer()xdr_pointer() to encode/decode the to encode/decode the stuff stuff pointed to by a pointer.pointed to by a pointer.

Check the online example "linkedlist".Check the online example "linkedlist".

rpcgen recognizes this as a linked list

Page 8: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

8

Declaring The ProgramDeclaring The Program

programprogram SIMP_PROGSIMP_PROG { {

versionversion SIMP_VERSIONSIMP_VERSION { {

type1 type1 PROC1PROC1(operands1) = 1;(operands1) = 1;

type2 type2 PROC2PROC2(operands2) = 2;(operands2) = 2;

} = 1;} = 1;

} = 40000000;} = 40000000;

Keywords Generated Symbolic Constants

Used to generate stub and procedure names

Color Code:

Page 9: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

9

Procedure NumbersProcedure Numbers

Procedure #0 is created for you Procedure #0 is created for you automatically.automatically.– Start at procedure #1!Start at procedure #1!

Procedure #0 is a dummy procedure Procedure #0 is a dummy procedure that can help debug things (sortof an that can help debug things (sortof an RPC ping server).RPC ping server).

Page 10: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

10

Procedure NamesProcedure NamesRpcgen converts to lower case and Rpcgen converts to lower case and

prepends underscore and version number:prepends underscore and version number:rtype PROCNAME(arg)rtype PROCNAME(arg)

Client stub:Client stub:rtype *proc_1(arg *, CLIENT *);rtype *proc_1(arg *, CLIENT *);

Server procedure: Server procedure: rtype *proc_1_svc(arg *, rtype *proc_1_svc(arg *, struct svc_req *);struct svc_req *);

Page 11: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

11

Program NumbersProgram Numbers

Use something like:Use something like:555555555 or 22222222555555555 or 22222222

You can find the numbers currently You can find the numbers currently used with "rpcinfo –p hostname"used with "rpcinfo –p hostname"

Page 12: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

12

Client ProgrammingClient Programming

Create RPC Create RPC handle. handle. – Establishes the address of the server.Establishes the address of the server.

RPC handle is passed to client stubs RPC handle is passed to client stubs (generated by rpcgen).(generated by rpcgen).

Type is CLIENT *Type is CLIENT *

Page 13: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

13

clnt_createclnt_create

CLIENT *clnt_create(CLIENT *clnt_create(

char *host,char *host,

u_long prog, u_long prog,

u_long vers,u_long vers,

char *proto);char *proto);

Hostname of server

Program number

Version number

Can be "tcp" or "udp"

Page 14: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

14

Calling Client StubsCalling Client Stubs

Remember:Remember:– Return value is a pointer to what you Return value is a pointer to what you

expect.expect.– Argument is passed as a pointer.Argument is passed as a pointer.– If you are passing a If you are passing a stringstring, you must pass , you must pass

a a char**char** When in doubt – look at the ".h" file When in doubt – look at the ".h" file

generated by rpcgengenerated by rpcgen

Page 15: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

15

Server ProceduresServer Procedures

Rpcgen writes most of the server.Rpcgen writes most of the server. You need to provide the actual remote You need to provide the actual remote

procedures.procedures. Look in the ".h" file for prototypes.Look in the ".h" file for prototypes. Run "Run "rpcgen –C –Ssrpcgen –C –Ss" to generate " to generate

(empty) remote procedures!(empty) remote procedures!

Page 16: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

16

Server Function NamesServer Function Names

Old Style (includes AIX): Remote Old Style (includes AIX): Remote procedure FOO, version 1 is named procedure FOO, version 1 is named foo_1()foo_1()

New Style (includes Sun,BSD,Linux): New Style (includes Sun,BSD,Linux): Remote procedure FOO, version 1 is Remote procedure FOO, version 1 is named named foo_1_svc()foo_1_svc()

Page 17: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

17

Running rpcgenRunning rpcgen

Command line options vary from one Command line options vary from one OS to another.OS to another.

Sun/BSD/Linux – you need to use "-C" Sun/BSD/Linux – you need to use "-C" to get ANSI C code!to get ANSI C code!

Rpcgen can help write the files you Rpcgen can help write the files you need to write:need to write:– To generate sample server code: "-Ss"To generate sample server code: "-Ss"– To generate sample client code: "-Sc"To generate sample client code: "-Sc"

Page 18: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

18

Other porting issuesOther porting issues

Shared header file generated by rpcgen Shared header file generated by rpcgen may have: may have: #include <rpc/rpc.h>#include <rpc/rpc.h>

Or Not!Or Not!

Page 19: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

19

RPC without rpcgenRPC without rpcgen

Can do asynchronous RPCCan do asynchronous RPC– CallbacksCallbacks– Single process is both client and server.Single process is both client and server.

Write your own dispatcher (and provide Write your own dispatcher (and provide concurrency)concurrency)

Can establish control over many Can establish control over many network parameters: protocols, network parameters: protocols, timeouts, resends, etc.timeouts, resends, etc.

Page 20: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

20

rpcinforpcinforpcinfo –p hostrpcinfo –p host prints a list of all prints a list of all

registered programs on host.registered programs on host.

rpcinfo –[ut] host program# rpcinfo –[ut] host program# makes a call to procedure #0 of the makes a call to procedure #0 of the specified RPC program (RPC ping).specified RPC program (RPC ping).

u : UDPt : TCP

Page 21: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

21

Sample CodeSample Code

simple – integer add and subtractsimple – integer add and subtract ulookup – look up username and uid.ulookup – look up username and uid. varray – variable length array example.varray – variable length array example. linkedlist – arg is linked list.linkedlist – arg is linked list. rpctalk – chat programrpctalk – chat program

– doesn't work anymore :(doesn't work anymore :(

Page 22: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

22

Example simpExample simp

Standalone program simp.cStandalone program simp.c– Takes 2 integers from command line and Takes 2 integers from command line and

prints out the sum and difference.prints out the sum and difference.– Functions:Functions:

int add( int x, int y );int add( int x, int y );

int subtract( int x, int y );int subtract( int x, int y );

Page 23: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

23

Splitting simp.cSplitting simp.c

Move the functions Move the functions add()add() and and subtract()subtract() to the server.to the server.

Change simp.c to be an RPC clientChange simp.c to be an RPC client– Calls stubs Calls stubs add_1()add_1() , , subtract_1()subtract_1()

Create server that serves up 2 remote Create server that serves up 2 remote procedures procedures – add_1_svc()add_1_svc() and and subtract_1_svc()subtract_1_svc()

Page 24: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

24

Protocol Definition: Protocol Definition: simp.xsimp.xstruct operands {struct operands { int x;int x; int y;int y;};};

program SIMP_PROG {program SIMP_PROG { version SIMP_VERSION {version SIMP_VERSION { int ADD(operands) = 1;int ADD(operands) = 1; int SUB(operands) = 2;int SUB(operands) = 2; } = VERSION_NUMBER;} = VERSION_NUMBER;} = 555555555;} = 555555555;

Page 25: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

25

rpcgen –C simp.xrpcgen –C simp.xsimp.x

rpcgen

simp_clnt.csimp_clnt.c

simp_xdr.csimp_xdr.csimp.hsimp.h

simp_svc.csimp_svc.cClient Stubs

XDR filtersheader file

Server skeleton

Page 26: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

26

xdr_operandsxdr_operands XDR filter XDR filterbool_t xdr_operands( XDR *xdrs, operands *objp){

if (!xdr_int(xdrs, &objp->x))

return (FALSE);

if (!xdr_int(xdrs, &objp->y))

return (FALSE);

return (TRUE);

}

Page 27: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

27

simpclient.csimpclient.c

This was the main program – is now the This was the main program – is now the client.client.

Reads 2 ints from the command line.Reads 2 ints from the command line. Creates a RPC handle.Creates a RPC handle. Calls the remote add and subtract Calls the remote add and subtract

procedures.procedures. Prints the results. Prints the results.

Page 28: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

28

simpservice.csimpservice.c

The server main is in The server main is in simp_svc.csimp_svc.c.. simpservice.csimpservice.c is what we write – it is what we write – it

holds the add and subtract procedures holds the add and subtract procedures that that simp_svcsimp_svc will call when it gets will call when it gets RPC requests.RPC requests.

The only thing you need to do is to The only thing you need to do is to match the name/parameters that match the name/parameters that simp_svcsimp_svc expects (check expects (check simp.hsimp.h!).!).

Page 29: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

29

Userlookup programUserlookup program

Provide access to passwd database via Provide access to passwd database via remote procedures:remote procedures:–getpwnam getpwnam BYNAMEBYNAME–getpwuid getpwuid BYNUMBYNUM

Unix library functions Remote Procedures

Page 30: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

30

userlookup.xuserlookup.x

typedef string username<10>;typedef string username<10>;

program ULKUP_PROG {program ULKUP_PROG {

version ULKUP_VERSION {version ULKUP_VERSION {

int byname(username) = 1;int byname(username) = 1;

username bynum(int) = 2;username bynum(int) = 2;

} = 1;} = 1;

} = 555555556;} = 555555556;

Page 31: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

31

Problem with userlookupProblem with userlookup

It's hard to tell if there are errors:It's hard to tell if there are errors:– What if there is no user with the name What if there is no user with the name

passed to passed to byname()byname() ? ?

– What if the username passed is not a valid What if the username passed is not a valid username?username?

Page 32: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

32

Better Better userlookup.huserlookup.h

%#define NOTFOUND 0%#define NOTFOUND 0%#define FOUND 1%#define FOUND 1

typedef string username<10>;typedef string username<10>;

struct uname_retval {struct uname_retval { int found; int found;

username name;username name;};};

Page 33: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

33

Better Better userlookup.h userlookup.h (cont.)(cont.)struct uid_retval {struct uid_retval {

int found; int found; int uid; int uid; };};

program ULKUP_PROG {program ULKUP_PROG { version ULKUP_VERSION {version ULKUP_VERSION { uid_retval byname(username) = 1;uid_retval byname(username) = 1; uname_retval bynum(int) = 2;uname_retval bynum(int) = 2; } = 1;} = 1;} = 555555556;} = 555555556;

Page 34: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

34

Varray exampleVarray example

Variable length array (determined at run Variable length array (determined at run time).time).

Remote procedure that returns the sum Remote procedure that returns the sum of the elements in an array of int.of the elements in an array of int.

Page 35: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

35

varray.xvarray.x

typedef int iarray<>;typedef int iarray<>;

program VADD_PROG {program VADD_PROG { version VADD_VERSION {version VADD_VERSION { int VADD(iarray) = 1;int VADD(iarray) = 1; } = 1;} = 1;} = 555575555;} = 555575555;

Page 36: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

36

iarrayiarray

typedef struct {

u_int iarray_len;

int *iarray_val;

} iarray;

typedef int iarray<>;

rpcgenrpcgen

Page 37: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

37

vadd_1_svc()vadd_1_svc()

int * vadd_1_svc(iarray *argp, struct svc_req *rqstp) { static int result; int i; result=0; for (i=0;i<argp->iarray_len;i++) result += argp->iarray_val[i];

return (&result);}

Page 38: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

38

linkedlistlinkedlist

Linked list of int.Linked list of int.

Remote procedure computes sum of Remote procedure computes sum of the integers in the list.the integers in the list.

Page 39: RPC Programming with  rpcgen

Netprog 2001 - RPC Programming

39

ll.xll.xstruct foo {struct foo { int x;int x; foo *next;foo *next;};};

program LL_PROG {program LL_PROG { version LL_VERSION {version LL_VERSION { int SUM(foo) = 1;int SUM(foo) = 1; } = VERSION_NUMBER;} = VERSION_NUMBER;} = 555553555;} = 555553555;