aria2/src/SocketCore.h

181 lines
5.3 KiB
C
Raw Normal View History

/* <!-- copyright */
/*
* aria2 - a simple utility for downloading files faster
*
* Copyright (C) 2006 Tatsuhiro Tsujikawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* copyright --> */
#ifndef _D_SOCKET_CORE_H_
#define _D_SOCKET_CORE_H_
#include <string>
2006-02-21 12:27:17 +00:00
#include <utility>
#include "common.h"
#ifdef HAVE_LIBSSL
// for SSL
# include <openssl/ssl.h>
#endif // HAVE_LIBSSL
2006-03-27 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> * configure.in: Added gnutls support. Added several CPP macros. * m4/openssl.m4: Added. * src/SocketCore.{h,cc}: Drop const quarifier from writeData(), readData(), peekData(). Added gnutls support. * src/HttpDownloadCommand.cc: Removed SleepCommand.h * src/TrackerWatcherCommand.{h,cc}: Added. This command creates TrackerInitCommand periodicaly(TorrentMan::minInterval). * src/TorrentMan.cc: Remove downloadedSize == 0 check from save(). Instead, added a check for whether setup method has executed successfully. * src/TorrentMan.h: Added member vaiable setupComplete. Updated DEFAULT_ANNOUNCE_INTERVAL and DEFAULT_ANNOUNCE_MIN_INTERVAL to 300. * src/Makefile.am: Updated. * src/messageDigest.h: Added. This is a macro calculating SHA1 digest using whether OpenSSL or gcrypt, depending on the result of configure script. * src/ShaVisitor.{h,cc}: Removed direct dependency on OpenSSL by using messageDigest.h. * src/TorrentAutoSaveCommand.h: Removed unused variable cuid. * src/PeerListenCommand.cc: Added log about port binded successfully. Fixed memory leak. * src/main.cc: Added gnutls support. Replaced LIB_SSL with ENABLE_BITTORRENT where they are not related to OpenSSL but BitTorrent. Removed instantiation of TrackerInitCommand. Instead, TrackerWatcherCommand is instantiated and pushed to the command queue. * src/InitiateConnectionCommandFactory.cc: Replaced HAVE_LIBSSL with ENABLE_SSL. * src/Request.cc: Replaced HAVE_LIBSSL with ENABLE_SSL. * src/RequestSlotMan.cc: (deleteCompletedRequestSlot) If a piece is already acquired by another command, delete the request slots for the piece. * src/TrackerUpdateCommand.cc: (execute) Changed log level of MSG_TRACKER_WARNING_MESSAGE from info to warn. Added a check whether peer list is null. Fixed the bug that causes sending completed event to the tracker several times. * src/TrackerInitCommand.cc: (execute) Fixed the bug that causes sending completed event to the tracker several times. * src/AbstractDiskWriter.{h,cc}: Removed direct dependency on OpenSSL by using messageDigest.h. 2006-03-26 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> * PeerConnection.cc: Replaced log message "keep-alive" with "keep alive". * PeerInteractionCommand.{h,cc}: Close connection if peer is choking localhost long time. * TorrentMan.cc: When adding new peer with duplicate = true, if the number of peer list is equal to or grater than MAX_PEER_LIST, delete at most 100 failure entry from the list. If with duplicate = false, MAX_PEER_LIST is not checked. * PeerListenCommand.cc: Fixed the argument order of log message. 2006-03-25 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> * Logger.h: Moved enum LEVEL from SimpleLogger.h to here. Added warn(). * SimpleLogger.h: Moved enum LEVEL to Logger.h. Implemented warn(). Defined 2 macros(WRITE_LOG, WRITE_LOG_EX) to avoid duplicated code.
2006-03-27 14:47:26 +00:00
#ifdef HAVE_LIBGNUTLS
# include <gnutls/gnutls.h>
#endif // HAVE_LIBGNUTLS
using namespace std;
class SocketCore {
friend class Socket;
private:
// socket endpoint descriptor
int sockfd;
// reference counter for this object.
int use;
bool secure;
#ifdef HAVE_LIBSSL
// for SSL
SSL_CTX* sslCtx;
SSL* ssl;
#endif // HAVE_LIBSSL
2006-03-27 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> * configure.in: Added gnutls support. Added several CPP macros. * m4/openssl.m4: Added. * src/SocketCore.{h,cc}: Drop const quarifier from writeData(), readData(), peekData(). Added gnutls support. * src/HttpDownloadCommand.cc: Removed SleepCommand.h * src/TrackerWatcherCommand.{h,cc}: Added. This command creates TrackerInitCommand periodicaly(TorrentMan::minInterval). * src/TorrentMan.cc: Remove downloadedSize == 0 check from save(). Instead, added a check for whether setup method has executed successfully. * src/TorrentMan.h: Added member vaiable setupComplete. Updated DEFAULT_ANNOUNCE_INTERVAL and DEFAULT_ANNOUNCE_MIN_INTERVAL to 300. * src/Makefile.am: Updated. * src/messageDigest.h: Added. This is a macro calculating SHA1 digest using whether OpenSSL or gcrypt, depending on the result of configure script. * src/ShaVisitor.{h,cc}: Removed direct dependency on OpenSSL by using messageDigest.h. * src/TorrentAutoSaveCommand.h: Removed unused variable cuid. * src/PeerListenCommand.cc: Added log about port binded successfully. Fixed memory leak. * src/main.cc: Added gnutls support. Replaced LIB_SSL with ENABLE_BITTORRENT where they are not related to OpenSSL but BitTorrent. Removed instantiation of TrackerInitCommand. Instead, TrackerWatcherCommand is instantiated and pushed to the command queue. * src/InitiateConnectionCommandFactory.cc: Replaced HAVE_LIBSSL with ENABLE_SSL. * src/Request.cc: Replaced HAVE_LIBSSL with ENABLE_SSL. * src/RequestSlotMan.cc: (deleteCompletedRequestSlot) If a piece is already acquired by another command, delete the request slots for the piece. * src/TrackerUpdateCommand.cc: (execute) Changed log level of MSG_TRACKER_WARNING_MESSAGE from info to warn. Added a check whether peer list is null. Fixed the bug that causes sending completed event to the tracker several times. * src/TrackerInitCommand.cc: (execute) Fixed the bug that causes sending completed event to the tracker several times. * src/AbstractDiskWriter.{h,cc}: Removed direct dependency on OpenSSL by using messageDigest.h. 2006-03-26 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> * PeerConnection.cc: Replaced log message "keep-alive" with "keep alive". * PeerInteractionCommand.{h,cc}: Close connection if peer is choking localhost long time. * TorrentMan.cc: When adding new peer with duplicate = true, if the number of peer list is equal to or grater than MAX_PEER_LIST, delete at most 100 failure entry from the list. If with duplicate = false, MAX_PEER_LIST is not checked. * PeerListenCommand.cc: Fixed the argument order of log message. 2006-03-25 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> * Logger.h: Moved enum LEVEL from SimpleLogger.h to here. Added warn(). * SimpleLogger.h: Moved enum LEVEL to Logger.h. Implemented warn(). Defined 2 macros(WRITE_LOG, WRITE_LOG_EX) to avoid duplicated code.
2006-03-27 14:47:26 +00:00
#ifdef HAVE_LIBGNUTLS
gnutls_session_t sslSession;
gnutls_certificate_credentials_t sslXcred;
char* peekBuf;
int peekBufLength;
int peekBufMax;
int shiftPeekData(char* data, int len);
void addPeekData(char* data, int len);
int gnutlsRecv(char* data, int len);
int gnutlsPeek(char* data, int len);
#endif // HAVE_LIBGNUTLS
2006-02-21 12:27:17 +00:00
void init();
SocketCore(int sockfd);
public:
SocketCore();
~SocketCore();
/**
* Creates a socket and listens form connection on it.
2006-03-21 14:12:51 +00:00
* @param port port to listen. If 0 is specified, os automaticaly
* choose avaiable port.
*/
2006-03-21 14:12:51 +00:00
void beginListen(int port = 0);
/**
* Stores host address and port of this socket to addrinfo.
* @param addrinfo placeholder to store host address and port.
*/
2006-02-21 12:27:17 +00:00
void getAddrInfo(pair<string, int>& addrinfo) const;
2006-03-21 14:12:51 +00:00
/**
* Stores peer's address and port to peerinfo.
* @param peerinfo placeholder to store peer's address and port.
*/
void getPeerInfo(pair<string, int>& peerinfo) const;
/**
* Accepts incoming connection on this socket.
* You must call beginListen() before calling this method.
* @return accepted socket. The caller must delete it after using it.
*/
2006-02-21 12:27:17 +00:00
SocketCore* acceptConnection() const;
/**
* Connects to the server named host and the destination port is port.
* This method makes socket non-blocking mode.
* To make the socket blocking mode again, call setBlockingMode() after
* the connection is established.
* @param host hostname or ip address to connect to
* @param port service port number to connect to
*/
void establishConnection(const string& host, int port);
2006-06-22 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> To make a listening socket non-block: * src/SocketCore.h (setNonBlockingMode): New function. * src/SocketCore.cc (setNonBlockingMode): New function. (beginListen): Added a call to setNonBlockingMode(). (acceptConnection): Call setNonBlockingMode(). (setBlockingMode): Updated. To handle tracker's NAT-checking: * src/PeerConnection.cc (receiveHandshake): 'msg' param is filled with received data and its length is assigned to 'length' param, even if all handshake message is not yet received. * src/PeerInteractionCommand.cc (executeInternal): Use peerInteraction->receiveHandshake(true) when a new peer connects to localhost. A call to peerInteraction->sendHandshake() was removed because it is called from peerInteraction->receiveHandshake(true). * src/PeerInteraction.h (quickReplied): New variable. (receiveHandshake): Added an argument. * src/PeerInteraction.cc (PeerInteraction): Added the initialization of quickReplied. (receiveHandshake): Send handshake message as soon as the info hash in a handshake message from a peer is correct. To change the random part of peer id and key parameter: * src/TorrentMan.h (key): New variable. * src/TorrentMan.cc (setupInternal1): Use Util::randomAlpha() to generate random part of peer id, which is now "-aria2-"+13 alphabet characters([A-Za-z]). key is also generated by Util::randomAlpha() and it is now 8 character long. * src/Util.h (randomAlpha): New function. * src/Util.cc (randomAlpha): New function. * src/TrackerWatcherCommand.cc (execute): In a tracker request: Use torrentMan->key as key parameter. Added no_peer_id=1. Set snubbing flag to false when a choke message is received from a snubbed peer. * src/ChokeMessage.cc (receivedAction): Set snubbing flag to false.
2006-06-22 15:26:18 +00:00
void setNonBlockingMode() const;
/**
* Makes this socket blocking mode.
*/
2006-02-21 12:27:17 +00:00
void setBlockingMode() const;
/**
* Closes the connection of this socket.
*/
void closeConnection();
/**
* Checks whether this socket is available for writing.
* @param timeout the amount of time elapsed before the checking are timed
* out.
* @return true if the socket is available for writing,
* otherwise returns false.
*/
2006-02-21 12:27:17 +00:00
bool isWritable(int timeout) const;
/**
* Checks whether this socket is available for reading.
* @param timeout the amount of time elapsed before the checking are timed
* out.
* @return true if the socket is available for reading,
* otherwise returns false.
*/
2006-02-21 12:27:17 +00:00
bool isReadable(int timeout) const;
/**
* Writes characters into this socket. data is a pointer pointing the first
* byte of the data and len is the length of data.
* This method internally calls isWritable(). The parmeter timeout is used
* for this method call.
* @param data data to write
* @param len length of data
*/
void writeData(const char* data, int len);
/**
* Reads up to len bytes from this socket.
* data is a pointer pointing the first
* byte of the data, which must be allocated before this method is called.
* len is the size of the allocated memory. When this method returns
* successfully, len is replaced by the size of the read data.
* This method internally calls isReadable(). The parameter timeout is used
* for this method call.
* @param data holder to store data.
* @param len the maximum size data can store. This method assigns
* the number of bytes read to len.
*/
void readData(char* data, int& len);
/**
* Reads up to len bytes from this socket, but bytes are not removed from
* this socket.
* This method internally calls isReadable(). The parameter timeout is used
* for this method call.
* @param data holder to store data.
* @param len the maximum size data can store. This method assigns
* the number of bytes read to len.
*/
void peekData(char* data, int& len);
/**
* Makes this socket secure.
* If the system has not OpenSSL, then this method do nothing.
* connection must be established before calling this method.
*/
2006-02-21 12:27:17 +00:00
void initiateSecureConnection() ;
};
#endif // _D_SOCKET_CORE_H_