xref: /illumos-gate/usr/src/lib/libnls/common/nlsrequest.c (revision 89b2a9fbeabf42fa54594df0e5927bcc50a07cc9)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 #ident	"%Z%%M%	%I%	%E% SMI"        /* SVr4.0 1.5.1.1	*/
27 
28 /*
29  *
30  * nlsrequest(3):
31  *
32  *		Send service request message to remote listener
33  *		on previously established virtual circuit to remote
34  *		listener process.
35  *
36  *		If an error occurrs, t_errno will contain an error code.
37  *
38  *		Setting the external integer "_nlslog" to any non-zero
39  *		value before calling nlsrequest,  will cause nlsrequest
40  *		to print debug information on stderr.
41  *
42  *		client/server process pairs should include their own
43  *		initial handshake to insure connectivity.
44  *
45  *		This version of nlsrequest includes the
46  *		service request response message.
47  */
48 
49 
50 #include	<stdio.h>
51 #include	<ctype.h>
52 #include	<fcntl.h>
53 #include	<errno.h>
54 #include	<string.h>
55 #include	<stdlib.h>
56 #include	<sys/tiuser.h>
57 #include	"listen.h"
58 
59 extern	int _nlslog;		/* non-zero allows use of stderr	*/
60 char *_nlsrmsg = (char *)0;
61 static char _nlsbuf[256];
62 
63 
64 int
65 nlsrequest(int fd, char *svc_code)
66 {
67 	int	len, err, flags;
68 	char 	buf[256];
69 	char	*p;
70 	int	version, ret;
71 	extern  int t_errno;
72 
73 	t_errno = 0;		/* indicates a 'name' problem	*/
74 	buf[0] = 0;
75 
76 	/*
77 	 * Validate service code
78 	 */
79 
80 	if (!svc_code || !strlen(svc_code) ||
81 	    (strlen(svc_code) >= (size_t)SVC_CODE_SZ)) {
82 		if (_nlslog)
83 			fprintf(stderr, "nlsrequest: invalid service code format\n");
84 		return(-1);
85 	}
86 
87 	/*
88 	 * send protocol message requesting the service
89 	 */
90 
91 	len = sprintf(buf, nls_v2_msg, svc_code)+1;/* inc trailing null */
92 
93 	if (t_snd(fd, buf, len, 0) < len) {
94 		if (_nlslog)
95 			t_error("t_snd of listener request message failed");
96 		return(-1);
97 	}
98 
99 	p = _nlsbuf;
100 	len = 0;
101 
102 	do {
103 		if (++len > sizeof(_nlsbuf)) {
104 			if (_nlslog)
105 				fprintf(stderr, "nlsrequest: _nlsbuf not large enough\n");
106 			return(-1);
107 		}
108 		if (t_rcv(fd, p, sizeof(char), &flags) != sizeof(char)) {
109 			if (_nlslog)
110 				t_error("t_rcv of listener response msg failed");
111 			return(-1);
112 		}
113 
114 	} while (*p++ != '\0');
115 
116 
117 	if ((p = strtok(_nlsbuf, ":")) == (char *)0)
118 		goto parsefail;
119 	version = atoi(p);
120 
121 	if ((p = strtok((char *)0, ":")) == (char *)0)
122 		goto parsefail;
123 	ret = atoi(p);
124 	_nlsrmsg = p + strlen(p) + 1;
125 	if (ret && _nlslog)
126 		fprintf(stderr, "%s\n", _nlsrmsg); /* debug only */
127 	return(ret);
128 
129 parsefail:
130 	if (_nlslog)
131 		fprintf(stderr, "nlsrequest: failed parse of response message\n");
132 	return(-1);
133 }
134