xref: /plan9/sys/include/sunrpc.h (revision 6ff5e9135082ce281d25c68a5544eef8249d480c)
1 /*
2  * Sun RPC; see RFC 1057
3  */
4 
5 #pragma lib "libsunrpc.a"
6 #pragma src "/sys/src/libsunrpc"
7 
8 typedef uchar u1int;
9 
10 typedef struct SunAuthInfo SunAuthInfo;
11 typedef struct SunAuthUnix SunAuthUnix;
12 typedef struct SunRpc SunRpc;
13 typedef struct SunCall SunCall;
14 
15 enum
16 {
17 	/* Authinfo.flavor */
18 	SunAuthNone = 0,
19 	SunAuthSys,
20 	SunAuthShort,
21 	SunAuthDes,
22 };
23 
24 typedef enum {
25 	SunAcceptError = 0x10000,
26 	SunRejectError = 0x20000,
27 	SunAuthError = 0x40000,
28 
29 	/* Reply.status */
30 	SunSuccess = 0,
31 
32 	SunProgUnavail = SunAcceptError | 1,
33 	SunProgMismatch,
34 	SunProcUnavail,
35 	SunGarbageArgs,
36 	SunSystemErr,
37 
38 	SunRpcMismatch = SunRejectError | 0,
39 
40 	SunAuthBadCred = SunAuthError | 1,
41 	SunAuthRejectedCred,
42 	SunAuthBadVerf,
43 	SunAuthRejectedVerf,
44 	SunAuthTooWeak,
45 	SunAuthInvalidResp,
46 	SunAuthFailed,
47 } SunStatus;
48 
49 struct SunAuthInfo
50 {
51 	uint flavor;
52 	uchar *data;
53 	uint ndata;
54 };
55 
56 struct SunAuthUnix
57 {
58 	u32int stamp;
59 	char *sysname;
60 	u32int uid;
61 	u32int gid;
62 	u32int g[16];
63 	u32int ng;
64 };
65 
66 struct SunRpc
67 {
68 	u32int xid;
69 	uint iscall;
70 
71 	/*
72 	 * only sent on wire in call
73 	 * caller fills in for the reply unpackers.
74 	 */
75 	u32int proc;
76 
77 	/* call */
78 	// uint proc;
79 	u32int prog, vers;
80 	SunAuthInfo cred;
81 	SunAuthInfo verf;
82 	uchar *data;
83 	uint ndata;
84 
85 	/* reply */
86 	u32int status;
87 	// SunAuthInfo verf;
88 	u32int low, high;
89 	// uchar *data;
90 	// uint ndata;
91 };
92 
93 typedef enum
94 {
95 	SunCallTypeTNull,
96 	SunCallTypeRNull,
97 } SunCallType;
98 
99 struct SunCall
100 {
101 	SunRpc rpc;
102 	SunCallType type;
103 };
104 
105 void sunErrstr(SunStatus);
106 
107 void sunRpcPrint(Fmt*, SunRpc*);
108 uint sunRpcSize(SunRpc*);
109 SunStatus sunRpcPack(uchar*, uchar*, uchar**, SunRpc*);
110 SunStatus sunRpcUnpack(uchar*, uchar*, uchar**, SunRpc*);
111 
112 void sunAuthInfoPrint(Fmt*, SunAuthInfo*);
113 uint sunAuthInfoSize(SunAuthInfo*);
114 int sunAuthInfoPack(uchar*, uchar*, uchar**, SunAuthInfo*);
115 int sunAuthInfoUnpack(uchar*, uchar*, uchar**, SunAuthInfo*);
116 
117 void sunAuthUnixPrint(Fmt*, SunAuthUnix*);
118 uint sunAuthUnixSize(SunAuthUnix*);
119 int sunAuthUnixPack(uchar*, uchar*, uchar**, SunAuthUnix*);
120 int sunAuthUnixUnpack(uchar*, uchar*, uchar**, SunAuthUnix*);
121 
122 int sunEnumPack(uchar*, uchar*, uchar**, int*);
123 int sunEnumUnpack(uchar*, uchar*, uchar**, int*);
124 int sunUint1Pack(uchar*, uchar*, uchar**, u1int*);
125 int sunUint1Unpack(uchar*, uchar*, uchar**, u1int*);
126 
127 int sunStringPack(uchar*, uchar*, uchar**, char**, u32int);
128 int sunStringUnpack(uchar*, uchar*, uchar**, char**, u32int);
129 uint sunStringSize(char*);
130 
131 int sunUint32Pack(uchar*, uchar*, uchar**, u32int*);
132 int sunUint32Unpack(uchar*, uchar*, uchar**, u32int*);
133 int sunUint64Pack(uchar*, uchar*, uchar**, u64int*);
134 int sunUint64Unpack(uchar*, uchar*, uchar**, u64int*);
135 
136 int sunVarOpaquePack(uchar*, uchar*, uchar**, uchar**, u32int*, u32int);
137 int sunVarOpaqueUnpack(uchar*, uchar*, uchar**, uchar**, u32int*, u32int);
138 uint sunVarOpaqueSize(u32int);
139 
140 int sunFixedOpaquePack(uchar*, uchar*, uchar**, uchar*, u32int);
141 int sunFixedOpaqueUnpack(uchar*, uchar*, uchar**, uchar*, u32int);
142 uint sunFixedOpaqueSize(u32int);
143 
144 /*
145  * Sun RPC Program
146  */
147 typedef struct SunProc SunProc;
148 typedef struct SunProg SunProg;
149 struct SunProg
150 {
151 	uint prog;
152 	uint vers;
153 	SunProc *proc;
154 	int nproc;
155 };
156 
157 struct SunProc
158 {
159 	int (*pack)(uchar*, uchar*, uchar**, SunCall*);
160 	int (*unpack)(uchar*, uchar*, uchar**, SunCall*);
161 	uint (*size)(SunCall*);
162 	void (*fmt)(Fmt*, SunCall*);
163 	uint sizeoftype;
164 };
165 
166 SunStatus sunCallPack(SunProg*, uchar*, uchar*, uchar**, SunCall*);
167 SunStatus sunCallUnpack(SunProg*, uchar*, uchar*, uchar**, SunCall*);
168 SunStatus sunCallUnpackAlloc(SunProg*, SunCallType, uchar*, uchar*, uchar**, SunCall**);
169 uint sunCallSize(SunProg*, SunCall*);
170 void sunCallSetup(SunCall*, SunProg*, uint);
171 
172 /*
173  * Formatting
174  */
175 #pragma varargck type "B" SunRpc*
176 #pragma varargck type "C" SunCall*
177 
178 int	sunRpcFmt(Fmt*);
179 int	sunCallFmt(Fmt*);
180 void	sunFmtInstall(SunProg*);
181 
182 
183 /*
184  * Sun RPC Server
185  */
186 typedef struct SunMsg SunMsg;
187 typedef struct SunSrv SunSrv;
188 
189 enum
190 {
191 	SunStackSize = 8192,
192 };
193 
194 struct SunMsg
195 {
196 	uchar *data;
197 	int count;
198 	SunSrv *srv;
199 	SunRpc rpc;
200 	SunProg *pg;
201 	SunCall *call;
202 	Channel *creply;	/* chan(SunMsg*) */
203 };
204 
205 struct SunSrv
206 {
207 	int chatty;
208 	int cacheReplies;
209 	int alwaysReject;
210 	SunProg **map;
211 	Channel *crequest;
212 
213 /* implementation use only */
214 	Channel **cdispatch;
215 	SunProg **prog;
216 	int nprog;
217 	void *cache;
218 	Channel *creply;
219 	Channel *cthread;
220 };
221 
222 SunSrv *sunSrv(void);
223 
224 void	sunSrvProg(SunSrv *srv, SunProg *prog, Channel *c);
225 int	sunSrvAnnounce(SunSrv *srv, char *address);
226 int	sunSrvUdp(SunSrv *srv, char *address);
227 int	sunSrvNet(SunSrv *srv, char *address);
228 int	sunSrvFd(SunSrv *srv, int fd);
229 void	sunSrvThreadCreate(SunSrv *srv, void (*fn)(void*), void*);
230 void	sunSrvClose(SunSrv*);
231 
232 int	sunMsgReply(SunMsg*, SunCall*);
233 int	sunMsgDrop(SunMsg*);
234 int	sunMsgReplyError(SunMsg*, SunStatus);
235 
236 /*
237  * Sun RPC Client
238  */
239 typedef struct SunClient SunClient;
240 
241 struct SunClient
242 {
243 	int		fd;
244 	int		chatty;
245 	int		needcount;
246 	ulong	maxwait;
247 	ulong	xidgen;
248 	int		nsend;
249 	int		nresend;
250 	struct {
251 		ulong min;
252 		ulong max;
253 		ulong avg;
254 	} rtt;
255 	Channel	*dying;
256 	Channel	*rpcchan;
257 	Channel	*timerchan;
258 	Channel	*flushchan;
259 	Channel	*readchan;
260 	SunProg	**prog;
261 	int		nprog;
262 	int 		timertid;
263 	int 		nettid;
264 };
265 
266 SunClient	*sunDial(char*);
267 
268 int	sunClientRpc(SunClient*, ulong, SunCall*, SunCall*, uchar**);
269 void	sunClientClose(SunClient*);
270 void	sunClientFlushRpc(SunClient*, ulong);
271 void	sunClientProg(SunClient*, SunProg*);
272 
273 
274 /*
275  * Provided by callers.
276  * Should remove dependence on this, but hard.
277  */
278 void	*emalloc(ulong);
279 void *erealloc(void*, ulong);
280 
281 
282 /*
283  * Sun RPC port mapper; see RFC 1057 Appendix A
284  */
285 
286 typedef struct PortMap PortMap;
287 typedef struct PortTNull PortTNull;
288 typedef struct PortRNull PortRNull;
289 typedef struct PortTSet PortTSet;
290 typedef struct PortRSet PortRSet;
291 typedef struct PortTUnset PortTUnset;
292 typedef struct PortRUnset PortRUnset;
293 typedef struct PortTGetport PortTGetport;
294 typedef struct PortRGetport PortRGetport;
295 typedef struct PortTDump PortTDump;
296 typedef struct PortRDump PortRDump;
297 typedef struct PortTCallit PortTCallit;
298 typedef struct PortRCallit PortRCallit;
299 
300 typedef enum
301 {
302 	PortCallTNull,
303 	PortCallRNull,
304 	PortCallTSet,
305 	PortCallRSet,
306 	PortCallTUnset,
307 	PortCallRUnset,
308 	PortCallTGetport,
309 	PortCallRGetport,
310 	PortCallTDump,
311 	PortCallRDump,
312 	PortCallTCallit,
313 	PortCallRCallit,
314 } PortCallType;
315 
316 enum
317 {
318 	PortProgram	= 100000,
319 	PortVersion	= 2,
320 
321 	PortProtoTcp	= 6,	/* protocol number for TCP/IP */
322 	PortProtoUdp	= 17	/* protocol number for UDP/IP */
323 };
324 
325 struct PortMap {
326 	u32int prog;
327 	u32int vers;
328 	u32int prot;
329 	u32int port;
330 };
331 
332 struct PortTNull {
333 	SunCall call;
334 };
335 
336 struct PortRNull {
337 	SunCall call;
338 };
339 
340 struct PortTSet {
341 	SunCall call;
342 	PortMap map;
343 };
344 
345 struct PortRSet {
346 	SunCall call;
347 	u1int b;
348 };
349 
350 struct PortTUnset {
351 	SunCall call;
352 	PortMap map;
353 };
354 
355 struct PortRUnset {
356 	SunCall call;
357 	u1int b;
358 };
359 
360 struct PortTGetport {
361 	SunCall call;
362 	PortMap map;
363 };
364 
365 struct PortRGetport {
366 	SunCall call;
367 	u32int port;
368 };
369 
370 struct PortTDump {
371 	SunCall call;
372 };
373 
374 struct PortRDump {
375 	SunCall call;
376 	PortMap *map;
377 	int nmap;
378 };
379 
380 struct PortTCallit {
381 	SunCall call;
382 	u32int prog;
383 	u32int vers;
384 	u32int proc;
385 	uchar *data;
386 	u32int count;
387 };
388 
389 struct PortRCallit {
390 	SunCall call;
391 	u32int port;
392 	uchar *data;
393 	u32int count;
394 };
395 
396 extern SunProg portProg;
397