xref: /illumos-gate/usr/src/lib/libnsl/yp/yp_rsvd.c (revision 9164a50bf932130cbb5097a16f6986873ce0e6e5)
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 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #include "mt.h"
29 #include <sys/types.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <netconfig.h>
33 #include <netdir.h>
34 #include <rpc/rpc.h>
35 
36 static CLIENT *
37 __yp_clnt_create_rsvdport_netid_req(const char *hostname, rpcprog_t prog,
38 			rpcvers_t vers, const char *nettype,
39 			const uint_t sendsz, const uint_t recvsz)
40 {
41 	struct netconfig *nconf;
42 	struct netbuf *svcaddr;
43 	struct t_bind *tbind;
44 	CLIENT *clnt = NULL;
45 	int fd;
46 	const char *nt;
47 
48 	if (nettype == NULL)
49 		return (0);
50 	else
51 		nt = nettype;
52 
53 	if (strcmp(nt, "udp") && strcmp(nt, "tcp") &&
54 		strcmp(nt, "udp6") && strcmp(nt, "tcp6"))
55 		return (clnt_create(hostname, prog, vers, nt));
56 
57 	if ((nconf = getnetconfigent((void *) nt)) == NULL)
58 		return (NULL);
59 
60 	if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) == -1) {
61 		freenetconfigent(nconf);
62 		return (NULL);
63 	}
64 
65 	/* Attempt to set reserved port, but we don't care if it fails */
66 	(void) netdir_options(nconf, ND_SET_RESERVEDPORT, fd, NULL);
67 
68 	/* LINTED pointer cast */
69 	if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR)) == NULL) {
70 		freenetconfigent(nconf);
71 		return (NULL);
72 	}
73 
74 	svcaddr = &(tbind->addr);
75 
76 	if (!rpcb_getaddr(prog, vers, nconf, svcaddr, hostname)) {
77 		(void) t_close(fd);
78 		(void) t_free((char *)tbind, T_BIND);
79 		freenetconfigent(nconf);
80 		return (NULL);
81 	}
82 
83 	if ((clnt = clnt_tli_create(fd, nconf, svcaddr,
84 				prog, vers, sendsz, recvsz)) == NULL) {
85 		(void) t_close(fd);
86 		(void) t_free((char *)tbind, T_BIND);
87 	} else {
88 		(void) t_free((char *)tbind, T_BIND);
89 		clnt_control(clnt, CLSET_FD_CLOSE, NULL);
90 	}
91 	freenetconfigent(nconf);
92 	return (clnt);
93 }
94 
95 
96 CLIENT *
97 __yp_clnt_create_rsvdport(const char *hostname, rpcprog_t prog,
98 			rpcvers_t vers, const char *nettype,
99 			const uint_t sendsz, const uint_t recvsz)
100 {
101 	if (nettype == 0) {
102 		CLIENT *ret;
103 		ret = __yp_clnt_create_rsvdport_netid_req(hostname, prog,
104 					vers, "udp6", sendsz, recvsz);
105 		if (ret == 0)
106 			ret = __yp_clnt_create_rsvdport_netid_req(hostname,
107 					prog, vers, "udp", sendsz, recvsz);
108 		return (ret);
109 	} else {
110 		return (__yp_clnt_create_rsvdport_netid_req(hostname, prog,
111 							vers, nettype,
112 							sendsz, recvsz));
113 	}
114 }
115