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 #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>
39
40
41 /*
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.
45 */
46 CLIENT *
clnt_create(const char * hostname,u_long prog,u_long vers,const char * proto)47 clnt_create (const char *hostname, u_long prog, u_long vers,
48 const char *proto)
49 {
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;
62
63 if (strcmp (proto, "unix") == 0)
64 {
65 memset ((char *)&sun, 0, 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 #if 0
73 /* This is not wanted. This would disable the user from having
74 a timeout in the clnt_call() call. Only a call to cnlt_control()
75 by the user should set the timeout value. */
76 tv.tv_sec = 25;
77 tv.tv_usec = 0;
78 clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
79 #endif
80 return client;
81 }
82
83 hstbuflen = 1024;
84 hsttmpbuf = alloca (hstbuflen);
85 while (gethostbyname_r (hostname, &hostbuf, hsttmpbuf, hstbuflen,
86 &h, &herr) != 0
87 || h == NULL)
88 if (herr != NETDB_INTERNAL || errno != ERANGE)
89 {
90 get_rpc_createerr().cf_stat = RPC_UNKNOWNHOST;
91 return NULL;
92 }
93 else
94 {
95 /* Enlarge the buffer. */
96 hstbuflen *= 2;
97 hsttmpbuf = alloca (hstbuflen);
98 }
99
100 if (h->h_addrtype != AF_INET)
101 {
102 /*
103 * Only support INET for now
104 */
105 struct rpc_createerr *ce = &get_rpc_createerr ();
106 ce->cf_stat = RPC_SYSTEMERROR;
107 ce->cf_error.re_errno = EAFNOSUPPORT;
108 return NULL;
109 }
110 sin.sin_family = h->h_addrtype;
111 sin.sin_port = 0;
112 memset (sin.sin_zero, 0, sizeof (sin.sin_zero));
113 memcpy ((char *) &sin.sin_addr, h->h_addr, h->h_length);
114
115 prtbuflen = 1024;
116 prttmpbuf = alloca (prtbuflen);
117 while (getprotobyname_r (proto, &protobuf, prttmpbuf, prtbuflen, &p) != 0
118 || p == NULL)
119 if (errno != ERANGE)
120 {
121 struct rpc_createerr *ce = &get_rpc_createerr ();
122 ce->cf_stat = RPC_UNKNOWNPROTO;
123 ce->cf_error.re_errno = EPFNOSUPPORT;
124 return NULL;
125 }
126 else
127 {
128 /* Enlarge the buffer. */
129 prtbuflen *= 2;
130 prttmpbuf = alloca (prtbuflen);
131 }
132
133 sock = RPC_ANYSOCK;
134 switch (p->p_proto)
135 {
136 case IPPROTO_UDP:
137 tv.tv_sec = 5;
138 tv.tv_usec = 0;
139 client = clntudp_create (&sin, prog, vers, tv, &sock);
140 if (client == NULL)
141 {
142 return NULL;
143 }
144 #if 0
145 /* This is not wanted. This would disable the user from having
146 a timeout in the clnt_call() call. Only a call to cnlt_control()
147 by the user should set the timeout value. */
148 tv.tv_sec = 25;
149 clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
150 #endif
151 break;
152 case IPPROTO_TCP:
153 client = clnttcp_create (&sin, prog, vers, &sock, 0, 0);
154 if (client == NULL)
155 {
156 return NULL;
157 }
158 #if 0
159 /* This is not wanted. This would disable the user from having
160 a timeout in the clnt_call() call. Only a call to cnlt_control()
161 by the user should set the timeout value. */
162 tv.tv_sec = 25;
163 tv.tv_usec = 0;
164 clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
165 #endif
166 break;
167 default:
168 {
169 struct rpc_createerr *ce = &get_rpc_createerr ();
170 ce->cf_stat = RPC_SYSTEMERROR;
171 ce->cf_error.re_errno = EPFNOSUPPORT;
172 }
173 return (NULL);
174 }
175 return client;
176 }
177