socket.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #ifdef HAVE_LIBSSL
  45. char *cert_file;
  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. #endif
  78. void (*bad_callback)( void *aux ); /* async fail while sending or listening */
  79. void (*read_callback)( void *aux ); /* data available for reading */
  80. int (*write_callback)( void *aux ); /* all *queued* data was sent */
  81. union {
  82. void (*connect)( int ok, void *aux );
  83. void (*starttls)( int ok, void *aux );
  84. } callbacks;
  85. void *callback_aux;
  86. notifier_t notify;
  87. wakeup_t fd_fake;
  88. /* writing */
  89. buff_chunk_t *append_buf; /* accumulating buffer */
  90. buff_chunk_t *write_buf, **write_buf_append; /* buffer head & tail */
  91. #ifdef HAVE_LIBZ
  92. int append_avail; /* space left in accumulating buffer */
  93. #endif
  94. int write_offset; /* offset into buffer head */
  95. int buffer_mem; /* memory currently occupied by buffers in the queue */
  96. /* reading */
  97. int offset; /* start of filled bytes in buffer */
  98. int bytes; /* number of filled bytes in buffer */
  99. int scanoff; /* offset to continue scanning for newline at, relative to 'offset' */
  100. char buf[100000];
  101. #ifdef HAVE_LIBZ
  102. char z_buf[100000];
  103. #endif
  104. } conn_t;
  105. /* call this before doing anything with the socket */
  106. static INLINE void socket_init( conn_t *conn,
  107. const server_conf_t *conf,
  108. void (*bad_callback)( void *aux ),
  109. void (*read_callback)( void *aux ),
  110. int (*write_callback)( void *aux ),
  111. void *aux )
  112. {
  113. conn->conf = conf;
  114. conn->bad_callback = bad_callback;
  115. conn->read_callback = read_callback;
  116. conn->write_callback = write_callback;
  117. conn->callback_aux = aux;
  118. conn->fd = -1;
  119. conn->name = 0;
  120. conn->write_buf_append = &conn->write_buf;
  121. }
  122. void socket_connect( conn_t *conn, void (*cb)( int ok, void *aux ) );
  123. void socket_start_tls(conn_t *conn, void (*cb)( int ok, void *aux ) );
  124. void socket_start_deflate( conn_t *conn );
  125. void socket_close( conn_t *sock );
  126. int socket_read( conn_t *sock, char *buf, int len ); /* never waits */
  127. char *socket_read_line( conn_t *sock ); /* don't free return value; never waits */
  128. typedef enum { KeepOwn = 0, GiveOwn } ownership_t;
  129. typedef struct conn_iovec {
  130. char *buf;
  131. int len;
  132. ownership_t takeOwn;
  133. } conn_iovec_t;
  134. int socket_write( conn_t *sock, conn_iovec_t *iov, int iovcnt );
  135. #endif