Sun RPC - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Sun RPC

Description:

Sun RPC – PowerPoint PPT presentation

Number of Views:576
Avg rating:3.0/5.0
Slides: 20
Provided by: r335
Category:
Tags: rpc | argv | sun

less

Transcript and Presenter's Notes

Title: Sun RPC


1
Sun RPC
  • Ref Stevens chpt. 16

2
Outline
  • Sun RPC
  • Application Interface
  • Pointers
  • Multithreading
  • XDR

3
Sun RPC
  • Another example of remote procedure call
    mechanism
  • Implements RPC between hosts
  • You will get some practice using this in project
    3
  • Language independent RPC
  • Uses a separate IDL called XDR
  • Same name (XDR) used for data format (External
    Data Representation)
  • A simple example
  • RPC specification file (foo.x)
  • processed by compiler (rpcgen) to generate stub
    code
  • Client program
  • Server program

4
RPC Specification File
  • / procedure to square a number /
  • / file square.x /
  • struct square_in / input (argument) /
  • long arg1
  • struct square_out / output (result) /
  • long res1
  • program SQUARE_PROG
  • version SQUARE_VERS
  • square_out SQUAREPROC(square_in) 1 / proc
    1 /
  • 1 / version /
  • 0x31230000 / program number-identifies
    service /

5
Program Numbers
  • rpcinfo -p shows the programs currently
    registered on the system
  • Program number conventions
  • 0x00000000 - 0x1fffffff defined by Sun
  • 0x20000000 - 0x3fffffff defined by user (you)
  • 0x40000000 - 0x5fffffff transient
  • 0x60000000 - 0xffffffff reserved

6
IDL Compilation
  • The XDR compiler is called rpcgen
  • rpcgen -C square.x
  • -C forces ANSI C prototypes to be output
  • Creates header files, code for client and server
    stub, main routine for server program

7
Example Client Program
  • include"unpipc.h / our header /
  • include"square.h / generated by rpcgen /
  • int main(int argc, char argv)
  • CLIENT cl / Client handle /
  • square_in in
  • square_out outp
  • if (argc ! 3)
  • err_quit("usage client lthostnamegt
    ltinteger-valuegt")
  • / obtain handle to client procedure /
  • cl Clnt_create(argv1,SQUARE_PROG,SQUARE_VERS,
    "tcp")
  • in.arg1 atol(argv2)
  • / invoke procedure RPC runtime allocates
    result struct /
  • / SQUAREPROC for version 1 becomes
    squareproc_1 /
  • if ((outp squareproc_1(in, cl)) NULL)
  • err_quit("s", clnt_sperror(cl, argv1))
  • / RPC parameter includes a pointer /
  • printf("result ld\n", outp-gtres1)
  • exit(0)

8
Sun RPC Pointers
  • Issue Call by reference?
  • Yes! Pointers are serialized
  • RPC runtime on client side will traverse data
    structure, pack it into a message
  • Server side receives message, unpacks it,
    recreates data structure, passes pointers to
    server procedure
  • Server may modify data structures to return
    values to client

9
Example Server Program
  • include"unpipc.h / our header /
  • include"square.h / generated by rpcgen /
  • / rpcgen program generates main() on server side
    /
  • square_out
  • squareproc_1_svc(square_in inp, struct svc_req
    rqstp)
  • static square_out out / cannot be automatic
    variable /
  • out.res1 inp-gtarg1 inp-gtarg1
  • return(out)
  • Procedure name append _svc to distinguish
    client procedure from server procedure
  • Second parameter provides info on this invocation
    (ignore for now)
  • Storage for return result allocated by server

10
Multithreading
  • Above server code not thread safe need to
    eliminate static variable
  • To make it thread safe, use call by reference
    (avoid server allocating storage to hold return
    result)
  • By default, server not multithreaded (one thread
    handles all client requests)
  • Use -M flag in rpcgen to ensure it creates thread
    safe code

11
Multithreaded Version(Client Procedure)
  • include"unpipc.h / our header /
  • include"square.h / generated by rpcgen /
  • int main(int argc, char argv)
  • CLIENT cl / Client handle /
  • square_in in
  • square_out outp / storage for return result
    here /
  • if (argc ! 3)
  • err_quit("usage client lthostnamegt
    ltinteger-valuegt")
  • cl Clnt_create(argv1,SQUARE_PROG,SQUARE_VERS,
    "tcp")
  • in.arg1 atol(argv2)
  • if (squareproc_2(in, out, cl) ! RPC_SUCCESS)
  • err_quit("s", clnt_sperror(cl, argv1))
  • printf("result ld\n", outp-gtres1)
  • exit(0)

12
Multithreaded Version of Server
  • include"unpipc.h / our header /
  • include"square.h / generated by rpcgen /
  • / rpcgen program generates main() on server side
    /
  • bool_t
  • squareproc_2_svc(square_in inp, square_out
    outp, struct svc_req rqstp)
  • / static square_out out /
  • out-gtres1 inp-gtarg1 inp-gtarg1
  • return(TRUE)

13
Cleaning Up Memory
  • Alternatively, server procedure might allocate
    memory to hold result need a way to clean this
    up
  • Also, server stub may have allocated memory need
    to clean this up too
  • Must define procedure square_prog_2_freeresult
    for this purpose
  • Called by server stub after (1) server procedure
    returns, and (2) result has been sent back to
    client
  • Data that have been dynamically allocated by the
    XDR code (stubs) are deallocated using xdr_free
  • See example in handout

14
External Data Representation(XDR)
  • XDR used as IDL
  • Language for describing data (and procedure
    definitions)
  • XDR also refers to the rules for encoding data
  • XDR uses implicit encoding
  • Both sender and receiver know types and ordering
    of data (Sun RPC client and server have same
    header files)
  • Data converted to a canonical representation at
    sender, decoded at receiver to desired format
  • As opposed to explicit encoding, where tags are
    used in the data itself to provide type
    information

15
XDR Encoding Rules
  • Rules
  • All data types are encoded in multiples of 4
    bytes
  • Data represented Big Endian (same as Sun
    machines) on the wire
  • 2s complement integers
  • IEEE format for floating point values
  • Example a 5-character ASCII string represented
    as
  • 4 bytes for the length (value 5)
  • 5 bytes for the characters
  • 3 bytes padding

16
XDR Data Types
/ procedure to square a number / / file
square.x / struct square_in / input
(argument) / long arg1 struct square_out
/ output (result) / long res1 program
SQUARE_PROG version SQUARE_VERS square_out
SQUAREPROC(square_in) 1 / proc 1 / 1
/ version / 0x31230000 / program
number-identifies service /
  • Specification file (.x) includes data type
    specifications (as well as procedure specs)
  • These data types must be mapped to C/C
    datatypes
  • So stubs, client, server code can
    generate/interpret data
  • In general, mapping required for whatever
    language IDL compiler supports
  • Type checking of the RPC runtime different from C
    type checking (e.g., it will enforce maximum
    string lengths)
  • Prevents buffer overruns (security risk)

17
Example XDR Data Types
See Figure 16.14 Stevens for full list
18
Example
/ Please do not edit this file. It was
generated using rpcgen. / ifndef
_TEST_H_RPCGEN define _TEST_H_RPCGEN define
RPCGEN_VERSION 199506 include
ltrpc/rpc.hgt define foo 1000 struct square_in
long foo1 char foo2200
struct u_int foo3_len
char foo3_val foo3
int foo4400 struct
u_int foo5_len int foo5_val
foo5 typedef struct square_in
square_in ifdef __cplusplus extern "C" bool_t
xdr_square_in(XDR , square_in) elif
__STDC__ extern bool_t xdr_square_in(XDR ,
square_in) else / Old Style C / bool_t
xdr_square_in() endif / Old Style C / endif
/ !_TEST_H_RPCGEN /
const foo 1000 struct square_in long
foo1 opaque foo2200 opaque foo3lt300gt
int foo4400 int foo5lt500gt
rpcgen
test.x
rpcgen creates xdr_square_in() in test_xdr.c to
encode/decode data (marshal/unmarshal data)
test.h
19
Other Aspects
  • Binding how to connect to a server that
    implements the required service
  • User needs to know which machine has the service
    (specified in Clnt_create)
  • Daemon running on machine that knows about
    available services on that machine
  • Use TCP for project - UDP cannot transfer
    arbitrarily large amounts of data (e.g.,
    compressed JPEG images)
  • Timeouts (TCP)
  • TCP provides reliable delivery (retransmits is
    packet is lost)
  • Client can set total timeout time client waits
    for server reply set via clnt_control
Write a Comment
User Comments (0)
About PowerShow.com