portmap.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* Copyright © 2003 Russ Cox, MIT; see /sys/src/libsunrpc/COPYING */
  2. #include <u.h>
  3. #include <libc.h>
  4. #include <thread.h>
  5. #include <sunrpc.h>
  6. int chatty;
  7. SunClient *client;
  8. void
  9. usage(void)
  10. {
  11. fprint(2, "usage: portmap address [cmd]\n"
  12. "cmd is one of:\n"
  13. "\tnull\n"
  14. "\tset prog vers proto port\n"
  15. "\tunset prog vers proto port\n"
  16. "\tgetport prog vers proto\n"
  17. "\tdump (default)\n");
  18. threadexitsall("usage");
  19. }
  20. void
  21. portCall(SunCall *c, PortCallType type)
  22. {
  23. c->rpc.prog = PortProgram;
  24. c->rpc.vers = PortVersion;
  25. c->rpc.proc = type>>1;
  26. c->rpc.iscall = !(type&1);
  27. c->type = type;
  28. }
  29. void
  30. tnull(char **argv)
  31. {
  32. PortTNull tx;
  33. PortRNull rx;
  34. USED(argv);
  35. memset(&tx, 0, sizeof tx);
  36. portCall(&tx.call, PortCallTNull);
  37. memset(&rx, 0, sizeof rx);
  38. portCall(&rx.call, PortCallRNull);
  39. if(sunClientRpc(client, 0, &tx.call, &rx.call, nil) < 0)
  40. sysfatal("rpc: %r");
  41. }
  42. void
  43. tset(char **argv)
  44. {
  45. PortTSet tx;
  46. PortRSet rx;
  47. memset(&tx, 0, sizeof tx);
  48. portCall(&tx.call, PortCallTSet);
  49. tx.map.prog = strtol(argv[0], 0, 0);
  50. tx.map.vers = strtol(argv[1], 0, 0);
  51. tx.map.prot = strtol(argv[2], 0, 0);
  52. tx.map.port = strtol(argv[3], 0, 0);
  53. memset(&rx, 0, sizeof rx);
  54. portCall(&rx.call, PortCallRSet);
  55. if(sunClientRpc(client, 0, &tx.call, &rx.call, nil) < 0)
  56. sysfatal("rpc: %r");
  57. if(rx.b == 0)
  58. print("rejected\n");
  59. }
  60. void
  61. tunset(char **argv)
  62. {
  63. PortTUnset tx;
  64. PortRUnset rx;
  65. memset(&tx, 0, sizeof tx);
  66. portCall(&tx.call, PortCallTUnset);
  67. tx.map.prog = strtol(argv[0], 0, 0);
  68. tx.map.vers = strtol(argv[1], 0, 0);
  69. tx.map.prot = strtol(argv[2], 0, 0);
  70. tx.map.port = strtol(argv[3], 0, 0);
  71. memset(&rx, 0, sizeof rx);
  72. portCall(&rx.call, PortCallRUnset);
  73. if(sunClientRpc(client, 0, &tx.call, &rx.call, nil) < 0)
  74. sysfatal("rpc: %r");
  75. if(rx.b == 0)
  76. print("rejected\n");
  77. }
  78. void
  79. tgetport(char **argv)
  80. {
  81. PortTGetport tx;
  82. PortRGetport rx;
  83. memset(&tx, 0, sizeof tx);
  84. portCall(&tx.call, PortCallTGetport);
  85. tx.map.prog = strtol(argv[0], 0, 0);
  86. tx.map.vers = strtol(argv[1], 0, 0);
  87. tx.map.prot = strtol(argv[2], 0, 0);
  88. memset(&rx, 0, sizeof rx);
  89. portCall(&rx.call, PortCallRGetport);
  90. if(sunClientRpc(client, 0, &tx.call, &rx.call, nil) < 0)
  91. sysfatal("rpc: %r");
  92. print("%d\n", rx.port);
  93. }
  94. void
  95. tdump(char **argv)
  96. {
  97. int i;
  98. uchar *p;
  99. PortTDump tx;
  100. PortRDump rx;
  101. PortMap *m;
  102. USED(argv);
  103. memset(&tx, 0, sizeof tx);
  104. portCall(&tx.call, PortCallTDump);
  105. memset(&rx, 0, sizeof rx);
  106. portCall(&rx.call, PortCallRDump);
  107. if(sunClientRpc(client, 0, &tx.call, &rx.call, &p) < 0)
  108. sysfatal("rpc: %r");
  109. for(i=0, m=rx.map; i<rx.nmap; i++, m++)
  110. print("%ud %ud %ud %ud\n", (uint)m->prog, (uint)m->vers, (uint)m->prot, (uint)m->port);
  111. free(p);
  112. }
  113. static struct {
  114. char *cmd;
  115. int narg;
  116. void (*fn)(char**);
  117. } tab[] = {
  118. "null", 0, tnull,
  119. "set", 4, tset,
  120. "unset", 4, tunset,
  121. "getport", 3, tgetport,
  122. "dump", 0, tdump,
  123. };
  124. void
  125. threadmain(int argc, char **argv)
  126. {
  127. char *dflt[] = { "dump", };
  128. char *addr, *cmd;
  129. int i;
  130. ARGBEGIN{
  131. case 'R':
  132. chatty++;
  133. break;
  134. }ARGEND
  135. if(argc < 1)
  136. usage();
  137. fmtinstall('B', sunRpcFmt);
  138. fmtinstall('C', sunCallFmt);
  139. fmtinstall('H', encodefmt);
  140. sunFmtInstall(&portProg);
  141. addr = netmkaddr(argv[0], "udp", "portmap");
  142. if((client = sunDial(addr)) == nil)
  143. sysfatal("dial %s: %r", addr);
  144. client->chatty = chatty;
  145. sunClientProg(client, &portProg);
  146. argv++;
  147. argc--;
  148. if(argc == 0){
  149. argc = 1;
  150. argv = dflt;
  151. }
  152. cmd = argv[0];
  153. argv++;
  154. argc--;
  155. for(i=0; i<nelem(tab); i++){
  156. if(strcmp(tab[i].cmd, cmd) == 0){
  157. if(tab[i].narg != argc)
  158. usage();
  159. (*tab[i].fn)(argv);
  160. threadexitsall(nil);
  161. }
  162. }
  163. usage();
  164. }