socket.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. enum {
  31. SSLv3 = 2,
  32. TLSv1 = 4,
  33. TLSv1_1 = 8,
  34. TLSv1_2 = 16
  35. };
  36. #endif
  37. typedef struct {
  38. char *tunnel;
  39. char *host;
  40. ushort port;
  41. int timeout;
  42. #ifdef HAVE_LIBSSL
  43. char *cert_file;
  44. char *client_certfile;
  45. char *client_keyfile;
  46. char system_certs;
  47. char ssl_versions;
  48. /* these are actually variables and are leaked at the end */
  49. char ssl_ctx_valid;
  50. _STACK *trusted_certs;
  51. SSL_CTX *SSLContext;
  52. #endif
  53. } server_conf_t;
  54. typedef struct buff_chunk {
  55. struct buff_chunk *next;
  56. int len;
  57. char data[1];
  58. } buff_chunk_t;
  59. typedef struct {
  60. /* connection */
  61. int fd;
  62. int state;
  63. const server_conf_t *conf; /* needed during connect */
  64. #ifdef HAVE_IPV6
  65. struct addrinfo *addrs, *curr_addr; /* needed during connect */
  66. #else
  67. char **curr_addr; /* needed during connect */
  68. #endif
  69. char *name;
  70. #ifdef HAVE_LIBSSL
  71. SSL *ssl;
  72. wakeup_t ssl_fake;
  73. #endif
  74. #ifdef HAVE_LIBZ
  75. z_streamp in_z, out_z;
  76. wakeup_t z_fake;
  77. int z_written;
  78. #endif
  79. void (*bad_callback)( void *aux ); /* async fail while sending or listening */
  80. void (*read_callback)( void *aux ); /* data available for reading */
  81. void (*write_callback)( void *aux ); /* all *queued* data was sent */
  82. union {
  83. void (*connect)( int ok, void *aux );
  84. void (*starttls)( int ok, void *aux );
  85. } callbacks;
  86. void *callback_aux;
  87. notifier_t notify;
  88. wakeup_t fd_fake;
  89. wakeup_t fd_timeout;
  90. /* writing */
  91. buff_chunk_t *append_buf; /* accumulating buffer */
  92. buff_chunk_t *write_buf, **write_buf_append; /* buffer head & tail */
  93. int writing;
  94. #ifdef HAVE_LIBZ
  95. int append_avail; /* space left in accumulating buffer */
  96. #endif
  97. int write_offset; /* offset into buffer head */
  98. int buffer_mem; /* memory currently occupied by buffers in the queue */
  99. /* reading */
  100. int offset; /* start of filled bytes in buffer */
  101. int bytes; /* number of filled bytes in buffer */
  102. int scanoff; /* offset to continue scanning for newline at, relative to 'offset' */
  103. char buf[100000];
  104. #ifdef HAVE_LIBZ
  105. char z_buf[100000];
  106. #endif
  107. } conn_t;
  108. /* call this before doing anything with the socket */
  109. static INLINE void socket_init( conn_t *conn,
  110. const server_conf_t *conf,
  111. void (*bad_callback)( void *aux ),
  112. void (*read_callback)( void *aux ),
  113. void (*write_callback)( void *aux ),
  114. void *aux )
  115. {
  116. conn->conf = conf;
  117. conn->bad_callback = bad_callback;
  118. conn->read_callback = read_callback;
  119. conn->write_callback = write_callback;
  120. conn->callback_aux = aux;
  121. conn->fd = -1;
  122. conn->name = 0;
  123. conn->write_buf_append = &conn->write_buf;
  124. }
  125. void socket_connect( conn_t *conn, void (*cb)( int ok, void *aux ) );
  126. void socket_start_tls(conn_t *conn, void (*cb)( int ok, void *aux ) );
  127. void socket_start_deflate( conn_t *conn );
  128. void socket_close( conn_t *sock );
  129. void socket_expect_read( conn_t *sock, int expect );
  130. int socket_read( conn_t *sock, char *buf, int len ); /* never waits */
  131. char *socket_read_line( conn_t *sock ); /* don't free return value; never waits */
  132. typedef enum { KeepOwn = 0, GiveOwn } ownership_t;
  133. typedef struct {
  134. char *buf;
  135. int len;
  136. ownership_t takeOwn;
  137. } conn_iovec_t;
  138. void socket_write( conn_t *sock, conn_iovec_t *iov, int iovcnt );
  139. #endif