1 // libTorrent - BitTorrent library
2 // Copyright (C) 2005-2011, Jari Sundell
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 //
18 // In addition, as a special exception, the copyright holders give
19 // permission to link the code of portions of this program with the
20 // OpenSSL library under certain conditions as described in each
21 // individual source file, and distribute linked combinations
22 // including the two.
23 //
24 // You must obey the GNU General Public License in all respects for
25 // all of the code used other than OpenSSL.  If you modify file(s)
26 // with this exception, you may extend this exception to your version
27 // of the file(s), but you are not obligated to do so.  If you do not
28 // wish to do so, delete this exception statement from your version.
29 // If you delete this exception statement from all source files in the
30 // program, then also delete it here.
31 //
32 // Contact:  Jari Sundell <jaris@ifi.uio.no>
33 //
34 //           Skomakerveien 33
35 //           3185 Skoppum, NORWAY
36 
37 #ifndef LIBTORRENT_TRACKER_TRACKER_UDP_H
38 #define LIBTORRENT_TRACKER_TRACKER_UDP_H
39 
40 #include <array>
41 #include <rak/socket_address.h>
42 
43 #include "net/protocol_buffer.h"
44 #include "net/socket_datagram.h"
45 #include "torrent/connection_manager.h"
46 #include "torrent/tracker.h"
47 
48 #include "globals.h"
49 
50 namespace torrent {
51 
52 class TrackerUdp : public SocketDatagram, public Tracker {
53 public:
54   typedef std::array<char, 1024> hostname_type;
55 
56   typedef ProtocolBuffer<512> ReadBuffer;
57   typedef ProtocolBuffer<512> WriteBuffer;
58 
59   typedef ConnectionManager::slot_resolver_result_type resolver_type;
60 
61   static const uint64_t magic_connection_id = 0x0000041727101980ll;
62 
63   TrackerUdp(TrackerList* parent, const std::string& url, int flags);
64   ~TrackerUdp();
65 
type_name()66   const char*         type_name() const { return "tracker_udp"; }
67 
68   virtual bool        is_busy() const;
69 
70   virtual void        send_state(int state);
71 
72   virtual void        close();
73   virtual void        disown();
74 
75   virtual Type        type() const;
76 
77   virtual void        event_read();
78   virtual void        event_write();
79   virtual void        event_error();
80 
81 private:
82   void                close_directly();
83 
84   void                receive_failed(const std::string& msg);
85   void                receive_timeout();
86 
87   void                start_announce(const sockaddr* sa, int err);
88 
89   void                prepare_connect_input();
90   void                prepare_announce_input();
91 
92   bool                process_connect_output();
93   bool                process_announce_output();
94   bool                process_error_output();
95 
96   bool                parse_udp_url(const std::string& url, hostname_type& hostname, int& port) const;
97   resolver_type*      make_resolver_slot(const hostname_type& hostname);
98 
99   rak::socket_address m_connectAddress;
100   int                 m_port;
101 
102   int                 m_sendState;
103 
104   resolver_type*      m_slot_resolver;
105 
106   uint32_t            m_action;
107   uint64_t            m_connectionId;
108   uint32_t            m_transactionId;
109 
110   ReadBuffer*         m_readBuffer;
111   WriteBuffer*        m_writeBuffer;
112 
113   uint32_t            m_tries;
114 
115   rak::priority_item  m_taskTimeout;
116 };
117 
118 }
119 
120 #endif
121