root/honeyclient/branches/rel/1.0/Capture2/capture-client-xeno-mod/FileUploader.cpp

Revision 823, 6.8 kB (checked in by xkovah, 1 year ago)

adding the files finally

Line 
1 #include "FileUploader.h"
2
3 FileUploader::FileUploader(Server* s)
4 {
5     server = s;
6     hFileAcknowledged = CreateEvent(NULL, FALSE, FALSE, NULL);
7     fileAccepted = false;
8     fileOpened = false;
9     busy = false;
10
11     onReceiveFileOkEventConnection = EventController::getInstance()->connect_onServerEvent(L"file-ok", boost::bind(&FileUploader::onReceiveFileOkEvent, this, _1));
12     onReceiveFileErrorEventConnection = EventController::getInstance()->connect_onServerEvent(L"file-error", boost::bind(&FileUploader::onReceiveFileErrorEvent, this, _1));
13     onReceiveFileAcceptEventConnection = EventController::getInstance()->connect_onServerEvent(L"file-accept", boost::bind(&FileUploader::onReceiveFileAcceptEvent, this, _1));
14     onReceiveFileRejectEventConnection = EventController::getInstance()->connect_onServerEvent(L"file-reject", boost::bind(&FileUploader::onReceiveFileRejectEvent, this, _1));
15 }
16
17 FileUploader::~FileUploader(void)
18 {
19     onReceiveFileOkEventConnection.disconnect();
20     onReceiveFileErrorEventConnection.disconnect();
21     onReceiveFileAcceptEventConnection.disconnect();
22     onReceiveFileRejectEventConnection.disconnect();
23
24     if(fileOpened)
25     {
26         fclose(pFileStream);
27     }
28 }
29
30 void
31 FileUploader::onReceiveFileOkEvent(Element* pElement)
32 {
33
34 }
35
36 void
37 FileUploader::onReceiveFileErrorEvent(Element* pElement)
38 {
39     int start = 0;
40     int end = 0;
41     vector<Attribute>::iterator it;
42     for(it = pElement->attributes.begin(); it != pElement->attributes.end(); it++)
43     {
44         if(it->name == L"part-start") {
45             start = boost::lexical_cast<int>(it->value);
46         } else if(it->name == L"part-end") {
47             end = boost::lexical_cast<int>(it->value);
48         }
49     }
50
51     /* If the part-start and part-end attributes are set then send that part again */
52     if((start != 0) && (end != 0))
53     {
54         int size = end - start;
55         sendFilePart(start, end);
56     } else {
57         /* TODO General error */
58     }
59 }
60
61 void
62 FileUploader::onReceiveFileAcceptEvent(Element* pElement)
63 {
64     fileAccepted = true;
65     SetEvent(hFileAcknowledged);
66 }
67
68 void
69 FileUploader::onReceiveFileRejectEvent(Element* pElement)
70 {
71     SetEvent(hFileAcknowledged);
72 }
73
74 BOOL
75 FileUploader::getFileSize(wstring file, PLARGE_INTEGER fileSize)
76 {
77     /* Open the file and get is size */
78     HANDLE hFile;
79     hFile = CreateFile(file.c_str(),    // file to open
80                    GENERIC_READ,          // open for reading
81                    FILE_SHARE_READ,       // share for reading
82                    NULL,                  // default security
83                    OPEN_EXISTING,         // existing file only
84                    FILE_ATTRIBUTE_NORMAL, // normal file
85                    NULL);                 // no attr. template
86     if(hFile ==  INVALID_HANDLE_VALUE)
87     {
88         printf("FileUploader: ERROR - Could get file size: %08x\n", GetLastError());
89         return false;
90     } else {
91         BOOL gotSize = FALSE;
92         gotSize = GetFileSizeEx(hFile, fileSize);
93         CloseHandle(hFile);
94         return gotSize;
95     }
96 }
97
98 bool
99 FileUploader::sendFile(wstring file)
100 {
101     if(!server->isConnected())
102     {
103         printf("FileUploader: ERROR - Not connected to server so not sending file\n");
104         return false;
105     }
106
107     busy = true;
108
109     Attribute fileNameAttribute;
110     Attribute fileSizeAttribute;
111     Attribute fileTypeAttribute;
112     fileName = file;
113     errno_t error;
114    
115
116     /* Get the file size */
117     if(!getFileSize(file, &fileSize))
118     {
119         busy = false;
120         return false;
121     }
122
123     error = _wfopen_s( &pFileStream, file.c_str(), L"rb");
124     if(error != 0)
125     {
126         printf("FileUploader: ERROR - Could not open file: %08x\n", error);
127         busy = false;
128         return false;
129     } else {
130         fileOpened = true;
131     }
132    
133     queue<Attribute> atts;
134     fileNameAttribute.name = L"name";
135     fileNameAttribute.value = file;
136     fileSizeAttribute.name = L"size";
137     fileSizeAttribute.value = boost::lexical_cast<wstring>(fileSize.QuadPart);
138     fileTypeAttribute.name = L"type";
139     fileTypeAttribute.value = file.substr(file.find_last_of(L".")+1);
140     atts.push(fileNameAttribute);
141     atts.push(fileSizeAttribute);
142     atts.push(fileTypeAttribute);
143    
144     /* Send request to server to accept a file */
145     server->sendXML(L"file", &atts);
146
147     /* Wait for the server to accept the file or timeout if it fails
148        to respond or rejects it */
149    
150     DWORD timeout = WaitForSingleObject(hFileAcknowledged, 5000);
151
152     if((timeout == WAIT_TIMEOUT) || !fileAccepted)
153     {
154         printf("FileUploader: ERROR - Server did not acknowledge the request to receive a file\n");
155         busy = false;
156         if(fileOpened)
157         {
158             fclose(pFileStream);
159             fileOpened = false;
160         }
161         return false;   
162     }   
163  
164     printf("FileUplodaer: Sending file: %ls\n", fileName.c_str());
165
166     /* Loop sending the file inside <file-part /> xml messages */
167     size_t offset = 0; 
168     size_t bytesRead;
169     do
170     {
171         bytesRead = sendFilePart((UINT)offset, 8192);
172         offset += bytesRead;
173     } while(bytesRead > 0);
174
175     atts.push(fileNameAttribute);
176     atts.push(fileSizeAttribute);
177
178     server->sendXML(L"file-finished", &atts);
179
180     fclose(pFileStream);
181     fileOpened = false;
182     busy = false;
183     return true;
184 }
185
186 size_t
187 FileUploader::sendFilePart(unsigned int offset, unsigned int size)
188 {
189     if(fileOpened)
190     {
191         char *pFilePart = (char*)malloc(size);
192         Element* pElement = new Element();
193         pElement->name = L"part";
194    
195         Attribute fileNameAttribute;
196         Attribute filePartStartAttribute;
197         Attribute filePartEndAttribute;
198         Attribute filePartEncoding;
199
200         fileNameAttribute.name = L"name";
201         fileNameAttribute.value = fileName.c_str();
202         filePartStartAttribute.name = L"part-start";
203         filePartStartAttribute.value = boost::lexical_cast<wstring>(offset);
204         filePartEncoding.name = L"encoding";
205         filePartEncoding.value = L"base64";
206
207         pElement->attributes.push_back(fileNameAttribute);
208         pElement->attributes.push_back(filePartStartAttribute);
209         pElement->attributes.push_back(filePartEncoding);
210        
211
212         /* Seek to offset and read size bytes */
213         //printf("size: %i\n", size);
214         //printf("offset: %i\n", offset);
215         int err = fseek(pFileStream, offset, 0);
216         //printf("fseel: %i\n", err);
217         size_t bytesRead = fread(pFilePart , sizeof(char), size, pFileStream);
218         //printf("bytesRead: %i\n", bytesRead);
219         //printf("fread error: %i\n", ferror(pFileStream));
220         filePartEndAttribute.name = L"part-end";
221         int end = offset + bytesRead;
222         filePartEndAttribute.value = boost::lexical_cast<wstring>(end);
223         pElement->attributes.push_back(filePartEndAttribute);
224
225         /* Encode the data just read in base64 and append to XML element */
226         size_t encodedLength = 0;
227         char* pEncodedFilePart = encode_base64(pFilePart, bytesRead, &encodedLength);
228         pElement->data = pEncodedFilePart;
229         pElement->dataLength = encodedLength;
230
231         /* Send the XML element to the server */
232         //printf("feof: %i\n", feof(pFileStream));
233         if(bytesRead > 0)
234         {
235             server->sendXMLElement(pElement);
236         }
237
238         /* Cleanup */
239         free(pEncodedFilePart);
240         free(pFilePart);
241         delete pElement;
242         if(feof(pFileStream) > 0)
243         {
244             return 0;
245         } else {
246             return bytesRead;
247         }
248     }
249     return 0;
250 }
Note: See TracBrowser for help on using the browser.