1 /********************************************************************************
2 * Nepenthes
3 * - finest collection -
4 *
5 *
6 *
7 * Copyright (C) 2005 Paul Baecher & Markus Koetter
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 *
23 *
24 * contact nepenthesdev@users.sourceforge.net
25 *
26 *******************************************************************************/
27
28 /* $Id: CSendDialogue.cpp 2271 2006-01-14 20:31:52Z common $ */
29
30 #include <cstdlib>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
34 #include <netinet/in.h>
35
36 #include "CSendDialogue.hpp"
37
38 #include "UDPSocket.hpp"
39 #include "Download.hpp"
40 #include "DownloadUrl.hpp"
41 #include "DownloadBuffer.hpp"
42 #include "Message.hpp"
43 #include "LogManager.hpp"
44 #include "SubmitManager.hpp"
45
46
47 #ifdef STDTAGS
48 #undef STDTAGS
49 #endif
50 #define STDTAGS l_dl | l_dia | l_hlr
51
52
53 using namespace nepenthes;
54
55
56
CSendDialogue(Socket * socket)57 CSendDialogue::CSendDialogue(Socket *socket)
58 {
59 m_DialogueName = "CSendDialogue";
60 m_DialogueDescription = "download a file via csend variants";
61
62 m_Socket = socket;
63 m_ConsumeLevel = CL_ASSIGN;
64 m_CuttedOffset = false;
65
66 m_ExpectedFileSize = 0;
67 }
68
69
~CSendDialogue()70 CSendDialogue::~CSendDialogue()
71 {
72 logPF();
73 delete m_Download;
74 }
75
setDownload(Download * down)76 void CSendDialogue::setDownload(Download *down)
77 {
78 m_Download = down;
79 if (m_Download->getDownloadUrl()->getPath().size() == 0 || atoi(m_Download->getDownloadUrl()->getPath().c_str()) == 0) // if there is no offset, no need to cut it
80 {
81 m_CuttedOffset = true;
82 }
83 }
84
85
setMaxFileSize(uint32_t ul)86 void CSendDialogue::setMaxFileSize(uint32_t ul)
87 {
88 m_MaxFileSize = ul;
89 }
90
incomingData(Message * msg)91 ConsumeLevel CSendDialogue::incomingData(Message *msg)
92 {
93
94 logInfo("got %i bytes data\n",msg->getSize());
95 m_Download->getDownloadBuffer()->addData(msg->getMsg(),msg->getSize());
96 if (m_CuttedOffset == false)
97 {
98 uint32_t len = atoi(m_Download->getDownloadUrl()->getPath().c_str());
99 if (m_Download->getDownloadBuffer()->getSize() >= len )
100 {
101 if (len == 4 )
102 {
103 uint32_t expectedSize = *(uint32_t *)m_Download->getDownloadBuffer()->getData();
104 logSpam("Agobot CSend, leading 4 bytes are length ... (%i bytes)\n",expectedSize);
105 m_ExpectedFileSize = expectedSize;
106 }
107
108 logSpam("Removing %i bytes from buffer, as requested by urls path \nURL '%s'\nPATH %s\n",len, m_Download->getUrl().c_str(),
109 m_Download->getDownloadUrl()->getPath().c_str());
110 m_Download->getDownloadBuffer()->cutFront(len);
111 m_CuttedOffset = true;
112 }
113 }
114
115
116 return CL_ASSIGN;
117 }
118
outgoingData(Message * msg)119 ConsumeLevel CSendDialogue::outgoingData(Message *msg)
120 {
121 return CL_ASSIGN;
122 }
123
handleTimeout(Message * msg)124 ConsumeLevel CSendDialogue::handleTimeout(Message *msg)
125 {
126
127 return CL_DROP;
128 }
129
connectionLost(Message * msg)130 ConsumeLevel CSendDialogue::connectionLost(Message *msg)
131 {
132 return CL_DROP;
133 }
134
connectionShutdown(Message * msg)135 ConsumeLevel CSendDialogue::connectionShutdown(Message *msg)
136 {
137 logPF();
138 if (m_ExpectedFileSize > 0)
139 {
140 if (m_Download->getDownloadBuffer()->getSize() != m_ExpectedFileSize)
141 {
142 logInfo("CSend Filetransferr failed, expected %i bytes, got %i bytes\n",m_ExpectedFileSize,m_Download->getDownloadBuffer()->getSize());
143 return CL_DROP;
144 }
145 }
146 g_Nepenthes->getSubmitMgr()->addSubmission(m_Download);
147 return CL_DROP;
148 }
149