1 /*
2  * Channel management functions.
3  *
4  * Copyright 2000-2014 Willy Tarreau <w@1wt.eu>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  *
11  */
12 
13 #include <ctype.h>
14 #include <stdarg.h>
15 #include <stdio.h>
16 #include <string.h>
17 
18 #include <common/config.h>
19 #include <common/buffer.h>
20 
21 #include <proto/channel.h>
22 
23 
24 /* Schedule up to <bytes> more bytes to be forwarded via the channel without
25  * notifying the owner task. Any data pending in the buffer are scheduled to be
26  * sent as well, within the limit of the number of bytes to forward. This must
27  * be the only method to use to schedule bytes to be forwarded. If the requested
28  * number is too large, it is automatically adjusted. The number of bytes taken
29  * into account is returned. Directly touching ->to_forward will cause lockups
30  * when buf->o goes down to zero if nobody is ready to push the remaining data.
31  */
__channel_forward(struct channel * chn,unsigned long long bytes)32 unsigned long long __channel_forward(struct channel *chn, unsigned long long bytes)
33 {
34 	unsigned int budget;
35 	unsigned int forwarded;
36 
37 	/* This is more of a safety measure as it's not supposed to happen in
38 	 * regular code paths.
39 	 */
40 	if (unlikely(chn->to_forward == CHN_INFINITE_FORWARD)) {
41 		b_adv(chn->buf, chn->buf->i);
42 		return bytes;
43 	}
44 
45 	/* Bound the transferred size to a 32-bit count since all our values
46 	 * are 32-bit, and we don't want to reach CHN_INFINITE_FORWARD.
47 	 */
48 	budget = MIN(bytes, CHN_INFINITE_FORWARD - 1);
49 
50 	/* transfer as much as we can of buf->i */
51 	forwarded = MIN(chn->buf->i, budget);
52 	b_adv(chn->buf, forwarded);
53 	budget -= forwarded;
54 
55 	if (!budget)
56 		return forwarded;
57 
58 	/* Now we must ensure chn->to_forward sats below CHN_INFINITE_FORWARD,
59 	 * which also implies it won't overflow. It's less operations in 64-bit.
60 	 */
61 	bytes = (unsigned long long)chn->to_forward + budget;
62 	if (bytes >= CHN_INFINITE_FORWARD)
63 		bytes = CHN_INFINITE_FORWARD - 1;
64 	budget = bytes - chn->to_forward;
65 
66 	chn->to_forward += budget;
67 	forwarded += budget;
68 	return forwarded;
69 }
70 
71 /* writes <len> bytes from message <msg> to the channel's buffer. Returns -1 in
72  * case of success, -2 if the message is larger than the buffer size, or the
73  * number of bytes available otherwise. The send limit is automatically
74  * adjusted to the amount of data written. FIXME-20060521: handle unaligned
75  * data. Note: this function appends data to the buffer's output and possibly
76  * overwrites any pending input data which are assumed not to exist.
77  */
bo_inject(struct channel * chn,const char * msg,int len)78 int bo_inject(struct channel *chn, const char *msg, int len)
79 {
80 	int max;
81 
82 	if (len == 0)
83 		return -1;
84 
85 	if (len < 0 || len > chn->buf->size) {
86 		/* we can't write this chunk and will never be able to, because
87 		 * it is larger than the buffer. This must be reported as an
88 		 * error. Then we return -2 so that writers that don't care can
89 		 * ignore it and go on, and others can check for this value.
90 		 */
91 		return -2;
92 	}
93 
94 	buffer_realign(chn->buf);
95 	max = bo_contig_space(chn->buf);
96 	if (len > max)
97 		return max;
98 
99 	memcpy(chn->buf->p, msg, len);
100 	chn->buf->o += len;
101 	chn->buf->p = b_ptr(chn->buf, len);
102 	chn->total += len;
103 	return -1;
104 }
105 
106 /* Tries to copy character <c> into the channel's buffer after some length
107  * controls. The chn->o and to_forward pointers are updated. If the channel
108  * input is closed, -2 is returned. If there is not enough room left in the
109  * buffer, -1 is returned. Otherwise the number of bytes copied is returned
110  * (1). Channel flag READ_PARTIAL is updated if some data can be transferred.
111  */
bi_putchr(struct channel * chn,char c)112 int bi_putchr(struct channel *chn, char c)
113 {
114 	if (unlikely(channel_input_closed(chn)))
115 		return -2;
116 
117 	if (!channel_may_recv(chn))
118 		return -1;
119 
120 	*bi_end(chn->buf) = c;
121 
122 	chn->buf->i++;
123 	chn->flags |= CF_READ_PARTIAL;
124 
125 	if (chn->to_forward >= 1) {
126 		if (chn->to_forward != CHN_INFINITE_FORWARD)
127 			chn->to_forward--;
128 		b_adv(chn->buf, 1);
129 	}
130 
131 	chn->total++;
132 	return 1;
133 }
134 
135 /* Tries to copy block <blk> at once into the channel's buffer after length
136  * controls. The chn->o and to_forward pointers are updated. If the channel
137  * input is closed, -2 is returned. If the block is too large for this buffer,
138  * -3 is returned. If there is not enough room left in the buffer, -1 is
139  * returned. Otherwise the number of bytes copied is returned (0 being a valid
140  * number). Channel flag READ_PARTIAL is updated if some data can be
141  * transferred.
142  */
bi_putblk(struct channel * chn,const char * blk,int len)143 int bi_putblk(struct channel *chn, const char *blk, int len)
144 {
145 	int max;
146 
147 	if (unlikely(channel_input_closed(chn)))
148 		return -2;
149 
150 	if (len < 0)
151 		return -3;
152 
153 	max = channel_recv_limit(chn);
154 	if (unlikely(len > max - buffer_len(chn->buf))) {
155 		/* we can't write this chunk right now because the buffer is
156 		 * almost full or because the block is too large. Returns
157 		 * -3 if block is too large for this buffer. Or -1 if the
158 		 * room left is not large enough.
159 		 */
160 		if (len > max)
161 			return -3;
162 
163 		return -1;
164 	}
165 
166 	if (unlikely(len == 0))
167 		return 0;
168 
169 	/* OK so the data fits in the buffer in one or two blocks */
170 	max = bi_contig_space(chn->buf);
171 	memcpy(bi_end(chn->buf), blk, MIN(len, max));
172 	if (len > max)
173 		memcpy(chn->buf->data, blk + max, len - max);
174 
175 	chn->buf->i += len;
176 	chn->total += len;
177 	if (chn->to_forward) {
178 		unsigned long fwd = len;
179 		if (chn->to_forward != CHN_INFINITE_FORWARD) {
180 			if (fwd > chn->to_forward)
181 				fwd = chn->to_forward;
182 			chn->to_forward -= fwd;
183 		}
184 		b_adv(chn->buf, fwd);
185 	}
186 
187 	/* notify that some data was read from the SI into the buffer */
188 	chn->flags |= CF_READ_PARTIAL;
189 	return len;
190 }
191 
192 /* Tries to copy the whole buffer <buf> into the channel's buffer after length
193  * controls. It will only succeed if the target buffer is empty, in which case
194  * it will simply swap the buffers. The buffer not attached to the channel is
195  * returned so that the caller can store it locally. The chn->buf->o and
196  * to_forward pointers are updated. If the output buffer is a dummy buffer or
197  * if it still contains data <buf> is returned, indicating that nothing could
198  * be done. Channel flag READ_PARTIAL is updated if some data can be transferred.
199  * The chunk's length is updated with the number of bytes sent. On errors, NULL
200  * is returned. Note that only buf->i is considered.
201  */
bi_swpbuf(struct channel * chn,struct buffer * buf)202 struct buffer *bi_swpbuf(struct channel *chn, struct buffer *buf)
203 {
204 	struct buffer *old;
205 
206 	if (unlikely(channel_input_closed(chn)))
207 		return NULL;
208 
209 	if (!chn->buf->size || !buffer_empty(chn->buf))
210 		return buf;
211 
212 	old = chn->buf;
213 	chn->buf = buf;
214 
215 	if (!buf->i)
216 		return old;
217 
218 	chn->total += buf->i;
219 
220 	if (chn->to_forward) {
221 		unsigned long fwd = buf->i;
222 		if (chn->to_forward != CHN_INFINITE_FORWARD) {
223 			if (fwd > chn->to_forward)
224 				fwd = chn->to_forward;
225 			chn->to_forward -= fwd;
226 		}
227 		b_adv(chn->buf, fwd);
228 	}
229 
230 	/* notify that some data was read from the SI into the buffer */
231 	chn->flags |= CF_READ_PARTIAL;
232 	return old;
233 }
234 
235 /* Gets one text line out of a channel's buffer from a stream interface.
236  * Return values :
237  *   >0 : number of bytes read. Includes the \n if present before len or end.
238  *   =0 : no '\n' before end found. <str> is left undefined.
239  *   <0 : no more bytes readable because output is shut.
240  * The channel status is not changed. The caller must call bo_skip() to
241  * update it. The '\n' is waited for as long as neither the buffer nor the
242  * output are full. If either of them is full, the string may be returned
243  * as is, without the '\n'.
244  */
bo_getline(struct channel * chn,char * str,int len)245 int bo_getline(struct channel *chn, char *str, int len)
246 {
247 	int ret, max;
248 	char *p;
249 
250 	ret = 0;
251 	max = len;
252 
253 	/* closed or empty + imminent close = -1; empty = 0 */
254 	if (unlikely((chn->flags & CF_SHUTW) || channel_is_empty(chn))) {
255 		if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW))
256 			ret = -1;
257 		goto out;
258 	}
259 
260 	p = bo_ptr(chn->buf);
261 
262 	if (max > chn->buf->o) {
263 		max = chn->buf->o;
264 		str[max-1] = 0;
265 	}
266 	while (max) {
267 		*str++ = *p;
268 		ret++;
269 		max--;
270 
271 		if (*p == '\n')
272 			break;
273 		p = buffer_wrap_add(chn->buf, p + 1);
274 	}
275 	if (ret > 0 && ret < len &&
276 	    (ret < chn->buf->o || channel_may_recv(chn)) &&
277 	    *(str-1) != '\n' &&
278 	    !(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
279 		ret = 0;
280  out:
281 	if (max)
282 		*str = 0;
283 	return ret;
284 }
285 
286 /* Gets one full block of data at once from a channel's buffer, optionally from
287  * a specific offset. Return values :
288  *   >0 : number of bytes read, equal to requested size.
289  *   =0 : not enough data available. <blk> is left undefined.
290  *   <0 : no more bytes readable because output is shut.
291  * The channel status is not changed. The caller must call bo_skip() to
292  * update it.
293  */
bo_getblk(struct channel * chn,char * blk,int len,int offset)294 int bo_getblk(struct channel *chn, char *blk, int len, int offset)
295 {
296 	int firstblock;
297 
298 	if (chn->flags & CF_SHUTW)
299 		return -1;
300 
301 	if (len + offset > chn->buf->o) {
302 		if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW))
303 			return -1;
304 		return 0;
305 	}
306 
307 	firstblock = chn->buf->data + chn->buf->size - bo_ptr(chn->buf);
308 	if (firstblock > offset) {
309 		if (firstblock >= len + offset) {
310 			memcpy(blk, bo_ptr(chn->buf) + offset, len);
311 			return len;
312 		}
313 
314 		memcpy(blk, bo_ptr(chn->buf) + offset, firstblock - offset);
315 		memcpy(blk + firstblock - offset, chn->buf->data, len - firstblock + offset);
316 		return len;
317 	}
318 
319 	memcpy(blk, chn->buf->data + offset - firstblock, len);
320 	return len;
321 }
322 
323 /* Gets one or two blocks of data at once from a channel's output buffer.
324  * Return values :
325  *   >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
326  *   =0 : not enough data available. <blk*> are left undefined.
327  *   <0 : no more bytes readable because output is shut.
328  * The channel status is not changed. The caller must call bo_skip() to
329  * update it. Unused buffers are left in an undefined state.
330  */
bo_getblk_nc(struct channel * chn,char ** blk1,int * len1,char ** blk2,int * len2)331 int bo_getblk_nc(struct channel *chn, char **blk1, int *len1, char **blk2, int *len2)
332 {
333 	if (unlikely(chn->buf->o == 0)) {
334 		if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW))
335 			return -1;
336 		return 0;
337 	}
338 
339 	if (unlikely(chn->buf->p - chn->buf->o < chn->buf->data)) {
340 		*blk1 = chn->buf->p - chn->buf->o + chn->buf->size;
341 		*len1 = chn->buf->data + chn->buf->size - *blk1;
342 		*blk2 = chn->buf->data;
343 		*len2 = chn->buf->p - chn->buf->data;
344 		return 2;
345 	}
346 
347 	*blk1 = chn->buf->p - chn->buf->o;
348 	*len1 = chn->buf->o;
349 	return 1;
350 }
351 
352 /* Gets one text line out of a channel's output buffer from a stream interface.
353  * Return values :
354  *   >0 : number of blocks returned (1 or 2). blk1 is always filled before blk2.
355  *   =0 : not enough data available.
356  *   <0 : no more bytes readable because output is shut.
357  * The '\n' is waited for as long as neither the buffer nor the output are
358  * full. If either of them is full, the string may be returned as is, without
359  * the '\n'. Unused buffers are left in an undefined state.
360  */
bo_getline_nc(struct channel * chn,char ** blk1,int * len1,char ** blk2,int * len2)361 int bo_getline_nc(struct channel *chn,
362                   char **blk1, int *len1,
363                   char **blk2, int *len2)
364 {
365 	int retcode;
366 	int l;
367 
368 	retcode = bo_getblk_nc(chn, blk1, len1, blk2, len2);
369 	if (unlikely(retcode <= 0))
370 		return retcode;
371 
372 	for (l = 0; l < *len1 && (*blk1)[l] != '\n'; l++);
373 	if (l < *len1 && (*blk1)[l] == '\n') {
374 		*len1 = l + 1;
375 		return 1;
376 	}
377 
378 	if (retcode >= 2) {
379 		for (l = 0; l < *len2 && (*blk2)[l] != '\n'; l++);
380 		if (l < *len2 && (*blk2)[l] == '\n') {
381 			*len2 = l + 1;
382 			return 2;
383 		}
384 	}
385 
386 	if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
387 		/* If we have found no LF and the buffer is shut, then
388 		 * the resulting string is made of the concatenation of
389 		 * the pending blocks (1 or 2).
390 		 */
391 		return retcode;
392 	}
393 
394 	/* No LF yet and not shut yet */
395 	return 0;
396 }
397 
398 /* Gets one full block of data at once from a channel's input buffer.
399  * This function can return the data slitted in one or two blocks.
400  * Return values :
401  *   >0 : number of blocks returned (1 or 2). blk1 is always filled before blk2.
402  *   =0 : not enough data available.
403  *   <0 : no more bytes readable because input is shut.
404  */
bi_getblk_nc(struct channel * chn,char ** blk1,int * len1,char ** blk2,int * len2)405 int bi_getblk_nc(struct channel *chn,
406                  char **blk1, int *len1,
407                  char **blk2, int *len2)
408 {
409 	if (unlikely(chn->buf->i == 0)) {
410 		if (chn->flags & CF_SHUTR)
411 			return -1;
412 		return 0;
413 	}
414 
415 	if (unlikely(chn->buf->p + chn->buf->i > chn->buf->data + chn->buf->size)) {
416 		*blk1 = chn->buf->p;
417 		*len1 = chn->buf->data + chn->buf->size - chn->buf->p;
418 		*blk2 = chn->buf->data;
419 		*len2 = chn->buf->i - *len1;
420 		return 2;
421 	}
422 
423 	*blk1 = chn->buf->p;
424 	*len1 = chn->buf->i;
425 	return 1;
426 }
427 
428 /* Gets one text line out of a channel's input buffer from a stream interface.
429  * Return values :
430  *   >0 : number of blocks returned (1 or 2). blk1 is always filled before blk2.
431  *   =0 : not enough data available.
432  *   <0 : no more bytes readable because output is shut.
433  * The '\n' is waited for as long as neither the buffer nor the input are
434  * full. If either of them is full, the string may be returned as is, without
435  * the '\n'. Unused buffers are left in an undefined state.
436  */
bi_getline_nc(struct channel * chn,char ** blk1,int * len1,char ** blk2,int * len2)437 int bi_getline_nc(struct channel *chn,
438                   char **blk1, int *len1,
439                   char **blk2, int *len2)
440 {
441 	int retcode;
442 	int l;
443 
444 	retcode = bi_getblk_nc(chn, blk1, len1, blk2, len2);
445 	if (unlikely(retcode <= 0))
446 		return retcode;
447 
448 	for (l = 0; l < *len1 && (*blk1)[l] != '\n'; l++);
449 	if (l < *len1 && (*blk1)[l] == '\n') {
450 		*len1 = l + 1;
451 		return 1;
452 	}
453 
454 	if (retcode >= 2) {
455 		for (l = 0; l < *len2 && (*blk2)[l] != '\n'; l++);
456 		if (l < *len2 && (*blk2)[l] == '\n') {
457 			*len2 = l + 1;
458 			return 2;
459 		}
460 	}
461 
462 	if (chn->flags & CF_SHUTW) {
463 		/* If we have found no LF and the buffer is shut, then
464 		 * the resulting string is made of the concatenation of
465 		 * the pending blocks (1 or 2).
466 		 */
467 		return retcode;
468 	}
469 
470 	/* No LF yet and not shut yet */
471 	return 0;
472 }
473 
474 /*
475  * Local variables:
476  *  c-indent-level: 8
477  *  c-basic-offset: 8
478  * End:
479  */
480