socket.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. typedef struct ssl_st SSL;
  26. typedef struct ssl_ctx_st SSL_CTX;
  27. typedef struct stack_st _STACK;
  28. typedef struct server_conf {
  29. char *tunnel;
  30. char *host;
  31. int port;
  32. #ifdef HAVE_LIBSSL
  33. char *cert_file;
  34. char use_imaps;
  35. char use_sslv2, use_sslv3, use_tlsv1, use_tlsv11, use_tlsv12;
  36. /* these are actually variables and are leaked at the end */
  37. char ssl_ctx_valid;
  38. _STACK *trusted_certs;
  39. SSL_CTX *SSLContext;
  40. #endif
  41. } server_conf_t;
  42. typedef struct buff_chunk {
  43. struct buff_chunk *next;
  44. char *data;
  45. int len;
  46. char buf[1];
  47. } buff_chunk_t;
  48. typedef struct {
  49. /* connection */
  50. int fd;
  51. int state;
  52. const server_conf_t *conf; /* needed during connect */
  53. #ifdef HAVE_IPV6
  54. struct addrinfo *addrs, *curr_addr; /* needed during connect */
  55. #else
  56. char **curr_addr; /* needed during connect */
  57. #endif
  58. char *name;
  59. #ifdef HAVE_LIBSSL
  60. SSL *ssl;
  61. int force_trusted;
  62. #endif
  63. void (*bad_callback)( void *aux ); /* async fail while sending or listening */
  64. void (*read_callback)( void *aux ); /* data available for reading */
  65. int (*write_callback)( void *aux ); /* all *queued* data was sent */
  66. union {
  67. void (*connect)( int ok, void *aux );
  68. void (*starttls)( int ok, void *aux );
  69. } callbacks;
  70. void *callback_aux;
  71. /* writing */
  72. buff_chunk_t *write_buf, **write_buf_append; /* buffer head & tail */
  73. int write_offset; /* offset into buffer head */
  74. /* reading */
  75. int offset; /* start of filled bytes in buffer */
  76. int bytes; /* number of filled bytes in buffer */
  77. int scanoff; /* offset to continue scanning for newline at, relative to 'offset' */
  78. char buf[100000];
  79. } conn_t;
  80. /* call this before doing anything with the socket */
  81. static INLINE void socket_init( conn_t *conn,
  82. const server_conf_t *conf,
  83. void (*bad_callback)( void *aux ),
  84. void (*read_callback)( void *aux ),
  85. int (*write_callback)( void *aux ),
  86. void *aux )
  87. {
  88. conn->conf = conf;
  89. conn->bad_callback = bad_callback;
  90. conn->read_callback = read_callback;
  91. conn->write_callback = write_callback;
  92. conn->callback_aux = aux;
  93. conn->fd = -1;
  94. conn->name = 0;
  95. conn->write_buf_append = &conn->write_buf;
  96. }
  97. void socket_connect( conn_t *conn, void (*cb)( int ok, void *aux ) );
  98. void socket_start_tls(conn_t *conn, void (*cb)( int ok, void *aux ) );
  99. void socket_close( conn_t *sock );
  100. int socket_read( conn_t *sock, char *buf, int len ); /* never waits */
  101. char *socket_read_line( conn_t *sock ); /* don't free return value; never waits */
  102. typedef enum { KeepOwn = 0, GiveOwn } ownership_t;
  103. int socket_write( conn_t *sock, char *buf, int len, ownership_t takeOwn );
  104. void cram( const char *challenge, const char *user, const char *pass,
  105. char **_final, int *_finallen );
  106. #endif