-rw-r--r-- | kmicromail/libetpan/include/libetpan/mailstream.h | 1 | ||||
-rw-r--r-- | kmicromail/libetpan/tools/mailstream.c | 2 | ||||
-rw-r--r-- | kmicromail/libetpan/tools/mailstream.h | 3 | ||||
-rw-r--r-- | kmicromail/libetpan/tools/mailstream_socket.c | 13 |
4 files changed, 14 insertions, 5 deletions
diff --git a/kmicromail/libetpan/include/libetpan/mailstream.h b/kmicromail/libetpan/include/libetpan/mailstream.h index a4e35cd..ffb5062 100644 --- a/kmicromail/libetpan/include/libetpan/mailstream.h +++ b/kmicromail/libetpan/include/libetpan/mailstream.h | |||
@@ -1,73 +1,74 @@ | |||
1 | /* | 1 | /* |
2 | * libEtPan! -- a mail stuff library | 2 | * libEtPan! -- a mail stuff library |
3 | * | 3 | * |
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | 4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa |
5 | * All rights reserved. | 5 | * All rights reserved. |
6 | * | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | 14 | * documentation and/or other materials provided with the distribution. |
15 | * 3. Neither the name of the libEtPan! project nor the names of its | 15 | * 3. Neither the name of the libEtPan! project nor the names of its |
16 | * contributors may be used to endorse or promote products derived | 16 | * contributors may be used to endorse or promote products derived |
17 | * from this software without specific prior written permission. | 17 | * from this software without specific prior written permission. |
18 | * | 18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
29 | * SUCH DAMAGE. | 29 | * SUCH DAMAGE. |
30 | */ | 30 | */ |
31 | 31 | ||
32 | /* | 32 | /* |
33 | * $Id$ | 33 | * $Id$ |
34 | */ | 34 | */ |
35 | 35 | ||
36 | #ifndef MAILSTREAM_H | 36 | #ifndef MAILSTREAM_H |
37 | #define DEFAULT_NETWORK_TIMEOUT 30 | ||
37 | 38 | ||
38 | #define MAILSTREAM_H | 39 | #define MAILSTREAM_H |
39 | 40 | ||
40 | #include <sys/time.h> | 41 | #include <sys/time.h> |
41 | 42 | ||
42 | #include <libetpan/mailstream_low.h> | 43 | #include <libetpan/mailstream_low.h> |
43 | #include <libetpan/mailstream_helper.h> | 44 | #include <libetpan/mailstream_helper.h> |
44 | #include <libetpan/mailstream_socket.h> | 45 | #include <libetpan/mailstream_socket.h> |
45 | #include <libetpan/mailstream_ssl.h> | 46 | #include <libetpan/mailstream_ssl.h> |
46 | #include <libetpan/mailstream_types.h> | 47 | #include <libetpan/mailstream_types.h> |
47 | 48 | ||
48 | #ifdef __cplusplus | 49 | #ifdef __cplusplus |
49 | extern "C" { | 50 | extern "C" { |
50 | #endif | 51 | #endif |
51 | 52 | ||
52 | mailstream * mailstream_new(mailstream_low * low, size_t buffer_size); | 53 | mailstream * mailstream_new(mailstream_low * low, size_t buffer_size); |
53 | ssize_t mailstream_write(mailstream * s, const void * buf, size_t count); | 54 | ssize_t mailstream_write(mailstream * s, const void * buf, size_t count); |
54 | ssize_t mailstream_read(mailstream * s, void * buf, size_t count); | 55 | ssize_t mailstream_read(mailstream * s, void * buf, size_t count); |
55 | int mailstream_close(mailstream * s); | 56 | int mailstream_close(mailstream * s); |
56 | int mailstream_flush(mailstream * s); | 57 | int mailstream_flush(mailstream * s); |
57 | ssize_t mailstream_feed_read_buffer(mailstream * s); | 58 | ssize_t mailstream_feed_read_buffer(mailstream * s); |
58 | mailstream_low * mailstream_get_low(mailstream * s); | 59 | mailstream_low * mailstream_get_low(mailstream * s); |
59 | void mailstream_set_low(mailstream * s, mailstream_low * low); | 60 | void mailstream_set_low(mailstream * s, mailstream_low * low); |
60 | 61 | ||
61 | #ifdef LIBETPAN_MAILSTREAM_DEBUG | 62 | #ifdef LIBETPAN_MAILSTREAM_DEBUG |
62 | extern int mailstream_debug; | 63 | extern int mailstream_debug; |
63 | #endif | 64 | #endif |
64 | 65 | ||
65 | #define LIBETPAN_MAILSTREAM_NETWORK_DELAY | 66 | #define LIBETPAN_MAILSTREAM_NETWORK_DELAY |
66 | extern struct timeval mailstream_network_delay; | 67 | extern struct timeval mailstream_network_delay; |
67 | 68 | ||
68 | #ifdef __cplusplus | 69 | #ifdef __cplusplus |
69 | } | 70 | } |
70 | #endif | 71 | #endif |
71 | 72 | ||
72 | #endif | 73 | #endif |
73 | 74 | ||
diff --git a/kmicromail/libetpan/tools/mailstream.c b/kmicromail/libetpan/tools/mailstream.c index 0f55e67..6d1a8cc 100644 --- a/kmicromail/libetpan/tools/mailstream.c +++ b/kmicromail/libetpan/tools/mailstream.c | |||
@@ -1,394 +1,394 @@ | |||
1 | /* | 1 | /* |
2 | * libEtPan! -- a mail stuff library | 2 | * libEtPan! -- a mail stuff library |
3 | * | 3 | * |
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | 4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa |
5 | * All rights reserved. | 5 | * All rights reserved. |
6 | * | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | 14 | * documentation and/or other materials provided with the distribution. |
15 | * 3. Neither the name of the libEtPan! project nor the names of its | 15 | * 3. Neither the name of the libEtPan! project nor the names of its |
16 | * contributors may be used to endorse or promote products derived | 16 | * contributors may be used to endorse or promote products derived |
17 | * from this software without specific prior written permission. | 17 | * from this software without specific prior written permission. |
18 | * | 18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
29 | * SUCH DAMAGE. | 29 | * SUCH DAMAGE. |
30 | */ | 30 | */ |
31 | 31 | ||
32 | /* | 32 | /* |
33 | * $Id$ | 33 | * $Id$ |
34 | */ | 34 | */ |
35 | 35 | ||
36 | #include "mailstream.h" | 36 | #include "mailstream.h" |
37 | #include "maillock.h" | 37 | #include "maillock.h" |
38 | #include <string.h> | 38 | #include <string.h> |
39 | #include <stdlib.h> | 39 | #include <stdlib.h> |
40 | #include <sys/types.h> | 40 | #include <sys/types.h> |
41 | #include <sys/stat.h> | 41 | #include <sys/stat.h> |
42 | 42 | ||
43 | #define DEFAULT_NETWORK_TIMEOUT 300 | 43 | |
44 | 44 | ||
45 | #ifdef LIBETPAN_MAILSTREAM_DEBUG | 45 | #ifdef LIBETPAN_MAILSTREAM_DEBUG |
46 | 46 | ||
47 | #define STREAM_DEBUG | 47 | #define STREAM_DEBUG |
48 | 48 | ||
49 | #include <stdio.h> | 49 | #include <stdio.h> |
50 | 50 | ||
51 | #define LOG_FILE "libetpan-stream-debug.log" | 51 | #define LOG_FILE "libetpan-stream-debug.log" |
52 | 52 | ||
53 | int mailstream_debug = 0; | 53 | int mailstream_debug = 0; |
54 | 54 | ||
55 | #define STREAM_LOG_BUF(buf, size) \ | 55 | #define STREAM_LOG_BUF(buf, size) \ |
56 | if (mailstream_debug) { \ | 56 | if (mailstream_debug) { \ |
57 | FILE * f; \ | 57 | FILE * f; \ |
58 | mode_t old_mask; \ | 58 | mode_t old_mask; \ |
59 | \ | 59 | \ |
60 | old_mask = umask(0077); \ | 60 | old_mask = umask(0077); \ |
61 | f = fopen(LOG_FILE, "a"); \ | 61 | f = fopen(LOG_FILE, "a"); \ |
62 | umask(old_mask); \ | 62 | umask(old_mask); \ |
63 | if (f != NULL) { \ | 63 | if (f != NULL) { \ |
64 | maillock_write_lock(LOG_FILE, fileno(f)); \ | 64 | maillock_write_lock(LOG_FILE, fileno(f)); \ |
65 | fwrite((buf), 1, (size), f); \ | 65 | fwrite((buf), 1, (size), f); \ |
66 | maillock_write_unlock(LOG_FILE, fileno(f)); \ | 66 | maillock_write_unlock(LOG_FILE, fileno(f)); \ |
67 | fclose(f); \ | 67 | fclose(f); \ |
68 | } \ | 68 | } \ |
69 | } | 69 | } |
70 | 70 | ||
71 | #define STREAM_LOG(str) \ | 71 | #define STREAM_LOG(str) \ |
72 | if (mailstream_debug) { \ | 72 | if (mailstream_debug) { \ |
73 | FILE * f; \ | 73 | FILE * f; \ |
74 | mode_t old_mask; \ | 74 | mode_t old_mask; \ |
75 | \ | 75 | \ |
76 | old_mask = umask(0077); \ | 76 | old_mask = umask(0077); \ |
77 | f = fopen(LOG_FILE, "a"); \ | 77 | f = fopen(LOG_FILE, "a"); \ |
78 | umask(old_mask); \ | 78 | umask(old_mask); \ |
79 | if (f != NULL) { \ | 79 | if (f != NULL) { \ |
80 | maillock_write_lock(LOG_FILE, fileno(f)); \ | 80 | maillock_write_lock(LOG_FILE, fileno(f)); \ |
81 | fputs((str), f); \ | 81 | fputs((str), f); \ |
82 | maillock_write_unlock(LOG_FILE, fileno(f)); \ | 82 | maillock_write_unlock(LOG_FILE, fileno(f)); \ |
83 | fclose(f); \ | 83 | fclose(f); \ |
84 | } \ | 84 | } \ |
85 | } | 85 | } |
86 | 86 | ||
87 | #else | 87 | #else |
88 | 88 | ||
89 | #define STREAM_LOG_BUF(buf, size) do { } while (0) | 89 | #define STREAM_LOG_BUF(buf, size) do { } while (0) |
90 | #define STREAM_LOG(buf) do { } while (0) | 90 | #define STREAM_LOG(buf) do { } while (0) |
91 | 91 | ||
92 | #endif | 92 | #endif |
93 | 93 | ||
94 | 94 | ||
95 | mailstream * mailstream_new(mailstream_low * low, size_t buffer_size) | 95 | mailstream * mailstream_new(mailstream_low * low, size_t buffer_size) |
96 | { | 96 | { |
97 | mailstream * s; | 97 | mailstream * s; |
98 | 98 | ||
99 | s = malloc(sizeof(* s)); | 99 | s = malloc(sizeof(* s)); |
100 | if (s == NULL) | 100 | if (s == NULL) |
101 | goto err; | 101 | goto err; |
102 | 102 | ||
103 | s->read_buffer = malloc(buffer_size); | 103 | s->read_buffer = malloc(buffer_size); |
104 | if (s->read_buffer == NULL) | 104 | if (s->read_buffer == NULL) |
105 | goto free_s; | 105 | goto free_s; |
106 | s->read_buffer_len = 0; | 106 | s->read_buffer_len = 0; |
107 | 107 | ||
108 | s->write_buffer = malloc(buffer_size); | 108 | s->write_buffer = malloc(buffer_size); |
109 | if (s->write_buffer == NULL) | 109 | if (s->write_buffer == NULL) |
110 | goto free_read_buffer; | 110 | goto free_read_buffer; |
111 | s->write_buffer_len = 0; | 111 | s->write_buffer_len = 0; |
112 | 112 | ||
113 | s->buffer_max_size = buffer_size; | 113 | s->buffer_max_size = buffer_size; |
114 | s->low = low; | 114 | s->low = low; |
115 | 115 | ||
116 | return s; | 116 | return s; |
117 | 117 | ||
118 | free_read_buffer: | 118 | free_read_buffer: |
119 | free(s->read_buffer); | 119 | free(s->read_buffer); |
120 | free_s: | 120 | free_s: |
121 | free(s); | 121 | free(s); |
122 | err: | 122 | err: |
123 | return NULL; | 123 | return NULL; |
124 | } | 124 | } |
125 | 125 | ||
126 | static size_t write_to_internal_buffer(mailstream * s, | 126 | static size_t write_to_internal_buffer(mailstream * s, |
127 | const void * buf, size_t count) | 127 | const void * buf, size_t count) |
128 | { | 128 | { |
129 | memcpy(s->write_buffer + s->write_buffer_len, buf, count); | 129 | memcpy(s->write_buffer + s->write_buffer_len, buf, count); |
130 | s->write_buffer_len += count; | 130 | s->write_buffer_len += count; |
131 | 131 | ||
132 | return count; | 132 | return count; |
133 | } | 133 | } |
134 | 134 | ||
135 | static size_t write_direct(mailstream * s, const void * buf, size_t count) | 135 | static size_t write_direct(mailstream * s, const void * buf, size_t count) |
136 | { | 136 | { |
137 | size_t left; | 137 | size_t left; |
138 | const char * cur_buf; | 138 | const char * cur_buf; |
139 | ssize_t written; | 139 | ssize_t written; |
140 | 140 | ||
141 | cur_buf = buf; | 141 | cur_buf = buf; |
142 | left = count; | 142 | left = count; |
143 | while (left > 0) { | 143 | while (left > 0) { |
144 | written = mailstream_low_write(s->low, cur_buf, left); | 144 | written = mailstream_low_write(s->low, cur_buf, left); |
145 | 145 | ||
146 | if (written == -1) { | 146 | if (written == -1) { |
147 | if (count == left) | 147 | if (count == left) |
148 | return -1; | 148 | return -1; |
149 | else | 149 | else |
150 | return count - left; | 150 | return count - left; |
151 | } | 151 | } |
152 | 152 | ||
153 | cur_buf += written; | 153 | cur_buf += written; |
154 | left -= written; | 154 | left -= written; |
155 | } | 155 | } |
156 | 156 | ||
157 | return count; | 157 | return count; |
158 | } | 158 | } |
159 | 159 | ||
160 | ssize_t mailstream_write(mailstream * s, const void * buf, size_t count) | 160 | ssize_t mailstream_write(mailstream * s, const void * buf, size_t count) |
161 | { | 161 | { |
162 | int r; | 162 | int r; |
163 | 163 | ||
164 | if (s == NULL) | 164 | if (s == NULL) |
165 | return -1; | 165 | return -1; |
166 | 166 | ||
167 | if (count + s->write_buffer_len > s->buffer_max_size) { | 167 | if (count + s->write_buffer_len > s->buffer_max_size) { |
168 | r = mailstream_flush(s); | 168 | r = mailstream_flush(s); |
169 | if (r == -1) | 169 | if (r == -1) |
170 | return -1; | 170 | return -1; |
171 | 171 | ||
172 | if (count > s->buffer_max_size) | 172 | if (count > s->buffer_max_size) |
173 | return write_direct(s, buf, count); | 173 | return write_direct(s, buf, count); |
174 | } | 174 | } |
175 | 175 | ||
176 | #ifdef STREAM_DEBUG | 176 | #ifdef STREAM_DEBUG |
177 | STREAM_LOG(">>>>>>> send >>>>>>\n"); | 177 | STREAM_LOG(">>>>>>> send >>>>>>\n"); |
178 | STREAM_LOG_BUF(buf, count); | 178 | STREAM_LOG_BUF(buf, count); |
179 | STREAM_LOG("\n"); | 179 | STREAM_LOG("\n"); |
180 | STREAM_LOG(">>>>>>> end send >>>>>>\n"); | 180 | STREAM_LOG(">>>>>>> end send >>>>>>\n"); |
181 | #endif | 181 | #endif |
182 | 182 | ||
183 | return write_to_internal_buffer(s, buf, count); | 183 | return write_to_internal_buffer(s, buf, count); |
184 | } | 184 | } |
185 | 185 | ||
186 | int mailstream_flush(mailstream * s) | 186 | int mailstream_flush(mailstream * s) |
187 | { | 187 | { |
188 | char * cur_buf; | 188 | char * cur_buf; |
189 | size_t left; | 189 | size_t left; |
190 | ssize_t written; | 190 | ssize_t written; |
191 | 191 | ||
192 | if (s == NULL) | 192 | if (s == NULL) |
193 | return -1; | 193 | return -1; |
194 | 194 | ||
195 | cur_buf = s->write_buffer; | 195 | cur_buf = s->write_buffer; |
196 | left = s->write_buffer_len; | 196 | left = s->write_buffer_len; |
197 | while (left > 0) { | 197 | while (left > 0) { |
198 | written = mailstream_low_write(s->low, cur_buf, left); | 198 | written = mailstream_low_write(s->low, cur_buf, left); |
199 | 199 | ||
200 | if (written == -1) | 200 | if (written == -1) |
201 | goto move_buffer; | 201 | goto move_buffer; |
202 | cur_buf += written; | 202 | cur_buf += written; |
203 | left -= written; | 203 | left -= written; |
204 | } | 204 | } |
205 | 205 | ||
206 | s->write_buffer_len = 0; | 206 | s->write_buffer_len = 0; |
207 | 207 | ||
208 | return 0; | 208 | return 0; |
209 | 209 | ||
210 | move_buffer: | 210 | move_buffer: |
211 | memmove(s->write_buffer, cur_buf, left); | 211 | memmove(s->write_buffer, cur_buf, left); |
212 | s->write_buffer_len = left; | 212 | s->write_buffer_len = left; |
213 | return -1; | 213 | return -1; |
214 | } | 214 | } |
215 | 215 | ||
216 | static ssize_t read_from_internal_buffer(mailstream * s, | 216 | static ssize_t read_from_internal_buffer(mailstream * s, |
217 | void * buf, size_t count) | 217 | void * buf, size_t count) |
218 | { | 218 | { |
219 | if (count >= s->read_buffer_len) | 219 | if (count >= s->read_buffer_len) |
220 | count = s->read_buffer_len; | 220 | count = s->read_buffer_len; |
221 | if (count != 0) | 221 | if (count != 0) |
222 | memcpy(buf, s->read_buffer, count); | 222 | memcpy(buf, s->read_buffer, count); |
223 | 223 | ||
224 | s->read_buffer_len -= count; | 224 | s->read_buffer_len -= count; |
225 | if (s->read_buffer_len != 0) | 225 | if (s->read_buffer_len != 0) |
226 | memmove(s->read_buffer, s->read_buffer + count, | 226 | memmove(s->read_buffer, s->read_buffer + count, |
227 | s->read_buffer_len); | 227 | s->read_buffer_len); |
228 | 228 | ||
229 | return count; | 229 | return count; |
230 | } | 230 | } |
231 | 231 | ||
232 | static ssize_t read_through_buffer(mailstream * s, void * buf, size_t count) | 232 | static ssize_t read_through_buffer(mailstream * s, void * buf, size_t count) |
233 | { | 233 | { |
234 | size_t left; | 234 | size_t left; |
235 | char * cur_buf; | 235 | char * cur_buf; |
236 | ssize_t bytes_read; | 236 | ssize_t bytes_read; |
237 | 237 | ||
238 | cur_buf = buf; | 238 | cur_buf = buf; |
239 | left = count; | 239 | left = count; |
240 | 240 | ||
241 | while (left > 0) { | 241 | while (left > 0) { |
242 | bytes_read = mailstream_low_read(s->low, cur_buf, left); | 242 | bytes_read = mailstream_low_read(s->low, cur_buf, left); |
243 | 243 | ||
244 | if (bytes_read == -1) { | 244 | if (bytes_read == -1) { |
245 | if (count == left) | 245 | if (count == left) |
246 | return -1; | 246 | return -1; |
247 | else | 247 | else |
248 | return count - left; | 248 | return count - left; |
249 | } | 249 | } |
250 | else if (bytes_read == 0) | 250 | else if (bytes_read == 0) |
251 | return count - left; | 251 | return count - left; |
252 | 252 | ||
253 | cur_buf += bytes_read; | 253 | cur_buf += bytes_read; |
254 | left -= bytes_read; | 254 | left -= bytes_read; |
255 | } | 255 | } |
256 | 256 | ||
257 | return count; | 257 | return count; |
258 | } | 258 | } |
259 | 259 | ||
260 | ssize_t mailstream_read(mailstream * s, void * buf, size_t count) | 260 | ssize_t mailstream_read(mailstream * s, void * buf, size_t count) |
261 | { | 261 | { |
262 | ssize_t read_bytes; | 262 | ssize_t read_bytes; |
263 | char * cur_buf; | 263 | char * cur_buf; |
264 | size_t left; | 264 | size_t left; |
265 | 265 | ||
266 | if (s == NULL) | 266 | if (s == NULL) |
267 | return -1; | 267 | return -1; |
268 | 268 | ||
269 | left = count; | 269 | left = count; |
270 | cur_buf = buf; | 270 | cur_buf = buf; |
271 | read_bytes = read_from_internal_buffer(s, cur_buf, left); | 271 | read_bytes = read_from_internal_buffer(s, cur_buf, left); |
272 | cur_buf += read_bytes; | 272 | cur_buf += read_bytes; |
273 | left -= read_bytes; | 273 | left -= read_bytes; |
274 | 274 | ||
275 | if (left == 0) { | 275 | if (left == 0) { |
276 | #ifdef STREAM_DEBUG | 276 | #ifdef STREAM_DEBUG |
277 | STREAM_LOG("<<<<<<< read <<<<<<\n"); | 277 | STREAM_LOG("<<<<<<< read <<<<<<\n"); |
278 | STREAM_LOG_BUF(buf, read_bytes); | 278 | STREAM_LOG_BUF(buf, read_bytes); |
279 | STREAM_LOG("\n"); | 279 | STREAM_LOG("\n"); |
280 | STREAM_LOG("<<<<<<< end read <<<<<<\n"); | 280 | STREAM_LOG("<<<<<<< end read <<<<<<\n"); |
281 | #endif | 281 | #endif |
282 | 282 | ||
283 | return read_bytes; | 283 | return read_bytes; |
284 | } | 284 | } |
285 | 285 | ||
286 | if (left > s->buffer_max_size) { | 286 | if (left > s->buffer_max_size) { |
287 | read_bytes = read_through_buffer(s, cur_buf, left); | 287 | read_bytes = read_through_buffer(s, cur_buf, left); |
288 | if (read_bytes == -1) { | 288 | if (read_bytes == -1) { |
289 | if (count == left) | 289 | if (count == left) |
290 | return -1; | 290 | return -1; |
291 | else { | 291 | else { |
292 | 292 | ||
293 | #ifdef STREAM_DEBUG | 293 | #ifdef STREAM_DEBUG |
294 | STREAM_LOG("<<<<<<< read <<<<<<\n"); | 294 | STREAM_LOG("<<<<<<< read <<<<<<\n"); |
295 | STREAM_LOG_BUF(buf, count - left); | 295 | STREAM_LOG_BUF(buf, count - left); |
296 | STREAM_LOG("\n"); | 296 | STREAM_LOG("\n"); |
297 | STREAM_LOG("<<<<<<< end read <<<<<<\n"); | 297 | STREAM_LOG("<<<<<<< end read <<<<<<\n"); |
298 | #endif | 298 | #endif |
299 | 299 | ||
300 | return count - left; | 300 | return count - left; |
301 | } | 301 | } |
302 | } | 302 | } |
303 | 303 | ||
304 | cur_buf += read_bytes; | 304 | cur_buf += read_bytes; |
305 | left -= read_bytes; | 305 | left -= read_bytes; |
306 | 306 | ||
307 | #ifdef STREAM_DEBUG | 307 | #ifdef STREAM_DEBUG |
308 | STREAM_LOG("<<<<<<< read <<<<<<\n"); | 308 | STREAM_LOG("<<<<<<< read <<<<<<\n"); |
309 | STREAM_LOG_BUF(buf, count - left); | 309 | STREAM_LOG_BUF(buf, count - left); |
310 | STREAM_LOG("\n"); | 310 | STREAM_LOG("\n"); |
311 | STREAM_LOG("<<<<<<< end read <<<<<<\n"); | 311 | STREAM_LOG("<<<<<<< end read <<<<<<\n"); |
312 | #endif | 312 | #endif |
313 | 313 | ||
314 | return count - left; | 314 | return count - left; |
315 | } | 315 | } |
316 | 316 | ||
317 | read_bytes = mailstream_low_read(s->low, s->read_buffer, s->buffer_max_size); | 317 | read_bytes = mailstream_low_read(s->low, s->read_buffer, s->buffer_max_size); |
318 | if (read_bytes == -1) { | 318 | if (read_bytes == -1) { |
319 | if (left == count) | 319 | if (left == count) |
320 | return -1; | 320 | return -1; |
321 | else { | 321 | else { |
322 | #ifdef STREAM_DEBUG | 322 | #ifdef STREAM_DEBUG |
323 | STREAM_LOG("<<<<<<< read <<<<<<\n"); | 323 | STREAM_LOG("<<<<<<< read <<<<<<\n"); |
324 | STREAM_LOG_BUF(buf, count - left); | 324 | STREAM_LOG_BUF(buf, count - left); |
325 | STREAM_LOG("\n"); | 325 | STREAM_LOG("\n"); |
326 | STREAM_LOG("<<<<<<< end read <<<<<<\n"); | 326 | STREAM_LOG("<<<<<<< end read <<<<<<\n"); |
327 | #endif | 327 | #endif |
328 | 328 | ||
329 | return count - left; | 329 | return count - left; |
330 | } | 330 | } |
331 | } | 331 | } |
332 | else | 332 | else |
333 | s->read_buffer_len += read_bytes; | 333 | s->read_buffer_len += read_bytes; |
334 | 334 | ||
335 | read_bytes = read_from_internal_buffer(s, cur_buf, left); | 335 | read_bytes = read_from_internal_buffer(s, cur_buf, left); |
336 | cur_buf += read_bytes; | 336 | cur_buf += read_bytes; |
337 | left -= read_bytes; | 337 | left -= read_bytes; |
338 | 338 | ||
339 | #ifdef STREAM_DEBUG | 339 | #ifdef STREAM_DEBUG |
340 | STREAM_LOG("<<<<<<< read <<<<<<\n"); | 340 | STREAM_LOG("<<<<<<< read <<<<<<\n"); |
341 | STREAM_LOG_BUF(buf, count - left); | 341 | STREAM_LOG_BUF(buf, count - left); |
342 | STREAM_LOG("\n"); | 342 | STREAM_LOG("\n"); |
343 | STREAM_LOG("<<<<<<< end read <<<<<<\n"); | 343 | STREAM_LOG("<<<<<<< end read <<<<<<\n"); |
344 | #endif | 344 | #endif |
345 | 345 | ||
346 | return count - left; | 346 | return count - left; |
347 | } | 347 | } |
348 | 348 | ||
349 | mailstream_low * mailstream_get_low(mailstream * s) | 349 | mailstream_low * mailstream_get_low(mailstream * s) |
350 | { | 350 | { |
351 | return s->low; | 351 | return s->low; |
352 | } | 352 | } |
353 | 353 | ||
354 | void mailstream_set_low(mailstream * s, mailstream_low * low) | 354 | void mailstream_set_low(mailstream * s, mailstream_low * low) |
355 | { | 355 | { |
356 | s->low = low; | 356 | s->low = low; |
357 | } | 357 | } |
358 | 358 | ||
359 | int mailstream_close(mailstream * s) | 359 | int mailstream_close(mailstream * s) |
360 | { | 360 | { |
361 | mailstream_low_close(s->low); | 361 | mailstream_low_close(s->low); |
362 | mailstream_low_free(s->low); | 362 | mailstream_low_free(s->low); |
363 | 363 | ||
364 | free(s->read_buffer); | 364 | free(s->read_buffer); |
365 | free(s->write_buffer); | 365 | free(s->write_buffer); |
366 | 366 | ||
367 | free(s); | 367 | free(s); |
368 | 368 | ||
369 | return 0; | 369 | return 0; |
370 | } | 370 | } |
371 | 371 | ||
372 | 372 | ||
373 | 373 | ||
374 | ssize_t mailstream_feed_read_buffer(mailstream * s) | 374 | ssize_t mailstream_feed_read_buffer(mailstream * s) |
375 | { | 375 | { |
376 | ssize_t read_bytes; | 376 | ssize_t read_bytes; |
377 | 377 | ||
378 | if (s == NULL) | 378 | if (s == NULL) |
379 | return -1; | 379 | return -1; |
380 | 380 | ||
381 | if (s->read_buffer_len == 0) { | 381 | if (s->read_buffer_len == 0) { |
382 | read_bytes = mailstream_low_read(s->low, s->read_buffer, | 382 | read_bytes = mailstream_low_read(s->low, s->read_buffer, |
383 | s->buffer_max_size); | 383 | s->buffer_max_size); |
384 | if (read_bytes == -1) | 384 | if (read_bytes == -1) |
385 | return -1; | 385 | return -1; |
386 | s->read_buffer_len += read_bytes; | 386 | s->read_buffer_len += read_bytes; |
387 | } | 387 | } |
388 | 388 | ||
389 | return s->read_buffer_len; | 389 | return s->read_buffer_len; |
390 | } | 390 | } |
391 | 391 | ||
392 | struct timeval mailstream_network_delay = | 392 | struct timeval mailstream_network_delay = |
393 | { .tv_sec = DEFAULT_NETWORK_TIMEOUT, .tv_usec = 0 }; | 393 | { .tv_sec = DEFAULT_NETWORK_TIMEOUT, .tv_usec = 0 }; |
394 | 394 | ||
diff --git a/kmicromail/libetpan/tools/mailstream.h b/kmicromail/libetpan/tools/mailstream.h index a4e35cd..8a89a72 100644 --- a/kmicromail/libetpan/tools/mailstream.h +++ b/kmicromail/libetpan/tools/mailstream.h | |||
@@ -1,73 +1,74 @@ | |||
1 | /* | 1 | /* |
2 | * libEtPan! -- a mail stuff library | 2 | * libEtPan! -- a mail stuff library |
3 | * | 3 | * |
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | 4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa |
5 | * All rights reserved. | 5 | * All rights reserved. |
6 | * | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | 14 | * documentation and/or other materials provided with the distribution. |
15 | * 3. Neither the name of the libEtPan! project nor the names of its | 15 | * 3. Neither the name of the libEtPan! project nor the names of its |
16 | * contributors may be used to endorse or promote products derived | 16 | * contributors may be used to endorse or promote products derived |
17 | * from this software without specific prior written permission. | 17 | * from this software without specific prior written permission. |
18 | * | 18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
29 | * SUCH DAMAGE. | 29 | * SUCH DAMAGE. |
30 | */ | 30 | */ |
31 | 31 | ||
32 | /* | 32 | /* |
33 | * $Id$ | 33 | * $Id$ |
34 | */ | 34 | */ |
35 | 35 | ||
36 | #ifndef MAILSTREAM_H | 36 | #ifndef MAILSTREAM_H |
37 | 37 | ||
38 | #define MAILSTREAM_H | 38 | #define MAILSTREAM_H |
39 | 39 | //dont forget to change the value in <libetpan/mailstream.h> as well | |
40 | #define DEFAULT_NETWORK_TIMEOUT 30 | ||
40 | #include <sys/time.h> | 41 | #include <sys/time.h> |
41 | 42 | ||
42 | #include <libetpan/mailstream_low.h> | 43 | #include <libetpan/mailstream_low.h> |
43 | #include <libetpan/mailstream_helper.h> | 44 | #include <libetpan/mailstream_helper.h> |
44 | #include <libetpan/mailstream_socket.h> | 45 | #include <libetpan/mailstream_socket.h> |
45 | #include <libetpan/mailstream_ssl.h> | 46 | #include <libetpan/mailstream_ssl.h> |
46 | #include <libetpan/mailstream_types.h> | 47 | #include <libetpan/mailstream_types.h> |
47 | 48 | ||
48 | #ifdef __cplusplus | 49 | #ifdef __cplusplus |
49 | extern "C" { | 50 | extern "C" { |
50 | #endif | 51 | #endif |
51 | 52 | ||
52 | mailstream * mailstream_new(mailstream_low * low, size_t buffer_size); | 53 | mailstream * mailstream_new(mailstream_low * low, size_t buffer_size); |
53 | ssize_t mailstream_write(mailstream * s, const void * buf, size_t count); | 54 | ssize_t mailstream_write(mailstream * s, const void * buf, size_t count); |
54 | ssize_t mailstream_read(mailstream * s, void * buf, size_t count); | 55 | ssize_t mailstream_read(mailstream * s, void * buf, size_t count); |
55 | int mailstream_close(mailstream * s); | 56 | int mailstream_close(mailstream * s); |
56 | int mailstream_flush(mailstream * s); | 57 | int mailstream_flush(mailstream * s); |
57 | ssize_t mailstream_feed_read_buffer(mailstream * s); | 58 | ssize_t mailstream_feed_read_buffer(mailstream * s); |
58 | mailstream_low * mailstream_get_low(mailstream * s); | 59 | mailstream_low * mailstream_get_low(mailstream * s); |
59 | void mailstream_set_low(mailstream * s, mailstream_low * low); | 60 | void mailstream_set_low(mailstream * s, mailstream_low * low); |
60 | 61 | ||
61 | #ifdef LIBETPAN_MAILSTREAM_DEBUG | 62 | #ifdef LIBETPAN_MAILSTREAM_DEBUG |
62 | extern int mailstream_debug; | 63 | extern int mailstream_debug; |
63 | #endif | 64 | #endif |
64 | 65 | ||
65 | #define LIBETPAN_MAILSTREAM_NETWORK_DELAY | 66 | #define LIBETPAN_MAILSTREAM_NETWORK_DELAY |
66 | extern struct timeval mailstream_network_delay; | 67 | extern struct timeval mailstream_network_delay; |
67 | 68 | ||
68 | #ifdef __cplusplus | 69 | #ifdef __cplusplus |
69 | } | 70 | } |
70 | #endif | 71 | #endif |
71 | 72 | ||
72 | #endif | 73 | #endif |
73 | 74 | ||
diff --git a/kmicromail/libetpan/tools/mailstream_socket.c b/kmicromail/libetpan/tools/mailstream_socket.c index 29e50e1..04a6f48 100644 --- a/kmicromail/libetpan/tools/mailstream_socket.c +++ b/kmicromail/libetpan/tools/mailstream_socket.c | |||
@@ -1,239 +1,246 @@ | |||
1 | /* | 1 | /* |
2 | * libEtPan! -- a mail stuff library | 2 | * libEtPan! -- a mail stuff library |
3 | * | 3 | * |
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | 4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa |
5 | * All rights reserved. | 5 | * All rights reserved. |
6 | * | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | 14 | * documentation and/or other materials provided with the distribution. |
15 | * 3. Neither the name of the libEtPan! project nor the names of its | 15 | * 3. Neither the name of the libEtPan! project nor the names of its |
16 | * contributors may be used to endorse or promote products derived | 16 | * contributors may be used to endorse or promote products derived |
17 | * from this software without specific prior written permission. | 17 | * from this software without specific prior written permission. |
18 | * | 18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
29 | * SUCH DAMAGE. | 29 | * SUCH DAMAGE. |
30 | */ | 30 | */ |
31 | 31 | ||
32 | /* | 32 | /* |
33 | * $Id$ | 33 | * $Id$ |
34 | */ | 34 | */ |
35 | 35 | ||
36 | #include "mailstream_socket.h" | 36 | #include "mailstream_socket.h" |
37 | #include <unistd.h> | 37 | #include <unistd.h> |
38 | #include <stdlib.h> | 38 | #include <stdlib.h> |
39 | #include <fcntl.h> | 39 | #include <fcntl.h> |
40 | 40 | ||
41 | /* | 41 | /* |
42 | these 3 headers MUST be included before <sys/select.h> | 42 | these 3 headers MUST be included before <sys/select.h> |
43 | to insure compatibility with Mac OS X (this is true for 10.2) | 43 | to insure compatibility with Mac OS X (this is true for 10.2) |
44 | */ | 44 | */ |
45 | #include <sys/time.h> | 45 | #include <sys/time.h> |
46 | #include <sys/socket.h> | 46 | #include <sys/socket.h> |
47 | #include <sys/types.h> | 47 | #include <sys/types.h> |
48 | #include <unistd.h> | 48 | #include <unistd.h> |
49 | #include <sys/select.h> | 49 | #include <sys/select.h> |
50 | 50 | ||
51 | /* mailstream_low, socket */ | 51 | /* mailstream_low, socket */ |
52 | 52 | ||
53 | static int mailstream_low_socket_close(mailstream_low * s); | 53 | static int mailstream_low_socket_close(mailstream_low * s); |
54 | static ssize_t mailstream_low_socket_read(mailstream_low * s, | 54 | static ssize_t mailstream_low_socket_read(mailstream_low * s, |
55 | void * buf, size_t count); | 55 | void * buf, size_t count); |
56 | static ssize_t mailstream_low_socket_write(mailstream_low * s, | 56 | static ssize_t mailstream_low_socket_write(mailstream_low * s, |
57 | const void * buf, size_t count); | 57 | const void * buf, size_t count); |
58 | static void mailstream_low_socket_free(mailstream_low * s); | 58 | static void mailstream_low_socket_free(mailstream_low * s); |
59 | static int mailstream_low_socket_get_fd(mailstream_low * s); | 59 | static int mailstream_low_socket_get_fd(mailstream_low * s); |
60 | 60 | ||
61 | static mailstream_low_driver local_mailstream_socket_driver = { | 61 | static mailstream_low_driver local_mailstream_socket_driver = { |
62 | mailstream_read: mailstream_low_socket_read, | 62 | mailstream_read: mailstream_low_socket_read, |
63 | mailstream_write: mailstream_low_socket_write, | 63 | mailstream_write: mailstream_low_socket_write, |
64 | mailstream_close: mailstream_low_socket_close, | 64 | mailstream_close: mailstream_low_socket_close, |
65 | mailstream_free: mailstream_low_socket_free, | 65 | mailstream_free: mailstream_low_socket_free, |
66 | mailstream_get_fd: mailstream_low_socket_get_fd, | 66 | mailstream_get_fd: mailstream_low_socket_get_fd, |
67 | }; | 67 | }; |
68 | 68 | ||
69 | mailstream_low_driver * mailstream_socket_driver = | 69 | mailstream_low_driver * mailstream_socket_driver = |
70 | &local_mailstream_socket_driver; | 70 | &local_mailstream_socket_driver; |
71 | 71 | ||
72 | /* file descriptor must be given in (default) blocking-mode */ | 72 | /* file descriptor must be given in (default) blocking-mode */ |
73 | 73 | ||
74 | static struct mailstream_socket_data * socket_data_new(int fd) | 74 | static struct mailstream_socket_data * socket_data_new(int fd) |
75 | { | 75 | { |
76 | struct mailstream_socket_data * socket_data; | 76 | struct mailstream_socket_data * socket_data; |
77 | 77 | ||
78 | socket_data = malloc(sizeof(* socket_data)); | 78 | socket_data = malloc(sizeof(* socket_data)); |
79 | if (socket_data == NULL) | 79 | if (socket_data == NULL) |
80 | goto err; | 80 | goto err; |
81 | 81 | ||
82 | socket_data->fd = fd; | 82 | socket_data->fd = fd; |
83 | 83 | ||
84 | return socket_data; | 84 | return socket_data; |
85 | 85 | ||
86 | err: | 86 | err: |
87 | return NULL; | 87 | return NULL; |
88 | } | 88 | } |
89 | 89 | ||
90 | static void socket_data_free(struct mailstream_socket_data * socket_data) | 90 | static void socket_data_free(struct mailstream_socket_data * socket_data) |
91 | { | 91 | { |
92 | free(socket_data); | 92 | free(socket_data); |
93 | } | 93 | } |
94 | 94 | ||
95 | static void socket_data_close(struct mailstream_socket_data * socket_data) | 95 | static void socket_data_close(struct mailstream_socket_data * socket_data) |
96 | { | 96 | { |
97 | close(socket_data->fd); | 97 | close(socket_data->fd); |
98 | socket_data->fd = -1; | 98 | socket_data->fd = -1; |
99 | } | 99 | } |
100 | 100 | ||
101 | mailstream_low * mailstream_low_socket_open(int fd) | 101 | mailstream_low * mailstream_low_socket_open(int fd) |
102 | { | 102 | { |
103 | mailstream_low * s; | 103 | mailstream_low * s; |
104 | struct mailstream_socket_data * socket_data; | 104 | struct mailstream_socket_data * socket_data; |
105 | 105 | ||
106 | socket_data = socket_data_new(fd); | 106 | socket_data = socket_data_new(fd); |
107 | if (socket_data == NULL) | 107 | if (socket_data == NULL) |
108 | goto err; | 108 | goto err; |
109 | 109 | ||
110 | s = mailstream_low_new(socket_data, mailstream_socket_driver); | 110 | s = mailstream_low_new(socket_data, mailstream_socket_driver); |
111 | if (s == NULL) | 111 | if (s == NULL) |
112 | goto free_socket_data; | 112 | goto free_socket_data; |
113 | 113 | ||
114 | return s; | 114 | return s; |
115 | 115 | ||
116 | free_socket_data: | 116 | free_socket_data: |
117 | socket_data_free(socket_data); | 117 | socket_data_free(socket_data); |
118 | err: | 118 | err: |
119 | return NULL; | 119 | return NULL; |
120 | } | 120 | } |
121 | 121 | ||
122 | static int mailstream_low_socket_close(mailstream_low * s) | 122 | static int mailstream_low_socket_close(mailstream_low * s) |
123 | { | 123 | { |
124 | struct mailstream_socket_data * socket_data; | 124 | struct mailstream_socket_data * socket_data; |
125 | 125 | ||
126 | socket_data = (struct mailstream_socket_data *) s->data; | 126 | socket_data = (struct mailstream_socket_data *) s->data; |
127 | socket_data_close(socket_data); | 127 | socket_data_close(socket_data); |
128 | 128 | ||
129 | return 0; | 129 | return 0; |
130 | } | 130 | } |
131 | 131 | ||
132 | static void mailstream_low_socket_free(mailstream_low * s) | 132 | static void mailstream_low_socket_free(mailstream_low * s) |
133 | { | 133 | { |
134 | struct mailstream_socket_data * socket_data; | 134 | struct mailstream_socket_data * socket_data; |
135 | 135 | ||
136 | socket_data = (struct mailstream_socket_data *) s->data; | 136 | socket_data = (struct mailstream_socket_data *) s->data; |
137 | socket_data_free(socket_data); | 137 | socket_data_free(socket_data); |
138 | s->data = NULL; | 138 | s->data = NULL; |
139 | 139 | ||
140 | free(s); | 140 | free(s); |
141 | } | 141 | } |
142 | 142 | ||
143 | static int mailstream_low_socket_get_fd(mailstream_low * s) | 143 | static int mailstream_low_socket_get_fd(mailstream_low * s) |
144 | { | 144 | { |
145 | struct mailstream_socket_data * socket_data; | 145 | struct mailstream_socket_data * socket_data; |
146 | 146 | ||
147 | socket_data = (struct mailstream_socket_data *) s->data; | 147 | socket_data = (struct mailstream_socket_data *) s->data; |
148 | return socket_data->fd; | 148 | return socket_data->fd; |
149 | } | 149 | } |
150 | 150 | ||
151 | 151 | ||
152 | static ssize_t mailstream_low_socket_read(mailstream_low * s, | 152 | static ssize_t mailstream_low_socket_read(mailstream_low * s, |
153 | void * buf, size_t count) | 153 | void * buf, size_t count) |
154 | { | 154 | { |
155 | struct mailstream_socket_data * socket_data; | 155 | struct mailstream_socket_data * socket_data; |
156 | 156 | ||
157 | socket_data = (struct mailstream_socket_data *) s->data; | 157 | socket_data = (struct mailstream_socket_data *) s->data; |
158 | 158 | ||
159 | /* timeout */ | 159 | /* timeout */ |
160 | { | 160 | { |
161 | fd_set fds_read; | 161 | fd_set fds_read; |
162 | fd_set fds_excp; | 162 | fd_set fds_excp; |
163 | struct timeval timeout; | 163 | struct timeval timeout; |
164 | int r; | 164 | int r; |
165 | 165 | ||
166 | timeout = mailstream_network_delay; | 166 | timeout = mailstream_network_delay; |
167 | 167 | ||
168 | FD_ZERO(&fds_read); | 168 | FD_ZERO(&fds_read); |
169 | FD_SET(socket_data->fd, &fds_read); | 169 | FD_SET(socket_data->fd, &fds_read); |
170 | FD_ZERO(&fds_excp); | 170 | FD_ZERO(&fds_excp); |
171 | FD_SET(socket_data->fd, &fds_excp); | 171 | FD_SET(socket_data->fd, &fds_excp); |
172 | // LUTZ for safety I insert here a max val as well | ||
173 | if ( timeout.tv_sec > DEFAULT_NETWORK_TIMEOUT ) | ||
174 | timeout.tv_sec = DEFAULT_NETWORK_TIMEOUT; | ||
172 | r = select(socket_data->fd + 1, &fds_read, NULL, &fds_excp, &timeout); | 175 | r = select(socket_data->fd + 1, &fds_read, NULL, &fds_excp, &timeout); |
173 | if (r == 0) | 176 | if (r < 1 ) |
174 | return -1; | 177 | return -1; |
175 | if (FD_ISSET(socket_data->fd, &fds_excp)) | 178 | if (FD_ISSET(socket_data->fd, &fds_excp)) |
176 | return -1; | 179 | return -1; |
177 | if (!FD_ISSET(socket_data->fd, &fds_read)) | 180 | if (!FD_ISSET(socket_data->fd, &fds_read)) |
178 | return 0; | 181 | return 0; |
179 | } | 182 | } |
180 | return recv(socket_data->fd,buf,count,MSG_NOSIGNAL); | 183 | return recv(socket_data->fd,buf,count,MSG_NOSIGNAL); |
181 | //return read(socket_data->fd, buf, count); | 184 | //return read(socket_data->fd, buf, count); |
182 | } | 185 | } |
183 | 186 | #include <stdio.h> | |
184 | static ssize_t mailstream_low_socket_write(mailstream_low * s, | 187 | static ssize_t mailstream_low_socket_write(mailstream_low * s, |
185 | const void * buf, size_t count) | 188 | const void * buf, size_t count) |
186 | { | 189 | { |
187 | struct mailstream_socket_data * socket_data; | 190 | struct mailstream_socket_data * socket_data; |
188 | 191 | ||
189 | socket_data = (struct mailstream_socket_data *) s->data; | 192 | socket_data = (struct mailstream_socket_data *) s->data; |
190 | /* timeout */ | 193 | /* timeout */ |
191 | { | 194 | { |
192 | fd_set fds_write; | 195 | fd_set fds_write; |
193 | fd_set fds_excp; | 196 | fd_set fds_excp; |
194 | struct timeval timeout; | 197 | struct timeval timeout; |
195 | int r; | 198 | int r; |
196 | 199 | ||
197 | timeout = mailstream_network_delay; | 200 | timeout = mailstream_network_delay; |
198 | 201 | ||
199 | FD_ZERO(&fds_write); | 202 | FD_ZERO(&fds_write); |
200 | FD_SET(socket_data->fd, &fds_write); | 203 | FD_SET(socket_data->fd, &fds_write); |
201 | FD_ZERO(&fds_excp); | 204 | FD_ZERO(&fds_excp); |
202 | FD_SET(socket_data->fd, &fds_excp); | 205 | FD_SET(socket_data->fd, &fds_excp); |
206 | // LUTZ next line blocks sometimes | ||
207 | if ( timeout.tv_sec > DEFAULT_NETWORK_TIMEOUT ) | ||
208 | timeout.tv_sec = DEFAULT_NETWORK_TIMEOUT; | ||
209 | fprintf(stderr,"fd %d to secs %d \n", socket_data->fd, timeout.tv_sec ); | ||
203 | r = select(socket_data->fd + 1, NULL, &fds_write, &fds_excp, &timeout); | 210 | r = select(socket_data->fd + 1, NULL, &fds_write, &fds_excp, &timeout); |
204 | if (r == 0) | 211 | if (r < 1) |
205 | return -1; | 212 | return -1; |
206 | if (FD_ISSET(socket_data->fd, &fds_excp)) | 213 | if (FD_ISSET(socket_data->fd, &fds_excp)) |
207 | return -1; | 214 | return -1; |
208 | if (!FD_ISSET(socket_data->fd, &fds_write)) | 215 | if (!FD_ISSET(socket_data->fd, &fds_write)) |
209 | return 0; | 216 | return 0; |
210 | } | 217 | } |
211 | 218 | ||
212 | return send(socket_data->fd,buf,count,MSG_NOSIGNAL); | 219 | return send(socket_data->fd,buf,count,MSG_NOSIGNAL); |
213 | //return write(socket_data->fd, buf, count); | 220 | //return write(socket_data->fd, buf, count); |
214 | } | 221 | } |
215 | 222 | ||
216 | 223 | ||
217 | /* mailstream */ | 224 | /* mailstream */ |
218 | 225 | ||
219 | mailstream * mailstream_socket_open(int fd) | 226 | mailstream * mailstream_socket_open(int fd) |
220 | { | 227 | { |
221 | mailstream_low * low; | 228 | mailstream_low * low; |
222 | mailstream * s; | 229 | mailstream * s; |
223 | 230 | ||
224 | low = mailstream_low_socket_open(fd); | 231 | low = mailstream_low_socket_open(fd); |
225 | if (low == NULL) | 232 | if (low == NULL) |
226 | goto err; | 233 | goto err; |
227 | 234 | ||
228 | s = mailstream_new(low, 8192); | 235 | s = mailstream_new(low, 8192); |
229 | if (s == NULL) | 236 | if (s == NULL) |
230 | goto free_low; | 237 | goto free_low; |
231 | 238 | ||
232 | return s; | 239 | return s; |
233 | 240 | ||
234 | free_low: | 241 | free_low: |
235 | mailstream_low_close(low); | 242 | mailstream_low_close(low); |
236 | err: | 243 | err: |
237 | return NULL; | 244 | return NULL; |
238 | } | 245 | } |
239 | 246 | ||