SocketCore.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #ifndef _D_SOCKET_CORE_H_
  23. #define _D_SOCKET_CORE_H_
  24. #include <string>
  25. #include <utility>
  26. #include "common.h"
  27. #ifdef HAVE_LIBSSL
  28. // for SSL
  29. # include <openssl/ssl.h>
  30. #endif // HAVE_LIBSSL
  31. using namespace std;
  32. class SocketCore {
  33. friend class Socket;
  34. private:
  35. // socket endpoint descriptor
  36. int sockfd;
  37. // reference counter for this object.
  38. int use;
  39. bool secure;
  40. #ifdef HAVE_LIBSSL
  41. // for SSL
  42. SSL_CTX* sslCtx;
  43. SSL* ssl;
  44. #endif // HAVE_LIBSSL
  45. void init();
  46. SocketCore(int sockfd);
  47. public:
  48. SocketCore();
  49. ~SocketCore();
  50. void beginListen();
  51. void getAddrInfo(pair<string, int>& addrinfo) const;
  52. SocketCore* acceptConnection() const;
  53. /**
  54. * Connects to the server named host and the destination port is port.
  55. * This method make socket non-blocking mode.
  56. * To make the socket blocking mode, call setBlockingMode() after
  57. * the connection is established.
  58. */
  59. void establishConnection(string host, int port);
  60. void setBlockingMode() const;
  61. // Closes the connection which this socket object has
  62. void closeConnection();
  63. // examines whether the socket of this SocketCore object is available for writing.
  64. // returns true if the socket is available for writing, otherwise returns false.
  65. bool isWritable(int timeout) const;
  66. // examines whether the socket of this SocketCore object is available for reading.
  67. // returns true if the socket is available for reading, otherwise returns false.
  68. bool isReadable(int timeout) const;
  69. // writes characters into the socket. data is a pointer pointing the first
  70. // byte of the data and len is the length of the data.
  71. void writeData(const char* data, int len, int timeout = 5) const;
  72. // Reads up to len bytes from this socket.
  73. // data is a pointer pointing the first
  74. // byte of the data, which must be allocated before this method is called.
  75. // len is the size of the allocated memory. When this method returns
  76. // successfully, len is replaced by the size of the read data.
  77. void readData(char* data, int& len, int timeout = 5) const;
  78. // Reads up to len bytes from this socket, but bytes are not removed from
  79. // this socket.
  80. void peekData(char* data, int& len, int timeout = 5) const;
  81. /**
  82. * Makes this socket secure.
  83. * If the system has not OpenSSL, then this method do nothing.
  84. */
  85. void initiateSecureConnection() ;
  86. };
  87. #endif // _D_SOCKET_CORE_H_