home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume6 / rpc2 / part01 / rpc / tools / rpcinfo.c < prev   
Encoding:
C/C++ Source or Header  |  1986-11-30  |  5.9 KB  |  254 lines

  1. /*      rpcinfo.c     1.1     86/02/05     */
  2.  
  3. /*
  4.  * Copyright (C) 1984, Sun Microsystems, Inc.
  5.  */
  6.  
  7. /*
  8.  * rpcinfo: ping a particular rpc program
  9.  *     or dump the portmapper
  10.  */
  11.  
  12. /*
  13.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  14.  * unrestricted use provided that this legend is included on all tape
  15.  * media and as a part of the software program in whole or part.  Users
  16.  * may copy or modify Sun RPC without charge, but are not authorized
  17.  * to license or distribute it to anyone else except as part of a product or
  18.  * program developed by the user.
  19.  * 
  20.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  21.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  22.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  23.  * 
  24.  * Sun RPC is provided with no support and without any obligation on the
  25.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  26.  * modification or enhancement.
  27.  * 
  28.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  29.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  30.  * OR ANY PART THEREOF.
  31.  * 
  32.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  33.  * or profits or other special, indirect and consequential damages, even if
  34.  * Sun has been advised of the possibility of such damages.
  35.  * 
  36.  * Sun Microsystems, Inc.
  37.  * 2550 Garcia Avenue
  38.  * Mountain View, California  94043
  39.  */
  40.  
  41. #include <rpc/rpc.h>
  42. #include <stdio.h>
  43. #include <sys/socket.h>
  44. #include <sys/time.h>
  45. #include <netdb.h>
  46. #include <rpc/pmap_prot.h>
  47. #include <rpc/pmap_clnt.h>
  48. #include <ctype.h>
  49.  
  50. #define MAXHOSTLEN 256
  51.  
  52. main(argc, argv)
  53.     char **argv;
  54. {
  55.     if (argc < 2) {
  56.         usage();
  57.         exit(1);
  58.     }
  59.     if (argv[1][0] == '-') {
  60.         switch(argv[1][1]) {
  61.             case 't':
  62.                 tcpping(argc-1, argv+1);
  63.                 break;
  64.             case 'p':
  65.                 pmapdump(argc-1, argv+1);
  66.                 break;
  67.             case 'u':
  68.                 udpping(argc-1, argv+1);
  69.                 break;
  70.             default:
  71.                 usage();
  72.                 exit(1);
  73.                 break;
  74.         }
  75.     }
  76.     else
  77.         usage();
  78. }
  79.         
  80. udpping(argc, argv)
  81.     char **argv;
  82. {
  83.     int ans, prognum, vers;
  84.     struct rpcent *rpc;
  85.     
  86.     if (argc < 3 || argc > 4) {
  87.         usage();
  88.         exit(1);
  89.     }
  90.     if (isalpha(argv[2][0])) {
  91.         rpc = getrpcbyname(argv[2]);
  92.         if (rpc == NULL) {
  93.             fprintf(stderr, "%s is unknown name\n", argv[2]);
  94.             exit(1);
  95.         }
  96.         prognum = rpc->r_number;
  97.     }
  98.     else
  99.         prognum = atoi(argv[2]);
  100.     if (argc == 3)
  101.         vers = 1;
  102.     else
  103.         vers = atoi(argv[3]);
  104.     ans = callrpc(argv[1], prognum, vers, NULLPROC,
  105.         xdr_void, 0, xdr_void, 0);
  106.     if (ans != 0) {
  107.         clnt_perrno(ans);
  108.         fprintf(stderr, "\n");
  109.         printf("program %d version %d is not available\n",
  110.             prognum, vers);
  111.         exit(1);
  112.     }
  113.     else
  114.         printf("program %d version %d ready and waiting\n",
  115.             prognum, vers);
  116. }
  117.  
  118. tcpping(argc, argv)
  119.     int argc;
  120.     char **argv;
  121. {
  122.     struct timeval to;
  123.     struct sockaddr_in addr;
  124.     enum clnt_stat rpc_stat;
  125.     CLIENT *client;
  126.     int prognum, vers;
  127.     int sock = -1;
  128.     struct hostent *hp;
  129.     struct rpcent *rpc;
  130.  
  131.     if (argc < 3 || argc > 4) {
  132.         usage();
  133.         exit(1);
  134.     }
  135.     if (isalpha(argv[2][0])) {
  136.         rpc = getrpcbyname(argv[2]);
  137.         if (rpc == NULL) {
  138.             fprintf(stderr, "%s is unknown name\n", argv[2]);
  139.             exit(1);
  140.         }
  141.         prognum = rpc->r_number;
  142.     }
  143.     else
  144.         prognum = atoi(argv[2]);
  145.     if ((hp = gethostbyname(argv[1])) == NULL) {
  146.         fprintf(stderr, "can't find %s\n", argv[1]);
  147.         exit(1);
  148.     }
  149.     addr.sin_family = AF_INET;
  150.     addr.sin_port = 0;
  151.     addr.sin_addr.s_addr = *(int *)hp->h_addr;
  152.     if (argc == 3)
  153.         vers = 1;
  154.     else
  155.         vers = atoi(argv[3]);
  156.     if ((client = clnttcp_create(&addr, prognum,
  157.         vers, &sock, 0, 0)) == NULL) {
  158.             clnt_pcreateerror("");
  159.             printf("program %d version %d is not available\n",
  160.                 prognum, vers);
  161.             exit(1);
  162.         }
  163.     to.tv_usec = 0;
  164.     to.tv_sec = 10;
  165.     rpc_stat = clnt_call(client, 0, xdr_void, NULL, xdr_void, NULL, to);
  166.     if (rpc_stat != RPC_SUCCESS) {
  167.         clnt_perrno(rpc_stat);
  168.         fprintf(stderr, "\n");
  169.         printf("program %d version %d is not available\n",
  170.             prognum, vers);
  171.         exit(1);
  172.     }
  173.     else
  174.         printf("program %d version %d ready and waiting\n",
  175.             prognum, vers);
  176. }
  177.  
  178. pmapdump(argc, argv)
  179.     int argc;
  180.     char **argv;
  181. {
  182.     struct sockaddr_in server_addr;
  183.     struct hostent *hp;
  184.     struct pmaplist *head = NULL;
  185.     char hoststr[MAXHOSTLEN];
  186.     int socket = -1;
  187.     struct timeval minutetimeout;
  188.     char *hostnm;
  189.     register CLIENT *client;
  190.     enum clnt_stat rpc_stat;
  191.     struct rpcent *rpc;
  192.     
  193.     if (argc > 2) {
  194.         usage();
  195.         exit(1);
  196.     }
  197.     if (argc == 2) {
  198.         hostnm = argv[1];
  199.     } else {
  200.         gethostname(hoststr, sizeof(hoststr));
  201.         hostnm = hoststr;
  202.     }
  203.     if ((hp = gethostbyname(hostnm)) == NULL) {
  204.         fprintf(stderr, "cannot get addr for '%s'\n", hostnm);
  205.         exit(0);
  206.     }
  207.     bcopy(hp->h_addr, (caddr_t)&server_addr.sin_addr, hp->h_length);
  208.     server_addr.sin_family = AF_INET;
  209.     minutetimeout.tv_sec = 60;
  210.     minutetimeout.tv_usec = 0;
  211.     server_addr.sin_port = htons(PMAPPORT);
  212.     if ((client = clnttcp_create(&server_addr, PMAPPROG,
  213.         PMAPVERS, &socket, 50, 500)) == NULL) {
  214.         clnt_pcreateerror("rpcinfo: can't contact portmapper");
  215.         exit(1);
  216.     }
  217.     if ((rpc_stat = clnt_call(client, PMAPPROC_DUMP, xdr_void, NULL,
  218.         xdr_pmaplist, &head, minutetimeout)) != RPC_SUCCESS) {
  219.         fprintf(stderr, "rpcinfo: can't contact portmapper: ");
  220.         clnt_perrno(rpc_stat);
  221.         fprintf(stderr, "\n");
  222.         exit(1);
  223.     }
  224.     if (head == NULL) {
  225.         printf("No remote programs registered.\n");
  226.     } else {
  227.         printf("   program vers proto   port\n");
  228.         for (; head != NULL; head = head->pml_next) {
  229.             printf("%10ld%5ld",
  230.                 head->pml_map.pm_prog,
  231.                 head->pml_map.pm_vers);
  232.             if (head->pml_map.pm_prot == IPPROTO_UDP)
  233.                 printf("%6s",  "udp");
  234.             else if (head->pml_map.pm_prot == IPPROTO_TCP)
  235.                 printf("%6s", "tcp");
  236.             else
  237.                 printf("%6ld",  head->pml_map.pm_prot);
  238.             printf("%7ld",  head->pml_map.pm_port);
  239.             rpc = getrpcbynumber(head->pml_map.pm_prog);
  240.             if (rpc)
  241.                 printf("  %s\n", rpc->r_name);
  242.             else
  243.                 printf("\n");
  244.         }
  245.     }
  246. }
  247.  
  248. usage()
  249. {
  250.     fprintf(stderr, "Usage: rpcinfo -u host prognum [versnum]\n");
  251.     fprintf(stderr, "       rpcinfo -t host prognum [versnum]\n");
  252.     fprintf(stderr, "       rpcinfo -p [host]\n");
  253. }
  254.