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. typedef struct ssl_st SSL;
  30. typedef struct ssl_ctx_st SSL_CTX;
  31. typedef struct stack_st _STACK;
  32. enum {
  33. SSLv2 = 1,
  34. SSLv3 = 2,
  35. TLSv1 = 4,
  36. TLSv1_1 = 8,
  37. TLSv1_2 = 16
  38. };
  39. #endif
  40. typedef struct server_conf {
  41. char *tunnel;
  42. char *host;
  43. int port;
  44. int timeout;
  45. #ifdef HAVE_LIBSSL
  46. char *cert_file;
  47. char system_certs;
  48. char ssl_versions;
  49. /* these are actually variables and are leaked at the end */
  50. char ssl_ctx_valid;
  51. _STACK *trusted_certs;
  52. SSL_CTX *SSLContext;
  53. #endif
  54. } server_conf_t;
  55. typedef struct buff_chunk {
  56. struct buff_chunk *next;
  57. int len;
  58. char data[1];
  59. } buff_chunk_t;
  60. typedef struct {
  61. /* connection */
  62. int fd;
  63. int state;
  64. const server_conf_t *conf; /* needed during connect */
  65. #ifdef HAVE_IPV6
  66. struct addrinfo *addrs, *curr_addr; /* needed during connect */
  67. #else
  68. char **curr_addr; /* needed during connect */
  69. #endif
  70. char *name;
  71. #ifdef HAVE_LIBSSL
  72. SSL *ssl;
  73. wakeup_t ssl_fake;
  74. #endif
  75. #ifdef HAVE_LIBZ
  76. z_streamp in_z, out_z;
  77. wakeup_t z_fake;
  78. int z_written;
  79. #endif
  80. void (*bad_callback)( void *aux ); /* async fail while sending or listening */
  81. void (*read_callback)( void *aux ); /* data available for reading */
  82. void (*write_callback)( void *aux ); /* all *queued* data was sent */
  83. union {
  84. void (*connect)( int ok, void *aux );
  85. void (*starttls)( int ok, void *aux );
  86. } callbacks;
  87. void *callback_aux;
  88. notifier_t notify;
  89. wakeup_t fd_fake;
  90. wakeup_t fd_timeout;
  91. /* writing */
  92. buff_chunk_t *append_buf; /* accumulating buffer */
  93. buff_chunk_t *write_buf, **write_buf_append; /* buffer head & tail */
  94. int writing;
  95. #ifdef HAVE_LIBZ
  96. int append_avail; /* space left in accumulating buffer */
  97. #endif
  98. int write_offset; /* offset into buffer head */
  99. int buffer_mem; /* memory currently occupied by buffers in the queue */
  100. /* reading */
  101. int offset; /* start of filled bytes in buffer */
  102. int bytes; /* number of filled bytes in buffer */
  103. int 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 = 0;
  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_read( conn_t *sock, int expect );
  131. int socket_read( conn_t *sock, char *buf, int 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 conn_iovec {
  135. char *buf;
  136. int len;
  137. ownership_t takeOwn;
  138. } conn_iovec_t;
  139. void socket_write( conn_t *sock, conn_iovec_t *iov, int iovcnt );
  140. #endif