OSDN Git Service

Last portion of libc_hidden_proto removal.
[uclinux-h8/uClibc.git] / libc / inet / rpc / clnt_generic.c
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  */
29 /*
30  * Copyright (C) 1987, Sun Microsystems, Inc.
31  */
32
33 #define __FORCE_GLIBC
34 #include <features.h>
35
36 #include <alloca.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <rpc/rpc.h>
40 #include <sys/socket.h>
41 #include <netdb.h>
42
43 /* Experimentally off - libc_hidden_proto(memcpy) */
44 /* Experimentally off - libc_hidden_proto(memset) */
45 /* Experimentally off - libc_hidden_proto(strcmp) */
46 /* Experimentally off - libc_hidden_proto(strcpy) */
47 /* libc_hidden_proto(clnttcp_create) */
48 /* libc_hidden_proto(clntudp_create) */
49 /* libc_hidden_proto(clntunix_create) */
50 /* libc_hidden_proto(getprotobyname_r) */
51 /* libc_hidden_proto(gethostbyname_r) */
52 /* libc_hidden_proto(__rpc_thread_createerr) */
53
54 /*
55  * Generic client creation: takes (hostname, program-number, protocol) and
56  * returns client handle. Default options are set, which the user can
57  * change using the rpc equivalent of ioctl()'s.
58  */
59 CLIENT *
60 clnt_create (const char *hostname, u_long prog, u_long vers,
61              const char *proto)
62 {
63   struct hostent hostbuf, *h;
64   size_t hstbuflen;
65   char *hsttmpbuf;
66   struct protoent protobuf, *p;
67   size_t prtbuflen;
68   char *prttmpbuf;
69   struct sockaddr_in sin;
70   struct sockaddr_un sun;
71   int sock;
72   struct timeval tv;
73   CLIENT *client;
74   int herr;
75
76   if (strcmp (proto, "unix") == 0)
77     {
78       memset ((char *)&sun, 0, sizeof (sun));
79       sun.sun_family = AF_UNIX;
80       strcpy (sun.sun_path, hostname);
81       sock = RPC_ANYSOCK;
82       client = clntunix_create (&sun, prog, vers, &sock, 0, 0);
83       if (client == NULL)
84         return NULL;
85 #if 0
86       /* This is not wanted.  This would disable the user from having
87          a timeout in the clnt_call() call.  Only a call to cnlt_control()
88          by the user should set the timeout value.  */
89       tv.tv_sec = 25;
90       tv.tv_usec = 0;
91       clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
92 #endif
93       return client;
94     }
95
96   hstbuflen = 1024;
97   hsttmpbuf = alloca (hstbuflen);
98   while (gethostbyname_r (hostname, &hostbuf, hsttmpbuf, hstbuflen,
99                             &h, &herr) != 0
100          || h == NULL)
101     if (herr != NETDB_INTERNAL || errno != ERANGE)
102       {
103         get_rpc_createerr().cf_stat = RPC_UNKNOWNHOST;
104         return NULL;
105       }
106     else
107       {
108         /* Enlarge the buffer.  */
109         hstbuflen *= 2;
110         hsttmpbuf = alloca (hstbuflen);
111       }
112
113   if (h->h_addrtype != AF_INET)
114     {
115       /*
116        * Only support INET for now
117        */
118       struct rpc_createerr *ce = &get_rpc_createerr ();
119       ce->cf_stat = RPC_SYSTEMERROR;
120       ce->cf_error.re_errno = EAFNOSUPPORT;
121       return NULL;
122     }
123   sin.sin_family = h->h_addrtype;
124   sin.sin_port = 0;
125   memset (sin.sin_zero, 0, sizeof (sin.sin_zero));
126   memcpy ((char *) &sin.sin_addr, h->h_addr, h->h_length);
127
128   prtbuflen = 1024;
129   prttmpbuf = alloca (prtbuflen);
130   while (getprotobyname_r (proto, &protobuf, prttmpbuf, prtbuflen, &p) != 0
131          || p == NULL)
132         if (errno != ERANGE)
133       {
134         struct rpc_createerr *ce = &get_rpc_createerr ();
135         ce->cf_stat = RPC_UNKNOWNPROTO;
136         ce->cf_error.re_errno = EPFNOSUPPORT;
137         return NULL;
138       }
139     else
140       {
141         /* Enlarge the buffer.  */
142         prtbuflen *= 2;
143         prttmpbuf = alloca (prtbuflen);
144       }
145
146   sock = RPC_ANYSOCK;
147   switch (p->p_proto)
148     {
149     case IPPROTO_UDP:
150       tv.tv_sec = 5;
151       tv.tv_usec = 0;
152       client = clntudp_create (&sin, prog, vers, tv, &sock);
153       if (client == NULL)
154         {
155           return NULL;
156         }
157 #if 0
158       /* This is not wanted.  This would disable the user from having
159          a timeout in the clnt_call() call.  Only a call to cnlt_control()
160          by the user should set the timeout value.  */
161       tv.tv_sec = 25;
162       clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
163 #endif
164       break;
165     case IPPROTO_TCP:
166       client = clnttcp_create (&sin, prog, vers, &sock, 0, 0);
167       if (client == NULL)
168         {
169           return NULL;
170         }
171 #if 0
172       /* This is not wanted.  This would disable the user from having
173          a timeout in the clnt_call() call.  Only a call to cnlt_control()
174          by the user should set the timeout value.  */
175       tv.tv_sec = 25;
176       tv.tv_usec = 0;
177       clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
178 #endif
179       break;
180     default:
181       {
182         struct rpc_createerr *ce = &get_rpc_createerr ();
183         ce->cf_stat = RPC_SYSTEMERROR;
184         ce->cf_error.re_errno = EPFNOSUPPORT;
185       }
186       return (NULL);
187     }
188   return client;
189 }