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.

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