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) {
20 unsigned int buffer_unused(buffer_t *buf) {
21 return (MAX_DATA_SIZE - buf->used); }
22 int buffer_full(buffer_t *buf) {
23 return (buf->used == MAX_DATA_SIZE ? 1 : 0); }
24 int buffer_notfull(buffer_t *buf) {
25 return (buf->used < MAX_DATA_SIZE ? 1 : 0); }
26 int buffer_empty(buffer_t *buf) {
27 return (buf->used == 0 ? 1 : 0); }
28 int buffer_notempty(buffer_t *buf) {
29 return (buf->used > 0 ? 1 : 0); }
30 unsigned long buffer_total_in(buffer_t *buf) {
31 return buf->total_in; }
32 unsigned long buffer_total_out(buffer_t *buf) {
33 return buf->total_out; }
35 /* These 3 static (internal) functions don't adjust the "total" variables as
36 * it's not sure when they're called how it should be interpreted. Only the
37 * higher-level "buffer_[to|from]_[fd|SSL|BIO]" functions should alter these
39 static unsigned int buffer_adddata(buffer_t *buf, const unsigned char *ptr,
42 unsigned int added = MAX_DATA_SIZE - buf->used;
47 memcpy(buf->data + buf->used, ptr, added);
49 buf->total_in += added;
53 static unsigned int buffer_takedata(buffer_t *buf, unsigned char *ptr,
56 unsigned int taken = buf->used;
62 memcpy(ptr, buf->data, taken);
64 /* Do we have to scroll? */
66 memmove(buf->data, buf->data + taken, buf->used);
70 static unsigned int buffer_tobuffer(buffer_t *to, buffer_t *from, int cap)
72 unsigned int moved, tomove = from->used;
77 moved = buffer_adddata(to, from->data, tomove);
80 buffer_takedata(from, NULL, moved);
86 int buffer_from_fd(buffer_t *buf, int fd)
88 unsigned int toread = buffer_unused(buf);
90 /* Shouldn't be called in this case! */
92 toread = read(fd, buf->data + buf->used, toread);
95 buf->total_in += toread;
100 int buffer_to_fd(buffer_t *buf, int fd)
102 unsigned int towrite = buffer_used(buf);
104 /* Shouldn't be called in this case! */
106 towrite = write(fd, buf->data, towrite);
108 buffer_takedata(buf, NULL, towrite);
109 buf->total_out += towrite;
114 #endif /* !defined(NO_IP) */
118 static void int_ssl_check(SSL *s, int ret)
120 int e = SSL_get_error(s, ret);
122 /* These seem to be harmless and already "dealt with" by our
123 * non-blocking environment. NB: "ZERO_RETURN" is the clean
124 * "error" indicating a successfully closed SSL tunnel. We let
125 * this happen because our IO loop should not appear to have
126 * broken on this condition - and outside the IO loop, the
127 * "shutdown" state is checked. */
129 case SSL_ERROR_WANT_READ:
130 case SSL_ERROR_WANT_WRITE:
131 case SSL_ERROR_WANT_X509_LOOKUP:
132 case SSL_ERROR_ZERO_RETURN:
134 /* These seem to be indications of a genuine error that should
135 * result in the SSL tunnel being regarded as "dead". */
136 case SSL_ERROR_SYSCALL:
138 SSL_set_app_data(s, (char *)1);
143 /* For any other errors that (a) exist, and (b) crop up - we need to
144 * interpret what to do with them - so "politely inform" the caller that
145 * the code needs updating here. */
149 void buffer_from_SSL(buffer_t *buf, SSL *ssl)
152 if(!ssl || buffer_full(buf))
154 ret = SSL_read(ssl, buf->data + buf->used, buffer_unused(buf));
157 buf->total_in += ret;
160 int_ssl_check(ssl, ret);
163 void buffer_to_SSL(buffer_t *buf, SSL *ssl)
166 if(!ssl || buffer_empty(buf))
168 ret = SSL_write(ssl, buf->data, buf->used);
170 buffer_takedata(buf, NULL, ret);
171 buf->total_out += ret;
174 int_ssl_check(ssl, ret);
177 void buffer_from_BIO(buffer_t *buf, BIO *bio)
180 if(!bio || buffer_full(buf))
182 ret = BIO_read(bio, buf->data + buf->used, buffer_unused(buf));
185 buf->total_in += ret;
189 void buffer_to_BIO(buffer_t *buf, BIO *bio)
192 if(!bio || buffer_empty(buf))
194 ret = BIO_write(bio, buf->data, buf->used);
196 buffer_takedata(buf, NULL, ret);
197 buf->total_out += ret;
201 #endif /* !defined(NO_OPENSSL) */
203 #endif /* !defined(NO_BUFFER) */