Skip to content
This repository was archived by the owner on Nov 16, 2020. It is now read-only.

Commit d6eee34

Browse files
authored
Add files via upload
1 parent de9b655 commit d6eee34

File tree

22 files changed

+1033
-0
lines changed

22 files changed

+1033
-0
lines changed

rls/Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CROSS_COMPILE=
2+
CC=$(CROSS_COMPILE)gcc
3+
4+
all: xdr server client
5+
6+
xdr:
7+
rpcgen dir.x
8+
server:
9+
$(CC) dir_xdr.c dir_svc.c dir_proc.c -lnsl -o dir_svc
10+
client:
11+
$(CC) dir_xdr.c dir_clnt.c rls.c -lnsl -o rls
12+
clean:
13+
rm -f *.o dir_svc rls

rls/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
3+
## Example : Passing Complex Data Structures
4+
from https://docs.oracle.com/cd/E19683-01/816-1435/rpcgenpguide-21470/index.html
5+
6+
#### dir.x
7+
8+
```
9+
const MAXNAMELEN = 255; /* max length of directory
10+
entry */
11+
typedef string nametype<MAXNAMELEN>; /* director entry */
12+
typedef struct namenode *namelist; /* link in the listing */
13+
14+
/* A node in the directory listing */
15+
struct namenode {
16+
nametype name; /* name of directory entry */
17+
namelist next; /* next entry */
18+
};
19+
20+
union readdir_res switch (int errno) {
21+
case 0:
22+
namelist list; /* no error: return directory listing */
23+
default:
24+
void; /* error occurred: nothing else to return */
25+
};
26+
27+
program DIRPROG {
28+
version DIRVERS {
29+
readdir_res READDIR(nametype) = 1;
30+
} = 1;
31+
} = 0x20000076;
32+
```
33+
34+
#### Server
35+
36+
> $ rpcgen dir.x
37+
> $ gcc dir_svc.c dir_proc.c dir_xdr.c -o dir_svc -lnsl
38+
> $ dir_svc &
39+
40+
#### Client
41+
42+
> $ rpcgen dir.x
43+
> $ gcc rls.c dir_clnt.c dir_xdr.c -o rls -lnsl
44+
45+
####
46+
> $ rls "127.0.0.1" /usr/share/lib
47+
> ascii
48+
> eqnchar
49+
> ...
50+
> tabs4
51+
> local
52+
> $
53+

rls/dir.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Please do not edit this file.
3+
* It was generated using rpcgen.
4+
*/
5+
6+
#ifndef _DIR_H_RPCGEN
7+
#define _DIR_H_RPCGEN
8+
9+
#include <rpc/rpc.h>
10+
11+
12+
#ifdef __cplusplus
13+
extern "C" {
14+
#endif
15+
16+
#define MAXNAMELEN 255
17+
18+
typedef char *nametype;
19+
20+
typedef struct namenode *namelist;
21+
22+
struct namenode {
23+
nametype name;
24+
namelist next;
25+
};
26+
typedef struct namenode namenode;
27+
28+
struct readdir_res {
29+
int errno;
30+
union {
31+
namelist list;
32+
} readdir_res_u;
33+
};
34+
typedef struct readdir_res readdir_res;
35+
36+
#define DIRPROG 0x20000076
37+
#define DIRVERS 1
38+
39+
#if defined(__STDC__) || defined(__cplusplus)
40+
#define READDIR 1
41+
extern readdir_res * readdir_1(nametype *, CLIENT *);
42+
extern readdir_res * readdir_1_svc(nametype *, struct svc_req *);
43+
extern int dirprog_1_freeresult (SVCXPRT *, xdrproc_t, caddr_t);
44+
45+
#else /* K&R C */
46+
#define READDIR 1
47+
extern readdir_res * readdir_1();
48+
extern readdir_res * readdir_1_svc();
49+
extern int dirprog_1_freeresult ();
50+
#endif /* K&R C */
51+
52+
/* the xdr functions */
53+
54+
#if defined(__STDC__) || defined(__cplusplus)
55+
extern bool_t xdr_nametype (XDR *, nametype*);
56+
extern bool_t xdr_namelist (XDR *, namelist*);
57+
extern bool_t xdr_namenode (XDR *, namenode*);
58+
extern bool_t xdr_readdir_res (XDR *, readdir_res*);
59+
60+
#else /* K&R C */
61+
extern bool_t xdr_nametype ();
62+
extern bool_t xdr_namelist ();
63+
extern bool_t xdr_namenode ();
64+
extern bool_t xdr_readdir_res ();
65+
66+
#endif /* K&R C */
67+
68+
#ifdef __cplusplus
69+
}
70+
#endif
71+
72+
#endif /* !_DIR_H_RPCGEN */

rls/dir.x

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* dir.x: Remote directory listing protocol
3+
*/
4+
5+
const MAXNAMELEN = 255;/* max length of directory entry */
6+
typedef string nametype<MAXNAMELEN>;/* director entry */
7+
typedef struct namenode *namelist;/* link in the listing */
8+
9+
/* A node in the directory listing */
10+
struct namenode {
11+
nametype name;/* name of directory entry */
12+
namelist next;/* next entry */
13+
};
14+
15+
16+
union readdir_res switch (int errno) {
17+
case 0:
18+
namelist list;/* no error: return directory listing */
19+
default:
20+
void;/* error occurred: nothing else to return */
21+
};
22+
/* The directory program definition */
23+
program DIRPROG {
24+
version DIRVERS {
25+
readdir_res READDIR(nametype) = 1;
26+
} = 1;
27+
} = 0x20000076;

rls/dir_clnt.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Please do not edit this file.
3+
* It was generated using rpcgen.
4+
*/
5+
6+
#include <memory.h> /* for memset */
7+
#include "dir.h"
8+
9+
/* Default timeout can be changed using clnt_control() */
10+
static struct timeval TIMEOUT = { 25, 0 };
11+
12+
readdir_res *
13+
readdir_1(nametype *argp, CLIENT *clnt)
14+
{
15+
static readdir_res clnt_res;
16+
17+
memset((char *)&clnt_res, 0, sizeof(clnt_res));
18+
if (clnt_call (clnt, READDIR,
19+
(xdrproc_t) xdr_nametype, (caddr_t) argp,
20+
(xdrproc_t) xdr_readdir_res, (caddr_t) &clnt_res,
21+
TIMEOUT) != RPC_SUCCESS) {
22+
return (NULL);
23+
}
24+
return (&clnt_res);
25+
}

rls/dir_proc.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* dir_proc.c: remote readdir
3+
* implementation
4+
*/
5+
#include <dirent.h>
6+
#include "dir.h" /* Created by rpcgen */
7+
8+
9+
readdir_res *
10+
readdir_1_svc(dirname, req)
11+
nametype *dirname;
12+
struct svc_req *req;
13+
14+
{
15+
DIR *dirp;
16+
struct dirent *d;
17+
namelist nl;
18+
namelist *nlp;
19+
static readdir_res res; /* must be static! */
20+
21+
/* Open directory */
22+
dirp = opendir(*dirname);
23+
if (dirp == (DIR *)NULL) {
24+
res.errno = -1;
25+
return (&res);
26+
}
27+
/* Free previous result */
28+
xdr_free(xdr_readdir_res, &res);
29+
/*
30+
* Collect directory entries.
31+
* Memory allocated here is free by
32+
* xdr_free the next time readdir_1
33+
* is called
34+
*/
35+
nlp = &res.readdir_res_u.list;
36+
while (d = readdir(dirp)) {
37+
nl = *nlp = (namenode *) malloc(sizeof(namenode));
38+
if (nl == (namenode *) NULL) {
39+
res.errno = -1;
40+
closedir(dirp);
41+
return(&res);
42+
}
43+
nl->name = strdup(d->d_name);
44+
nlp = &nl->next;
45+
}
46+
*nlp = (namelist)NULL;
47+
/* Return the result */
48+
res.errno = 0;
49+
closedir(dirp);
50+
return (&res);
51+
}

rls/dir_svc.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Please do not edit this file.
3+
* It was generated using rpcgen.
4+
*/
5+
6+
#include "dir.h"
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
#include <rpc/pmap_clnt.h>
10+
#include <string.h>
11+
#include <memory.h>
12+
#include <sys/socket.h>
13+
#include <netinet/in.h>
14+
15+
#ifndef SIG_PF
16+
#define SIG_PF void(*)(int)
17+
#endif
18+
19+
static void
20+
dirprog_1(struct svc_req *rqstp, register SVCXPRT *transp)
21+
{
22+
union {
23+
nametype readdir_1_arg;
24+
} argument;
25+
char *result;
26+
xdrproc_t _xdr_argument, _xdr_result;
27+
char *(*local)(char *, struct svc_req *);
28+
29+
switch (rqstp->rq_proc) {
30+
case NULLPROC:
31+
(void) svc_sendreply (transp, (xdrproc_t) xdr_void, (char *)NULL);
32+
return;
33+
34+
case READDIR:
35+
_xdr_argument = (xdrproc_t) xdr_nametype;
36+
_xdr_result = (xdrproc_t) xdr_readdir_res;
37+
local = (char *(*)(char *, struct svc_req *)) readdir_1_svc;
38+
break;
39+
40+
default:
41+
svcerr_noproc (transp);
42+
return;
43+
}
44+
memset ((char *)&argument, 0, sizeof (argument));
45+
if (!svc_getargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
46+
svcerr_decode (transp);
47+
return;
48+
}
49+
result = (*local)((char *)&argument, rqstp);
50+
if (result != NULL && !svc_sendreply(transp, (xdrproc_t) _xdr_result, result)) {
51+
svcerr_systemerr (transp);
52+
}
53+
if (!svc_freeargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
54+
fprintf (stderr, "%s", "unable to free arguments");
55+
exit (1);
56+
}
57+
return;
58+
}
59+
60+
int
61+
main (int argc, char **argv)
62+
{
63+
register SVCXPRT *transp;
64+
65+
pmap_unset (DIRPROG, DIRVERS);
66+
67+
transp = svcudp_create(RPC_ANYSOCK);
68+
if (transp == NULL) {
69+
fprintf (stderr, "%s", "cannot create udp service.");
70+
exit(1);
71+
}
72+
if (!svc_register(transp, DIRPROG, DIRVERS, dirprog_1, IPPROTO_UDP)) {
73+
fprintf (stderr, "%s", "unable to register (DIRPROG, DIRVERS, udp).");
74+
exit(1);
75+
}
76+
77+
transp = svctcp_create(RPC_ANYSOCK, 0, 0);
78+
if (transp == NULL) {
79+
fprintf (stderr, "%s", "cannot create tcp service.");
80+
exit(1);
81+
}
82+
if (!svc_register(transp, DIRPROG, DIRVERS, dirprog_1, IPPROTO_TCP)) {
83+
fprintf (stderr, "%s", "unable to register (DIRPROG, DIRVERS, tcp).");
84+
exit(1);
85+
}
86+
87+
svc_run ();
88+
fprintf (stderr, "%s", "svc_run returned");
89+
exit (1);
90+
/* NOTREACHED */
91+
}

rls/dir_xdr.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Please do not edit this file.
3+
* It was generated using rpcgen.
4+
*/
5+
6+
#include "dir.h"
7+
8+
bool_t
9+
xdr_nametype (XDR *xdrs, nametype *objp)
10+
{
11+
register int32_t *buf;
12+
13+
if (!xdr_string (xdrs, objp, MAXNAMELEN))
14+
return FALSE;
15+
return TRUE;
16+
}
17+
18+
bool_t
19+
xdr_namelist (XDR *xdrs, namelist *objp)
20+
{
21+
register int32_t *buf;
22+
23+
if (!xdr_pointer (xdrs, (char **)objp, sizeof (struct namenode), (xdrproc_t) xdr_namenode))
24+
return FALSE;
25+
return TRUE;
26+
}
27+
28+
bool_t
29+
xdr_namenode (XDR *xdrs, namenode *objp)
30+
{
31+
register int32_t *buf;
32+
33+
if (!xdr_nametype (xdrs, &objp->name))
34+
return FALSE;
35+
if (!xdr_namelist (xdrs, &objp->next))
36+
return FALSE;
37+
return TRUE;
38+
}
39+
40+
bool_t
41+
xdr_readdir_res (XDR *xdrs, readdir_res *objp)
42+
{
43+
register int32_t *buf;
44+
45+
if (!xdr_int (xdrs, &objp->errno))
46+
return FALSE;
47+
switch (objp->errno) {
48+
case 0:
49+
if (!xdr_namelist (xdrs, &objp->readdir_res_u.list))
50+
return FALSE;
51+
break;
52+
default:
53+
break;
54+
}
55+
return TRUE;
56+
}

0 commit comments

Comments
 (0)