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