portmap.c 9.5 KB

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