Branch data Line data Source code
1 : : // $Id:$
2 : : //
3 : : // This code contributed by Nadi Sarrar.
4 : :
5 : : #ifndef bittorrenttracker_h
6 : : #define bittorrenttracker_h
7 : :
8 : : #include "TCP.h"
9 : :
10 : : #define BTTRACKER_BUF 2048
11 : :
12 : : // If the following is defined, then the analyzer will store all of
13 : : // the headers seen in tracker messages.
14 : : //#define BTTRACKER_STORE_HEADERS 1
15 : :
16 : : enum btt_states {
17 : : BTT_REQ_GET,
18 : : BTT_REQ_HEADER,
19 : : BTT_REQ_DONE,
20 : :
21 : : BTT_RES_STATUS,
22 : : BTT_RES_HEADER,
23 : : BTT_RES_BODY,
24 : : BTT_RES_DONE,
25 : : };
26 : :
27 : : // "benc" = Bencode ("Bee-Encode"), per http://en.wikipedia.org/wiki/Bencode
28 : : enum btt_benc_types {
29 : : BENC_TYPE_INT = 0,
30 : : BENC_TYPE_STR = 1,
31 : : BENC_TYPE_DIR = 2,
32 : : BENC_TYPE_LIST = 3,
33 : : BENC_TYPE_NONE = 10,
34 : : };
35 : :
36 : : enum btt_benc_states {
37 : : BENC_STATE_EMPTY,
38 : : BENC_STATE_INT1,
39 : : BENC_STATE_INT2,
40 : : BENC_STATE_INT3,
41 : : BENC_STATE_STR1,
42 : : BENC_STATE_STR2,
43 : : };
44 : :
45 : : class BitTorrentTracker_Analyzer : public TCP_ApplicationAnalyzer {
46 : : public:
47 : : BitTorrentTracker_Analyzer(Connection* conn);
48 : : virtual ~BitTorrentTracker_Analyzer();
49 : :
50 : : virtual void Done();
51 : : virtual void DeliverStream(int len, const u_char* data, bool orig);
52 : : virtual void Undelivered(int seq, int len, bool orig);
53 : : virtual void EndpointEOF(TCP_Reassembler* endp);
54 : :
55 : 0 : static Analyzer* InstantiateAnalyzer(Connection* conn)
56 : 0 : { return new BitTorrentTracker_Analyzer(conn); }
57 : :
58 : 1 : static bool Available()
59 [ + - ][ - + ]: 1 : { return bt_tracker_request || bt_tracker_response; }
60 : :
61 : : protected:
62 : : void ClientRequest(int len, const u_char* data);
63 : : void ServerReply(int len, const u_char* data);
64 : :
65 : : void InitBencParser(void);
66 : :
67 : : void DeliverWeird(const char* msg, bool orig);
68 : :
69 : : bool ParseRequest(char* line);
70 : : void RequestGet(char* uri);
71 : 0 : void RequestHeader(char* name, char* value)
72 : 0 : { ParseHeader(name, value, true); }
73 : : void EmitRequest(void);
74 : :
75 : : bool ParseResponse(char* line);
76 : : void ResponseStatus(char* status);
77 : 0 : void ResponseHeader(char* name, char* value)
78 : 0 : { ParseHeader(name, value, false); }
79 : : void ResponseBody(void);
80 : : void ResponseBenc(int name_len, char* name, enum btt_benc_types type,
81 : : int value_len, char* value);
82 : : void ResponseBenc(int name_len, char* name, enum btt_benc_types type,
83 : : bro_int_t value);
84 : : int ResponseParseBenc(void);
85 : : void EmitResponse(void);
86 : :
87 : : void ParseHeader(char* name, char* value, bool is_request);
88 : :
89 : : // HTTP state.
90 : : bool keep_alive;
91 : :
92 : : // Request.
93 : : enum btt_states req_state;
94 : : char req_buf[BTTRACKER_BUF];
95 : : char* req_buf_pos;
96 : : unsigned int req_buf_len;
97 : : StringVal* req_val_uri;
98 : : TableVal* req_val_headers;
99 : :
100 : : // Response.
101 : : enum btt_states res_state;
102 : : bool res_allow_blank_line;
103 : : char res_buf[BTTRACKER_BUF];
104 : : char* res_buf_pos;
105 : : unsigned int res_buf_len;
106 : : bro_uint_t res_status;
107 : : TableVal* res_val_headers;
108 : : TableVal* res_val_peers;
109 : : TableVal* res_val_benc;
110 : :
111 : : vector<char> benc_stack;
112 : : vector<unsigned int> benc_count;
113 : : enum btt_benc_states benc_state;
114 : :
115 : : char* benc_raw;
116 : : enum btt_benc_types benc_raw_type;
117 : : unsigned int benc_raw_len;
118 : :
119 : : char* benc_key;
120 : : unsigned int benc_key_len;
121 : :
122 : : char* benc_strlen;
123 : : char* benc_str;
124 : : unsigned int benc_str_len;
125 : : unsigned int benc_str_have;
126 : :
127 : : char* benc_int;
128 : : bro_int_t benc_int_val;
129 : :
130 : : // True on protocol violation.
131 : : bool stop_orig, stop_resp;
132 : : };
133 : :
134 : : #endif
|