1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
|
/* ----------------------------------------------------------------------- *
*
* rpc_subs.c - routines for rpc discovery
*
* Copyright 2004 Ian Kent <raven@themaw.net> - All Rights Reserved
* Copyright 2004 Jeff Moyer <jmoyer@redaht.com> - All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
* USA; either version 2 of the License, or (at your option) any later
* version; incorporated herein by reference.
*
* ----------------------------------------------------------------------- */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include "config.h"
#include <rpc/types.h>
#include <rpc/rpc.h>
#include <rpc/pmap_prot.h>
#include <sys/socket.h>
#include <netdb.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <rpcsvc/ypclnt.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <ctype.h>
#include <pthread.h>
#include <poll.h>
#ifdef WITH_LIBTIRPC
#undef auth_destroy
#define auth_destroy(auth) \
do { \
int refs; \
if ((refs = auth_put((auth))) == 0) \
((*((auth)->ah_ops->ah_destroy))(auth));\
} while (0)
#endif
#include "mount.h"
#include "rpc_subs.h"
#include "automount.h"
/* #define STANDALONE */
#ifdef STANDALONE
#define error(logopt, msg, args...) fprintf(stderr, msg "\n", ##args)
#else
#include "log.h"
#endif
#define MAX_IFC_BUF 1024
#define MAX_ERR_BUF 128
#define MAX_NETWORK_LEN 255
/* Get numeric value of the n bits starting at position p */
#define getbits(x, p, n) ((x >> (p + 1 - n)) & ~(~0 << n))
static const rpcvers_t mount_vers[] = {
MOUNTVERS_NFSV3,
MOUNTVERS_POSIX,
MOUNTVERS,
};
static int connect_nb(int, struct sockaddr *, socklen_t, struct timeval *);
inline void dump_core(void);
/*
* Perform a non-blocking connect on the socket fd.
*
* The input struct timeval always has tv_nsec set to zero,
* we only ever use tv_sec for timeouts.
*/
static int connect_nb(int fd, struct sockaddr *addr, socklen_t len, struct timeval *tout)
{
struct pollfd pfd[1];
int timeout = tout->tv_sec;
int flags, ret;
flags = fcntl(fd, F_GETFL, 0);
if (flags < 0)
return -errno;
ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
if (ret < 0)
return -errno;
/*
* From here on subsequent sys calls could change errno so
* we set ret = -errno to capture it in case we decide to
* use it later.
*/
ret = connect(fd, addr, len);
if (ret < 0 && errno != EINPROGRESS) {
ret = -errno;
goto done;
}
if (ret == 0)
goto done;
if (timeout != -1) {
if (timeout >= (INT_MAX - 1)/1000)
timeout = INT_MAX - 1;
else
timeout = timeout * 1000;
}
pfd[0].fd = fd;
pfd[0].events = POLLOUT;
ret = poll(pfd, 1, timeout);
if (ret <= 0) {
if (ret == 0)
ret = -ETIMEDOUT;
else
ret = -errno;
goto done;
}
if (pfd[0].revents) {
int status;
len = sizeof(ret);
status = getsockopt(fd, SOL_SOCKET, SO_ERROR, &ret, &len);
if (status < 0) {
char buf[MAX_ERR_BUF + 1];
char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
/*
* We assume getsockopt amounts to a read on the
* descriptor and gives us the errno we need for
* the POLLERR revent case.
*/
ret = -errno;
/* Unexpected case, log it so we know we got caught */
if (pfd[0].revents & POLLNVAL)
logerr("unexpected poll(2) error on connect:"
" %s", estr);
goto done;
}
/* Oops - something wrong with connect */
if (ret)
ret = -ret;
}
done:
fcntl(fd, F_SETFL, flags);
return ret;
}
#ifndef WITH_LIBTIRPC
static int rpc_do_create_client(struct sockaddr *addr, struct conn_info *info, int *fd, CLIENT **client)
{
CLIENT *clnt = NULL;
struct sockaddr_in in4_laddr;
struct sockaddr_in *in4_raddr;
int type, proto, ret;
socklen_t slen;
*client = NULL;
proto = info->proto->p_proto;
if (proto == IPPROTO_UDP)
type = SOCK_DGRAM;
else
type = SOCK_STREAM;
/*
* bind to any unused port. If we left this up to the rpc
* layer, it would bind to a reserved port, which has been shown
* to exhaust the reserved port range in some situations.
*/
in4_laddr.sin_family = AF_INET;
in4_laddr.sin_port = htons(0);
in4_laddr.sin_addr.s_addr = htonl(INADDR_ANY);
slen = sizeof(struct sockaddr_in);
if (!info->client) {
struct sockaddr *laddr;
*fd = open_sock(addr->sa_family, type, proto);
if (*fd < 0)
return -errno;
laddr = (struct sockaddr *) &in4_laddr;
if (bind(*fd, laddr, slen) < 0)
return -errno;
}
in4_raddr = (struct sockaddr_in *) addr;
in4_raddr->sin_port = htons(info->port);
switch (info->proto->p_proto) {
case IPPROTO_UDP:
clnt = clntudp_bufcreate(in4_raddr,
info->program, info->version,
info->timeout, fd,
info->send_sz, info->recv_sz);
break;
case IPPROTO_TCP:
ret = connect_nb(*fd, addr, slen, &info->timeout);
if (ret < 0)
return ret;
clnt = clnttcp_create(in4_raddr,
info->program, info->version, fd,
info->send_sz, info->recv_sz);
break;
default:
break;
}
*client = clnt;
return 0;
}
#else
static int rpc_do_create_client(struct sockaddr *addr, struct conn_info *info, int *fd, CLIENT **client)
{
CLIENT *clnt = NULL;
struct sockaddr_in in4_laddr;
struct sockaddr_in6 in6_laddr;
struct sockaddr *laddr = NULL;
struct netbuf nb_addr;
int type, proto;
size_t slen;
int ret;
*client = NULL;
proto = info->proto->p_proto;
if (proto == IPPROTO_UDP)
type = SOCK_DGRAM;
else
type = SOCK_STREAM;
/*
* bind to any unused port. If we left this up to the rpc
* layer, it would bind to a reserved port, which has been shown
* to exhaust the reserved port range in some situations.
*/
if (addr->sa_family == AF_INET) {
struct sockaddr_in *in4_raddr = (struct sockaddr_in *) addr;
in4_laddr.sin_family = AF_INET;
in4_laddr.sin_port = htons(0);
in4_laddr.sin_addr.s_addr = htonl(INADDR_ANY);
laddr = (struct sockaddr *) &in4_laddr;
in4_raddr->sin_port = htons(info->port);
slen = sizeof(struct sockaddr_in);
} else if (addr->sa_family == AF_INET6) {
struct sockaddr_in6 *in6_raddr = (struct sockaddr_in6 *) addr;
in6_laddr.sin6_family = AF_INET6;
in6_laddr.sin6_port = htons(0);
in6_laddr.sin6_addr = in6addr_any;
laddr = (struct sockaddr *) &in6_laddr;
in6_raddr->sin6_port = htons(info->port);
slen = sizeof(struct sockaddr_in6);
} else
return -EINVAL;
/*
* bind to any unused port. If we left this up to the rpc layer,
* it would bind to a reserved port, which has been shown to
* exhaust the reserved port range in some situations.
*/
if (!info->client) {
*fd = open_sock(addr->sa_family, type, proto);
if (*fd < 0) {
ret = -errno;
return ret;
}
if (bind(*fd, laddr, slen) < 0) {
ret = -errno;
return ret;
}
}
nb_addr.maxlen = nb_addr.len = slen;
nb_addr.buf = addr;
if (info->proto->p_proto == IPPROTO_UDP)
clnt = clnt_dg_create(*fd, &nb_addr,
info->program, info->version,
info->send_sz, info->recv_sz);
else if (info->proto->p_proto == IPPROTO_TCP) {
ret = connect_nb(*fd, addr, slen, &info->timeout);
if (ret < 0)
return ret;
clnt = clnt_vc_create(*fd, &nb_addr,
info->program, info->version,
info->send_sz, info->recv_sz);
} else
return -EINVAL;
/* Our timeout is in seconds */
if (clnt && info->timeout.tv_sec)
clnt_control(clnt, CLSET_TIMEOUT, (void *) &info->timeout);
*client = clnt;
return 0;
}
#endif
/*
* Create an RPC client
*/
static int create_client(struct conn_info *info, CLIENT **client)
{
struct addrinfo *ai, *haddr;
struct addrinfo hints;
int fd, ret;
fd = RPC_ANYSOCK;
*client = NULL;
if (info->client) {
if (!clnt_control(info->client, CLGET_FD, (char *) &fd)) {
fd = RPC_ANYSOCK;
clnt_destroy(info->client);
info->client = NULL;
} else {
clnt_control(info->client, CLSET_FD_NCLOSE, NULL);
clnt_destroy(info->client);
}
}
if (info->addr) {
ret = rpc_do_create_client(info->addr, info, &fd, client);
if (ret == 0)
goto done;
if (ret == -EHOSTUNREACH)
goto out_close;
if (!info->client && fd != RPC_ANYSOCK) {
close(fd);
fd = RPC_ANYSOCK;
}
}
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_ADDRCONFIG;
hints.ai_family = AF_UNSPEC;
if (info->proto->p_proto == IPPROTO_UDP)
hints.ai_socktype = SOCK_DGRAM;
else
hints.ai_socktype = SOCK_STREAM;
ret = getaddrinfo(info->host, NULL, &hints, &ai);
if (ret) {
error(LOGOPT_ANY,
"hostname lookup failed: %s", gai_strerror(ret));
info->client = NULL;
goto out_close;
}
haddr = ai;
while (haddr) {
if (haddr->ai_protocol != info->proto->p_proto) {
haddr = haddr->ai_next;
continue;
}
ret = rpc_do_create_client(haddr->ai_addr, info, &fd, client);
if (ret == 0)
break;
if (ret == -EHOSTUNREACH)
goto out_close;
if (!info->client && fd != RPC_ANYSOCK) {
close(fd);
fd = RPC_ANYSOCK;
}
haddr = haddr->ai_next;
}
freeaddrinfo(ai);
done:
if (!*client) {
info->client = NULL;
ret = -ENOTCONN;
goto out_close;
}
/* Close socket fd on destroy, as is default for rpcowned fds */
if (!clnt_control(*client, CLSET_FD_CLOSE, NULL)) {
clnt_destroy(*client);
info->client = NULL;
ret = -ENOTCONN;
goto out_close;
}
return 0;
out_close:
if (fd != -1)
close(fd);
return ret;
}
int rpc_udp_getclient(struct conn_info *info,
unsigned int program, unsigned int version)
{
struct protoent *pe_proto;
CLIENT *client;
int ret;
if (!info->client) {
pe_proto = getprotobyname("udp");
if (!pe_proto)
return -ENOENT;
info->proto = pe_proto;
info->timeout.tv_sec = RPC_TOUT_UDP;
info->timeout.tv_usec = 0;
info->send_sz = UDPMSGSIZE;
info->recv_sz = UDPMSGSIZE;
}
info->program = program;
info->version = version;
ret = create_client(info, &client);
if (ret < 0)
return ret;
info->client = client;
return 0;
}
void rpc_destroy_udp_client(struct conn_info *info)
{
if (!info->client)
return;
clnt_destroy(info->client);
info->client = NULL;
return;
}
int rpc_tcp_getclient(struct conn_info *info,
unsigned int program, unsigned int version)
{
struct protoent *pe_proto;
CLIENT *client;
int ret;
if (!info->client) {
pe_proto = getprotobyname("tcp");
if (!pe_proto)
return -ENOENT;
info->proto = pe_proto;
info->timeout.tv_sec = RPC_TOUT_TCP;
info->timeout.tv_usec = 0;
info->send_sz = 0;
info->recv_sz = 0;
}
info->program = program;
info->version = version;
ret = create_client(info, &client);
if (ret < 0)
return ret;
info->client = client;
return 0;
}
void rpc_destroy_tcp_client(struct conn_info *info)
{
struct linger lin = { 1, 0 };
socklen_t lin_len = sizeof(struct linger);
int fd;
if (!info->client)
return;
if (!clnt_control(info->client, CLGET_FD, (char *) &fd))
fd = -1;
switch (info->close_option) {
case RPC_CLOSE_NOLINGER:
if (fd >= 0)
setsockopt(fd, SOL_SOCKET, SO_LINGER, &lin, lin_len);
break;
}
clnt_destroy(info->client);
info->client = NULL;
return;
}
int rpc_portmap_getclient(struct conn_info *info,
const char *host, struct sockaddr *addr, size_t addr_len,
const char *proto, unsigned int option)
{
struct protoent *pe_proto;
CLIENT *client;
int ret;
pe_proto = getprotobyname(proto);
if (!pe_proto)
return -ENOENT;
info->host = host;
info->addr = addr;
info->addr_len = addr_len;
info->program = PMAPPROG;
info->port = PMAPPORT;
info->version = PMAPVERS;
info->proto = pe_proto;
info->send_sz = RPCSMALLMSGSIZE;
info->recv_sz = RPCSMALLMSGSIZE;
info->timeout.tv_sec = PMAP_TOUT_UDP;
info->timeout.tv_usec = 0;
info->close_option = option;
info->client = NULL;
if (pe_proto->p_proto == IPPROTO_TCP)
info->timeout.tv_sec = PMAP_TOUT_TCP;
ret = create_client(info, &client);
if (ret < 0)
return ret;
info->client = client;
return 0;
}
int rpc_portmap_getport(struct conn_info *info,
struct pmap *parms, unsigned short *port)
{
struct conn_info pmap_info;
CLIENT *client;
enum clnt_stat status;
int proto = info->proto->p_proto;
int ret;
memset(&pmap_info, 0, sizeof(struct conn_info));
if (proto == IPPROTO_TCP)
pmap_info.timeout.tv_sec = PMAP_TOUT_TCP;
else
pmap_info.timeout.tv_sec = PMAP_TOUT_UDP;
if (info->client)
client = info->client;
else {
pmap_info.host = info->host;
pmap_info.addr = info->addr;
pmap_info.addr_len = info->addr_len;
pmap_info.port = PMAPPORT;
pmap_info.program = PMAPPROG;
pmap_info.version = PMAPVERS;
pmap_info.proto = info->proto;
pmap_info.send_sz = RPCSMALLMSGSIZE;
pmap_info.recv_sz = RPCSMALLMSGSIZE;
ret = create_client(&pmap_info, &client);
if (ret < 0)
return ret;
}
/*
* Check to see if server is up otherwise a getport will take
* forever to timeout.
*/
status = clnt_call(client, PMAPPROC_NULL,
(xdrproc_t) xdr_void, 0, (xdrproc_t) xdr_void, 0,
pmap_info.timeout);
if (status == RPC_SUCCESS) {
status = clnt_call(client, PMAPPROC_GETPORT,
(xdrproc_t) xdr_pmap, (caddr_t) parms,
(xdrproc_t) xdr_u_short, (caddr_t) port,
pmap_info.timeout);
}
if (!info->client) {
/*
* Only play with the close options if we think it
* completed OK
*/
if (proto == IPPROTO_TCP && status == RPC_SUCCESS) {
struct linger lin = { 1, 0 };
socklen_t lin_len = sizeof(struct linger);
int fd;
if (!clnt_control(client, CLGET_FD, (char *) &fd))
fd = -1;
switch (info->close_option) {
case RPC_CLOSE_NOLINGER:
if (fd >= 0)
setsockopt(fd, SOL_SOCKET, SO_LINGER, &lin, lin_len);
break;
}
}
clnt_destroy(client);
}
if (status == RPC_TIMEDOUT)
return -ETIMEDOUT;
else if (status != RPC_SUCCESS)
return -EIO;
return 0;
}
int rpc_ping_proto(struct conn_info *info)
{
CLIENT *client;
enum clnt_stat status;
int proto = info->proto->p_proto;
int ret;
if (info->client)
client = info->client;
else {
if (info->proto->p_proto == IPPROTO_UDP) {
info->send_sz = UDPMSGSIZE;
info->recv_sz = UDPMSGSIZE;
}
ret = create_client(info, &client);
if (ret < 0)
return ret;
}
clnt_control(client, CLSET_TIMEOUT, (char *) &info->timeout);
clnt_control(client, CLSET_RETRY_TIMEOUT, (char *) &info->timeout);
status = clnt_call(client, NFSPROC_NULL,
(xdrproc_t) xdr_void, 0, (xdrproc_t) xdr_void, 0,
info->timeout);
if (!info->client) {
/*
* Only play with the close options if we think it
* completed OK
*/
if (proto == IPPROTO_TCP && status == RPC_SUCCESS) {
struct linger lin = { 1, 0 };
socklen_t lin_len = sizeof(struct linger);
int fd;
if (!clnt_control(client, CLGET_FD, (char *) &fd))
fd = -1;
switch (info->close_option) {
case RPC_CLOSE_NOLINGER:
if (fd >= 0)
setsockopt(fd, SOL_SOCKET, SO_LINGER, &lin, lin_len);
break;
}
}
clnt_destroy(client);
}
if (status == RPC_TIMEDOUT)
return -ETIMEDOUT;
else if (status != RPC_SUCCESS)
return -EIO;
return 1;
}
static unsigned int __rpc_ping(const char *host,
unsigned long version,
char *proto,
long seconds, long micros,
unsigned int option)
{
unsigned int status;
struct conn_info info;
struct pmap parms;
info.host = host;
info.addr = NULL;
info.addr_len = 0;
info.program = NFS_PROGRAM;
info.version = version;
info.send_sz = 0;
info.recv_sz = 0;
info.timeout.tv_sec = seconds;
info.timeout.tv_usec = micros;
info.close_option = option;
info.client = NULL;
status = RPC_PING_FAIL;
info.proto = getprotobyname(proto);
if (!info.proto)
return status;
parms.pm_prog = NFS_PROGRAM;
parms.pm_vers = version;
parms.pm_prot = info.proto->p_proto;
parms.pm_port = 0;
status = rpc_portmap_getport(&info, &parms, &info.port);
if (status < 0)
return status;
status = rpc_ping_proto(&info);
return status;
}
int rpc_ping(const char *host, long seconds, long micros, unsigned int option)
{
unsigned long vers3 = NFS3_VERSION;
unsigned long vers2 = NFS2_VERSION;
unsigned int status;
status = __rpc_ping(host, vers2, "udp", seconds, micros, option);
if (status > 0)
return RPC_PING_V2 | RPC_PING_UDP;
status = __rpc_ping(host, vers3, "udp", seconds, micros, option);
if (status > 0)
return RPC_PING_V3 | RPC_PING_UDP;
status = __rpc_ping(host, vers2, "tcp", seconds, micros, option);
if (status > 0)
return RPC_PING_V2 | RPC_PING_TCP;
status = __rpc_ping(host, vers3, "tcp", seconds, micros, option);
if (status > 0)
return RPC_PING_V3 | RPC_PING_TCP;
return status;
}
double elapsed(struct timeval start, struct timeval end)
{
double t1, t2;
t1 = (double)start.tv_sec + (double)start.tv_usec/(1000*1000);
t2 = (double)end.tv_sec + (double)end.tv_usec/(1000*1000);
return t2-t1;
}
int rpc_time(const char *host,
unsigned int ping_vers, unsigned int ping_proto,
long seconds, long micros, unsigned int option, double *result)
{
int status;
double taken;
struct timeval start, end;
struct timezone tz;
char *proto = (ping_proto & RPC_PING_UDP) ? "udp" : "tcp";
unsigned long vers = ping_vers;
gettimeofday(&start, &tz);
status = __rpc_ping(host, vers, proto, seconds, micros, option);
gettimeofday(&end, &tz);
if (status == RPC_PING_FAIL || status < 0)
return status;
taken = elapsed(start, end);
if (result != NULL)
*result = taken;
return status;
}
static int rpc_get_exports_proto(struct conn_info *info, exports *exp)
{
CLIENT *client;
enum clnt_stat status;
int proto = info->proto->p_proto;
unsigned int option = info->close_option;
int vers_entry;
int ret;
if (info->proto->p_proto == IPPROTO_UDP) {
info->send_sz = UDPMSGSIZE;
info->recv_sz = UDPMSGSIZE;
}
ret = create_client(info, &client);
if (ret < 0)
return 0;
clnt_control(client, CLSET_TIMEOUT, (char *) &info->timeout);
clnt_control(client, CLSET_RETRY_TIMEOUT, (char *) &info->timeout);
client->cl_auth = authunix_create_default();
vers_entry = 0;
while (1) {
status = clnt_call(client, MOUNTPROC_EXPORT,
(xdrproc_t) xdr_void, NULL,
(xdrproc_t) xdr_exports, (caddr_t) exp,
info->timeout);
if (status == RPC_SUCCESS)
break;
if (++vers_entry > 2)
break;
CLNT_CONTROL(client, CLSET_VERS,
(void *) &mount_vers[vers_entry]);
}
/* Only play with the close options if we think it completed OK */
if (proto == IPPROTO_TCP && status == RPC_SUCCESS) {
struct linger lin = { 1, 0 };
socklen_t lin_len = sizeof(struct linger);
int fd;
if (!clnt_control(client, CLGET_FD, (char *) &fd))
fd = -1;
switch (option) {
case RPC_CLOSE_NOLINGER:
if (fd >= 0)
setsockopt(fd, SOL_SOCKET, SO_LINGER, &lin, lin_len);
break;
}
}
auth_destroy(client->cl_auth);
clnt_destroy(client);
if (status != RPC_SUCCESS)
return 0;
return 1;
}
static void rpc_export_free(exports item)
{
groups grp;
groups tmp;
if (item->ex_dir)
free(item->ex_dir);
grp = item->ex_groups;
while (grp) {
if (grp->gr_name)
free(grp->gr_name);
tmp = grp;
grp = grp->gr_next;
free(tmp);
}
free(item);
}
void rpc_exports_free(exports list)
{
exports tmp;
while (list) {
tmp = list;
list = list->ex_next;
rpc_export_free(tmp);
}
return;
}
exports rpc_get_exports(const char *host, long seconds, long micros, unsigned int option)
{
struct conn_info info;
exports exportlist;
struct pmap parms;
int status;
info.host = host;
info.addr = NULL;
info.addr_len = 0;
info.program = MOUNTPROG;
info.version = mount_vers[0];
info.send_sz = 0;
info.recv_sz = 0;
info.timeout.tv_sec = seconds;
info.timeout.tv_usec = micros;
info.close_option = option;
info.client = NULL;
parms.pm_prog = info.program;
parms.pm_vers = info.version;
parms.pm_port = 0;
/* Try UDP first */
info.proto = getprotobyname("udp");
if (!info.proto)
goto try_tcp;
parms.pm_prot = info.proto->p_proto;
status = rpc_portmap_getport(&info, &parms, &info.port);
if (status < 0)
goto try_tcp;
memset(&exportlist, '\0', sizeof(exportlist));
status = rpc_get_exports_proto(&info, &exportlist);
if (status)
return exportlist;
try_tcp:
info.proto = getprotobyname("tcp");
if (!info.proto)
return NULL;
parms.pm_prot = info.proto->p_proto;
status = rpc_portmap_getport(&info, &parms, &info.port);
if (status < 0)
return NULL;
memset(&exportlist, '\0', sizeof(exportlist));
status = rpc_get_exports_proto(&info, &exportlist);
if (!status)
return NULL;
return exportlist;
}
const char *get_addr_string(struct sockaddr *sa, char *name, socklen_t len)
{
void *addr;
if (len < INET6_ADDRSTRLEN)
return NULL;
if (sa->sa_family == AF_INET) {
struct sockaddr_in *ipv4 = (struct sockaddr_in *) sa;
addr = &(ipv4->sin_addr);
} else if (sa->sa_family == AF_INET6) {
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *) sa;
addr = &(ipv6->sin6_addr);
} else {
return NULL;
}
return inet_ntop(sa->sa_family, addr, name, len);
}
#if 0
#include <stdio.h>
int main(int argc, char **argv)
{
int ret;
double res = 0.0;
exports exportlist, tmp;
groups grouplist;
int n, maxlen;
/*
ret = rpc_ping("budgie", 10, 0, RPC_CLOSE_DEFAULT);
printf("ret = %d\n", ret);
res = 0.0;
ret = rpc_time("budgie", NFS2_VERSION, RPC_PING_TCP, 10, 0, RPC_CLOSE_DEFAULT, &res);
printf("v2 tcp ret = %d, res = %f\n", ret, res);
res = 0.0;
ret = rpc_time("budgie", NFS3_VERSION, RPC_PING_TCP, 10, 0, RPC_CLOSE_DEFAULT, &res);
printf("v3 tcp ret = %d, res = %f\n", ret, res);
res = 0.0;
ret = rpc_time("budgie", NFS2_VERSION, RPC_PING_UDP, 10, 0, RPC_CLOSE_DEFAULT, &res);
printf("v2 udp ret = %d, res = %f\n", ret, res);
res = 0.0;
ret = rpc_time("budgie", NFS3_VERSION, RPC_PING_UDP, 10, 0, RPC_CLOSE_DEFAULT, &res);
printf("v3 udp ret = %d, res = %f\n", ret, res);
*/
exportlist = rpc_get_exports("budgie", 10, 0, RPC_CLOSE_NOLINGER);
exportlist = rpc_exports_prune(exportlist);
maxlen = 0;
for (tmp = exportlist; tmp; tmp = tmp->ex_next) {
if ((n = strlen(tmp->ex_dir)) > maxlen)
maxlen = n;
}
if (exportlist) {
while (exportlist) {
printf("%-*s ", maxlen, exportlist->ex_dir);
grouplist = exportlist->ex_groups;
if (grouplist) {
while (grouplist) {
printf("%s%s", grouplist->gr_name,
grouplist->gr_next ? "," : "");
grouplist = grouplist->gr_next;
}
}
printf("\n");
exportlist = exportlist->ex_next;
}
}
rpc_exports_free(exportlist);
exit(0);
}
#endif
|