xref: /illumos-gate/usr/src/lib/libnsl/yp/yp_rsvd.c (revision 61961e0f20c7637a3846bb39786bb9dffa91dfb9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <sys/types.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <netconfig.h>
34 #include <netdir.h>
35 #include <rpc/rpc.h>
36 
37 static CLIENT *
38 __yp_clnt_create_rsvdport_netid_req(const char *hostname, rpcprog_t prog,
39 			rpcvers_t vers, const char *nettype,
40 			const uint_t sendsz, const uint_t recvsz)
41 {
42 	struct netconfig *nconf;
43 	struct netbuf *svcaddr;
44 	struct t_bind *tbind;
45 	CLIENT *clnt = NULL;
46 	int fd;
47 	const char *nt;
48 
49 	if (nettype == NULL)
50 		return (0);
51 	else
52 		nt = nettype;
53 
54 	if (strcmp(nt, "udp") && strcmp(nt, "tcp") &&
55 		strcmp(nt, "udp6") && strcmp(nt, "tcp6"))
56 		return (clnt_create(hostname, prog, vers, nt));
57 
58 	if ((nconf = getnetconfigent((void *) nt)) == NULL)
59 		return (NULL);
60 
61 	if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) == -1) {
62 		freenetconfigent(nconf);
63 		return (NULL);
64 	}
65 
66 	/* Attempt to set reserved port, but we don't care if it fails */
67 	(void) netdir_options(nconf, ND_SET_RESERVEDPORT, fd, NULL);
68 
69 	/* LINTED pointer cast */
70 	if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR)) == NULL) {
71 		freenetconfigent(nconf);
72 		return (NULL);
73 	}
74 
75 	svcaddr = &(tbind->addr);
76 
77 	if (!rpcb_getaddr(prog, vers, nconf, svcaddr, hostname)) {
78 		(void) t_close(fd);
79 		(void) t_free((char *)tbind, T_BIND);
80 		freenetconfigent(nconf);
81 		return (NULL);
82 	}
83 
84 	if ((clnt = clnt_tli_create(fd, nconf, svcaddr,
85 				prog, vers, sendsz, recvsz)) == NULL) {
86 		(void) t_close(fd);
87 		(void) t_free((char *)tbind, T_BIND);
88 	} else {
89 		(void) t_free((char *)tbind, T_BIND);
90 		clnt_control(clnt, CLSET_FD_CLOSE, NULL);
91 	}
92 	freenetconfigent(nconf);
93 	return (clnt);
94 }
95 
96 
97 CLIENT *
98 __yp_clnt_create_rsvdport(const char *hostname, rpcprog_t prog,
99 			rpcvers_t vers, const char *nettype,
100 			const uint_t sendsz, const uint_t recvsz)
101 {
102 	if (nettype == 0) {
103 		CLIENT *ret;
104 		ret = __yp_clnt_create_rsvdport_netid_req(hostname, prog,
105 					vers, "udp6", sendsz, recvsz);
106 		if (ret == 0)
107 			ret = __yp_clnt_create_rsvdport_netid_req(hostname,
108 					prog, vers, "udp", sendsz, recvsz);
109 		return (ret);
110 	} else {
111 		return (__yp_clnt_create_rsvdport_netid_req(hostname, prog,
112 							vers, nettype,
113 							sendsz, recvsz));
114 	}
115 }
116