2013-12-08 19:46:40 +00:00
|
|
|
/*
|
|
|
|
* mbsync - mailbox synchronizer
|
|
|
|
* Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
|
|
|
|
* Copyright (C) 2002-2006,2010-2012 Oswald Buddenhagen <ossi@users.sf.net>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* As a special exception, mbsync may be linked with the OpenSSL library,
|
|
|
|
* despite that library's more restrictive license.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SOCKET_H
|
|
|
|
#define SOCKET_H
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2014-12-13 11:09:15 +00:00
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
#include <zlib.h>
|
|
|
|
#endif
|
|
|
|
|
2014-07-12 18:35:55 +00:00
|
|
|
#ifdef HAVE_LIBSSL
|
2017-10-07 12:09:39 +00:00
|
|
|
# include <openssl/ssl.h>
|
2020-07-29 18:23:54 +00:00
|
|
|
# include <openssl/x509.h>
|
2013-12-08 19:46:40 +00:00
|
|
|
|
2014-07-12 18:35:55 +00:00
|
|
|
enum {
|
|
|
|
TLSv1 = 4,
|
|
|
|
TLSv1_1 = 8,
|
2019-11-26 15:05:46 +00:00
|
|
|
TLSv1_2 = 16,
|
|
|
|
TLSv1_3 = 32
|
2014-07-12 18:35:55 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2017-04-02 13:42:18 +00:00
|
|
|
typedef struct {
|
2013-12-08 19:46:40 +00:00
|
|
|
char *tunnel;
|
|
|
|
char *host;
|
2018-07-01 11:22:17 +00:00
|
|
|
ushort port;
|
2015-04-06 14:49:33 +00:00
|
|
|
int timeout;
|
2013-12-08 19:46:40 +00:00
|
|
|
#ifdef HAVE_LIBSSL
|
|
|
|
char *cert_file;
|
2015-08-08 17:45:53 +00:00
|
|
|
char *client_certfile;
|
|
|
|
char *client_keyfile;
|
2019-11-09 18:47:55 +00:00
|
|
|
char *cipher_string;
|
2014-07-27 13:42:33 +00:00
|
|
|
char system_certs;
|
2014-07-12 18:35:55 +00:00
|
|
|
char ssl_versions;
|
2013-12-08 19:46:40 +00:00
|
|
|
|
|
|
|
/* these are actually variables and are leaked at the end */
|
|
|
|
char ssl_ctx_valid;
|
2020-07-29 18:23:54 +00:00
|
|
|
STACK_OF(X509) *trusted_certs;
|
2013-12-08 19:46:40 +00:00
|
|
|
SSL_CTX *SSLContext;
|
|
|
|
#endif
|
|
|
|
} server_conf_t;
|
|
|
|
|
|
|
|
typedef struct buff_chunk {
|
|
|
|
struct buff_chunk *next;
|
2020-07-08 15:27:37 +00:00
|
|
|
uint len;
|
2014-11-08 14:42:41 +00:00
|
|
|
char data[1];
|
2013-12-08 19:46:40 +00:00
|
|
|
} buff_chunk_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
/* connection */
|
|
|
|
int fd;
|
|
|
|
int state;
|
|
|
|
const server_conf_t *conf; /* needed during connect */
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
struct addrinfo *addrs, *curr_addr; /* needed during connect */
|
|
|
|
#else
|
2020-07-27 20:48:41 +00:00
|
|
|
struct addr_info *addrs, *curr_addr; /* needed during connect */
|
2013-12-08 19:46:40 +00:00
|
|
|
#endif
|
|
|
|
char *name;
|
|
|
|
#ifdef HAVE_LIBSSL
|
|
|
|
SSL *ssl;
|
2014-11-30 18:44:41 +00:00
|
|
|
wakeup_t ssl_fake;
|
2013-12-08 19:46:40 +00:00
|
|
|
#endif
|
2014-12-13 11:09:15 +00:00
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
z_streamp in_z, out_z;
|
|
|
|
wakeup_t z_fake;
|
2015-05-09 17:17:41 +00:00
|
|
|
int z_written;
|
2014-12-13 11:09:15 +00:00
|
|
|
#endif
|
2013-12-08 19:46:40 +00:00
|
|
|
|
|
|
|
void (*bad_callback)( void *aux ); /* async fail while sending or listening */
|
|
|
|
void (*read_callback)( void *aux ); /* data available for reading */
|
2015-05-09 17:18:40 +00:00
|
|
|
void (*write_callback)( void *aux ); /* all *queued* data was sent */
|
2013-12-08 19:46:40 +00:00
|
|
|
union {
|
|
|
|
void (*connect)( int ok, void *aux );
|
|
|
|
void (*starttls)( int ok, void *aux );
|
|
|
|
} callbacks;
|
|
|
|
void *callback_aux;
|
|
|
|
|
2014-11-29 18:15:50 +00:00
|
|
|
notifier_t notify;
|
2014-11-08 14:42:41 +00:00
|
|
|
wakeup_t fd_fake;
|
2015-04-06 14:49:33 +00:00
|
|
|
wakeup_t fd_timeout;
|
2014-11-29 18:15:50 +00:00
|
|
|
|
2013-12-08 19:46:40 +00:00
|
|
|
/* writing */
|
2014-11-08 14:42:41 +00:00
|
|
|
buff_chunk_t *append_buf; /* accumulating buffer */
|
2013-12-08 19:46:40 +00:00
|
|
|
buff_chunk_t *write_buf, **write_buf_append; /* buffer head & tail */
|
2014-12-13 11:09:15 +00:00
|
|
|
#ifdef HAVE_LIBZ
|
2020-07-08 15:27:37 +00:00
|
|
|
uint append_avail; /* space left in accumulating buffer */
|
2014-12-13 11:09:15 +00:00
|
|
|
#endif
|
2020-07-08 15:27:37 +00:00
|
|
|
uint write_offset; /* offset into buffer head */
|
|
|
|
uint buffer_mem; /* memory currently occupied by buffers in the queue */
|
2013-12-08 19:46:40 +00:00
|
|
|
|
|
|
|
/* reading */
|
2020-07-08 15:27:37 +00:00
|
|
|
uint offset; /* start of filled bytes in buffer */
|
|
|
|
uint bytes; /* number of filled bytes in buffer */
|
|
|
|
uint scanoff; /* offset to continue scanning for newline at, relative to 'offset' */
|
2013-12-08 19:46:40 +00:00
|
|
|
char buf[100000];
|
2014-12-13 11:09:15 +00:00
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
char z_buf[100000];
|
|
|
|
#endif
|
2013-12-08 19:46:40 +00:00
|
|
|
} conn_t;
|
|
|
|
|
|
|
|
/* call this before doing anything with the socket */
|
|
|
|
static INLINE void socket_init( conn_t *conn,
|
|
|
|
const server_conf_t *conf,
|
|
|
|
void (*bad_callback)( void *aux ),
|
|
|
|
void (*read_callback)( void *aux ),
|
2015-05-09 17:18:40 +00:00
|
|
|
void (*write_callback)( void *aux ),
|
2013-12-08 19:46:40 +00:00
|
|
|
void *aux )
|
|
|
|
{
|
|
|
|
conn->conf = conf;
|
|
|
|
conn->bad_callback = bad_callback;
|
|
|
|
conn->read_callback = read_callback;
|
|
|
|
conn->write_callback = write_callback;
|
|
|
|
conn->callback_aux = aux;
|
|
|
|
conn->fd = -1;
|
2019-07-28 18:50:31 +00:00
|
|
|
conn->name = NULL;
|
2013-12-08 19:46:40 +00:00
|
|
|
conn->write_buf_append = &conn->write_buf;
|
|
|
|
}
|
|
|
|
void socket_connect( conn_t *conn, void (*cb)( int ok, void *aux ) );
|
|
|
|
void socket_start_tls(conn_t *conn, void (*cb)( int ok, void *aux ) );
|
2014-12-13 11:09:15 +00:00
|
|
|
void socket_start_deflate( conn_t *conn );
|
2013-12-08 19:46:40 +00:00
|
|
|
void socket_close( conn_t *sock );
|
2019-11-16 16:14:57 +00:00
|
|
|
void socket_expect_activity( conn_t *sock, int expect );
|
2020-07-08 15:27:37 +00:00
|
|
|
int socket_read( conn_t *sock, char *buf, uint len ); /* never waits */
|
2013-12-08 19:46:40 +00:00
|
|
|
char *socket_read_line( conn_t *sock ); /* don't free return value; never waits */
|
|
|
|
typedef enum { KeepOwn = 0, GiveOwn } ownership_t;
|
2017-04-02 13:42:18 +00:00
|
|
|
typedef struct {
|
2014-10-26 20:10:25 +00:00
|
|
|
char *buf;
|
2020-07-08 15:27:37 +00:00
|
|
|
uint len;
|
2014-10-26 20:10:25 +00:00
|
|
|
ownership_t takeOwn;
|
|
|
|
} conn_iovec_t;
|
2015-05-09 17:17:41 +00:00
|
|
|
void socket_write( conn_t *sock, conn_iovec_t *iov, int iovcnt );
|
2013-12-08 19:46:40 +00:00
|
|
|
|
|
|
|
#endif
|