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