sunrpc.h 7.7 KB

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