1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"        /* SVr4.0 1.5.1.1	*/
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate /*
29*0Sstevel@tonic-gate  *
30*0Sstevel@tonic-gate  * nlsrequest(3):
31*0Sstevel@tonic-gate  *
32*0Sstevel@tonic-gate  *		Send service request message to remote listener
33*0Sstevel@tonic-gate  *		on previously established virtual circuit to remote
34*0Sstevel@tonic-gate  *		listener process.
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  *		If an error occurrs, t_errno will contain an error code.
37*0Sstevel@tonic-gate  *
38*0Sstevel@tonic-gate  *		Setting the external integer "_nlslog" to any non-zero
39*0Sstevel@tonic-gate  *		value before calling nlsrequest,  will cause nlsrequest
40*0Sstevel@tonic-gate  *		to print debug information on stderr.
41*0Sstevel@tonic-gate  *
42*0Sstevel@tonic-gate  *		client/server process pairs should include their own
43*0Sstevel@tonic-gate  *		initial handshake to insure connectivity.
44*0Sstevel@tonic-gate  *
45*0Sstevel@tonic-gate  *		This version of nlsrequest includes the
46*0Sstevel@tonic-gate  *		service request response message.
47*0Sstevel@tonic-gate  */
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #include	<stdio.h>
51*0Sstevel@tonic-gate #include	<ctype.h>
52*0Sstevel@tonic-gate #include	<fcntl.h>
53*0Sstevel@tonic-gate #include	<errno.h>
54*0Sstevel@tonic-gate #include	<string.h>
55*0Sstevel@tonic-gate #include	<stdlib.h>
56*0Sstevel@tonic-gate #include	<sys/tiuser.h>
57*0Sstevel@tonic-gate #include	"listen.h"
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate extern	int _nlslog;		/* non-zero allows use of stderr	*/
60*0Sstevel@tonic-gate char *_nlsrmsg = (char *)0;
61*0Sstevel@tonic-gate static char _nlsbuf[256];
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate int
65*0Sstevel@tonic-gate nlsrequest(int fd, char *svc_code)
66*0Sstevel@tonic-gate {
67*0Sstevel@tonic-gate 	int	len, err, flags;
68*0Sstevel@tonic-gate 	char 	buf[256];
69*0Sstevel@tonic-gate 	char	*p;
70*0Sstevel@tonic-gate 	int	version, ret;
71*0Sstevel@tonic-gate 	extern  int t_errno;
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 	t_errno = 0;		/* indicates a 'name' problem	*/
74*0Sstevel@tonic-gate 	buf[0] = 0;
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	/*
77*0Sstevel@tonic-gate 	 * Validate service code
78*0Sstevel@tonic-gate 	 */
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	if (!svc_code || !strlen(svc_code) ||
81*0Sstevel@tonic-gate 	    (strlen(svc_code) >= (size_t)SVC_CODE_SZ)) {
82*0Sstevel@tonic-gate 		if (_nlslog)
83*0Sstevel@tonic-gate 			fprintf(stderr, "nlsrequest: invalid service code format\n");
84*0Sstevel@tonic-gate 		return(-1);
85*0Sstevel@tonic-gate 	}
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	/*
88*0Sstevel@tonic-gate 	 * send protocol message requesting the service
89*0Sstevel@tonic-gate 	 */
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	len = sprintf(buf, nls_v2_msg, svc_code)+1;/* inc trailing null */
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	if (t_snd(fd, buf, len, 0) < len) {
94*0Sstevel@tonic-gate 		if (_nlslog)
95*0Sstevel@tonic-gate 			t_error("t_snd of listener request message failed");
96*0Sstevel@tonic-gate 		return(-1);
97*0Sstevel@tonic-gate 	}
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	p = _nlsbuf;
100*0Sstevel@tonic-gate 	len = 0;
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	do {
103*0Sstevel@tonic-gate 		if (++len > sizeof(_nlsbuf)) {
104*0Sstevel@tonic-gate 			if (_nlslog)
105*0Sstevel@tonic-gate 				fprintf(stderr, "nlsrequest: _nlsbuf not large enough\n");
106*0Sstevel@tonic-gate 			return(-1);
107*0Sstevel@tonic-gate 		}
108*0Sstevel@tonic-gate 		if (t_rcv(fd, p, sizeof(char), &flags) != sizeof(char)) {
109*0Sstevel@tonic-gate 			if (_nlslog)
110*0Sstevel@tonic-gate 				t_error("t_rcv of listener response msg failed");
111*0Sstevel@tonic-gate 			return(-1);
112*0Sstevel@tonic-gate 		}
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	} while (*p++ != '\0');
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	if ((p = strtok(_nlsbuf, ":")) == (char *)0)
118*0Sstevel@tonic-gate 		goto parsefail;
119*0Sstevel@tonic-gate 	version = atoi(p);
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	if ((p = strtok((char *)0, ":")) == (char *)0)
122*0Sstevel@tonic-gate 		goto parsefail;
123*0Sstevel@tonic-gate 	ret = atoi(p);
124*0Sstevel@tonic-gate 	_nlsrmsg = p + strlen(p) + 1;
125*0Sstevel@tonic-gate 	if (ret && _nlslog)
126*0Sstevel@tonic-gate 		fprintf(stderr, "%s\n", _nlsrmsg); /* debug only */
127*0Sstevel@tonic-gate 	return(ret);
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate parsefail:
130*0Sstevel@tonic-gate 	if (_nlslog)
131*0Sstevel@tonic-gate 		fprintf(stderr, "nlsrequest: failed parse of response message\n");
132*0Sstevel@tonic-gate 	return(-1);
133*0Sstevel@tonic-gate }
134