From: Frank Lahm Date: Mon, 23 May 2011 09:06:54 +0000 (+0200) Subject: Check againt disconnected flags. Move static funcs at top of file X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=netatalk.git;a=commitdiff_plain;h=daf7143d8b751bea2da8ae3358ffaf930c74d6d8 Check againt disconnected flags. Move static funcs at top of file --- diff --git a/libatalk/dsi/dsi_stream.c b/libatalk/dsi/dsi_stream.c index 1ed2b447..5c99ac2c 100644 --- a/libatalk/dsi/dsi_stream.c +++ b/libatalk/dsi/dsi_stream.c @@ -122,6 +122,116 @@ static int dsi_peek(DSI *dsi) return 0; } +/* + * Return all bytes up to count from dsi->buffer if there are any buffered there + */ +static size_t from_buf(DSI *dsi, u_int8_t *buf, size_t count) +{ + size_t nbe = 0; + + if (dsi->buffer == NULL) + /* afpd master has no DSI buffering */ + return 0; + + LOG(log_maxdebug, logtype_dsi, "from_buf: %u bytes", count); + + nbe = dsi->eof - dsi->start; + + if (nbe > 0) { + nbe = min((size_t)nbe, count); + memcpy(buf, dsi->start, nbe); + dsi->start += nbe; + + if (dsi->eof == dsi->start) + dsi->start = dsi->eof = dsi->buffer; + } + + LOG(log_debug, logtype_dsi, "from_buf(read: %u, unread:%u , space left: %u): returning %u", + dsi->start - dsi->buffer, dsi->eof - dsi->start, dsi->end - dsi->eof, nbe); + + return nbe; +} + +/* + * Get bytes from buffer dsi->buffer or read from socket + * + * 1. Check if there are bytes in the the dsi->buffer buffer. + * 2. Return bytes from (1) if yes. + * Note: this may return fewer bytes then requested in count !! + * 3. If the buffer was empty, read from the socket. + */ +static ssize_t buf_read(DSI *dsi, u_int8_t *buf, size_t count) +{ + ssize_t len; + + LOG(log_maxdebug, logtype_dsi, "buf_read(%u bytes)", count); + + if (!count) + return 0; + + len = from_buf(dsi, buf, count); /* 1. */ + if (len) + return len; /* 2. */ + + len = readt(dsi->socket, buf, count, 0, 1); /* 3. */ + + LOG(log_maxdebug, logtype_dsi, "buf_read(%u bytes): got: %d", count, len); + + return len; +} + +/* + * Get "length" bytes from buffer and/or socket. In order to avoid frequent small reads + * this tries to read larger chunks (8192 bytes) into a buffer. + */ +static size_t dsi_buffered_stream_read(DSI *dsi, u_int8_t *data, const size_t length) +{ + size_t len; + size_t buflen; + + LOG(log_maxdebug, logtype_dsi, "dsi_buffered_stream_read: %u bytes", length); + + len = from_buf(dsi, data, length); /* read from buffer dsi->buffer */ + dsi->read_count += len; + if (len == length) { /* got enough bytes from there ? */ + return len; /* yes */ + } + + /* fill the buffer with 8192 bytes or until buffer is full */ + buflen = min(8192, dsi->end - dsi->eof); + if (buflen > 0) { + ssize_t ret; + ret = read(dsi->socket, dsi->eof, buflen); + if (ret > 0) + dsi->eof += ret; + } + + /* now get the remaining data */ + if ((buflen = dsi_stream_read(dsi, data + len, length - len)) != length - len) + return 0; + len += buflen; + + return len; +} + +/* --------------------------------------- +*/ +static void block_sig(DSI *dsi) +{ + dsi->in_write++; +} + +/* --------------------------------------- +*/ +static void unblock_sig(DSI *dsi) +{ + dsi->in_write--; +} + +/********************************************************************************* + * Public functions + *********************************************************************************/ + /*! * Communication error with the client, enter disconnected state * @@ -150,6 +260,9 @@ ssize_t dsi_stream_write(DSI *dsi, void *data, const size_t length, int mode) LOG(log_maxdebug, logtype_dsi, "dsi_stream_write: sending %u bytes", length); + if (dsi->flags & DSI_DISCONNECTED) + return -1; + while (written < length) { len = send(dsi->socket, (u_int8_t *) data + written, length - written, flags); if (len >= 0) { @@ -201,6 +314,9 @@ ssize_t dsi_stream_read_file(DSI *dsi, int fromfd, off_t offset, const size_t le LOG(log_maxdebug, logtype_dsi, "dsi_stream_read_file: sending %u bytes", length); + if (dsi->flags & DSI_DISCONNECTED) + return -1; + dsi->in_write++; written = 0; @@ -240,63 +356,6 @@ ssize_t dsi_stream_read_file(DSI *dsi, int fromfd, off_t offset, const size_t le } #endif -/* - * Return all bytes up to count from dsi->buffer if there are any buffered there - */ -static size_t from_buf(DSI *dsi, u_int8_t *buf, size_t count) -{ - size_t nbe = 0; - - if (dsi->buffer == NULL) - /* afpd master has no DSI buffering */ - return 0; - - LOG(log_maxdebug, logtype_dsi, "from_buf: %u bytes", count); - - nbe = dsi->eof - dsi->start; - - if (nbe > 0) { - nbe = min((size_t)nbe, count); - memcpy(buf, dsi->start, nbe); - dsi->start += nbe; - - if (dsi->eof == dsi->start) - dsi->start = dsi->eof = dsi->buffer; - } - - LOG(log_debug, logtype_dsi, "from_buf(read: %u, unread:%u , space left: %u): returning %u", - dsi->start - dsi->buffer, dsi->eof - dsi->start, dsi->end - dsi->eof, nbe); - - return nbe; -} - -/* - * Get bytes from buffer dsi->buffer or read from socket - * - * 1. Check if there are bytes in the the dsi->buffer buffer. - * 2. Return bytes from (1) if yes. - * Note: this may return fewer bytes then requested in count !! - * 3. If the buffer was empty, read from the socket. - */ -static ssize_t buf_read(DSI *dsi, u_int8_t *buf, size_t count) -{ - ssize_t len; - - LOG(log_maxdebug, logtype_dsi, "buf_read(%u bytes)", count); - - if (!count) - return 0; - - len = from_buf(dsi, buf, count); /* 1. */ - if (len) - return len; /* 2. */ - - len = readt(dsi->socket, buf, count, 0, 1); /* 3. */ - - LOG(log_maxdebug, logtype_dsi, "buf_read(%u bytes): got: %d", count, len); - - return len; -} /* * Essentially a loop around buf_read() to ensure "length" bytes are read @@ -309,6 +368,9 @@ size_t dsi_stream_read(DSI *dsi, void *data, const size_t length) size_t stored; ssize_t len; + if (dsi->flags & DSI_DISCONNECTED) + return 0; + LOG(log_maxdebug, logtype_dsi, "dsi_stream_read(%u bytes)", length); stored = 0; @@ -338,54 +400,6 @@ size_t dsi_stream_read(DSI *dsi, void *data, const size_t length) return stored; } -/* - * Get "length" bytes from buffer and/or socket. In order to avoid frequent small reads - * this tries to read larger chunks (8192 bytes) into a buffer. - */ -static size_t dsi_buffered_stream_read(DSI *dsi, u_int8_t *data, const size_t length) -{ - size_t len; - size_t buflen; - - LOG(log_maxdebug, logtype_dsi, "dsi_buffered_stream_read: %u bytes", length); - - len = from_buf(dsi, data, length); /* read from buffer dsi->buffer */ - dsi->read_count += len; - if (len == length) { /* got enough bytes from there ? */ - return len; /* yes */ - } - - /* fill the buffer with 8192 bytes or until buffer is full */ - buflen = min(8192, dsi->end - dsi->eof); - if (buflen > 0) { - ssize_t ret; - ret = read(dsi->socket, dsi->eof, buflen); - if (ret > 0) - dsi->eof += ret; - } - - /* now get the remaining data */ - if ((buflen = dsi_stream_read(dsi, data + len, length - len)) != length - len) - return 0; - len += buflen; - - return len; -} - -/* --------------------------------------- -*/ -static void block_sig(DSI *dsi) -{ - dsi->in_write++; -} - -/* --------------------------------------- -*/ -static void unblock_sig(DSI *dsi) -{ - dsi->in_write--; -} - /* --------------------------------------- * write data. 0 on failure. this assumes that dsi_len will never * cause an overflow in the data buffer. @@ -400,6 +414,9 @@ int dsi_stream_send(DSI *dsi, void *buf, size_t length) LOG(log_maxdebug, logtype_dsi, "dsi_stream_send: %u bytes", length ? length : sizeof(block)); + if (dsi->flags & DSI_DISCONNECTED) + return 0; + block[0] = dsi->header.dsi_flags; block[1] = dsi->header.dsi_command; memcpy(block + 2, &dsi->header.dsi_requestID, @@ -470,6 +487,9 @@ int dsi_stream_receive(DSI *dsi, void *buf, const size_t ilength, LOG(log_maxdebug, logtype_dsi, "dsi_stream_receive: %u bytes", ilength); + if (dsi->flags & DSI_DISCONNECTED) + return 0; + /* read in the header */ if (dsi_buffered_stream_read(dsi, (u_int8_t *)block, sizeof(block)) != sizeof(block)) return 0;