2006-02-17 14:47:45 +00:00
|
|
|
/* <!-- 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>
|
2006-02-17 18:51:12 +00:00
|
|
|
#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
|
2006-02-17 14:47:45 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
class SocketCore {
|
|
|
|
friend class Socket;
|
|
|
|
private:
|
|
|
|
// socket endpoint descriptor
|
|
|
|
int sockfd;
|
|
|
|
// reference counter for this object.
|
|
|
|
int use;
|
2006-02-17 18:51:12 +00:00
|
|
|
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);
|
2006-02-17 14:47:45 +00:00
|
|
|
public:
|
|
|
|
SocketCore();
|
|
|
|
~SocketCore();
|
|
|
|
|
2006-03-01 07:00:39 +00:00
|
|
|
/**
|
|
|
|
* 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-01 07:00:39 +00:00
|
|
|
*/
|
2006-03-21 14:12:51 +00:00
|
|
|
void beginListen(int port = 0);
|
2006-03-01 07:00:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2006-03-01 07:00:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2006-03-01 07:00:39 +00:00
|
|
|
|
2006-02-17 14:47:45 +00:00
|
|
|
/**
|
|
|
|
* Connects to the server named host and the destination port is port.
|
2006-03-01 07:00:39 +00:00
|
|
|
* This method makes socket non-blocking mode.
|
|
|
|
* To make the socket blocking mode again, call setBlockingMode() after
|
2006-02-17 14:47:45 +00:00
|
|
|
* the connection is established.
|
2006-03-01 07:00:39 +00:00
|
|
|
* @param host hostname or ip address to connect to
|
|
|
|
* @param port service port number to connect to
|
2006-02-17 14:47:45 +00:00
|
|
|
*/
|
2006-04-19 17:49:03 +00:00
|
|
|
void establishConnection(const string& host, int port);
|
2006-02-17 14:47:45 +00:00
|
|
|
|
2006-06-22 15:26:18 +00:00
|
|
|
void setNonBlockingMode() const;
|
|
|
|
|
2006-03-01 07:00:39 +00:00
|
|
|
/**
|
|
|
|
* Makes this socket blocking mode.
|
|
|
|
*/
|
2006-02-21 12:27:17 +00:00
|
|
|
void setBlockingMode() const;
|
2006-02-17 14:47:45 +00:00
|
|
|
|
2006-03-01 07:00:39 +00:00
|
|
|
/**
|
|
|
|
* Closes the connection of this socket.
|
|
|
|
*/
|
2006-02-17 14:47:45 +00:00
|
|
|
void closeConnection();
|
|
|
|
|
2006-03-01 07:00:39 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
2006-02-17 14:47:45 +00:00
|
|
|
|
2006-03-01 07:00:39 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
2006-02-17 14:47:45 +00:00
|
|
|
|
2006-03-01 07:00:39 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2006-04-29 11:23:39 +00:00
|
|
|
void writeData(const char* data, int len);
|
2006-02-17 14:47:45 +00:00
|
|
|
|
2006-03-01 07:00:39 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2006-04-29 11:23:39 +00:00
|
|
|
void readData(char* data, int& len);
|
2006-03-01 07:00:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2006-04-29 11:23:39 +00:00
|
|
|
void peekData(char* data, int& len);
|
2006-02-17 18:51:12 +00:00
|
|
|
|
|
|
|
/**
|
2006-02-17 19:04:22 +00:00
|
|
|
* Makes this socket secure.
|
|
|
|
* If the system has not OpenSSL, then this method do nothing.
|
2006-03-01 07:00:39 +00:00
|
|
|
* connection must be established before calling this method.
|
2006-02-17 18:51:12 +00:00
|
|
|
*/
|
2006-02-21 12:27:17 +00:00
|
|
|
void initiateSecureConnection() ;
|
2006-02-17 14:47:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _D_SOCKET_CORE_H_
|