socket.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * mbsync - mailbox synchronizer
  3. * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
  4. * Copyright (C) 2002-2006,2010-2012 Oswald Buddenhagen <ossi@users.sf.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * As a special exception, mbsync may be linked with the OpenSSL library,
  20. * despite that library's more restrictive license.
  21. */
  22. #ifndef SOCKET_H
  23. #define SOCKET_H
  24. #include "common.h"
  25. #ifdef HAVE_LIBZ
  26. #include <zlib.h>
  27. #endif
  28. #ifdef HAVE_LIBSSL
  29. # include <openssl/ssl.h>
  30. # include <openssl/x509.h>
  31. enum {
  32. TLSv1 = 4,
  33. TLSv1_1 = 8,
  34. TLSv1_2 = 16,
  35. TLSv1_3 = 32
  36. };
  37. #endif
  38. typedef struct {
  39. char *tunnel;
  40. char *host;
  41. ushort port;
  42. int timeout;
  43. #ifdef HAVE_LIBSSL
  44. char *cert_file;
  45. char *client_certfile;
  46. char *client_keyfile;
  47. char *cipher_string;
  48. char system_certs;
  49. char ssl_versions;
  50. /* these are actually variables and are leaked at the end */
  51. char ssl_ctx_valid;
  52. STACK_OF(X509) *trusted_certs;
  53. SSL_CTX *SSLContext;
  54. #endif
  55. } server_conf_t;
  56. typedef struct buff_chunk {
  57. struct buff_chunk *next;
  58. uint len;
  59. char data[1];
  60. } buff_chunk_t;
  61. typedef struct {
  62. /* connection */
  63. int fd;
  64. int state;
  65. const server_conf_t *conf; /* needed during connect */
  66. #ifdef HAVE_IPV6
  67. struct addrinfo *addrs, *curr_addr; /* needed during connect */
  68. #else
  69. struct addr_info *addrs, *curr_addr; /* needed during connect */
  70. #endif
  71. char *name;
  72. #ifdef HAVE_LIBSSL
  73. SSL *ssl;
  74. wakeup_t ssl_fake;
  75. #endif
  76. #ifdef HAVE_LIBZ
  77. z_streamp in_z, out_z;
  78. wakeup_t z_fake;
  79. int z_written;
  80. #endif
  81. void (*bad_callback)( void *aux ); /* async fail while sending or listening */
  82. void (*read_callback)( void *aux ); /* data available for reading */
  83. void (*write_callback)( void *aux ); /* all *queued* data was sent */
  84. union {
  85. void (*connect)( int ok, void *aux );
  86. void (*starttls)( int ok, void *aux );
  87. } callbacks;
  88. void *callback_aux;
  89. notifier_t notify;
  90. wakeup_t fd_fake;
  91. wakeup_t fd_timeout;
  92. /* writing */
  93. buff_chunk_t *append_buf; /* accumulating buffer */
  94. buff_chunk_t *write_buf, **write_buf_append; /* buffer head & tail */
  95. #ifdef HAVE_LIBZ
  96. uint append_avail; /* space left in accumulating buffer */
  97. #endif
  98. uint write_offset; /* offset into buffer head */
  99. uint buffer_mem; /* memory currently occupied by buffers in the queue */
  100. /* reading */
  101. uint offset; /* start of filled bytes in buffer */
  102. uint bytes; /* number of filled bytes in buffer */
  103. uint scanoff; /* offset to continue scanning for newline at, relative to 'offset' */
  104. char buf[100000];
  105. #ifdef HAVE_LIBZ
  106. char z_buf[100000];
  107. #endif
  108. } conn_t;
  109. /* call this before doing anything with the socket */
  110. static INLINE void socket_init( conn_t *conn,
  111. const server_conf_t *conf,
  112. void (*bad_callback)( void *aux ),
  113. void (*read_callback)( void *aux ),
  114. void (*write_callback)( void *aux ),
  115. void *aux )
  116. {
  117. conn->conf = conf;
  118. conn->bad_callback = bad_callback;
  119. conn->read_callback = read_callback;
  120. conn->write_callback = write_callback;
  121. conn->callback_aux = aux;
  122. conn->fd = -1;
  123. conn->name = NULL;
  124. conn->write_buf_append = &conn->write_buf;
  125. }
  126. void socket_connect( conn_t *conn, void (*cb)( int ok, void *aux ) );
  127. void socket_start_tls(conn_t *conn, void (*cb)( int ok, void *aux ) );
  128. void socket_start_deflate( conn_t *conn );
  129. void socket_close( conn_t *sock );
  130. void socket_expect_activity( conn_t *sock, int expect );
  131. int socket_read( conn_t *sock, char *buf, uint len ); /* never waits */
  132. char *socket_read_line( conn_t *sock ); /* don't free return value; never waits */
  133. typedef enum { KeepOwn = 0, GiveOwn } ownership_t;
  134. typedef struct {
  135. char *buf;
  136. uint len;
  137. ownership_t takeOwn;
  138. } conn_iovec_t;
  139. void socket_write( conn_t *sock, conn_iovec_t *iov, int iovcnt );
  140. #endif