portmap.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. #include <u.h>
  10. #include <libc.h>
  11. #include <thread.h>
  12. #include <sunrpc.h>
  13. static void
  14. portMapPrint(Fmt *fmt, PortMap *x)
  15. {
  16. fmtprint(fmt, "[%ud %ud %ud %ud]", x->prog, x->vers, x->prot, x->port);
  17. }
  18. static uint
  19. portMapSize(PortMap *x)
  20. {
  21. uint a;
  22. USED(x);
  23. a = 0 + 4 + 4 + 4 + 4;
  24. return a;
  25. }
  26. static int
  27. portMapPack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortMap *x)
  28. {
  29. if(sunUint32Pack(a, ea, &a, &x->prog) < 0) goto Err;
  30. if(sunUint32Pack(a, ea, &a, &x->vers) < 0) goto Err;
  31. if(sunUint32Pack(a, ea, &a, &x->prot) < 0) goto Err;
  32. if(sunUint32Pack(a, ea, &a, &x->port) < 0) goto Err;
  33. *pa = a;
  34. return 0;
  35. Err:
  36. *pa = ea;
  37. return -1;
  38. }
  39. static int
  40. portMapUnpack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortMap *x)
  41. {
  42. if(sunUint32Unpack(a, ea, &a, &x->prog) < 0) goto Err;
  43. if(sunUint32Unpack(a, ea, &a, &x->vers) < 0) goto Err;
  44. if(sunUint32Unpack(a, ea, &a, &x->prot) < 0) goto Err;
  45. if(sunUint32Unpack(a, ea, &a, &x->port) < 0) goto Err;
  46. *pa = a;
  47. return 0;
  48. Err:
  49. *pa = ea;
  50. return -1;
  51. }
  52. static void
  53. portTNullPrint(Fmt *fmt, PortTNull *x)
  54. {
  55. USED(x);
  56. fmtprint(fmt, "%s", "PortTNull");
  57. }
  58. static uint
  59. portTNullSize(PortTNull *x)
  60. {
  61. uint a;
  62. USED(x);
  63. a = 0;
  64. return a;
  65. }
  66. static int
  67. portTNullPack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortTNull *x)
  68. {
  69. USED(ea);
  70. USED(x);
  71. *pa = a;
  72. return 0;
  73. }
  74. static int
  75. portTNullUnpack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortTNull *x)
  76. {
  77. USED(ea);
  78. USED(x);
  79. *pa = a;
  80. return 0;
  81. }
  82. static void
  83. portRNullPrint(Fmt *fmt, PortRNull *x)
  84. {
  85. USED(x);
  86. fmtprint(fmt, "%s", "PortRNull");
  87. }
  88. static uint
  89. portRNullSize(PortRNull *x)
  90. {
  91. uint a;
  92. USED(x);
  93. a = 0;
  94. return a;
  95. }
  96. static int
  97. portRNullPack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortRNull *x)
  98. {
  99. USED(ea);
  100. USED(x);
  101. *pa = a;
  102. return 0;
  103. }
  104. static int
  105. portRNullUnpack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortRNull *x)
  106. {
  107. USED(ea);
  108. USED(x);
  109. *pa = a;
  110. return 0;
  111. }
  112. static void
  113. portTSetPrint(Fmt *fmt, PortTSet *x)
  114. {
  115. fmtprint(fmt, "PortTSet ");
  116. portMapPrint(fmt, &x->map);
  117. }
  118. static uint
  119. portTSetSize(PortTSet *x)
  120. {
  121. uint a;
  122. USED(x);
  123. a = 0 + portMapSize(&x->map);
  124. return a;
  125. }
  126. static int
  127. portTSetPack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortTSet *x)
  128. {
  129. if(portMapPack(a, ea, &a, &x->map) < 0) goto Err;
  130. *pa = a;
  131. return 0;
  132. Err:
  133. *pa = ea;
  134. return -1;
  135. }
  136. static int
  137. portTSetUnpack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortTSet *x)
  138. {
  139. if(portMapUnpack(a, ea, &a, &x->map) < 0) goto Err;
  140. *pa = a;
  141. return 0;
  142. Err:
  143. *pa = ea;
  144. return -1;
  145. }
  146. static void
  147. portRSetPrint(Fmt *fmt, PortRSet *x)
  148. {
  149. fmtprint(fmt, "PortRSet %ud", x->b);
  150. }
  151. static uint
  152. portRSetSize(PortRSet *x)
  153. {
  154. uint a;
  155. USED(x);
  156. a = 0 + 4;
  157. return a;
  158. }
  159. static int
  160. portRSetPack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortRSet *x)
  161. {
  162. if(sunUint1Pack(a, ea, &a, &x->b) < 0) goto Err;
  163. *pa = a;
  164. return 0;
  165. Err:
  166. *pa = ea;
  167. return -1;
  168. }
  169. static int
  170. portRSetUnpack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortRSet *x)
  171. {
  172. if(sunUint1Unpack(a, ea, &a, &x->b) < 0) goto Err;
  173. *pa = a;
  174. return 0;
  175. Err:
  176. *pa = ea;
  177. return -1;
  178. }
  179. static void
  180. portTUnsetPrint(Fmt *fmt, PortTUnset *x)
  181. {
  182. fmtprint(fmt, "PortTUnset ");
  183. portMapPrint(fmt, &x->map);
  184. }
  185. static uint
  186. portTUnsetSize(PortTUnset *x)
  187. {
  188. uint a;
  189. USED(x);
  190. a = 0 + portMapSize(&x->map);
  191. return a;
  192. }
  193. static int
  194. portTUnsetPack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortTUnset *x)
  195. {
  196. if(portMapPack(a, ea, &a, &x->map) < 0) goto Err;
  197. *pa = a;
  198. return 0;
  199. Err:
  200. *pa = ea;
  201. return -1;
  202. }
  203. static int
  204. portTUnsetUnpack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortTUnset *x)
  205. {
  206. if(portMapUnpack(a, ea, &a, &x->map) < 0) goto Err;
  207. *pa = a;
  208. return 0;
  209. Err:
  210. *pa = ea;
  211. return -1;
  212. }
  213. static void
  214. portRUnsetPrint(Fmt *fmt, PortRUnset *x)
  215. {
  216. fmtprint(fmt, "PortRUnset %ud", x->b);
  217. }
  218. static uint
  219. portRUnsetSize(PortRUnset *x)
  220. {
  221. uint a;
  222. USED(x);
  223. a = 0 + 4;
  224. return a;
  225. }
  226. static int
  227. portRUnsetPack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortRUnset *x)
  228. {
  229. if(sunUint1Pack(a, ea, &a, &x->b) < 0) goto Err;
  230. *pa = a;
  231. return 0;
  232. Err:
  233. *pa = ea;
  234. return -1;
  235. }
  236. static int
  237. portRUnsetUnpack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortRUnset *x)
  238. {
  239. if(sunUint1Unpack(a, ea, &a, &x->b) < 0) goto Err;
  240. *pa = a;
  241. return 0;
  242. Err:
  243. *pa = ea;
  244. return -1;
  245. }
  246. static void
  247. portTGetportPrint(Fmt *fmt, PortTGetport *x)
  248. {
  249. fmtprint(fmt, "PortTGetport ");
  250. portMapPrint(fmt, &x->map);
  251. }
  252. static uint
  253. portTGetportSize(PortTGetport *x)
  254. {
  255. uint a;
  256. USED(x);
  257. a = 0 + portMapSize(&x->map);
  258. return a;
  259. }
  260. static int
  261. portTGetportPack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortTGetport *x)
  262. {
  263. if(portMapPack(a, ea, &a, &x->map) < 0) goto Err;
  264. *pa = a;
  265. return 0;
  266. Err:
  267. *pa = ea;
  268. return -1;
  269. }
  270. static int
  271. portTGetportUnpack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortTGetport *x)
  272. {
  273. if(portMapUnpack(a, ea, &a, &x->map) < 0) goto Err;
  274. *pa = a;
  275. return 0;
  276. Err:
  277. *pa = ea;
  278. return -1;
  279. }
  280. static void
  281. portRGetportPrint(Fmt *fmt, PortRGetport *x)
  282. {
  283. fmtprint(fmt, "PortRGetport %ud", x->port);
  284. }
  285. static uint
  286. portRGetportSize(PortRGetport *x)
  287. {
  288. uint a;
  289. USED(x);
  290. a = 0 + 4;
  291. return a;
  292. }
  293. static int
  294. portRGetportPack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortRGetport *x)
  295. {
  296. if(sunUint32Pack(a, ea, &a, &x->port) < 0) goto Err;
  297. *pa = a;
  298. return 0;
  299. Err:
  300. *pa = ea;
  301. return -1;
  302. }
  303. static int
  304. portRGetportUnpack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortRGetport *x)
  305. {
  306. if(sunUint32Unpack(a, ea, &a, &x->port) < 0) goto Err;
  307. *pa = a;
  308. return 0;
  309. Err:
  310. *pa = ea;
  311. return -1;
  312. }
  313. static void
  314. portTDumpPrint(Fmt *fmt, PortTDump *x)
  315. {
  316. USED(x);
  317. fmtprint(fmt, "PortTDump");
  318. }
  319. static uint
  320. portTDumpSize(PortTDump *x)
  321. {
  322. uint a;
  323. USED(x);
  324. a = 0;
  325. return a;
  326. }
  327. static int
  328. portTDumpPack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortTDump *x)
  329. {
  330. USED(ea);
  331. USED(x);
  332. *pa = a;
  333. return 0;
  334. }
  335. static int
  336. portTDumpUnpack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortTDump *x)
  337. {
  338. USED(ea);
  339. USED(x);
  340. *pa = a;
  341. return 0;
  342. }
  343. static void
  344. portRDumpPrint(Fmt *fmt, PortRDump *x)
  345. {
  346. int i;
  347. fmtprint(fmt, "PortRDump");
  348. for(i=0; i<x->nmap; i++){
  349. fmtprint(fmt, " ");
  350. portMapPrint(fmt, &x->map[i]);
  351. }
  352. }
  353. static uint
  354. portRDumpSize(PortRDump *x)
  355. {
  356. return (5*4*x->nmap) + 4;
  357. }
  358. static int
  359. portRDumpPack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortRDump *x)
  360. {
  361. int i;
  362. uint32_t zero, one;
  363. zero = 0;
  364. one = 1;
  365. for(i=0; i<x->nmap; i++){
  366. if(sunUint32Pack(a, ea, &a, &one) < 0
  367. || portMapPack(a, ea, &a, &x->map[i]) < 0)
  368. goto Err;
  369. }
  370. if(sunUint32Pack(a, ea, &a, &zero) < 0)
  371. goto Err;
  372. *pa = a;
  373. return 0;
  374. Err:
  375. *pa = ea;
  376. return -1;
  377. }
  378. static int
  379. portRDumpUnpack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortRDump *x)
  380. {
  381. int i;
  382. u1int u1;
  383. PortMap *m;
  384. m = (PortMap*)a;
  385. for(i=0;; i++){
  386. if(sunUint1Unpack(a, ea, &a, &u1) < 0)
  387. goto Err;
  388. if(u1 == 0)
  389. break;
  390. if(portMapUnpack(a, ea, &a, &m[i]) < 0)
  391. goto Err;
  392. }
  393. x->nmap = i;
  394. x->map = m;
  395. *pa = a;
  396. return 0;
  397. Err:
  398. *pa = ea;
  399. return -1;
  400. }
  401. static void
  402. portTCallitPrint(Fmt *fmt, PortTCallit *x)
  403. {
  404. fmtprint(fmt, "PortTCallit [%ud,%ud,%ud] %ud", x->prog, x->vers, x->proc, x->count);
  405. }
  406. static uint
  407. portTCallitSize(PortTCallit *x)
  408. {
  409. uint a;
  410. USED(x);
  411. a = 0 + 4 + 4 + 4 + sunVarOpaqueSize(x->count);
  412. return a;
  413. }
  414. static int
  415. portTCallitPack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortTCallit *x)
  416. {
  417. if(sunUint32Pack(a, ea, &a, &x->prog) < 0) goto Err;
  418. if(sunUint32Pack(a, ea, &a, &x->vers) < 0) goto Err;
  419. if(sunUint32Pack(a, ea, &a, &x->proc) < 0) goto Err;
  420. if(sunVarOpaquePack(a, ea, &a, &x->data, &x->count, -1) < 0) goto Err;
  421. *pa = a;
  422. return 0;
  423. Err:
  424. *pa = ea;
  425. return -1;
  426. }
  427. static int
  428. portTCallitUnpack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortTCallit *x)
  429. {
  430. if(sunUint32Unpack(a, ea, &a, &x->prog) < 0) goto Err;
  431. if(sunUint32Unpack(a, ea, &a, &x->vers) < 0) goto Err;
  432. if(sunUint32Unpack(a, ea, &a, &x->proc) < 0) goto Err;
  433. if(sunVarOpaqueUnpack(a, ea, &a, &x->data, &x->count, -1) < 0) goto Err;
  434. *pa = a;
  435. return 0;
  436. Err:
  437. *pa = ea;
  438. return -1;
  439. }
  440. static void
  441. portRCallitPrint(Fmt *fmt, PortRCallit *x)
  442. {
  443. fmtprint(fmt, "PortRCallit %ud %ud", x->port, x->count);
  444. }
  445. static uint
  446. portRCallitSize(PortRCallit *x)
  447. {
  448. uint a;
  449. USED(x);
  450. a = 0 + 4 + sunVarOpaqueSize(x->count);
  451. return a;
  452. }
  453. static int
  454. portRCallitPack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortRCallit *x)
  455. {
  456. if(sunUint32Pack(a, ea, &a, &x->port) < 0) goto Err;
  457. if(sunVarOpaquePack(a, ea, &a, &x->data, &x->count, -1) < 0) goto Err;
  458. *pa = a;
  459. return 0;
  460. Err:
  461. *pa = ea;
  462. return -1;
  463. }
  464. static int
  465. portRCallitUnpack(uint8_t *a, uint8_t *ea, uint8_t **pa, PortRCallit *x)
  466. {
  467. if(sunUint32Unpack(a, ea, &a, &x->port) < 0) goto Err;
  468. if(sunVarOpaqueUnpack(a, ea, &a, &x->data, &x->count, -1) < 0) goto Err;
  469. *pa = a;
  470. return 0;
  471. Err:
  472. *pa = ea;
  473. return -1;
  474. }
  475. typedef int (*P)(uint8_t*, uint8_t*, uint8_t**, SunCall*);
  476. typedef void (*F)(Fmt*, SunCall*);
  477. typedef uint (*S)(SunCall*);
  478. static SunProc proc[] = {
  479. (P)portTNullPack, (P)portTNullUnpack, (S)portTNullSize, (F)portTNullPrint, sizeof(PortTNull),
  480. (P)portRNullPack, (P)portRNullUnpack, (S)portRNullSize, (F)portRNullPrint, sizeof(PortRNull),
  481. (P)portTSetPack, (P)portTSetUnpack, (S)portTSetSize, (F)portTSetPrint, sizeof(PortTSet),
  482. (P)portRSetPack, (P)portRSetUnpack, (S)portRSetSize, (F)portRSetPrint, sizeof(PortRSet),
  483. (P)portTUnsetPack, (P)portTUnsetUnpack, (S)portTUnsetSize, (F)portTUnsetPrint, sizeof(PortTUnset),
  484. (P)portRUnsetPack, (P)portRUnsetUnpack, (S)portRUnsetSize, (F)portRUnsetPrint, sizeof(PortRUnset),
  485. (P)portTGetportPack, (P)portTGetportUnpack, (S)portTGetportSize, (F)portTGetportPrint, sizeof(PortTGetport),
  486. (P)portRGetportPack, (P)portRGetportUnpack, (S)portRGetportSize, (F)portRGetportPrint, sizeof(PortRGetport),
  487. (P)portTDumpPack, (P)portTDumpUnpack, (S)portTDumpSize, (F)portTDumpPrint, sizeof(PortTDump),
  488. (P)portRDumpPack, (P)portRDumpUnpack, (S)portRDumpSize, (F)portRDumpPrint, sizeof(PortRDump),
  489. (P)portTCallitPack, (P)portTCallitUnpack, (S)portTCallitSize, (F)portTCallitPrint, sizeof(PortTCallit),
  490. (P)portRCallitPack, (P)portRCallitUnpack, (S)portRCallitSize, (F)portRCallitPrint, sizeof(PortRCallit),
  491. };
  492. SunProg portProg =
  493. {
  494. PortProgram,
  495. PortVersion,
  496. proc,
  497. nelem(proc),
  498. };