xref: /reactos/dll/3rdparty/libtirpc/src/rpc_soc.c (revision c2c66aff)
1*c2c66affSColin Finck 
2*c2c66affSColin Finck /*
3*c2c66affSColin Finck  * Copyright (c) 2009, Sun Microsystems, Inc.
4*c2c66affSColin Finck  * All rights reserved.
5*c2c66affSColin Finck  *
6*c2c66affSColin Finck  * Redistribution and use in source and binary forms, with or without
7*c2c66affSColin Finck  * modification, are permitted provided that the following conditions are met:
8*c2c66affSColin Finck  * - Redistributions of source code must retain the above copyright notice,
9*c2c66affSColin Finck  *   this list of conditions and the following disclaimer.
10*c2c66affSColin Finck  * - Redistributions in binary form must reproduce the above copyright notice,
11*c2c66affSColin Finck  *   this list of conditions and the following disclaimer in the documentation
12*c2c66affSColin Finck  *   and/or other materials provided with the distribution.
13*c2c66affSColin Finck  * - Neither the name of Sun Microsystems, Inc. nor the names of its
14*c2c66affSColin Finck  *   contributors may be used to endorse or promote products derived
15*c2c66affSColin Finck  *   from this software without specific prior written permission.
16*c2c66affSColin Finck  *
17*c2c66affSColin Finck  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*c2c66affSColin Finck  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*c2c66affSColin Finck  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*c2c66affSColin Finck  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*c2c66affSColin Finck  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*c2c66affSColin Finck  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*c2c66affSColin Finck  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*c2c66affSColin Finck  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*c2c66affSColin Finck  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*c2c66affSColin Finck  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*c2c66affSColin Finck  * POSSIBILITY OF SUCH DAMAGE.
28*c2c66affSColin Finck  */
29*c2c66affSColin Finck 
30*c2c66affSColin Finck /*
31*c2c66affSColin Finck  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
32*c2c66affSColin Finck  * In addition, portions of such source code were derived from Berkeley
33*c2c66affSColin Finck  * 4.3 BSD under license from the Regents of the University of
34*c2c66affSColin Finck  * California.
35*c2c66affSColin Finck  */
36*c2c66affSColin Finck 
37*c2c66affSColin Finck #if defined(PORTMAP) || defined (_WIN32)
38*c2c66affSColin Finck /*
39*c2c66affSColin Finck  * rpc_soc.c
40*c2c66affSColin Finck  *
41*c2c66affSColin Finck  * The backward compatibility routines for the earlier implementation
42*c2c66affSColin Finck  * of RPC, where the only transports supported were tcp/ip and udp/ip.
43*c2c66affSColin Finck  * Based on berkeley socket abstraction, now implemented on the top
44*c2c66affSColin Finck  * of TLI/Streams
45*c2c66affSColin Finck  */
46*c2c66affSColin Finck #include <wintirpc.h>
47*c2c66affSColin Finck //#include <pthread.h>
48*c2c66affSColin Finck #include <reentrant.h>
49*c2c66affSColin Finck #include <sys/types.h>
50*c2c66affSColin Finck //#include <sys/socket.h>
51*c2c66affSColin Finck #include <stdio.h>
52*c2c66affSColin Finck #include <rpc/rpc.h>
53*c2c66affSColin Finck #include <rpc/pmap_clnt.h>
54*c2c66affSColin Finck #include <rpc/pmap_prot.h>
55*c2c66affSColin Finck #include <rpc/nettype.h>
56*c2c66affSColin Finck //#include <syslog.h>
57*c2c66affSColin Finck //#include <netinet/in.h>
58*c2c66affSColin Finck //#include <netdb.h>
59*c2c66affSColin Finck #include <errno.h>
60*c2c66affSColin Finck //#include <syslog.h>
61*c2c66affSColin Finck #include <stdlib.h>
62*c2c66affSColin Finck #include <string.h>
63*c2c66affSColin Finck //#include <unistd.h>
64*c2c66affSColin Finck 
65*c2c66affSColin Finck #include "rpc_com.h"
66*c2c66affSColin Finck 
67*c2c66affSColin Finck extern mutex_t	rpcsoc_lock;
68*c2c66affSColin Finck 
69*c2c66affSColin Finck static CLIENT *clnt_com_create(struct sockaddr_in *, rpcprog_t, rpcvers_t,
70*c2c66affSColin Finck     int *, u_int, u_int, char *);
71*c2c66affSColin Finck static SVCXPRT *svc_com_create(SOCKET, u_int, u_int, char *);
72*c2c66affSColin Finck static bool_t rpc_wrap_bcast(char *, struct netbuf *, struct netconfig *);
73*c2c66affSColin Finck 
74*c2c66affSColin Finck /* XXX */
75*c2c66affSColin Finck #define IN4_LOCALHOST_STRING    "127.0.0.1"
76*c2c66affSColin Finck #define IN6_LOCALHOST_STRING    "::1"
77*c2c66affSColin Finck 
78*c2c66affSColin Finck /*
79*c2c66affSColin Finck  * A common clnt create routine
80*c2c66affSColin Finck  */
81*c2c66affSColin Finck static CLIENT *
clnt_com_create(raddr,prog,vers,sockp,sendsz,recvsz,tp)82*c2c66affSColin Finck clnt_com_create(raddr, prog, vers, sockp, sendsz, recvsz, tp)
83*c2c66affSColin Finck 	struct sockaddr_in *raddr;
84*c2c66affSColin Finck 	rpcprog_t prog;
85*c2c66affSColin Finck 	rpcvers_t vers;
86*c2c66affSColin Finck #ifndef __REACTOS__
87*c2c66affSColin Finck 	SOCKET *sockp;
88*c2c66affSColin Finck #else
89*c2c66affSColin Finck     int *sockp;
90*c2c66affSColin Finck #endif
91*c2c66affSColin Finck 	u_int sendsz;
92*c2c66affSColin Finck 	u_int recvsz;
93*c2c66affSColin Finck 	char *tp;
94*c2c66affSColin Finck {
95*c2c66affSColin Finck 	CLIENT *cl;
96*c2c66affSColin Finck 	int madefd = FALSE;
97*c2c66affSColin Finck 	SOCKET fd = *sockp;
98*c2c66affSColin Finck 	struct netconfig *nconf;
99*c2c66affSColin Finck 	struct netbuf bindaddr;
100*c2c66affSColin Finck 
101*c2c66affSColin Finck 	mutex_lock(&rpcsoc_lock);
102*c2c66affSColin Finck 	if ((nconf = __rpc_getconfip(tp)) == NULL) {
103*c2c66affSColin Finck 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
104*c2c66affSColin Finck 		mutex_unlock(&rpcsoc_lock);
105*c2c66affSColin Finck 		return (NULL);
106*c2c66affSColin Finck 	}
107*c2c66affSColin Finck 	if (fd == RPC_ANYSOCK) {
108*c2c66affSColin Finck 		fd = __rpc_nconf2fd(nconf);
109*c2c66affSColin Finck 		if (fd == -1)
110*c2c66affSColin Finck 			goto syserror;
111*c2c66affSColin Finck 		madefd = TRUE;
112*c2c66affSColin Finck 	}
113*c2c66affSColin Finck 
114*c2c66affSColin Finck 	if (raddr->sin_port == 0) {
115*c2c66affSColin Finck 		u_int proto;
116*c2c66affSColin Finck 		u_short sport;
117*c2c66affSColin Finck 
118*c2c66affSColin Finck 		mutex_unlock(&rpcsoc_lock);	/* pmap_getport is recursive */
119*c2c66affSColin Finck 		proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
120*c2c66affSColin Finck 		sport = pmap_getport(raddr, (u_long)prog, (u_long)vers,
121*c2c66affSColin Finck 		    proto);
122*c2c66affSColin Finck 		if (sport == 0) {
123*c2c66affSColin Finck 			goto err;
124*c2c66affSColin Finck 		}
125*c2c66affSColin Finck 		raddr->sin_port = htons(sport);
126*c2c66affSColin Finck 		mutex_lock(&rpcsoc_lock);	/* pmap_getport is recursive */
127*c2c66affSColin Finck 	}
128*c2c66affSColin Finck 
129*c2c66affSColin Finck 	/* Transform sockaddr_in to netbuf */
130*c2c66affSColin Finck 	bindaddr.maxlen = bindaddr.len =  sizeof (struct sockaddr_in);
131*c2c66affSColin Finck 	bindaddr.buf = raddr;
132*c2c66affSColin Finck 
133*c2c66affSColin Finck 	bindresvport(fd, NULL);
134*c2c66affSColin Finck 	cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
135*c2c66affSColin Finck 				sendsz, recvsz, NULL, NULL, NULL);
136*c2c66affSColin Finck 	if (cl) {
137*c2c66affSColin Finck 		if (madefd == TRUE) {
138*c2c66affSColin Finck 			/*
139*c2c66affSColin Finck 			 * The fd should be closed while destroying the handle.
140*c2c66affSColin Finck 			 */
141*c2c66affSColin Finck 			(void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
142*c2c66affSColin Finck 			*sockp = fd;
143*c2c66affSColin Finck 		}
144*c2c66affSColin Finck 		(void) freenetconfigent(nconf);
145*c2c66affSColin Finck 		mutex_unlock(&rpcsoc_lock);
146*c2c66affSColin Finck 		return (cl);
147*c2c66affSColin Finck 	}
148*c2c66affSColin Finck 	goto err;
149*c2c66affSColin Finck 
150*c2c66affSColin Finck syserror:
151*c2c66affSColin Finck 	rpc_createerr.cf_stat = RPC_SYSTEMERROR;
152*c2c66affSColin Finck 	rpc_createerr.cf_error.re_errno = errno;
153*c2c66affSColin Finck 
154*c2c66affSColin Finck err:	if (madefd == TRUE)
155*c2c66affSColin Finck 		(void)closesocket(fd);
156*c2c66affSColin Finck 	(void) freenetconfigent(nconf);
157*c2c66affSColin Finck 	mutex_unlock(&rpcsoc_lock);
158*c2c66affSColin Finck 	return (NULL);
159*c2c66affSColin Finck }
160*c2c66affSColin Finck 
161*c2c66affSColin Finck CLIENT *
clntudp_bufcreate(raddr,prog,vers,wait,sockp,sendsz,recvsz)162*c2c66affSColin Finck clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz)
163*c2c66affSColin Finck 	struct sockaddr_in *raddr;
164*c2c66affSColin Finck 	u_long prog;
165*c2c66affSColin Finck 	u_long vers;
166*c2c66affSColin Finck 	struct timeval wait;
167*c2c66affSColin Finck 	int *sockp;
168*c2c66affSColin Finck 	u_int sendsz;
169*c2c66affSColin Finck 	u_int recvsz;
170*c2c66affSColin Finck {
171*c2c66affSColin Finck 	CLIENT *cl;
172*c2c66affSColin Finck 
173*c2c66affSColin Finck 	cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
174*c2c66affSColin Finck 	    sendsz, recvsz, "udp");
175*c2c66affSColin Finck 	if (cl == NULL) {
176*c2c66affSColin Finck 		return (NULL);
177*c2c66affSColin Finck 	}
178*c2c66affSColin Finck 	(void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait);
179*c2c66affSColin Finck 	return (cl);
180*c2c66affSColin Finck }
181*c2c66affSColin Finck 
182*c2c66affSColin Finck CLIENT *
clntudp_create(raddr,program,version,wait,sockp)183*c2c66affSColin Finck clntudp_create(raddr, program, version, wait, sockp)
184*c2c66affSColin Finck 	struct sockaddr_in *raddr;
185*c2c66affSColin Finck 	u_long program;
186*c2c66affSColin Finck 	u_long version;
187*c2c66affSColin Finck 	struct timeval wait;
188*c2c66affSColin Finck 	int *sockp;
189*c2c66affSColin Finck {
190*c2c66affSColin Finck 	return clntudp_bufcreate(raddr, program, version, wait, sockp, UDPMSGSIZE, UDPMSGSIZE);
191*c2c66affSColin Finck }
192*c2c66affSColin Finck 
193*c2c66affSColin Finck CLIENT *
clnttcp_create(raddr,prog,vers,sockp,sendsz,recvsz)194*c2c66affSColin Finck clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
195*c2c66affSColin Finck 	struct sockaddr_in *raddr;
196*c2c66affSColin Finck 	u_long prog;
197*c2c66affSColin Finck 	u_long vers;
198*c2c66affSColin Finck 	int *sockp;
199*c2c66affSColin Finck 	u_int sendsz;
200*c2c66affSColin Finck 	u_int recvsz;
201*c2c66affSColin Finck {
202*c2c66affSColin Finck 	return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
203*c2c66affSColin Finck 	    sendsz, recvsz, "tcp");
204*c2c66affSColin Finck }
205*c2c66affSColin Finck 
206*c2c66affSColin Finck /* IPv6 version of clnt*_*create */
207*c2c66affSColin Finck 
208*c2c66affSColin Finck #ifdef INET6_NOT_USED
209*c2c66affSColin Finck 
210*c2c66affSColin Finck CLIENT *
clntudp6_bufcreate(raddr,prog,vers,wait,sockp,sendsz,recvsz)211*c2c66affSColin Finck clntudp6_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz)
212*c2c66affSColin Finck 	struct sockaddr_in6 *raddr;
213*c2c66affSColin Finck 	u_long prog;
214*c2c66affSColin Finck 	u_long vers;
215*c2c66affSColin Finck 	struct timeval wait;
216*c2c66affSColin Finck 	int *sockp;
217*c2c66affSColin Finck 	u_int sendsz;
218*c2c66affSColin Finck 	u_int recvsz;
219*c2c66affSColin Finck {
220*c2c66affSColin Finck 	CLIENT *cl;
221*c2c66affSColin Finck 
222*c2c66affSColin Finck 	cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
223*c2c66affSColin Finck 	    sendsz, recvsz, "udp6");
224*c2c66affSColin Finck 	if (cl == NULL) {
225*c2c66affSColin Finck 		return (NULL);
226*c2c66affSColin Finck 	}
227*c2c66affSColin Finck 	(void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait);
228*c2c66affSColin Finck 	return (cl);
229*c2c66affSColin Finck }
230*c2c66affSColin Finck 
231*c2c66affSColin Finck CLIENT *
clntudp6_create(raddr,program,version,wait,sockp)232*c2c66affSColin Finck clntudp6_create(raddr, program, version, wait, sockp)
233*c2c66affSColin Finck 	struct sockaddr_in6 *raddr;
234*c2c66affSColin Finck 	u_long program;
235*c2c66affSColin Finck 	u_long version;
236*c2c66affSColin Finck 	struct timeval wait;
237*c2c66affSColin Finck 	int *sockp;
238*c2c66affSColin Finck {
239*c2c66affSColin Finck 	return clntudp6_bufcreate(raddr, program, version, wait, sockp, UDPMSGSIZE, UDPMSGSIZE);
240*c2c66affSColin Finck }
241*c2c66affSColin Finck 
242*c2c66affSColin Finck CLIENT *
clnttcp6_create(raddr,prog,vers,sockp,sendsz,recvsz)243*c2c66affSColin Finck clnttcp6_create(raddr, prog, vers, sockp, sendsz, recvsz)
244*c2c66affSColin Finck 	struct sockaddr_in6 *raddr;
245*c2c66affSColin Finck 	u_long prog;
246*c2c66affSColin Finck 	u_long vers;
247*c2c66affSColin Finck 	int *sockp;
248*c2c66affSColin Finck 	u_int sendsz;
249*c2c66affSColin Finck 	u_int recvsz;
250*c2c66affSColin Finck {
251*c2c66affSColin Finck 	return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
252*c2c66affSColin Finck 	    sendsz, recvsz, "tcp6");
253*c2c66affSColin Finck }
254*c2c66affSColin Finck 
255*c2c66affSColin Finck #endif
256*c2c66affSColin Finck 
257*c2c66affSColin Finck CLIENT *
clntraw_create(prog,vers)258*c2c66affSColin Finck clntraw_create(prog, vers)
259*c2c66affSColin Finck 	u_long prog;
260*c2c66affSColin Finck 	u_long vers;
261*c2c66affSColin Finck {
262*c2c66affSColin Finck 	return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers);
263*c2c66affSColin Finck }
264*c2c66affSColin Finck 
265*c2c66affSColin Finck /*
266*c2c66affSColin Finck  * A common server create routine
267*c2c66affSColin Finck  */
268*c2c66affSColin Finck static SVCXPRT *
svc_com_create(fd,sendsize,recvsize,netid)269*c2c66affSColin Finck svc_com_create(fd, sendsize, recvsize, netid)
270*c2c66affSColin Finck 	SOCKET fd;
271*c2c66affSColin Finck 	u_int sendsize;
272*c2c66affSColin Finck 	u_int recvsize;
273*c2c66affSColin Finck 	char *netid;
274*c2c66affSColin Finck {
275*c2c66affSColin Finck 	struct netconfig *nconf;
276*c2c66affSColin Finck 	SVCXPRT *svc;
277*c2c66affSColin Finck 	int madefd = FALSE;
278*c2c66affSColin Finck 	int port;
279*c2c66affSColin Finck 	struct sockaddr_in sin;
280*c2c66affSColin Finck 
281*c2c66affSColin Finck 	if ((nconf = __rpc_getconfip(netid)) == NULL) {
282*c2c66affSColin Finck 		//(void) syslog(LOG_ERR, "Could not get %s transport", netid);
283*c2c66affSColin Finck 		return (NULL);
284*c2c66affSColin Finck 	}
285*c2c66affSColin Finck 	if (fd == RPC_ANYSOCK) {
286*c2c66affSColin Finck 		fd = __rpc_nconf2fd(nconf);
287*c2c66affSColin Finck 		if (fd == -1) {
288*c2c66affSColin Finck 			(void) freenetconfigent(nconf);
289*c2c66affSColin Finck 			//(void) syslog(LOG_ERR,
290*c2c66affSColin Finck 			//"svc%s_create: could not open connection", netid);
291*c2c66affSColin Finck 			return (NULL);
292*c2c66affSColin Finck 		}
293*c2c66affSColin Finck 		madefd = TRUE;
294*c2c66affSColin Finck 	}
295*c2c66affSColin Finck 
296*c2c66affSColin Finck 	memset(&sin, 0, sizeof sin);
297*c2c66affSColin Finck 	sin.sin_family = AF_INET;
298*c2c66affSColin Finck 	bindresvport(fd, &sin);
299*c2c66affSColin Finck 	listen(fd, SOMAXCONN);
300*c2c66affSColin Finck 	svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize);
301*c2c66affSColin Finck 	(void) freenetconfigent(nconf);
302*c2c66affSColin Finck 	if (svc == NULL) {
303*c2c66affSColin Finck 		if (madefd)
304*c2c66affSColin Finck 			(void)closesocket(fd);
305*c2c66affSColin Finck 		return (NULL);
306*c2c66affSColin Finck 	}
307*c2c66affSColin Finck 	port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
308*c2c66affSColin Finck 	svc->xp_port = ntohs((u_short)port);
309*c2c66affSColin Finck 	return (svc);
310*c2c66affSColin Finck }
311*c2c66affSColin Finck 
312*c2c66affSColin Finck SVCXPRT *
svctcp_create(fd,sendsize,recvsize)313*c2c66affSColin Finck svctcp_create(fd, sendsize, recvsize)
314*c2c66affSColin Finck #ifndef __REACTOS__
315*c2c66affSColin Finck 	SOCKET fd;
316*c2c66affSColin Finck #else
317*c2c66affSColin Finck     int fd;
318*c2c66affSColin Finck #endif
319*c2c66affSColin Finck 	u_int sendsize;
320*c2c66affSColin Finck 	u_int recvsize;
321*c2c66affSColin Finck {
322*c2c66affSColin Finck 
323*c2c66affSColin Finck 	return svc_com_create(fd, sendsize, recvsize, "tcp");
324*c2c66affSColin Finck }
325*c2c66affSColin Finck 
326*c2c66affSColin Finck 
327*c2c66affSColin Finck 
328*c2c66affSColin Finck SVCXPRT *
svcudp_bufcreate(fd,sendsz,recvsz)329*c2c66affSColin Finck svcudp_bufcreate(fd, sendsz, recvsz)
330*c2c66affSColin Finck #ifndef __REACTOS__
331*c2c66affSColin Finck 	SOCKET fd;
332*c2c66affSColin Finck #else
333*c2c66affSColin Finck     int fd;
334*c2c66affSColin Finck #endif
335*c2c66affSColin Finck 	u_int sendsz, recvsz;
336*c2c66affSColin Finck {
337*c2c66affSColin Finck 
338*c2c66affSColin Finck 	return svc_com_create(fd, sendsz, recvsz, "udp");
339*c2c66affSColin Finck }
340*c2c66affSColin Finck 
341*c2c66affSColin Finck 
342*c2c66affSColin Finck 
343*c2c66affSColin Finck SVCXPRT *
svcfd_create(fd,sendsize,recvsize)344*c2c66affSColin Finck svcfd_create(fd, sendsize, recvsize)
345*c2c66affSColin Finck #ifndef __REACTOS__
346*c2c66affSColin Finck 	SOCKET fd;
347*c2c66affSColin Finck #else
348*c2c66affSColin Finck     int fd;
349*c2c66affSColin Finck #endif
350*c2c66affSColin Finck 	u_int sendsize;
351*c2c66affSColin Finck 	u_int recvsize;
352*c2c66affSColin Finck {
353*c2c66affSColin Finck 
354*c2c66affSColin Finck 	return svc_fd_create(fd, sendsize, recvsize);
355*c2c66affSColin Finck }
356*c2c66affSColin Finck 
357*c2c66affSColin Finck 
358*c2c66affSColin Finck SVCXPRT *
svcudp_create(fd)359*c2c66affSColin Finck svcudp_create(fd)
360*c2c66affSColin Finck #ifndef __REACTOS__
361*c2c66affSColin Finck 	SOCKET fd;
362*c2c66affSColin Finck #else
363*c2c66affSColin Finck     int fd;
364*c2c66affSColin Finck #endif
365*c2c66affSColin Finck {
366*c2c66affSColin Finck 
367*c2c66affSColin Finck 	return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
368*c2c66affSColin Finck }
369*c2c66affSColin Finck 
370*c2c66affSColin Finck 
371*c2c66affSColin Finck SVCXPRT *
svcraw_create()372*c2c66affSColin Finck svcraw_create()
373*c2c66affSColin Finck {
374*c2c66affSColin Finck 
375*c2c66affSColin Finck 	return svc_raw_create();
376*c2c66affSColin Finck }
377*c2c66affSColin Finck 
378*c2c66affSColin Finck 
379*c2c66affSColin Finck /* IPV6 version */
380*c2c66affSColin Finck #ifdef INET6_NOT_USED
381*c2c66affSColin Finck SVCXPRT *
svcudp6_bufcreate(fd,sendsz,recvsz)382*c2c66affSColin Finck svcudp6_bufcreate(fd, sendsz, recvsz)
383*c2c66affSColin Finck 	int fd;
384*c2c66affSColin Finck 	u_int sendsz, recvsz;
385*c2c66affSColin Finck {
386*c2c66affSColin Finck 	return svc_com_create(fd, sendsz, recvsz, "udp6");
387*c2c66affSColin Finck }
388*c2c66affSColin Finck 
389*c2c66affSColin Finck 
390*c2c66affSColin Finck SVCXPRT *
svctcp6_create(fd,sendsize,recvsize)391*c2c66affSColin Finck svctcp6_create(fd, sendsize, recvsize)
392*c2c66affSColin Finck 	int fd;
393*c2c66affSColin Finck 	u_int sendsize;
394*c2c66affSColin Finck 	u_int recvsize;
395*c2c66affSColin Finck {
396*c2c66affSColin Finck 	return svc_com_create(fd, sendsize, recvsize, "tcp6");
397*c2c66affSColin Finck }
398*c2c66affSColin Finck 
399*c2c66affSColin Finck 
400*c2c66affSColin Finck SVCXPRT *
svcudp6_create(fd)401*c2c66affSColin Finck svcudp6_create(fd)
402*c2c66affSColin Finck 	int fd;
403*c2c66affSColin Finck {
404*c2c66affSColin Finck 	return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp6");
405*c2c66affSColin Finck }
406*c2c66affSColin Finck #endif
407*c2c66affSColin Finck 
408*c2c66affSColin Finck int
get_myaddress(addr)409*c2c66affSColin Finck get_myaddress(addr)
410*c2c66affSColin Finck 	struct sockaddr_in *addr;
411*c2c66affSColin Finck {
412*c2c66affSColin Finck 
413*c2c66affSColin Finck 	memset((void *) addr, 0, sizeof(*addr));
414*c2c66affSColin Finck 	addr->sin_family = AF_INET;
415*c2c66affSColin Finck 	addr->sin_port = htons(PMAPPORT);
416*c2c66affSColin Finck 	addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
417*c2c66affSColin Finck 	return (0);
418*c2c66affSColin Finck }
419*c2c66affSColin Finck 
420*c2c66affSColin Finck /*
421*c2c66affSColin Finck  * For connectionless "udp" transport. Obsoleted by rpc_call().
422*c2c66affSColin Finck  */
423*c2c66affSColin Finck int
callrpc(host,prognum,versnum,procnum,inproc,in,outproc,out)424*c2c66affSColin Finck callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
425*c2c66affSColin Finck 	const char *host;
426*c2c66affSColin Finck 	int prognum, versnum, procnum;
427*c2c66affSColin Finck 	xdrproc_t inproc, outproc;
428*c2c66affSColin Finck 	void *in, *out;
429*c2c66affSColin Finck {
430*c2c66affSColin Finck 
431*c2c66affSColin Finck 	return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum,
432*c2c66affSColin Finck 	    (rpcproc_t)procnum, inproc, in, outproc, out, "udp");
433*c2c66affSColin Finck }
434*c2c66affSColin Finck 
435*c2c66affSColin Finck /*
436*c2c66affSColin Finck  * For connectionless kind of transport. Obsoleted by rpc_reg()
437*c2c66affSColin Finck  */
438*c2c66affSColin Finck int
registerrpc(prognum,versnum,procnum,progname,inproc,outproc)439*c2c66affSColin Finck registerrpc(prognum, versnum, procnum, progname, inproc, outproc)
440*c2c66affSColin Finck 	int prognum, versnum, procnum;
441*c2c66affSColin Finck 	char *(*progname)(char [UDPMSGSIZE]);
442*c2c66affSColin Finck 	xdrproc_t inproc, outproc;
443*c2c66affSColin Finck {
444*c2c66affSColin Finck 
445*c2c66affSColin Finck 	return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum,
446*c2c66affSColin Finck 	    (rpcproc_t)procnum, progname, inproc, outproc, "udp");
447*c2c66affSColin Finck }
448*c2c66affSColin Finck 
449*c2c66affSColin Finck /*
450*c2c66affSColin Finck  * All the following clnt_broadcast stuff is convulated; it supports
451*c2c66affSColin Finck  * the earlier calling style of the callback function
452*c2c66affSColin Finck  */
453*c2c66affSColin Finck extern thread_key_t	clnt_broadcast_key;
454*c2c66affSColin Finck 
455*c2c66affSColin Finck /*
456*c2c66affSColin Finck  * Need to translate the netbuf address into sockaddr_in address.
457*c2c66affSColin Finck  * Dont care about netid here.
458*c2c66affSColin Finck  */
459*c2c66affSColin Finck /* ARGSUSED */
460*c2c66affSColin Finck static bool_t
rpc_wrap_bcast(resultp,addr,nconf)461*c2c66affSColin Finck rpc_wrap_bcast(resultp, addr, nconf)
462*c2c66affSColin Finck 	char *resultp;		/* results of the call */
463*c2c66affSColin Finck 	struct netbuf *addr;	/* address of the guy who responded */
464*c2c66affSColin Finck 	struct netconfig *nconf; /* Netconf of the transport */
465*c2c66affSColin Finck {
466*c2c66affSColin Finck 	resultproc_t clnt_broadcast_result;
467*c2c66affSColin Finck 
468*c2c66affSColin Finck 	if (strcmp(nconf->nc_netid, "udp"))
469*c2c66affSColin Finck 		return (FALSE);
470*c2c66affSColin Finck 	clnt_broadcast_result = (resultproc_t)thr_getspecific(clnt_broadcast_key);
471*c2c66affSColin Finck 	return (*clnt_broadcast_result)(resultp,
472*c2c66affSColin Finck 				(struct sockaddr_in *)addr->buf);
473*c2c66affSColin Finck }
474*c2c66affSColin Finck 
475*c2c66affSColin Finck /*
476*c2c66affSColin Finck  * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
477*c2c66affSColin Finck  */
478*c2c66affSColin Finck enum clnt_stat
clnt_broadcast(prog,vers,proc,xargs,argsp,xresults,resultsp,eachresult)479*c2c66affSColin Finck clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
480*c2c66affSColin Finck 	u_long		prog;		/* program number */
481*c2c66affSColin Finck 	u_long		vers;		/* version number */
482*c2c66affSColin Finck 	u_long		proc;		/* procedure number */
483*c2c66affSColin Finck 	xdrproc_t	xargs;		/* xdr routine for args */
484*c2c66affSColin Finck 	void	       *argsp;		/* pointer to args */
485*c2c66affSColin Finck 	xdrproc_t	xresults;	/* xdr routine for results */
486*c2c66affSColin Finck 	void	       *resultsp;	/* pointer to results */
487*c2c66affSColin Finck 	resultproc_t	eachresult;	/* call with each result obtained */
488*c2c66affSColin Finck {
489*c2c66affSColin Finck 	extern mutex_t tsd_lock;
490*c2c66affSColin Finck 
491*c2c66affSColin Finck 	if (clnt_broadcast_key == -1) {
492*c2c66affSColin Finck 		mutex_lock(&tsd_lock);
493*c2c66affSColin Finck 		if (clnt_broadcast_key == -1)
494*c2c66affSColin Finck 			thr_keycreate(&clnt_broadcast_key, free);
495*c2c66affSColin Finck 		mutex_unlock(&tsd_lock);
496*c2c66affSColin Finck 	}
497*c2c66affSColin Finck 	thr_setspecific(clnt_broadcast_key, (void *) eachresult);
498*c2c66affSColin Finck 	return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers,
499*c2c66affSColin Finck 	    (rpcproc_t)proc, xargs, argsp, xresults, resultsp,
500*c2c66affSColin Finck 	    (resultproc_t) rpc_wrap_bcast, "udp");
501*c2c66affSColin Finck }
502*c2c66affSColin Finck 
503*c2c66affSColin Finck #ifndef _WIN32
504*c2c66affSColin Finck /*
505*c2c66affSColin Finck  * Create the client des authentication object. Obsoleted by
506*c2c66affSColin Finck  * authdes_seccreate().
507*c2c66affSColin Finck  */
508*c2c66affSColin Finck AUTH *
authdes_create(servername,window,syncaddr,ckey)509*c2c66affSColin Finck authdes_create(servername, window, syncaddr, ckey)
510*c2c66affSColin Finck 	char *servername;		/* network name of server */
511*c2c66affSColin Finck 	u_int window;			/* time to live */
512*c2c66affSColin Finck 	struct sockaddr *syncaddr;	/* optional hostaddr to sync with */
513*c2c66affSColin Finck 	des_block *ckey;		/* optional conversation key to use */
514*c2c66affSColin Finck {
515*c2c66affSColin Finck 	AUTH *dummy;
516*c2c66affSColin Finck 	AUTH *nauth;
517*c2c66affSColin Finck 	char hostname[NI_MAXHOST];
518*c2c66affSColin Finck 
519*c2c66affSColin Finck 	if (syncaddr) {
520*c2c66affSColin Finck 		/*
521*c2c66affSColin Finck 		 * Change addr to hostname, because that is the way
522*c2c66affSColin Finck 		 * new interface takes it.
523*c2c66affSColin Finck 		 */
524*c2c66affSColin Finck 		if (getnameinfo(syncaddr, sizeof(syncaddr), hostname,
525*c2c66affSColin Finck 		    sizeof hostname, NULL, 0, 0) != 0)
526*c2c66affSColin Finck 			goto fallback;
527*c2c66affSColin Finck 
528*c2c66affSColin Finck 		nauth = authdes_seccreate(servername, window, hostname, ckey);
529*c2c66affSColin Finck 		return (nauth);
530*c2c66affSColin Finck 	}
531*c2c66affSColin Finck fallback:
532*c2c66affSColin Finck 	dummy = authdes_seccreate(servername, window, NULL, ckey);
533*c2c66affSColin Finck 	return (dummy);
534*c2c66affSColin Finck }
535*c2c66affSColin Finck #endif
536*c2c66affSColin Finck 
537*c2c66affSColin Finck /*
538*c2c66affSColin Finck  * Create a client handle for a unix connection. Obsoleted by clnt_vc_create()
539*c2c66affSColin Finck  */
540*c2c66affSColin Finck CLIENT *
clntunix_create(raddr,prog,vers,sockp,sendsz,recvsz)541*c2c66affSColin Finck clntunix_create(raddr, prog, vers, sockp, sendsz, recvsz)
542*c2c66affSColin Finck 	struct sockaddr_un *raddr;
543*c2c66affSColin Finck 	u_long prog;
544*c2c66affSColin Finck 	u_long vers;
545*c2c66affSColin Finck #ifndef __REACTOS__
546*c2c66affSColin Finck 	SOCKET *sockp;
547*c2c66affSColin Finck #else
548*c2c66affSColin Finck     int *sockp;
549*c2c66affSColin Finck #endif
550*c2c66affSColin Finck 	u_int sendsz;
551*c2c66affSColin Finck 	u_int recvsz;
552*c2c66affSColin Finck {
553*c2c66affSColin Finck 	struct netbuf *svcaddr;
554*c2c66affSColin Finck 	struct netconfig *nconf;
555*c2c66affSColin Finck 	CLIENT *cl;
556*c2c66affSColin Finck 	int len;
557*c2c66affSColin Finck 
558*c2c66affSColin Finck 	cl = NULL;
559*c2c66affSColin Finck 	nconf = NULL;
560*c2c66affSColin Finck 	svcaddr = NULL;
561*c2c66affSColin Finck 	if (((svcaddr = malloc(sizeof(struct netbuf))) == NULL ) ||
562*c2c66affSColin Finck 	    ((svcaddr->buf = malloc(sizeof(struct sockaddr_un))) == NULL)) {
563*c2c66affSColin Finck 		if (svcaddr != NULL)
564*c2c66affSColin Finck 			free(svcaddr);
565*c2c66affSColin Finck 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
566*c2c66affSColin Finck 		rpc_createerr.cf_error.re_errno = errno;
567*c2c66affSColin Finck 		return(cl);
568*c2c66affSColin Finck 	}
569*c2c66affSColin Finck 	if (*sockp == SOCKET_ERROR) {
570*c2c66affSColin Finck 		*sockp = socket(AF_UNIX, SOCK_STREAM, 0);
571*c2c66affSColin Finck 		len = SUN_LEN(raddr);
572*c2c66affSColin Finck 		if ((*sockp == INVALID_SOCKET) || (connect(*sockp,
573*c2c66affSColin Finck 		    (struct sockaddr *)raddr, len) == SOCKET_ERROR)) {
574*c2c66affSColin Finck 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
575*c2c66affSColin Finck 			rpc_createerr.cf_error.re_errno = errno;
576*c2c66affSColin Finck 			if (*sockp != INVALID_SOCKET)
577*c2c66affSColin Finck 				(void)closesocket(*sockp);
578*c2c66affSColin Finck 			goto done;
579*c2c66affSColin Finck 		}
580*c2c66affSColin Finck 	}
581*c2c66affSColin Finck 	svcaddr->buf = raddr;
582*c2c66affSColin Finck 	svcaddr->len = sizeof(raddr);
583*c2c66affSColin Finck 	svcaddr->maxlen = sizeof (struct sockaddr_un);
584*c2c66affSColin Finck 	cl = clnt_vc_create(*sockp, svcaddr, prog,
585*c2c66affSColin Finck 	    vers, sendsz, recvsz, NULL, NULL, NULL);
586*c2c66affSColin Finck done:
587*c2c66affSColin Finck 	free(svcaddr->buf);
588*c2c66affSColin Finck 	free(svcaddr);
589*c2c66affSColin Finck 	return(cl);
590*c2c66affSColin Finck }
591*c2c66affSColin Finck 
592*c2c66affSColin Finck /*
593*c2c66affSColin Finck  * Creates, registers, and returns a (rpc) unix based transporter.
594*c2c66affSColin Finck  * Obsoleted by svc_vc_create().
595*c2c66affSColin Finck  */
596*c2c66affSColin Finck SVCXPRT *
svcunix_create(sock,sendsize,recvsize,path)597*c2c66affSColin Finck svcunix_create(sock, sendsize, recvsize, path)
598*c2c66affSColin Finck #ifndef __REACTOS__
599*c2c66affSColin Finck 	SOCKET sock;
600*c2c66affSColin Finck #else
601*c2c66affSColin Finck     int sock;
602*c2c66affSColin Finck #endif
603*c2c66affSColin Finck 	u_int sendsize;
604*c2c66affSColin Finck 	u_int recvsize;
605*c2c66affSColin Finck 	char *path;
606*c2c66affSColin Finck {
607*c2c66affSColin Finck 	struct netconfig *nconf;
608*c2c66affSColin Finck 	void *localhandle;
609*c2c66affSColin Finck 	struct sockaddr_un sun;
610*c2c66affSColin Finck 	struct sockaddr *sa;
611*c2c66affSColin Finck 	struct t_bind taddr;
612*c2c66affSColin Finck 	SVCXPRT *xprt;
613*c2c66affSColin Finck 	int addrlen;
614*c2c66affSColin Finck 
615*c2c66affSColin Finck 	xprt = (SVCXPRT *)NULL;
616*c2c66affSColin Finck 	localhandle = setnetconfig();
617*c2c66affSColin Finck 	while ((nconf = getnetconfig(localhandle)) != NULL) {
618*c2c66affSColin Finck 		if (nconf->nc_protofmly != NULL &&
619*c2c66affSColin Finck 		    strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
620*c2c66affSColin Finck 			break;
621*c2c66affSColin Finck 	}
622*c2c66affSColin Finck 	if (nconf == NULL)
623*c2c66affSColin Finck 		return(xprt);
624*c2c66affSColin Finck 
625*c2c66affSColin Finck 	if ((sock = __rpc_nconf2fd(nconf)) == SOCKET_ERROR)
626*c2c66affSColin Finck 		goto done;
627*c2c66affSColin Finck 
628*c2c66affSColin Finck 	memset(&sun, 0, sizeof sun);
629*c2c66affSColin Finck 	sun.sun_family = AF_UNIX;
630*c2c66affSColin Finck 	strncpy(sun.sun_path, path, sizeof(sun.sun_path));
631*c2c66affSColin Finck 	addrlen = sizeof(struct sockaddr_un);
632*c2c66affSColin Finck 	sa = (struct sockaddr *)&sun;
633*c2c66affSColin Finck 
634*c2c66affSColin Finck 	if (bind(sock, sa, addrlen) == SOCKET_ERROR)
635*c2c66affSColin Finck 		goto done;
636*c2c66affSColin Finck 
637*c2c66affSColin Finck 	taddr.addr.len = taddr.addr.maxlen = addrlen;
638*c2c66affSColin Finck 	taddr.addr.buf = malloc(addrlen);
639*c2c66affSColin Finck 	if (taddr.addr.buf == NULL)
640*c2c66affSColin Finck 		goto done;
641*c2c66affSColin Finck 	memcpy(taddr.addr.buf, sa, addrlen);
642*c2c66affSColin Finck 
643*c2c66affSColin Finck 	if (nconf->nc_semantics != NC_TPI_CLTS) {
644*c2c66affSColin Finck 		if (listen(sock, SOMAXCONN) == SOCKET_ERROR) {
645*c2c66affSColin Finck 			free(taddr.addr.buf);
646*c2c66affSColin Finck 			goto done;
647*c2c66affSColin Finck 		}
648*c2c66affSColin Finck 	}
649*c2c66affSColin Finck 
650*c2c66affSColin Finck 	xprt = (SVCXPRT *)svc_tli_create(sock, nconf, &taddr, sendsize, recvsize);
651*c2c66affSColin Finck 
652*c2c66affSColin Finck done:
653*c2c66affSColin Finck 	endnetconfig(localhandle);
654*c2c66affSColin Finck 	return(xprt);
655*c2c66affSColin Finck }
656*c2c66affSColin Finck 
657*c2c66affSColin Finck /*
658*c2c66affSColin Finck  * Like svunix_create(), except the routine takes any *open* UNIX file
659*c2c66affSColin Finck  * descriptor as its first input. Obsoleted by svc_fd_create();
660*c2c66affSColin Finck  */
661*c2c66affSColin Finck SVCXPRT *
svcunixfd_create(fd,sendsize,recvsize)662*c2c66affSColin Finck svcunixfd_create(fd, sendsize, recvsize)
663*c2c66affSColin Finck #ifndef __REACTOS__
664*c2c66affSColin Finck 	SOCKET fd;
665*c2c66affSColin Finck #else
666*c2c66affSColin Finck     int fd;
667*c2c66affSColin Finck #endif
668*c2c66affSColin Finck 	u_int sendsize;
669*c2c66affSColin Finck 	u_int recvsize;
670*c2c66affSColin Finck {
671*c2c66affSColin Finck  	return (svc_fd_create(fd, sendsize, recvsize));
672*c2c66affSColin Finck }
673*c2c66affSColin Finck 
674*c2c66affSColin Finck #endif /* PORTMAP */
675