]>
git.saurik.com Git - apple/network_cmds.git/blob - rpcinfo.tproj/rpcinfo.c
afa8f70be49254583f22e0a79503dad969479737
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* @(#)rpcinfo.c 2.2 88/08/11 4.0 RPCSRC */
25 static char sccsid
[] = "@(#)rpcinfo.c 1.22 87/08/12 SMI";
29 * Copyright (C) 1986, Sun Microsystems, Inc.
33 * rpcinfo: ping a particular rpc program
34 * or dump the portmapper
38 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
39 * unrestricted use provided that this legend is included on all tape
40 * media and as a part of the software program in whole or part. Users
41 * may copy or modify Sun RPC without charge, but are not authorized
42 * to license or distribute it to anyone else except as part of a product or
43 * program developed by the user.
45 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
46 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
47 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
49 * Sun RPC is provided with no support and without any obligation on the
50 * part of Sun Microsystems, Inc. to assist in its use, correction,
51 * modification or enhancement.
53 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
54 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
55 * OR ANY PART THEREOF.
57 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
58 * or profits or other special, indirect and consequential damages, even if
59 * Sun has been advised of the possibility of such damages.
61 * Sun Microsystems, Inc.
63 * Mountain View, California 94043
70 #include <sys/socket.h>
72 #include <rpc/pmap_prot.h>
73 #include <rpc/pmap_clnt.h>
77 #define MAXHOSTLEN 256
79 #define MIN_VERS ((u_long) 0)
80 #define MAX_VERS ((u_long) 4294967295L)
82 static void udpping(/*u_short portflag, int argc, char **argv*/);
83 static void tcpping(/*u_short portflag, int argc, char **argv*/);
84 static int pstatus(/*CLIENT *client, u_long prognum, u_long vers*/);
85 static void pmapdump(/*int argc, char **argv*/);
86 static bool_t
reply_proc(/*void *res, struct sockaddr_in *who*/);
87 static void brdcst(/*int argc, char **argv*/);
88 static void deletereg(/* int argc, char **argv */) ;
89 static void usage(/*void*/);
90 static u_long
getprognum(/*char *arg*/);
91 static u_long
getvers(/*char *arg*/);
92 static void get_inet_address(/*struct sockaddr_in *addr, char *host*/);
93 extern u_long
inet_addr(); /* in 4.2BSD, arpa/inet.h called that a in_addr */
94 extern char *inet_ntoa();
97 * Functions to be performed.
99 #define NONE 0 /* no function */
100 #define PMAPDUMP 1 /* dump portmapper registrations */
101 #define TCPPING 2 /* ping TCP service */
102 #define UDPPING 3 /* ping UDP service */
103 #define BRDCST 4 /* ping broadcast UDP service */
104 #define DELETES 5 /* delete registration for the service */
121 while ((c
= getopt(argc
, argv
, "ptubdn:")) != EOF
) {
125 if (function
!= NONE
)
132 if (function
!= NONE
)
139 if (function
!= NONE
)
146 if (function
!= NONE
)
153 portnum
= (u_short
) atoi(optarg
); /* hope we don't get bogus # */
157 if (function
!= NONE
)
168 if (errflg
|| function
== NONE
) {
180 pmapdump(argc
- optind
, argv
+ optind
);
184 udpping(portnum
, argc
- optind
, argv
+ optind
);
188 tcpping(portnum
, argc
- optind
, argv
+ optind
);
196 brdcst(argc
- optind
, argv
+ optind
);
200 deletereg(argc
- optind
, argv
+ optind
);
208 udpping(portnum
, argc
, argv
)
214 struct sockaddr_in addr
;
215 enum clnt_stat rpc_stat
;
217 u_long prognum
, vers
, minvers
, maxvers
;
218 int sock
= RPC_ANYSOCK
;
219 struct rpc_err rpcerr
;
222 if (argc
< 2 || argc
> 3) {
226 prognum
= getprognum(argv
[1]);
227 get_inet_address(&addr
, argv
[0]);
228 /* Open the socket here so it will survive calls to clnt_destroy */
229 sock
= socket( AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
231 perror("rpcinfo: socket");
237 * A call to version 0 should fail with a program/version
238 * mismatch, and give us the range of versions supported.
240 addr
.sin_port
= htons(portnum
);
243 if ((client
= clntudp_create(&addr
, prognum
, (u_long
)0,
244 to
, &sock
)) == NULL
) {
245 clnt_pcreateerror("rpcinfo");
246 printf("program %lu is not available\n",
252 rpc_stat
= clnt_call(client
, NULLPROC
, xdr_void
, (char *)NULL
,
253 xdr_void
, (char *)NULL
, to
);
254 if (rpc_stat
== RPC_PROGVERSMISMATCH
) {
255 clnt_geterr(client
, &rpcerr
);
256 minvers
= rpcerr
.re_vers
.low
;
257 maxvers
= rpcerr
.re_vers
.high
;
258 } else if (rpc_stat
== RPC_SUCCESS
) {
260 * Oh dear, it DOES support version 0.
261 * Let's try version MAX_VERS.
263 addr
.sin_port
= htons(portnum
);
266 if ((client
= clntudp_create(&addr
, prognum
, MAX_VERS
,
267 to
, &sock
)) == NULL
) {
268 clnt_pcreateerror("rpcinfo");
269 printf("program %lu version %lu is not available\n",
275 rpc_stat
= clnt_call(client
, NULLPROC
, xdr_void
,
276 (char *)NULL
, xdr_void
, (char *)NULL
, to
);
277 if (rpc_stat
== RPC_PROGVERSMISMATCH
) {
278 clnt_geterr(client
, &rpcerr
);
279 minvers
= rpcerr
.re_vers
.low
;
280 maxvers
= rpcerr
.re_vers
.high
;
281 } else if (rpc_stat
== RPC_SUCCESS
) {
283 * It also supports version MAX_VERS.
284 * Looks like we have a wise guy.
285 * OK, we give them information on all
286 * 4 billion versions they support...
291 (void) pstatus(client
, prognum
, MAX_VERS
);
295 (void) pstatus(client
, prognum
, (u_long
)0);
298 clnt_destroy(client
);
299 for (vers
= minvers
; vers
<= maxvers
; vers
++) {
300 addr
.sin_port
= htons(portnum
);
303 if ((client
= clntudp_create(&addr
, prognum
, vers
,
304 to
, &sock
)) == NULL
) {
305 clnt_pcreateerror("rpcinfo");
306 printf("program %lu version %lu is not available\n",
312 rpc_stat
= clnt_call(client
, NULLPROC
, xdr_void
,
313 (char *)NULL
, xdr_void
, (char *)NULL
, to
);
314 if (pstatus(client
, prognum
, vers
) < 0)
316 clnt_destroy(client
);
320 vers
= getvers(argv
[2]);
321 addr
.sin_port
= htons(portnum
);
324 if ((client
= clntudp_create(&addr
, prognum
, vers
,
325 to
, &sock
)) == NULL
) {
326 clnt_pcreateerror("rpcinfo");
327 printf("program %lu version %lu is not available\n",
333 rpc_stat
= clnt_call(client
, 0, xdr_void
, (char *)NULL
,
334 xdr_void
, (char *)NULL
, to
);
335 if (pstatus(client
, prognum
, vers
) < 0)
338 (void) close(sock
); /* Close it up again */
344 tcpping(portnum
, argc
, argv
)
350 struct sockaddr_in addr
;
351 enum clnt_stat rpc_stat
;
353 u_long prognum
, vers
, minvers
, maxvers
;
354 int sock
= RPC_ANYSOCK
;
355 struct rpc_err rpcerr
;
358 if (argc
< 2 || argc
> 3) {
362 prognum
= getprognum(argv
[1]);
363 get_inet_address(&addr
, argv
[0]);
367 * A call to version 0 should fail with a program/version
368 * mismatch, and give us the range of versions supported.
370 addr
.sin_port
= htons(portnum
);
371 if ((client
= clnttcp_create(&addr
, prognum
, MIN_VERS
,
372 &sock
, 0, 0)) == NULL
) {
373 clnt_pcreateerror("rpcinfo");
374 printf("program %lu is not available\n",
380 rpc_stat
= clnt_call(client
, NULLPROC
, xdr_void
, (char *)NULL
,
381 xdr_void
, (char *)NULL
, to
);
382 if (rpc_stat
== RPC_PROGVERSMISMATCH
) {
383 clnt_geterr(client
, &rpcerr
);
384 minvers
= rpcerr
.re_vers
.low
;
385 maxvers
= rpcerr
.re_vers
.high
;
386 } else if (rpc_stat
== RPC_SUCCESS
) {
388 * Oh dear, it DOES support version 0.
389 * Let's try version MAX_VERS.
391 addr
.sin_port
= htons(portnum
);
392 if ((client
= clnttcp_create(&addr
, prognum
, MAX_VERS
,
393 &sock
, 0, 0)) == NULL
) {
394 clnt_pcreateerror("rpcinfo");
395 printf("program %lu version %lu is not available\n",
401 rpc_stat
= clnt_call(client
, NULLPROC
, xdr_void
,
402 (char *)NULL
, xdr_void
, (char *)NULL
, to
);
403 if (rpc_stat
== RPC_PROGVERSMISMATCH
) {
404 clnt_geterr(client
, &rpcerr
);
405 minvers
= rpcerr
.re_vers
.low
;
406 maxvers
= rpcerr
.re_vers
.high
;
407 } else if (rpc_stat
== RPC_SUCCESS
) {
409 * It also supports version MAX_VERS.
410 * Looks like we have a wise guy.
411 * OK, we give them information on all
412 * 4 billion versions they support...
417 (void) pstatus(client
, prognum
, MAX_VERS
);
421 (void) pstatus(client
, prognum
, MIN_VERS
);
424 clnt_destroy(client
);
426 sock
= RPC_ANYSOCK
; /* Re-initialize it for later */
427 for (vers
= minvers
; vers
<= maxvers
; vers
++) {
428 addr
.sin_port
= htons(portnum
);
429 if ((client
= clnttcp_create(&addr
, prognum
, vers
,
430 &sock
, 0, 0)) == NULL
) {
431 clnt_pcreateerror("rpcinfo");
432 printf("program %lu version %lu is not available\n",
438 rpc_stat
= clnt_call(client
, 0, xdr_void
, (char *)NULL
,
439 xdr_void
, (char *)NULL
, to
);
440 if (pstatus(client
, prognum
, vers
) < 0)
442 clnt_destroy(client
);
448 vers
= getvers(argv
[2]);
449 addr
.sin_port
= htons(portnum
);
450 if ((client
= clnttcp_create(&addr
, prognum
, vers
, &sock
,
452 clnt_pcreateerror("rpcinfo");
453 printf("program %lu version %lu is not available\n",
459 rpc_stat
= clnt_call(client
, 0, xdr_void
, (char *)NULL
,
460 xdr_void
, (char *)NULL
, to
);
461 if (pstatus(client
, prognum
, vers
) < 0)
469 * This routine should take a pointer to an "rpc_err" structure, rather than
470 * a pointer to a CLIENT structure, but "clnt_perror" takes a pointer to
471 * a CLIENT structure rather than a pointer to an "rpc_err" structure.
472 * As such, we have to keep the CLIENT structure around in order to print
473 * a good error message.
476 pstatus(client
, prognum
, vers
)
477 register CLIENT
*client
;
481 struct rpc_err rpcerr
;
483 clnt_geterr(client
, &rpcerr
);
484 if (rpcerr
.re_status
!= RPC_SUCCESS
) {
485 clnt_perror(client
, "rpcinfo");
486 printf("program %lu version %lu is not available\n",
490 printf("program %lu version %lu ready and waiting\n",
501 struct sockaddr_in server_addr
;
502 register struct hostent
*hp
;
503 struct pmaplist
*head
= NULL
;
504 int socket
= RPC_ANYSOCK
;
505 struct timeval minutetimeout
;
506 register CLIENT
*client
;
514 get_inet_address(&server_addr
, argv
[0]);
516 bzero((char *)&server_addr
, sizeof server_addr
);
517 server_addr
.sin_family
= AF_INET
;
518 if ((hp
= gethostbyname("localhost")) != NULL
)
519 bcopy(hp
->h_addr
, (caddr_t
)&server_addr
.sin_addr
,
522 server_addr
.sin_addr
.s_addr
= inet_addr("0.0.0.0");
524 minutetimeout
.tv_sec
= 60;
525 minutetimeout
.tv_usec
= 0;
526 server_addr
.sin_port
= htons(PMAPPORT
);
527 if ((client
= clnttcp_create(&server_addr
, PMAPPROG
,
528 PMAPVERS
, &socket
, 50, 500)) == NULL
) {
529 clnt_pcreateerror("rpcinfo: can't contact portmapper");
532 if (clnt_call(client
, PMAPPROC_DUMP
, xdr_void
, NULL
,
533 xdr_pmaplist
, &head
, minutetimeout
) != RPC_SUCCESS
) {
534 fprintf(stderr
, "rpcinfo: can't contact portmapper: ");
535 clnt_perror(client
, "rpcinfo");
539 printf("No remote programs registered.\n");
541 printf(" program vers proto port\n");
542 for (; head
!= NULL
; head
= head
->pml_next
) {
544 head
->pml_map
.pm_prog
,
545 head
->pml_map
.pm_vers
);
546 if (head
->pml_map
.pm_prot
== IPPROTO_UDP
)
547 printf("%6s", "udp");
548 else if (head
->pml_map
.pm_prot
== IPPROTO_TCP
)
549 printf("%6s", "tcp");
551 printf("%6ld", head
->pml_map
.pm_prot
);
552 printf("%7ld", head
->pml_map
.pm_port
);
553 rpc
= getrpcbynumber(head
->pml_map
.pm_prog
);
555 printf(" %s\n", rpc
->r_name
);
563 * reply_proc collects replies from the broadcast.
564 * to get a unique list of responses the output of rpcinfo should
565 * be piped through sort(1) and then uniq(1).
571 void *res
; /* Nothing comes back */
572 struct sockaddr_in
*who
; /* Who sent us the reply */
574 register struct hostent
*hp
;
576 hp
= gethostbyaddr((char *) &who
->sin_addr
, sizeof who
->sin_addr
,
578 printf("%s %s\n", inet_ntoa(who
->sin_addr
),
579 (hp
== NULL
) ? "(unknown)" : hp
->h_name
);
588 enum clnt_stat rpc_stat
;
589 u_long prognum
, vers
;
595 prognum
= getprognum(argv
[0]);
596 vers
= getvers(argv
[1]);
597 rpc_stat
= clnt_broadcast(prognum
, vers
, NULLPROC
, xdr_void
,
598 (char *)NULL
, xdr_void
, (char *)NULL
, reply_proc
);
599 if ((rpc_stat
!= RPC_SUCCESS
) && (rpc_stat
!= RPC_TIMEDOUT
)) {
600 fprintf(stderr
, "rpcinfo: broadcast failed: %s\n",
601 clnt_sperrno(rpc_stat
));
608 deletereg(argc
, argv
)
611 { u_long prog_num
, version_num
;
617 if (getuid()) { /* This command allowed only to root */
618 fprintf(stderr
, "Sorry. You are not root\n") ;
621 prog_num
= getprognum(argv
[0]);
622 version_num
= getvers(argv
[1]);
623 if ((pmap_unset(prog_num
, version_num
)) == 0) {
624 fprintf(stderr
, "rpcinfo: Could not delete registration for prog %s version %s\n",
633 fprintf(stderr
, "Usage: rpcinfo [ -n portnum ] -u host prognum [ versnum ]\n");
634 fprintf(stderr
, " rpcinfo [ -n portnum ] -t host prognum [ versnum ]\n");
635 fprintf(stderr
, " rpcinfo -p [ host ]\n");
636 fprintf(stderr
, " rpcinfo -b prognum versnum\n");
637 fprintf(stderr
, " rpcinfo -d prognum versnum\n") ;
644 register struct rpcent
*rpc
;
645 register u_long prognum
;
648 rpc
= getrpcbyname(arg
);
650 fprintf(stderr
, "rpcinfo: %s is unknown service\n",
654 prognum
= rpc
->r_number
;
656 prognum
= (u_long
) atoi(arg
);
666 register u_long vers
;
668 vers
= (int) atoi(arg
);
673 get_inet_address(addr
, host
)
674 struct sockaddr_in
*addr
;
677 register struct hostent
*hp
;
679 bzero((char *)addr
, sizeof *addr
);
680 addr
->sin_addr
.s_addr
= (u_long
) inet_addr(host
);
681 if (addr
->sin_addr
.s_addr
== -1 || addr
->sin_addr
.s_addr
== 0) {
682 if ((hp
= gethostbyname(host
)) == NULL
) {
683 fprintf(stderr
, "rpcinfo: %s is unknown host\n", host
);
686 bcopy(hp
->h_addr
, (char *)&addr
->sin_addr
, hp
->h_length
);
688 addr
->sin_family
= AF_INET
;