Branch data Line data Source code
1 : : // $Id:$
2 : : //
3 : : // This code contributed by Nadi Sarrar.
4 : :
5 : : #include "BitTorrent.h"
6 : : #include "TCP_Reassembler.h"
7 : :
8 : 0 : BitTorrent_Analyzer::BitTorrent_Analyzer(Connection* c)
9 : 0 : : TCP_ApplicationAnalyzer(AnalyzerTag::BitTorrent, c)
10 : : {
11 : 0 : interp = new binpac::BitTorrent::BitTorrent_Conn(this);
12 : 0 : stop_orig = stop_resp = false;
13 : 0 : stream_len_orig = stream_len_resp = 0;
14 : 0 : }
15 : :
16 : 0 : BitTorrent_Analyzer::~BitTorrent_Analyzer()
17 : : {
18 [ # # ][ # # ]: 0 : delete interp;
[ # # ]
19 [ # # ][ # # ]: 0 : }
[ # # ]
20 : :
21 : 0 : void BitTorrent_Analyzer::Done()
22 : : {
23 : 0 : TCP_ApplicationAnalyzer::Done();
24 : :
25 : 0 : interp->FlowEOF(true);
26 : 0 : interp->FlowEOF(false);
27 : 0 : }
28 : :
29 : 0 : void BitTorrent_Analyzer::DeliverStream(int len, const u_char* data, bool orig)
30 : : {
31 [ # # ]: 0 : uint64& this_stream_len = orig ? stream_len_orig : stream_len_resp;
32 [ # # ]: 0 : bool& this_stop = orig ? stop_orig : stop_resp;
33 : :
34 : 0 : TCP_ApplicationAnalyzer::DeliverStream(len, data, orig);
35 : :
36 [ # # ]: 0 : assert(TCP());
37 : :
38 [ # # ]: 0 : if ( TCP()->IsPartial() )
39 : : // punt on partial.
40 : 0 : return;
41 : :
42 [ # # ]: 0 : if ( this_stop )
43 : 0 : return;
44 : :
45 : 0 : this_stream_len += len;
46 : :
47 : : try
48 : : {
49 : 0 : interp->NewData(orig, data, data + len);
50 : : }
51 : 0 : catch ( binpac::Exception const &e )
52 : : {
53 : 0 : const char except[] = "binpac exception: invalid handshake";
54 [ # # ]: 0 : if ( ! strncmp(e.c_msg(), except, strlen(except)) )
55 : : // Does not look like bittorrent - silently
56 : : // drop the connection.
57 : 0 : Parent()->RemoveChildAnalyzer(this);
58 : : else
59 : : {
60 : : DeliverWeird(fmt("Stopping BitTorrent analysis: protocol violation (%s)",
61 : 0 : e.c_msg()), orig);
62 : 0 : this_stop = true;
63 [ # # # # ]: 0 : if ( stop_orig && stop_resp )
64 : 0 : ProtocolViolation("BitTorrent: content gap and/or protocol violation");
65 : : }
66 : : }
67 : : }
68 : :
69 : 0 : void BitTorrent_Analyzer::Undelivered(int seq, int len, bool orig)
70 : : {
71 : : uint64 entry_offset = orig ?
72 : : *interp->upflow()->next_message_offset() :
73 [ # # ]: 0 : *interp->downflow()->next_message_offset();
74 [ # # ]: 0 : uint64& this_stream_len = orig ? stream_len_orig : stream_len_resp;
75 [ # # ]: 0 : bool& this_stop = orig ? stop_orig : stop_resp;
76 : :
77 : 0 : TCP_ApplicationAnalyzer::Undelivered(seq, len, orig);
78 : :
79 : 0 : this_stream_len += len;
80 : :
81 [ # # ]: 0 : if ( entry_offset < this_stream_len )
82 : : { // entry point is somewhere in the gap
83 : 0 : DeliverWeird("Stopping BitTorrent analysis: cannot recover from content gap", orig);
84 : 0 : this_stop = true;
85 [ # # # # ]: 0 : if ( stop_orig && stop_resp )
86 : 0 : ProtocolViolation("BitTorrent: content gap and/or protocol violation");
87 : : }
88 : : else
89 : : { // fill the gap
90 : : try
91 : : {
92 : 0 : u_char gap[len];
93 : 0 : memset(gap, 0, len);
94 : 0 : interp->NewData(orig, gap, gap + len);
95 : : }
96 : 0 : catch ( binpac::Exception const &e )
97 : : {
98 : 0 : DeliverWeird("Stopping BitTorrent analysis: filling content gap failed", orig);
99 : 0 : this_stop = true;
100 [ # # # # ]: 0 : if ( stop_orig && stop_resp )
101 : 0 : ProtocolViolation("BitTorrent: content gap and/or protocol violation");
102 : : }
103 : : }
104 : 0 : }
105 : :
106 : 0 : void BitTorrent_Analyzer::EndpointEOF(TCP_Reassembler* endp)
107 : : {
108 : 0 : TCP_ApplicationAnalyzer::EndpointEOF(endp);
109 : 0 : interp->FlowEOF(endp->IsOrig());
110 : 0 : }
111 : :
112 : 0 : void BitTorrent_Analyzer::DeliverWeird(const char* msg, bool orig)
113 : : {
114 [ # # ]: 0 : if ( bittorrent_peer_weird )
115 : : {
116 : 0 : val_list* vl = new val_list;
117 : 0 : vl->append(BuildConnVal());
118 : 0 : vl->append(new Val(orig, TYPE_BOOL));
119 : 0 : vl->append(new StringVal(msg));
120 : 0 : ConnectionEvent(bittorrent_peer_weird, vl);
121 : : }
122 : : else
123 : 0 : Weird(msg);
124 [ + - ][ + - ]: 6 : }
|