Download presentation
Presentation is loading. Please wait.
Published byOswin Norton Modified over 9 years ago
1
1 XDR External Data Representation Process A XDR Encode/Decode Transport Process B XDR Encode/Decode Transport
2
2 XDR as a case study n Sun RPC uses XDR. n A good example of a “layer”. n Interesting API. n Powerful paradigm for creation of complex data structures.
3
3 XDR n XDR provides a service associated with the OSI Presentation Layer. –Common data representation –Library (not part of the O.S.). –Easy to port to new architectures. –Independence from transport layer.
4
4 Data Conversion n Asymmetric Data Conversion –client always converts to the server’s data representation. n Symmetric Data Conversion –both client and server convert to some standard representation.
5
5 XDR Data Types n boolean n char n short n int n long n float n double n enumeration n structure n string n fixed length array (1-D) n variable length array (1-D) n union n opaque data
6
6 Implicit vs. Explicit Typing n Explicit Typing means that each piece of data includes information about the type. n Implicit typing means that the sender and receiver must agree on the order and type of all data. n XDR uses Implicit Typing
7
7 XDR Programming n Most XDR implementations are based on a buffer paradigm. n A buffer is allocated that is large enough to hold an entire message. n Individual data items are added to the buffer one at a time. n XDR buffers can be attached to a file, pipe, socket or memory.
8
8 Conversion Terminology n Converting from local representation to XDR representation is called Encoding. n Converting from XDR representation to local representation is called Decoding. Sender Receiver DECODE XDR ENCODE
9
9 XDR Buffer Creation n There are a number of buffer creation functions in the XDR library. –xdrmem_create »destination for encoding -or- source for decoding is a chunk of memory. –xdrstdio_create »destination for encoding -or- source for decoding is a file descriptor.
10
10 XDR filters n The XDR library includes an extensive set of filters that perform encoding/decoding operations. n Each XDR stream includes an attribute that determines the specific operation that will be performed by a filter (encoding or decoding).
11
11 XDR Filters n The filter to encode/decode an integer is called xdr_int: bool_t xdr_int( XDR *xdrs, int *ip); n the return value indicates success or failure. n xdrs is a pointer to an XDR stream n ip is a pointer to an integer.
12
12 xdr_int() n If the XDR stream operation is XDR_ENCODE int count; XDR *xstream; xdr_int(xstream, &count); will convert (encode) the value of count to the integer representation used by XDR (big- endian) and put the result on the XDR stream.
13
13 xdr_int() n If the XDR stream operation is XDR_DECODE int count; XDR *xstream; xdr_int(xstream, &count); will get an XDR integer from the stream, convert (decode) the value to local integer representation and put the result in count.
14
14 xdr_int() int count; xdr_int(xstream,&count); int count; xdr_int(xstream,&count); Source Encodes Destintation decodes
15
15 Complex Data Filters n The XDR library includes a set of filters designed to translate complex C data structures to and from XDR representation. n Many of these filters make use of the simpler filters to convert individual components.
16
16 xdr_array() n The xdr_array() filter provides support for encoding/decoding a variable length array. bool_t xdr_array( XDR *xdrs, char *arrp, u_int *sizep, u_int maxsize, u_int elsize, xdrproc_t elproc); sizep is a pointer to the size of the array. elsize is the size of each element (in bytes). elproc is a pointer to a function that can encode/decode individual array elements.
17
17 xdr_array() 0123 Source Array -> Source Array -> 0123 Destination Array -> Destination Array -> elproc()
18
18 Inside xdr_array() Inside xdr_array() xdr_int(xdrs,&sizep); for (i=0;i<sizep;i++) elproc(xdrs,arrp+elsize*i); encode/decode the number of elements in the array encode/decode each array element.
19
19 xdr_string() n the string conversion filter is a little different since XDR strings have a maximum size. bool_t xdr_string( XDR *xdrs, char *string, u_int maxsize);
20
20 Problem!! n We want to send an array of strings between processes. n What is the problem (using xdr_array)? n What is a possible solution?
21
21 Distributed Program Design n Communication-Oriented Design –Design protocol first. –Build programs that adhere to the protocol. n Application-Oriented Design –Build application(s). –Divide programs up and add communication protocols. TypicalSocketsApproach RPC
22
22 RPC Remote Procedure Call n Call a procedure (subroutine) that is running on another machine. n Issues: –identifying and accessing the remote procedure –parameters –return value
23
23 blah, blah, blah bar = foo(a,b); blah, blah, blah bar = foo(a,b); blah, blah, blah int foo(int x, int y ) { if (x>100) return(y-2); else if (x>10) return(y-x); else return(x+y); } int foo(int x, int y ) { if (x>100) return(y-2); else if (x>10) return(y-x); else return(x+y); } Client Server protocol
24
24 Sun RPC n There are a number of popular RPC specifications. n Sun RPC (ONC RPC) is widely used. n NFS (Network File System) is RPC based. n Rich set of support tools.
25
25 Sun RPC Organization Procedure 1 Procedure 2 Procedure 3 Shared Global Data Remote Program
26
26 Procedure Arguments n To reduce the complexity of interface specification Sun RPC includes support for a single argument to a remote procedure.* n Typically the single argument is a structure that contains a number of values. n * Newer versions can handle multiple args.
27
27 Procedure Identification n Each procedure is identified by: –Hostname (IP Address) –Program identifier (32 bit integer) –Procedure identifier (32 bit integer)
28
28 Procedure Identification n Each procedure is identified by: –Hostname (IP Address) –Program identifier (32 bit integer) –Procedure identifier (32 bit integer) –Program Version identifier »for testing and migration.
29
29 Program Identifiers n Each remote program has a unique ID. n Sun divided up the IDs: 0x00000000 - 0x1fffffff 0x20000000 - 0x3fffffff 0x40000000 - 0x5fffffff 0x60000000 - 0xffffffff Sun Sys Admin TransientReserved
30
30 Procedure Identifiers & Program Version Numbers n Procedure Identifiers usually start at 1 and are numbered sequentially n Version Numbers typically start at 1 and are numbered sequentially.
31
31 Iterative Server n Sun RPC specifies that at most one remote procedure within a program can be invoked at any given time. n If a 2nd procedure is called the caller blocks until the 1st procedure has completed. n This is useful for applications that may share data among procedures. n Example: database - to avoid insert/delete/modify collisions.
32
32 Communication Semantics n To act like a local procedure (exactly one invocation per call) - a reliable transport (TCP) is necessary. n Sun RPC does not support reliable call semantics. n At Least Once Semantics n Zero or More Semantics If the procedure returns No reply
33
33 Dynamic Port Mapping n Servers typically do not use well known protocol ports. n Clients known the Program ID (and host). n A port lookup service runs on each host that contains RPC servers. n RPC servers register themselves with the port mapper
34
34 The portmapper n Each system which will support RPC servers runs a port mapper server that provides a central registry for RPC services. n Servers tell the port mapper what services they offer. n Clients ask a remote port mapper for the port number corresponsing to Remote Program ID.
35
35 More on the portmapper n The portmapper is itself an RPC server! n The portmapper is available on a well- known port (111).
36
36 Sun RPC Programming n The RPC library is a collection of tools for automating the creation of RPC clients and servers. n RPC clients are processes that call remote procedures. n RPC servers are processes that include procedure(s) that can be called by clients.
37
37 RPC Programming n RPC library –XDR routines –RPC run time library »call rpc service »register with portmapper »dispatch incoming request to correct procedure –Program Generator
38
38 RPC Run-time Library n High- and Low-level functions that can be used by clients and servers. n High-level functions provide simple access to RPC services.
39
39 High-level Client Library int callrpc( char *host, u_long prognum, u_long versnum, u_long procnum, xdrproc_t inproc, char *in, xdrproc_t outproc, char *out);
40
40 High-Level Server Library int registerrpc( u_long prognum, u_long versnum, u_long procnum, char *(*procname)() xdrproc_t inproc, xdrproc_t outproc);
41
41 High-Level Server Library (cont.) void svc_run(); svc_run() is a dispatcher. svc_run() is a dispatcher. n A dispatcher waits for incoming connections and invokes the appropriate function to handle each incoming request.
42
42 High-Level Library Limitation n The High-Level RPC library calls support UDP only (no TCP). n You must use lower-level RPC library functions to use TCP. n The High-Level library calls do not support any kind of authentication.
43
43 Low-level RPC Library n Full control over all IPC options –TCP & UDP –Timeout values –Asynchronous procedure calls n Multi-tasking Servers n Broadcasting
44
44 RPCGEN n There is a tool for automating the creation of RPC clients and servers. n The program rpcgen does most of the work for you. n The input to rpcgen is a protocol definition in the form of a list of remote procedures and parameter types.
45
45 RPCGEN Input File rpcgen Client StubsXDR filtersheader fileServer skeleton C Source Code Protocol Description
46
46 rpcgen Output Files > rpcgen foo.x foo_clnt.c (client stubs) foo_svc.c (server main) foo_xdr.c (xdr filters) foo.h (shared header file)
47
47 Client Creation > gcc -o fooclient foomain.c foo_clnt.c foo_xdr.c n foomain.c is the client main() (and possibly other function) that call rpc services via the client stub functions in foo_clnt.c n The client stubs use the xdr functions.
48
48 Server Creation gcc -o fooserver fooservices.c foo_svc.c foo_xdr.c n fooservices.c contains the definitions of the actual remote procedures.
49
49 Example Protocol Definition struct twonums { int a; int b; }; program UIDPROG { version UIDVERS { int RGETUID(string ) = 1; string RGETLOGIN( int ) = 2; int RADD(twonums) = 3; } = 1; } = 0x20000001;
50
50
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.