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