| 1 |
// License: BSD/LGPL |
| 2 |
// Copyright (C) 2011-2018 Thomas d'Otreppe |
| 3 |
using System; |
| 4 |
using System.Collections; |
| 5 |
using System.Collections.Generic; |
| 6 |
using System.Data; |
| 7 |
using System.IO; |
| 8 |
|
| 9 |
namespace WirelessPanda.Readers |
| 10 |
{ |
| 11 |
public class Reader |
| 12 |
{ |
| 13 |
public const string ACCESSPOINTS_DATATABLE = "Access Points"; |
| 14 |
public const string STATIONS_DATATABLE = "Stations"; |
| 15 |
|
| 16 |
|
| 17 |
#region Private members |
| 18 |
private DataSet _dataset = new DataSet(); |
| 19 |
private List<AccessPoint> _accessPoints = new List<AccessPoint>(); |
| 20 |
private List<Station> _stations = new List<Station>(); |
| 21 |
private string _filename = string.Empty; |
| 22 |
private bool _parseSuccess = false; |
| 23 |
#endregion |
| 24 |
|
| 25 |
#region Properties |
| 26 |
|
| 27 |
/// <summary> |
| 28 |
/// Returns true if the file exist |
| 29 |
/// </summary> |
| 30 |
public bool FileExist |
| 31 |
{ |
| 32 |
get |
| 33 |
{ |
| 34 |
return File.Exists(this._filename); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
/// <summary> |
| 39 |
/// DataSet containing 2 tables: "Access Points" and "Stations" |
| 40 |
/// </summary> |
| 41 |
public virtual DataSet Dataset |
| 42 |
{ |
| 43 |
get |
| 44 |
{ |
| 45 |
return this._dataset; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/// <summary> |
| 50 |
/// Was the file parsed successfully? |
| 51 |
/// </summary> |
| 52 |
public bool ParseSuccess |
| 53 |
{ |
| 54 |
get |
| 55 |
{ |
| 56 |
return this._parseSuccess; |
| 57 |
} |
| 58 |
protected set |
| 59 |
{ |
| 60 |
this._parseSuccess = value; |
| 61 |
} |
| 62 |
} |
| 63 |
/// <summary> |
| 64 |
/// Array of access points |
| 65 |
/// </summary> |
| 66 |
public virtual AccessPoint[] AccessPoints |
| 67 |
{ |
| 68 |
get |
| 69 |
{ |
| 70 |
return this._accessPoints.ToArray().Clone() as AccessPoint[]; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
/// <summary> |
| 76 |
/// Array of stations |
| 77 |
/// </summary> |
| 78 |
public virtual Station[] Stations |
| 79 |
{ |
| 80 |
get |
| 81 |
{ |
| 82 |
return this._stations.ToArray().Clone() as Station[]; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/// <summary> |
| 87 |
/// Filename |
| 88 |
/// </summary> |
| 89 |
public string Filename |
| 90 |
{ |
| 91 |
get |
| 92 |
{ |
| 93 |
return this._filename; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/// <summary> |
| 98 |
/// Reader type |
| 99 |
/// </summary> |
| 100 |
public virtual string ReaderType |
| 101 |
{ |
| 102 |
get |
| 103 |
{ |
| 104 |
return "Unknown"; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/// <summary> |
| 109 |
/// Reader type |
| 110 |
/// </summary> |
| 111 |
protected virtual string DATE_FORMAT |
| 112 |
{ |
| 113 |
get |
| 114 |
{ |
| 115 |
return null; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/// <summary> |
| 120 |
/// Reader type |
| 121 |
/// </summary> |
| 122 |
protected virtual string ALT_DATE_FORMAT |
| 123 |
{ |
| 124 |
get |
| 125 |
{ |
| 126 |
return null; |
| 127 |
} |
| 128 |
} |
| 129 |
#endregion |
| 130 |
|
| 131 |
/// <summary> |
| 132 |
/// Constructor |
| 133 |
/// </summary> |
| 134 |
/// <param name="filename">Filename (doesn't need to exist now but MUST when using Read() )</param> |
| 135 |
public Reader(string filename) |
| 136 |
{ |
| 137 |
if (string.IsNullOrEmpty(filename)) |
| 138 |
{ |
| 139 |
throw new FileNotFoundException("Filename cannot be null or empty"); |
| 140 |
} |
| 141 |
|
| 142 |
this._filename = filename; |
| 143 |
} |
| 144 |
|
| 145 |
protected void Clear() |
| 146 |
{ |
| 147 |
// Clear all the values and re-create datatables |
| 148 |
this._dataset.Tables.Clear(); |
| 149 |
this._accessPoints.Clear(); |
| 150 |
this._stations.Clear(); |
| 151 |
|
| 152 |
this._parseSuccess = false; |
| 153 |
} |
| 154 |
|
| 155 |
/// <summary> |
| 156 |
/// Open the file and returns its content |
| 157 |
/// </summary> |
| 158 |
/// <returns></returns> |
| 159 |
/// <exception cref="FileNotFoundException">File does not exist</exception> |
| 160 |
/// <exception cref="Exception">Fails to open file</exception> |
| 161 |
protected string[] getStrippedFileContent() |
| 162 |
{ |
| 163 |
if (string.IsNullOrEmpty(this.Filename)) |
| 164 |
{ |
| 165 |
throw new FileNotFoundException("Filename cannot be null or empty"); |
| 166 |
} |
| 167 |
|
| 168 |
FileInfo f = new FileInfo(this.Filename); |
| 169 |
if (!f.Exists) |
| 170 |
{ |
| 171 |
throw new FileNotFoundException("File <" + this.Filename + "> does not exist"); |
| 172 |
} |
| 173 |
|
| 174 |
// Returns an array with one empty string |
| 175 |
if (f.Length == 0) |
| 176 |
{ |
| 177 |
return new string[] { string.Empty }; |
| 178 |
} |
| 179 |
|
| 180 |
StreamReader sr = null; |
| 181 |
|
| 182 |
// Open the file |
| 183 |
try |
| 184 |
{ |
| 185 |
sr = f.OpenText(); |
| 186 |
} |
| 187 |
catch (Exception e) |
| 188 |
{ |
| 189 |
throw new Exception("Failed to open <" + this.Filename + ">", e); |
| 190 |
} |
| 191 |
|
| 192 |
List<string> lines = new List<string>(); |
| 193 |
|
| 194 |
// Read the file |
| 195 |
try |
| 196 |
{ |
| 197 |
while (!sr.EndOfStream) |
| 198 |
{ |
| 199 |
lines.Add(sr.ReadLine().Trim()); |
| 200 |
} |
| 201 |
} |
| 202 |
catch { /* Done or failure so stop */} |
| 203 |
|
| 204 |
// Close file |
| 205 |
try |
| 206 |
{ |
| 207 |
sr.Close(); |
| 208 |
} |
| 209 |
catch { } |
| 210 |
|
| 211 |
return lines.ToArray(); |
| 212 |
} |
| 213 |
|
| 214 |
/// <summary> |
| 215 |
/// Read/Update the content of the file |
| 216 |
/// </summary> |
| 217 |
/// <returns>true if successful</returns> |
| 218 |
public virtual bool Read() { return this.ParseSuccess; } |
| 219 |
|
| 220 |
/// <summary> |
| 221 |
/// Generate the columns for the DataTable from the Hashtable (and in a specific order if needed) |
| 222 |
/// </summary> |
| 223 |
/// <param name="ht"></param> |
| 224 |
/// <returns></returns> |
| 225 |
private DataColumn[] getColumnsFromHashtable(Hashtable ht, Hashtable order) |
| 226 |
{ |
| 227 |
List<DataColumn> columnList = new List<DataColumn>(); |
| 228 |
|
| 229 |
if (ht != null) |
| 230 |
{ |
| 231 |
if (order == null) |
| 232 |
{ |
| 233 |
// No specific order but that's not going to happen |
| 234 |
foreach (string key in ht.Keys) |
| 235 |
{ |
| 236 |
Type t = ht[key].GetType(); |
| 237 |
columnList.Add(new DataColumn(key, t)); |
| 238 |
} |
| 239 |
} |
| 240 |
else |
| 241 |
{ |
| 242 |
for (int i = 0; i < order.Count; i++) |
| 243 |
{ |
| 244 |
Type t = ht[(string)order[i]].GetType(); |
| 245 |
columnList.Add(new DataColumn((string)order[i], t)); |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
return columnList.ToArray(); |
| 251 |
} |
| 252 |
|
| 253 |
/// <summary> |
| 254 |
/// Add a station to the list |
| 255 |
/// </summary> |
| 256 |
/// <param name="s">Station</param> |
| 257 |
/// <returns></returns> |
| 258 |
protected bool addStation(Station s) |
| 259 |
{ |
| 260 |
if (s == null) |
| 261 |
{ |
| 262 |
return false; |
| 263 |
} |
| 264 |
|
| 265 |
// Create DataTable if needed |
| 266 |
if (!this._dataset.Tables.Contains(STATIONS_DATATABLE)) |
| 267 |
{ |
| 268 |
// Create Stations DataTable |
| 269 |
DataTable dtStations = new DataTable(STATIONS_DATATABLE); |
| 270 |
dtStations.CaseSensitive = true; |
| 271 |
|
| 272 |
// Create columns |
| 273 |
dtStations.Columns.AddRange(this.getColumnsFromHashtable(s.FieldsDictionary, s.FieldsOrder)); |
| 274 |
|
| 275 |
// And add it to the dataset |
| 276 |
this._dataset.Tables.Add(dtStations); |
| 277 |
} |
| 278 |
|
| 279 |
// Add row |
| 280 |
DataRow dr = this._dataset.Tables[STATIONS_DATATABLE].NewRow(); |
| 281 |
|
| 282 |
// Set value for each field |
| 283 |
foreach (string key in s.FieldsDictionary.Keys) |
| 284 |
{ |
| 285 |
dr[key] = s.FieldsDictionary[key]; |
| 286 |
} |
| 287 |
|
| 288 |
// Add row |
| 289 |
this._dataset.Tables[STATIONS_DATATABLE].Rows.Add(dr); |
| 290 |
|
| 291 |
// Add station to the list |
| 292 |
this._stations.Add(s); |
| 293 |
|
| 294 |
return true; |
| 295 |
} |
| 296 |
|
| 297 |
/// <summary> |
| 298 |
/// Link clients to their associated AP |
| 299 |
/// </summary> |
| 300 |
protected void LinkAPClients() |
| 301 |
{ |
| 302 |
foreach (Station s in this._stations) |
| 303 |
{ |
| 304 |
if (string.IsNullOrEmpty(s.BSSID)) |
| 305 |
{ |
| 306 |
continue; |
| 307 |
} |
| 308 |
|
| 309 |
foreach (AccessPoint ap in this._accessPoints) |
| 310 |
{ |
| 311 |
if (ap.BSSID == s.BSSID) |
| 312 |
{ |
| 313 |
ap.addClient(s); |
| 314 |
break; |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
//this._dataset.Tables[ACCESSPOINTS_DATATABLE].ChildRelations.Add(new DataRelation("Cients", this._dataset.Tables[ACCESSPOINTS_DATATABLE].Columns["BSSID"], this._dataset.Tables[STATIONS_DATATABLE].Columns["BSSID"])); |
| 320 |
//this._dataset.Tables[STATIONS_DATATABLE].ParentRelations.Add(new DataRelation("Associated AP", this._dataset.Tables[ACCESSPOINTS_DATATABLE].Columns["BSSID"], this._dataset.Tables[STATIONS_DATATABLE].Columns["BSSID"])); |
| 321 |
|
| 322 |
} |
| 323 |
|
| 324 |
/// <summary> |
| 325 |
/// Add Access Point to the list |
| 326 |
/// </summary> |
| 327 |
/// <param name="ap">Access Point</param> |
| 328 |
/// <returns></returns> |
| 329 |
protected bool addAccessPoint(AccessPoint ap) |
| 330 |
{ |
| 331 |
if (ap == null) |
| 332 |
{ |
| 333 |
return false; |
| 334 |
} |
| 335 |
|
| 336 |
// Create DataTable if needed |
| 337 |
if (!this._dataset.Tables.Contains(ACCESSPOINTS_DATATABLE)) |
| 338 |
{ |
| 339 |
// Create Access Points DataTable |
| 340 |
DataTable dtAPs = new DataTable(ACCESSPOINTS_DATATABLE); |
| 341 |
dtAPs.CaseSensitive = true; |
| 342 |
|
| 343 |
// Create columns |
| 344 |
dtAPs.Columns.AddRange(this.getColumnsFromHashtable(ap.FieldsDictionary, ap.FieldsOrder)); |
| 345 |
|
| 346 |
this._dataset.Tables.Add(dtAPs); |
| 347 |
} |
| 348 |
|
| 349 |
// Add row |
| 350 |
DataRow dr = this._dataset.Tables[ACCESSPOINTS_DATATABLE].NewRow(); |
| 351 |
|
| 352 |
foreach (string key in ap.FieldsDictionary.Keys) |
| 353 |
{ |
| 354 |
dr[key] = ap.FieldsDictionary[key]; |
| 355 |
} |
| 356 |
|
| 357 |
// Add row |
| 358 |
this._dataset.Tables[ACCESSPOINTS_DATATABLE].Rows.Add(dr); |
| 359 |
|
| 360 |
// Add the Access Point to the list |
| 361 |
this._accessPoints.Add(ap); |
| 362 |
|
| 363 |
return true; |
| 364 |
} |
| 365 |
|
| 366 |
/// <summary> |
| 367 |
/// Return the type of the file (and obviously, also the "name" of the reader to use |
| 368 |
/// </summary> |
| 369 |
/// <param name="path">Path to the file</param> |
| 370 |
/// <returns>Null if type is unknown or a string with the type</returns> |
| 371 |
public static string getFileType(string path) |
| 372 |
{ |
| 373 |
Reader r = new CsvReader(path); |
| 374 |
|
| 375 |
try |
| 376 |
{ |
| 377 |
r.Read(); |
| 378 |
} |
| 379 |
catch |
| 380 |
{ |
| 381 |
r = new KismetCsvReader(path); |
| 382 |
|
| 383 |
try |
| 384 |
{ |
| 385 |
r.Read(); |
| 386 |
} |
| 387 |
catch |
| 388 |
{ |
| 389 |
r = new NetXMLReader(path); |
| 390 |
|
| 391 |
try |
| 392 |
{ |
| 393 |
r.Read(); |
| 394 |
} |
| 395 |
catch { } |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
if (!r.ParseSuccess) |
| 400 |
{ |
| 401 |
return null; |
| 402 |
} |
| 403 |
|
| 404 |
return r.ReaderType; |
| 405 |
} |
| 406 |
|
| 407 |
/// <summary> |
| 408 |
/// Parse a string containing the date and time |
| 409 |
/// </summary> |
| 410 |
/// <param name="s">Date string</param> |
| 411 |
/// <returns>DateTime value</returns> |
| 412 |
/// <exception cref="ArgumentNullException">Date/Time string cannot be null or empty</exception> |
| 413 |
/// <exception cref="FormatException">Date Format is not set</exception> |
| 414 |
protected DateTime parseDateTime(string s) |
| 415 |
{ |
| 416 |
if (string.IsNullOrEmpty(this.DATE_FORMAT)) |
| 417 |
{ |
| 418 |
throw new FormatException("Date Format is not set"); |
| 419 |
} |
| 420 |
|
| 421 |
if (string.IsNullOrEmpty(s)) |
| 422 |
{ |
| 423 |
throw new ArgumentNullException("Date/Time string cannot be null or empty"); |
| 424 |
} |
| 425 |
|
| 426 |
// Parse it |
| 427 |
DateTime ret = new DateTime(); |
| 428 |
|
| 429 |
try |
| 430 |
{ |
| 431 |
ret = DateTime.ParseExact(s.Trim(), DATE_FORMAT, null); |
| 432 |
} |
| 433 |
catch |
| 434 |
{ |
| 435 |
ret = DateTime.ParseExact(s.Trim(), ALT_DATE_FORMAT, null); |
| 436 |
} |
| 437 |
|
| 438 |
return ret; |
| 439 |
} |
| 440 |
} |
| 441 |
} |