1 /*
2     This file is part of Kismet
3 
4     Kismet is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8 
9     Kismet is distributed in the hope that it will be useful,
10       but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with Kismet; if not, write to the Free Software
16     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18 
19 #ifndef __KIS_PANEL_FRONTEND_H__
20 #define __KIS_PANEL_FRONTEND_H__
21 
22 #include "config.h"
23 
24 // Panel has to be here to pass configure, so just test these
25 #if (defined(HAVE_LIBNCURSES) || defined (HAVE_LIBCURSES))
26 
27 #include <stdio.h>
28 #include <string>
29 #include <vector>
30 #include <map>
31 
32 #include "uuid.h"
33 
34 #include "pollable.h"
35 #include "messagebus.h"
36 #include "kis_panel_widgets.h"
37 #include "kis_panel_windows.h"
38 
39 #include "kis_clinetframe.h"
40 
41 #include "kis_panel_plugin.h"
42 
43 #include "configfile.h"
44 
45 #include "popenclient.h"
46 #include "text_cliframe.h"
47 
48 #define WIN_CENTER(h, w)	(LINES / 2) - ((h) / 2), (COLS / 2) - ((w) / 2), (h), (w)
49 
50 class KisPanelInterface;
51 
52 // Our specialized actual kismet frontend
53 // Most of the drawing is inherited from the generic case panel interface,
54 // but we need to add our own tracking systems and such here.
55 //
56 // This also implements all the hooks which get linked to the clients to
57 // process protocols.
58 
59 #define KPI_ADDCLI_CB_PARMS		GlobalRegistry *globalreg, KisNetClient *netcli, \
60 	int add, void *auxptr
61 typedef void (*KPI_AddCli_Callback)(KPI_ADDCLI_CB_PARMS);
62 
63 // Plugin version information, v1
64 // This holds revision information for the KISMET THE PLUGIN WAS COMPILED WITH,
65 // NOT THE PLUGIN VERSION (plugin version is passed in the info struct!)
66 struct panel_plugin_revision {
67 	// V1 data
68 
69 	// Versioned for possible updates to the version api
70 	int version_api_revision;
71 
72 	string major;
73 	string minor;
74 	string tiny;
75 
76 	// End V1 data
77 };
78 
79 #define KIS_PANEL_PLUGIN_VREVISION		1
80 
81 // Plugin revision call.  If the kis_plugin_revision  symbol is available in the plugin,
82 // then it will be passed an allocated plugin_revision struct, with the version_api_rev
83 // set appropriately.  Plugins MUST ONLY use fields in the negotiated plugin version
84 // record.  This record is not expected to change significantly over time, BUT IT MAY,
85 // should it become necessary to add more complex data.
86 typedef void (*panel_plugin_revisioncall)(panel_plugin_revision *);
87 
88 class KisPanelInterface : public PanelInterface {
89 public:
90 	KisPanelInterface();
91 	KisPanelInterface(GlobalRegistry *in_globalreg);
92 	virtual ~KisPanelInterface();
93 
94 	virtual int MergeSet(int in_max_fd, fd_set *out_rset, fd_set *out_wset);
95 
96 	virtual int Poll(fd_set& in_rset, fd_set& in_wset);
97 
98 	virtual void Shutdown();
99 
100 	virtual void AddPanel(Kis_Panel *in_panel);
101 	virtual void KillPanel(Kis_Panel *in_panel);
102 
103 	virtual int LoadPreferences();
104 	virtual int SavePreferences();
105 
106 	// Connect to a network client & register callbacks for when one is added
107 	virtual int AddNetClient(string in_host, int in_reconnect);
108 	virtual void RemoveNetClient();
109 
110 	virtual int Add_NetCli_AddCli_CB(KPI_AddCli_Callback in_cb, void *in_aux);
111 	virtual void Remove_Netcli_AddCli_CB(int in_cbref);
112 	virtual void Remove_All_Netcli_Conf_CB(CliConf_Callback in_cb);
113 	virtual void Remove_All_Netcli_Cmd_CB(CliCmd_Callback in_cb, void *in_aux);
114 	virtual int Remove_All_Netcli_ProtoHandler(string in_proto,
115 											   CliProto_Callback in_cb,
116 											   void *in_aux);
117 
118 	// Fetch the client
FetchNetClient()119 	KisNetClient *FetchNetClient() { return network_client; }
120 
121 	// Are we connected to a client?
FetchNetConnected()122 	int FetchNetConnected() {
123 		if (network_client && network_client->Valid())
124 			return 1;
125 		return 0;
126 	}
127 
128 	// Configured client callback
129 	virtual void NetClientConfigure(KisNetClient *in_cli, int in_recon);
130 
131 	// Bring up a modal alert (may be queued if an alert is already displayed)
132 	virtual void RaiseAlert(string in_title, string in_text);
133 	// Queue a modal panel, we only display one modal panel at a time.
134 	// Alerts are modal, prompt boxes should almost always be considered modal.
135 	virtual void QueueModalPanel(Kis_Panel *in_panel);
136 
137 	// We track cards at the interface level because we need instant feedback on them
138 	// without waiting for individual widgets to do their own activate and poll, though
139 	// a widget CAN still directly talk the SOURCE protocol if it needs to
140 	struct knc_card {
141 		// Last time this record got updated
142 		time_t last_update;
143 
144 		// Hash for the UUID, used as a placeholder in the select table since
145 		// we need just an int there.  We hope this never collides, and if it
146 		// does, we'll figure out some other way to deal with this
147 		uint32_t uuid_hash;
148 
149 		string interface;
150 		string type;
151 		string name;
152 
153 		// We need a copy of this anyhow
154 		uuid carduuid;
155 
156 		int channel;
157 		int packets;
158 		int hopping;
159 		int hopvelocity;
160 		int dwell;
161 
162 		struct timeval hop_tm;
163 
164 		// Store as a string since we don't necessarily care
165 		string channellist;
166 
167 		// Are we in an error state?
168 		int error;
169 
170 		// Do we have a warning?
171 		string warning;
172 	};
173 
174 	struct knc_alert {
175 		struct timeval tv;
176 		string alertname;
177 		mac_addr bssid, source, dest, other;
178 		int channel;
179 		string text;
180 	};
181 
182 	// Internal parser for ALERT proto
183 	void proto_ALERT(CLIPROTO_CB_PARMS);
FetchAlertVec()184 	vector<KisPanelInterface::knc_alert *> *FetchAlertVec() { return &alert_vec; }
185 
186 	// Internal parser for the CARD proto, linked to the callback
187 	void proto_SOURCE(CLIPROTO_CB_PARMS);
188 	// Fetch the list of cards from the system
189 	map<uuid, KisPanelInterface::knc_card *> *FetchNetCardMap();
190 
191 	void proto_INFO(CLIPROTO_CB_PARMS);
192 
193 	struct addcli_cb_rec {
194 		int refnum;
195 		KPI_AddCli_Callback cb;
196 		void *auxptr;
197 	};
198 
199 	void LoadPlugin(string in_fname, string in_objname);
FetchPluginVec()200 	vector<panel_plugin_meta *> *FetchPluginVec() { return &plugin_vec; }
201 	void ScanPlugins();
202 	void LoadPlugins();
203 
FetchMainPanel()204 	Kis_Main_Panel *FetchMainPanel() { return mainp; }
205 
206 	// Public so we don't have pointless wrappers
207 	ConfigFile *prefs;
208 	Kis_Panel_Color colors;
209 
210 	// Interface level since it's independent of the UI
211 	void SpawnServer(string in_parm);
212 	void SpawnServer();
213 	void KillServer();
214 	// These need to be exposed to the callbacks for clean shutdown
FetchServerFramework()215 	TextCliFrame *FetchServerFramework() { return server_framework; }
FetchServerPopen()216 	PopenClient *FetchServerPopen() { return server_popen; }
FetchServerConsole()217 	vector<string> *FetchServerConsole() { return &server_console; }
218 
ResetWarnAllClear()219 	void ResetWarnAllClear() {
220 		warned_cleared = 1;
221 		warned_all_errors_consec = 0;
222 		warned_all_errors = time(0);
223 	}
224 
225 protected:
226 	int shutdown_mode;
227 
228 	// Only allow one server, I don't think anyone really used multiple
229 	// simultaneous servers and if they did, too bad, it introduced way too
230 	// much hassle
231 	KisNetClient *network_client;
232 
233 	// Map of UUIDs of sources to representations
234 	map<uuid, KisPanelInterface::knc_card *> netcard_map;
235 
236 	// Alerts
237 	vector<KisPanelInterface::knc_alert *> alert_vec;
238 
239 	int addcb_ref;
240 	vector<KisPanelInterface::addcli_cb_rec *> addclicb_vec;
241 
242 	// Map of all the settings and prefs
243 
244 	vector<panel_plugin_meta *> plugin_vec;
245 	KisPanelPluginData plugdata;
246 
247 	Kis_Main_Panel *mainp;
248 
249 	// Server monitoring stuff
250 	TextCliFrame *server_framework;
251 	PopenClient *server_popen;
252 	string server_parm;
253 	vector<string> server_console;
254 	int server_text_cb;
255 
256 	// Have we yelled at the user for not having any sources enabled?
257 	int warned_no_sources;
258 
259 	// Or are they all broken?
260 	int warned_all_errors, warned_all_errors_consec, warned_cleared;
261 
262 	vector<Kis_Panel *> modal_vec;
263 };
264 
265 #endif // panel
266 #endif // header
267 
268