You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

985 lines
28 KiB

7 years ago
​
​
6 years ago
7 years ago
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
6 years ago
7 years ago
​
​
​
​
​
​
6 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
6 years ago
6 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
5 years ago
​
7 years ago
​
​
​
​
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
7 years ago
​
​
7 years ago
6 years ago
6 years ago
7 years ago
  1. #include "basicFunctionality.h"
  2. #include "../../alg/generic/md5.h"
  3. #include <sys/ioctl.h>
  4. //#include <sys/sendfile.h>
  5. #ifdef DEBUG
  6. #include "basicLogCollection.h"
  7. #define URGENT_LOG(X) basicLogCollection::getInstance()->Log("basic Functionality", NULL, X )
  8. #endif
  9. #ifdef __linux__
  10. int chown( const char *file_path, const char *user_name, const char *group_name){
  11. uid_t uid;
  12. gid_t gid;
  13. struct passwd *pwd;
  14. struct group *grp;
  15. pwd = getpwnam(user_name);
  16. if (pwd == NULL) {
  17. #ifdef DEBUG
  18. URGENT_LOG("Failed to get uid!");
  19. #endif
  20. return -1;
  21. };
  22. uid = pwd->pw_uid;
  23. grp = getgrnam(group_name);
  24. if (grp == NULL){
  25. #ifdef DEBUG
  26. URGENT_LOG("Failed to get gid!");
  27. #endif
  28. return -1;
  29. };
  30. gid = grp->gr_gid;
  31. return chown(file_path, uid, gid);
  32. };
  33. #endif
  34. int checkCmdExists( CSTR cmd ){
  35. FILE * fd = popen( cmd, "r" );
  36. char tB[1024];
  37. int rVal = -1;
  38. if( fd!= NULL ){
  39. while ( !feof(fd)) {
  40. fgets((CSTR)tB, 1024, fd );
  41. trim( tB );
  42. if( strstr(tB, "command not found" ) != NULL){
  43. rVal = 0;
  44. break;
  45. };
  46. };
  47. if( rVal < 1 ) rVal = 1;
  48. fclose(fd);
  49. };
  50. return rVal;
  51. }
  52. #ifdef __linux__
  53. int fileCopy( const char* source, const char* destination ){
  54. int input, output;
  55. if ((input = open(source, O_RDONLY)) == -1) return -1;
  56. if ((output = creat(destination, 0660)) == -1){
  57. close(input);
  58. return -1;
  59. };
  60. //Here we use kernel-space copying for performance reasons
  61. #if defined(__APPLE__) || defined(__FreeBSD__)
  62. //fcopyfile works on FreeBSD and OS X 10.5+
  63. int result = fcopyfile(input, output, 0, COPYFILE_ALL);
  64. #else
  65. //sendfile will work with non-socket output (i.e. regular file) on Linux 2.6.33+
  66. off_t bytesCopied = 0;
  67. struct stat fileinfo = {0};
  68. fstat(input, &fileinfo);
  69. int result = sendfile(output, input, &bytesCopied, fileinfo.st_size);
  70. #endif
  71. close(input);
  72. close(output);
  73. return result;
  74. };
  75. #endif
  76. bool fileExist(const char *strFilename) {
  77. struct stat stFileInfo;
  78. int intStat;
  79. intStat = stat( strFilename ,&stFileInfo);
  80. if( intStat == 0 ){
  81. return true;
  82. };
  83. return false;
  84. };
  85. bool dirEmpty( const char *dirname ){
  86. int n = 0;
  87. struct dirent *d;
  88. DIR *dir = opendir(dirname);
  89. if( dir == NULL) return true;
  90. while( (d = readdir(dir)) != NULL )if( ++n > 2 ) break;
  91. closedir( dir );
  92. if( n <= 2 ) return true;
  93. return false;
  94. };
  95. #ifndef __linux__
  96. #else
  97. #include <sys/types.h>
  98. #include <unistd.h>
  99. #include <sys/syscall.h>
  100. #include <sys/types.h>
  101. pid_t gettid( void ){
  102. return syscall(SYS_gettid);
  103. };
  104. #endif
  105. const char *__BuildDate = (__DATE__);
  106. const char *__BuildTime = (__TIME__);
  107. const char *__BuildTS = (__TIMESTAMP__);
  108. CSTR getBuildID( void ){
  109. return NULL;
  110. };
  111. /* Global Time Diff Value to correct Timestamps */
  112. static int g_iTimeDiff = 0;
  113. /**
  114. * number of seconds from 1 Jan. 1601 00:00 to 1 Jan 1970 00:00 UTC
  115. */
  116. // Defined in Header File
  117. //#define EPOCH_DIFF 11644473600LL
  118. //#define UNIX_TimeStamp_2_WINDOWS_FileTime_SHIFT 116444736000000000
  119. bool correctSysTime( int diff ){
  120. #ifdef __linux__
  121. struct timeval tData;
  122. struct timezone tTZ;
  123. tTZ.tz_minuteswest = 0;
  124. tTZ.tz_dsttime = 0;
  125. if( gettimeofday( &tData, &tTZ ) == -1 ){
  126. return false;
  127. } else {
  128. tData.tv_sec += diff / 1000;
  129. tData.tv_usec += ( diff * 1000 ) % 1000000;
  130. };
  131. if( settimeofday( &tData, &tTZ ) == -1 ){
  132. return false;
  133. };
  134. #else
  135. unsigned __int64 tMilliseconds;
  136. SYSTEMTIME Ztime;
  137. GetSystemTime(&Ztime);
  138. SystemTimeToFileTime( &Ztime, (FILETIME*) &tMilliseconds );
  139. tMilliseconds += diff * 10000;
  140. FileTimeToSystemTime( (FILETIME*) &tMilliseconds, &Ztime );
  141. if( 0 == SetSystemTime(&Ztime) ) return false;
  142. #endif
  143. return true;
  144. };
  145. bool correctSoftTime( int diff ){
  146. g_iTimeDiff = diff;
  147. return true;
  148. };
  149. #ifdef __linux__
  150. unsigned long long getTimeValue( void ){
  151. unsigned long long tMilliseconds;
  152. struct timeval tData;
  153. struct timezone tTZ;
  154. tMilliseconds = 0;
  155. tTZ.tz_minuteswest = 0;
  156. tTZ.tz_dsttime = 0;
  157. if( gettimeofday( &tData, &tTZ ) == -1 ){
  158. } else {
  159. tMilliseconds = tData.tv_sec;
  160. tMilliseconds = tMilliseconds *1000;
  161. tMilliseconds += tData.tv_usec / 1000;
  162. };
  163. #else
  164. /*
  165. void TimetToFileTime( time_t t, LPFILETIME pft ){
  166. LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000;
  167. pft->dwLowDateTime = (DWORD) ll;
  168. pft->dwHighDateTime = ll >>32;
  169. }*/
  170. unsigned __int64 getTimeValue( void ){
  171. unsigned __int64 tMilliseconds;
  172. SYSTEMTIME Ztime;
  173. GetSystemTime(&Ztime);
  174. SystemTimeToFileTime( &Ztime, (FILETIME*) &tMilliseconds );
  175. tMilliseconds -= UNIX_TimeStamp_2_WINDOWS_FileTime_SHIFT;
  176. tMilliseconds = tMilliseconds / 10000;
  177. #endif
  178. tMilliseconds += g_iTimeDiff;
  179. return tMilliseconds;
  180. };
  181. static uint32_t UID_INCREMENTAL_VALUE = 0;
  182. #define UID_INCREMEMTAL (UID_INCREMENTAL_VALUE++)
  183. CSTR getTimeStamp( void ){
  184. CSTR RetVal = NULL;
  185. time_t rawtime;
  186. uint32_t msec;
  187. struct tm * ptm = NULL;
  188. time ( &rawtime );
  189. ptm = gmtime ( &rawtime );
  190. msec = getTimeValue() % 1000;
  191. //2019-03-24T12:32:59.670Z
  192. if( ptm != NULL ) {
  193. RetVal = (CSTR) malloc( 128 );
  194. if( RetVal!=NULL ){
  195. sprintf( RetVal, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
  196. (ptm->tm_year+1900),
  197. (ptm->tm_mon+1),
  198. ptm->tm_mday,
  199. ptm->tm_hour,
  200. ptm->tm_min,
  201. ptm->tm_sec,
  202. msec
  203. );
  204. };
  205. };
  206. return RetVal;
  207. };
  208. CSTR getTimeStampI( void ){
  209. CSTR RetVal = NULL;
  210. time_t rawtime;
  211. uint32_t msec;
  212. struct tm * ptm = NULL;
  213. time ( &rawtime );
  214. ptm = gmtime ( &rawtime );
  215. msec = getTimeValue() % 1000;
  216. //2019-03-24T12:32:59.670Z
  217. if( ptm != NULL ) {
  218. RetVal = (CSTR) malloc( 128 );
  219. if( RetVal!=NULL ){
  220. sprintf( RetVal, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ %d",
  221. (ptm->tm_year+1900),
  222. (ptm->tm_mon+1),
  223. ptm->tm_mday,
  224. ptm->tm_hour,
  225. ptm->tm_min,
  226. ptm->tm_sec,
  227. msec,
  228. UID_INCREMEMTAL
  229. );
  230. };
  231. };
  232. return RetVal;
  233. };
  234. CSTR getUniqID_MD5( void ){
  235. CSTR tVal = NULL;
  236. CSTR tMD5 = NULL;
  237. char buf[33];
  238. tVal = getTimeStampI();
  239. buf[0] = (char)0;
  240. if( tVal ){
  241. tMD5 = MD5String( tVal );
  242. if( tMD5 ) {
  243. sprintf( buf, "%s", tMD5 );
  244. free( tMD5 );
  245. };
  246. free( tVal );
  247. };
  248. #ifdef __linux__
  249. return strdup( buf );
  250. #else
  251. return _strdup( buf );
  252. #endif
  253. };
  254. CSTR getUniqID_SHA256(void){ return NULL;};
  255. int rand( int a, int b ){
  256. if( b > a ){
  257. return a + (rand() *( b - a )) / RAND_MAX;
  258. };
  259. return b + ( rand() * ( a - b )) / RAND_MAX;
  260. };
  261. CSTR getUniqID_UUID( uint32_t SEED ){
  262. /*
  263. char buf[46];
  264. if( SEED ) {
  265. srand( SEED );
  266. } else {
  267. srand( getMilliseconds() );// time(NULL) );
  268. };
  269. snprintf( buf, 42, "%8.8x-%4.4x-%4.4x-%4.4x-%6.6x%6.6x",
  270. rand() & 0xFFFFFFFF,
  271. rand() & 0xFFFF,
  272. (rand() & 0x0FFF) | 0x4000, // M 4 MSB version => version 4
  273. (rand() & 0x1FFF) | 0x8000, // N: 3 MSB variant => variant 1
  274. rand() & 0xFFFFFF, rand() & 0xFFFFFF
  275. );
  276. #ifdef __linux__
  277. return strdup( buf );
  278. #else
  279. return _strdup( buf );
  280. #endif
  281. */
  282. CSTR tVal = NULL;
  283. CSTR tMD5 = NULL;
  284. char buf[37];
  285. tVal = getTimeStamp();
  286. buf[0] = (char)0;
  287. if( tVal ){
  288. char tt[256];
  289. sprintf( tt, "%s %d %d", tVal ,SEED, UID_INCREMEMTAL );
  290. tMD5 = MD5String( tt );
  291. if( tMD5 ) {
  292. memcpy( buf, tMD5, 8 );
  293. buf[8] = '-';
  294. memcpy( buf+9, tMD5+8, 4 );
  295. buf[13] = '-';
  296. memcpy( buf+14, tMD5+12, 4 );
  297. buf[18] = '-';
  298. memcpy( buf+19, tMD5+16, 4 );
  299. buf[23] = '-';
  300. memcpy( buf+24, tMD5+20, 12 );
  301. buf[36] = (char)0;
  302. free( tMD5 );
  303. };
  304. free( tVal );
  305. };
  306. #ifdef __linux__
  307. return strdup( buf );
  308. #else
  309. return _strdup( buf );
  310. #endif
  311. };
  312. CSTR trim( CSTR val ){
  313. int i = 0;
  314. int j = strlen( val );
  315. if( j ){
  316. while(( i < j ) && (((unsigned char)val[i])< 0x21 )) i++;
  317. while(( j > i ) && (((unsigned char)val[j])< 0x21 )) j--;
  318. if( ( i > 0 ) && ((j-i+1) > 0 ) ) memcpy( val, val+i, j-i+1 );
  319. val[j-i+1]=(char)0;
  320. };
  321. return val;
  322. };
  323. // orig - input String to Search In
  324. // rep - token to search
  325. // with - replacement for token
  326. // in case with is NULL - cutting off the token
  327. // You must free the result if result is non-NULL.
  328. CSTR strReplace( CSTR orig, CSTR rep, CSTR with ){
  329. char *result; // the return string
  330. char *ins; // the next insert point
  331. char *tmp; // varies
  332. int len_rep; // length of rep (the string to remove)
  333. int len_with; // length of with (the string to replace rep with)
  334. int len_front; // distance between rep and end of last rep
  335. int count; // number of replacements
  336. // sanity checks and initialization
  337. if (!orig || !rep) return NULL;
  338. len_rep = strlen(rep);
  339. if (len_rep == 0) return NULL; // empty rep causes infinite loop during count
  340. if (!with) with =(CSTR)""; // In case With not suplied - just cut off Token
  341. len_with = strlen(with);
  342. // count the number of replacements needed
  343. ins = orig;
  344. for (count = 0; tmp = strstr(ins, rep); ++count) {
  345. ins = tmp + len_rep;
  346. }
  347. tmp = result = (CSTR) malloc(strlen(orig) + (len_with - len_rep) * count + 1);
  348. if( !result ) return NULL;
  349. // first time through the loop, all the variable are set correctly
  350. // from here on,
  351. // tmp points to the end of the result string
  352. // ins points to the next occurrence of rep in orig
  353. // orig points to the remainder of orig after "end of rep"
  354. while (count--) {
  355. ins = strstr(orig, rep);
  356. len_front = ins - orig;
  357. tmp = strncpy(tmp, orig, len_front) + len_front;
  358. tmp = strcpy(tmp, with) + len_with;
  359. orig += len_front + len_rep; // move to next "end of rep"
  360. }
  361. strcpy(tmp, orig);
  362. return result;
  363. };
  364. uint8_t checksum8( uint8_t *Buffer, size_t BufferSize ){
  365. uint16_t TempVal;
  366. TempVal = 0;
  367. for( int index = 0; index < BufferSize; index++ ) {
  368. TempVal+=Buffer[index];
  369. };
  370. return (uint8_t)(TempVal & 0x00FF);
  371. };
  372. uint16_t checksum16( uint8_t *Buffer, size_t BufferSize ){
  373. uint32_t TempVal;
  374. TempVal = 0;
  375. for( int index = 0; index < BufferSize; index++ ) {
  376. TempVal+=Buffer[index];
  377. };
  378. return (uint16_t)(TempVal & 0x0000FFFF);
  379. };
  380. uint8_t checksumLGS( uint8_t *Buffer, size_t BufferSize ){
  381. uint16_t TempVal;
  382. TempVal = 0;
  383. for( int index = 0; index < BufferSize; index++ ) {
  384. TempVal+=Buffer[index];
  385. };
  386. return (uint8_t)(TempVal & 0x00FF);
  387. };
  388. // Calculating the Check Sum for Ping package
  389. uint16_t checksumPing(void *b, size_t len){
  390. uint16_t *buf = (uint16_t *) b;
  391. uint32_t sum=0;
  392. uint16_t result;
  393. for ( sum = 0; len > 1; len -= 2 )
  394. sum += *buf++;
  395. if ( len == 1 )
  396. sum += *(unsigned char*)buf;
  397. sum = (sum >> 16) + (sum & 0xFFFF);
  398. sum += (sum >> 16);
  399. result = ~sum;
  400. return result;
  401. };
  402. CArray *explode( char Delim, CSTR S_Str ){
  403. bool ws = false;
  404. CArray * RetVal = NULL;
  405. CSTR b_ptr = S_Str;
  406. U16 s_ptr = 0;
  407. if( S_Str ){
  408. RetVal = new CArray;
  409. if( RetVal ){
  410. while ((U8)S_Str[s_ptr]) {
  411. if( (( U8 ) Delim ) == (( U8 )S_Str[s_ptr]) ){
  412. if (!ws) {
  413. ws = true;
  414. S_Str[s_ptr] = (char)0;
  415. RetVal->push_back( b_ptr );
  416. s_ptr++;
  417. b_ptr = &S_Str[s_ptr];
  418. } else s_ptr++;
  419. } else {
  420. ws = false;
  421. s_ptr++;
  422. };
  423. };
  424. RetVal->push_back( b_ptr );
  425. };
  426. };
  427. return RetVal;
  428. };
  429. CSTR implode( CArray *Data, CSTR S_Str, U8 Start, U8 End ){
  430. CSTR RetVal;
  431. U8 RStart = Start;
  432. U8 REnd = End;
  433. if( Start > Data->size() ) return NULL;
  434. if( End < Start ) return NULL;
  435. if( End > Data->size() ) REnd = (U8)(Data->size()-1);
  436. if( strlen( (*Data).at( RStart )) ){
  437. RetVal = (CSTR) malloc( strlen( (*Data).at( RStart )) + 1 );
  438. sprintf( RetVal,"%s",(*Data).at( RStart ));
  439. } else {
  440. RetVal = (CSTR) malloc( 1 );
  441. *RetVal=(char)0;
  442. };
  443. if (RetVal != NULL) {
  444. for (int i = RStart + 1; i < REnd; i++) {
  445. RetVal = (CSTR)realloc(RetVal, strlen(RetVal) + strlen((*Data).at(i)) + 1 + strlen(S_Str));
  446. sprintf(RetVal, "%s%s%s", RetVal, S_Str, (*Data).at(i));
  447. };
  448. };
  449. return RetVal;
  450. };
  451. int getIP4( if_info **arrParam, int max_ifs ){
  452. int index = 0;
  453. #ifdef __linux__
  454. struct ifaddrs *addrs, *tmp;
  455. getifaddrs(&addrs);
  456. tmp = addrs;
  457. //arrParam= malloc( 10 * sezeof(if_info) );
  458. while (tmp){
  459. if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_INET){
  460. if( strncmp( tmp->ifa_name, "lo", 2 ) != 0 ){
  461. (*arrParam)[index].if_name = (CSTR)malloc(strlen(tmp->ifa_name)+1);
  462. snprintf( (*arrParam)[index].if_name, strlen(tmp->ifa_name)+1,"%s", tmp->ifa_name );
  463. (*arrParam)[index].if_addr = (CSTR)malloc(16);
  464. struct sockaddr_in *pAddr = (struct sockaddr_in *)tmp->ifa_addr;
  465. snprintf( (*arrParam)[index].if_addr, 16, "%s", inet_ntoa(pAddr->sin_addr) );
  466. index++;
  467. if( index == max_ifs ) break;
  468. }
  469. };
  470. tmp = tmp->ifa_next;
  471. };
  472. freeifaddrs(addrs);
  473. #endif
  474. return index;
  475. };
  476. #ifdef __linux__
  477. long long getFreeSpace( CSTR DirName ){
  478. struct statvfs fiData;
  479. if( ( statvfs( DirName, &fiData ) ) < 0 ) {
  480. return -1;
  481. };
  482. return fiData.f_bsize * fiData.f_bfree;
  483. };
  484. long long getTotalSpace( CSTR DirName ){
  485. struct statvfs fiData;
  486. if( ( statvfs( DirName, &fiData ) ) < 0 ) {
  487. return -1;
  488. };
  489. return fiData.f_bsize * fiData.f_blocks;
  490. };
  491. #else
  492. __int64 getFreeSpace( CSTR DirName ){
  493. return -1;
  494. };
  495. __int64 getTotalSpace( CSTR DirName ){
  496. return -1;
  497. };
  498. #endif
  499. CSTR getTStr( CSTR Fmt ){
  500. //CSTR RetVal = NULL;
  501. char DateStr[200];
  502. time_t t;
  503. struct tm *tmp;
  504. t = time(NULL);
  505. tmp = gmtime(&t);
  506. if( tmp == NULL) {
  507. fprintf(stderr, "localtime!");
  508. return NULL;
  509. };
  510. if( Fmt == NULL ) {
  511. if( strftime( DateStr, 200, "%F %T GMT", tmp )==0 ){
  512. fprintf( stderr, "strftime returned 0" );
  513. return NULL;
  514. };
  515. } else {
  516. if( strftime( DateStr, 200, Fmt, tmp )==0 ){
  517. fprintf( stderr, "strftime returned 0" );
  518. return NULL;
  519. };
  520. }
  521. //RetVal =(CSTR) malloc( strlen(DateStr) +1 );
  522. //if( RetVal != NULL ) strcpy( RetVal, DateStr );
  523. #ifdef __linux__
  524. return strdup(DateStr);
  525. #else
  526. return _strdup(DateStr);
  527. #endif
  528. };
  529. #ifdef __linux__
  530. int xioctl(int fh, int request, void *arg){
  531. int r;
  532. do {
  533. r = ioctl( fh, request, arg );
  534. } while (-1 == r && EINTR == errno);
  535. return r;
  536. };
  537. uint64_t getMilliseconds( void ){
  538. uint64_t tMilliseconds = (uint64_t) 0;
  539. struct timeval tData;
  540. struct timezone tTZ;
  541. tTZ.tz_minuteswest = 0;
  542. tTZ.tz_dsttime = 0;
  543. if( gettimeofday( &tData, &tTZ ) == -1 ){
  544. tMilliseconds = (uint64_t) 0;
  545. #ifdef DEBUG
  546. URGENT_LOG( "EbioMat'!");
  547. #endif
  548. } else {
  549. tMilliseconds = (uint64_t) tData.tv_sec;
  550. tMilliseconds = ( tMilliseconds * 1000 );
  551. tMilliseconds += (uint64_t)( tData.tv_usec / 1000 );
  552. //URGENT_LOG("MilSecCollected!");
  553. };
  554. #else
  555. #include<ctime>
  556. unsigned __int64 getMilliseconds( void ){
  557. unsigned __int64 tMilliseconds;
  558. SYSTEMTIME time;
  559. GetSystemTime(&time);
  560. tMilliseconds = (uint32_t)std::time(NULL);
  561. tMilliseconds *= 1000;
  562. tMilliseconds += time.wMilliseconds;
  563. #endif
  564. return tMilliseconds;
  565. };
  566. #ifdef __linux__
  567. bool enumerateDevices( enumeratorInfo *mlist, const char *dTemplate){
  568. DIR *d;
  569. struct dirent *dir;
  570. DeviceInfo *tDevInfo;
  571. if( mlist == NULL )return false;
  572. if( dTemplate == NULL )return false;
  573. for( int i = 0; i < mlist->size(); i++ ) {
  574. delete( mlist->at(i) );
  575. };
  576. mlist->clear();
  577. d = opendir("/dev");
  578. if( d ){
  579. while((dir = readdir(d)) != NULL){
  580. if( dir->d_type == DT_CHR || dir->d_type == DT_BLK ){
  581. if( strncmp( dir->d_name, dTemplate, 5 ) == 0 ) {
  582. tDevInfo = (DeviceInfo *) malloc( sizeof(DeviceInfo) );
  583. sprintf( tDevInfo->DevicePath, "/dev/%s", dir->d_name );
  584. tDevInfo->FriendlyName[0] =(char)0;
  585. if( strncmp( dTemplate, "video", 5 ) == 0 ){
  586. FILE *fd;
  587. char CMD[256];
  588. sprintf( CMD, "v4l2-ctl -d %s -D", tDevInfo->DevicePath );
  589. fd = popen( CMD, "r");
  590. if( fd != NULL ) {
  591. char * line = NULL;
  592. size_t len = 0;
  593. ssize_t rd;
  594. while ((rd = getline(&line, &len, fd)) != -1) {
  595. char *tS1, *tS2;
  596. // Future use strcasestr() or make both stings same case
  597. if( ( tS1 = strstr( line, "Bus info" )) != NULL ) {
  598. tS2 = trim( strstr( tS1, ":" ) + 2 );
  599. sprintf( tDevInfo->FriendlyName, "%s", tS2 );
  600. };
  601. };
  602. fclose( fd );
  603. if( line ) free( line );
  604. };
  605. };
  606. mlist->push_back( tDevInfo );
  607. //printf("%s\n", dir->d_name );
  608. };
  609. //if( strncmp( dir->d_name, "ttyS", 4 ) == 0 ){
  610. // tBuffer = ( char * ) malloc( strlen(dir->d_name)+1 );
  611. // strncpy( tBuffer, dir->d_name, strlen(dir->d_name)+1 );
  612. // mlist->pushBack( tBuffer );
  613. // //printf("%s\n", dir->d_name );
  614. //};
  615. };
  616. };
  617. closedir(d);
  618. } else {
  619. return false;
  620. };
  621. return true;
  622. };
  623. #else
  624. #ifndef IID_PPV_ARGS
  625. extern "C++" {
  626. template<typename T> void** IID_PPV_ARGS_Helper(T** pp)
  627. {
  628. // make sure everyone derives from IUnknown
  629. static_cast<IUnknown*>(*pp);
  630. return reinterpret_cast<void**>(pp);
  631. }
  632. }
  633. #define IID_PPV_ARGS(ppType) __uuidof(**(ppType)), IID_PPV_ARGS_Helper(ppType)
  634. #endif
  635. #pragma comment(lib,"Strmiids.lib")
  636. bool enumerateDevices(enumeratorInfo *mlist, REFGUID category){
  637. HRESULT hr;
  638. bool initOk = true;
  639. hr = CoInitializeEx( NULL, COINIT_SPEED_OVER_MEMORY | COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE );
  640. if( hr == E_INVALIDARG ){
  641. printf("Couldn't Init! hr=0x%x", hr);
  642. return false;
  643. } else if ( hr == E_OUTOFMEMORY ){
  644. printf("Couldn't Init! hr=0x%x", hr);
  645. return false;
  646. } else if ( hr == E_UNEXPECTED ){
  647. printf("Couldn't Init! hr=0x%x", hr);
  648. return false;
  649. } else if( hr == RPC_E_CHANGED_MODE ){
  650. initOk = false;
  651. };
  652. if( hr == S_OK || hr == RPC_E_CHANGED_MODE ){
  653. // Create the system device enumerator
  654. ICreateDevEnum *pDevEnum;
  655. hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum) );
  656. if( hr != S_OK ){
  657. printf("Couldn't create system enumerator! hr=0x%x", hr);
  658. CoUninitialize();
  659. return false;
  660. };
  661. if( pDevEnum != NULL ){
  662. IEnumMoniker *pClsEnum = NULL;
  663. hr = pDevEnum->CreateClassEnumerator(category, &pClsEnum, 0);
  664. if( hr == S_FALSE ){
  665. // The category is empty. Treat as an error.
  666. pDevEnum->Release();
  667. CoUninitialize();
  668. return false;
  669. };
  670. if( pClsEnum != NULL ){
  671. IMoniker *pMoniker = NULL;
  672. while( pClsEnum->Next(1, &pMoniker, NULL) == S_OK ){
  673. IPropertyBag *pPropBag = NULL;
  674. hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag));
  675. if( FAILED(hr) ){
  676. pMoniker->Release();
  677. continue;
  678. };
  679. VARIANT var;
  680. VariantInit(&var);
  681. DeviceInfo *tRec;
  682. tRec = (DeviceInfo *)malloc(sizeof(DeviceInfo));
  683. // Get description or friendly name.
  684. hr = pPropBag->Read(L"Description", &var, 0);
  685. if( FAILED(hr) ){
  686. hr = pPropBag->Read(L"FriendlyName", &var, 0);
  687. };
  688. if (SUCCEEDED(hr)){
  689. if (tRec != NULL) {
  690. sprintf(tRec->FriendlyName, "%S", var.bstrVal);
  691. mlist->push_back(tRec);
  692. };
  693. VariantClear(&var);
  694. };
  695. hr = pPropBag->Write(L"FriendlyName", &var);
  696. // WaveInID applies only to audio capture devices.
  697. hr = pPropBag->Read(L"WaveInID", &var, 0);
  698. if (SUCCEEDED(hr)){
  699. //printf("WaveIn ID: %d\n", var.lVal);
  700. VariantClear(&var);
  701. };
  702. hr = pPropBag->Read(L"DevicePath", &var, 0);
  703. if (SUCCEEDED(hr)){
  704. // The device path is not intended for display.
  705. if( tRec != NULL) {
  706. sprintf(tRec->DevicePath, "%S", var.bstrVal);
  707. };
  708. VariantClear(&var);
  709. };
  710. pPropBag->Release();
  711. pMoniker->Release();
  712. };
  713. pClsEnum->Release();
  714. };
  715. pDevEnum->Release();
  716. };
  717. if( initOk ) CoUninitialize();
  718. };
  719. return true;
  720. };
  721. #endif
  722. size_t escape_string( char *to, size_t to_length, const char *from, size_t length ){
  723. const char *to_start = to;
  724. const char *end, *to_end = to_start + (to_length ? to_length - 1 : 2 * length);
  725. bool overflow = false;
  726. for( end = from + length; from < end; from++ ) {
  727. char escape = 0;
  728. int tmp_length;
  729. /*
  730. If the next character appears to begin a multi-byte character, we
  731. escape that first byte of that apparent multi-byte character. (The
  732. character just looks like a multi-byte character -- if it were actually
  733. a multi-byte character, it would have been passed through in the test
  734. above.)
  735. Without this check, we can create a problem by converting an invalid
  736. multi-byte character into a valid one. For example, 0xbf27 is not
  737. a valid GBK character, but 0xbf5c is. (0x27 = ', 0x5c = \)
  738. */
  739. tmp_length = 0;
  740. if( tmp_length > 1 ){
  741. escape = *from;
  742. } else{
  743. switch (*from) {
  744. case 0: /* Must be escaped for 'mysql' */
  745. escape = '0';
  746. break;
  747. case '\n': /* Must be escaped for logs */
  748. escape = 'n';
  749. break;
  750. case '\r':
  751. escape = 'r';
  752. break;
  753. case '\\':
  754. escape = '\\';
  755. break;
  756. case '\'':
  757. escape = '\'';
  758. break;
  759. case '"': /* Better safe than sorry */
  760. escape = '"';
  761. break;
  762. case '\032': /* This gives problems on Win32 */
  763. escape = 'Z';
  764. break;
  765. };
  766. };
  767. if( escape ) {
  768. if (to + 2 > to_end) { overflow = true; break; };
  769. *to++ = '\\';
  770. *to++ = escape;
  771. } else {
  772. if (to + 1 > to_end) { overflow = true; break; };
  773. *to++ = *from;
  774. };
  775. };
  776. *to = 0;
  777. return overflow ? (size_t)-1 : (size_t)(to - to_start);
  778. };
  779. localIpAddresses g_vLocalIpAdressess;
  780. bool g_bAdressesAreDiscovered = false;
  781. #ifdef __linux__
  782. #include <linux/if.h>
  783. localIpAddresses *getLocalIP(){
  784. // Check for Filter LO and TUN Devices!
  785. if( !g_bAdressesAreDiscovered ) {
  786. struct ifaddrs *ifaddr, *ifa;
  787. localIp4Info *tIpAddress = NULL;
  788. if (getifaddrs(&ifaddr) != -1) {
  789. /* Walk through linked list, maintaining head pointer so we can free list later */
  790. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  791. // Filter Loopback if
  792. if( strstr( ifa->ifa_name, "lo" ) != NULL ) continue;
  793. // Filter vpn TUN if
  794. if( strstr( ifa->ifa_name, "tun" ) != NULL ) continue;
  795. char protocol[IFNAMSIZ] = {0};
  796. if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET) continue;
  797. tIpAddress = (localIp4Info*)malloc(sizeof(localIp4Info));
  798. if(tIpAddress!=NULL){
  799. sprintf( tIpAddress->address , "%d.%d.%d.%d",
  800. (uint8_t)ifa->ifa_addr->sa_data[2],
  801. (uint8_t)ifa->ifa_addr->sa_data[3],
  802. (uint8_t)ifa->ifa_addr->sa_data[4],
  803. (uint8_t)ifa->ifa_addr->sa_data[5]
  804. );
  805. sprintf( tIpAddress->netmask , "%d.%d.%d.%d",
  806. (uint8_t)ifa->ifa_netmask->sa_data[2],
  807. (uint8_t)ifa->ifa_netmask->sa_data[3],
  808. (uint8_t)ifa->ifa_netmask->sa_data[4],
  809. (uint8_t)ifa->ifa_netmask->sa_data[5]
  810. );
  811. sprintf( tIpAddress->broadcast , "%d.%d.%d.%d",
  812. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[2],
  813. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[3],
  814. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[4],
  815. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[5]
  816. );
  817. g_vLocalIpAdressess.push_back(tIpAddress);
  818. };
  819. };
  820. freeifaddrs(ifaddr);
  821. };
  822. g_bAdressesAreDiscovered = true;
  823. };
  824. return &g_vLocalIpAdressess;
  825. };
  826. #else // linux
  827. //https://stackoverflow.com/questions/5213629/how-to-enumerate-all-available-network-interfaces
  828. #include <iphlpapi.h>
  829. #pragma comment(lib, "IPHLPAPI.lib")
  830. localIpAddresses* getLocalIP() {
  831. char psBuffer[128];
  832. FILE* fd;
  833. BYTE mB;
  834. localIp4Info* tIpAddress;
  835. if (!g_bAdressesAreDiscovered) {
  836. ULONG outBufLen = 0;
  837. DWORD dwRetVal = 0;
  838. IP_ADAPTER_INFO* pAdapterInfos = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO));
  839. // retry up to 5 times, to get the adapter infos needed
  840. for (int i = 0; i < 5 && (dwRetVal == ERROR_BUFFER_OVERFLOW || dwRetVal == NO_ERROR); ++i) {
  841. dwRetVal = GetAdaptersInfo(pAdapterInfos, &outBufLen);
  842. if (dwRetVal == NO_ERROR) {
  843. break;
  844. } else {
  845. if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
  846. free(pAdapterInfos);
  847. pAdapterInfos = (IP_ADAPTER_INFO*)malloc(outBufLen);
  848. } else {
  849. //pAdapterInfos = 0;
  850. break;
  851. };
  852. };
  853. };
  854. if (dwRetVal == NO_ERROR) {
  855. IP_ADAPTER_INFO* pAdapterInfo = pAdapterInfos;
  856. while (pAdapterInfo) {
  857. IP_ADDR_STRING* pIpAddress = &(pAdapterInfo->IpAddressList);
  858. while (pIpAddress != 0) {
  859. uint8_t A, B, C, D;
  860. uint8_t M, O, S, K, G, U, T, E;
  861. uint32_t Z1, Z2, Z3, Z4;
  862. int RetVal;
  863. RetVal = sscanf(pIpAddress->IpAddress.String, "%d.%d.%d.%d", &Z1, &Z2, &Z3, &Z4);
  864. A = Z1 & 0xFF;
  865. B = Z2 & 0xFF;
  866. C = Z3 & 0xFF;
  867. D = Z4 & 0xFF;
  868. RetVal = sscanf(pIpAddress->IpMask.String, "%d.%d.%d.%d", &Z1, &Z2, &Z3, &Z4);
  869. M = Z1 & 0xFF;
  870. O = Z2 & 0xFF;
  871. S = Z3 & 0xFF;
  872. K = Z4 & 0xFF;
  873. //
  874. // <<<<
  875. // here pAdapterInfo->Address should contain the MAC address
  876. // >>>>
  877. //
  878. if (A > 0) {
  879. tIpAddress = (localIp4Info*)malloc(sizeof(localIp4Info));
  880. if (tIpAddress != NULL) {
  881. sprintf(tIpAddress->address, "%d.%d.%d.%d", A, B, C, D );
  882. sprintf(tIpAddress->netmask, "%d.%d.%d.%d", M, O, S, K );
  883. G = (A & M) | (~M);
  884. U = (B & O) | (~O);
  885. T = (C & S) | (~S);
  886. E = (D & K) | (~K);
  887. sprintf(tIpAddress->broadcast, "%d.%d.%d.%d", G, U, T, E );
  888. g_vLocalIpAdressess.push_back(tIpAddress);
  889. };
  890. };
  891. pIpAddress = pIpAddress->Next;
  892. };
  893. pAdapterInfo = pAdapterInfo->Next;
  894. };
  895. };
  896. free(pAdapterInfos);
  897. };
  898. return &g_vLocalIpAdressess;
  899. };
  900. #endif
  901. #ifdef DEBUG
  902. #undef URGENT_LOG
  903. #endif