aboutsummaryrefslogtreecommitdiff
path: root/include/sunrpc.h
blob: 4f108aa0bf3d2efd39f02f44c0145a06b42f4fcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
 * Sun RPC; see RFC 1057
 */

/*
#pragma lib "libsunrpc.a"
#pragma src "/sys/src/libsunrpc"
*/

typedef uchar u1int;

typedef struct SunAuthInfo SunAuthInfo;
typedef struct SunAuthUnix SunAuthUnix;
typedef struct SunRpc SunRpc;
typedef struct SunCall SunCall;

enum
{
	/* Authinfo.flavor */
	SunAuthNone = 0,
	SunAuthSys,
	SunAuthShort,
	SunAuthDes,
};

typedef enum {
	SunAcceptError = 0x10000,
	SunRejectError = 0x20000,
	SunAuthError = 0x40000,

	/* Reply.status */
	SunSuccess = 0,

	SunProgUnavail = SunAcceptError | 1,
	SunProgMismatch,
	SunProcUnavail,
	SunGarbageArgs,
	SunSystemErr,

	SunRpcMismatch = SunRejectError | 0,

	SunAuthBadCred = SunAuthError | 1,
	SunAuthRejectedCred,
	SunAuthBadVerf,
	SunAuthRejectedVerf,
	SunAuthTooWeak,
	SunAuthInvalidResp,
	SunAuthFailed,
} SunStatus;

struct SunAuthInfo
{
	uint flavor;
	uchar *data;
	uint ndata;
};

struct SunAuthUnix
{
	u32int stamp;
	char *sysname;
	u32int uid;
	u32int gid;
	u32int g[16];
	u32int ng;
};

struct SunRpc
{
	u32int xid;
	uint iscall;

	/*
	 * only sent on wire in call
	 * caller fills in for the reply unpackers.
	 */
	u32int proc;

	/* call */
	// uint proc;
	u32int prog, vers;
	SunAuthInfo cred;
	SunAuthInfo verf;
	uchar *data;
	uint ndata;

	/* reply */
	u32int status;
	// SunAuthInfo verf;
	u32int low, high;
	// uchar *data;
	// uint ndata;
};

typedef enum
{
	SunCallTypeTNull,
	SunCallTypeRNull,
} SunCallType;

struct SunCall
{
	SunRpc rpc;
	SunCallType type;
};

void sunerrstr(SunStatus);

void sunrpcprint(Fmt*, SunRpc*);
uint sunrpcsize(SunRpc*);
SunStatus sunrpcpack(uchar*, uchar*, uchar**, SunRpc*);
SunStatus sunrpcunpack(uchar*, uchar*, uchar**, SunRpc*);

void sunauthinfoprint(Fmt*, SunAuthInfo*);
uint sunauthinfosize(SunAuthInfo*);
int sunauthinfopack(uchar*, uchar*, uchar**, SunAuthInfo*);
int sunauthinfounpack(uchar*, uchar*, uchar**, SunAuthInfo*);

void sunauthunixprint(Fmt*, SunAuthUnix*);
uint sunauthunixsize(SunAuthUnix*);
int sunauthunixpack(uchar*, uchar*, uchar**, SunAuthUnix*);
int sunauthunixunpack(uchar*, uchar*, uchar**, SunAuthUnix*);

int sunenumpack(uchar*, uchar*, uchar**, int*);
int sunenumunpack(uchar*, uchar*, uchar**, int*);
int sunuint1pack(uchar*, uchar*, uchar**, u1int*);
int sunuint1unpack(uchar*, uchar*, uchar**, u1int*);

int sunstringpack(uchar*, uchar*, uchar**, char**, u32int);
int sunstringunpack(uchar*, uchar*, uchar**, char**, u32int);
uint sunstringsize(char*);

int sunuint32pack(uchar*, uchar*, uchar**, u32int*);
int sunuint32unpack(uchar*, uchar*, uchar**, u32int*);
int sunuint64pack(uchar*, uchar*, uchar**, u64int*);
int sunuint64unpack(uchar*, uchar*, uchar**, u64int*);

int sunvaropaquepack(uchar*, uchar*, uchar**, uchar**, u32int*, u32int);
int sunvaropaqueunpack(uchar*, uchar*, uchar**, uchar**, u32int*, u32int);
uint sunvaropaquesize(u32int);

int sunfixedopaquepack(uchar*, uchar*, uchar**, uchar*, u32int);
int sunfixedopaqueunpack(uchar*, uchar*, uchar**, uchar*, u32int);
uint sunfixedopaquesize(u32int);

/*
 * Sun RPC Program
 */
typedef struct SunProc SunProc;
typedef struct SunProg SunProg;
struct SunProg
{
	uint prog;
	uint vers;
	SunProc *proc;
	int nproc;
};

struct SunProc
{
	int (*pack)(uchar*, uchar*, uchar**, SunCall*);
	int (*unpack)(uchar*, uchar*, uchar**, SunCall*);
	uint (*size)(SunCall*);
	void (*fmt)(Fmt*, SunCall*);
	uint sizeoftype;
};

SunStatus suncallpack(SunProg*, uchar*, uchar*, uchar**, SunCall*);
SunStatus suncallunpack(SunProg*, uchar*, uchar*, uchar**, SunCall*);
SunStatus suncallunpackalloc(SunProg*, SunCallType, uchar*, uchar*, uchar**, SunCall**);
void suncallsetup(SunCall*, SunProg*, uint);
uint suncallsize(SunProg*, SunCall*);

/*
 * Formatting
#pragma varargck type "B" SunRpc*
#pragma varargck type "C" SunCall*
 */

int	sunrpcfmt(Fmt*);
int	suncallfmt(Fmt*);
void	sunfmtinstall(SunProg*);


/*
 * Sun RPC Server
 */
typedef struct SunMsg SunMsg;
typedef struct SunSrv SunSrv;

enum
{
	SunStackSize = 32768,
};

struct SunMsg
{
	uchar *data;
	int count;
	SunSrv *srv;
	SunRpc rpc;
	SunProg *pg;
	SunCall *call;
	Channel *creply;	/* chan(SunMsg*) */
};

struct SunSrv
{
	int chatty;
	int cachereplies;
	int alwaysreject;
	int localonly;
	int localparanoia;
	SunProg **map;
	Channel *crequest;

/* implementation use only */
	Channel **cdispatch;
	SunProg **prog;
	int nprog;
	void *cache;
	Channel *creply;
	Channel *cthread;
};

SunSrv *sunsrv(void);

void	sunsrvprog(SunSrv *srv, SunProg *prog, Channel *c);
int	sunsrvannounce(SunSrv *srv, char *address);
int	sunsrvudp(SunSrv *srv, char *address);
int	sunsrvnet(SunSrv *srv, char *address);
int	sunsrvfd(SunSrv *srv, int fd);
void	sunsrvthreadcreate(SunSrv *srv, void (*fn)(void*), void*);
void	sunsrvclose(SunSrv*);

int	sunmsgreply(SunMsg*, SunCall*);
int	sunmsgdrop(SunMsg*);
int	sunmsgreplyerror(SunMsg*, SunStatus);

/*
 * Sun RPC Client
 */
typedef struct SunClient SunClient;

struct SunClient
{
	int		fd;
	int		chatty;
	int		needcount;
	ulong	maxwait;
	ulong	xidgen;
	int		nsend;
	int		nresend;
	struct {
		ulong min;
		ulong max;
		ulong avg;
	} rtt;
	Channel	*dying;
	Channel	*rpcchan;
	Channel	*timerchan;
	Channel	*flushchan;
	Channel	*readchan;
	SunProg	**prog;
	int		nprog;
	int 		timertid;
	int 		nettid;
};

SunClient	*sundial(char*);

int	sunclientrpc(SunClient*, ulong, SunCall*, SunCall*, uchar**);
void	sunclientclose(SunClient*);
void	sunclientflushrpc(SunClient*, ulong);
void	sunclientprog(SunClient*, SunProg*);


/*
 * Provided by callers.
 * Should remove dependence on this, but hard.
 */
void	*emalloc(ulong);
void *erealloc(void*, ulong);


/*
 * Sun RPC port mapper; see RFC 1057 Appendix A
 */

typedef struct PortMap PortMap;
typedef struct PortTNull PortTNull;
typedef struct PortRNull PortRNull;
typedef struct PortTSet PortTSet;
typedef struct PortRSet PortRSet;
typedef struct PortTUnset PortTUnset;
typedef struct PortRUnset PortRUnset;
typedef struct PortTGetport PortTGetport;
typedef struct PortRGetport PortRGetport;
typedef struct PortTDump PortTDump;
typedef struct PortRDump PortRDump;
typedef struct PortTCallit PortTCallit;
typedef struct PortRCallit PortRCallit;

typedef enum
{
	PortCallTNull,
	PortCallRNull,
	PortCallTSet,
	PortCallRSet,
	PortCallTUnset,
	PortCallRUnset,
	PortCallTGetport,
	PortCallRGetport,
	PortCallTDump,
	PortCallRDump,
	PortCallTCallit,
	PortCallRCallit,
} PortCallType;

enum
{
	PortProgram	= 100000,
	PortVersion	= 2,

	PortProtoTcp	= 6,	/* protocol number for TCP/IP */
	PortProtoUdp	= 17	/* protocol number for UDP/IP */
};

struct PortMap {
	u32int prog;
	u32int vers;
	u32int prot;
	u32int port;
};

struct PortTNull {
	SunCall call;
};

struct PortRNull {
	SunCall call;
};

struct PortTSet {
	SunCall call;
	PortMap map;
};

struct PortRSet {
	SunCall call;
	u1int b;
};

struct PortTUnset {
	SunCall call;
	PortMap map;
};

struct PortRUnset {
	SunCall call;
	u1int b;
};

struct PortTGetport {
	SunCall call;
	PortMap map;
};

struct PortRGetport {
	SunCall call;
	u32int port;
};

struct PortTDump {
	SunCall call;
};

struct PortRDump {
	SunCall call;
	PortMap *map;
	int nmap;
};

struct PortTCallit {
	SunCall call;
	u32int prog;
	u32int vers;
	u32int proc;
	uchar *data;
	u32int count;
};

struct PortRCallit {
	SunCall call;
	u32int port;
	uchar *data;
	u32int count;
};

extern SunProg portprog;