1*ba96d07fShrs /*-
2*ba96d07fShrs * Copyright (c) 2009, Sun Microsystems, Inc.
3*ba96d07fShrs * All rights reserved.
4ce0e08e2SPeter Avalos *
5*ba96d07fShrs * Redistribution and use in source and binary forms, with or without
6*ba96d07fShrs * modification, are permitted provided that the following conditions are met:
7*ba96d07fShrs * - Redistributions of source code must retain the above copyright notice,
8*ba96d07fShrs * this list of conditions and the following disclaimer.
9*ba96d07fShrs * - Redistributions in binary form must reproduce the above copyright notice,
10*ba96d07fShrs * this list of conditions and the following disclaimer in the documentation
11*ba96d07fShrs * and/or other materials provided with the distribution.
12*ba96d07fShrs * - Neither the name of Sun Microsystems, Inc. nor the names of its
13*ba96d07fShrs * contributors may be used to endorse or promote products derived
14*ba96d07fShrs * from this software without specific prior written permission.
15ce0e08e2SPeter Avalos *
16*ba96d07fShrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*ba96d07fShrs * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*ba96d07fShrs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*ba96d07fShrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*ba96d07fShrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*ba96d07fShrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*ba96d07fShrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*ba96d07fShrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*ba96d07fShrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*ba96d07fShrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*ba96d07fShrs * POSSIBILITY OF SUCH DAMAGE.
27ce0e08e2SPeter Avalos *
28ce0e08e2SPeter Avalos * @(#)rpc_generic.c 1.17 94/04/24 SMI
29ce0e08e2SPeter Avalos * $NetBSD: rpc_generic.c,v 1.4 2000/09/28 09:07:04 kleink Exp $
30ce0e08e2SPeter Avalos * $FreeBSD: src/lib/libc/rpc/rpc_generic.c,v 1.14 2007/09/20 22:35:24 matteo Exp $
31ce0e08e2SPeter Avalos */
32ce0e08e2SPeter Avalos /*
33ce0e08e2SPeter Avalos * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34ce0e08e2SPeter Avalos */
35ce0e08e2SPeter Avalos
36ce0e08e2SPeter Avalos /*
37ce0e08e2SPeter Avalos * rpc_generic.c, Misc routines for RPC.
38ce0e08e2SPeter Avalos *
39ce0e08e2SPeter Avalos */
40ce0e08e2SPeter Avalos
41ce0e08e2SPeter Avalos #include "namespace.h"
42ce0e08e2SPeter Avalos #include "reentrant.h"
43ce0e08e2SPeter Avalos #include <sys/types.h>
44ce0e08e2SPeter Avalos #include <sys/param.h>
45ce0e08e2SPeter Avalos #include <sys/socket.h>
46ce0e08e2SPeter Avalos #include <sys/time.h>
47ce0e08e2SPeter Avalos #include <sys/un.h>
48ce0e08e2SPeter Avalos #include <sys/resource.h>
49ce0e08e2SPeter Avalos #include <netinet/in.h>
50ce0e08e2SPeter Avalos #include <arpa/inet.h>
51ce0e08e2SPeter Avalos #include <rpc/rpc.h>
52ce0e08e2SPeter Avalos #include <ctype.h>
53ce0e08e2SPeter Avalos #include <stddef.h>
54ce0e08e2SPeter Avalos #include <stdio.h>
55ce0e08e2SPeter Avalos #include <netdb.h>
56ce0e08e2SPeter Avalos #include <netconfig.h>
57ce0e08e2SPeter Avalos #include <stdlib.h>
58ce0e08e2SPeter Avalos #include <string.h>
59ce0e08e2SPeter Avalos #include <syslog.h>
60ce0e08e2SPeter Avalos #include <rpc/nettype.h>
61ce0e08e2SPeter Avalos #include "un-namespace.h"
62ce0e08e2SPeter Avalos #include "rpc_com.h"
63ce0e08e2SPeter Avalos #include "mt_misc.h"
64ce0e08e2SPeter Avalos
65ce0e08e2SPeter Avalos struct handle {
66ce0e08e2SPeter Avalos NCONF_HANDLE *nhandle;
67ce0e08e2SPeter Avalos int nflag; /* Whether NETPATH or NETCONFIG */
68ce0e08e2SPeter Avalos int nettype;
69ce0e08e2SPeter Avalos };
70ce0e08e2SPeter Avalos
71ce0e08e2SPeter Avalos static const struct _rpcnettype {
72ce0e08e2SPeter Avalos const char *name;
73ce0e08e2SPeter Avalos const int type;
74ce0e08e2SPeter Avalos } _rpctypelist[] = {
75ce0e08e2SPeter Avalos { "netpath", _RPC_NETPATH },
76ce0e08e2SPeter Avalos { "visible", _RPC_VISIBLE },
77ce0e08e2SPeter Avalos { "circuit_v", _RPC_CIRCUIT_V },
78ce0e08e2SPeter Avalos { "datagram_v", _RPC_DATAGRAM_V },
79ce0e08e2SPeter Avalos { "circuit_n", _RPC_CIRCUIT_N },
80ce0e08e2SPeter Avalos { "datagram_n", _RPC_DATAGRAM_N },
81ce0e08e2SPeter Avalos { "tcp", _RPC_TCP },
82ce0e08e2SPeter Avalos { "udp", _RPC_UDP },
83ce0e08e2SPeter Avalos { 0, _RPC_NONE }
84ce0e08e2SPeter Avalos };
85ce0e08e2SPeter Avalos
86ce0e08e2SPeter Avalos struct netid_af {
87ce0e08e2SPeter Avalos const char *netid;
88ce0e08e2SPeter Avalos int af;
89ce0e08e2SPeter Avalos int protocol;
90ce0e08e2SPeter Avalos };
91ce0e08e2SPeter Avalos
92ce0e08e2SPeter Avalos static const struct netid_af na_cvt[] = {
93ce0e08e2SPeter Avalos { "udp", AF_INET, IPPROTO_UDP },
94ce0e08e2SPeter Avalos { "tcp", AF_INET, IPPROTO_TCP },
95ce0e08e2SPeter Avalos #ifdef INET6
96ce0e08e2SPeter Avalos { "udp6", AF_INET6, IPPROTO_UDP },
97ce0e08e2SPeter Avalos { "tcp6", AF_INET6, IPPROTO_TCP },
98ce0e08e2SPeter Avalos #endif
99ce0e08e2SPeter Avalos { "local", AF_LOCAL, 0 }
100ce0e08e2SPeter Avalos };
101ce0e08e2SPeter Avalos
102ce0e08e2SPeter Avalos #if 0
103ce0e08e2SPeter Avalos static char *strlocase(char *);
104ce0e08e2SPeter Avalos #endif
105ce0e08e2SPeter Avalos static int getnettype(const char *);
106ce0e08e2SPeter Avalos
107ce0e08e2SPeter Avalos /*
108ce0e08e2SPeter Avalos * Cache the result of getrlimit(), so we don't have to do an
109ce0e08e2SPeter Avalos * expensive call every time.
110ce0e08e2SPeter Avalos */
111ce0e08e2SPeter Avalos int
__rpc_dtbsize(void)112ce0e08e2SPeter Avalos __rpc_dtbsize(void)
113ce0e08e2SPeter Avalos {
114ce0e08e2SPeter Avalos static int tbsize;
115ce0e08e2SPeter Avalos struct rlimit rl;
116ce0e08e2SPeter Avalos
117ce0e08e2SPeter Avalos if (tbsize) {
118ce0e08e2SPeter Avalos return (tbsize);
119ce0e08e2SPeter Avalos }
120ce0e08e2SPeter Avalos if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
121ce0e08e2SPeter Avalos return (tbsize = (int)rl.rlim_max);
122ce0e08e2SPeter Avalos }
123ce0e08e2SPeter Avalos /*
124ce0e08e2SPeter Avalos * Something wrong. I'll try to save face by returning a
125ce0e08e2SPeter Avalos * pessimistic number.
126ce0e08e2SPeter Avalos */
127ce0e08e2SPeter Avalos return (32);
128ce0e08e2SPeter Avalos }
129ce0e08e2SPeter Avalos
130ce0e08e2SPeter Avalos
131ce0e08e2SPeter Avalos /*
132ce0e08e2SPeter Avalos * Find the appropriate buffer size
133ce0e08e2SPeter Avalos */
134ce0e08e2SPeter Avalos u_int
135ce0e08e2SPeter Avalos /*ARGSUSED*/
__rpc_get_t_size(int af __unused,int proto,int size)1366d7019e6SSascha Wildner __rpc_get_t_size(int af __unused, int proto,
137ce0e08e2SPeter Avalos int size) /* Size requested */
138ce0e08e2SPeter Avalos {
139ce0e08e2SPeter Avalos int maxsize, defsize;
140ce0e08e2SPeter Avalos
141ce0e08e2SPeter Avalos maxsize = 256 * 1024; /* XXX */
142ce0e08e2SPeter Avalos switch (proto) {
143ce0e08e2SPeter Avalos case IPPROTO_TCP:
144ce0e08e2SPeter Avalos defsize = 64 * 1024; /* XXX */
145ce0e08e2SPeter Avalos break;
146ce0e08e2SPeter Avalos case IPPROTO_UDP:
147ce0e08e2SPeter Avalos defsize = UDPMSGSIZE;
148ce0e08e2SPeter Avalos break;
149ce0e08e2SPeter Avalos default:
150ce0e08e2SPeter Avalos defsize = RPC_MAXDATASIZE;
151ce0e08e2SPeter Avalos break;
152ce0e08e2SPeter Avalos }
153ce0e08e2SPeter Avalos if (size == 0)
154ce0e08e2SPeter Avalos return defsize;
155ce0e08e2SPeter Avalos
156ce0e08e2SPeter Avalos /* Check whether the value is within the upper max limit */
157ce0e08e2SPeter Avalos return (size > maxsize ? (u_int)maxsize : (u_int)size);
158ce0e08e2SPeter Avalos }
159ce0e08e2SPeter Avalos
160ce0e08e2SPeter Avalos /*
161ce0e08e2SPeter Avalos * Find the appropriate address buffer size
162ce0e08e2SPeter Avalos */
163ce0e08e2SPeter Avalos u_int
__rpc_get_a_size(int af)164ce0e08e2SPeter Avalos __rpc_get_a_size(int af)
165ce0e08e2SPeter Avalos {
166ce0e08e2SPeter Avalos switch (af) {
167ce0e08e2SPeter Avalos case AF_INET:
168ce0e08e2SPeter Avalos return sizeof (struct sockaddr_in);
169ce0e08e2SPeter Avalos #ifdef INET6
170ce0e08e2SPeter Avalos case AF_INET6:
171ce0e08e2SPeter Avalos return sizeof (struct sockaddr_in6);
172ce0e08e2SPeter Avalos #endif
173ce0e08e2SPeter Avalos case AF_LOCAL:
174ce0e08e2SPeter Avalos return sizeof (struct sockaddr_un);
175ce0e08e2SPeter Avalos default:
176ce0e08e2SPeter Avalos break;
177ce0e08e2SPeter Avalos }
178ce0e08e2SPeter Avalos return ((u_int)RPC_MAXADDRSIZE);
179ce0e08e2SPeter Avalos }
180ce0e08e2SPeter Avalos
181ce0e08e2SPeter Avalos #if 0
182ce0e08e2SPeter Avalos static char *
183ce0e08e2SPeter Avalos strlocase(char *p)
184ce0e08e2SPeter Avalos {
185ce0e08e2SPeter Avalos char *t = p;
186ce0e08e2SPeter Avalos
187ce0e08e2SPeter Avalos for (; *p; p++)
188ce0e08e2SPeter Avalos if (isupper(*p))
189ce0e08e2SPeter Avalos *p = tolower(*p);
190ce0e08e2SPeter Avalos return (t);
191ce0e08e2SPeter Avalos }
192ce0e08e2SPeter Avalos #endif
193ce0e08e2SPeter Avalos
194ce0e08e2SPeter Avalos /*
195ce0e08e2SPeter Avalos * Returns the type of the network as defined in <rpc/nettype.h>
196ce0e08e2SPeter Avalos * If nettype is NULL, it defaults to NETPATH.
197ce0e08e2SPeter Avalos */
198ce0e08e2SPeter Avalos static int
getnettype(const char * nettype)199ce0e08e2SPeter Avalos getnettype(const char *nettype)
200ce0e08e2SPeter Avalos {
201ce0e08e2SPeter Avalos int i;
202ce0e08e2SPeter Avalos
203ce0e08e2SPeter Avalos if ((nettype == NULL) || (nettype[0] == 0)) {
204ce0e08e2SPeter Avalos return (_RPC_NETPATH); /* Default */
205ce0e08e2SPeter Avalos }
206ce0e08e2SPeter Avalos
207ce0e08e2SPeter Avalos #if 0
208ce0e08e2SPeter Avalos nettype = strlocase(nettype);
209ce0e08e2SPeter Avalos #endif
210ce0e08e2SPeter Avalos for (i = 0; _rpctypelist[i].name; i++)
211ce0e08e2SPeter Avalos if (strcasecmp(nettype, _rpctypelist[i].name) == 0) {
212ce0e08e2SPeter Avalos return (_rpctypelist[i].type);
213ce0e08e2SPeter Avalos }
214ce0e08e2SPeter Avalos return (_rpctypelist[i].type);
215ce0e08e2SPeter Avalos }
216ce0e08e2SPeter Avalos
217ce0e08e2SPeter Avalos /*
218ce0e08e2SPeter Avalos * For the given nettype (tcp or udp only), return the first structure found.
219ce0e08e2SPeter Avalos * This should be freed by calling freenetconfigent()
220ce0e08e2SPeter Avalos */
221ce0e08e2SPeter Avalos struct netconfig *
__rpc_getconfip(const char * nettype)222ce0e08e2SPeter Avalos __rpc_getconfip(const char *nettype)
223ce0e08e2SPeter Avalos {
224ce0e08e2SPeter Avalos char *netid;
2252038fb68SSascha Wildner char *netid_tcp = NULL;
2262038fb68SSascha Wildner char *netid_udp = NULL;
227ce0e08e2SPeter Avalos static char *netid_tcp_main;
228ce0e08e2SPeter Avalos static char *netid_udp_main;
229ce0e08e2SPeter Avalos struct netconfig *dummy;
230ce0e08e2SPeter Avalos int main_thread;
231ce0e08e2SPeter Avalos static thread_key_t tcp_key, udp_key;
232ce0e08e2SPeter Avalos
233ce0e08e2SPeter Avalos if ((main_thread = thr_main())) {
234ce0e08e2SPeter Avalos netid_udp = netid_udp_main;
235ce0e08e2SPeter Avalos netid_tcp = netid_tcp_main;
236ce0e08e2SPeter Avalos } else {
237ce0e08e2SPeter Avalos if (tcp_key == 0) {
238ce0e08e2SPeter Avalos mutex_lock(&tsd_lock);
239ce0e08e2SPeter Avalos if (tcp_key == 0)
240ce0e08e2SPeter Avalos thr_keycreate(&tcp_key, free);
241ce0e08e2SPeter Avalos mutex_unlock(&tsd_lock);
242ce0e08e2SPeter Avalos }
243ce0e08e2SPeter Avalos netid_tcp = (char *)thr_getspecific(tcp_key);
244ce0e08e2SPeter Avalos if (udp_key == 0) {
245ce0e08e2SPeter Avalos mutex_lock(&tsd_lock);
246ce0e08e2SPeter Avalos if (udp_key == 0)
247ce0e08e2SPeter Avalos thr_keycreate(&udp_key, free);
248ce0e08e2SPeter Avalos mutex_unlock(&tsd_lock);
249ce0e08e2SPeter Avalos }
250ce0e08e2SPeter Avalos netid_udp = (char *)thr_getspecific(udp_key);
251ce0e08e2SPeter Avalos }
252ce0e08e2SPeter Avalos if (!netid_udp && !netid_tcp) {
253ce0e08e2SPeter Avalos struct netconfig *nconf;
254ce0e08e2SPeter Avalos void *confighandle;
255ce0e08e2SPeter Avalos
256ce0e08e2SPeter Avalos if (!(confighandle = setnetconfig())) {
257ce0e08e2SPeter Avalos syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
258ce0e08e2SPeter Avalos return (NULL);
259ce0e08e2SPeter Avalos }
260ce0e08e2SPeter Avalos while ((nconf = getnetconfig(confighandle)) != NULL) {
261ce0e08e2SPeter Avalos if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
262ce0e08e2SPeter Avalos if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
263ce0e08e2SPeter Avalos netid_tcp = strdup(nconf->nc_netid);
264ce0e08e2SPeter Avalos if (main_thread)
265ce0e08e2SPeter Avalos netid_tcp_main = netid_tcp;
266ce0e08e2SPeter Avalos else
267ce0e08e2SPeter Avalos thr_setspecific(tcp_key,
268ce0e08e2SPeter Avalos (void *) netid_tcp);
269ce0e08e2SPeter Avalos } else
270ce0e08e2SPeter Avalos if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
271ce0e08e2SPeter Avalos netid_udp = strdup(nconf->nc_netid);
272ce0e08e2SPeter Avalos if (main_thread)
273ce0e08e2SPeter Avalos netid_udp_main = netid_udp;
274ce0e08e2SPeter Avalos else
275ce0e08e2SPeter Avalos thr_setspecific(udp_key,
276ce0e08e2SPeter Avalos (void *) netid_udp);
277ce0e08e2SPeter Avalos }
278ce0e08e2SPeter Avalos }
279ce0e08e2SPeter Avalos }
280ce0e08e2SPeter Avalos endnetconfig(confighandle);
281ce0e08e2SPeter Avalos }
282ce0e08e2SPeter Avalos if (strcmp(nettype, "udp") == 0)
283ce0e08e2SPeter Avalos netid = netid_udp;
284ce0e08e2SPeter Avalos else if (strcmp(nettype, "tcp") == 0)
285ce0e08e2SPeter Avalos netid = netid_tcp;
286ce0e08e2SPeter Avalos else {
287ce0e08e2SPeter Avalos return (NULL);
288ce0e08e2SPeter Avalos }
289ce0e08e2SPeter Avalos if ((netid == NULL) || (netid[0] == 0)) {
290ce0e08e2SPeter Avalos return (NULL);
291ce0e08e2SPeter Avalos }
292ce0e08e2SPeter Avalos dummy = getnetconfigent(netid);
293ce0e08e2SPeter Avalos return (dummy);
294ce0e08e2SPeter Avalos }
295ce0e08e2SPeter Avalos
296ce0e08e2SPeter Avalos /*
297ce0e08e2SPeter Avalos * Returns the type of the nettype, which should then be used with
298ce0e08e2SPeter Avalos * __rpc_getconf().
299ce0e08e2SPeter Avalos */
300ce0e08e2SPeter Avalos void *
__rpc_setconf(const char * nettype)301ce0e08e2SPeter Avalos __rpc_setconf(const char *nettype)
302ce0e08e2SPeter Avalos {
303ce0e08e2SPeter Avalos struct handle *handle;
304ce0e08e2SPeter Avalos
305ce0e08e2SPeter Avalos handle = (struct handle *) malloc(sizeof (struct handle));
306ce0e08e2SPeter Avalos if (handle == NULL) {
307ce0e08e2SPeter Avalos return (NULL);
308ce0e08e2SPeter Avalos }
309ce0e08e2SPeter Avalos switch (handle->nettype = getnettype(nettype)) {
310ce0e08e2SPeter Avalos case _RPC_NETPATH:
311ce0e08e2SPeter Avalos case _RPC_CIRCUIT_N:
312ce0e08e2SPeter Avalos case _RPC_DATAGRAM_N:
313ce0e08e2SPeter Avalos if (!(handle->nhandle = setnetpath()))
314ce0e08e2SPeter Avalos goto failed;
315ce0e08e2SPeter Avalos handle->nflag = TRUE;
316ce0e08e2SPeter Avalos break;
317ce0e08e2SPeter Avalos case _RPC_VISIBLE:
318ce0e08e2SPeter Avalos case _RPC_CIRCUIT_V:
319ce0e08e2SPeter Avalos case _RPC_DATAGRAM_V:
320ce0e08e2SPeter Avalos case _RPC_TCP:
321ce0e08e2SPeter Avalos case _RPC_UDP:
322ce0e08e2SPeter Avalos if (!(handle->nhandle = setnetconfig())) {
323ce0e08e2SPeter Avalos syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
324ce0e08e2SPeter Avalos goto failed;
325ce0e08e2SPeter Avalos }
326ce0e08e2SPeter Avalos handle->nflag = FALSE;
327ce0e08e2SPeter Avalos break;
328ce0e08e2SPeter Avalos default:
329ce0e08e2SPeter Avalos goto failed;
330ce0e08e2SPeter Avalos }
331ce0e08e2SPeter Avalos
332ce0e08e2SPeter Avalos return (handle);
333ce0e08e2SPeter Avalos
334ce0e08e2SPeter Avalos failed:
335ce0e08e2SPeter Avalos free(handle);
336ce0e08e2SPeter Avalos return (NULL);
337ce0e08e2SPeter Avalos }
338ce0e08e2SPeter Avalos
339ce0e08e2SPeter Avalos /*
340ce0e08e2SPeter Avalos * Returns the next netconfig struct for the given "net" type.
341ce0e08e2SPeter Avalos * __rpc_setconf() should have been called previously.
342ce0e08e2SPeter Avalos */
343ce0e08e2SPeter Avalos struct netconfig *
__rpc_getconf(void * vhandle)344ce0e08e2SPeter Avalos __rpc_getconf(void *vhandle)
345ce0e08e2SPeter Avalos {
346ce0e08e2SPeter Avalos struct handle *handle;
347ce0e08e2SPeter Avalos struct netconfig *nconf;
348ce0e08e2SPeter Avalos
349ce0e08e2SPeter Avalos handle = (struct handle *)vhandle;
350ce0e08e2SPeter Avalos if (handle == NULL) {
351ce0e08e2SPeter Avalos return (NULL);
352ce0e08e2SPeter Avalos }
353ce0e08e2SPeter Avalos for (;;) {
354ce0e08e2SPeter Avalos if (handle->nflag)
355ce0e08e2SPeter Avalos nconf = getnetpath(handle->nhandle);
356ce0e08e2SPeter Avalos else
357ce0e08e2SPeter Avalos nconf = getnetconfig(handle->nhandle);
358ce0e08e2SPeter Avalos if (nconf == NULL)
359ce0e08e2SPeter Avalos break;
360ce0e08e2SPeter Avalos if ((nconf->nc_semantics != NC_TPI_CLTS) &&
361ce0e08e2SPeter Avalos (nconf->nc_semantics != NC_TPI_COTS) &&
362ce0e08e2SPeter Avalos (nconf->nc_semantics != NC_TPI_COTS_ORD))
363ce0e08e2SPeter Avalos continue;
364ce0e08e2SPeter Avalos switch (handle->nettype) {
365ce0e08e2SPeter Avalos case _RPC_VISIBLE:
366ce0e08e2SPeter Avalos if (!(nconf->nc_flag & NC_VISIBLE))
367ce0e08e2SPeter Avalos continue;
368ce0e08e2SPeter Avalos /* FALLTHROUGH */
369ce0e08e2SPeter Avalos case _RPC_NETPATH: /* Be happy */
370ce0e08e2SPeter Avalos break;
371ce0e08e2SPeter Avalos case _RPC_CIRCUIT_V:
372ce0e08e2SPeter Avalos if (!(nconf->nc_flag & NC_VISIBLE))
373ce0e08e2SPeter Avalos continue;
374ce0e08e2SPeter Avalos /* FALLTHROUGH */
375ce0e08e2SPeter Avalos case _RPC_CIRCUIT_N:
376ce0e08e2SPeter Avalos if ((nconf->nc_semantics != NC_TPI_COTS) &&
377ce0e08e2SPeter Avalos (nconf->nc_semantics != NC_TPI_COTS_ORD))
378ce0e08e2SPeter Avalos continue;
379ce0e08e2SPeter Avalos break;
380ce0e08e2SPeter Avalos case _RPC_DATAGRAM_V:
381ce0e08e2SPeter Avalos if (!(nconf->nc_flag & NC_VISIBLE))
382ce0e08e2SPeter Avalos continue;
383ce0e08e2SPeter Avalos /* FALLTHROUGH */
384ce0e08e2SPeter Avalos case _RPC_DATAGRAM_N:
385ce0e08e2SPeter Avalos if (nconf->nc_semantics != NC_TPI_CLTS)
386ce0e08e2SPeter Avalos continue;
387ce0e08e2SPeter Avalos break;
388ce0e08e2SPeter Avalos case _RPC_TCP:
389ce0e08e2SPeter Avalos if (((nconf->nc_semantics != NC_TPI_COTS) &&
390ce0e08e2SPeter Avalos (nconf->nc_semantics != NC_TPI_COTS_ORD)) ||
391ce0e08e2SPeter Avalos (strcmp(nconf->nc_protofmly, NC_INET)
392ce0e08e2SPeter Avalos #ifdef INET6
393ce0e08e2SPeter Avalos && strcmp(nconf->nc_protofmly, NC_INET6))
394ce0e08e2SPeter Avalos #else
395ce0e08e2SPeter Avalos )
396ce0e08e2SPeter Avalos #endif
397ce0e08e2SPeter Avalos ||
398ce0e08e2SPeter Avalos strcmp(nconf->nc_proto, NC_TCP))
399ce0e08e2SPeter Avalos continue;
400ce0e08e2SPeter Avalos break;
401ce0e08e2SPeter Avalos case _RPC_UDP:
402ce0e08e2SPeter Avalos if ((nconf->nc_semantics != NC_TPI_CLTS) ||
403ce0e08e2SPeter Avalos (strcmp(nconf->nc_protofmly, NC_INET)
404ce0e08e2SPeter Avalos #ifdef INET6
405ce0e08e2SPeter Avalos && strcmp(nconf->nc_protofmly, NC_INET6))
406ce0e08e2SPeter Avalos #else
407ce0e08e2SPeter Avalos )
408ce0e08e2SPeter Avalos #endif
409ce0e08e2SPeter Avalos ||
410ce0e08e2SPeter Avalos strcmp(nconf->nc_proto, NC_UDP))
411ce0e08e2SPeter Avalos continue;
412ce0e08e2SPeter Avalos break;
413ce0e08e2SPeter Avalos }
414ce0e08e2SPeter Avalos break;
415ce0e08e2SPeter Avalos }
416ce0e08e2SPeter Avalos return (nconf);
417ce0e08e2SPeter Avalos }
418ce0e08e2SPeter Avalos
419ce0e08e2SPeter Avalos void
__rpc_endconf(void * vhandle)420ce0e08e2SPeter Avalos __rpc_endconf(void *vhandle)
421ce0e08e2SPeter Avalos {
422ce0e08e2SPeter Avalos struct handle *handle;
423ce0e08e2SPeter Avalos
424ce0e08e2SPeter Avalos handle = (struct handle *) vhandle;
425ce0e08e2SPeter Avalos if (handle == NULL) {
426ce0e08e2SPeter Avalos return;
427ce0e08e2SPeter Avalos }
428ce0e08e2SPeter Avalos if (handle->nflag) {
429ce0e08e2SPeter Avalos endnetpath(handle->nhandle);
430ce0e08e2SPeter Avalos } else {
431ce0e08e2SPeter Avalos endnetconfig(handle->nhandle);
432ce0e08e2SPeter Avalos }
433ce0e08e2SPeter Avalos free(handle);
434ce0e08e2SPeter Avalos }
435ce0e08e2SPeter Avalos
436ce0e08e2SPeter Avalos /*
437ce0e08e2SPeter Avalos * Used to ping the NULL procedure for clnt handle.
438ce0e08e2SPeter Avalos * Returns NULL if fails, else a non-NULL pointer.
439ce0e08e2SPeter Avalos */
440ce0e08e2SPeter Avalos void *
rpc_nullproc(CLIENT * clnt)441ce0e08e2SPeter Avalos rpc_nullproc(CLIENT *clnt)
442ce0e08e2SPeter Avalos {
443ce0e08e2SPeter Avalos struct timeval TIMEOUT = {25, 0};
444ce0e08e2SPeter Avalos
445ce0e08e2SPeter Avalos if (clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, NULL,
446ce0e08e2SPeter Avalos (xdrproc_t) xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
447ce0e08e2SPeter Avalos return (NULL);
448ce0e08e2SPeter Avalos }
449ce0e08e2SPeter Avalos return ((void *) clnt);
450ce0e08e2SPeter Avalos }
451ce0e08e2SPeter Avalos
452ce0e08e2SPeter Avalos /*
453ce0e08e2SPeter Avalos * Try all possible transports until
454ce0e08e2SPeter Avalos * one succeeds in finding the netconf for the given fd.
455ce0e08e2SPeter Avalos */
456ce0e08e2SPeter Avalos struct netconfig *
__rpcgettp(int fd)457ce0e08e2SPeter Avalos __rpcgettp(int fd)
458ce0e08e2SPeter Avalos {
459ce0e08e2SPeter Avalos const char *netid;
460ce0e08e2SPeter Avalos struct __rpc_sockinfo si;
461ce0e08e2SPeter Avalos
462ce0e08e2SPeter Avalos if (!__rpc_fd2sockinfo(fd, &si))
463ce0e08e2SPeter Avalos return NULL;
464ce0e08e2SPeter Avalos
465ce0e08e2SPeter Avalos if (!__rpc_sockinfo2netid(&si, &netid))
466ce0e08e2SPeter Avalos return NULL;
467ce0e08e2SPeter Avalos
468ce0e08e2SPeter Avalos /*LINTED const castaway*/
469ce0e08e2SPeter Avalos return getnetconfigent((char *)netid);
470ce0e08e2SPeter Avalos }
471ce0e08e2SPeter Avalos
472ce0e08e2SPeter Avalos int
__rpc_fd2sockinfo(int fd,struct __rpc_sockinfo * sip)473ce0e08e2SPeter Avalos __rpc_fd2sockinfo(int fd, struct __rpc_sockinfo *sip)
474ce0e08e2SPeter Avalos {
475ce0e08e2SPeter Avalos socklen_t len;
476ce0e08e2SPeter Avalos int type, proto;
477ce0e08e2SPeter Avalos struct sockaddr_storage ss;
478ce0e08e2SPeter Avalos
479ce0e08e2SPeter Avalos len = sizeof ss;
480ce0e08e2SPeter Avalos if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &len) < 0)
481ce0e08e2SPeter Avalos return 0;
482ce0e08e2SPeter Avalos sip->si_alen = len;
483ce0e08e2SPeter Avalos
484ce0e08e2SPeter Avalos len = sizeof type;
485ce0e08e2SPeter Avalos if (_getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &len) < 0)
486ce0e08e2SPeter Avalos return 0;
487ce0e08e2SPeter Avalos
488ce0e08e2SPeter Avalos /* XXX */
489ce0e08e2SPeter Avalos if (ss.ss_family != AF_LOCAL) {
490ce0e08e2SPeter Avalos if (type == SOCK_STREAM)
491ce0e08e2SPeter Avalos proto = IPPROTO_TCP;
492ce0e08e2SPeter Avalos else if (type == SOCK_DGRAM)
493ce0e08e2SPeter Avalos proto = IPPROTO_UDP;
494ce0e08e2SPeter Avalos else
495ce0e08e2SPeter Avalos return 0;
496ce0e08e2SPeter Avalos } else
497ce0e08e2SPeter Avalos proto = 0;
498ce0e08e2SPeter Avalos
499ce0e08e2SPeter Avalos sip->si_af = ss.ss_family;
500ce0e08e2SPeter Avalos sip->si_proto = proto;
501ce0e08e2SPeter Avalos sip->si_socktype = type;
502ce0e08e2SPeter Avalos
503ce0e08e2SPeter Avalos return 1;
504ce0e08e2SPeter Avalos }
505ce0e08e2SPeter Avalos
506ce0e08e2SPeter Avalos /*
507ce0e08e2SPeter Avalos * Linear search, but the number of entries is small.
508ce0e08e2SPeter Avalos */
509ce0e08e2SPeter Avalos int
__rpc_nconf2sockinfo(const struct netconfig * nconf,struct __rpc_sockinfo * sip)510ce0e08e2SPeter Avalos __rpc_nconf2sockinfo(const struct netconfig *nconf, struct __rpc_sockinfo *sip)
511ce0e08e2SPeter Avalos {
512ce0e08e2SPeter Avalos int i;
513ce0e08e2SPeter Avalos
514ce0e08e2SPeter Avalos for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++)
515ce0e08e2SPeter Avalos if (strcmp(na_cvt[i].netid, nconf->nc_netid) == 0 || (
516ce0e08e2SPeter Avalos strcmp(nconf->nc_netid, "unix") == 0 &&
517ce0e08e2SPeter Avalos strcmp(na_cvt[i].netid, "local") == 0)) {
518ce0e08e2SPeter Avalos sip->si_af = na_cvt[i].af;
519ce0e08e2SPeter Avalos sip->si_proto = na_cvt[i].protocol;
520ce0e08e2SPeter Avalos sip->si_socktype =
521ce0e08e2SPeter Avalos __rpc_seman2socktype((int)nconf->nc_semantics);
522ce0e08e2SPeter Avalos if (sip->si_socktype == -1)
523ce0e08e2SPeter Avalos return 0;
524ce0e08e2SPeter Avalos sip->si_alen = __rpc_get_a_size(sip->si_af);
525ce0e08e2SPeter Avalos return 1;
526ce0e08e2SPeter Avalos }
527ce0e08e2SPeter Avalos
528ce0e08e2SPeter Avalos return 0;
529ce0e08e2SPeter Avalos }
530ce0e08e2SPeter Avalos
531ce0e08e2SPeter Avalos int
__rpc_nconf2fd(const struct netconfig * nconf)532ce0e08e2SPeter Avalos __rpc_nconf2fd(const struct netconfig *nconf)
533ce0e08e2SPeter Avalos {
534ce0e08e2SPeter Avalos struct __rpc_sockinfo si;
535ce0e08e2SPeter Avalos
536ce0e08e2SPeter Avalos if (!__rpc_nconf2sockinfo(nconf, &si))
537ce0e08e2SPeter Avalos return 0;
538ce0e08e2SPeter Avalos
539ce0e08e2SPeter Avalos return _socket(si.si_af, si.si_socktype, si.si_proto);
540ce0e08e2SPeter Avalos }
541ce0e08e2SPeter Avalos
542ce0e08e2SPeter Avalos int
__rpc_sockinfo2netid(struct __rpc_sockinfo * sip,const char ** netid)543ce0e08e2SPeter Avalos __rpc_sockinfo2netid(struct __rpc_sockinfo *sip, const char **netid)
544ce0e08e2SPeter Avalos {
545ce0e08e2SPeter Avalos int i;
546ce0e08e2SPeter Avalos struct netconfig *nconf;
547ce0e08e2SPeter Avalos
548ce0e08e2SPeter Avalos nconf = getnetconfigent("local");
549ce0e08e2SPeter Avalos
550ce0e08e2SPeter Avalos for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++) {
551ce0e08e2SPeter Avalos if (na_cvt[i].af == sip->si_af &&
552ce0e08e2SPeter Avalos na_cvt[i].protocol == sip->si_proto) {
553ce0e08e2SPeter Avalos if (strcmp(na_cvt[i].netid, "local") == 0 && nconf == NULL) {
554ce0e08e2SPeter Avalos if (netid)
555ce0e08e2SPeter Avalos *netid = "unix";
556ce0e08e2SPeter Avalos } else {
557ce0e08e2SPeter Avalos if (netid)
558ce0e08e2SPeter Avalos *netid = na_cvt[i].netid;
559ce0e08e2SPeter Avalos }
560ce0e08e2SPeter Avalos if (nconf != NULL)
561ce0e08e2SPeter Avalos freenetconfigent(nconf);
562ce0e08e2SPeter Avalos return 1;
563ce0e08e2SPeter Avalos }
564ce0e08e2SPeter Avalos }
565ce0e08e2SPeter Avalos if (nconf != NULL)
566ce0e08e2SPeter Avalos freenetconfigent(nconf);
567ce0e08e2SPeter Avalos
568ce0e08e2SPeter Avalos return 0;
569ce0e08e2SPeter Avalos }
570ce0e08e2SPeter Avalos
571ce0e08e2SPeter Avalos char *
taddr2uaddr(const struct netconfig * nconf,const struct netbuf * nbuf)572ce0e08e2SPeter Avalos taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf)
573ce0e08e2SPeter Avalos {
574ce0e08e2SPeter Avalos struct __rpc_sockinfo si;
575ce0e08e2SPeter Avalos
576ce0e08e2SPeter Avalos if (!__rpc_nconf2sockinfo(nconf, &si))
577ce0e08e2SPeter Avalos return NULL;
578ce0e08e2SPeter Avalos return __rpc_taddr2uaddr_af(si.si_af, nbuf);
579ce0e08e2SPeter Avalos }
580ce0e08e2SPeter Avalos
581ce0e08e2SPeter Avalos struct netbuf *
uaddr2taddr(const struct netconfig * nconf,const char * uaddr)582ce0e08e2SPeter Avalos uaddr2taddr(const struct netconfig *nconf, const char *uaddr)
583ce0e08e2SPeter Avalos {
584ce0e08e2SPeter Avalos struct __rpc_sockinfo si;
585ce0e08e2SPeter Avalos
586ce0e08e2SPeter Avalos if (!__rpc_nconf2sockinfo(nconf, &si))
587ce0e08e2SPeter Avalos return NULL;
588ce0e08e2SPeter Avalos return __rpc_uaddr2taddr_af(si.si_af, uaddr);
589ce0e08e2SPeter Avalos }
590ce0e08e2SPeter Avalos
591ce0e08e2SPeter Avalos char *
__rpc_taddr2uaddr_af(int af,const struct netbuf * nbuf)592ce0e08e2SPeter Avalos __rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf)
593ce0e08e2SPeter Avalos {
594ce0e08e2SPeter Avalos char *ret;
595ce0e08e2SPeter Avalos struct sockaddr_in *sin;
596ce0e08e2SPeter Avalos struct sockaddr_un *sun;
597ce0e08e2SPeter Avalos char namebuf[INET_ADDRSTRLEN];
598ce0e08e2SPeter Avalos #ifdef INET6
599ce0e08e2SPeter Avalos struct sockaddr_in6 *sin6;
600ce0e08e2SPeter Avalos char namebuf6[INET6_ADDRSTRLEN];
601ce0e08e2SPeter Avalos #endif
602ce0e08e2SPeter Avalos u_int16_t port;
603ce0e08e2SPeter Avalos
604ce0e08e2SPeter Avalos switch (af) {
605ce0e08e2SPeter Avalos case AF_INET:
606ce0e08e2SPeter Avalos sin = nbuf->buf;
607ce0e08e2SPeter Avalos if (inet_ntop(af, &sin->sin_addr, namebuf, sizeof namebuf)
608ce0e08e2SPeter Avalos == NULL)
609ce0e08e2SPeter Avalos return NULL;
610ce0e08e2SPeter Avalos port = ntohs(sin->sin_port);
611ce0e08e2SPeter Avalos if (asprintf(&ret, "%s.%u.%u", namebuf, ((u_int32_t)port) >> 8,
612ce0e08e2SPeter Avalos port & 0xff) < 0)
613ce0e08e2SPeter Avalos return NULL;
614ce0e08e2SPeter Avalos break;
615ce0e08e2SPeter Avalos #ifdef INET6
616ce0e08e2SPeter Avalos case AF_INET6:
617ce0e08e2SPeter Avalos sin6 = nbuf->buf;
618ce0e08e2SPeter Avalos if (inet_ntop(af, &sin6->sin6_addr, namebuf6, sizeof namebuf6)
619ce0e08e2SPeter Avalos == NULL)
620ce0e08e2SPeter Avalos return NULL;
621ce0e08e2SPeter Avalos port = ntohs(sin6->sin6_port);
622ce0e08e2SPeter Avalos if (asprintf(&ret, "%s.%u.%u", namebuf6, ((u_int32_t)port) >> 8,
623ce0e08e2SPeter Avalos port & 0xff) < 0)
624ce0e08e2SPeter Avalos return NULL;
625ce0e08e2SPeter Avalos break;
626ce0e08e2SPeter Avalos #endif
627ce0e08e2SPeter Avalos case AF_LOCAL:
628ce0e08e2SPeter Avalos sun = nbuf->buf;
629ce0e08e2SPeter Avalos if (asprintf(&ret, "%.*s", (int)(sun->sun_len -
630ce0e08e2SPeter Avalos offsetof(struct sockaddr_un, sun_path)),
631ce0e08e2SPeter Avalos sun->sun_path) < 0)
632ce0e08e2SPeter Avalos return (NULL);
633ce0e08e2SPeter Avalos break;
634ce0e08e2SPeter Avalos default:
635ce0e08e2SPeter Avalos return NULL;
636ce0e08e2SPeter Avalos }
637ce0e08e2SPeter Avalos
638ce0e08e2SPeter Avalos return ret;
639ce0e08e2SPeter Avalos }
640ce0e08e2SPeter Avalos
641ce0e08e2SPeter Avalos struct netbuf *
__rpc_uaddr2taddr_af(int af,const char * uaddr)642ce0e08e2SPeter Avalos __rpc_uaddr2taddr_af(int af, const char *uaddr)
643ce0e08e2SPeter Avalos {
644ce0e08e2SPeter Avalos struct netbuf *ret = NULL;
645ce0e08e2SPeter Avalos char *addrstr, *p;
646ce0e08e2SPeter Avalos unsigned port, portlo, porthi;
647ce0e08e2SPeter Avalos struct sockaddr_in *sin;
648ce0e08e2SPeter Avalos #ifdef INET6
649ce0e08e2SPeter Avalos struct sockaddr_in6 *sin6;
650ce0e08e2SPeter Avalos #endif
651ce0e08e2SPeter Avalos struct sockaddr_un *sun;
652ce0e08e2SPeter Avalos
653ce0e08e2SPeter Avalos port = 0;
654ce0e08e2SPeter Avalos sin = NULL;
655ce0e08e2SPeter Avalos addrstr = strdup(uaddr);
656ce0e08e2SPeter Avalos if (addrstr == NULL)
657ce0e08e2SPeter Avalos return NULL;
658ce0e08e2SPeter Avalos
659ce0e08e2SPeter Avalos /*
660ce0e08e2SPeter Avalos * AF_LOCAL addresses are expected to be absolute
661ce0e08e2SPeter Avalos * pathnames, anything else will be AF_INET or AF_INET6.
662ce0e08e2SPeter Avalos */
663ce0e08e2SPeter Avalos if (*addrstr != '/') {
664ce0e08e2SPeter Avalos p = strrchr(addrstr, '.');
665ce0e08e2SPeter Avalos if (p == NULL)
666ce0e08e2SPeter Avalos goto out;
667ce0e08e2SPeter Avalos portlo = (unsigned)atoi(p + 1);
668ce0e08e2SPeter Avalos *p = '\0';
669ce0e08e2SPeter Avalos
670ce0e08e2SPeter Avalos p = strrchr(addrstr, '.');
671ce0e08e2SPeter Avalos if (p == NULL)
672ce0e08e2SPeter Avalos goto out;
673ce0e08e2SPeter Avalos porthi = (unsigned)atoi(p + 1);
674ce0e08e2SPeter Avalos *p = '\0';
675ce0e08e2SPeter Avalos port = (porthi << 8) | portlo;
676ce0e08e2SPeter Avalos }
677ce0e08e2SPeter Avalos
678ce0e08e2SPeter Avalos ret = (struct netbuf *)malloc(sizeof *ret);
679ce0e08e2SPeter Avalos if (ret == NULL)
680ce0e08e2SPeter Avalos goto out;
681ce0e08e2SPeter Avalos
682ce0e08e2SPeter Avalos switch (af) {
683ce0e08e2SPeter Avalos case AF_INET:
684ce0e08e2SPeter Avalos sin = (struct sockaddr_in *)malloc(sizeof *sin);
685ce0e08e2SPeter Avalos if (sin == NULL)
686ce0e08e2SPeter Avalos goto out;
687ce0e08e2SPeter Avalos memset(sin, 0, sizeof *sin);
688ce0e08e2SPeter Avalos sin->sin_family = AF_INET;
689ce0e08e2SPeter Avalos sin->sin_port = htons(port);
690ce0e08e2SPeter Avalos if (inet_pton(AF_INET, addrstr, &sin->sin_addr) <= 0) {
691ce0e08e2SPeter Avalos free(sin);
692ce0e08e2SPeter Avalos free(ret);
693ce0e08e2SPeter Avalos ret = NULL;
694ce0e08e2SPeter Avalos goto out;
695ce0e08e2SPeter Avalos }
696ce0e08e2SPeter Avalos sin->sin_len = ret->maxlen = ret->len = sizeof *sin;
697ce0e08e2SPeter Avalos ret->buf = sin;
698ce0e08e2SPeter Avalos break;
699ce0e08e2SPeter Avalos #ifdef INET6
700ce0e08e2SPeter Avalos case AF_INET6:
701ce0e08e2SPeter Avalos sin6 = (struct sockaddr_in6 *)malloc(sizeof *sin6);
702ce0e08e2SPeter Avalos if (sin6 == NULL)
703ce0e08e2SPeter Avalos goto out;
704ce0e08e2SPeter Avalos memset(sin6, 0, sizeof *sin6);
705ce0e08e2SPeter Avalos sin6->sin6_family = AF_INET6;
706ce0e08e2SPeter Avalos sin6->sin6_port = htons(port);
707ce0e08e2SPeter Avalos if (inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) <= 0) {
708ce0e08e2SPeter Avalos free(sin6);
709ce0e08e2SPeter Avalos free(ret);
710ce0e08e2SPeter Avalos ret = NULL;
711ce0e08e2SPeter Avalos goto out;
712ce0e08e2SPeter Avalos }
713ce0e08e2SPeter Avalos sin6->sin6_len = ret->maxlen = ret->len = sizeof *sin6;
714ce0e08e2SPeter Avalos ret->buf = sin6;
715ce0e08e2SPeter Avalos break;
716ce0e08e2SPeter Avalos #endif
717ce0e08e2SPeter Avalos case AF_LOCAL:
718ce0e08e2SPeter Avalos sun = (struct sockaddr_un *)malloc(sizeof *sun);
719ce0e08e2SPeter Avalos if (sun == NULL)
720ce0e08e2SPeter Avalos goto out;
721ce0e08e2SPeter Avalos memset(sun, 0, sizeof *sun);
722ce0e08e2SPeter Avalos sun->sun_family = AF_LOCAL;
723ce0e08e2SPeter Avalos strncpy(sun->sun_path, addrstr, sizeof(sun->sun_path) - 1);
724ce0e08e2SPeter Avalos ret->len = ret->maxlen = sun->sun_len = SUN_LEN(sun);
725ce0e08e2SPeter Avalos ret->buf = sun;
726ce0e08e2SPeter Avalos break;
727ce0e08e2SPeter Avalos default:
728ce0e08e2SPeter Avalos break;
729ce0e08e2SPeter Avalos }
730ce0e08e2SPeter Avalos out:
731ce0e08e2SPeter Avalos free(addrstr);
732ce0e08e2SPeter Avalos return ret;
733ce0e08e2SPeter Avalos }
734ce0e08e2SPeter Avalos
735ce0e08e2SPeter Avalos int
__rpc_seman2socktype(int semantics)736ce0e08e2SPeter Avalos __rpc_seman2socktype(int semantics)
737ce0e08e2SPeter Avalos {
738ce0e08e2SPeter Avalos switch (semantics) {
739ce0e08e2SPeter Avalos case NC_TPI_CLTS:
740ce0e08e2SPeter Avalos return SOCK_DGRAM;
741ce0e08e2SPeter Avalos case NC_TPI_COTS_ORD:
742ce0e08e2SPeter Avalos return SOCK_STREAM;
743ce0e08e2SPeter Avalos case NC_TPI_RAW:
744ce0e08e2SPeter Avalos return SOCK_RAW;
745ce0e08e2SPeter Avalos default:
746ce0e08e2SPeter Avalos break;
747ce0e08e2SPeter Avalos }
748ce0e08e2SPeter Avalos
749ce0e08e2SPeter Avalos return -1;
750ce0e08e2SPeter Avalos }
751ce0e08e2SPeter Avalos
752ce0e08e2SPeter Avalos int
__rpc_socktype2seman(int socktype)753ce0e08e2SPeter Avalos __rpc_socktype2seman(int socktype)
754ce0e08e2SPeter Avalos {
755ce0e08e2SPeter Avalos switch (socktype) {
756ce0e08e2SPeter Avalos case SOCK_DGRAM:
757ce0e08e2SPeter Avalos return NC_TPI_CLTS;
758ce0e08e2SPeter Avalos case SOCK_STREAM:
759ce0e08e2SPeter Avalos return NC_TPI_COTS_ORD;
760ce0e08e2SPeter Avalos case SOCK_RAW:
761ce0e08e2SPeter Avalos return NC_TPI_RAW;
762ce0e08e2SPeter Avalos default:
763ce0e08e2SPeter Avalos break;
764ce0e08e2SPeter Avalos }
765ce0e08e2SPeter Avalos
766ce0e08e2SPeter Avalos return -1;
767ce0e08e2SPeter Avalos }
768ce0e08e2SPeter Avalos
769ce0e08e2SPeter Avalos /*
770ce0e08e2SPeter Avalos * XXXX - IPv6 scope IDs can't be handled in universal addresses.
771ce0e08e2SPeter Avalos * Here, we compare the original server address to that of the RPC
772ce0e08e2SPeter Avalos * service we just received back from a call to rpcbind on the remote
773ce0e08e2SPeter Avalos * machine. If they are both "link local" or "site local", copy
774ce0e08e2SPeter Avalos * the scope id of the server address over to the service address.
775ce0e08e2SPeter Avalos */
776ce0e08e2SPeter Avalos int
__rpc_fixup_addr(struct netbuf * new,const struct netbuf * svc)777ce0e08e2SPeter Avalos __rpc_fixup_addr(struct netbuf *new, const struct netbuf *svc)
778ce0e08e2SPeter Avalos {
779ce0e08e2SPeter Avalos #ifdef INET6
780ce0e08e2SPeter Avalos struct sockaddr *sa_new, *sa_svc;
781ce0e08e2SPeter Avalos struct sockaddr_in6 *sin6_new, *sin6_svc;
782ce0e08e2SPeter Avalos
783ce0e08e2SPeter Avalos sa_svc = (struct sockaddr *)svc->buf;
784ce0e08e2SPeter Avalos sa_new = (struct sockaddr *)new->buf;
785ce0e08e2SPeter Avalos
786ce0e08e2SPeter Avalos if (sa_new->sa_family == sa_svc->sa_family &&
787ce0e08e2SPeter Avalos sa_new->sa_family == AF_INET6) {
788ce0e08e2SPeter Avalos sin6_new = (struct sockaddr_in6 *)new->buf;
789ce0e08e2SPeter Avalos sin6_svc = (struct sockaddr_in6 *)svc->buf;
790ce0e08e2SPeter Avalos
791ce0e08e2SPeter Avalos if ((IN6_IS_ADDR_LINKLOCAL(&sin6_new->sin6_addr) &&
792ce0e08e2SPeter Avalos IN6_IS_ADDR_LINKLOCAL(&sin6_svc->sin6_addr)) ||
793ce0e08e2SPeter Avalos (IN6_IS_ADDR_SITELOCAL(&sin6_new->sin6_addr) &&
794ce0e08e2SPeter Avalos IN6_IS_ADDR_SITELOCAL(&sin6_svc->sin6_addr))) {
795ce0e08e2SPeter Avalos sin6_new->sin6_scope_id = sin6_svc->sin6_scope_id;
796ce0e08e2SPeter Avalos }
797ce0e08e2SPeter Avalos }
798ce0e08e2SPeter Avalos #endif
799ce0e08e2SPeter Avalos return 1;
800ce0e08e2SPeter Avalos }
801ce0e08e2SPeter Avalos
802ce0e08e2SPeter Avalos int
__rpc_sockisbound(int fd)803ce0e08e2SPeter Avalos __rpc_sockisbound(int fd)
804ce0e08e2SPeter Avalos {
805ce0e08e2SPeter Avalos struct sockaddr_storage ss;
806ce0e08e2SPeter Avalos socklen_t slen;
807ce0e08e2SPeter Avalos
808ce0e08e2SPeter Avalos slen = sizeof (struct sockaddr_storage);
809ce0e08e2SPeter Avalos if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0)
810ce0e08e2SPeter Avalos return 0;
811ce0e08e2SPeter Avalos
812ce0e08e2SPeter Avalos switch (ss.ss_family) {
813ce0e08e2SPeter Avalos case AF_INET:
814ce0e08e2SPeter Avalos return (((struct sockaddr_in *)
815ce0e08e2SPeter Avalos (void *)&ss)->sin_port != 0);
816ce0e08e2SPeter Avalos #ifdef INET6
817ce0e08e2SPeter Avalos case AF_INET6:
818ce0e08e2SPeter Avalos return (((struct sockaddr_in6 *)
819ce0e08e2SPeter Avalos (void *)&ss)->sin6_port != 0);
820ce0e08e2SPeter Avalos #endif
821ce0e08e2SPeter Avalos case AF_LOCAL:
822ce0e08e2SPeter Avalos /* XXX check this */
823ce0e08e2SPeter Avalos return (((struct sockaddr_un *)
824ce0e08e2SPeter Avalos (void *)&ss)->sun_path[0] != '\0');
825ce0e08e2SPeter Avalos default:
826ce0e08e2SPeter Avalos break;
827ce0e08e2SPeter Avalos }
828ce0e08e2SPeter Avalos
829ce0e08e2SPeter Avalos return 0;
830ce0e08e2SPeter Avalos }
831