[22] | 1 | /** |
---|
| 2 | * @file CommonTrafficCaptureMessages.cpp |
---|
| 3 | * @author Tomas Urban |
---|
| 4 | * @version 0.3 |
---|
| 5 | * @date 23/07/2009 |
---|
| 6 | */ |
---|
| 7 | #include "CommonTrafficCaptureMessages.h" |
---|
| 8 | |
---|
| 9 | OpenDeviceRequest::OpenDeviceRequest() : |
---|
| 10 | m_captureType(ECaptureType_PCAP), |
---|
| 11 | m_captureMode(ECaptureMode_Live) |
---|
| 12 | { |
---|
| 13 | m_timestamp.tv_sec = 0; |
---|
| 14 | m_timestamp.tv_usec = 0; |
---|
| 15 | } |
---|
| 16 | |
---|
| 17 | void OpenDeviceRequest::SetCaptureType(ECaptureType captureType) |
---|
| 18 | { |
---|
| 19 | DisposeEncodedData(); |
---|
| 20 | m_captureType = captureType; |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | void OpenDeviceRequest::SetCaptureMode(ECaptureMode captureMode) |
---|
| 24 | { |
---|
| 25 | DisposeEncodedData(); |
---|
| 26 | m_captureMode = captureMode; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | void OpenDeviceRequest::SetTimestamp(struct timeval timestamp) |
---|
| 30 | { |
---|
| 31 | DisposeEncodedData(); |
---|
| 32 | m_timestamp = timestamp; |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | void OpenDeviceRequest::SetParameters(const std::string & sParams) |
---|
| 36 | { |
---|
| 37 | DisposeEncodedData(); |
---|
| 38 | m_sParams = sParams; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | void OpenDeviceRequest::SetCaptureFile(const std::string & sFile) |
---|
| 42 | { |
---|
| 43 | DisposeEncodedData(); |
---|
| 44 | m_sCaptureFile = sFile; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | unsigned int OpenDeviceRequest::CalculateDataLength() |
---|
| 48 | { |
---|
| 49 | return TrafficCaptureMessage::CalculateDataLength() + |
---|
| 50 | 1 + // capture type |
---|
| 51 | 1 + // capture mode |
---|
| 52 | TIME_SIZE + // timestamp is forced to 32bits |
---|
| 53 | CalculateStringLength(m_sParams) + // parameters |
---|
| 54 | CalculateStringLength(m_sCaptureFile); // file name for saving captured data |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | void OpenDeviceRequest::EncodePayload(unsigned int & nOffset) |
---|
| 58 | { |
---|
| 59 | TrafficCaptureMessage::EncodePayload(nOffset); |
---|
| 60 | EncodeUChar(static_cast<unsigned char>(m_captureType), nOffset); |
---|
| 61 | EncodeUChar(static_cast<unsigned char>(m_captureMode), nOffset); |
---|
| 62 | EncodeTime(m_timestamp, nOffset); |
---|
| 63 | EncodeString(m_sParams, nOffset); |
---|
| 64 | EncodeString(m_sCaptureFile, nOffset); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | bool OpenDeviceRequest::DecodePayload(const char * pPayload, unsigned int nPayloadLength, |
---|
| 68 | unsigned int & nOffset) |
---|
| 69 | { |
---|
| 70 | if (!TrafficCaptureMessage::DecodePayload(pPayload, nPayloadLength, nOffset)) |
---|
| 71 | return false; |
---|
| 72 | unsigned char c; |
---|
| 73 | if (!DecodeUChar(c, pPayload, nPayloadLength, nOffset)) |
---|
| 74 | return false; |
---|
| 75 | m_captureType = static_cast<ECaptureType>(c); |
---|
| 76 | if (!DecodeUChar(c, pPayload, nPayloadLength, nOffset)) |
---|
| 77 | return false; |
---|
| 78 | m_captureMode = static_cast<ECaptureMode>(c); |
---|
| 79 | if (!DecodeTime(m_timestamp, pPayload, nPayloadLength, nOffset)) |
---|
| 80 | return false; |
---|
| 81 | if (!DecodeString(m_sParams, pPayload, nPayloadLength, nOffset)) |
---|
| 82 | return false; |
---|
| 83 | if (!DecodeString(m_sCaptureFile, pPayload, nPayloadLength, nOffset)) |
---|
| 84 | return false; |
---|
| 85 | return true; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | OpenDeviceReply::OpenDeviceReply() : |
---|
| 89 | m_result(ECaptureInit_Failed) |
---|
| 90 | { |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | void OpenDeviceReply::SetResult(ECaptureInitResult result) |
---|
| 94 | { |
---|
| 95 | DisposeEncodedData(); |
---|
| 96 | m_result = result; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | unsigned int OpenDeviceReply::CalculateDataLength() |
---|
| 100 | { |
---|
| 101 | return TrafficCaptureMessage::CalculateDataLength() + |
---|
| 102 | 1; // operation result |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | void OpenDeviceReply::EncodePayload(unsigned int & nOffset) |
---|
| 106 | { |
---|
| 107 | TrafficCaptureMessage::EncodePayload(nOffset); |
---|
| 108 | EncodeUChar(static_cast<unsigned char>(m_result), nOffset); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | bool OpenDeviceReply::DecodePayload(const char * pPayload, unsigned int nPayloadLength, |
---|
| 112 | unsigned int & nOffset) |
---|
| 113 | { |
---|
| 114 | if (!TrafficCaptureMessage::DecodePayload(pPayload, nPayloadLength, nOffset)) |
---|
| 115 | return false; |
---|
| 116 | unsigned char c; |
---|
| 117 | if (!DecodeUChar(c, pPayload, nPayloadLength, nOffset)) |
---|
| 118 | return false; |
---|
| 119 | m_result = static_cast<ECaptureInitResult>(c); |
---|
| 120 | return true; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | void SetFilterRequest::SetFilter(const std::string & sFilter) |
---|
| 125 | { |
---|
| 126 | DisposeEncodedData(); |
---|
| 127 | m_sFilter = sFilter; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | unsigned int SetFilterRequest::CalculateDataLength() |
---|
| 131 | { |
---|
| 132 | return TrafficCaptureMessage::CalculateDataLength() + |
---|
| 133 | CalculateStringLength(m_sFilter); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | void SetFilterRequest::EncodePayload(unsigned int & nOffset) |
---|
| 137 | { |
---|
| 138 | TrafficCaptureMessage::EncodePayload(nOffset); |
---|
| 139 | EncodeString(m_sFilter, nOffset); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | bool SetFilterRequest::DecodePayload(const char * pPayload, unsigned int nPayloadLength, |
---|
| 143 | unsigned int & nOffset) |
---|
| 144 | { |
---|
| 145 | if (!TrafficCaptureMessage::DecodePayload(pPayload, nPayloadLength, nOffset)) |
---|
| 146 | return false; |
---|
| 147 | if (!DecodeString(m_sFilter, pPayload, nPayloadLength, nOffset)) |
---|
| 148 | return false; |
---|
| 149 | return true; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | void MergePcapFilesRequest::SetMergeSource(const std::string & sMergePath, |
---|
| 153 | const std::vector<std::string> & vsMergeFiles) |
---|
| 154 | { |
---|
| 155 | DisposeEncodedData(); |
---|
| 156 | m_sMergePath = sMergePath; |
---|
| 157 | m_vsMergeFiles = vsMergeFiles; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | void MergePcapFilesRequest::SetMergecapDirectory(const std::string & sPath) |
---|
| 162 | { |
---|
| 163 | DisposeEncodedData(); |
---|
| 164 | m_sMergecapPath = sPath; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | unsigned int MergePcapFilesRequest::CalculateDataLength() |
---|
| 168 | { |
---|
| 169 | int nLen = TrafficCaptureMessage::CalculateDataLength() + |
---|
| 170 | CalculateStringLength(m_sMergecapPath) + |
---|
| 171 | CalculateStringLength(m_sMergePath) + |
---|
| 172 | sizeof(unsigned int); |
---|
| 173 | int nCount = m_vsMergeFiles.size(); |
---|
| 174 | for (int i = 0; i < nCount; ++i) |
---|
| 175 | nLen += CalculateStringLength(m_vsMergeFiles[i]); |
---|
| 176 | return nLen; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | void MergePcapFilesRequest::EncodePayload(unsigned int & nOffset) |
---|
| 180 | { |
---|
| 181 | TrafficCaptureMessage::EncodePayload(nOffset); |
---|
| 182 | EncodeString(m_sMergecapPath, nOffset); |
---|
| 183 | EncodeString(m_sMergePath, nOffset); |
---|
| 184 | unsigned int nCount = m_vsMergeFiles.size(); |
---|
| 185 | EncodeUInt(nCount, nOffset); |
---|
| 186 | for (unsigned int i = 0; i < nCount; ++i) |
---|
| 187 | EncodeString(m_vsMergeFiles[i], nOffset); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | bool MergePcapFilesRequest::DecodePayload(const char * pPayload, unsigned int nPayloadLength, |
---|
| 191 | unsigned int & nOffset) |
---|
| 192 | { |
---|
| 193 | if (!TrafficCaptureMessage::DecodePayload(pPayload, nPayloadLength, nOffset)) |
---|
| 194 | return false; |
---|
| 195 | if (!DecodeString(m_sMergecapPath, pPayload, nPayloadLength, nOffset)) |
---|
| 196 | return false; |
---|
| 197 | if (!DecodeString(m_sMergePath, pPayload, nPayloadLength, nOffset)) |
---|
| 198 | return false; |
---|
| 199 | unsigned int nCount = 0; |
---|
| 200 | if (!DecodeUInt(nCount, pPayload, nPayloadLength, nOffset)) |
---|
| 201 | return false; |
---|
| 202 | m_vsMergeFiles.clear(); |
---|
| 203 | for (unsigned int i = 0; i < nCount; ++i) |
---|
| 204 | { |
---|
| 205 | m_vsMergeFiles.push_back(""); |
---|
| 206 | if (!DecodeString(m_vsMergeFiles[i], pPayload, nPayloadLength, nOffset)) |
---|
| 207 | return false; |
---|
| 208 | } |
---|
| 209 | return true; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | void MergePcapFilesReply::SetMergeFile(const std::string & sMergeFile) |
---|
| 213 | { |
---|
| 214 | DisposeEncodedData(); |
---|
| 215 | m_sMergeFile = sMergeFile; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | unsigned int MergePcapFilesReply::CalculateDataLength() |
---|
| 219 | { |
---|
| 220 | int nLen = TrafficCaptureMessage::CalculateDataLength(); |
---|
| 221 | if (GetResult()) |
---|
| 222 | nLen += CalculateStringLength(m_sMergeFile); |
---|
| 223 | return nLen; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | void MergePcapFilesReply::EncodePayload(unsigned int & nOffset) |
---|
| 227 | { |
---|
| 228 | TrafficCaptureMessage::EncodePayload(nOffset); |
---|
| 229 | if (GetResult()) |
---|
| 230 | EncodeString(m_sMergeFile, nOffset); |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | bool MergePcapFilesReply::DecodePayload(const char * pPayload, unsigned int nPayloadLength, |
---|
| 234 | unsigned int & nOffset) |
---|
| 235 | { |
---|
| 236 | if (!TrafficCaptureMessage::DecodePayload(pPayload, nPayloadLength, nOffset)) |
---|
| 237 | return false; |
---|
| 238 | if (GetResult() && !DecodeString(m_sMergeFile, pPayload, nPayloadLength, nOffset)) |
---|
| 239 | return false; |
---|
| 240 | return true; |
---|
| 241 | } |
---|