ECChannel.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #ifndef ECCHANNEL_H
  18. #define ECCHANNEL_H
  19. #include <kopano/zcdefs.h>
  20. #include <cstdio>
  21. #include <iostream>
  22. #include <sys/socket.h>
  23. #include <openssl/ssl.h>
  24. #include <openssl/err.h>
  25. #include <string>
  26. #include <kopano/ECConfig.h>
  27. #include <kopano/ECLogger.h>
  28. struct sockaddr;
  29. namespace KC {
  30. // ECChannel is the communication channel with the other side. Initially, it is
  31. // a simple way to read/write full lines of data. The reason why we specify
  32. // a special 'HrWriteLine' instead of 'HrWrite' is that SSL encryption prefers
  33. // writing all the data at once, instead of via multiple write() calls. Also,
  34. // this ensures that the ECChannel class is responsible for reading, writing
  35. // and culling newline characters.
  36. class _kc_export ECChannel _kc_final {
  37. public:
  38. _kc_hidden ECChannel(int sockfd);
  39. ~ECChannel();
  40. HRESULT HrEnableTLS(void);
  41. _kc_hidden HRESULT HrGets(char *buf, ULONG bufsize, ULONG *have_read);
  42. HRESULT HrReadLine(std::string * strBuffer, ULONG ulMaxBuffer = 65536);
  43. _kc_hidden HRESULT HrWriteString(const char *buf);
  44. HRESULT HrWriteString(const std::string & strBuffer);
  45. HRESULT HrWriteLine(const char *szBuffer, int len = 0);
  46. HRESULT HrWriteLine(const std::string & strBuffer);
  47. _kc_hidden HRESULT HrReadBytes(char *buf, ULONG count);
  48. HRESULT HrReadBytes(std::string * strBuffer, ULONG ulByteCount);
  49. HRESULT HrReadAndDiscardBytes(ULONG ulByteCount);
  50. HRESULT HrSelect(int seconds);
  51. _kc_hidden void SetIPAddress(const struct sockaddr *, size_t);
  52. _kc_hidden const char *peer_addr(void) const { return peer_atxt; }
  53. int peer_is_local(void) const;
  54. _kc_hidden bool UsingSsl(void) const { return lpSSL != NULL; }
  55. _kc_hidden bool sslctx(void) const { return lpCTX != NULL; }
  56. static HRESULT HrSetCtx(ECConfig *);
  57. static HRESULT HrFreeCtx();
  58. private:
  59. int fd;
  60. SSL *lpSSL = nullptr;
  61. static SSL_CTX *lpCTX;
  62. char peer_atxt[256+16];
  63. struct sockaddr_storage peer_sockaddr;
  64. socklen_t peer_salen = 0;
  65. _kc_hidden char *fd_gets(char *buf, int *len);
  66. _kc_hidden char *SSL_gets(char *buf, int *len);
  67. };
  68. /* helpers to open socket */
  69. extern HRESULT HrListen(const char *path, int *fd);
  70. extern _kc_export HRESULT HrListen(const char *bind, uint16_t port, int *fd);
  71. /* accept data on connection */
  72. extern _kc_export HRESULT HrAccept(int fd, ECChannel **ch);
  73. extern _kc_export int zcp_bindtodevice(int fd, const char *iface);
  74. extern int zcp_peeraddr_is_local(const struct sockaddr *, socklen_t);
  75. extern _kc_export int zcp_peerfd_is_local(int);
  76. } /* namespace KC */
  77. #endif