Browse Source

support for CFNetwork, helpers for IMAP IDLE

dvh-chacham15-master
DINH Viet Hoa 11 years ago
parent
commit
5f535b03ce
20 changed files with 1576 additions and 9 deletions
  1. +26
    -2
      build-mac/libetpan.xcodeproj/project.pbxproj
  2. +2
    -1
      src/data-types/Makefile.am
  3. +125
    -2
      src/data-types/mailstream.c
  4. +8
    -0
      src/data-types/mailstream.h
  5. +1001
    -0
      src/data-types/mailstream_cfstream.c
  6. +93
    -0
      src/data-types/mailstream_cfstream.h
  7. +7
    -0
      src/data-types/mailstream_low.c
  8. +3
    -0
      src/data-types/mailstream_low.h
  9. +10
    -0
      src/data-types/mailstream_socket.c
  10. +13
    -0
      src/data-types/mailstream_ssl.c
  11. +12
    -0
      src/data-types/mailstream_types.h
  12. +48
    -1
      src/low-level/imap/mailimap_socket.c
  13. +29
    -0
      src/low-level/imap/mailimap_ssl.c
  14. +20
    -1
      src/low-level/nntp/newsnntp_socket.c
  15. +1
    -0
      src/low-level/nntp/newsnntp_socket.h
  16. +28
    -0
      src/low-level/nntp/newsnntp_ssl.c
  17. +47
    -1
      src/low-level/pop3/mailpop3_socket.c
  18. +28
    -0
      src/low-level/pop3/mailpop3_ssl.c
  19. +45
    -1
      src/low-level/smtp/mailsmtp_socket.c
  20. +30
    -0
      src/low-level/smtp/mailsmtp_ssl.c

+ 26
- 2
build-mac/libetpan.xcodeproj/project.pbxproj View File

@ -509,6 +509,11 @@
C6E572131103770400897274 /* quota_sender.h in Headers */ = {isa = PBXBuildFile; fileRef = C6E571FD110376F200897274 /* quota_sender.h */; };
C6E572141103770400897274 /* quota_types.c in Sources */ = {isa = PBXBuildFile; fileRef = C6E571FE110376F200897274 /* quota_types.c */; };
C6E572151103770400897274 /* quota_types.h in Headers */ = {isa = PBXBuildFile; fileRef = C6E571FF110376F200897274 /* quota_types.h */; };
C6EFB8781433F1F300F805C0 /* mailstream_cfstream.c in Sources */ = {isa = PBXBuildFile; fileRef = C6EFB8761433F1F300F805C0 /* mailstream_cfstream.c */; };
C6EFB8791433F1F300F805C0 /* mailstream_cfstream.h in Headers */ = {isa = PBXBuildFile; fileRef = C6EFB8771433F1F300F805C0 /* mailstream_cfstream.h */; };
C6EFB87A1433F1F300F805C0 /* mailstream_cfstream.c in Sources */ = {isa = PBXBuildFile; fileRef = C6EFB8761433F1F300F805C0 /* mailstream_cfstream.c */; };
C6EFB8AE1433F58500F805C0 /* CoreFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C6EFB8AD1433F58500F805C0 /* CoreFoundation.framework */; };
C6EFB8B41433F5A400F805C0 /* CoreServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C6EFB8B31433F5A400F805C0 /* CoreServices.framework */; };
C6F9EAFB105335BD0059C3BA /* base64.c in Sources */ = {isa = PBXBuildFile; fileRef = C6F9E84F105335BC0059C3BA /* base64.c */; };
C6F9EAFD105335BD0059C3BA /* carray.c in Sources */ = {isa = PBXBuildFile; fileRef = C6F9E851105335BC0059C3BA /* carray.c */; };
C6F9EAFF105335BD0059C3BA /* charconv.c in Sources */ = {isa = PBXBuildFile; fileRef = C6F9E853105335BC0059C3BA /* charconv.c */; };
@ -848,6 +853,10 @@
C6E571FF110376F200897274 /* quota_types.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = quota_types.h; sourceTree = "<group>"; };
C6E57200110376F200897274 /* quota.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = quota.c; sourceTree = "<group>"; };
C6E57201110376F200897274 /* quota.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = quota.h; sourceTree = "<group>"; };
C6EFB8761433F1F300F805C0 /* mailstream_cfstream.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = mailstream_cfstream.c; sourceTree = "<group>"; };
C6EFB8771433F1F300F805C0 /* mailstream_cfstream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = mailstream_cfstream.h; sourceTree = "<group>"; };
C6EFB8AD1433F58500F805C0 /* CoreFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreFoundation.framework; path = System/Library/Frameworks/CoreFoundation.framework; sourceTree = SDKROOT; };
C6EFB8B31433F5A400F805C0 /* CoreServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreServices.framework; path = System/Library/Frameworks/CoreServices.framework; sourceTree = SDKROOT; };
C6F9E84F105335BC0059C3BA /* base64.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = base64.c; sourceTree = "<group>"; };
C6F9E850105335BC0059C3BA /* base64.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = base64.h; sourceTree = "<group>"; };
C6F9E851105335BC0059C3BA /* carray.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = carray.c; sourceTree = "<group>"; };
@ -1195,6 +1204,8 @@
C6F9EDAD1053395A0059C3BA /* libexpat.dylib in Frameworks */,
C6F9EDB2105339650059C3BA /* libcurl.dylib in Frameworks */,
C6F9EDBE105339EA0059C3BA /* libcrypto.dylib in Frameworks */,
C6EFB8AE1433F58500F805C0 /* CoreFoundation.framework in Frameworks */,
C6EFB8B41433F5A400F805C0 /* CoreServices.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -1233,6 +1244,8 @@
isa = PBXGroup;
children = (
C6F9ED9A105338F60059C3BA /* Libraries */,
C6EFB8AD1433F58500F805C0 /* CoreFoundation.framework */,
C6EFB8B31433F5A400F805C0 /* CoreServices.framework */,
);
name = "External Frameworks and Libraries";
sourceTree = "<group>";
@ -1467,6 +1480,8 @@
C6F9E868105335BC0059C3BA /* mailstream_cancel.c */,
C6F9E869105335BC0059C3BA /* mailstream_cancel.h */,
C6F9E86A105335BC0059C3BA /* mailstream_cancel_types.h */,
C6EFB8761433F1F300F805C0 /* mailstream_cfstream.c */,
C6EFB8771433F1F300F805C0 /* mailstream_cfstream.h */,
C6F9E86B105335BC0059C3BA /* mailstream_helper.c */,
C6F9E86C105335BC0059C3BA /* mailstream_helper.h */,
C6F9E86D105335BC0059C3BA /* mailstream_low.c */,
@ -2363,6 +2378,7 @@
C68C620C130FFE7E00F16728 /* quota_types.h in Headers */,
C68C620D130FFE7E00F16728 /* quota.h in Headers */,
C6667DF01342ACCD00969A8E /* xlist.h in Headers */,
C6EFB8791433F1F300F805C0 /* mailstream_cfstream.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -2604,6 +2620,7 @@
C6517A08130E86C6004ADD56 /* namespace_types.c in Sources */,
C6517A0E130E86D3004ADD56 /* namespace_sender.c in Sources */,
C6667DEF1342ACCD00969A8E /* xlist.c in Sources */,
C6EFB8781433F1F300F805C0 /* mailstream_cfstream.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -2769,6 +2786,7 @@
C6517A0A130E86C6004ADD56 /* namespace_types.c in Sources */,
C6517A10130E86D3004ADD56 /* namespace_sender.c in Sources */,
C6667DF11342ACCD00969A8E /* xlist.c in Sources */,
C6EFB87A1433F1F300F805C0 /* mailstream_cfstream.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -2820,7 +2838,10 @@
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = "$(SRCROOT)/include";
ONLY_ACTIVE_ARCH = YES;
OTHER_CFLAGS = "-DHAVE_CONFIG_H";
OTHER_CFLAGS = (
"-DHAVE_CONFIG_H=1",
"-DHAVE_CFNETWORK=1",
);
PREBINDING = NO;
SDKROOT = macosx10.5;
};
@ -2834,7 +2855,10 @@
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = "$(SRCROOT)/include";
OTHER_CFLAGS = "-DHAVE_CONFIG_H";
OTHER_CFLAGS = (
"-DHAVE_CONFIG_H=1",
"-DHAVE_CFNETWORK=1",
);
PREBINDING = NO;
SDKROOT = macosx10.5;
};


+ 2
- 1
src/data-types/Makefile.am View File

@ -48,4 +48,5 @@ libdata_types_la_SOURCES = connect.h connect.c base64.h hmac-md5.h \
charconv.c maillock.c base64.c mail_cache_db_types.h \
mail_cache_db.h mail_cache_db.c mailsem.c mailsasl.h \
mailsasl.c mailstream_cancel_types.h mailstream_cancel.h \
mailstream_cancel.c timeutils.h timeutils.c
mailstream_cancel.c timeutils.h timeutils.c \
mailstream_cfstream.c mailstream_cfstream.h

+ 125
- 2
src/data-types/mailstream.c View File

@ -43,6 +43,8 @@
#include "mailstream.h"
#include "maillock.h"
#include "mailstream_cfstream.h"
#include "mailstream_cancel.h"
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
@ -50,6 +52,9 @@
#define DEFAULT_NETWORK_TIMEOUT 300
struct timeval mailstream_network_delay =
{ DEFAULT_NETWORK_TIMEOUT, 0 };
mailstream * mailstream_new(mailstream_low * low, size_t buffer_size)
{
mailstream * s;
@ -71,6 +76,9 @@ mailstream * mailstream_new(mailstream_low * low, size_t buffer_size)
s->buffer_max_size = buffer_size;
s->low = low;
s->idle = NULL;
s->idling = 0;
return s;
free_read_buffer:
@ -273,6 +281,10 @@ void mailstream_set_low(mailstream * s, mailstream_low * low)
int mailstream_close(mailstream * s)
{
if (s->idle != NULL) {
mailstream_cancel_free(s->idle);
}
mailstream_low_close(s->low);
mailstream_low_free(s->low);
@ -322,5 +334,116 @@ void mailstream_set_privacy(mailstream * s, int can_be_public)
mailstream_low_set_privacy(s->low, can_be_public);
}
struct timeval mailstream_network_delay =
{ DEFAULT_NETWORK_TIMEOUT, 0 };
int mailstream_wait_idle(mailstream * s, int max_idle_delay)
{
int fd;
int idle_fd;
int cancel_fd;
int maxfd;
fd_set readfds;
struct timeval delay;
int r;
if (s->low->driver == mailstream_cfstream_driver) {
return mailstream_cfstream_wait_idle(s, max_idle_delay);
}
fd = mailstream_low_get_fd(mailstream_get_low(s));
idle_fd = mailstream_cancel_get_fd(s->idle);
cancel_fd = mailstream_cancel_get_fd(mailstream_low_get_cancel(mailstream_get_low(s)));
FD_ZERO(&readfds);
FD_SET(fd, &readfds);
FD_SET(idle_fd, &readfds);
FD_SET(cancel_fd, &readfds);
maxfd = fd;
if (idle_fd > maxfd) {
maxfd = idle_fd;
}
if (cancel_fd > maxfd) {
maxfd = cancel_fd;
}
delay.tv_sec = max_idle_delay;
delay.tv_usec = 0;
r = select(maxfd + 1, &readfds, NULL, NULL, &delay);
if (r == 0) {
// timeout
return MAILSTREAM_IDLE_TIMEOUT;
}
else if (r == -1) {
// do nothing
return MAILSTREAM_IDLE_ERROR;
}
else {
if (FD_ISSET(fd, &readfds)) {
// has something on socket
return MAILSTREAM_IDLE_HASDATA;
}
if (FD_ISSET(idle_fd, &readfds)) {
// idle interrupted
mailstream_cancel_ack(s->idle);
return MAILSTREAM_IDLE_INTERRUPTED;
}
if (FD_ISSET(cancel_fd, &readfds)) {
// idle cancelled
mailstream_cancel_ack(mailstream_low_get_cancel(mailstream_get_low(s)));
return MAILSTREAM_IDLE_CANCELLED;
}
return MAILSTREAM_IDLE_ERROR;
}
}
int mailstream_setup_idle(mailstream * s)
{
if (s->idling) {
return -1;
}
if (s->low->driver == mailstream_cfstream_driver) {
mailstream_cfstream_setup_idle(s);
}
else {
s->idle = mailstream_cancel_new();
if (s->idle == NULL)
return -1;
}
s->idling = 1;
return 0;
}
void mailstream_interrupt_idle(mailstream * s)
{
if (!s->idling) {
return;
}
if (s->low->driver == mailstream_cfstream_driver) {
mailstream_cfstream_interrupt_idle(s);
return;
}
else {
mailstream_cancel_notify(s->idle);
}
}
void mailstream_unsetup_idle(mailstream * s)
{
if (!s->idling) {
return;
}
if (s->low->driver == mailstream_cfstream_driver) {
mailstream_cfstream_unsetup_idle(s);
return;
}
else {
mailstream_cancel_free(s->idle);
}
s->idle = NULL;
s->idling = 0;
}

+ 8
- 0
src/data-types/mailstream.h View File

@ -85,6 +85,14 @@ extern void (* mailstream_logger_id)(mailstream_low * s, int is_stream_data, int
const char * str, size_t size);
#endif
/* can be run in thread */
int mailstream_wait_idle(mailstream * s, int max_idle_delay);
/* in main thread */
int mailstream_setup_idle(mailstream * s);
void mailstream_unsetup_idle(mailstream * s);
void mailstream_interrupt_idle(mailstream * s);
#define LIBETPAN_MAILSTREAM_NETWORK_DELAY
LIBETPAN_EXPORT
extern struct timeval mailstream_network_delay;


+ 1001
- 0
src/data-types/mailstream_cfstream.c
File diff suppressed because it is too large
View File


+ 93
- 0
src/data-types/mailstream_cfstream.h View File

@ -0,0 +1,93 @@
/*
* libEtPan! -- a mail stuff library
*
* Copyright (C) 2001, 2005 - DINH Viet Hoa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the libEtPan! project nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef MAILSTREAM_CFSTREAM_H
#define MAILSTREAM_CFSTREAM_H
#include <libetpan/mailstream.h>
#ifdef __cplusplus
extern "C" {
#endif
enum {
MAILSTREAM_CFSTREAM_SSL_ALLOWS_EXPIRED_CERTIFICATES = 1 << 0,
MAILSTREAM_CFSTREAM_SSL_ALLOWS_EXPIRED_ROOTS = 1 << 1,
MAILSTREAM_CFSTREAM_SSL_ALLOWS_ANY_ROOT = 1 << 2,
MAILSTREAM_CFSTREAM_SSL_DISABLE_VALIDATES_CERTIFICATE_CHAIN = 1 << 3,
MAILSTREAM_CFSTREAM_SSL_NO_VERIFICATION = MAILSTREAM_CFSTREAM_SSL_ALLOWS_EXPIRED_CERTIFICATES |
MAILSTREAM_CFSTREAM_SSL_ALLOWS_EXPIRED_ROOTS |
MAILSTREAM_CFSTREAM_SSL_ALLOWS_ANY_ROOT |
MAILSTREAM_CFSTREAM_SSL_DISABLE_VALIDATES_CERTIFICATE_CHAIN
};
enum {
MAILSTREAM_CFSTREAM_SSL_LEVEL_NONE,
MAILSTREAM_CFSTREAM_SSL_LEVEL_SSLv2,
MAILSTREAM_CFSTREAM_SSL_LEVEL_SSLv1,
MAILSTREAM_CFSTREAM_SSL_LEVEL_TLSv1,
MAILSTREAM_CFSTREAM_SSL_LEVEL_NEGOCIATED_SSL
};
/* socket */
extern mailstream_low_driver * mailstream_cfstream_driver;
mailstream_low * mailstream_low_cfstream_open(const char * hostname, int16_t port);
mailstream * mailstream_cfstream_open(const char * hostname, int16_t port);
/* first, set these settings */
void mailstream_cfstream_set_ssl_verification_mask(mailstream * s, int verification_mask);
void mailstream_cfstream_set_ssl_peer_name(mailstream * s, const char * peer_name);
void mailstream_cfstream_set_ssl_is_server(mailstream * s, int is_server);
void mailstream_cfstream_set_ssl_level(mailstream * s, int ssl_level);
/* missing setting certificate */
/* then, enable SSL */
int mailstream_cfstream_set_ssl_enabled(mailstream * s, int ssl_enabled);
int mailstream_cfstream_is_ssl_enabled(mailstream * s);
/* support for IMAP IDLE */
/* can be run in thread */
int mailstream_cfstream_wait_idle(mailstream * s, int max_idle_delay);
/* in main thread */
void mailstream_cfstream_setup_idle(mailstream * s);
void mailstream_cfstream_interrupt_idle(mailstream * s);
void mailstream_cfstream_unsetup_idle(mailstream * s);
#ifdef __cplusplus
}
#endif
#endif

+ 7
- 0
src/data-types/mailstream_low.c View File

@ -186,6 +186,13 @@ int mailstream_low_get_fd(mailstream_low * s)
return s->driver->mailstream_get_fd(s);
}
struct mailstream_cancel * mailstream_low_get_cancel(mailstream_low * s)
{
if (s == NULL)
return NULL;
return s->driver->mailstream_get_cancel(s);
}
void mailstream_low_free(mailstream_low * s)
{
free(s->identifier);


+ 3
- 0
src/data-types/mailstream_low.h View File

@ -61,6 +61,9 @@ int mailstream_low_close(mailstream_low * s);
LIBETPAN_EXPORT
int mailstream_low_get_fd(mailstream_low * s);
LIBETPAN_EXPORT
struct mailstream_cancel * mailstream_low_get_cancel(mailstream_low * s);
LIBETPAN_EXPORT
void mailstream_low_free(mailstream_low * s);


+ 10
- 0
src/data-types/mailstream_socket.c View File

@ -85,6 +85,7 @@ static ssize_t mailstream_low_socket_write(mailstream_low * s,
static void mailstream_low_socket_free(mailstream_low * s);
static int mailstream_low_socket_get_fd(mailstream_low * s);
static void mailstream_low_socket_cancel(mailstream_low * s);
static struct mailstream_cancel * mailstream_low_socket_get_cancel(mailstream_low * s);
static mailstream_low_driver local_mailstream_socket_driver = {
/* mailstream_read */ mailstream_low_socket_read,
@ -93,6 +94,7 @@ static mailstream_low_driver local_mailstream_socket_driver = {
/* mailstream_get_fd */ mailstream_low_socket_get_fd,
/* mailstream_free */ mailstream_low_socket_free,
/* mailstream_cancel */ mailstream_low_socket_cancel,
/* mailstream_get_cancel */ mailstream_low_socket_get_cancel,
};
mailstream_low_driver * mailstream_socket_driver =
@ -368,3 +370,11 @@ void mailstream_socket_set_use_read(mailstream * stream, int use_read)
socket_data = (struct mailstream_socket_data *) low->data;
socket_data->use_read = use_read;
}
static struct mailstream_cancel * mailstream_low_socket_get_cancel(mailstream_low * s)
{
struct mailstream_socket_data * socket_data;
socket_data = (struct mailstream_socket_data *) s->data;
return socket_data->cancel;
}

+ 13
- 0
src/data-types/mailstream_ssl.c View File

@ -254,6 +254,7 @@ static ssize_t mailstream_low_ssl_write(mailstream_low * s,
static void mailstream_low_ssl_free(mailstream_low * s);
static int mailstream_low_ssl_get_fd(mailstream_low * s);
static void mailstream_low_ssl_cancel(mailstream_low * s);
static struct mailstream_cancel * mailstream_low_ssl_get_cancel(mailstream_low * s);
static mailstream_low_driver local_mailstream_ssl_driver = {
/* mailstream_read */ mailstream_low_ssl_read,
@ -262,6 +263,7 @@ static mailstream_low_driver local_mailstream_ssl_driver = {
/* mailstream_get_fd */ mailstream_low_ssl_get_fd,
/* mailstream_free */ mailstream_low_ssl_free,
/* mailstream_cancel */ mailstream_low_ssl_cancel,
/* mailstream_get_cancel */ mailstream_low_ssl_get_cancel,
};
mailstream_low_driver * mailstream_ssl_driver = &local_mailstream_ssl_driver;
@ -1154,3 +1156,14 @@ int mailstream_ssl_get_fd(struct mailstream_ssl_context * ssl_context)
return ssl_context->fd;
}
static struct mailstream_cancel * mailstream_low_ssl_get_cancel(mailstream_low * s)
{
#ifdef USE_SSL
struct mailstream_ssl_data * data;
data = s->data;
return data->cancel;
#else
return NULL;
#endif
}

+ 12
- 0
src/data-types/mailstream_types.h View File

@ -64,6 +64,9 @@ struct _mailstream {
size_t read_buffer_len;
mailstream_low * low;
struct mailstream_cancel * idle;
int idling;
};
struct mailstream_low_driver {
@ -73,6 +76,7 @@ struct mailstream_low_driver {
int (* mailstream_get_fd)(mailstream_low *);
void (* mailstream_free)(mailstream_low *);
void (* mailstream_cancel)(mailstream_low *);
struct mailstream_cancel * (* mailstream_get_cancel)(mailstream_low *);
};
typedef struct mailstream_low_driver mailstream_low_driver;
@ -88,6 +92,14 @@ typedef void progress_function(size_t current, size_t maximum);
typedef void mailprogress_function(size_t current, size_t maximum, void * context);
enum {
MAILSTREAM_IDLE_ERROR,
MAILSTREAM_IDLE_INTERRUPTED,
MAILSTREAM_IDLE_HASDATA,
MAILSTREAM_IDLE_TIMEOUT,
MAILSTREAM_IDLE_CANCELLED
};
#ifdef __cplusplus
}
#endif


+ 48
- 1
src/low-level/imap/mailimap_socket.c View File

@ -51,16 +51,24 @@
# include <unistd.h>
#endif
#include "mailstream_cfstream.h"
#define DEFAULT_IMAP_PORT 143
#define SERVICE_NAME_IMAP "imap2"
#define SERVICE_TYPE_TCP "tcp"
static int mailimap_cfsocket_connect(mailimap * f, const char * server, uint16_t port);
LIBETPAN_EXPORT
int mailimap_socket_connect(mailimap * f, const char * server, uint16_t port)
{
int s;
mailstream * stream;
#if HAVE_CFNETWORK
return mailimap_cfsocket_connect(f, server, port);
#endif
if (port == 0) {
port = mail_get_service_port(SERVICE_NAME_IMAP, SERVICE_TYPE_TCP);
if (port == 0)
@ -92,6 +100,8 @@ int mailimap_socket_starttls(mailimap * f)
return mailimap_socket_starttls_with_callback(f, NULL, NULL);
}
static int mailimap_cfsocket_starttls(mailimap * f);
int mailimap_socket_starttls_with_callback(mailimap * f,
void (* callback)(struct mailstream_ssl_context * ssl_context, void * data), void * data)
{
@ -100,6 +110,12 @@ int mailimap_socket_starttls_with_callback(mailimap * f,
int r;
int fd;
low = mailstream_get_low(f->imap_stream);
if (low->driver == mailstream_cfstream_driver) {
// won't use callback
return mailimap_cfsocket_starttls(f);
}
r = mailimap_starttls(f);
switch (r) {
@ -109,7 +125,6 @@ int mailimap_socket_starttls_with_callback(mailimap * f,
return r;
}
low = mailstream_get_low(f->imap_stream);
fd = mailstream_low_get_fd(low);
if (fd == -1)
return MAILIMAP_ERROR_STREAM;
@ -123,3 +138,35 @@ int mailimap_socket_starttls_with_callback(mailimap * f,
return MAILIMAP_NO_ERROR;
}
static int mailimap_cfsocket_connect(mailimap * f, const char * server, uint16_t port)
{
mailstream * stream;
stream = mailstream_cfstream_open(server, port);
if (stream == NULL) {
return MAILIMAP_ERROR_CONNECTION_REFUSED;
}
return mailimap_connect(f, stream);
}
static int mailimap_cfsocket_starttls(mailimap * f)
{
int r;
r = mailimap_starttls(f);
switch (r) {
case MAILIMAP_NO_ERROR:
break;
default:
return r;
}
r = mailstream_cfstream_set_ssl_enabled(f->imap_stream, 1);
if (r < 0) {
return MAILIMAP_ERROR_SSL;
}
return MAILIMAP_NO_ERROR;
}

+ 29
- 0
src/low-level/imap/mailimap_ssl.c View File

@ -51,16 +51,26 @@
# include <unistd.h>
#endif
#include "mailstream_cfstream.h"
#define DEFAULT_IMAPS_PORT 993
#define SERVICE_NAME_IMAPS "imaps"
#define SERVICE_TYPE_TCP "tcp"
static int mailimap_cfssl_connect(mailimap * f, const char * server, uint16_t port);
int mailimap_ssl_connect_with_callback(mailimap * f, const char * server, uint16_t port,
void (* callback)(struct mailstream_ssl_context * ssl_context, void * data), void * data)
{
int s;
mailstream * stream;
#if HAVE_CFNETWORK
if (callback == NULL) {
return mailimap_cfssl_connect(f, server, port);
}
#endif
if (port == 0) {
port = mail_get_service_port(SERVICE_NAME_IMAPS, SERVICE_TYPE_TCP);
if (port == 0)
@ -91,3 +101,22 @@ int mailimap_ssl_connect(mailimap * f, const char * server, uint16_t port)
return mailimap_ssl_connect_with_callback(f, server, port,
NULL, NULL);
}
static int mailimap_cfssl_connect(mailimap * f, const char * server, uint16_t port)
{
mailstream * stream;
int r;
stream = mailstream_cfstream_open(server, port);
if (stream == NULL) {
return MAILIMAP_ERROR_CONNECTION_REFUSED;
}
mailstream_cfstream_set_ssl_verification_mask(stream, MAILSTREAM_CFSTREAM_SSL_NO_VERIFICATION);
r = mailstream_cfstream_set_ssl_enabled(stream, 1);
if (r < 0) {
mailstream_close(stream);
return MAILIMAP_ERROR_SSL;
}
return mailimap_connect(f, stream);
}

+ 20
- 1
src/low-level/nntp/newsnntp_socket.c View File

@ -41,6 +41,7 @@
#include "newsnntp.h"
#include "mailstream_cfstream.h"
#include "connect.h"
#ifdef HAVE_NETINET_IN_H
@ -54,11 +55,17 @@
#define SERVICE_NAME_NNTP "nntp"
#define SERVICE_TYPE_TCP "tcp"
static int newsnntp_cfsocket_connect(newsnntp * f, const char * server, uint16_t port);
int newsnntp_socket_connect(newsnntp * f, const char * server, uint16_t port)
{
int s;
mailstream * stream;
#if HAVE_CFNETWORK
return newsnntp_cfsocket_connect(f, server, port);
#endif
if (port == 0) {
port = mail_get_service_port(SERVICE_NAME_NNTP, SERVICE_TYPE_TCP);
if (port == 0)
@ -74,7 +81,7 @@ int newsnntp_socket_connect(newsnntp * f, const char * server, uint16_t port)
stream = mailstream_socket_open(s);
if (stream == NULL) {
#ifdef WIN32
closesocket(s);
closesocket(s);
#else
close(s);
#endif
@ -83,3 +90,15 @@ int newsnntp_socket_connect(newsnntp * f, const char * server, uint16_t port)
return newsnntp_connect(f, stream);
}
static int newsnntp_cfsocket_connect(newsnntp * f, const char * server, uint16_t port)
{
mailstream * stream;
stream = mailstream_cfstream_open(server, port);
if (stream == NULL) {
return NEWSNNTP_ERROR_STREAM;
}
return newsnntp_connect(f, stream);
}

+ 1
- 0
src/low-level/nntp/newsnntp_socket.h View File

@ -48,6 +48,7 @@ extern "C" {
#include <libetpan/newsnntp_types.h>
LIBETPAN_EXPORT
int newsnntp_socket_connect(newsnntp * f, const char * server, uint16_t port);
#ifdef __cplusplus


+ 28
- 0
src/low-level/nntp/newsnntp_ssl.c View File

@ -40,6 +40,7 @@
#include "newsnntp_ssl.h"
#include "newsnntp.h"
#include "mailstream_cfstream.h"
#include "connect.h"
#ifdef HAVE_NETINET_IN_H
@ -53,6 +54,8 @@
#define SERVICE_NAME_NNTPS "nntps"
#define SERVICE_TYPE_TCP "tcp"
static int newsnntp_cfssl_connect(newsnntp * f, const char * server, uint16_t port);
int newsnntp_ssl_connect(newsnntp * f, const char * server, uint16_t port)
{
return newsnntp_ssl_connect_with_callback(f, server, port,
@ -65,6 +68,12 @@ int newsnntp_ssl_connect_with_callback(newsnntp * f, const char * server, uint16
int s;
mailstream * stream;
#if HAVE_CFNETWORK
if (callback == NULL) {
return newsnntp_cfssl_connect(f, server, port);
}
#endif
if (port == 0) {
port = mail_get_service_port(SERVICE_NAME_NNTPS, SERVICE_TYPE_TCP);
if (port == 0)
@ -89,3 +98,22 @@ int newsnntp_ssl_connect_with_callback(newsnntp * f, const char * server, uint16
return newsnntp_connect(f, stream);
}
static int newsnntp_cfssl_connect(newsnntp * f, const char * server, uint16_t port)
{
mailstream * stream;
int r;
stream = mailstream_cfstream_open(server, port);
if (stream == NULL) {
return NEWSNNTP_ERROR_CONNECTION_REFUSED;
}
mailstream_cfstream_set_ssl_verification_mask(stream, MAILSTREAM_CFSTREAM_SSL_NO_VERIFICATION);
r = mailstream_cfstream_set_ssl_enabled(stream, 1);
if (r < 0) {
mailstream_close(stream);
return NEWSNNTP_ERROR_SSL;
}
return newsnntp_connect(f, stream);
}

+ 47
- 1
src/low-level/pop3/mailpop3_socket.c View File

@ -40,6 +40,7 @@
#include "mailpop3_socket.h"
#include "mailpop3.h"
#include "mailstream_cfstream.h"
#include "connect.h"
#ifdef HAVE_NETINET_IN_H
@ -54,11 +55,17 @@
#define SERVICE_NAME_POP3 "pop3"
#define SERVICE_TYPE_TCP "tcp"
static int mailpop3_cfsocket_connect(mailpop3 * f, const char * server, uint16_t port);
int mailpop3_socket_connect(mailpop3 * f, const char * server, uint16_t port)
{
int s;
mailstream * stream;
#if HAVE_CFNETWORK
return mailpop3_cfsocket_connect(f, server, port);
#endif
if (port == 0) {
port = mail_get_service_port(SERVICE_NAME_POP3, SERVICE_TYPE_TCP);
if (port == 0)
@ -84,6 +91,8 @@ int mailpop3_socket_connect(mailpop3 * f, const char * server, uint16_t port)
return mailpop3_connect(f, stream);
}
static int mailpop3_cfsocket_starttls(mailpop3 * f);
LIBETPAN_EXPORT
int mailpop3_socket_starttls(mailpop3 * f)
{
@ -99,6 +108,12 @@ int mailpop3_socket_starttls_with_callback(mailpop3 * f,
mailstream_low * low;
mailstream_low * new_low;
low = mailstream_get_low(f->pop3_stream);
if (low->driver == mailstream_cfstream_driver) {
// won't use callback
return mailpop3_cfsocket_starttls(f);
}
r = mailpop3_stls(f);
switch (r) {
@ -108,7 +123,6 @@ int mailpop3_socket_starttls_with_callback(mailpop3 * f,
return r;
}
low = mailstream_get_low(f->pop3_stream);
fd = mailstream_low_get_fd(low);
if (fd == -1)
return MAILPOP3_ERROR_STREAM;
@ -123,3 +137,35 @@ int mailpop3_socket_starttls_with_callback(mailpop3 * f,
return MAILPOP3_NO_ERROR;
}
static int mailpop3_cfsocket_connect(mailpop3 * f, const char * server, uint16_t port)
{
mailstream * stream;
stream = mailstream_cfstream_open(server, port);
if (stream == NULL) {
return MAILPOP3_ERROR_CONNECTION_REFUSED;
}
return mailpop3_connect(f, stream);
}
static int mailpop3_cfsocket_starttls(mailpop3 * f)
{
int r;
r = mailpop3_stls(f);
switch (r) {
case MAILPOP3_NO_ERROR:
break;
default:
return r;
}
r = mailstream_cfstream_set_ssl_enabled(f->pop3_stream, 1);
if (r < 0) {
return MAILPOP3_ERROR_SSL;
}
return MAILPOP3_NO_ERROR;
}

+ 28
- 0
src/low-level/pop3/mailpop3_ssl.c View File

@ -40,6 +40,7 @@
#include "mailpop3_ssl.h"
#include "mailpop3.h"
#include "mailstream_cfstream.h"
#include "connect.h"
#ifdef HAVE_NETINET_IN_H
@ -54,6 +55,8 @@
#define SERVICE_NAME_POP3S "pop3s"
#define SERVICE_TYPE_TCP "tcp"
static int mailpop3_cfssl_connect(mailpop3 * f, const char * server, uint16_t port);
int mailpop3_ssl_connect(mailpop3 * f, const char * server, uint16_t port)
{
return mailpop3_ssl_connect_with_callback(f, server, port,
@ -66,6 +69,12 @@ int mailpop3_ssl_connect_with_callback(mailpop3 * f, const char * server, uint16
int s;
mailstream * stream;
#if HAVE_CFNETWORK
if (callback == NULL) {
return (mailpop3_cfssl_connect, server, port);
}
#endif
if (port == 0) {
port = mail_get_service_port(SERVICE_NAME_POP3S, SERVICE_TYPE_TCP);
if (port == 0)
@ -90,3 +99,22 @@ int mailpop3_ssl_connect_with_callback(mailpop3 * f, const char * server, uint16
return mailpop3_connect(f, stream);
}
static int mailpop3_cfssl_connect(mailpop3 * f, const char * server, uint16_t port)
{
mailstream * stream;
int r;
stream = mailstream_cfstream_open(server, port);
if (stream == NULL) {
return MAILPOP3_ERROR_CONNECTION_REFUSED;
}
mailstream_cfstream_set_ssl_verification_mask(stream, MAILSTREAM_CFSTREAM_SSL_NO_VERIFICATION);
r = mailstream_cfstream_set_ssl_enabled(stream, 1);
if (r < 0) {
mailstream_close(stream);
return MAILPOP3_ERROR_SSL;
}
return mailpop3_connect(f, stream);
}

+ 45
- 1
src/low-level/smtp/mailsmtp_socket.c View File

@ -41,6 +41,7 @@
#include "mailsmtp.h"
#include "mailstream_cfstream.h"
#include "connect.h"
#include <stdlib.h>
@ -55,12 +56,19 @@
#define SERVICE_NAME_SMTP "smtp"
#define SERVICE_TYPE_TCP "tcp"
static int mailsmtp_cfsocket_connect(mailsmtp * session,
const char * server, uint16_t port);
int mailsmtp_socket_connect(mailsmtp * session,
const char * server, uint16_t port)
{
int s;
mailstream * stream;
#if HAVE_CFNETWORK
return mailsmtp_cfsocket_connect(session, server, port);
#endif
if (port == 0) {
port = mail_get_service_port(SERVICE_NAME_SMTP, SERVICE_TYPE_TCP);
if (port == 0)
@ -86,6 +94,8 @@ int mailsmtp_socket_connect(mailsmtp * session,
return mailsmtp_connect(session, stream);
}
static int mailsmtp_cfsocket_starttls(mailsmtp * session);
int mailsmtp_socket_starttls(mailsmtp * session)
{
return mailsmtp_socket_starttls_with_callback(session, NULL, NULL);
@ -99,11 +109,16 @@ int mailsmtp_socket_starttls_with_callback(mailsmtp * session,
mailstream_low * low;
mailstream_low * new_low;
low = mailstream_get_low(session->stream);
if (low->driver == mailstream_cfstream_driver) {
// won't use callback
return mailsmtp_cfsocket_starttls(session);
}
r = mailesmtp_starttls(session);
if (r != MAILSMTP_NO_ERROR)
return r;
low = mailstream_get_low(session->stream);
fd = mailstream_low_get_fd(low);
if (fd == -1)
return MAILSMTP_ERROR_STREAM;
@ -117,3 +132,32 @@ int mailsmtp_socket_starttls_with_callback(mailsmtp * session,
return MAILSMTP_NO_ERROR;
}
static int mailsmtp_cfsocket_connect(mailsmtp * session,
const char * server, uint16_t port)
{
mailstream * stream;
stream = mailstream_cfstream_open(server, port);
if (stream == NULL) {
return MAILSMTP_ERROR_CONNECTION_REFUSED;
}
return mailsmtp_connect(session, stream);
}
static int mailsmtp_cfsocket_starttls(mailsmtp * session)
{
int r;
r = mailesmtp_starttls(session);
if (r != MAILSMTP_NO_ERROR)
return r;
r = mailstream_cfstream_set_ssl_enabled(session->stream, 1);
if (r < 0) {
return MAILSMTP_ERROR_SSL;
}
return MAILSMTP_NO_ERROR;
}

+ 30
- 0
src/low-level/smtp/mailsmtp_ssl.c View File

@ -41,6 +41,7 @@
#include "mailsmtp.h"
#include "mailstream_cfstream.h"
#include "connect.h"
#include <stdlib.h>
@ -55,6 +56,9 @@
#define SERVICE_NAME_SMTPS "smtps"
#define SERVICE_TYPE_TCP "tcp"
static int mailsmtp_cfssl_connect(mailsmtp * session,
const char * server, uint16_t port);
int mailsmtp_ssl_connect(mailsmtp * session,
const char * server, uint16_t port)
{
@ -69,6 +73,12 @@ int mailsmtp_ssl_connect_with_callback(mailsmtp * session,
int s;
mailstream * stream;
#if HAVE_CFNETWORK
if (callback == NULL) {
return mailsmtp_cfssl_connect(session, server, port);
}
#endif
if (port == 0) {
port = mail_get_service_port(SERVICE_NAME_SMTPS, SERVICE_TYPE_TCP);
if (port == 0)
@ -89,3 +99,23 @@ int mailsmtp_ssl_connect_with_callback(mailsmtp * session,
return mailsmtp_connect(session, stream);
}
static int mailsmtp_cfssl_connect(mailsmtp * session,
const char * server, uint16_t port)
{
mailstream * stream;
int r;
stream = mailstream_cfstream_open(server, port);
if (stream == NULL) {
return MAILSMTP_ERROR_CONNECTION_REFUSED;
}
mailstream_cfstream_set_ssl_verification_mask(stream, MAILSTREAM_CFSTREAM_SSL_NO_VERIFICATION);
r = mailstream_cfstream_set_ssl_enabled(stream, 1);
if (r < 0) {
mailstream_close(stream);
return MAILSMTP_ERROR_SSL;
}
return mailsmtp_connect(session, stream);
}

Loading…
Cancel
Save