]>
Commit | Line | Data |
---|---|---|
c28fb3c8 | 1 | /* |
a7ab6ec8 UD |
2 | * clnt_simple.c |
3 | * Simplified front end to rpc. | |
ab09b221 | 4 | * |
a7ab6ec8 | 5 | * Copyright (c) 2010, Oracle America, Inc. |
cb636bb2 | 6 | * |
a7ab6ec8 UD |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions are | |
9 | * met: | |
cb636bb2 | 10 | * |
a7ab6ec8 UD |
11 | * * Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * * Redistributions in binary form must reproduce the above | |
14 | * copyright notice, this list of conditions and the following | |
15 | * disclaimer in the documentation and/or other materials | |
16 | * provided with the distribution. | |
17 | * * Neither the name of the "Oracle America, Inc." nor the names of its | |
18 | * contributors may be used to endorse or promote products derived | |
19 | * from this software without specific prior written permission. | |
ab09b221 | 20 | * |
a7ab6ec8 UD |
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
24 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
25 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
28 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
30 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
31 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
32 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28f540f4 RM |
33 | */ |
34 | ||
e4cf5070 UD |
35 | #include <alloca.h> |
36 | #include <errno.h> | |
28f540f4 | 37 | #include <stdio.h> |
e7fd8a39 | 38 | #include <unistd.h> |
28f540f4 RM |
39 | #include <rpc/rpc.h> |
40 | #include <sys/socket.h> | |
41 | #include <netdb.h> | |
a5a0310d | 42 | #include <string.h> |
28f540f4 | 43 | |
f1e4a4a4 | 44 | struct callrpc_private_s |
e7fd8a39 UD |
45 | { |
46 | CLIENT *client; | |
47 | int socket; | |
48 | u_long oldprognum, oldversnum, valid; | |
49 | char *oldhost; | |
f1e4a4a4 UD |
50 | }; |
51 | #ifdef _RPC_THREAD_SAFE_ | |
1bc1a2b9 | 52 | #define callrpc_private RPC_THREAD_VARIABLE(callrpc_private_s) |
f1e4a4a4 UD |
53 | #else |
54 | static struct callrpc_private_s *callrpc_private; | |
55 | #endif | |
28f540f4 | 56 | |
e7fd8a39 UD |
57 | int |
58 | callrpc (const char *host, u_long prognum, u_long versnum, u_long procnum, | |
59 | xdrproc_t inproc, const char *in, xdrproc_t outproc, char *out) | |
28f540f4 | 60 | { |
f1e4a4a4 | 61 | struct callrpc_private_s *crp = callrpc_private; |
e7fd8a39 UD |
62 | struct sockaddr_in server_addr; |
63 | enum clnt_stat clnt_stat; | |
64 | struct hostent hostbuf, *hp; | |
65 | struct timeval timeout, tottimeout; | |
28f540f4 | 66 | |
e7fd8a39 UD |
67 | if (crp == 0) |
68 | { | |
f1e4a4a4 | 69 | crp = (struct callrpc_private_s *) calloc (1, sizeof (*crp)); |
e7fd8a39 UD |
70 | if (crp == 0) |
71 | return 0; | |
72 | callrpc_private = crp; | |
73 | } | |
74 | if (crp->oldhost == NULL) | |
75 | { | |
76 | crp->oldhost = malloc (256); | |
77 | crp->oldhost[0] = 0; | |
78 | crp->socket = RPC_ANYSOCK; | |
79 | } | |
80 | if (crp->valid && crp->oldprognum == prognum && crp->oldversnum == versnum | |
81 | && strcmp (crp->oldhost, host) == 0) | |
82 | { | |
83 | /* reuse old client */ | |
84 | } | |
85 | else | |
86 | { | |
87 | size_t buflen; | |
88 | char *buffer; | |
89 | int herr; | |
90 | ||
91 | crp->valid = 0; | |
92 | if (crp->socket != RPC_ANYSOCK) | |
93 | { | |
50304ef0 | 94 | (void) __close (crp->socket); |
e7fd8a39 | 95 | crp->socket = RPC_ANYSOCK; |
28f540f4 | 96 | } |
e7fd8a39 UD |
97 | if (crp->client) |
98 | { | |
99 | clnt_destroy (crp->client); | |
100 | crp->client = NULL; | |
28f540f4 | 101 | } |
e4cf5070 | 102 | |
e7fd8a39 UD |
103 | buflen = 1024; |
104 | buffer = __alloca (buflen); | |
105 | while (__gethostbyname_r (host, &hostbuf, buffer, buflen, | |
1d863dc0 UD |
106 | &hp, &herr) != 0 |
107 | || hp == NULL) | |
e7fd8a39 UD |
108 | if (herr != NETDB_INTERNAL || errno != ERANGE) |
109 | return (int) RPC_UNKNOWNHOST; | |
110 | else | |
111 | { | |
112 | /* Enlarge the buffer. */ | |
113 | buflen *= 2; | |
114 | buffer = __alloca (buflen); | |
115 | } | |
e4cf5070 | 116 | |
e7fd8a39 UD |
117 | timeout.tv_usec = 0; |
118 | timeout.tv_sec = 5; | |
9af652f6 | 119 | memcpy ((char *) &server_addr.sin_addr, hp->h_addr, hp->h_length); |
e7fd8a39 UD |
120 | server_addr.sin_family = AF_INET; |
121 | server_addr.sin_port = 0; | |
7b57bfe5 | 122 | if ((crp->client = clntudp_create (&server_addr, (u_long) prognum, |
e7fd8a39 | 123 | (u_long) versnum, timeout, &crp->socket)) == NULL) |
543cf8a9 | 124 | return (int) get_rpc_createerr().cf_stat; |
e7fd8a39 UD |
125 | crp->valid = 1; |
126 | crp->oldprognum = prognum; | |
127 | crp->oldversnum = versnum; | |
128 | (void) strncpy (crp->oldhost, host, 255); | |
6bf22cc7 | 129 | crp->oldhost[255] = '\0'; |
e7fd8a39 UD |
130 | } |
131 | tottimeout.tv_sec = 25; | |
132 | tottimeout.tv_usec = 0; | |
133 | clnt_stat = clnt_call (crp->client, procnum, inproc, (char *) in, | |
134 | outproc, out, tottimeout); | |
135 | /* | |
136 | * if call failed, empty cache | |
137 | */ | |
138 | if (clnt_stat != RPC_SUCCESS) | |
139 | crp->valid = 0; | |
140 | return (int) clnt_stat; | |
28f540f4 | 141 | } |
021db4be | 142 | libc_hidden_nolink_sunrpc (callrpc, GLIBC_2_0) |
f1e4a4a4 UD |
143 | |
144 | #ifdef _RPC_THREAD_SAFE_ | |
145 | void | |
146 | __rpc_thread_clnt_cleanup (void) | |
147 | { | |
148 | struct callrpc_private_s *rcp = RPC_THREAD_VARIABLE(callrpc_private_s); | |
149 | ||
150 | if (rcp) { | |
151 | if (rcp->client) | |
152 | CLNT_DESTROY (rcp->client); | |
153 | free (rcp); | |
154 | } | |
155 | } | |
156 | #endif /* _RPC_THREAD_SAFE_ */ |