]>
git.saurik.com Git - apple/network_cmds.git/blob - rpcinfo.tproj/rpcinfo.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 /* @(#)rpcinfo.c 2.2 88/08/11 4.0 RPCSRC */
26 static char sccsid
[] = "@(#)rpcinfo.c 1.22 87/08/12 SMI";
30 * Copyright (C) 1986, Sun Microsystems, Inc.
34 * rpcinfo: ping a particular rpc program
35 * or dump the portmapper
39 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
40 * unrestricted use provided that this legend is included on all tape
41 * media and as a part of the software program in whole or part. Users
42 * may copy or modify Sun RPC without charge, but are not authorized
43 * to license or distribute it to anyone else except as part of a product or
44 * program developed by the user.
46 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
47 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
48 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
50 * Sun RPC is provided with no support and without any obligation on the
51 * part of Sun Microsystems, Inc. to assist in its use, correction,
52 * modification or enhancement.
54 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
55 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
56 * OR ANY PART THEREOF.
58 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
59 * or profits or other special, indirect and consequential damages, even if
60 * Sun has been advised of the possibility of such damages.
62 * Sun Microsystems, Inc.
64 * Mountain View, California 94043
71 #include <sys/socket.h>
73 #include <rpc/pmap_prot.h>
74 #include <rpc/pmap_clnt.h>
78 #define MAXHOSTLEN 256
80 #define MIN_VERS ((u_long) 0)
81 #define MAX_VERS ((u_long) 4294967295L)
83 static void udpping(/*u_short portflag, int argc, char **argv*/);
84 static void tcpping(/*u_short portflag, int argc, char **argv*/);
85 static int pstatus(/*CLIENT *client, u_long prognum, u_long vers*/);
86 static void pmapdump(/*int argc, char **argv*/);
87 static bool_t
reply_proc(/*void *res, struct sockaddr_in *who*/);
88 static void brdcst(/*int argc, char **argv*/);
89 static void deletereg(/* int argc, char **argv */) ;
90 static void usage(/*void*/);
91 static u_long
getprognum(/*char *arg*/);
92 static u_long
getvers(/*char *arg*/);
93 static void get_inet_address(/*struct sockaddr_in *addr, char *host*/);
94 extern u_long
inet_addr(); /* in 4.2BSD, arpa/inet.h called that a in_addr */
95 extern char *inet_ntoa();
98 * Functions to be performed.
100 #define NONE 0 /* no function */
101 #define PMAPDUMP 1 /* dump portmapper registrations */
102 #define TCPPING 2 /* ping TCP service */
103 #define UDPPING 3 /* ping UDP service */
104 #define BRDCST 4 /* ping broadcast UDP service */
105 #define DELETES 5 /* delete registration for the service */
122 while ((c
= getopt(argc
, argv
, "ptubdn:")) != EOF
) {
126 if (function
!= NONE
)
133 if (function
!= NONE
)
140 if (function
!= NONE
)
147 if (function
!= NONE
)
154 portnum
= (u_short
) atoi(optarg
); /* hope we don't get bogus # */
158 if (function
!= NONE
)
169 if (errflg
|| function
== NONE
) {
181 pmapdump(argc
- optind
, argv
+ optind
);
185 udpping(portnum
, argc
- optind
, argv
+ optind
);
189 tcpping(portnum
, argc
- optind
, argv
+ optind
);
197 brdcst(argc
- optind
, argv
+ optind
);
201 deletereg(argc
- optind
, argv
+ optind
);
209 udpping(portnum
, argc
, argv
)
215 struct sockaddr_in addr
;
216 enum clnt_stat rpc_stat
;
218 u_long prognum
, vers
, minvers
, maxvers
;
219 int sock
= RPC_ANYSOCK
;
220 struct rpc_err rpcerr
;
223 if (argc
< 2 || argc
> 3) {
227 prognum
= getprognum(argv
[1]);
228 get_inet_address(&addr
, argv
[0]);
229 /* Open the socket here so it will survive calls to clnt_destroy */
230 sock
= socket( AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
232 perror("rpcinfo: socket");
238 * A call to version 0 should fail with a program/version
239 * mismatch, and give us the range of versions supported.
241 addr
.sin_port
= htons(portnum
);
244 if ((client
= clntudp_create(&addr
, prognum
, (u_long
)0,
245 to
, &sock
)) == NULL
) {
246 clnt_pcreateerror("rpcinfo");
247 printf("program %lu is not available\n",
253 rpc_stat
= clnt_call(client
, NULLPROC
, xdr_void
, (char *)NULL
,
254 xdr_void
, (char *)NULL
, to
);
255 if (rpc_stat
== RPC_PROGVERSMISMATCH
) {
256 clnt_geterr(client
, &rpcerr
);
257 minvers
= rpcerr
.re_vers
.low
;
258 maxvers
= rpcerr
.re_vers
.high
;
259 } else if (rpc_stat
== RPC_SUCCESS
) {
261 * Oh dear, it DOES support version 0.
262 * Let's try version MAX_VERS.
264 addr
.sin_port
= htons(portnum
);
267 if ((client
= clntudp_create(&addr
, prognum
, MAX_VERS
,
268 to
, &sock
)) == NULL
) {
269 clnt_pcreateerror("rpcinfo");
270 printf("program %lu version %lu is not available\n",
276 rpc_stat
= clnt_call(client
, NULLPROC
, xdr_void
,
277 (char *)NULL
, xdr_void
, (char *)NULL
, to
);
278 if (rpc_stat
== RPC_PROGVERSMISMATCH
) {
279 clnt_geterr(client
, &rpcerr
);
280 minvers
= rpcerr
.re_vers
.low
;
281 maxvers
= rpcerr
.re_vers
.high
;
282 } else if (rpc_stat
== RPC_SUCCESS
) {
284 * It also supports version MAX_VERS.
285 * Looks like we have a wise guy.
286 * OK, we give them information on all
287 * 4 billion versions they support...
292 (void) pstatus(client
, prognum
, MAX_VERS
);
296 (void) pstatus(client
, prognum
, (u_long
)0);
299 clnt_destroy(client
);
300 for (vers
= minvers
; vers
<= maxvers
; vers
++) {
301 addr
.sin_port
= htons(portnum
);
304 if ((client
= clntudp_create(&addr
, prognum
, vers
,
305 to
, &sock
)) == NULL
) {
306 clnt_pcreateerror("rpcinfo");
307 printf("program %lu version %lu is not available\n",
313 rpc_stat
= clnt_call(client
, NULLPROC
, xdr_void
,
314 (char *)NULL
, xdr_void
, (char *)NULL
, to
);
315 if (pstatus(client
, prognum
, vers
) < 0)
317 clnt_destroy(client
);
321 vers
= getvers(argv
[2]);
322 addr
.sin_port
= htons(portnum
);
325 if ((client
= clntudp_create(&addr
, prognum
, vers
,
326 to
, &sock
)) == NULL
) {
327 clnt_pcreateerror("rpcinfo");
328 printf("program %lu version %lu is not available\n",
334 rpc_stat
= clnt_call(client
, 0, xdr_void
, (char *)NULL
,
335 xdr_void
, (char *)NULL
, to
);
336 if (pstatus(client
, prognum
, vers
) < 0)
339 (void) close(sock
); /* Close it up again */
345 tcpping(portnum
, argc
, argv
)
351 struct sockaddr_in addr
;
352 enum clnt_stat rpc_stat
;
354 u_long prognum
, vers
, minvers
, maxvers
;
355 int sock
= RPC_ANYSOCK
;
356 struct rpc_err rpcerr
;
359 if (argc
< 2 || argc
> 3) {
363 prognum
= getprognum(argv
[1]);
364 get_inet_address(&addr
, argv
[0]);
368 * A call to version 0 should fail with a program/version
369 * mismatch, and give us the range of versions supported.
371 addr
.sin_port
= htons(portnum
);
372 if ((client
= clnttcp_create(&addr
, prognum
, MIN_VERS
,
373 &sock
, 0, 0)) == NULL
) {
374 clnt_pcreateerror("rpcinfo");
375 printf("program %lu is not available\n",
381 rpc_stat
= clnt_call(client
, NULLPROC
, xdr_void
, (char *)NULL
,
382 xdr_void
, (char *)NULL
, to
);
383 if (rpc_stat
== RPC_PROGVERSMISMATCH
) {
384 clnt_geterr(client
, &rpcerr
);
385 minvers
= rpcerr
.re_vers
.low
;
386 maxvers
= rpcerr
.re_vers
.high
;
387 } else if (rpc_stat
== RPC_SUCCESS
) {
389 * Oh dear, it DOES support version 0.
390 * Let's try version MAX_VERS.
392 addr
.sin_port
= htons(portnum
);
393 if ((client
= clnttcp_create(&addr
, prognum
, MAX_VERS
,
394 &sock
, 0, 0)) == NULL
) {
395 clnt_pcreateerror("rpcinfo");
396 printf("program %lu version %lu is not available\n",
402 rpc_stat
= clnt_call(client
, NULLPROC
, xdr_void
,
403 (char *)NULL
, xdr_void
, (char *)NULL
, to
);
404 if (rpc_stat
== RPC_PROGVERSMISMATCH
) {
405 clnt_geterr(client
, &rpcerr
);
406 minvers
= rpcerr
.re_vers
.low
;
407 maxvers
= rpcerr
.re_vers
.high
;
408 } else if (rpc_stat
== RPC_SUCCESS
) {
410 * It also supports version MAX_VERS.
411 * Looks like we have a wise guy.
412 * OK, we give them information on all
413 * 4 billion versions they support...
418 (void) pstatus(client
, prognum
, MAX_VERS
);
422 (void) pstatus(client
, prognum
, MIN_VERS
);
425 clnt_destroy(client
);
427 sock
= RPC_ANYSOCK
; /* Re-initialize it for later */
428 for (vers
= minvers
; vers
<= maxvers
; vers
++) {
429 addr
.sin_port
= htons(portnum
);
430 if ((client
= clnttcp_create(&addr
, prognum
, vers
,
431 &sock
, 0, 0)) == NULL
) {
432 clnt_pcreateerror("rpcinfo");
433 printf("program %lu version %lu is not available\n",
439 rpc_stat
= clnt_call(client
, 0, xdr_void
, (char *)NULL
,
440 xdr_void
, (char *)NULL
, to
);
441 if (pstatus(client
, prognum
, vers
) < 0)
443 clnt_destroy(client
);
449 vers
= getvers(argv
[2]);
450 addr
.sin_port
= htons(portnum
);
451 if ((client
= clnttcp_create(&addr
, prognum
, vers
, &sock
,
453 clnt_pcreateerror("rpcinfo");
454 printf("program %lu version %lu is not available\n",
460 rpc_stat
= clnt_call(client
, 0, xdr_void
, (char *)NULL
,
461 xdr_void
, (char *)NULL
, to
);
462 if (pstatus(client
, prognum
, vers
) < 0)
470 * This routine should take a pointer to an "rpc_err" structure, rather than
471 * a pointer to a CLIENT structure, but "clnt_perror" takes a pointer to
472 * a CLIENT structure rather than a pointer to an "rpc_err" structure.
473 * As such, we have to keep the CLIENT structure around in order to print
474 * a good error message.
477 pstatus(client
, prognum
, vers
)
478 register CLIENT
*client
;
482 struct rpc_err rpcerr
;
484 clnt_geterr(client
, &rpcerr
);
485 if (rpcerr
.re_status
!= RPC_SUCCESS
) {
486 clnt_perror(client
, "rpcinfo");
487 printf("program %lu version %lu is not available\n",
491 printf("program %lu version %lu ready and waiting\n",
502 struct sockaddr_in server_addr
;
503 register struct hostent
*hp
;
504 struct pmaplist
*head
= NULL
;
505 int socket
= RPC_ANYSOCK
;
506 struct timeval minutetimeout
;
507 register CLIENT
*client
;
515 get_inet_address(&server_addr
, argv
[0]);
517 bzero((char *)&server_addr
, sizeof server_addr
);
518 server_addr
.sin_family
= AF_INET
;
519 if ((hp
= gethostbyname("localhost")) != NULL
)
520 bcopy(hp
->h_addr
, (caddr_t
)&server_addr
.sin_addr
,
523 server_addr
.sin_addr
.s_addr
= inet_addr("0.0.0.0");
525 minutetimeout
.tv_sec
= 60;
526 minutetimeout
.tv_usec
= 0;
527 server_addr
.sin_port
= htons(PMAPPORT
);
528 if ((client
= clnttcp_create(&server_addr
, PMAPPROG
,
529 PMAPVERS
, &socket
, 50, 500)) == NULL
) {
530 clnt_pcreateerror("rpcinfo: can't contact portmapper");
533 if (clnt_call(client
, PMAPPROC_DUMP
, xdr_void
, NULL
,
534 xdr_pmaplist
, &head
, minutetimeout
) != RPC_SUCCESS
) {
535 fprintf(stderr
, "rpcinfo: can't contact portmapper: ");
536 clnt_perror(client
, "rpcinfo");
540 printf("No remote programs registered.\n");
542 printf(" program vers proto port\n");
543 for (; head
!= NULL
; head
= head
->pml_next
) {
545 head
->pml_map
.pm_prog
,
546 head
->pml_map
.pm_vers
);
547 if (head
->pml_map
.pm_prot
== IPPROTO_UDP
)
548 printf("%6s", "udp");
549 else if (head
->pml_map
.pm_prot
== IPPROTO_TCP
)
550 printf("%6s", "tcp");
552 printf("%6ld", head
->pml_map
.pm_prot
);
553 printf("%7ld", head
->pml_map
.pm_port
);
554 rpc
= getrpcbynumber(head
->pml_map
.pm_prog
);
556 printf(" %s\n", rpc
->r_name
);
564 * reply_proc collects replies from the broadcast.
565 * to get a unique list of responses the output of rpcinfo should
566 * be piped through sort(1) and then uniq(1).
572 void *res
; /* Nothing comes back */
573 struct sockaddr_in
*who
; /* Who sent us the reply */
575 register struct hostent
*hp
;
577 hp
= gethostbyaddr((char *) &who
->sin_addr
, sizeof who
->sin_addr
,
579 printf("%s %s\n", inet_ntoa(who
->sin_addr
),
580 (hp
== NULL
) ? "(unknown)" : hp
->h_name
);
589 enum clnt_stat rpc_stat
;
590 u_long prognum
, vers
;
596 prognum
= getprognum(argv
[0]);
597 vers
= getvers(argv
[1]);
598 rpc_stat
= clnt_broadcast(prognum
, vers
, NULLPROC
, xdr_void
,
599 (char *)NULL
, xdr_void
, (char *)NULL
, reply_proc
);
600 if ((rpc_stat
!= RPC_SUCCESS
) && (rpc_stat
!= RPC_TIMEDOUT
)) {
601 fprintf(stderr
, "rpcinfo: broadcast failed: %s\n",
602 clnt_sperrno(rpc_stat
));
609 deletereg(argc
, argv
)
612 { u_long prog_num
, version_num
;
618 if (getuid()) { /* This command allowed only to root */
619 fprintf(stderr
, "Sorry. You are not root\n") ;
622 prog_num
= getprognum(argv
[0]);
623 version_num
= getvers(argv
[1]);
624 if ((pmap_unset(prog_num
, version_num
)) == 0) {
625 fprintf(stderr
, "rpcinfo: Could not delete registration for prog %s version %s\n",
634 fprintf(stderr
, "Usage: rpcinfo [ -n portnum ] -u host prognum [ versnum ]\n");
635 fprintf(stderr
, " rpcinfo [ -n portnum ] -t host prognum [ versnum ]\n");
636 fprintf(stderr
, " rpcinfo -p [ host ]\n");
637 fprintf(stderr
, " rpcinfo -b prognum versnum\n");
638 fprintf(stderr
, " rpcinfo -d prognum versnum\n") ;
645 register struct rpcent
*rpc
;
646 register u_long prognum
;
649 rpc
= getrpcbyname(arg
);
651 fprintf(stderr
, "rpcinfo: %s is unknown service\n",
655 prognum
= rpc
->r_number
;
657 prognum
= (u_long
) atoi(arg
);
667 register u_long vers
;
669 vers
= (int) atoi(arg
);
674 get_inet_address(addr
, host
)
675 struct sockaddr_in
*addr
;
678 register struct hostent
*hp
;
680 bzero((char *)addr
, sizeof *addr
);
681 addr
->sin_addr
.s_addr
= (u_long
) inet_addr(host
);
682 if (addr
->sin_addr
.s_addr
== -1 || addr
->sin_addr
.s_addr
== 0) {
683 if ((hp
= gethostbyname(host
)) == NULL
) {
684 fprintf(stderr
, "rpcinfo: %s is unknown host\n", host
);
687 bcopy(hp
->h_addr
, (char *)&addr
->sin_addr
, hp
->h_length
);
689 addr
->sin_family
= AF_INET
;