Client/Server Distributed Systems

102
40-322 Cli/Serv.: rpc/10 Client/Server Distributed Syste Client/Server Distributed Syste ms ms Objectives Objectives look at how to program with SunOS RPCs look at how to program with SunOS RPCs use XDR and use XDR and rpcgen rpcgen briefly look at authentication briefly look at authentication 240-322, Semester 1, 2005-2006 10. Remote Procedure Calls (RPCs)

description

Client/Server Distributed Systems. 240-322, Semester 1, 2005-2006. Objectives look at how to program with SunOS RPCs use XDR and rpcgen briefly look at authentication. 10. Remote Procedure Calls (RPCs). Overview. 1. What is a RPC? 2. XDR (eXternal Data Representation) - PowerPoint PPT Presentation

Transcript of Client/Server Distributed Systems

240-322 Cli/Serv.: rpc/10 1

Client/Server Distributed SystemsClient/Server Distributed Systems

ObjectivesObjectives– look at how to program with SunOS RPCslook at how to program with SunOS RPCs– use XDR and use XDR and rpcgenrpcgen– briefly look at authenticationbriefly look at authentication

240-322, Semester 1, 2005-2006

10. Remote Procedure Calls (RPCs)

240-322 Cli/Serv.: rpc/10 2

OverviewOverview1. What is a RPC?1. What is a RPC?

2.2. XDR (eXternal Data Representation) XDR (eXternal Data Representation)

3.3. primes.cprimes.c Example Example

4.4. Networking Networking primes.cprimes.c

5.5. RPC Authentication RPC Authentication

6. Other RPC Features6. Other RPC Features

7. RPC Benefits7. RPC Benefits

8. More Information8. More Information

240-322 Cli/Serv.: rpc/10 3

1. What is a RPC?1. What is a RPC? A remote procedure call (RPC) is a call to a A remote procedure call (RPC) is a call to a

procedure/function located on another machine.procedure/function located on another machine.

1.1. Background1.1. Background1.2. Ordinary Procedure Calls1.2. Ordinary Procedure Calls1.3. Parts of a RPC1.3. Parts of a RPC1.4. The Good News1.4. The Good News1.5. What kind of RPC?1.5. What kind of RPC?1.6. Finding a Remote Procedure1.6. Finding a Remote Procedure

240-322 Cli/Serv.: rpc/10 4

1.1. Background1.1. Background

Client-server code using sockets is Client-server code using sockets is complicated to write and understandcomplicated to write and understand– must deal with networking must deal with networking andand application application

codingcoding

Is there an easier networking model?Is there an easier networking model?– i.e. one that hides or reduces the networking i.e. one that hides or reduces the networking

codingcoding

240-322 Cli/Serv.: rpc/10 5

1.2. Ordinary Procedure Calls1.2. Ordinary Procedure Calls

We can view a procedure/function call as a client-We can view a procedure/function call as a client-server communication on the same machine.server communication on the same machine.

main()

function

call return

client

server

240-322 Cli/Serv.: rpc/10 6

1.3. Parts of a RPC1.3. Parts of a RPC This communication can be spread across This communication can be spread across

two machines.two machines.

main()

client stub

XDR filters

network interface

client

call return

serverfunctionnetwork

interface

XDR filters

server wrapper

server

The Network

240-322 Cli/Serv.: rpc/10 7

The Client StubThe Client Stub The client stub is the client’s interface to the The client stub is the client’s interface to the

networking code.networking code.

It is called like the original function.It is called like the original function.

It converts the input arguments into network form, It converts the input arguments into network form, and sends them to the server.and sends them to the server.

It receives the server’s answer in network form, It receives the server’s answer in network form, converts it to ordinary data, and returns it to converts it to ordinary data, and returns it to main()main()..

240-322 Cli/Serv.: rpc/10 8

XDR FiltersXDR Filters

A set of functions for converting data into A set of functions for converting data into network form, and back again.network form, and back again.

XDR = eXternal Data RepresentationXDR = eXternal Data Representation

240-322 Cli/Serv.: rpc/10 9

Server WrapperServer Wrapper

The server wrapper receives data in network form, The server wrapper receives data in network form, calls its XDR filters to extract the original data, calls its XDR filters to extract the original data, and calls the server function with it.and calls the server function with it.

When the function returns, the wrapper converts When the function returns, the wrapper converts the result into network form, and sends it back to the result into network form, and sends it back to the client.the client.

240-322 Cli/Serv.: rpc/10 10

1.4. The Good News1.4. The Good News

A RPC tool, A RPC tool, rpcgenrpcgen, will generate the:, will generate the:– client stub, XDR filters, server wrapperclient stub, XDR filters, server wrapper

It will even suggest possible code for:It will even suggest possible code for:– client client main()main(), the server function, the server function

The programmer must supply The programmer must supply rpcgenrpcgen with the with the (XDR) data structures passed between (XDR) data structures passed between main()main() and the server function.and the server function.

240-322 Cli/Serv.: rpc/10 11

1.5. What kind of RPC?1.5. What kind of RPC?

The most popular one is probably Sun RPCThe most popular one is probably Sun RPC– Sun Microsystems invented itSun Microsystems invented it– also called ONC RPC (Open Network Computing)also called ONC RPC (Open Network Computing)– NIS, NFS built using Sun RPCNIS, NFS built using Sun RPC

more information in sections 3.3, 3.4 of Brownmore information in sections 3.3, 3.4 of Brown

continued

240-322 Cli/Serv.: rpc/10 12

Other RPC implementations:Other RPC implementations:– Courier RPC (from Xerox)Courier RPC (from Xerox)– NCS RPC (Network Computing SystemNCS RPC (Network Computing System

developed by Apollodeveloped by Apollo used in DCE (Distributed Computing used in DCE (Distributed Computing

Environment)Environment)

240-322 Cli/Serv.: rpc/10 13

1.6. Finding a Remote Procedure1.6. Finding a Remote Procedure

How does a client find the right server over How does a client find the right server over the network?the network?

In ordinary client-server code, the user must In ordinary client-server code, the user must supply a host name and a port number.supply a host name and a port number.

In RPC, the user only supplies a host name.In RPC, the user only supplies a host name.

continued

240-322 Cli/Serv.: rpc/10 14

The client asks a The client asks a portmapperportmapper on the host for on the host for the port of the server.the port of the server.– the portmapper holds a database of all RPC the portmapper holds a database of all RPC

services on its machineservices on its machine

On SunOS the portmapper is called On SunOS the portmapper is called rpcbindrpcbind– (sometimes) see (sometimes) see man rpcbindman rpcbind

continued

240-322 Cli/Serv.: rpc/10 15

This approach requires that each server This approach requires that each server registers itself with the portmapper when it registers itself with the portmapper when it first starts.first starts.

The server registers:The server registers:– { program number, version number, { program number, version number,

procedure/function number }procedure/function number }

continued

240-322 Cli/Serv.: rpc/10 16

Steps in RPC CommunicationSteps in RPC Communication

client serverport4: send data

portmapper(rpcbind)

port111

1: register details

2: request server details

3: Sendserver details,includingport.

240-322 Cli/Serv.: rpc/10 17

Using rpcinfoUsing rpcinfo

Look at the portmapper using Look at the portmapper using rpcinforpcinfo::

$ rpcinfo -p takasila program vers proto port 100000 2 tcp 111 portmapper 100000 2 udp 111 portmapper 100024 1 udp 918 status 100024 1 tcp 921 status 100003 2 udp 2049 nfs 100003 3 udp 2049 nfs : :

called from fivedots

often only rootcan use rpcinfo

240-322 Cli/Serv.: rpc/10 18

Points to NotePoints to Note The port numbers may change when the system The port numbers may change when the system

is rebooted.is rebooted.

A server can be registered with the portmapper A server can be registered with the portmapper for more than one protocol (UDP and TCP).for more than one protocol (UDP and TCP).

Some servers may have many versionsSome servers may have many versions– allows new code to be developed alongside old codeallows new code to be developed alongside old code

240-322 Cli/Serv.: rpc/10 19

Program NamesProgram Names

A program name is associated with a program A program name is associated with a program number in the host’s number in the host’s /etc/rpc/etc/rpc file: file:

portmapper 100000 portmap sunrpcportmapper 100000 portmap sunrpcrstatd 100001 rstat rstat_svc rup rstatd 100001 rstat rstat_svc rup rusersd 100002 rusersrusersd 100002 rusersnfs 100003 nfsprognfs 100003 nfsprog

:

The super-user can add new server details.The super-user can add new server details.

240-322 Cli/Serv.: rpc/10 20

Choosing a Program NumberChoosing a Program Number

A server must have a program number in the A server must have a program number in the range range 0x200000000x20000000 - - 0x3fffffff0x3fffffff

Other ranges:Other ranges:00 - - 1fffffff1fffffff for Sun Microsystemsfor Sun Microsystems2000000020000000 - - 3fffffff3fffffff user-defineduser-defined4000000040000000 - - 5fffffff5fffffff transienttransient6000000060000000 - - ffffffffffffffff reserved for future usereserved for future use

240-322 Cli/Serv.: rpc/10 21

2. XDR (eXternal Data Representation)2. XDR (eXternal Data Representation)

XDR is used to encode data in a network form XDR is used to encode data in a network form for communication between the client and for communication between the client and server. server. Why?Why?

AnswerAnswer: data is represented differently on : data is represented differently on different machines.different machines.– e.g. sending an integer (or array, or struct, etc.) e.g. sending an integer (or array, or struct, etc.)

directly from machine A to machine B is not directly from machine A to machine B is not usually possibleusually possible

240-322 Cli/Serv.: rpc/10 22

2.1. Machine Archtecture Problems2.1. Machine Archtecture Problems

a) Different byte ordersa) Different byte orders– big-endian:big-endian: 68000 family, SPARC68000 family, SPARC

– little-endian:little-endian:Intel, VAXIntel, VAX

b) Alignment rulesb) Alignment rules– e.g. all 32-bit integers must start at an address which is a e.g. all 32-bit integers must start at an address which is a

multiple of 4 so they align with the processor’s 32-bit multiple of 4 so they align with the processor’s 32-bit wordswords

– the compiler will generate ‘holes’ in the data structures the compiler will generate ‘holes’ in the data structures to get the correct alignmentto get the correct alignment

continued

240-322 Cli/Serv.: rpc/10 23

Structure Alignment ExampleStructure Alignment Examplestruct demo { char c; int i; long x;}

c i x

0 1 2 3 4 5 6 7 8 9 10 11

c i x

c i x

12

2-byte int, no align rule

4-byte int,2-byte align rule

4-byte int,4-bytealign rule

continued

240-322 Cli/Serv.: rpc/10 24

c) Programming Language Differencesc) Programming Language Differences– e.g. a Pascal string is stored with its lengthe.g. a Pascal string is stored with its length– e.g. a C 'string' is stored with a e.g. a C 'string' is stored with a '\0''\0'

d) Pointersd) Pointers– we cannot directly pass pointers between we cannot directly pass pointers between

machines since an address on one machine means machines since an address on one machine means nothing on anothernothing on another

– how do we transfer linked-lists, trees, stacks, etc.?how do we transfer linked-lists, trees, stacks, etc.?

240-322 Cli/Serv.: rpc/10 25

2.2. The XDR Data Description Lang.2.2. The XDR Data Description Lang.

XDR data types are very similar to C types, XDR data types are very similar to C types, butbut there are some differences. there are some differences.

Some XDR types:Some XDR types:– int, float (like C)int, float (like C)– arrays (fixed and arrays (fixed and variablevariable length) length)– stringsstrings– structs and unions (like C)structs and unions (like C)

240-322 Cli/Serv.: rpc/10 26

XDR Type ExamplesXDR Type Examples

int page_number;int page_number; same as Csame as C

const SIZE = 4;const SIZE = 4; like C’s like C’s #define#define

int week[7]int week[7]

– a a fixedfixed length array of 7 integers length array of 7 integers– the array must contain 7 values when it is the array must contain 7 values when it is

transferred over the networktransferred over the network

continued

240-322 Cli/Serv.: rpc/10 27

double density<50>double density<50>

– a a variablevariable length array of length array of at mostat most 50 doubles 50 doubles– the array can contain less than 50 values when the array can contain less than 50 values when

it is transferred over the networkit is transferred over the network

string name<50>string name<50>

– a a variablevariable length string of length string of at mostat most 50 chars 50 chars– there is no there is no char *char * in XDRin XDR

continued

240-322 Cli/Serv.: rpc/10 28

XDR can represent pointer data structures, XDR can represent pointer data structures, so long as they do not contain loopsso long as they do not contain loops– ok: lists, stacks, trees, queuesok: lists, stacks, trees, queues– not ok: doubly-linked lists, ringsnot ok: doubly-linked lists, rings

For other types, see Brown p.304-311For other types, see Brown p.304-311– alsoalso man xdrman xdr

240-322 Cli/Serv.: rpc/10 29

3. primes.c Example3. primes.c Example

This program prints out the primes in the This program prints out the primes in the range given by the user on the command line.range given by the user on the command line.

$ ./primes 1 65 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61$

No networked RPC yet; first get the stand-alone application working.

240-322 Cli/Serv.: rpc/10 30

Function Calls DiagramFunction Calls Diagram

main()

find_primes() report_results()

isprime()

callsrange rpinfo pi

All on one machine,in one program.

240-322 Cli/Serv.: rpc/10 31

primes.cprimes.c#include <stdio.h>#include <stdlib.h> /* for atoi() */

#define MAXPRI 1000 /* max no of primes */

/* I/O structures for find_primes() */struct range { /* range for search */ int min, max;};

struct pinfo { /* collected primes */ int primes[MAXPRI]; /* at most MAXPRI primes */ int num_primes; /* total number found */};

struct pinfo find_primes(struct range r);int isprime(int n);:

240-322 Cli/Serv.: rpc/10 32

int main(int argc, char *argv[]){ struct range r; struct pinfo pi;

if (argc != 3) { fprintf(stderr, "Usage: primes <min> <max>\n"); exit(1); } r.min = atoi(argv[1]); /* no error checking */ r.max = atoi(argv[2]);

pi = find_primes(r); report_results(pi);

return 0;}

240-322 Cli/Serv.: rpc/10 33

struct pinfo find_primes(struct range r)/* Collect primes between r.min and r.max */{ struct pinfo pi; int i;

if (r.min > r.max) pi.num_primes = -1; else { pi.num_primes = 0; for (i = r.min; i <= r.max; i++) if (isprime(i)) { if (pi.num_primes < MAXPRI) pi.primes[pi.num_primes] = i; pi.num_primes++; } } return pi;}

240-322 Cli/Serv.: rpc/10 34

int isprime(int n){ int i;

for (i = 2; i*i <= n; i++) if ((n % i) == 0) return 0; return 1;}

240-322 Cli/Serv.: rpc/10 35

void report_results(struct pinfo pi){ int i;

if (pi.num_primes == -1) fprintf(stderr, "range error\n"); else { if (pi.num_primes > MAXPRI) { fprintf(stderr, "Too many primes: %d\n", pi.num_primes); pi.num_primes = MAXPRI; } for (i = 0; i < pi.num_primes; i++) { printf("%5d", pi.primes[i]); if (((i+1)%10) == 0) putchar('\n'); } putchar('\n'); }}

240-322 Cli/Serv.: rpc/10 36

CommentsComments

The complex data structures are to make the The complex data structures are to make the conversion from a standalone to networked conversion from a standalone to networked RPC version easier.RPC version easier.

Coding strategy:Coding strategy:– get the application working first get the application working first

(generate primes);(generate primes);– then add in the network code (RPC)then add in the network code (RPC)

240-322 Cli/Serv.: rpc/10 37

4. Networking Primes4. Networking Primes Convert Convert primes.cprimes.c into a network application using RPC. into a network application using RPC.

find_primes()find_primes() (and (and isprime()isprime()) will become the remote ) will become the remote procedure.procedure.

Usually the choice depends on:Usually the choice depends on:– resource utilisationresource utilisation– balancing work against communication costsbalancing work against communication costs

240-322 Cli/Serv.: rpc/10 38

Function Calls DiagramFunction Calls Diagram

server(takasila)

client (fivedots)

main()

find_primes() report_results()

isprime()

range rpinfo pi

240-322 Cli/Serv.: rpc/10 39

RPC CommunicationRPC Communication

main()report_results()

client stub

XDR filters

network interface

client(fivedots)

call return

find_primes()and isprime()network

interface

XDR filters

server wrapper

server (takasila)

The Networkrange rpinfo pi

240-322 Cli/Serv.: rpc/10 40

4.1. The ‘.x’ Files4.1. The ‘.x’ Files The The ‘.x’‘.x’ file contains the XDR data types passed file contains the XDR data types passed

between between main()main() and and find_primes()find_primes()

$ rpcgen -C primes.x$ rpcgen -C primes.x-->--> primes.hprimes.h header file for C datatypes header file for C datatypes

involved in network comms. involved in network comms.primes_xdr.cprimes_xdr.c XDR filtersXDR filtersprimes_clnt.cprimes_clnt.c client stubclient stubprimes_svc.cprimes_svc.c server wrapper server wrapper

No need to even look at these No need to even look at these ‘.c’‘.c’ files files

continued

240-322 Cli/Serv.: rpc/10 41

$ rpcgen -a -C primes.x$ rpcgen -a -C primes.x

– all of the above, plus:all of the above, plus:primes_client.cprimes_client.c simple client simple client main()main()primes_server.cprimes_server.c simple server functionsimple server functionmakefile.primesmakefile.primes a makefilea makefile

– these files must these files must notnot already exist already exist

– the C files show how to use the C datatypes in the C files show how to use the C datatypes in primes.hprimes.h for network commicationfor network commication

– the programmer must add the application codethe programmer must add the application code

240-322 Cli/Serv.: rpc/10 42

Relationships between the FilesRelationships between the Files

primes_client.c

primes_clnt.c

primes_xdr.c

network interface

primes_ server.cnetwork

interface

primes_xdr.c

primes_svc.c

The Network

prim

es.h

prim

es.h

client (fivedots)

server (takasila)

240-322 Cli/Serv.: rpc/10 43

4.2. primes.x4.2. primes.x

This file contains the XDR data types This file contains the XDR data types andand the server information:the server information:

{ program number, version number, { program number, version number, function number }function number }

240-322 Cli/Serv.: rpc/10 44

const MAXPRI = 1000; /* max no. of primes */

struct range { /* range for search */ int min; int max;};

struct pinfo { /* collected primes */ int primes<MAXPRI>; /*at most MAXPRI primes */ int num_primes;};

program PRIMEPROG { /* server info. */ version PRIMEVERS { pinfo FIND_PRIMES(range) = 1; } = 1; /* the version number */} = 0x2000009a; /* the program number */

240-322 Cli/Serv.: rpc/10 45

NotesNotes

A restriction of standard RPC is that the remote A restriction of standard RPC is that the remote procedure can only take one input, and return procedure can only take one input, and return one output.one output.

pinfo FIND_PRIMES(range)pinfo FIND_PRIMES(range)

Program, version, and function names must be Program, version, and function names must be in uppercase.in uppercase.

primes<MAXPRI>primes<MAXPRI> is a variable length array. is a variable length array.

240-322 Cli/Serv.: rpc/10 46

4.3. primes.h4.3. primes.h This header file contains the C datatypes generated This header file contains the C datatypes generated

from the XDR datatypes.from the XDR datatypes.

They are used in They are used in primes_client.cprimes_client.c and and primes_server.cprimes_server.c for communication between for communication between main()main() and and find_primes()find_primes()..

primes.hprimes.h will contain 3 versions of the datatypes: will contain 3 versions of the datatypes:– K&R C, ANSI C, C++K&R C, ANSI C, C++

240-322 Cli/Serv.: rpc/10 47

ANSI C headers in primes.hANSI C headers in primes.h

#include <rpc/rpc.h>

#define MAXPRI 1000

struct range { int min; int max;};typedef struct range range;

extern bool_t xdr_range(XDR *, range*);:

For these slides, I've deleted the K&R C and C++ code

240-322 Cli/Serv.: rpc/10 48

struct pinfo { struct { u_int primes_len; /* the array size */ int *primes_val; /* pointer to array */ } primes; int num_primes;};typedef struct pinfo pinfo;

extern bool_t xdr_pinfo((XDR *, pinfo*);:

more complex version of pinfo structto do with variable length

240-322 Cli/Serv.: rpc/10 49

#define PRIMEPROG ((unsigned long)(0x2000009a))#define PRIMEVERS ((unsigned long)(1))#define FIND_PRIMES ((unsigned long)(1))

extern pinfo * find_primes_1(range *, CLIENT *);

extern pinfo * find_primes_1_svc(range *, struct svc_req *);

240-322 Cli/Serv.: rpc/10 50

NotesNotes

There are There are typedefstypedefs for for rangerange and and pinfopinfo..

struct pinfostruct pinfo is more complex: is more complex:– primes_valprimes_val is a pointer, so we must either is a pointer, so we must either

malloc space for it or make it point to an array at malloc space for it or make it point to an array at run timerun time

– primes_lenprimes_len must be assigned the length of the must be assigned the length of the arrayarray

continued

240-322 Cli/Serv.: rpc/10 51

xdr_range()xdr_range() and and xdr_pinfo()xdr_pinfo() are new XDR are new XDR filters for the new structsfilters for the new structs– see see primes_xdr.cprimes_xdr.c for their implementation for their implementation– they use the built-in XDR filters for ints and arraysthey use the built-in XDR filters for ints and arrays

The client The client main()main() program must call: program must call:find_primes_1()find_primes_1()

The server function must be:The server function must be:find_primes_1_svc()find_primes_1_svc()

240-322 Cli/Serv.: rpc/10 52

4.4. primes_client.c4.4. primes_client.c

primes_client.cprimes_client.c shows how to: shows how to:– obtain a connection to the serverobtain a connection to the server– how to call how to call find_primes_1()find_primes_1()

It contacts the server by calling It contacts the server by calling clnt_create()clnt_create()

– clnt_create()clnt_create() tries to contact the portmapper on tries to contact the portmapper on the specified host to get the server detailsthe specified host to get the server details

– it may fail, returning it may fail, returning NULLNULL

– if it succeeds, it returns aif it succeeds, it returns a client handleclient handle

240-322 Cli/Serv.: rpc/10 53

clnt = clnt_create(host, prog, version, protocol);

client handle (usedin other RPC libraryfunctions)

host where serverand portmapper arelocated

program and versionnumbers for the required server

communicationprotocol (usually“udp” or “tcp”).A detailed list can be found in /etc/netconfig

240-322 Cli/Serv.: rpc/10 54

primes_client.cprimes_client.c

#include "primes.h"

void primeprog_1(char* host){ CLIENT *clnt; pinfo *result_1; range find_primes_1_arg;

clnt = clnt_create(host, PRIMEPROG, PRIMEVERS, ”netpath");

:

check thisvalue

240-322 Cli/Serv.: rpc/10 55

if (clnt == NULL) { clnt_pcreateerror(host); exit(1); }

result_1 = find_primes_1(&find_primes_1_arg, clnt);

if (result_1 == NULL) { clnt_perror(clnt, "call failed:"); }

clnt_destroy( clnt );}

client-side versionof find_primes()

240-322 Cli/Serv.: rpc/10 56

main(int argc, char* argv[]){ char *host;

if(argc < 2) { printf("usage: %s server_host\n", argv[0]); exit(1); } host = argv[1]; primeprog_1(host);}

240-322 Cli/Serv.: rpc/10 57

NotesNotes clnt_create()clnt_create() uses uses ““netpathnetpath”” which is a default which is a default

setting that will cause the protocol to be decided setting that will cause the protocol to be decided at runtime (usually by looking in at runtime (usually by looking in /etc/netconfig/etc/netconfig))– often this string is changed to often this string is changed to ““udpudp”” or or ““tcptcp””

– seesee man rpcman rpc

clnt_perror()clnt_perror() and and clnt_destroy()clnt_destroy() are RPC are RPC library functions.library functions.

240-322 Cli/Serv.: rpc/10 58

primes_cli.cprimes_cli.c

My My primes_cli.cprimes_cli.c is built from: is built from:– primes_client.c primes_client.c – main()main() and and report_results()report_results() from from primes.cprimes.c

240-322 Cli/Serv.: rpc/10 59

primes_cli.c Codeprimes_cli.c Code

/* Based on primes_client.c and primes.c Usage: primes_client <host> <min> <max>*/

#include <stdio.h>#include <stdlib.h> /* for atoi() */#include "primes.h"

pinfo primeprog_1(char *host, range r);void report_results(pinfo pi);

:

240-322 Cli/Serv.: rpc/10 60

int main(int argc, char *argv[]){ range r; pinfo pi;

if (argc != 4) { fprintf(stderr, "Usage: primes_client

<host> <min> <max>\n"); exit(1); } r.min = atoi(argv[2]); /* no error checks */ r.max = atoi(argv[3]);

pi = primeprog_1(argv[1], r); report_results(pi);

return 0;}

240-322 Cli/Serv.: rpc/10 61

pinfo primeprog_1(char *host, range r){ CLIENT *clnt; pinfo *result_1, ans;

clnt = clnt_create(host, PRIMEPROG, PRIMEVERS,

"netpath"); if (clnt == NULL) { clnt_pcreateerror(host); exit(1); }

:

240-322 Cli/Serv.: rpc/10 62

ans.num_primes = -2; /* means no answer */

result_1 = find_primes_1(&r, clnt); if (result_1 == NULL) clnt_perror(clnt, "call failed:"); else ans = *result_1;

clnt_destroy(clnt);

return ans;}

240-322 Cli/Serv.: rpc/10 63

void report_results(pinfo pi){ int i;

if (pi.num_primes == -2) /* new error */ fprintf(stderr, "RPC error\n"); else if (pi.num_primes == -1) fprintf(stderr, "range error\n"); else { if (pi.num_primes > MAXPRI) { fprintf(stderr, "Too many primes: %d\n",

pi.num_primes); pi.num_primes = MAXPRI; }

:

Compare this function with theone in primes.c on slide 35

240-322 Cli/Serv.: rpc/10 64

for (i = 0; i < pi.num_primes; i++) { printf("%5d", pi.primes.primes_val[i]);

/* data structure change */ if (((i+1)%10) == 0) putchar('\n'); } putchar('\n'); }}

240-322 Cli/Serv.: rpc/10 65

NoteNote The code now uses the The code now uses the rangerange and and pinfopinfo data types data types

from from primes.hprimes.h

– requires a change to requires a change to report_results()report_results() to access the to access the primes_val[]primes_val[] array array

pi.num_primespi.num_primes is assigned -2 before the is assigned -2 before the find_primes_1()find_primes_1() call call– if it is the same afterwards then the networked function if it is the same afterwards then the networked function

was not called, meaning there was some kind of errorwas not called, meaning there was some kind of error

240-322 Cli/Serv.: rpc/10 66

4.5. primes_clnt.c4.5. primes_clnt.c

primes_clnt.cprimes_clnt.c is the client stub for network is the client stub for network communication with the servercommunication with the server– it calls the XDR filters for argument passingit calls the XDR filters for argument passing

– it sends/receives network data to/from the serverit sends/receives network data to/from the server

– sets a timeout for trying to contact the serversets a timeout for trying to contact the server

– all this is done by calling all this is done by calling clnt_call()clnt_call()

240-322 Cli/Serv.: rpc/10 67

4.6. Client Compilation4.6. Client Compilation

$ gcc -Wall primes_cli.c primes_clnt.c primes_xdr.c -o

primes_client

On On fivedotsfivedots, , gccgcc generates warnings for generates warnings for two type mismatches in two type mismatches in primes_clnt.cprimes_clnt.c

– ignore these warningsignore these warnings

onfivedots

240-322 Cli/Serv.: rpc/10 68

4.7. RPC Semantics4.7. RPC Semantics

An ordinary function is called and returns An ordinary function is called and returns (or exits)(or exits)– simple semanticssimple semantics

A RPC is really a network communication A RPC is really a network communication using UDP or TCPusing UDP or TCP– must consider network failure, and datagram must consider network failure, and datagram

loss/reorder when using UDPloss/reorder when using UDP– more complex semanticsmore complex semantics

240-322 Cli/Serv.: rpc/10 69

TCP Semantics for RPCTCP Semantics for RPC

TCP guarantees that a single RPC will cause the TCP guarantees that a single RPC will cause the remote function to be called 0 or 1 timesremote function to be called 0 or 1 times– the 0 means that the network and/or server is downthe 0 means that the network and/or server is down– very similar to the meaning of an ordinary function very similar to the meaning of an ordinary function

callcall

240-322 Cli/Serv.: rpc/10 70

UDP Semantics for RPCUDP Semantics for RPC

A single RPC using UDP may cause the remote A single RPC using UDP may cause the remote function to be called many times. function to be called many times. Why?Why?

The server is sent data, but this is sent again if there The server is sent data, but this is sent again if there is no reply after a short time.is no reply after a short time.

But the server may have received the data and be But the server may have received the data and be slow respondingslow responding– so the function can be called many timesso the function can be called many times

continued

240-322 Cli/Serv.: rpc/10 71

This behaviour is a problem if the function changes This behaviour is a problem if the function changes thingsthings– e.g. deduct 1000 Baht from my accounte.g. deduct 1000 Baht from my account– one client function call may result in several deductions one client function call may result in several deductions

on the server!on the server!

ConclusionConclusion: only use UDP to call remote functions : only use UDP to call remote functions that do not change thingsthat do not change things– e.g. list my bank account, generate primese.g. list my bank account, generate primes– called called idempotentidempotent functions functions

240-322 Cli/Serv.: rpc/10 72

Another UDP ProblemAnother UDP Problem

UDP in Sun RPC can only pass at most 8K UDP in Sun RPC can only pass at most 8K of data in a call/returnof data in a call/return

– this may be a problem in our code if we this may be a problem in our code if we generate too many primesgenerate too many primes

240-322 Cli/Serv.: rpc/10 73

4.8. primes_svc.c4.8. primes_svc.c The top-level of the server, which:The top-level of the server, which:

– registers its details with the portmapperregisters its details with the portmapper

– handles incoming messageshandles incoming messages converts XDR format data to Cconverts XDR format data to C calls calls find_primes_1_svc()find_primes_1_svc()

– this is the server-side version of this is the server-side version of find_primes()find_primes()

deals with “ping” callsdeals with “ping” calls

– sends results back to the clientsends results back to the client converts C data to XDR formatconverts C data to XDR format

240-322 Cli/Serv.: rpc/10 74

4.9. primes_server.c4.9. primes_server.c

This file contains This file contains find_primes_1_svc()find_primes_1_svc(). .

The application code needs to be added.The application code needs to be added.

240-322 Cli/Serv.: rpc/10 75

#include "primes.h"

pinfo *find_primes_1_svc(range *argp, struct svc_req *rqstp)

{ static pinfo result;

/* insert server-side version of find_primes() here */

return(&result);}

stores details used forclient authentication(see later)

240-322 Cli/Serv.: rpc/10 76

NotesNotes The The rangerange and and pinfopinfo data types are used from data types are used from primes.hprimes.h

resultresult must be static so that its memory is not must be static so that its memory is not deleted when the function returns.deleted when the function returns.

resultresult is retained so that the top-level server is retained so that the top-level server can convert it to network formcan convert it to network form– in in primeprog_1()primeprog_1() in in primes_svc.cprimes_svc.c

240-322 Cli/Serv.: rpc/10 77

4.10. primes_serv.c4.10. primes_serv.c

My My primes_serv.cprimes_serv.c is built from: is built from:– primes_server.cprimes_server.c

– find_primes()find_primes() and and isprime()isprime() from from primes.cprimes.c

240-322 Cli/Serv.: rpc/10 78

primes_serv.c Codeprimes_serv.c Code

/* Based on primes_server.c and primes.c */

#include "primes.h"

pinfo *find_primes_1_svc(range *rp, struct svc_req *rqstp);

int isprime(int n); /* same as in primes.c */:

240-322 Cli/Serv.: rpc/10 79

pinfo *find_primes_1_svc(range *rp, struct svc_req *rqstp)

{ static pinfo pi; static int parray[MAXPRI]; int i;

if (rp->min > rp->max) pi.num_primes = -1; else { pi.num_primes = 0; for (i = rp->min; i <= rp->max; i++) if (isprime(i)) { if (pi.num_primes < MAXPRI) parray[pi.num_primes] = i; pi.num_primes++; } }

:

240-322 Cli/Serv.: rpc/10 80

pi.primes.primes_len = (pi.num_primes < MAXPRI) ?

pi.num_primes : MAXPRI;

pi.primes.primes_val = parray; return(&pi);}

point primes_val to thestatic array parray

primes_len mustbe initialised

240-322 Cli/Serv.: rpc/10 81

4.11. Server Compilation & 4.11. Server Compilation & InvocationInvocation

$ gcc -Wall primes_svc.c primes_serv.c primes_xdr.c -o primes_server

$ ./primes_server & /* on takasila */1 [19339]

$ ps -a | grep primes

No “&” needed in some OSes. The code is run in the background.Drawback: the process is not shown with ps.

ontakasila

240-322 Cli/Serv.: rpc/10 82

4.12. Is the server registered?4.12. Is the server registered?

$ rpcinfo -p takasila program vers proto port 100000 2 tcp 111 portmapper 100000 2 udp 111 portmapper : : 536871066 1 udp 36991 536871066 1 tcp 55441

• No name, but 536871066 is decimal for 0x2000009a• The super-user can add a name to /etc/rpc

240-322 Cli/Serv.: rpc/10 83

Is the server alive?Is the server alive?

$ rpcinfo -t takasila 536871066 1program 536871066 version 1 ready and waiting

$ rpcinfo -u takasila 536871066 1program 536871066 version 1 ready and waiting

$ rpcinfo -t takasila 536871066 2rpcinfo: RPC: Program/version mismatch;

low version = 1, high version = 1program 536871066 version 2 is not available

ping the protocols and versions

240-322 Cli/Serv.: rpc/10 84

4.13. Run the Client4.13. Run the Client

$ ./primes_client takasila 1 500 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499$

on fivedots

240-322 Cli/Serv.: rpc/10 85

4.14 Killing the server4.14 Killing the server

$ kill -9 19339$ rpcinfo -d 536871066 1

$ rpcinfo -t takasila 536871066 1rpcinfo: RPC: Program not registeredprogram 536871066 version 1 is not available

$ rpcinfo -p takasila | grep 536871066$

Very important.Always kill your serverwhen you’ve finished with it (if possible).

on takasila

240-322 Cli/Serv.: rpc/10 86

5. RPC Authentication5. RPC Authentication

primes_serverprimes_server does not know anything about its does not know anything about its clientsclients– does not know the user’s UID, the client’s machine, does not know the user’s UID, the client’s machine,

etc.etc.

– without this information, there is no way to control without this information, there is no way to control access to the serveraccess to the server

240-322 Cli/Serv.: rpc/10 87

Credentials & VerifiersCredentials & Verifiers Authentication is based on credentials and verifiers.Authentication is based on credentials and verifiers.

A A credentialcredential: information about the client: information about the client– e.g. user ID, machine detailse.g. user ID, machine details– can be fakedcan be faked

A A verifierverifier: information about the client which : information about the client which cannot be forgedcannot be forged– e.g. the user’s photo, a passworde.g. the user’s photo, a password

240-322 Cli/Serv.: rpc/10 88

5.1. RPC Authentication ‘flavours’5.1. RPC Authentication ‘flavours’

There are 4 main types (‘flavours’) of authentication:There are 4 main types (‘flavours’) of authentication:

– AUTH_NONEAUTH_NONE none (the default)none (the default)

– AUTH_SYSAUTH_SYS or or AUTH_UNIXAUTH_UNIX

send credentials but no verifiersend credentials but no verifier(credentials can be faked)(credentials can be faked)

continued

240-322 Cli/Serv.: rpc/10 89

– AUTH_DESAUTH_DES send credentials and a verifiersend credentials and a verifierbased on DES encryptedbased on DES encryptedtimestampstimestamps

– AUTH_KERBAUTH_KERB Kerberos style encryption ofKerberos style encryption ofcommunicationcommunication

See See <rpc/auth.h><rpc/auth.h>

means /usr/include/rpc/auth.h

240-322 Cli/Serv.: rpc/10 90

Kerberos (simply)Kerberos (simply)

session keyclient name: C

ticket =

ticketsession key

client C’s key

message =

Key Distribution Center (KDC)

continued

Client C“want to use

server S”1

message 2Decrypt message:get session keyand ticket. 3

server S’s key

240-322 Cli/Serv.: rpc/10 91

Client C Server S

4

Decrypt ticket:get session keyand name of client, C

5ticket

start communicatingusing the session keyto encrypt messages

6

message

session key

240-322 Cli/Serv.: rpc/10 92

5.2. Using 5.2. Using AUTH_UNIXAUTH_UNIX Authentication Authentication

The client must attach an authentication structure The client must attach an authentication structure to the handle returned by to the handle returned by clnt_create()clnt_create()::

clnt = clnt_create(...);clnt = clnt_create(...);clnt->cl_auth = authunix_create_default();clnt->cl_auth = authunix_create_default();

::

continued

Would appear in the client-side code;e.g. in primes_cli.c , slide 60

240-322 Cli/Serv.: rpc/10 93

The server can access this information via The server can access this information via the the rqstprqstp argument of the server function.argument of the server function.

For example:For example:pinfo *find_primes_1_svc(range *r,pinfo *find_primes_1_svc(range *r,

struct svc_req struct svc_req **rqstprqstp)){ ... }{ ... }

240-322 Cli/Serv.: rpc/10 94

svc_req Structuresvc_req Structure

struct svc_req { u_long rq_prog; /* service program no. */ u_long rq_vers; /* service protocol no. */ u_long rq_proc; /* service procedure no. */ struct opaque_auth rq_cred;

/* authentication type */ caddr_t rq_clntcred; /* authentication info. */ SVCXPRT *rq_xprt; /* transport info. */};

More details can be found in <rpc/svc.h>

240-322 Cli/Serv.: rpc/10 95

Using rqstpUsing rqstp

Get the authentication flavour by reading Get the authentication flavour by reading rq_cred.oa_flavorrq_cred.oa_flavor (see (see <rpc/auth.h><rpc/auth.h>))

If the flavour is If the flavour is AUTH_UNIXAUTH_UNIX, then cast , then cast rq_clntcredrq_clntcred to to (struct authunix_parms *)(struct authunix_parms *) and read its details.and read its details.

240-322 Cli/Serv.: rpc/10 96

authunix_parms Structureauthunix_parms Structure

struct authunix_parms {struct authunix_parms { u_long aup_time; u_long aup_time; char *aup_machname; /* char *aup_machname; /* client host nameclient host name */ */ uid_t aup_uid; uid_t aup_uid; /* /* client UIDclient UID */ */ gid_t aup_gid; gid_t aup_gid; /* client group ID */ /* client group ID */ u_int aup_len; u_int aup_len; /* length of group list */ /* length of group list */ gid_t *aup_gids; gid_t *aup_gids; /* group list */ /* group list */};};

More (or less) details can be found in More (or less) details can be found in <rpc/auth_unix.h><rpc/auth_unix.h>

240-322 Cli/Serv.: rpc/10 97

ExampleExample

pinfo *find_primes_1_svc(range *rp,struct svc_req *rqstp)

{ struct authunix_parms *ucred;

printf(“flavour = %d\n”, rqstp->rq_cred.oa_flavor);

if (rqstp->rq_cred.oa_flavor == AUTH_UNIX) { ucred = (struct authunix_parms *)

(rqstp->rq_clntcred); printf(“host = %s\n”, ucred->aup_machname); printf(“uid = %d\n”, ucred->aup_uid); printf(“gid = %d\n”, ucred->aup_gid); }

: /* continue with primes calculation */

240-322 Cli/Serv.: rpc/10 98

NoteNote

This example only prints the user’s details. This example only prints the user’s details. We must add more code to restrict accessWe must add more code to restrict access– e.g. do not allow certain users and/or machines e.g. do not allow certain users and/or machines

to do primes calculationsto do primes calculations

For an example, see Brown p.329-337.For an example, see Brown p.329-337.

240-322 Cli/Serv.: rpc/10 99

6. Other RPC features6. Other RPC features

rpcgenrpcgen generates RPC code which ‘mimics’ a generates RPC code which ‘mimics’ a function call over a network.function call over a network.

If we code with the RPC library functions If we code with the RPC library functions directly, then we can implement other directly, then we can implement other behaviours.behaviours.

continued

240-322 Cli/Serv.: rpc/10 100

Examples:Examples:– write servers that register multiple versions of a write servers that register multiple versions of a

functionfunction– implement our own authentication schemesimplement our own authentication schemes– broadcast to serversbroadcast to servers– use use asynchronous RPCsasynchronous RPCs

the client does not wait for an answerthe client does not wait for an answer the server can reply by calling a function on the server can reply by calling a function on

the client side (a the client side (a callback RPCcallback RPC))

240-322 Cli/Serv.: rpc/10 101

7. RPC Benefits7. RPC Benefits

The parts of the client/server code that the The parts of the client/server code that the programmer writes are programmer writes are more high-levelmore high-level– closer to function calls; less networkingcloser to function calls; less networking

The XDR language is an The XDR language is an easier way of easier way of communicatingcommunicating between clients and servers than between clients and servers than using streams of bytes/charactersusing streams of bytes/characters– can have complex data structures;can have complex data structures;

hides machine differenceshides machine differences

Supports Supports authenticationauthentication..

240-322 Cli/Serv.: rpc/10 102

8. More Information8. More Information

Practical UNIX ProgrammingPractical UNIX ProgrammingRobbins and RobbinsRobbins and RobbinsPrentice Hall 1996, chapter 14Prentice Hall 1996, chapter 14

UNIX Distributed ProgrammingUNIX Distributed ProgrammingChris BrownChris BrownPrentice Hall, 1994, chapter 6Prentice Hall, 1994, chapter 6

Power Programming with RPCPower Programming with RPCJohn BloomerJohn BloomerO’Reilly, 1992O’Reilly, 1992

in the PSUlibrary

in our library