Update.
[glibc.git] / sunrpc / clnt_gen.c
blob0f388572d433b7cdad4f88e9c3e5a66bafd66a0f
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 <sys/errno.h>
39 #include <netdb.h>
42 * Generic client creation: takes (hostname, program-number, protocol) and
43 * returns client handle. Default options are set, which the user can
44 * change using the rpc equivalent of ioctl()'s.
46 CLIENT *
47 clnt_create (const char *hostname, u_long prog, u_long vers,
48 const char *proto)
50 struct hostent hostbuf, *h;
51 size_t hstbuflen;
52 char *hsttmpbuf;
53 struct protoent protobuf, *p;
54 size_t prtbuflen;
55 char *prttmpbuf;
56 struct sockaddr_in sin;
57 struct sockaddr_un sun;
58 int sock;
59 struct timeval tv;
60 CLIENT *client;
61 int herr;
63 if (strcmp (proto, "unix") == 0)
65 __bzero ((char *)&sun, sizeof (sun));
66 sun.sun_family = AF_UNIX;
67 strcpy (sun.sun_path, hostname);
68 sock = RPC_ANYSOCK;
69 client = clntunix_create (&sun, prog, vers, &sock, 0, 0);
70 if (client == NULL)
71 return NULL;
72 tv.tv_sec = 25;
73 tv.tv_usec = 0;
74 clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
75 return client;
78 hstbuflen = 1024;
79 hsttmpbuf = __alloca (hstbuflen);
80 while (__gethostbyname_r (hostname, &hostbuf, hsttmpbuf, hstbuflen,
81 &h, &herr) != 0
82 || h == NULL)
83 if (herr != NETDB_INTERNAL || errno != ERANGE)
85 rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
86 return NULL;
88 else
90 /* Enlarge the buffer. */
91 hstbuflen *= 2;
92 hsttmpbuf = __alloca (hstbuflen);
95 if (h->h_addrtype != AF_INET)
98 * Only support INET for now
100 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
101 rpc_createerr.cf_error.re_errno = EAFNOSUPPORT;
102 return NULL;
104 sin.sin_family = h->h_addrtype;
105 sin.sin_port = 0;
106 __bzero (sin.sin_zero, sizeof (sin.sin_zero));
107 bcopy (h->h_addr, (char *) &sin.sin_addr, h->h_length);
109 prtbuflen = 1024;
110 prttmpbuf = __alloca (prtbuflen);
111 while (__getprotobyname_r (proto, &protobuf, prttmpbuf, prtbuflen, &p) != 0
112 || p == NULL)
113 if (errno != ERANGE)
115 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
116 rpc_createerr.cf_error.re_errno = EPFNOSUPPORT;
117 return NULL;
119 else
121 /* Enlarge the buffer. */
122 prtbuflen *= 2;
123 prttmpbuf = __alloca (prtbuflen);
126 sock = RPC_ANYSOCK;
127 switch (p->p_proto)
129 case IPPROTO_UDP:
130 tv.tv_sec = 5;
131 tv.tv_usec = 0;
132 client = clntudp_create (&sin, prog, vers, tv, &sock);
133 if (client == NULL)
135 return NULL;
137 tv.tv_sec = 25;
138 clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
139 break;
140 case IPPROTO_TCP:
141 client = clnttcp_create (&sin, prog, vers, &sock, 0, 0);
142 if (client == NULL)
144 return NULL;
146 tv.tv_sec = 25;
147 tv.tv_usec = 0;
148 clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
149 break;
150 default:
151 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
152 rpc_createerr.cf_error.re_errno = EPFNOSUPPORT;
153 return (NULL);
155 return client;