1 /* $OpenBSD: getline.c,v 1.15 2003/06/28 01:04:57 deraadt Exp $ */
2 /* $DragonFly: src/libexec/ftp-proxy/getline.c,v 1.2 2005/02/24 15:38:09 joerg Exp $ */
5 * Copyright (c) 1985, 1988 Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)ftpcmd.y 5.24 (Berkeley) 2/25/91
35 #include <sys/types.h>
36 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/telnet.h>
51 int refill_buffer(struct csiob *iobp);
54 * Refill the io buffer if we KNOW that data is available
56 * Returns 1 if any new data was obtained, 0 otherwise.
60 refill_buffer(struct csiob *iobp)
64 if (!(iobp->data_available))
71 * The buffer has been entirely consumed if next_byte == io_buffer_len.
72 * Otherwise, there is some still-to-be-used data in io_buffer.
73 * Shuffle it to the start of the buffer.
74 * Note that next_byte will never exceed io_buffer_len.
75 * Also, note that we MUST use bcopy because the two regions could
76 * overlap (memcpy isn't defined to work properly with overlapping
79 if (iobp->next_byte < iobp->io_buffer_len) {
81 int src_ix = iobp->next_byte;
82 int amount = iobp->io_buffer_len - iobp->next_byte;
84 bcopy(&iobp->io_buffer[src_ix], &iobp->io_buffer[dst_ix],
86 iobp->io_buffer_len = amount;
87 } else if (iobp->next_byte == iobp->io_buffer_len)
88 iobp->io_buffer_len = 0;
90 syslog(LOG_ERR, "next_byte(%d) > io_buffer_len(%d)",
91 iobp->next_byte, iobp->io_buffer_len);
97 /* don't do tiny reads, grow first if we need to */
98 rqlen = iobp->io_buffer_size - iobp->io_buffer_len;
102 iobp->io_buffer_size += 128;
103 tmp = realloc(iobp->io_buffer, iobp->io_buffer_size);
105 syslog(LOG_INFO, "Insufficient memory");
106 exit(EX_UNAVAILABLE);
108 iobp->io_buffer = tmp;
109 rqlen = iobp->io_buffer_size - iobp->io_buffer_len;
113 * Always leave an unused byte at the end of the buffer
114 * because the debug output uses that byte from time to time
115 * to ensure that something that is being printed is \0 terminated.
120 rlen = read(iobp->fd, &iobp->io_buffer[iobp->io_buffer_len], rqlen);
121 iobp->data_available = 0;
124 if (errno == EAGAIN || errno == EINTR)
126 if (errno != ECONNRESET) {
127 syslog(LOG_INFO, "read() failed on socket from %s (%m)",
131 /* fall through to EOF case */
137 iobp->io_buffer_len += rlen;
144 * telnet_getline - a hacked up version of fgets to ignore TELNET escape codes.
146 * This code is derived from the getline routine found in the UC Berkeley
152 telnet_getline(struct csiob *iobp, struct csiob *telnet_passthrough)
156 unsigned char tbuf[100];
158 iobp->line_buffer[0] = '\0';
161 * If the buffer is empty then refill it right away.
163 if (iobp->next_byte == iobp->io_buffer_len)
164 if (!refill_buffer(iobp))
168 * Is there a telnet command in the buffer?
170 ch = iobp->io_buffer[iobp->next_byte];
173 * Yes - buffer must have at least three bytes in it
175 if (iobp->io_buffer_len - iobp->next_byte < 3) {
176 if (!refill_buffer(iobp))
178 if (iobp->io_buffer_len - iobp->next_byte < 3)
183 ch = iobp->io_buffer[iobp->next_byte++];
192 tbuf[2] = iobp->io_buffer[iobp->next_byte++];
193 (void)send(telnet_passthrough->fd, tbuf, 3,
194 telnet_passthrough->send_oob_flags);
206 * Is there a newline in the buffer?
208 for (ix = iobp->next_byte; ix < iobp->io_buffer_len;
210 if (iobp->io_buffer[ix] == '\n')
212 if (iobp->io_buffer[ix] == '\0') {
214 "got NUL byte from %s - bye!",
220 if (ix == iobp->io_buffer_len) {
221 if (!refill_buffer(iobp))
224 * Empty line returned
225 * will try again soon!
231 * Expand the line buffer if it isn't big enough. We
232 * use a fudge factor of 5 rather than trying to
233 * figure out exactly how to account for the '\0 \r\n' and
234 * such. The correct fudge factor is 0, 1 or 2 but
235 * anything higher also works. We also grow it by a
236 * bunch to avoid having to do this often. Yes this is
239 if (ix - iobp->next_byte > iobp->line_buffer_size - 5) {
242 iobp->line_buffer_size = 256 + ix - iobp->next_byte;
243 tmp = realloc(iobp->line_buffer,
244 iobp->line_buffer_size);
246 syslog(LOG_INFO, "Insufficient memory");
247 exit(EX_UNAVAILABLE);
249 iobp->line_buffer = tmp;
252 /* +1 is for the newline */
253 clen = (ix+1) - iobp->next_byte;
254 memcpy(iobp->line_buffer, &iobp->io_buffer[iobp->next_byte],
256 iobp->next_byte += clen;
257 iobp->line_buffer[clen] = '\0';