File: Reader.cs

package info (click to toggle)
aircrack-ng 1%3A1.7%2Bgit20230807.4bf83f1a-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 21,812 kB
  • sloc: ansic: 69,601; cs: 5,392; sh: 3,924; python: 2,647; pascal: 1,242; makefile: 257; cpp: 43
file content (441 lines) | stat: -rw-r--r-- 12,961 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// License: BSD/LGPL
// Copyright (C) 2011-2018 Thomas d'Otreppe
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;

namespace WirelessPanda.Readers
{
    public class Reader
    {
        public const string ACCESSPOINTS_DATATABLE = "Access Points";
        public const string STATIONS_DATATABLE = "Stations";


        #region Private members
        private DataSet _dataset = new DataSet();
        private List<AccessPoint> _accessPoints = new List<AccessPoint>();
        private List<Station> _stations = new List<Station>();
        private string _filename = string.Empty;
        private bool _parseSuccess = false;
        #endregion

        #region Properties

        /// <summary>
        /// Returns true if the file exist
        /// </summary>
        public bool FileExist
        {
            get
            {
                return File.Exists(this._filename);
            }
        }

        /// <summary>
        /// DataSet containing 2 tables: "Access Points" and "Stations"
        /// </summary>
        public virtual DataSet Dataset
        {
            get
            {
                return this._dataset;
            }
        }

        /// <summary>
        /// Was the file parsed successfully?
        /// </summary>
        public bool ParseSuccess
        {
            get
            {
                return this._parseSuccess;
            }
            protected set
            {
                this._parseSuccess = value;
            }
        }
        /// <summary>
        /// Array of access points
        /// </summary>
        public virtual AccessPoint[] AccessPoints
        {
            get
            {
                return this._accessPoints.ToArray().Clone() as AccessPoint[];
            }
        }
        

        /// <summary>
        /// Array of stations
        /// </summary>
        public virtual Station[] Stations
        {
            get
            {
                return this._stations.ToArray().Clone() as Station[];
            }
        }

        /// <summary>
        /// Filename
        /// </summary>
        public string Filename
        {
            get
            {
                return this._filename;
            }
        }

        /// <summary>
        /// Reader type
        /// </summary>
        public virtual string ReaderType
        {
            get
            {
                return "Unknown";
            }
        }

        /// <summary>
        /// Reader type
        /// </summary>
        protected virtual string DATE_FORMAT
        {
            get
            {
                return null;
            }
        }

        /// <summary>
        /// Reader type
        /// </summary>
        protected virtual string ALT_DATE_FORMAT
        {
            get
            {
                return null;
            }
        }
        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="filename">Filename (doesn't need to exist now but MUST when using Read() )</param>
        public Reader(string filename)
        {
            if (string.IsNullOrEmpty(filename))
            {
                throw new FileNotFoundException("Filename cannot be null or empty");
            }

            this._filename = filename;
        }

        protected void Clear()
        {
            // Clear all the values and re-create datatables
            this._dataset.Tables.Clear();
            this._accessPoints.Clear();
            this._stations.Clear();

            this._parseSuccess = false;
        }

        /// <summary>
        /// Open the file and returns its content
        /// </summary>
        /// <returns></returns>
        /// <exception cref="FileNotFoundException">File does not exist</exception>
        /// <exception cref="Exception">Fails to open file</exception>
        protected string[] getStrippedFileContent()
        {
            if (string.IsNullOrEmpty(this.Filename))
            {
                throw new FileNotFoundException("Filename cannot be null or empty");
            }

            FileInfo f = new FileInfo(this.Filename);
            if (!f.Exists)
            {
                throw new FileNotFoundException("File <" + this.Filename + "> does not exist");
            }

            // Returns an array with one empty string
            if (f.Length == 0)
            {
                return new string[] { string.Empty };
            }

            StreamReader sr = null;

            // Open the file
            try
            {
                sr = f.OpenText();
            }
            catch (Exception e)
            {
                throw new Exception("Failed to open <" + this.Filename + ">", e);
            }

            List<string> lines = new List<string>();

            // Read the file
            try
            {
                while (!sr.EndOfStream)
                {
                    lines.Add(sr.ReadLine().Trim());
                }
            }
            catch { /* Done or failure so stop */}

            // Close file
            try
            {
                sr.Close();
            }
            catch { }

            return lines.ToArray();
        }

        /// <summary>
        /// Read/Update the content of the file
        /// </summary>
        /// <returns>true if successful</returns>
        public virtual bool Read() { return this.ParseSuccess; }

        /// <summary>
        /// Generate the columns for the DataTable from the Hashtable (and in a specific order if needed)
        /// </summary>
        /// <param name="ht"></param>
        /// <returns></returns>
        private DataColumn[] getColumnsFromHashtable(Hashtable ht, Hashtable order)
        {
            List<DataColumn> columnList = new List<DataColumn>();

            if (ht != null)
            {
                if (order == null)
                {
                    // No specific order but that's not going to happen
                    foreach (string key in ht.Keys)
                    {
                        Type t = ht[key].GetType();
                        columnList.Add(new DataColumn(key, t));
                    }
                }
                else
                {
                    for (int i = 0; i < order.Count; i++)
                    {
                        Type t = ht[(string)order[i]].GetType();
                        columnList.Add(new DataColumn((string)order[i], t));
                    }
                }
            }

            return columnList.ToArray();
        }

        /// <summary>
        /// Add a station to the list
        /// </summary>
        /// <param name="s">Station</param>
        /// <returns></returns>
        protected bool addStation(Station s)
        {
            if (s == null)
            {
                return false;
            }

            // Create DataTable if needed
            if (!this._dataset.Tables.Contains(STATIONS_DATATABLE))
            {
                // Create Stations DataTable
                DataTable dtStations = new DataTable(STATIONS_DATATABLE);
                dtStations.CaseSensitive = true;
                
                // Create columns
                dtStations.Columns.AddRange(this.getColumnsFromHashtable(s.FieldsDictionary, s.FieldsOrder));
                
                // And add it to the dataset
                this._dataset.Tables.Add(dtStations);
            }

            // Add row
            DataRow dr = this._dataset.Tables[STATIONS_DATATABLE].NewRow();

            // Set value for each field
            foreach (string key in s.FieldsDictionary.Keys)
            {
                dr[key] = s.FieldsDictionary[key];
            }

            // Add row
            this._dataset.Tables[STATIONS_DATATABLE].Rows.Add(dr);

            // Add station to the list
            this._stations.Add(s);

            return true;
        }

        /// <summary>
        /// Link clients to their associated AP
        /// </summary>
        protected void LinkAPClients()
        {
            foreach (Station s in this._stations)
            {
                if (string.IsNullOrEmpty(s.BSSID))
                {
                    continue;
                }

                foreach (AccessPoint ap in this._accessPoints)
                {
                    if (ap.BSSID == s.BSSID)
                    {
                        ap.addClient(s);
                        break;
                    }
                }
            }

            //this._dataset.Tables[ACCESSPOINTS_DATATABLE].ChildRelations.Add(new DataRelation("Cients", this._dataset.Tables[ACCESSPOINTS_DATATABLE].Columns["BSSID"], this._dataset.Tables[STATIONS_DATATABLE].Columns["BSSID"]));
            //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"]));

        }

        /// <summary>
        /// Add Access Point to the list
        /// </summary>
        /// <param name="ap">Access Point</param>
        /// <returns></returns>
        protected bool addAccessPoint(AccessPoint ap)
        {
            if (ap == null)
            {
                return false;
            }

            // Create DataTable if needed
            if (!this._dataset.Tables.Contains(ACCESSPOINTS_DATATABLE))
            {
                // Create Access Points DataTable
                DataTable dtAPs = new DataTable(ACCESSPOINTS_DATATABLE);
                dtAPs.CaseSensitive = true;
                
                // Create columns
                dtAPs.Columns.AddRange(this.getColumnsFromHashtable(ap.FieldsDictionary, ap.FieldsOrder));

                this._dataset.Tables.Add(dtAPs);
            }

            // Add row
            DataRow dr = this._dataset.Tables[ACCESSPOINTS_DATATABLE].NewRow();

            foreach (string key in ap.FieldsDictionary.Keys)
            {
                dr[key] = ap.FieldsDictionary[key];
            }

            // Add row
            this._dataset.Tables[ACCESSPOINTS_DATATABLE].Rows.Add(dr);

            // Add the Access Point to the list
            this._accessPoints.Add(ap);

            return true;
        }

        /// <summary>
        /// Return the type of the file (and obviously, also the "name" of the reader to use
        /// </summary>
        /// <param name="path">Path to the file</param>
        /// <returns>Null if type is unknown or a string with the type</returns>
        public static string getFileType(string path)
        {
            Reader r = new CsvReader(path);

            try
            {
                r.Read();
            }
            catch 
            {
                r = new KismetCsvReader(path);

                try
                {
                    r.Read();
                }
                catch 
                {
                    r = new NetXMLReader(path);

                    try
                    {
                        r.Read();
                    }
                    catch { }
                }
            }

            if (!r.ParseSuccess)
            {
                return null;
            }

            return r.ReaderType;
        }

        /// <summary>
        /// Parse a string containing the date and time
        /// </summary>
        /// <param name="s">Date string</param>
        /// <returns>DateTime value</returns>
        /// <exception cref="ArgumentNullException">Date/Time string cannot be null or empty</exception>
        /// <exception cref="FormatException">Date Format is not set</exception>
        protected DateTime parseDateTime(string s)
        {
            if (string.IsNullOrEmpty(this.DATE_FORMAT))
            {
                throw new FormatException("Date Format is not set");
            }

            if (string.IsNullOrEmpty(s))
            {
                throw new ArgumentNullException("Date/Time string cannot be null or empty");
            }

            // Parse it
            DateTime ret = new DateTime();

            try
            {
                ret = DateTime.ParseExact(s.Trim(), DATE_FORMAT, null);
            }
            catch 
            {
                ret = DateTime.ParseExact(s.Trim(), ALT_DATE_FORMAT, null);
            }

            return ret;
        }
    }
}