mux-parse.c 7.03 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
2
 * $Id: mux-parse.c,v 0.18 1996/07/24 23:57:24 ceder Exp $
3
 * Copyright (C) 1991, 1993, 1994, 1995  Lysator Academic Computer Association.
Linus Tolke Y's avatar
Linus Tolke Y committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 1, or (at your option) 
 * any later version.
 * 
 * LysKOM is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */
Per Cederqvist's avatar
Per Cederqvist committed
25
26
27
28
/*
 ** mux-parse.c                                    Handle the MUX protocol
 * This is a hack. Clean this mess up with lacgen.
 */
Per Cederqvist's avatar
Per Cederqvist committed
29

30
static char *rcsid = "$Id: mux-parse.c,v 0.18 1996/07/24 23:57:24 ceder Exp $";
31
32
#include "rcs.h"
USE(rcsid);
Per Cederqvist's avatar
Per Cederqvist committed
33

Per Cederqvist's avatar
Per Cederqvist committed
34
35
#include <stdio.h>
#include <setjmp.h>
Per Cederqvist's avatar
Per Cederqvist committed
36
#include <sys/types.h>
37
38
39
40
41
#include <time.h>
#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#endif
#include <sys/socket.h>
Per Cederqvist's avatar
Per Cederqvist committed
42

43
#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
44
#include "kom-types.h"
Per Cederqvist's avatar
Per Cederqvist committed
45
#include "com.h"
46
#include "async.h"
Per Cederqvist's avatar
Per Cederqvist committed
47
#include "connections.h"
48
#include "isc-interface.h"
Per Cederqvist's avatar
Per Cederqvist committed
49
#include "config.h"
50
#include "log.h"
Per Cederqvist's avatar
Per Cederqvist committed
51
52
53
#include "minmax.h"
#include "mux.h"
#include "mux-parse.h"
Per Cederqvist's avatar
Per Cederqvist committed
54
55
56
57
58
59
60
61
62
63
64
65

jmp_buf mux_parse_env;

static String
mux_get_token(Mux *mux)
{
    String result;
    String_size old_first;

    old_first = mux->parse.first_to_parse;
    
    result = s_strtok(mux->parse.unparsed, &mux->parse.first_to_parse,
66
		      s_fcrea_str(WHITESPACE));
Per Cederqvist's avatar
Per Cederqvist committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

    /* Check that there was at least one trailing blank. */
    
    if ( mux->parse.first_to_parse >= s_strlen(mux->parse.unparsed) )
    {
	mux->parse.first_to_parse = old_first;
	longjmp(mux_parse_env, MUX_MSG_INCOMPLETE);
    }

    return result;
}

static long
mux_parse_long(Mux *mux)
{
    String	token;
    String_size end;
    long	res;

    token = mux_get_token(mux);
    res = s_strtol(token, &end, PROTOCOL_NUMBER_BASE);
    if (end != s_strlen(token))
	longjmp(mux_parse_env, MUX_PROTOCOL_ERR);
    
    return res;
}


/*
 * Parse a string. At most 'maxlen' characters are allowed. If the
 * mux sends a longer string only the first 'maxlen+1' characters
98
99
100
101
102
 * are read. Any remaining characters are discarded.
 *
 * longjmp if not enough characters are available or if there
 * is an error. Result is used to save state in that case.
 * +++ Result should not be used to save state.
Per Cederqvist's avatar
Per Cederqvist committed
103
104
105
106
107
108
109
110
111
112
 */
static void
mux_parse_string(Mux	    *mux,
		 String	    *result,
		 int	     maxlen)
{
    String_size hptr;		/* Pointer to 'H' */
    String_size mux_len;	/* The len the mux is sending. */
    String_size truncated_len;	/* How much the server will receive. */
    String_size to_skip;
113
    static u_long err_cnt = 0;
Per Cederqvist's avatar
Per Cederqvist committed
114
115
116
117

    switch ( mux->parse.string_parse_pos )
    {
    case 0:
118
119
	if ( ( result->len != 0 || result->string != NULL) && err_cnt++ < 20 )
	{
Per Cederqvist's avatar
Per Cederqvist committed
120
121
122
123
	    log ("%s == %lu, result->string == %lu. %s.\n",
		 "mux_parse_string(): result->len",
		 (u_long)result->len, (u_long)result->string,
		 "This memory will not be free()'d");
124
125
126
127
128
	    *result = EMPTY_STRING;
	    if ( err_cnt == 20 )
		log("Won't log the above warning no more.");
	}
	
Per Cederqvist's avatar
Per Cederqvist committed
129
	/* Get number and discard trailing 'H' */
130
131
132
133
	mux_len = s_strtol(s_fsubstr(mux->parse.unparsed,
				     mux->parse.first_to_parse,
				     END_OF_STRING),
			   &hptr, PROTOCOL_NUMBER_BASE);
Per Cederqvist's avatar
Per Cederqvist committed
134
135
136
137
138
139
140

	if ( hptr == -1
	    || mux->parse.first_to_parse + hptr
	    >= s_strlen(mux->parse.unparsed) )
	{
	    longjmp(mux_parse_env, MUX_MSG_INCOMPLETE);
	}
141
142
143
144
145
146

	if (mux_len < 0)
	{
	    log("Insane string length from mux.  Logging out mux.\n");
	    longjmp(mux_parse_env, MUX_LOGOUT);
	}
Per Cederqvist's avatar
Per Cederqvist committed
147
148
149
150
151
	
	/* Check that
	      a) there is a trailing H
	      b) there was at least one digit before the H */

152
153
	/* +++ Use s-string.c to extract the char. */
	/* +++ Fix prot_a_parse_string also. */
Per Cederqvist's avatar
Per Cederqvist committed
154
155
156
157
158
159
160
161
162
	if ( mux->parse.unparsed.string[ mux->parse.first_to_parse
					  + hptr ] != 'H'
	    || hptr <= 0 )
	{
	    longjmp(mux_parse_env, MUX_PROTOCOL_ERR);
	}

	mux->parse.first_to_parse += 1 + hptr;
	mux->parse.string_parse_pos = 1;
163
	result->len = mux_len;	/* +++ Transfer mux_len. */
Per Cederqvist's avatar
Per Cederqvist committed
164
165
	/* Fall through */
    case 1:
166
	mux_len = result->len;
Per Cederqvist's avatar
Per Cederqvist committed
167
168
169
	/* Check that the entire string is transmitted. */
	/* (Don't care about the trailing part that will be skipped if the
	 *  string is longer than maxlen) */
170
	truncated_len = min(maxlen + 1, mux_len);
Per Cederqvist's avatar
Per Cederqvist committed
171
172
173
174
175
176
177
	
	if ( mux->parse.first_to_parse + truncated_len
	    > s_strlen(mux->parse.unparsed) )
	{
	    longjmp(mux_parse_env, MUX_MSG_INCOMPLETE);
	}

178
179
	*result = EMPTY_STRING;

180
181
182
183
	s_mem_crea_str(result,
		       mux->parse.unparsed.string + mux->parse.first_to_parse,
		       truncated_len);
	
Per Cederqvist's avatar
Per Cederqvist committed
184
185
	mux->parse.first_to_parse += truncated_len;
	mux->parse.string_parse_pos = 2;
186
187
188
189
190
	result->len = mux_len;	/* Can't transfer the local mux_len across
				 * call boundary. +++ There should be room
				 * for mux_len in the per-connection
				 * protocol data.
				 */
Per Cederqvist's avatar
Per Cederqvist committed
191
192
193
194
	/* Fall through */
    case 2:
	/* Was the string too long? If so, skip the truncated data. */

195
	mux_len = result->len;	/* +++ shouldn't modify ->len */
196
	truncated_len = min(maxlen+1, mux_len);
Per Cederqvist's avatar
Per Cederqvist committed
197
198
199
200
	
	if ( mux_len > truncated_len )
	{
	    to_skip = min(mux_len - truncated_len,
201
202
203
204
			  mux->parse.unparsed.len - mux->parse.first_to_parse);
	    /* From now on, mux_len is length of result + numer of
	       chars to skip. Clean up this! +++
	     */
Per Cederqvist's avatar
Per Cederqvist committed
205
206
207
	    mux_len -= to_skip;
	    mux->parse.first_to_parse += to_skip;
	}
208
 
Per Cederqvist's avatar
Per Cederqvist committed
209
210
211
	result->len = mux_len;
	
	if ( mux_len > truncated_len )
212
213
	{
	    /* +++ mux_len is transferred in result->len. */
Per Cederqvist's avatar
Per Cederqvist committed
214
	    longjmp(mux_parse_env, MUX_MSG_INCOMPLETE);
215
216
	}
	
Per Cederqvist's avatar
Per Cederqvist committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
	/* Fall through */
    default:
	mux->parse.string_parse_pos = 0;
    }
}





static Bool
mux_is_legal_fnc(int fnc)
{
    switch(fnc)
    {
    case 0:
    case 1:
    case 2:
    case 3:
	return TRUE;

    default:
	return FALSE;
    }
}    

static void
mux_parse_client_message(Mux *mux)
{
    switch(mux->parse.parse_pos_2)
    {
    case 0:
	mux->parse.num = mux_parse_long(mux);
	mux->parse.parse_pos_2 = 1;
	/* Fall through */
    case 1:
	mux_parse_string(mux, &mux->parse.string, 10000);
	/* Fall through */
    default:
	mux->parse.parse_pos_2 = 0;
    }
}

void
mux_parse_packet(Mux *mux)
{
    switch(mux->parse.parse_pos)
    {
    case 0:			/* Get fnc_no */
	mux->parse.function = mux_parse_long(mux);
	if ( !mux_is_legal_fnc(mux->parse.function) )
	    longjmp(mux_parse_env, MUX_PROTOCOL_ERR);
	mux->parse.parse_pos = 1;
	/* Fall through */
    case 1:
	/* Call the function that parses the arguments for this call. */
	switch(mux->parse.function)
	{
	case 0:
	    break;

	case 2:
	    mux->parse.num = mux_parse_long(mux);
	    break;

	case 1:
	case 3:
	    mux_parse_client_message(mux);
	    break;
	}
	/* Fall through */
    default:
	mux->parse.parse_pos = 0;
    }
}