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