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 __UTIL_H__
20 #define __UTIL_H__
21 
22 #include "config.h"
23 
24 #include <stdio.h>
25 #ifdef HAVE_STDINT_H
26 #include <stdint.h>
27 #endif
28 #ifdef HAVE_INTTYPES_H
29 #include <inttypes.h>
30 #endif
31 #include <unistd.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <sys/wait.h>
35 #include <stdlib.h>
36 #include <pwd.h>
37 #include <ctype.h>
38 #include <math.h>
39 
40 #include <string>
41 #include <map>
42 #include <vector>
43 #include <list>
44 #include <sstream>
45 #include <iomanip>
46 
47 // ieee float struct for a 64bit float for serialization
48 typedef struct {
49 	uint64_t mantissa:52 __attribute__ ((packed));
50 	uint64_t exponent:11 __attribute__ ((packed));
51 	uint64_t sign:1 __attribute__ ((packed));
52 } ieee_64_float_t;
53 
54 typedef struct {
55 	unsigned int mantissal:32;
56 	unsigned int mantissah:20;
57 	unsigned int exponent:11;
58 	unsigned int sign:1;
59 } ieee_double_t;
60 
61 typedef struct {
62 	unsigned int mantissal:32;
63 	unsigned int mantissah:32;
64 	unsigned int exponent:15;
65 	unsigned int sign:1;
66 	unsigned int empty:16;
67 } ieee_long_double_t;
68 
69 // Munge a string to characters safe for calling in a shell
70 void MungeToShell(char *in_data, int max);
71 string MungeToShell(string in_data);
72 string MungeToPrintable(const char *in_data, int max, int nullterm);
73 string MungeToPrintable(string in_str);
74 
75 string StrLower(string in_str);
76 string StrUpper(string in_str);
77 string StrStrip(string in_str);
78 string StrPrintable(string in_str);
79 string AlignString(string in_txt, char in_spacer, int in_align, int in_width);
80 
81 int HexStrToUint8(string in_str, uint8_t *in_buf, int in_buflen);
82 string HexStrFromUint8(uint8_t *in_buf, int in_buflen);
83 
84 template<class t> class NtoString {
85 public:
86 	NtoString(t in_n, int in_precision = 0, int in_hex = 0) {
87 		ostringstream osstr;
88 
89 		if (in_hex)
90 			osstr << hex;
91 
92 		if (in_precision)
93 			osstr << setprecision(in_precision) << fixed;
94 
95 		osstr << in_n;
96 
97 		s = osstr.str();
98 	}
99 
Str()100 	string Str() { return s; }
101 
102 	string s;
103 };
104 
105 #define IntToString(I)			NtoString<int>((I)).Str()
106 #define HexIntToString(I)		NtoString<int>((I), 0, 1).Str()
107 #define LongIntToString(L)		NtoString<long int>((L)).Str()
108 #define FloatToString(F)		NtoString<float>((F)).Str()
109 
110 void SubtractTimeval(struct timeval *in_tv1, struct timeval *in_tv2,
111 					 struct timeval *out_tv);
112 
113 // Generic options pair
114 struct opt_pair {
115 	string opt;
116 	string val;
117 	int quoted;
118 };
119 
120 // Generic option handlers
121 string FetchOpt(string in_key, vector<opt_pair> *in_vec);
122 int FetchOptBoolean(string in_key, vector<opt_pair> *in_vec, int dvalue);
123 vector<string> FetchOptVec(string in_key, vector<opt_pair> *in_vec);
124 int StringToOpts(string in_line, string in_sep, vector<opt_pair> *in_vec);
125 void AddOptToOpts(string opt, string val, vector<opt_pair> *in_vec);
126 void ReplaceAllOpts(string opt, string val, vector<opt_pair> *in_vec);
127 
128 // String compare, 1 true 0 false -1 unknown, or default value as provided
129 int StringToBool(string s, int dvalue = -1);
130 
131 // Append to a string, with a delimiter if applicable
132 string StringAppend(string s, string a, string d = " ");
133 
134 int XtoI(char x);
135 int Hex2UChar(unsigned char *in_hex, unsigned char *in_chr);
136 
137 vector<string> StrTokenize(string in_str, string in_split, int return_partial = 1);
138 
139 // 'smart' tokenizeing with start/end positions
140 struct smart_word_token {
141     string word;
142     size_t begin;
143     size_t end;
144 
145     smart_word_token& operator= (const smart_word_token& op) {
146         word = op.word;
147         begin = op.begin;
148         end = op.end;
149         return *this;
150     }
151 };
152 
153 vector<smart_word_token> BaseStrTokenize(string in_str,
154 										 string in_split, string in_quote);
155 vector<smart_word_token> NetStrTokenize(string in_str, string in_split,
156 										int return_partial = 1);
157 
158 // Simplified quoted string tokenizer, expects " ' to start at the beginning
159 // of the token, no abc"def ghi"
160 vector<string> QuoteStrTokenize(string in_str, string in_split);
161 
162 int TokenNullJoin(string *ret_str, const char **in_list);
163 
164 string InLineWrap(string in_txt, unsigned int in_hdr_len,
165 				  unsigned int in_max_len);
166 vector<string> LineWrap(string in_txt, unsigned int in_hdr_len,
167 						unsigned int in_maxlen);
168 vector<int> Str2IntVec(string in_text);
169 
170 int IsBlank(const char *s);
171 
172 // Clean up XML and CSV data for output
173 string SanitizeXML(string);
174 string SanitizeCSV(string);
175 
176 void Float2Pair(float in_float, int16_t *primary, int64_t *mantissa);
177 float Pair2Float(int16_t primary, int64_t mantissa);
178 
179 // Convert a standard channel to a frequency
180 int ChanToFreq(int in_chan);
181 int FreqToChan(int in_freq);
182 
183 // Convert an IEEE beacon rate to an integer # of beacons per second
184 unsigned int Ieee80211Interval2NSecs(int in_rate);
185 
186 // Run a system command and return the error code.  Caller is responsible
187 // for security.  Does not fork out
188 int RunSysCmd(char *in_cmd);
189 
190 // Fork and exec a syscmd, return the pid of the new process
191 pid_t ExecSysCmd(char *in_cmd);
192 
193 #ifdef SYS_LINUX
194 int FetchSysLoadAvg(uint8_t *in_avgmaj, uint8_t *in_avgmin);
195 #endif
196 
197 // Adler-32 checksum, derived from rsync, adler-32
198 uint32_t Adler32Checksum(const char *buf1, int len);
199 
200 // 802.11 checksum functions, derived from the BBN USRP 802.11 code
201 #define IEEE_802_3_CRC32_POLY	0xEDB88320
202 unsigned int update_crc32_80211(unsigned int crc, const unsigned char *data,
203 								int len, unsigned int poly);
204 void crc32_init_table_80211(unsigned int *crc32_table);
205 unsigned int crc32_le_80211(unsigned int *crc32_table, const unsigned char *buf,
206 							int len);
207 
208 
209 // Proftpd process title manipulation functions
210 void init_proc_title(int argc, char *argv[], char *envp[]);
211 void set_proc_title(const char *fmt, ...);
212 
213 // Simple lexer for "advanced" filter stuff and other tools
214 #define _kis_lex_none			0
215 #define _kis_lex_string			1
216 #define _kis_lex_quotestring	2
217 #define _kis_lex_popen			3
218 #define _kis_lex_pclose			4
219 #define _kis_lex_negate			5
220 #define _kis_lex_delim			6
221 
222 typedef struct {
223 	int type;
224 	string data;
225 } _kis_lex_rec;
226 
227 list<_kis_lex_rec> LexString(string in_line, string& errstr);
228 
229 #define LAT_CONVERSION_FACTOR 10000000
230 #define LON_CONVERSION_FACTOR 10000000
231 #define ALT_CONVERSION_FACTOR 1000
232 
233 /* PPI-Geolocation tag conversion routines. (from lib_ppi_geotag)
234  * Floating point numbers are stored on disk in a handful of fixed-point formats (fixedX_Y)
235  * designed to preserve the appropriate amount of precision vs range. These functions convert
236  * the fixedX_Y fixed point values into 'native' doubles for displaying.
237  * Documentation on these formats can be found in the PPI-GEOLOCATION specification
238  */
239 double fixed3_7_to_double(u_int32_t in);
240 double fixed3_6_to_double(u_int32_t in);
241 double fixed6_4_to_double(u_int32_t in);
242 
243 u_int32_t double_to_fixed3_7(double in);
244 u_int32_t double_to_fixed3_6(double in);
245 u_int32_t double_to_fixed6_4(double in);
246 
247 /*
248  * Some values are encoded as 32-bit unsigned nano-second counters.
249  * Usually we want to display these values as doubles.
250  */
251 double    ns_to_double(u_int32_t in);
252 u_int32_t double_to_ns(double in);
253 
254 
255 #endif
256 
257