1/*
2 * Copyright (c) 2010, Oracle America, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
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.
17 *
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.
30 */
31
32#include <alloca.h>
33#include <errno.h>
34#include <string.h>
35#include <rpc/rpc.h>
36#include <sys/socket.h>
37#include <netdb.h>
38
39/*
40 * Generic client creation: takes (hostname, program-number, protocol) and
41 * returns client handle. Default options are set, which the user can
42 * change using the rpc equivalent of ioctl()'s.
43 */
44CLIENT *
45clnt_create (const char *hostname, u_long prog, u_long vers,
46 const char *proto)
47{
48 struct protoent protobuf, *p;
49 size_t prtbuflen;
50 char *prttmpbuf;
51 struct sockaddr_in sin;
52 struct sockaddr_un sun;
53 int sock;
54 struct timeval tv;
55 CLIENT *client;
56
57 if (strcmp (proto, "unix") == 0)
58 {
59 __bzero ((char *)&sun, sizeof (sun));
60 sun.sun_family = AF_UNIX;
61 strcpy (sun.sun_path, hostname);
62 sock = RPC_ANYSOCK;
63 client = clntunix_create (&sun, prog, vers, &sock, 0, 0);
64 if (client == NULL)
65 return NULL;
66#if 0
67 /* This is not wanted. This would disable the user from having
68 a timeout in the clnt_call() call. Only a call to cnlt_control()
69 by the user should set the timeout value. */
70 tv.tv_sec = 25;
71 tv.tv_usec = 0;
72 clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
73#endif
74 return client;
75 }
76
77 if (__libc_rpc_gethostbyname (hostname, &sin) != 0)
78 return NULL;
79
80 prtbuflen = 1024;
81 prttmpbuf = __alloca (prtbuflen);
82 while (__getprotobyname_r (proto, &protobuf, prttmpbuf, prtbuflen, &p) != 0
83 || p == NULL)
84 if (errno != ERANGE)
85 {
86 struct rpc_createerr *ce = &get_rpc_createerr ();
87 ce->cf_stat = RPC_UNKNOWNPROTO;
88 ce->cf_error.re_errno = EPFNOSUPPORT;
89 return NULL;
90 }
91 else
92 {
93 /* Enlarge the buffer. */
94 prtbuflen *= 2;
95 prttmpbuf = __alloca (prtbuflen);
96 }
97
98 sock = RPC_ANYSOCK;
99 switch (p->p_proto)
100 {
101 case IPPROTO_UDP:
102 tv.tv_sec = 5;
103 tv.tv_usec = 0;
104 client = clntudp_create (&sin, prog, vers, tv, &sock);
105 if (client == NULL)
106 {
107 return NULL;
108 }
109#if 0
110 /* This is not wanted. This would disable the user from having
111 a timeout in the clnt_call() call. Only a call to cnlt_control()
112 by the user should set the timeout value. */
113 tv.tv_sec = 25;
114 clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
115#endif
116 break;
117 case IPPROTO_TCP:
118 client = clnttcp_create (&sin, prog, vers, &sock, 0, 0);
119 if (client == NULL)
120 {
121 return NULL;
122 }
123#if 0
124 /* This is not wanted. This would disable the user from having
125 a timeout in the clnt_call() call. Only a call to cnlt_control()
126 by the user should set the timeout value. */
127 tv.tv_sec = 25;
128 tv.tv_usec = 0;
129 clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
130#endif
131 break;
132 default:
133 {
134 struct rpc_createerr *ce = &get_rpc_createerr ();
135 ce->cf_stat = RPC_SYSTEMERROR;
136 ce->cf_error.re_errno = EPFNOSUPPORT;
137 }
138 return (NULL);
139 }
140 return client;
141}
142#ifdef EXPORT_RPC_SYMBOLS
143libc_hidden_def (clnt_create)
144#else
145libc_hidden_nolink_sunrpc (clnt_create, GLIBC_2_0)
146#endif
147