_POSIX_C_SOURCE >= 200112L implies C99.
[glibc/history.git] / sunrpc / clnt_gen.c
blobc7468f4d9386ccfedfb6d56e7abae2487fc34499
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.
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.
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.
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.
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.
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
30 * Copyright (C) 1987, Sun Microsystems, Inc.
33 #include <alloca.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <rpc/rpc.h>
37 #include <sys/socket.h>
38 #include <netdb.h>
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.
45 CLIENT *
46 clnt_create (const char *hostname, u_long prog, u_long vers,
47 const char *proto)
49 struct hostent hostbuf, *h;
50 size_t hstbuflen;
51 char *hsttmpbuf;
52 struct protoent protobuf, *p;
53 size_t prtbuflen;
54 char *prttmpbuf;
55 struct sockaddr_in sin;
56 struct sockaddr_un sun;
57 int sock;
58 struct timeval tv;
59 CLIENT *client;
60 int herr;
62 if (strcmp (proto, "unix") == 0)
64 __bzero ((char *)&sun, sizeof (sun));
65 sun.sun_family = AF_UNIX;
66 strcpy (sun.sun_path, hostname);
67 sock = RPC_ANYSOCK;
68 client = INTUSE(clntunix_create) (&sun, prog, vers, &sock, 0, 0);
69 if (client == NULL)
70 return NULL;
71 #if 0
72 /* This is not wanted. This would disable the user from having
73 a timeout in the clnt_call() call. Only a call to cnlt_control()
74 by the user should set the timeout value. */
75 tv.tv_sec = 25;
76 tv.tv_usec = 0;
77 clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
78 #endif
79 return client;
82 hstbuflen = 1024;
83 hsttmpbuf = __alloca (hstbuflen);
84 while (__gethostbyname_r (hostname, &hostbuf, hsttmpbuf, hstbuflen,
85 &h, &herr) != 0
86 || h == NULL)
87 if (herr != NETDB_INTERNAL || errno != ERANGE)
89 get_rpc_createerr().cf_stat = RPC_UNKNOWNHOST;
90 return NULL;
92 else
94 /* Enlarge the buffer. */
95 hstbuflen *= 2;
96 hsttmpbuf = __alloca (hstbuflen);
99 if (h->h_addrtype != AF_INET)
102 * Only support INET for now
104 struct rpc_createerr *ce = &get_rpc_createerr ();
105 ce->cf_stat = RPC_SYSTEMERROR;
106 ce->cf_error.re_errno = EAFNOSUPPORT;
107 return NULL;
109 sin.sin_family = h->h_addrtype;
110 sin.sin_port = 0;
111 __bzero (sin.sin_zero, sizeof (sin.sin_zero));
112 memcpy ((char *) &sin.sin_addr, h->h_addr, h->h_length);
114 prtbuflen = 1024;
115 prttmpbuf = __alloca (prtbuflen);
116 while (__getprotobyname_r (proto, &protobuf, prttmpbuf, prtbuflen, &p) != 0
117 || p == NULL)
118 if (errno != ERANGE)
120 struct rpc_createerr *ce = &get_rpc_createerr ();
121 ce->cf_stat = RPC_UNKNOWNPROTO;
122 ce->cf_error.re_errno = EPFNOSUPPORT;
123 return NULL;
125 else
127 /* Enlarge the buffer. */
128 prtbuflen *= 2;
129 prttmpbuf = __alloca (prtbuflen);
132 sock = RPC_ANYSOCK;
133 switch (p->p_proto)
135 case IPPROTO_UDP:
136 tv.tv_sec = 5;
137 tv.tv_usec = 0;
138 client = INTUSE(clntudp_create) (&sin, prog, vers, tv, &sock);
139 if (client == NULL)
141 return NULL;
143 #if 0
144 /* This is not wanted. This would disable the user from having
145 a timeout in the clnt_call() call. Only a call to cnlt_control()
146 by the user should set the timeout value. */
147 tv.tv_sec = 25;
148 clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
149 #endif
150 break;
151 case IPPROTO_TCP:
152 client = INTUSE(clnttcp_create) (&sin, prog, vers, &sock, 0, 0);
153 if (client == NULL)
155 return NULL;
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 tv.tv_usec = 0;
163 clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
164 #endif
165 break;
166 default:
168 struct rpc_createerr *ce = &get_rpc_createerr ();
169 ce->cf_stat = RPC_SYSTEMERROR;
170 ce->cf_error.re_errno = EPFNOSUPPORT;
172 return (NULL);
174 return client;
176 INTDEF (clnt_create)