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 #if 0 /* To avoid "unused" warnings */
40 static unsigned int buffer_adddata(buffer_t
*buf
, const unsigned char *ptr
,
43 unsigned int added
= MAX_DATA_SIZE
- buf
->used
;
48 memcpy(buf
->data
+ buf
->used
, ptr
, added
);
50 buf
->total_in
+= added
;
54 static unsigned int buffer_tobuffer(buffer_t
*to
, buffer_t
*from
, int cap
)
56 unsigned int moved
, tomove
= from
->used
;
61 moved
= buffer_adddata(to
, from
->data
, tomove
);
64 buffer_takedata(from
, NULL
, moved
);
69 static unsigned int buffer_takedata(buffer_t
*buf
, unsigned char *ptr
,
72 unsigned int taken
= buf
->used
;
78 memcpy(ptr
, buf
->data
, taken
);
80 /* Do we have to scroll? */
82 memmove(buf
->data
, buf
->data
+ taken
, buf
->used
);
88 int buffer_from_fd(buffer_t
*buf
, int fd
)
90 int toread
= buffer_unused(buf
);
92 /* Shouldn't be called in this case! */
94 toread
= read(fd
, buf
->data
+ buf
->used
, toread
);
97 buf
->total_in
+= toread
;
102 int buffer_to_fd(buffer_t
*buf
, int fd
)
104 int towrite
= buffer_used(buf
);
106 /* Shouldn't be called in this case! */
108 towrite
= write(fd
, buf
->data
, towrite
);
110 buffer_takedata(buf
, NULL
, towrite
);
111 buf
->total_out
+= towrite
;
116 #endif /* !defined(NO_IP) */
120 static void int_ssl_check(SSL
*s
, int ret
)
122 int e
= SSL_get_error(s
, ret
);
124 /* These seem to be harmless and already "dealt with" by our
125 * non-blocking environment. NB: "ZERO_RETURN" is the clean
126 * "error" indicating a successfully closed SSL tunnel. We let
127 * this happen because our IO loop should not appear to have
128 * broken on this condition - and outside the IO loop, the
129 * "shutdown" state is checked. */
131 case SSL_ERROR_WANT_READ
:
132 case SSL_ERROR_WANT_WRITE
:
133 case SSL_ERROR_WANT_X509_LOOKUP
:
134 case SSL_ERROR_ZERO_RETURN
:
136 /* These seem to be indications of a genuine error that should
137 * result in the SSL tunnel being regarded as "dead". */
138 case SSL_ERROR_SYSCALL
:
140 SSL_set_app_data(s
, (char *)1);
145 /* For any other errors that (a) exist, and (b) crop up - we need to
146 * interpret what to do with them - so "politely inform" the caller that
147 * the code needs updating here. */
151 void buffer_from_SSL(buffer_t
*buf
, SSL
*ssl
)
154 if(!ssl
|| buffer_full(buf
))
156 ret
= SSL_read(ssl
, buf
->data
+ buf
->used
, buffer_unused(buf
));
159 buf
->total_in
+= ret
;
162 int_ssl_check(ssl
, ret
);
165 void buffer_to_SSL(buffer_t
*buf
, SSL
*ssl
)
168 if(!ssl
|| buffer_empty(buf
))
170 ret
= SSL_write(ssl
, buf
->data
, buf
->used
);
172 buffer_takedata(buf
, NULL
, ret
);
173 buf
->total_out
+= ret
;
176 int_ssl_check(ssl
, ret
);
179 void buffer_from_BIO(buffer_t
*buf
, BIO
*bio
)
182 if(!bio
|| buffer_full(buf
))
184 ret
= BIO_read(bio
, buf
->data
+ buf
->used
, buffer_unused(buf
));
187 buf
->total_in
+= ret
;
191 void buffer_to_BIO(buffer_t
*buf
, BIO
*bio
)
194 if(!bio
|| buffer_empty(buf
))
196 ret
= BIO_write(bio
, buf
->data
, buf
->used
);
198 buffer_takedata(buf
, NULL
, ret
);
199 buf
->total_out
+= ret
;
203 #endif /* !defined(NO_OPENSSL) */
205 #endif /* !defined(NO_BUFFER) */