]>
Commit | Line | Data |
---|---|---|
28f540f4 | 1 | /* |
a7ab6ec8 | 2 | * Copyright (c) 2010, Oracle America, Inc. |
cb636bb2 | 3 | * |
a7ab6ec8 UD |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions are | |
6 | * met: | |
cb636bb2 | 7 | * |
a7ab6ec8 UD |
8 | * * Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * * Redistributions in binary form must reproduce the above | |
11 | * copyright notice, this list of conditions and the following | |
12 | * disclaimer in the documentation and/or other materials | |
13 | * provided with the distribution. | |
14 | * * Neither the name of the "Oracle America, Inc." nor the names of its | |
15 | * contributors may be used to endorse or promote products derived | |
16 | * from this software without specific prior written permission. | |
28f540f4 | 17 | * |
a7ab6ec8 UD |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
25 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
27 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28f540f4 | 30 | */ |
f8afba91 | 31 | |
e4cf5070 UD |
32 | #include <alloca.h> |
33 | #include <errno.h> | |
e7fd8a39 | 34 | #include <string.h> |
28f540f4 RM |
35 | #include <rpc/rpc.h> |
36 | #include <sys/socket.h> | |
28f540f4 RM |
37 | #include <netdb.h> |
38 | ||
39 | /* | |
40 | * Generic client creation: takes (hostname, program-number, protocol) and | |
e4cf5070 | 41 | * returns client handle. Default options are set, which the user can |
28f540f4 RM |
42 | * change using the rpc equivalent of ioctl()'s. |
43 | */ | |
44 | CLIENT * | |
e7fd8a39 UD |
45 | clnt_create (const char *hostname, u_long prog, u_long vers, |
46 | const char *proto) | |
28f540f4 | 47 | { |
e7fd8a39 UD |
48 | struct hostent hostbuf, *h; |
49 | size_t hstbuflen; | |
50 | char *hsttmpbuf; | |
51 | struct protoent protobuf, *p; | |
52 | size_t prtbuflen; | |
53 | char *prttmpbuf; | |
54 | struct sockaddr_in sin; | |
e852e889 | 55 | struct sockaddr_un sun; |
e7fd8a39 UD |
56 | int sock; |
57 | struct timeval tv; | |
58 | CLIENT *client; | |
59 | int herr; | |
e4cf5070 | 60 | |
e852e889 UD |
61 | if (strcmp (proto, "unix") == 0) |
62 | { | |
63 | __bzero ((char *)&sun, sizeof (sun)); | |
64 | sun.sun_family = AF_UNIX; | |
65 | strcpy (sun.sun_path, hostname); | |
66 | sock = RPC_ANYSOCK; | |
7b57bfe5 | 67 | client = clntunix_create (&sun, prog, vers, &sock, 0, 0); |
e852e889 UD |
68 | if (client == NULL) |
69 | return NULL; | |
bc710b3b UD |
70 | #if 0 |
71 | /* This is not wanted. This would disable the user from having | |
72 | a timeout in the clnt_call() call. Only a call to cnlt_control() | |
73 | by the user should set the timeout value. */ | |
e852e889 UD |
74 | tv.tv_sec = 25; |
75 | tv.tv_usec = 0; | |
76 | clnt_control (client, CLSET_TIMEOUT, (char *)&tv); | |
bc710b3b | 77 | #endif |
e852e889 UD |
78 | return client; |
79 | } | |
80 | ||
e7fd8a39 UD |
81 | hstbuflen = 1024; |
82 | hsttmpbuf = __alloca (hstbuflen); | |
83 | while (__gethostbyname_r (hostname, &hostbuf, hsttmpbuf, hstbuflen, | |
1d863dc0 UD |
84 | &h, &herr) != 0 |
85 | || h == NULL) | |
e7fd8a39 UD |
86 | if (herr != NETDB_INTERNAL || errno != ERANGE) |
87 | { | |
543cf8a9 | 88 | get_rpc_createerr().cf_stat = RPC_UNKNOWNHOST; |
e7fd8a39 UD |
89 | return NULL; |
90 | } | |
91 | else | |
92 | { | |
93 | /* Enlarge the buffer. */ | |
94 | hstbuflen *= 2; | |
e4cf5070 | 95 | hsttmpbuf = __alloca (hstbuflen); |
e7fd8a39 | 96 | } |
28f540f4 | 97 | |
e7fd8a39 UD |
98 | if (h->h_addrtype != AF_INET) |
99 | { | |
100 | /* | |
101 | * Only support INET for now | |
102 | */ | |
543cf8a9 UD |
103 | struct rpc_createerr *ce = &get_rpc_createerr (); |
104 | ce->cf_stat = RPC_SYSTEMERROR; | |
105 | ce->cf_error.re_errno = EAFNOSUPPORT; | |
e7fd8a39 UD |
106 | return NULL; |
107 | } | |
108 | sin.sin_family = h->h_addrtype; | |
109 | sin.sin_port = 0; | |
50304ef0 | 110 | __bzero (sin.sin_zero, sizeof (sin.sin_zero)); |
9af652f6 | 111 | memcpy ((char *) &sin.sin_addr, h->h_addr, h->h_length); |
e4cf5070 | 112 | |
e7fd8a39 UD |
113 | prtbuflen = 1024; |
114 | prttmpbuf = __alloca (prtbuflen); | |
1d863dc0 UD |
115 | while (__getprotobyname_r (proto, &protobuf, prttmpbuf, prtbuflen, &p) != 0 |
116 | || p == NULL) | |
e7fd8a39 UD |
117 | if (errno != ERANGE) |
118 | { | |
543cf8a9 UD |
119 | struct rpc_createerr *ce = &get_rpc_createerr (); |
120 | ce->cf_stat = RPC_UNKNOWNPROTO; | |
121 | ce->cf_error.re_errno = EPFNOSUPPORT; | |
e7fd8a39 UD |
122 | return NULL; |
123 | } | |
124 | else | |
125 | { | |
126 | /* Enlarge the buffer. */ | |
127 | prtbuflen *= 2; | |
e4cf5070 | 128 | prttmpbuf = __alloca (prtbuflen); |
e7fd8a39 | 129 | } |
e4cf5070 | 130 | |
e7fd8a39 UD |
131 | sock = RPC_ANYSOCK; |
132 | switch (p->p_proto) | |
133 | { | |
134 | case IPPROTO_UDP: | |
135 | tv.tv_sec = 5; | |
136 | tv.tv_usec = 0; | |
7b57bfe5 | 137 | client = clntudp_create (&sin, prog, vers, tv, &sock); |
e7fd8a39 UD |
138 | if (client == NULL) |
139 | { | |
140 | return NULL; | |
141 | } | |
bc710b3b UD |
142 | #if 0 |
143 | /* This is not wanted. This would disable the user from having | |
144 | a timeout in the clnt_call() call. Only a call to cnlt_control() | |
145 | by the user should set the timeout value. */ | |
e7fd8a39 UD |
146 | tv.tv_sec = 25; |
147 | clnt_control (client, CLSET_TIMEOUT, (char *)&tv); | |
bc710b3b | 148 | #endif |
e7fd8a39 UD |
149 | break; |
150 | case IPPROTO_TCP: | |
7b57bfe5 | 151 | client = clnttcp_create (&sin, prog, vers, &sock, 0, 0); |
e7fd8a39 UD |
152 | if (client == NULL) |
153 | { | |
154 | return NULL; | |
28f540f4 | 155 | } |
bc710b3b UD |
156 | #if 0 |
157 | /* This is not wanted. This would disable the user from having | |
158 | a timeout in the clnt_call() call. Only a call to cnlt_control() | |
159 | by the user should set the timeout value. */ | |
e7fd8a39 UD |
160 | tv.tv_sec = 25; |
161 | tv.tv_usec = 0; | |
162 | clnt_control (client, CLSET_TIMEOUT, (char *)&tv); | |
bc710b3b | 163 | #endif |
e7fd8a39 UD |
164 | break; |
165 | default: | |
543cf8a9 UD |
166 | { |
167 | struct rpc_createerr *ce = &get_rpc_createerr (); | |
168 | ce->cf_stat = RPC_SYSTEMERROR; | |
169 | ce->cf_error.re_errno = EPFNOSUPPORT; | |
170 | } | |
e7fd8a39 UD |
171 | return (NULL); |
172 | } | |
173 | return client; | |
28f540f4 | 174 | } |
7b57bfe5 UD |
175 | #ifdef EXPORT_RPC_SYMBOLS |
176 | libc_hidden_def (clnt_create) | |
177 | #else | |
021db4be | 178 | libc_hidden_nolink_sunrpc (clnt_create, GLIBC_2_0) |
7b57bfe5 | 179 | #endif |