Closing follow by a reopen was not done right.
Various little cleanup. No joke the pop client need a real good cleanup. I'll do that later.
Showing
2 changed files
with
58 additions
and
63 deletions
... | @@ -86,7 +86,7 @@ static int mailbox_pop_body_size (body_t, size_t *psize); | ... | @@ -86,7 +86,7 @@ static int mailbox_pop_body_size (body_t, size_t *psize); |
86 | struct _bio | 86 | struct _bio |
87 | { | 87 | { |
88 | #define POP_BUFSIZ 512 | 88 | #define POP_BUFSIZ 512 |
89 | int fd; | 89 | stream_t stream; |
90 | size_t maxlen; | 90 | size_t maxlen; |
91 | size_t len; | 91 | size_t len; |
92 | char *current; | 92 | char *current; |
... | @@ -97,7 +97,7 @@ struct _bio | ... | @@ -97,7 +97,7 @@ struct _bio |
97 | 97 | ||
98 | typedef struct _bio *bio_t; | 98 | typedef struct _bio *bio_t; |
99 | 99 | ||
100 | static int bio_create (bio_t *, int); | 100 | static int bio_create (bio_t *, stream_t); |
101 | static void bio_destroy (bio_t *); | 101 | static void bio_destroy (bio_t *); |
102 | static int bio_readline (bio_t); | 102 | static int bio_readline (bio_t); |
103 | static int bio_read (bio_t); | 103 | static int bio_read (bio_t); |
... | @@ -132,7 +132,6 @@ struct _mailbox_pop_data | ... | @@ -132,7 +132,6 @@ struct _mailbox_pop_data |
132 | pthread_mutex_t mutex; | 132 | pthread_mutex_t mutex; |
133 | #endif | 133 | #endif |
134 | int flags; | 134 | int flags; |
135 | int fd; | ||
136 | bio_t bio; | 135 | bio_t bio; |
137 | int is_updated; | 136 | int is_updated; |
138 | char *user; | 137 | char *user; |
... | @@ -185,7 +184,7 @@ mailbox_pop_create (mailbox_t *pmbox, const char *name) | ... | @@ -185,7 +184,7 @@ mailbox_pop_create (mailbox_t *pmbox, const char *name) |
185 | } | 184 | } |
186 | 185 | ||
187 | /* Allocate the struct for buffered I/O. */ | 186 | /* Allocate the struct for buffered I/O. */ |
188 | status = bio_create (&(mpd->bio), -1); | 187 | status = bio_create (&(mpd->bio), NULL); |
189 | if (status != 0) | 188 | if (status != 0) |
190 | { | 189 | { |
191 | mailbox_pop_destroy (&mbox); | 190 | mailbox_pop_destroy (&mbox); |
... | @@ -235,9 +234,10 @@ mailbox_pop_destroy (mailbox_t *pmbox) | ... | @@ -235,9 +234,10 @@ mailbox_pop_destroy (mailbox_t *pmbox) |
235 | { | 234 | { |
236 | if (pmbox && *pmbox) | 235 | if (pmbox && *pmbox) |
237 | { | 236 | { |
238 | if ((*pmbox)->data) | 237 | mailbox_t mbox = *pmbox; |
238 | if (mbox->data) | ||
239 | { | 239 | { |
240 | mailbox_pop_data_t mpd = (*pmbox)->data; | 240 | mailbox_pop_data_t mpd = mbox->data; |
241 | size_t i; | 241 | size_t i; |
242 | for (i = 0; i < mpd->pmessages_count; i++) | 242 | for (i = 0; i < mpd->pmessages_count; i++) |
243 | { | 243 | { |
... | @@ -252,10 +252,12 @@ mailbox_pop_destroy (mailbox_t *pmbox) | ... | @@ -252,10 +252,12 @@ mailbox_pop_destroy (mailbox_t *pmbox) |
252 | free (mpd->pmessages); | 252 | free (mpd->pmessages); |
253 | free (mpd); | 253 | free (mpd); |
254 | } | 254 | } |
255 | free ((*pmbox)->name); | 255 | free (mbox->name); |
256 | free ((*pmbox)->event); | 256 | free (mbox->event); |
257 | if ((*pmbox)->url) | 257 | stream_destroy (&(mbox->stream), mbox); |
258 | url_destroy (&((*pmbox)->url)); | 258 | auth_destroy (&(mbox->auth), mbox); |
259 | if (mbox->url) | ||
260 | url_destroy (&(mbox->url)); | ||
259 | *pmbox = NULL; | 261 | *pmbox = NULL; |
260 | } | 262 | } |
261 | } | 263 | } |
... | @@ -324,7 +326,6 @@ mailbox_pop_open (mailbox_t mbox, int flags) | ... | @@ -324,7 +326,6 @@ mailbox_pop_open (mailbox_t mbox, int flags) |
324 | int status; | 326 | int status; |
325 | bio_t bio; | 327 | bio_t bio; |
326 | void *func = (void *)mailbox_pop_open; | 328 | void *func = (void *)mailbox_pop_open; |
327 | int fd; | ||
328 | char host[256] ; | 329 | char host[256] ; |
329 | long port; | 330 | long port; |
330 | 331 | ||
... | @@ -333,16 +334,25 @@ mailbox_pop_open (mailbox_t mbox, int flags) | ... | @@ -333,16 +334,25 @@ mailbox_pop_open (mailbox_t mbox, int flags) |
333 | return EINVAL; | 334 | return EINVAL; |
334 | 335 | ||
335 | /* Create the networking stack. */ | 336 | /* Create the networking stack. */ |
336 | if (!mbox->stream) | 337 | if (mbox->stream == NULL) |
337 | { | 338 | { |
339 | status = tcp_stream_create (&(mbox->stream)); | ||
340 | if (status != 0) | ||
341 | return status; | ||
342 | } | ||
343 | |||
338 | if ((status = url_get_host (mbox->url, host, sizeof(host), NULL)) != 0 || | 344 | if ((status = url_get_host (mbox->url, host, sizeof(host), NULL)) != 0 || |
339 | (status = url_get_port (mbox->url, &port)) != 0 || | 345 | (status = url_get_port (mbox->url, &port)) != 0) |
340 | (status = tcp_stream_create (&(mbox->stream))) != 0) | 346 | return status; |
347 | |||
348 | /* Dealing whith Authentication. */ | ||
349 | /* So far only normal user/pass supported. */ | ||
350 | if (mbox->auth == NULL) | ||
341 | { | 351 | { |
342 | mpd->func = NULL; | 352 | status = auth_create (&(mbox->auth), mbox); |
343 | mpd->state = 0; | 353 | if (status != 0) |
344 | return status; | 354 | return status; |
345 | } | 355 | auth_set_authenticate (mbox->auth, pop_authenticate, mbox); |
346 | } | 356 | } |
347 | 357 | ||
348 | /* Flag busy. */ | 358 | /* Flag busy. */ |
... | @@ -351,15 +361,14 @@ mailbox_pop_open (mailbox_t mbox, int flags) | ... | @@ -351,15 +361,14 @@ mailbox_pop_open (mailbox_t mbox, int flags) |
351 | mpd->func = func; | 361 | mpd->func = func; |
352 | bio = mpd->bio; | 362 | bio = mpd->bio; |
353 | 363 | ||
354 | /* Spawn the prologue. */ | ||
355 | if (mbox->auth) | ||
356 | auth_prologue (mbox->auth); | ||
357 | |||
358 | /* Enter the state machine. */ | 364 | /* Enter the state machine. */ |
359 | switch (mpd->state) | 365 | switch (mpd->state) |
360 | { | 366 | { |
361 | /* Establish the connection. */ | 367 | /* Establish the connection. */ |
362 | case 0: | 368 | case 0: |
369 | /* Spawn auth prologue. */ | ||
370 | auth_prologue (mbox->auth); | ||
371 | |||
363 | status = stream_open (mbox->stream, host, port, flags); | 372 | status = stream_open (mbox->stream, host, port, flags); |
364 | if (status != 0) | 373 | if (status != 0) |
365 | { | 374 | { |
... | @@ -370,9 +379,10 @@ mailbox_pop_open (mailbox_t mbox, int flags) | ... | @@ -370,9 +379,10 @@ mailbox_pop_open (mailbox_t mbox, int flags) |
370 | } | 379 | } |
371 | return status; | 380 | return status; |
372 | } | 381 | } |
373 | /* Get the fd. */ | 382 | /* Set the Buffer I/O. */ |
374 | stream_get_fd (mbox->stream, &fd); | 383 | bio->stream = mbox->stream; |
375 | mpd->bio->fd = mpd->fd = fd; | 384 | bio->ptr = bio->buffer; |
385 | bio->nl = NULL; | ||
376 | mpd->state = 1; | 386 | mpd->state = 1; |
377 | /* Glob the greetings. */ | 387 | /* Glob the greetings. */ |
378 | case 1: | 388 | case 1: |
... | @@ -390,23 +400,10 @@ mailbox_pop_open (mailbox_t mbox, int flags) | ... | @@ -390,23 +400,10 @@ mailbox_pop_open (mailbox_t mbox, int flags) |
390 | { | 400 | { |
391 | mpd->func = NULL; | 401 | mpd->func = NULL; |
392 | mpd->state = 0; | 402 | mpd->state = 0; |
393 | close (mpd->fd); | 403 | stream_close (bio->stream); |
394 | mpd->bio->fd = -1; | 404 | bio->stream = NULL; |
395 | return EACCES; | 405 | return EACCES; |
396 | } | 406 | } |
397 | /* Dealing whith Authentication. */ | ||
398 | /* So far only normal user/pass supported. */ | ||
399 | if (mbox->auth == NULL) | ||
400 | { | ||
401 | status = auth_create (&(mbox->auth), mbox); | ||
402 | if (status != 0) | ||
403 | { | ||
404 | mpd->func = NULL; | ||
405 | mpd->state = 0; | ||
406 | return status; | ||
407 | } | ||
408 | auth_set_authenticate (mbox->auth, pop_authenticate, mbox); | ||
409 | } | ||
410 | 407 | ||
411 | auth_authenticate (mbox->auth, &mpd->user, &mpd->passwd); | 408 | auth_authenticate (mbox->auth, &mpd->user, &mpd->passwd); |
412 | /* FIXME: Use snprintf. */ | 409 | /* FIXME: Use snprintf. */ |
... | @@ -483,11 +480,10 @@ mailbox_pop_open (mailbox_t mbox, int flags) | ... | @@ -483,11 +480,10 @@ mailbox_pop_open (mailbox_t mbox, int flags) |
483 | }/* Swith state. */ | 480 | }/* Swith state. */ |
484 | 481 | ||
485 | /* Spawn cleanup functions. */ | 482 | /* Spawn cleanup functions. */ |
486 | if (mbox->auth) | ||
487 | auth_epilogue (mbox->auth); | 483 | auth_epilogue (mbox->auth); |
488 | 484 | ||
489 | /* Clear any state. */ | 485 | /* Clear any state. */ |
490 | mpd->func = NULL; | 486 | mpd->id = mpd->func = NULL; |
491 | mpd->state = 0; | 487 | mpd->state = 0; |
492 | 488 | ||
493 | return 0; | 489 | return 0; |
... | @@ -510,7 +506,7 @@ mailbox_pop_close (mailbox_t mbox) | ... | @@ -510,7 +506,7 @@ mailbox_pop_close (mailbox_t mbox) |
510 | mpd->func = func; | 506 | mpd->func = func; |
511 | bio = mpd->bio; | 507 | bio = mpd->bio; |
512 | 508 | ||
513 | if (mpd->fd != -1) | 509 | if (bio->stream != NULL) |
514 | { | 510 | { |
515 | switch (mpd->state) | 511 | switch (mpd->state) |
516 | { | 512 | { |
... | @@ -545,8 +541,8 @@ mailbox_pop_close (mailbox_t mbox) | ... | @@ -545,8 +541,8 @@ mailbox_pop_close (mailbox_t mbox) |
545 | if (strncasecmp (bio->buffer, "+OK", 3) != 0) | 541 | if (strncasecmp (bio->buffer, "+OK", 3) != 0) |
546 | return EINVAL; | 542 | return EINVAL; |
547 | case 3: | 543 | case 3: |
548 | close (mpd->fd); | 544 | stream_close (bio->stream); |
549 | mpd->fd = -1; | 545 | bio->stream = NULL; |
550 | } | 546 | } |
551 | } | 547 | } |
552 | 548 | ||
... | @@ -767,8 +763,8 @@ mailbox_pop_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg) | ... | @@ -767,8 +763,8 @@ mailbox_pop_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg) |
767 | message_set_header ((mpd->mpm->message), header, mpd->mpm); | 763 | message_set_header ((mpd->mpm->message), header, mpd->mpm); |
768 | } | 764 | } |
769 | 765 | ||
770 | /* Reallocate the working buffer. */ | 766 | /* Reallocate the working I/O buffer. */ |
771 | bio_create (&(mpd->mpm->bio), mpd->fd); | 767 | bio_create (&(mpd->mpm->bio), bio->stream); |
772 | 768 | ||
773 | /* Create the attribute. */ | 769 | /* Create the attribute. */ |
774 | { | 770 | { |
... | @@ -1074,9 +1070,7 @@ mailbox_pop_getfd (stream_t stream, int *pfd) | ... | @@ -1074,9 +1070,7 @@ mailbox_pop_getfd (stream_t stream, int *pfd) |
1074 | mailbox_pop_message_t mpm; | 1070 | mailbox_pop_message_t mpm; |
1075 | if (stream == NULL || (mpm = stream->owner) == NULL) | 1071 | if (stream == NULL || (mpm = stream->owner) == NULL) |
1076 | return EINVAL; | 1072 | return EINVAL; |
1077 | if (pfd) | 1073 | return stream_get_fd (mpm->bio->stream, pfd); |
1078 | *pfd = mpm->bio->fd; | ||
1079 | return 0; | ||
1080 | } | 1074 | } |
1081 | 1075 | ||
1082 | static int | 1076 | static int |
... | @@ -1220,7 +1214,7 @@ mailbox_pop_readstream (stream_t is, char *buffer, size_t buflen, | ... | @@ -1220,7 +1214,7 @@ mailbox_pop_readstream (stream_t is, char *buffer, size_t buflen, |
1220 | } | 1214 | } |
1221 | 1215 | ||
1222 | static int | 1216 | static int |
1223 | bio_create (bio_t *pbio, int fd) | 1217 | bio_create (bio_t *pbio, stream_t stream) |
1224 | { | 1218 | { |
1225 | bio_t bio; | 1219 | bio_t bio; |
1226 | bio = calloc (1, sizeof (*bio)); | 1220 | bio = calloc (1, sizeof (*bio)); |
... | @@ -1233,7 +1227,7 @@ bio_create (bio_t *pbio, int fd) | ... | @@ -1233,7 +1227,7 @@ bio_create (bio_t *pbio, int fd) |
1233 | free (bio); | 1227 | free (bio); |
1234 | return ENOMEM; | 1228 | return ENOMEM; |
1235 | } | 1229 | } |
1236 | bio->fd = fd; | 1230 | bio->stream = stream; |
1237 | *pbio = bio; | 1231 | *pbio = bio; |
1238 | return 0; | 1232 | return 0; |
1239 | } | 1233 | } |
... | @@ -1253,13 +1247,14 @@ bio_destroy (bio_t *pbio) | ... | @@ -1253,13 +1247,14 @@ bio_destroy (bio_t *pbio) |
1253 | static int | 1247 | static int |
1254 | bio_write (bio_t bio) | 1248 | bio_write (bio_t bio) |
1255 | { | 1249 | { |
1256 | int nwritten; | 1250 | size_t nwritten = 0; |
1251 | int status; | ||
1257 | 1252 | ||
1258 | while (bio->len > 0) | 1253 | while (bio->len > 0) |
1259 | { | 1254 | { |
1260 | nwritten = write (bio->fd, bio->ptr, bio->len); | 1255 | status = stream_write (bio->stream, bio->ptr, bio->len, 0, &nwritten); |
1261 | if (nwritten < 0) | 1256 | if (status != 0) |
1262 | return errno; | 1257 | return status; |
1263 | bio->len -= nwritten; | 1258 | bio->len -= nwritten; |
1264 | bio->ptr += nwritten; | 1259 | bio->ptr += nwritten; |
1265 | } | 1260 | } |
... | @@ -1271,12 +1266,13 @@ bio_write (bio_t bio) | ... | @@ -1271,12 +1266,13 @@ bio_write (bio_t bio) |
1271 | static int | 1266 | static int |
1272 | bio_read (bio_t bio) | 1267 | bio_read (bio_t bio) |
1273 | { | 1268 | { |
1274 | int nread, len; | 1269 | size_t nread = 0; |
1270 | int status, len; | ||
1275 | 1271 | ||
1276 | len = bio->maxlen - (bio->ptr - bio->buffer) - 1; | 1272 | len = bio->maxlen - (bio->ptr - bio->buffer) - 1; |
1277 | nread = read (bio->fd, bio->ptr, len); | 1273 | status = stream_read (bio->stream, bio->ptr, len, 0, &nread); |
1278 | if (nread < 0) | 1274 | if (status != 0) |
1279 | return errno; | 1275 | return status; |
1280 | else if (nread == 0) | 1276 | else if (nread == 0) |
1281 | { /* EOF ???? We got a situation here ??? */ | 1277 | { /* EOF ???? We got a situation here ??? */ |
1282 | bio->buffer[0] = '.'; | 1278 | bio->buffer[0] = '.'; | ... | ... |
... | @@ -333,9 +333,9 @@ mailbox_unix_destroy (mailbox_t *pmbox) | ... | @@ -333,9 +333,9 @@ mailbox_unix_destroy (mailbox_t *pmbox) |
333 | if (mbox->locker) | 333 | if (mbox->locker) |
334 | locker_destroy (&(mbox->locker)); | 334 | locker_destroy (&(mbox->locker)); |
335 | if (mbox->auth) | 335 | if (mbox->auth) |
336 | auth_destroy (&(mbox->auth), (*pmbox)); | 336 | auth_destroy (&(mbox->auth), mbox); |
337 | if(mbox->stream) | 337 | if(mbox->stream) |
338 | stream_destroy (&(mbox->stream), *pmbox); | 338 | stream_destroy (&(mbox->stream), mbox); |
339 | mailbox_unix_iunlock (mbox); | 339 | mailbox_unix_iunlock (mbox); |
340 | #ifdef HAVE_PTHREAD_H | 340 | #ifdef HAVE_PTHREAD_H |
341 | if (mbox->mutex) | 341 | if (mbox->mutex) |
... | @@ -879,8 +879,7 @@ mailbox_unix_getfd (stream_t is, int *pfd) | ... | @@ -879,8 +879,7 @@ mailbox_unix_getfd (stream_t is, int *pfd) |
879 | if (is == NULL || (mum = is->owner) == NULL) | 879 | if (is == NULL || (mum = is->owner) == NULL) |
880 | return EINVAL; | 880 | return EINVAL; |
881 | 881 | ||
882 | stream_get_fd (mum->stream, pfd); | 882 | return stream_get_fd (mum->stream, pfd); |
883 | return 0; | ||
884 | } | 883 | } |
885 | 884 | ||
886 | static int | 885 | static int | ... | ... |
-
Please register or sign in to post a comment