File: tcpclient.h

package info (click to toggle)
kismet 2008-05-R1-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,232 kB
  • ctags: 3,998
  • sloc: cpp: 33,568; sh: 5,544; ansic: 459; makefile: 457; perl: 62; sql: 41
file content (288 lines) | stat: -rw-r--r-- 8,210 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
/*
    This file is part of Kismet

    Kismet is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Kismet is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Kismet; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#ifndef __TCPCLIENT_H__
#define __TCPCLIENT_H__

#include "config.h"

#include <stdio.h>
#include <string>
#include <vector>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <fcntl.h>
#include <errno.h>

// Prototype tcpclient class
class TcpClient;

#include "packet.h"
#include "tracktypes.h"

#define TCP_SELECT_TIMEOUT 100

#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 64
#endif

#define LINKQ_MAX 100
#define LEVEL_MIN -255
#define LEVEL_MAX 255
#define NOISE_MIN -255
#define NOISE_MAX 255
#define SNR_MAX   50

#define CLIENT_NOTIFY  2
#define CLIENT_ALERT   4

// TCP Client.  Simple nonblocking client to extract network info exported by the
// Kismet TCP server component and parse it.  Frontends should include this.

class TcpClient {
public:
    typedef struct alert_info {
        timeval alert_ts;
        string alert_text;
    };

    // Sort alerts by alert time
    class SortAlerts {
    public:
        inline bool operator() (const TcpClient::alert_info x, const TcpClient::alert_info y) const {
            if ((x.alert_ts.tv_sec > y.alert_ts.tv_sec) ||
                ((x.alert_ts.tv_sec== y.alert_ts.tv_sec) && (x.alert_ts.tv_usec > y.alert_ts.tv_usec)))
                return 1;
            return 0;
        }
    };

    typedef struct string_info {
        mac_addr bssid;
        mac_addr source;
        timeval string_ts;
        string text;
    };

    // Sort strings by alert time
    class SortStrings {
    public:
        inline bool operator() (const TcpClient::string_info x, const TcpClient::string_info y) const {
            if ((x.string_ts.tv_sec > y.string_ts.tv_sec) ||
                ((x.string_ts.tv_sec== y.string_ts.tv_sec) && (x.string_ts.tv_usec > y.string_ts.tv_usec)))
                return 1;
            return 0;
        }
    };

    typedef struct card_info {
        string interface;
        string type;
        string username;
        int channel;
        int id;
        int packets;
        int hopping;
    };

    TcpClient();
    ~TcpClient();

    int Valid() { return sv_valid; };

    int Connect(short int in_port, char *in_host);

    void Disconnect();

    int FetchDescriptor() { return client_fd; }

    int Poll();

    // Enable a protocol - this lets clients based off our code pick what protocols they
    // support.
    void EnableProtocol(string in_protocol);
    // Disable a protocol
    void RemoveProtocol(string in_protocol);

    // Fetch the location
    int FetchLoc(float *in_lat, float *in_lon, float *in_alt, float *in_spd, float *in_hed, int *in_mode);
    // Fetch the mode
    int FetchMode() { return mode; }

    // Fetch the time reported by the server
    time_t FetchServTime();
    // Fetch a vector of all the BSSID's reported to us
    vector<wireless_network *> FetchNetworkList();
    // Get the most recently touched n-th networks
    vector<wireless_network *> FetchNthRecent(unsigned int n);

    // List of cards
    vector<card_info *> FetchCardList();

    short int FetchPort() { return port; }
    char *FetchHost() { return hostname; }

    char *FetchError() { return errstr; }

    string FetchServername() { return servername; }

    int FetchHopping() { return channel_hop; }

    int FetchNumNetworks() { return num_networks; }
    int FetchNumPackets() { return num_packets; }
    int FetchNumCrypt() { return num_crypt; }
    int FetchNumInteresting() { return num_interesting; }
    int FetchNumNoise() { return num_noise; }
    int FetchNumDropped() { return num_dropped; }

    int FetchDeltaNumNetworks() { return num_networks - old_num_networks; }
    int FetchDeltaNumPackets() { return num_packets - old_num_packets; }
    int FetchDeltaNumCrypt() { return num_crypt - old_num_crypt; }
    int FetchDeltaNumInteresting() { return num_interesting - old_num_interesting; }
    int FetchDeltaNumNoise() { return num_noise - old_num_noise; }
    int FetchDeltaNumDropped() { return num_dropped - old_num_dropped; }

    int FetchPacketRate() { return packet_rate; }

    char *FetchMajor() { return major; }
    char *FetchMinor() { return minor; }
    char *FetchTiny() { return tiny; }
    char *FetchBuild() { return build; }
    time_t FetchStart() { return start_time; }
    time_t FetchTime() { return serv_time; }

    char *FetchStatus() { return status; }

    int FetchPower() { return power; }
    int FetchQuality() { return quality; }
    int FetchNoise() { return noise; }

    int FetchChannelPower(int in_channel);

    int GetMaxStrings() { return maxstrings; }
    void SetMaxStrings(int in_max) {
        maxstrings = in_max;
        if (strings.size() > maxstrings)
            strings.erase(strings.begin(), strings.begin() + (strings.size() - maxstrings));
    }
    vector<string_info> FetchStrings() { return strings; }
    void ClearStrings() { strings.clear(); }

    int GetMaxPackInfos() { return maxpackinfos; }
    void SetMaxPackInfos(int in_max) {
        maxpackinfos = in_max;
        if (packinfos.size() > maxpackinfos)
            packinfos.erase(packinfos.begin(), packinfos.begin() + (packinfos.size() - maxpackinfos));
    }
    vector<packet_info> FetchPackInfos() { return packinfos; }
    void ClearPackInfos() { packinfos.clear(); }

    int GetMaxAlerts() { return maxalerts; }
    void SetMaxAlerts(int in_max) {
        maxalerts = in_max;
        if (alerts.size() > maxalerts)
            alerts.erase(alerts.begin(), alerts.begin() + (alerts.size() - maxalerts));
    }
    vector <alert_info> FetchAlerts() { return alerts; }
    void ClearAlarms() { alerts.clear(); }

    void RemoveNetwork(mac_addr in_bssid);

    wireless_network *FetchLastNewNetwork() { return last_new_network; }

    void SendRaw(const char *in_cmd);

    int FetchNetworkDirty();

protected:
    char errstr[1024];
    char status[STATUS_MAX];

    int Send(const char *in_data);
    int ParseData(char *in_data);

    // Active server
    int sv_valid;

    // Server info
    short int port;
    char hostname[MAXHOSTNAMELEN];

    int client_fd;
    FILE *clientf;

    struct sockaddr_in client_sock, local_sock;
    struct hostent *client_host;

    // Data sent to us
    // GPS
    float lat, lon, alt, spd, heading;
    int mode;
    // Timestampt
    time_t serv_time;

    map<mac_addr, wireless_network *> net_map;
    vector<wireless_network *> net_map_vec;
    wireless_network *last_new_network;

    map<string, card_info *> card_map;
    vector<card_info *> card_map_vec;

    int num_networks, num_packets, num_crypt,
        num_interesting, num_noise, num_dropped, packet_rate;

    int old_num_networks, old_num_packets, old_num_crypt,
        old_num_interesting, old_num_noise, old_num_dropped, old_packet_rate;

    char major[24], minor[24], tiny[24];
    char build[24];
    time_t start_time;

    int power, quality, noise;

    unsigned int maxstrings, maxpackinfos, maxalerts;

    vector<string_info> strings;
    vector<packet_info> packinfos;
    vector<alert_info> alerts;

    channel_power channel_graph[CHANNEL_MAX];

    string writebuf;

    char servername[32];

    // Protocols we have enabled so that we can replay them on a reconnect
    map<string, int> protocol_map;

    // Fields we enable (what we know how to parse)
    map<string, string> protocol_default_map;

    int network_dirty;

    int channel_hop;

};

#endif