5 void buffer_init(buffer_t * buf)
8 buf->total_in = buf->total_out = 0;
11 void buffer_close(buffer_t * buf)
13 /* Our data is static - nothing needs "release", just reset it */
17 /* Code these simple ones in compact form */
18 unsigned int buffer_used(buffer_t * buf)
23 unsigned int buffer_unused(buffer_t * buf)
25 return (MAX_DATA_SIZE - buf->used);
28 int buffer_full(buffer_t * buf)
30 return (buf->used == MAX_DATA_SIZE ? 1 : 0);
33 int buffer_notfull(buffer_t * buf)
35 return (buf->used < MAX_DATA_SIZE ? 1 : 0);
38 int buffer_empty(buffer_t * buf)
40 return (buf->used == 0 ? 1 : 0);
43 int buffer_notempty(buffer_t * buf)
45 return (buf->used > 0 ? 1 : 0);
48 unsigned long buffer_total_in(buffer_t * buf)
53 unsigned long buffer_total_out(buffer_t * buf)
55 return buf->total_out;
59 * These 3 static (internal) functions don't adjust the "total" variables as
60 * it's not sure when they're called how it should be interpreted. Only the
61 * higher-level "buffer_[to|from]_[fd|SSL|BIO]" functions should alter these
64 # if 0 /* To avoid "unused" warnings */
65 static unsigned int buffer_adddata(buffer_t * buf, const unsigned char *ptr,
68 unsigned int added = MAX_DATA_SIZE - buf->used;
73 memcpy(buf->data + buf->used, ptr, added);
75 buf->total_in += added;
79 static unsigned int buffer_tobuffer(buffer_t * to, buffer_t * from, int cap)
81 unsigned int moved, tomove = from->used;
82 if ((int)tomove > cap)
86 moved = buffer_adddata(to, from->data, tomove);
89 buffer_takedata(from, NULL, moved);
94 static unsigned int buffer_takedata(buffer_t * buf, unsigned char *ptr,
97 unsigned int taken = buf->used;
103 memcpy(ptr, buf->data, taken);
105 /* Do we have to scroll? */
107 memmove(buf->data, buf->data + taken, buf->used);
113 int buffer_from_fd(buffer_t * buf, int fd)
115 int toread = buffer_unused(buf);
117 /* Shouldn't be called in this case! */
119 toread = read(fd, buf->data + buf->used, toread);
122 buf->total_in += toread;
127 int buffer_to_fd(buffer_t * buf, int fd)
129 int towrite = buffer_used(buf);
131 /* Shouldn't be called in this case! */
133 towrite = write(fd, buf->data, towrite);
135 buffer_takedata(buf, NULL, towrite);
136 buf->total_out += towrite;
141 # endif /* !defined(NO_IP) */
145 static void int_ssl_check(SSL *s, int ret)
147 int e = SSL_get_error(s, ret);
150 * These seem to be harmless and already "dealt with" by our
151 * non-blocking environment. NB: "ZERO_RETURN" is the clean "error"
152 * indicating a successfully closed SSL tunnel. We let this happen
153 * because our IO loop should not appear to have broken on this
154 * condition - and outside the IO loop, the "shutdown" state is
158 case SSL_ERROR_WANT_READ:
159 case SSL_ERROR_WANT_WRITE:
160 case SSL_ERROR_WANT_X509_LOOKUP:
161 case SSL_ERROR_ZERO_RETURN:
164 * These seem to be indications of a genuine error that should result
165 * in the SSL tunnel being regarded as "dead".
167 case SSL_ERROR_SYSCALL:
169 SSL_set_app_data(s, (char *)1);
175 * For any other errors that (a) exist, and (b) crop up - we need to
176 * interpret what to do with them - so "politely inform" the caller that
177 * the code needs updating here.
182 void buffer_from_SSL(buffer_t * buf, SSL *ssl)
185 if (!ssl || buffer_full(buf))
187 ret = SSL_read(ssl, buf->data + buf->used, buffer_unused(buf));
190 buf->total_in += ret;
193 int_ssl_check(ssl, ret);
196 void buffer_to_SSL(buffer_t * buf, SSL *ssl)
199 if (!ssl || buffer_empty(buf))
201 ret = SSL_write(ssl, buf->data, buf->used);
203 buffer_takedata(buf, NULL, ret);
204 buf->total_out += ret;
207 int_ssl_check(ssl, ret);
210 void buffer_from_BIO(buffer_t * buf, BIO *bio)
213 if (!bio || buffer_full(buf))
215 ret = BIO_read(bio, buf->data + buf->used, buffer_unused(buf));
218 buf->total_in += ret;
222 void buffer_to_BIO(buffer_t * buf, BIO *bio)
225 if (!bio || buffer_empty(buf))
227 ret = BIO_write(bio, buf->data, buf->used);
229 buffer_takedata(buf, NULL, ret);
230 buf->total_out += ret;
234 # endif /* !defined(NO_OPENSSL) */
236 #endif /* !defined(NO_BUFFER) */