1 /*
2  * Copyright (C) 2002-2003 Fhg Fokus
3  *
4  * This file is part of SEMS, a free SIP media server.
5  *
6  * SEMS is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version. This program is released under
10  * the GPL with the additional exemption that compiling, linking,
11  * and/or using OpenSSL is allowed.
12  *
13  * For a license to use the SEMS software under conditions
14  * other than those described here, or to purchase support for this
15  * software, please contact iptel.org by e-mail at the following addresses:
16  *    info@iptel.org
17  *
18  * SEMS is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  */
27 
28 #ifndef _AmUtils_h_
29 #define _AmUtils_h_
30 
31 #include <stdio.h>
32 #include <sys/stat.h>
33 #include <errno.h>
34 #include <sys/types.h>
35 #include <regex.h>
36 #include <sys/socket.h>
37 
38 #include <string>
39 using std::string;
40 
41 #include <vector>
42 #include <utility>
43 #include <map>
44 
45 #include "md5.h"
46 
47 #define HASHLEN 16
48 typedef unsigned char HASH[HASHLEN];
49 
50 #define HASHHEXLEN 32
51 typedef unsigned char HASHHEX[HASHHEXLEN+1];
52 
53 //#define FIFO_PERM S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
54 
55 #define PARAM_HDR "P-App-Param"
56 #define APPNAME_HDR "P-App-Name"
57 
58 /** @file AmUtils.h */
59 
60 /**
61  * Convert an int to a string.
62  */
63 string int2str(int val);
64 
65 /**
66  * Convert an unsigned int to a string.
67  */
68 string int2str(unsigned int val);
69 
70 /**
71  * Convert a long to a string.
72  */
73 string long2str(long int val);
74 
75 /**
76  * Convert a long long to a string.
77  */
78 string longlong2str(long long int val);
79 
80 /**
81  * Convert a a byte to a string using hexdecimal representation.
82  */
83 string char2hex(unsigned char val, bool lowercase = false);
84 
85 /**
86  * Convert an unsigned int to a string using hexdecimal representation.
87  */
88 string int2hex(unsigned int val, bool lowercase = false);
89 
90 /**
91  * Convert an unsigned long to a string using hexdecimal representation.
92  */
93 string long2hex(unsigned long val);
94 
95 /**
96  * Convert a reversed hex string to uint.
97  * @param str    [in]  string to convert.
98  * @param result [out] result integer.
99  * @return true if failed.
100  */
101 bool reverse_hex2int(const string& str, unsigned int& result);
102 
103 /**
104  * Convert a double to a string.
105  */
106 string double2str(double val);
107 
108 /**
109  * Convert a string to an uint.
110  * @param str    [in]  string to convert.
111  * @param result [out] result integer.
112  * @return true if failed (!!!)
113  */
114 bool str2i(const string& str, unsigned int& result);
115 
116 /**
117  * Internal version of preceeding 'str2i' method.
118  * @param str    [in,out] gets incremented until sep char or error occurs
119  * @param result [out] result of the function
120  * @param sep    [in] character seprating the number to convert and the next token
121  * @return true if failed (!!!)
122  */
123 bool str2i(char*& str, unsigned int& result, char sep = ' ');
124 
125 
126 /**
127  * Convert a string to an int.
128  * @param str    [in]  string to convert.
129  * @param result [out] result integer.
130  * @return true on success (!!!)
131  */
132 bool str2int(const string& str, int& result);
133 
134 /**
135  * Internal version of preceeding 'str2int' method.
136  * @param str    [in,out] gets incremented until sep char or error occurs
137  * @param result [out] result of the function
138  * @param sep    [in] character seprating the number to convert and the next token
139  * @return true on success (!!!)
140  */
141 bool str2int(char*& str, int& result, char sep = ' ');
142 
143 /**
144  * Convert a string to a long int.
145  * On many systems nowadays this could be the same as str2int.
146  * @param str    [in]  string to convert.
147  * @param result [out] result integer.
148  * @return true if on success (!!!).
149  */
150 bool str2long(const string& str, long& result);
151 
152 /**
153  * Internal version of preceeding 'str2long' method.
154  * @param str    [in,out] gets incremented until sep char or error occurs
155  * @param result [out] result of the function
156  * @param sep    [in] character seprating the number to convert and the next token
157  * @return true on success
158  */
159 bool str2long(char*& str, long& result, char sep = ' ');
160 
161 /* translates string value into bool, returns false on error */
162 bool str2bool(const string &s, bool &dst);
163 
164 std::string URL_decode(const std::string& s);
165 std::string URL_encode(const std::string& s);
166 
167 /**
168  * Parse code/reason line.
169  * Syntax: "code reason"
170  *
171  * @param lbuf line     buffer to parse.
172  * @param res_code_str  char[4]; Syntax: xxxx whereby
173  *                      each x is a between 0 and 9.
174  * @param res_code      where to store the resulting integer 'code'.
175  * @param res_msg       where to store the 'reason'.
176  */
177 int parse_return_code(const char* lbuf,
178 		      unsigned int& res_code, string& res_msg );
179 /**
180  * Tells if a file exists.
181  * @param name file name.
182  * @return true if file could be openned.
183  */
184 bool file_exists(const string& name);
185 
186 /**
187  * @return A file name extracted from the given full path file name.
188  */
189 string filename_from_fullpath(const string& path);
190 
191 /**
192  * @return A file extension extracted from the given full path file name.
193  */
194 string file_extension(const string& path);
195 
196 /**
197  * @return new path resulting from the concatanation of path,
198  * suffix and eventually a slash between them
199  */
200 string add2path(const string& path, int n_suffix, ...);
201 
202 string get_addr_str(const sockaddr_storage* addr);
203 string get_addr_str_sip(const sockaddr_storage* addr);
204 
205 /** \brief microseconds sleep using select */
206 #define sleep_us(nusecs) \
207 	{ \
208 	struct timeval tval; \
209 	tval.tv_sec=nusecs/1000000; \
210 	tval.tv_usec=nusecs%1000000; \
211 	select(0, NULL, NULL, NULL, &tval ); \
212 	}
213 
214 /*
215  * Computes the local address for a specific destination address.
216  * This is done by opening a connected UDP socket and reading the
217  * local address with getsockname().
218  */
219 int get_local_addr_for_dest(sockaddr_storage* remote_ip, sockaddr_storage* local);
220 int get_local_addr_for_dest(const string& remote_ip, string& local);
221 
222 string extract_tag(const string& addr);
223 
224 /** returns true if key is in s_list, a list of items delimited by list_delim
225  *skips whitespaces, too */
226 bool key_in_list(const string& s_list, const string& key, char list_delim = ',');
227 
228 /** return string with trailing spaces and everything after ; including ; itself removed */
229 string strip_header_params(const string& hdr_string);
230 
231 /** get a header parameter value */
232 string get_header_param(const string& hdr_string, const string& param_name);
233 
234 /** get the value of key @param name from the list param_hdr*/
235 string get_header_keyvalue(const string& param_hdr, const string& name);
236 
237 /** get the value of key @param name from the list param_hdr, no comma separated values*/
238 string get_header_keyvalue_single(const string& param_hdr, const string& name);
239 
240 /** get the value of key @param short_name or @param name or from the list param_hdr*/
241 string get_header_keyvalue(const string& param_hdr, const string& short_name, const string& name);
242 
243 /** get the value of key @param name from P-Iptel-Param header in hdrs */
244 string get_session_param(const string& hdrs, const string& name);
245 
246 /** parse the P-App-Param header and extracts the parameters into a map */
247 void parse_app_params(const string& hdrs, std::map<string,string>& app_params);
248 
249 /** support for thread-safe pseudo-random numbers  - init function */
250 void init_random();
251 /** support for thread-safe pseudo-random numbers  - make a random number function */
252 unsigned int get_random();
253 
254 /** Explode string by a separator to a vector */
255 std::vector<string> explode(const string& s, const string& delim, const bool keep_empty = false);
256 
257 /** remove chars in sepSet from beginning and end of str */
trim(std::string const & str,char const * sepSet)258 inline std::string trim(std::string const& str,char const* sepSet)
259 {
260   std::string::size_type const first = str.find_first_not_of(sepSet);
261   return ( first==std::string::npos ) ?
262     std::string() : str.substr(first, str.find_last_not_of(sepSet)-first+1);
263 }
264 
265 /** add a directory to an environement variable */
266 void add_env_path(const char* name, const string& path);
267 
268 
269 size_t skip_to_end_of_brackets(const string& s, size_t start);
270 
271 typedef std::vector<std::pair<regex_t, string> > RegexMappingVector;
272 
273 /** read a regex=>string mapping from file
274     @return true on success
275  */
276 bool read_regex_mapping(const string& fname, const char* sep,
277 			const char* dbg_type,
278 			RegexMappingVector& result);
279 
280 /** run a regex mapping - result is the first matching entry
281     @return true if matched
282  */
283 bool run_regex_mapping(const RegexMappingVector& mapping, const char* test_s,
284 		       string& result);
285 
286 
287 /** convert a binary MD5 hash to hex representation */
288 void cvt_hex(HASH bin, HASHHEX hex);
289 
290 /** get an MD5 hash of a string */
291 string calculateMD5(const string& input);
292 
293 #endif
294 
295 // Local Variables:
296 // mode:C++
297 // End:
298