summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/ChangeLog10
-rw-r--r--lib/stream.c29
-rw-r--r--lib/stream.h17
3 files changed, 55 insertions, 1 deletions
diff --git a/lib/ChangeLog b/lib/ChangeLog
index 0627d206..ac05baba 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,5 +1,15 @@
2005-02-16 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
+ * stream.h: Declare new function stream_read_try suitable for use
+ with non-blocking file descriptors. Indicate that stream_read
+ and stream_read_unblock are deprecated.
+ * stream.c: (stream_read_try) New function for use with non-blocking
+ I/O.
+ (stream_recvmsg) Should return -1 if the stream is too small to
+ contain the data.
+
+2005-02-16 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
+
* network.c: (set_nonblocking) Should check return code from
fcntl(F_GETFL).
diff --git a/lib/stream.c b/lib/stream.c
index e15d0426..7e75f0da 100644
--- a/lib/stream.c
+++ b/lib/stream.c
@@ -603,6 +603,31 @@ stream_read_unblock (struct stream *s, int fd, size_t size)
return nbytes;
}
+ssize_t
+stream_read_try(struct stream *s, int fd, size_t size)
+{
+ ssize_t nbytes;
+
+ STREAM_VERIFY_SANE(s);
+
+ if (STREAM_WRITEABLE(s) < size)
+ {
+ STREAM_BOUND_WARN (s, "put");
+ /* Fatal (not transient) error, since retrying will not help
+ (stream is too small to contain the desired data). */
+ return -1;
+ }
+
+ if ((nbytes = read(fd, s->data + s->endp, size)) >= 0)
+ {
+ s->endp += nbytes;
+ return nbytes;
+ }
+ /* Error: was it transient (return -2) or fatal (return -1)? */
+ return ((errno == EAGAIN) || (errno == EWOULDBLOCK) || (errno == EINTR)) ?
+ -2 : -1;
+}
+
/* Read up to smaller of size or SIZE_REMAIN() bytes to the stream, starting
* from endp.
* First iovec will be used to receive the data.
@@ -621,7 +646,9 @@ stream_recvmsg (struct stream *s, int fd, struct msghdr *msgh, int flags,
if (STREAM_WRITEABLE (s) < size)
{
STREAM_BOUND_WARN (s, "put");
- return 0;
+ /* This is a logic error in the calling code: the stream is too small
+ to hold the desired data! */
+ return -1;
}
iov = &(msgh->msg_iov[0]);
diff --git a/lib/stream.h b/lib/stream.h
index fe45a4f5..54a16740 100644
--- a/lib/stream.h
+++ b/lib/stream.h
@@ -165,8 +165,25 @@ u_int32_t stream_get_ipv4 (struct stream *);
#undef stream_read
#undef stream_write
+
+/* Deprecated: assumes blocking I/O. Will be removed.
+ Use stream_read_try instead. */
int stream_read (struct stream *, int, size_t);
+
+/* Deprecated: all file descriptors should already be non-blocking.
+ Will be removed. Use stream_read_try instead. */
int stream_read_unblock (struct stream *, int, size_t);
+
+/* Read up to size bytes into the stream.
+ Return code:
+ >0: number of bytes read
+ 0: end-of-file
+ -1: fatal error
+ -2: transient error, should retry later (i.e. EAGAIN or EINTR)
+ This is suitable for use with non-blocking file descriptors.
+ */
+extern ssize_t stream_read_try(struct stream *s, int fd, size_t size);
+
int stream_recvmsg (struct stream *s, int fd, struct msghdr *,
int flags, size_t size);
int stream_write (struct stream *, u_char *, size_t);