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.

1244 lines
35 KiB

7 years ago
​
​
6 years ago
7 years ago
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
5 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
​
​
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 ch_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. uid_t getUserUid( CSTR uname ){
  53. struct passwd *pwd;
  54. if ( uname == NULL ) return -1;
  55. pwd = getpwnam( uname ); /* Try getting UID for username */
  56. if (pwd == NULL) return -1;
  57. return pwd->pw_uid;
  58. };
  59. gid_t getUserGid( CSTR uname ){
  60. struct passwd *pwd;
  61. if ( uname == NULL ) return -1;
  62. pwd = getpwnam( uname ); /* Try getting GID for username */
  63. if (pwd == NULL) return -1;
  64. return pwd->pw_gid;
  65. };
  66. bool createAccessibleDir( CSTR path, mode_t mode, uid_t uid, gid_t gid ){
  67. if( mkdir( path, mode ) != 0 ) return false;
  68. chown( path, uid, gid );
  69. return true;
  70. };
  71. #ifdef __linux__
  72. int fileCopy( const char* source, const char* destination ){
  73. int input, output;
  74. if ((input = open(source, O_RDONLY)) == -1) return -1;
  75. if ((output = creat(destination, 0660)) == -1){
  76. close(input);
  77. return -1;
  78. };
  79. //Here we use kernel-space copying for performance reasons
  80. #if defined(__APPLE__) || defined(__FreeBSD__)
  81. //fcopyfile works on FreeBSD and OS X 10.5+
  82. int result = fcopyfile(input, output, 0, COPYFILE_ALL);
  83. #else
  84. //sendfile will work with non-socket output (i.e. regular file) on Linux 2.6.33+
  85. off_t bytesCopied = 0;
  86. struct stat fileinfo = {0};
  87. fstat(input, &fileinfo);
  88. int result = sendfile(output, input, &bytesCopied, fileinfo.st_size);
  89. #endif
  90. close(input);
  91. close(output);
  92. return result;
  93. };
  94. #endif
  95. bool fileExist( CSTR strFilename) {
  96. struct stat stFileInfo;
  97. int intStat;
  98. intStat = stat( strFilename ,&stFileInfo);
  99. if( intStat == 0 ){
  100. return true;
  101. };
  102. return false;
  103. };
  104. bool dirEmpty( CSTR dirname ){
  105. int n = 0;
  106. struct dirent *d;
  107. DIR *dir = opendir(dirname);
  108. if( dir == NULL) return true;
  109. while( (d = readdir(dir)) != NULL )if( ++n > 2 ) break;
  110. closedir( dir );
  111. if( n <= 2 ) return true;
  112. return false;
  113. };
  114. #ifndef __linux__
  115. #else
  116. #include <sys/types.h>
  117. #include <unistd.h>
  118. #include <sys/syscall.h>
  119. #include <sys/types.h>
  120. pid_t gettid( void ){
  121. return syscall(SYS_gettid);
  122. };
  123. #endif
  124. const char *__BuildDate = (__DATE__);
  125. const char *__BuildTime = (__TIME__);
  126. const char *__BuildTS = (__TIMESTAMP__);
  127. CSTR getBuildID( void ){
  128. return NULL;
  129. };
  130. /* Global Time Diff Value to correct Timestamps */
  131. static int g_iTimeDiff = 0;
  132. /**
  133. * number of seconds from 1 Jan. 1601 00:00 to 1 Jan 1970 00:00 UTC
  134. */
  135. // Defined in Header File
  136. //#define EPOCH_DIFF 11644473600LL
  137. //#define UNIX_TimeStamp_2_WINDOWS_FileTime_SHIFT 116444736000000000
  138. bool correctSysTime( int diff ){
  139. #ifdef __linux__
  140. struct timeval tData;
  141. struct timezone tTZ;
  142. tTZ.tz_minuteswest = 0;
  143. tTZ.tz_dsttime = 0;
  144. if( gettimeofday( &tData, &tTZ ) == -1 ){
  145. return false;
  146. } else {
  147. tData.tv_sec += diff / 1000;
  148. tData.tv_usec += ( diff * 1000 ) % 1000000;
  149. };
  150. if( settimeofday( &tData, &tTZ ) == -1 ){
  151. return false;
  152. };
  153. #else
  154. unsigned __int64 tMilliseconds;
  155. SYSTEMTIME Ztime;
  156. GetSystemTime(&Ztime);
  157. SystemTimeToFileTime( &Ztime, (FILETIME*) &tMilliseconds );
  158. tMilliseconds += diff * 10000;
  159. FileTimeToSystemTime( (FILETIME*) &tMilliseconds, &Ztime );
  160. if( 0 == SetSystemTime(&Ztime) ) return false;
  161. #endif
  162. return true;
  163. };
  164. off_t getFileSize(const char *filename) {
  165. struct stat st;
  166. if( stat(filename, &st) == 0 ) return st.st_size;
  167. return -1;
  168. };
  169. bool correctSoftTime( int diff ){
  170. g_iTimeDiff = diff;
  171. return true;
  172. };
  173. #ifdef __linux__
  174. unsigned long long getTimeValue( void ){
  175. unsigned long long tMilliseconds;
  176. struct timeval tData;
  177. struct timezone tTZ;
  178. tMilliseconds = 0;
  179. tTZ.tz_minuteswest = 0;
  180. tTZ.tz_dsttime = 0;
  181. if( gettimeofday( &tData, &tTZ ) == -1 ){
  182. } else {
  183. tMilliseconds = tData.tv_sec;
  184. tMilliseconds = tMilliseconds *1000;
  185. tMilliseconds += tData.tv_usec / 1000;
  186. };
  187. #else
  188. /*
  189. void TimetToFileTime( time_t t, LPFILETIME pft ){
  190. LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000;
  191. pft->dwLowDateTime = (DWORD) ll;
  192. pft->dwHighDateTime = ll >>32;
  193. }*/
  194. unsigned __int64 getTimeValue( void ){
  195. unsigned __int64 tMilliseconds;
  196. SYSTEMTIME Ztime;
  197. GetSystemTime(&Ztime);
  198. SystemTimeToFileTime( &Ztime, (FILETIME*) &tMilliseconds );
  199. tMilliseconds -= UNIX_TimeStamp_2_WINDOWS_FileTime_SHIFT;
  200. tMilliseconds = tMilliseconds / 10000;
  201. #endif
  202. tMilliseconds += g_iTimeDiff;
  203. return tMilliseconds;
  204. };
  205. static uint32_t UID_INCREMENTAL_VALUE = 0;
  206. #define UID_INCREMEMTAL (UID_INCREMENTAL_VALUE++)
  207. CSTR getTimeStamp( void ){
  208. CSTR RetVal = NULL;
  209. time_t rawtime;
  210. uint32_t msec;
  211. struct tm * ptm = NULL;
  212. time ( &rawtime );
  213. ptm = gmtime ( &rawtime );
  214. msec = getTimeValue() % 1000;
  215. //2019-03-24T12:32:59.670Z
  216. if( ptm != NULL ) {
  217. RetVal = (CSTR) malloc( 128 );
  218. if( RetVal!=NULL ){
  219. sprintf( RetVal, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
  220. (ptm->tm_year+1900),
  221. (ptm->tm_mon+1),
  222. ptm->tm_mday,
  223. ptm->tm_hour,
  224. ptm->tm_min,
  225. ptm->tm_sec,
  226. msec
  227. );
  228. };
  229. };
  230. return RetVal;
  231. };
  232. CSTR getTimeStampI( void ){
  233. CSTR RetVal = NULL;
  234. time_t rawtime;
  235. uint32_t msec;
  236. struct tm * ptm = NULL;
  237. time ( &rawtime );
  238. ptm = gmtime ( &rawtime );
  239. msec = getTimeValue() % 1000;
  240. //2019-03-24T12:32:59.670Z
  241. if( ptm != NULL ) {
  242. RetVal = (CSTR) malloc( 128 );
  243. if( RetVal!=NULL ){
  244. sprintf( RetVal, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ %d",
  245. (ptm->tm_year+1900),
  246. (ptm->tm_mon+1),
  247. ptm->tm_mday,
  248. ptm->tm_hour,
  249. ptm->tm_min,
  250. ptm->tm_sec,
  251. msec,
  252. UID_INCREMEMTAL
  253. );
  254. };
  255. };
  256. return RetVal;
  257. };
  258. CSTR getUniqID_MD5( void ){
  259. CSTR tVal = NULL;
  260. CSTR tMD5 = NULL;
  261. char buf[33];
  262. tVal = getTimeStampI();
  263. buf[0] = (char)0;
  264. if( tVal ){
  265. tMD5 = MD5String( tVal );
  266. if( tMD5 ) {
  267. sprintf( buf, "%s", tMD5 );
  268. free( tMD5 );
  269. };
  270. free( tVal );
  271. };
  272. #ifdef __linux__
  273. return strdup( buf );
  274. #else
  275. return _strdup( buf );
  276. #endif
  277. };
  278. CSTR getUniqID_SHA256(void){ return NULL;};
  279. int rand( int a, int b ){
  280. if( b > a ){
  281. return a + (rand() *( b - a )) / RAND_MAX;
  282. };
  283. return b + ( rand() * ( a - b )) / RAND_MAX;
  284. };
  285. void getUniqID_UUID( CSTR buf, uint32_t SEED ){
  286. CSTR tVal = NULL;
  287. CSTR tMD5 = NULL;
  288. tVal = getTimeStamp();
  289. buf[0] = (char)0;
  290. if( tVal ){
  291. char tt[256];
  292. sprintf( tt, "%s %d %d", tVal ,SEED, UID_INCREMEMTAL );
  293. tMD5 = MD5String( tt );
  294. if( tMD5 ) {
  295. memcpy( buf, tMD5, 8 );
  296. buf[8] = '-';
  297. memcpy( buf+9, tMD5+8, 4 );
  298. buf[13] = '-';
  299. memcpy( buf+14, tMD5+12, 4 );
  300. buf[18] = '-';
  301. memcpy( buf+19, tMD5+16, 4 );
  302. buf[23] = '-';
  303. memcpy( buf+24, tMD5+20, 12 );
  304. buf[36] = (char)0;
  305. free( tMD5 );
  306. };
  307. free( tVal );
  308. };
  309. };
  310. CSTR getUniqID_UUID( uint32_t SEED ){
  311. char buf[37];
  312. getUniqID_UUID( buf, SEED );
  313. #ifdef __linux__
  314. return strdup( buf );
  315. #else
  316. return _strdup( buf );
  317. #endif
  318. };
  319. CSTR trim( CSTR val ){
  320. int i = 0;
  321. int j = strlen( val );
  322. if( j ){
  323. while(( i < j ) && (((unsigned char)val[i])< 0x21 )) i++;
  324. while(( j > i ) && (((unsigned char)val[j])< 0x21 )) j--;
  325. if( ( i > 0 ) && ((j-i+1) > 0 ) ) memcpy( val, val+i, j-i+1 );
  326. val[j-i+1]=(char)0;
  327. };
  328. return val;
  329. };
  330. // orig - input String to Search In
  331. // rep - token to search
  332. // with - replacement for token
  333. // in case with is NULL - cutting off the token
  334. // You must free the result if result is non-NULL.
  335. CSTR strReplace( CSTR orig, CSTR rep, CSTR with ){
  336. char *result; // the return string
  337. char *ins; // the next insert point
  338. char *tmp; // varies
  339. int len_rep; // length of rep (the string to remove)
  340. int len_with; // length of with (the string to replace rep with)
  341. int len_front; // distance between rep and end of last rep
  342. int count; // number of replacements
  343. // sanity checks and initialization
  344. if (!orig || !rep) return NULL;
  345. len_rep = strlen(rep);
  346. if (len_rep == 0) return NULL; // empty rep causes infinite loop during count
  347. if (!with) with =(CSTR)""; // In case With not suplied - just cut off Token
  348. len_with = strlen(with);
  349. // count the number of replacements needed
  350. ins = orig;
  351. for (count = 0; tmp = strstr(ins, rep); ++count) {
  352. ins = tmp + len_rep;
  353. }
  354. tmp = result = (CSTR) malloc(strlen(orig) + (len_with - len_rep) * count + 1);
  355. if( !result ) return NULL;
  356. // first time through the loop, all the variable are set correctly
  357. // from here on,
  358. // tmp points to the end of the result string
  359. // ins points to the next occurrence of rep in orig
  360. // orig points to the remainder of orig after "end of rep"
  361. while (count--) {
  362. ins = strstr(orig, rep);
  363. len_front = ins - orig;
  364. tmp = strncpy(tmp, orig, len_front) + len_front;
  365. tmp = strcpy(tmp, with) + len_with;
  366. orig += len_front + len_rep; // move to next "end of rep"
  367. }
  368. strcpy(tmp, orig);
  369. return result;
  370. };
  371. uint8_t checksum8( uint8_t *Buffer, size_t BufferSize ){
  372. uint16_t TempVal;
  373. TempVal = 0;
  374. for( int index = 0; index < BufferSize; index++ ) {
  375. TempVal+=Buffer[index];
  376. };
  377. return (uint8_t)(TempVal & 0x00FF);
  378. };
  379. uint16_t checksum16( uint8_t *Buffer, size_t BufferSize ){
  380. uint32_t TempVal;
  381. TempVal = 0;
  382. for( int index = 0; index < BufferSize; index++ ) {
  383. TempVal+=Buffer[index];
  384. };
  385. return (uint16_t)(TempVal & 0x0000FFFF);
  386. };
  387. uint8_t checksumLGS( uint8_t *Buffer, size_t BufferSize ){
  388. uint16_t TempVal;
  389. TempVal = 0;
  390. for( int index = 0; index < BufferSize; index++ ) {
  391. TempVal+=Buffer[index];
  392. };
  393. return (uint8_t)(TempVal & 0x00FF);
  394. };
  395. // Calculating the Check Sum for Ping package
  396. uint16_t checksumPing(void *b, size_t len){
  397. uint16_t *buf = (uint16_t *) b;
  398. uint32_t sum=0;
  399. uint16_t result;
  400. for ( sum = 0; len > 1; len -= 2 )
  401. sum += *buf++;
  402. if ( len == 1 )
  403. sum += *(unsigned char*)buf;
  404. sum = (sum >> 16) + (sum & 0xFFFF);
  405. sum += (sum >> 16);
  406. result = ~sum;
  407. return result;
  408. };
  409. // Calculating the Check Sum
  410. /*unsigned short checksum(void *b, int len){
  411. unsigned short *buf = (unsigned short *) b;
  412. unsigned int sum=0;
  413. unsigned short result;
  414. for ( sum = 0; len > 1; len -= 2 )
  415. sum += *buf++;
  416. if ( len == 1 )
  417. sum += *(unsigned char*)buf;
  418. sum = (sum >> 16) + (sum & 0xFFFF);
  419. sum += (sum >> 16);
  420. result = ~sum;
  421. return result;
  422. }*/
  423. CArray *explode( char Delim, CSTR S_Str ){
  424. bool ws = false;
  425. CArray * RetVal = NULL;
  426. CSTR b_ptr = S_Str;
  427. U16 s_ptr = 0;
  428. if( S_Str ){
  429. RetVal = new CArray;
  430. if( RetVal ){
  431. while ((U8)S_Str[s_ptr]) {
  432. if( (( U8 ) Delim ) == (( U8 )S_Str[s_ptr]) ){
  433. if (!ws) {
  434. ws = true;
  435. S_Str[s_ptr] = (char)0;
  436. RetVal->push_back( b_ptr );
  437. s_ptr++;
  438. b_ptr = &S_Str[s_ptr];
  439. } else s_ptr++;
  440. } else {
  441. ws = false;
  442. s_ptr++;
  443. };
  444. };
  445. RetVal->push_back( b_ptr );
  446. };
  447. };
  448. return RetVal;
  449. };
  450. CSTR implode( CArray *Data, CSTR S_Str, U8 Start, U8 End ){
  451. CSTR RetVal;
  452. U8 RStart = Start;
  453. U8 REnd = End;
  454. if( Start > Data->size() ) return NULL;
  455. if( End < Start ) return NULL;
  456. if( End > Data->size() ) REnd = (U8)(Data->size()-1);
  457. if( strlen( (*Data).at( RStart )) ){
  458. RetVal = (CSTR) malloc( strlen( (*Data).at( RStart )) + 1 );
  459. sprintf( RetVal,"%s",(*Data).at( RStart ));
  460. } else {
  461. RetVal = (CSTR) malloc( 1 );
  462. *RetVal=(char)0;
  463. };
  464. if (RetVal != NULL) {
  465. for (int i = RStart + 1; i < REnd; i++) {
  466. RetVal = (CSTR)realloc(RetVal, strlen(RetVal) + strlen((*Data).at(i)) + 1 + strlen(S_Str));
  467. sprintf(RetVal, "%s%s%s", RetVal, S_Str, (*Data).at(i));
  468. };
  469. };
  470. return RetVal;
  471. };
  472. int getIP4( if_info **arrParam, int max_ifs ){
  473. int index = 0;
  474. #ifdef __linux__
  475. struct ifaddrs *addrs, *tmp;
  476. getifaddrs(&addrs);
  477. tmp = addrs;
  478. //arrParam= malloc( 10 * sezeof(if_info) );
  479. while (tmp){
  480. if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_INET){
  481. if( strncmp( tmp->ifa_name, "lo", 2 ) != 0 ){
  482. (*arrParam)[index].if_name = (CSTR)malloc(strlen(tmp->ifa_name)+1);
  483. snprintf( (*arrParam)[index].if_name, strlen(tmp->ifa_name)+1,"%s", tmp->ifa_name );
  484. (*arrParam)[index].if_addr = (CSTR)malloc(16);
  485. struct sockaddr_in *pAddr = (struct sockaddr_in *)tmp->ifa_addr;
  486. snprintf( (*arrParam)[index].if_addr, 16, "%s", inet_ntoa(pAddr->sin_addr) );
  487. index++;
  488. if( index == max_ifs ) break;
  489. }
  490. };
  491. tmp = tmp->ifa_next;
  492. };
  493. freeifaddrs(addrs);
  494. #endif
  495. return index;
  496. };
  497. #ifdef __linux__
  498. long long getFreeSpace( CSTR DirName ){
  499. struct statvfs fiData;
  500. if( ( statvfs( DirName, &fiData ) ) < 0 ) {
  501. return -1;
  502. };
  503. return fiData.f_bsize * fiData.f_bfree;
  504. };
  505. long long getTotalSpace( CSTR DirName ){
  506. struct statvfs fiData;
  507. if( ( statvfs( DirName, &fiData ) ) < 0 ) {
  508. return -1;
  509. };
  510. return fiData.f_bsize * fiData.f_blocks;
  511. };
  512. #else
  513. __int64 getFreeSpace( CSTR DirName ){
  514. return -1;
  515. };
  516. __int64 getTotalSpace( CSTR DirName ){
  517. return -1;
  518. };
  519. #endif
  520. CSTR getTStr( CSTR Fmt ){
  521. //CSTR RetVal = NULL;
  522. char DateStr[200];
  523. time_t t;
  524. struct tm *tmp;
  525. t = time(NULL);
  526. tmp = gmtime(&t);
  527. if( tmp == NULL) {
  528. fprintf(stderr, "localtime!");
  529. return NULL;
  530. };
  531. if( Fmt == NULL ) {
  532. if( strftime( DateStr, 200, "%F %T GMT", tmp )==0 ){
  533. fprintf( stderr, "strftime returned 0" );
  534. return NULL;
  535. };
  536. } else {
  537. if( strftime( DateStr, 200, Fmt, tmp )==0 ){
  538. fprintf( stderr, "strftime returned 0" );
  539. return NULL;
  540. };
  541. }
  542. //RetVal =(CSTR) malloc( strlen(DateStr) +1 );
  543. //if( RetVal != NULL ) strcpy( RetVal, DateStr );
  544. #ifdef __linux__
  545. return strdup(DateStr);
  546. #else
  547. return _strdup(DateStr);
  548. #endif
  549. };
  550. #ifdef __linux__
  551. int xioctl(int fh, int request, void *arg){
  552. int r;
  553. do {
  554. r = ioctl( fh, request, arg );
  555. } while (-1 == r && EINTR == errno);
  556. return r;
  557. };
  558. uint64_t getMilliseconds( void ){
  559. uint64_t tMilliseconds = (uint64_t) 0;
  560. struct timeval tData;
  561. struct timezone tTZ;
  562. tTZ.tz_minuteswest = 0;
  563. tTZ.tz_dsttime = 0;
  564. if( gettimeofday( &tData, &tTZ ) == -1 ){
  565. tMilliseconds = (uint64_t) 0;
  566. #ifdef DEBUG
  567. URGENT_LOG( "EbioMat'!");
  568. #endif
  569. } else {
  570. tMilliseconds = (uint64_t) tData.tv_sec;
  571. tMilliseconds = ( tMilliseconds * 1000 );
  572. tMilliseconds += (uint64_t)( tData.tv_usec / 1000 );
  573. //URGENT_LOG("MilSecCollected!");
  574. };
  575. #else
  576. #include<ctime>
  577. unsigned __int64 getMilliseconds( void ){
  578. unsigned __int64 tMilliseconds;
  579. SYSTEMTIME time;
  580. GetSystemTime(&time);
  581. tMilliseconds = (uint32_t)std::time(NULL);
  582. tMilliseconds *= 1000;
  583. tMilliseconds += time.wMilliseconds;
  584. #endif
  585. return tMilliseconds;
  586. };
  587. #ifdef __linux__
  588. #ifndef USE_SYSTEM_CALL_TO_PING
  589. #include <netinet/ip_icmp.h>
  590. // Define the Packet Constants
  591. // ping packet size
  592. #define PING_PKT_S 64
  593. // Automatic port number
  594. #define PORT_NO 0
  595. // Automatic port number
  596. #define PING_SLEEP_RATE 1000000
  597. // Gives the timeout delay for receiving packets
  598. // in seconds
  599. #define RECV_TIMEOUT 1
  600. // ping packet structure
  601. struct ping_pkt {
  602. struct icmphdr hdr;
  603. char msg[PING_PKT_S-sizeof(struct icmphdr)];
  604. };
  605. #endif //!USE_SYSTEM_CALL_TO_PING
  606. bool pingIP4( CSTR ip4 ){
  607. #ifdef USE_SYSTEM_CALL_TO_PING
  608. // Ping google.com once, redirect output to /dev/null, and check the return value
  609. char tB[256];
  610. sprintf( tB, "ping -c 1 %s > /dev/null 2>&1", ip4 );
  611. if ( system( tB ) == 0) return true;
  612. return false;
  613. #else // !USE_SYSTEM_CALL_TO_PING
  614. bool RetVal = false;
  615. if ( ip4 == NULL ) return false;
  616. if ( strlen( ip4 ) < 7 ) return false;
  617. #ifdef DEBUG_EXTRA
  618. URGENT_LOG("Pinging Device!");
  619. #endif
  620. int sockfd;
  621. int ttl_val=64, msg_count=0, i, flag=1, msg_received_count=0;
  622. struct ping_pkt pckt;
  623. struct sockaddr_in ping_addr;
  624. struct sockaddr_in r_addr;
  625. struct timeval tv_out;
  626. tv_out.tv_sec = 0;
  627. tv_out.tv_usec = 300000; // 1/3 of second
  628. ping_addr.sin_family = AF_INET;
  629. ping_addr.sin_addr.s_addr = inet_addr( ip4 );
  630. //ping_addr.sin_port = htons( m_nPort );
  631. #ifdef DEBUG_EXTRA
  632. URGENT_LOG( ip4 );
  633. #endif
  634. sockfd = socket( AF_INET, SOCK_RAW, IPPROTO_ICMP );
  635. if( sockfd < 0 ){
  636. URGENT_LOG("Socket file descriptor not received!");
  637. return false;
  638. };
  639. // set socket options at ip to TTL and value to 64,
  640. // change to what you want by setting ttl_val
  641. if( setsockopt( sockfd, SOL_IP, IP_TTL, &ttl_val, sizeof(ttl_val)) != 0 ){
  642. URGENT_LOG("Setting socket options to TTL failed!");
  643. shutdown( sockfd, SHUT_RDWR );
  644. return false;
  645. };
  646. // setting timeout of recv setting
  647. setsockopt( sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv_out, sizeof tv_out );
  648. // flag is whether packet was sent or not
  649. flag=1;
  650. //filling packet
  651. bzero(&pckt, sizeof(pckt));
  652. pckt.hdr.type = ICMP_ECHO;
  653. pckt.hdr.un.echo.id = getpid();
  654. for ( i = 0; i < sizeof(pckt.msg)-1; i++ ) pckt.msg[i] = i+'0';
  655. pckt.msg[i] = 0;
  656. pckt.hdr.un.echo.sequence = msg_count++;
  657. pckt.hdr.checksum = checksumPing(&pckt, sizeof(pckt));
  658. //SLEEP( 300 );
  659. //send packet
  660. if( sendto( sockfd, &pckt, sizeof(pckt), 0, (struct sockaddr*)&ping_addr, sizeof(ping_addr)) <= 0 ){
  661. URGENT_LOG("Packet Sending Failed!");
  662. shutdown( sockfd, SHUT_RDWR );
  663. return false;
  664. };
  665. //receive packet
  666. socklen_t addr_len = sizeof(r_addr);
  667. if( recvfrom( sockfd, &pckt, sizeof(pckt), 0, (struct sockaddr*)&r_addr, &addr_len) <= 0 ){
  668. #ifdef DEBUG_EXTRA
  669. URGENT_LOG("Packet receive failed!");
  670. #endif
  671. } else {
  672. #ifdef DEBUG_EXTRA
  673. URGENT_LOG("Device pinged Sucessfully");
  674. #endif
  675. RetVal = true;
  676. };
  677. shutdown( sockfd, SHUT_RDWR );
  678. return RetVal;
  679. #endif //!USE_SYSTEM_CALL_TO_PING
  680. };
  681. bool enumerateDevices( enumeratorInfo *mlist, const char *dTemplate){
  682. DIR *d;
  683. struct dirent *dir;
  684. DeviceInfo *tDevInfo;
  685. if( mlist == NULL )return false;
  686. if( dTemplate == NULL )return false;
  687. for( int i = 0; i < mlist->size(); i++ ) {
  688. delete( mlist->at(i) );
  689. };
  690. mlist->clear();
  691. d = opendir("/dev");
  692. if( d ){
  693. while((dir = readdir(d)) != NULL){
  694. if( dir->d_type == DT_CHR || dir->d_type == DT_BLK ){
  695. if( strncmp( dir->d_name, dTemplate, 5 ) == 0 ) {
  696. tDevInfo = (DeviceInfo *) malloc( sizeof(DeviceInfo) );
  697. sprintf( tDevInfo->DevicePath, "/dev/%s", dir->d_name );
  698. tDevInfo->FriendlyName[0] =(char)0;
  699. if( strncmp( dTemplate, "video", 5 ) == 0 ){
  700. FILE *fd;
  701. char CMD[256];
  702. sprintf( CMD, "v4l2-ctl -d %s -D", tDevInfo->DevicePath );
  703. fd = popen( CMD, "r");
  704. if( fd != NULL ) {
  705. char * line = NULL;
  706. size_t len = 0;
  707. ssize_t rd;
  708. while ((rd = getline(&line, &len, fd)) != -1) {
  709. char *tS1, *tS2;
  710. // Future use strcasestr() or make both stings same case
  711. if( ( tS1 = strstr( line, "Bus info" )) != NULL ) {
  712. tS2 = trim( strstr( tS1, ":" ) + 2 );
  713. sprintf( tDevInfo->FriendlyName, "%s", tS2 );
  714. };
  715. };
  716. fclose( fd );
  717. if( line ) free( line );
  718. };
  719. };
  720. mlist->push_back( tDevInfo );
  721. //printf("%s\n", dir->d_name );
  722. };
  723. //if( strncmp( dir->d_name, "ttyS", 4 ) == 0 ){
  724. // tBuffer = ( char * ) malloc( strlen(dir->d_name)+1 );
  725. // strncpy( tBuffer, dir->d_name, strlen(dir->d_name)+1 );
  726. // mlist->pushBack( tBuffer );
  727. // //printf("%s\n", dir->d_name );
  728. //};
  729. };
  730. };
  731. closedir(d);
  732. } else {
  733. return false;
  734. };
  735. return true;
  736. };
  737. #else
  738. #ifndef IID_PPV_ARGS
  739. extern "C++" {
  740. template<typename T> void** IID_PPV_ARGS_Helper(T** pp)
  741. {
  742. // make sure everyone derives from IUnknown
  743. static_cast<IUnknown*>(*pp);
  744. return reinterpret_cast<void**>(pp);
  745. }
  746. }
  747. #define IID_PPV_ARGS(ppType) __uuidof(**(ppType)), IID_PPV_ARGS_Helper(ppType)
  748. #endif
  749. #pragma comment(lib,"Strmiids.lib")
  750. bool enumerateDevices(enumeratorInfo *mlist, REFGUID category){
  751. HRESULT hr;
  752. bool initOk = true;
  753. hr = CoInitializeEx( NULL, COINIT_SPEED_OVER_MEMORY | COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE );
  754. if( hr == E_INVALIDARG ){
  755. printf("Couldn't Init! hr=0x%x", hr);
  756. return false;
  757. } else if ( hr == E_OUTOFMEMORY ){
  758. printf("Couldn't Init! hr=0x%x", hr);
  759. return false;
  760. } else if ( hr == E_UNEXPECTED ){
  761. printf("Couldn't Init! hr=0x%x", hr);
  762. return false;
  763. } else if( hr == RPC_E_CHANGED_MODE ){
  764. initOk = false;
  765. };
  766. if( hr == S_OK || hr == RPC_E_CHANGED_MODE ){
  767. // Create the system device enumerator
  768. ICreateDevEnum *pDevEnum;
  769. hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum) );
  770. if( hr != S_OK ){
  771. printf("Couldn't create system enumerator! hr=0x%x", hr);
  772. CoUninitialize();
  773. return false;
  774. };
  775. if( pDevEnum != NULL ){
  776. IEnumMoniker *pClsEnum = NULL;
  777. hr = pDevEnum->CreateClassEnumerator(category, &pClsEnum, 0);
  778. if( hr == S_FALSE ){
  779. // The category is empty. Treat as an error.
  780. pDevEnum->Release();
  781. CoUninitialize();
  782. return false;
  783. };
  784. if( pClsEnum != NULL ){
  785. IMoniker *pMoniker = NULL;
  786. while( pClsEnum->Next(1, &pMoniker, NULL) == S_OK ){
  787. IPropertyBag *pPropBag = NULL;
  788. hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag));
  789. if( FAILED(hr) ){
  790. pMoniker->Release();
  791. continue;
  792. };
  793. VARIANT var;
  794. VariantInit(&var);
  795. DeviceInfo *tRec;
  796. tRec = (DeviceInfo *)malloc(sizeof(DeviceInfo));
  797. // Get description or friendly name.
  798. hr = pPropBag->Read(L"Description", &var, 0);
  799. if( FAILED(hr) ){
  800. hr = pPropBag->Read(L"FriendlyName", &var, 0);
  801. };
  802. if (SUCCEEDED(hr)){
  803. if (tRec != NULL) {
  804. sprintf(tRec->FriendlyName, "%S", var.bstrVal);
  805. mlist->push_back(tRec);
  806. };
  807. VariantClear(&var);
  808. };
  809. hr = pPropBag->Write(L"FriendlyName", &var);
  810. // WaveInID applies only to audio capture devices.
  811. hr = pPropBag->Read(L"WaveInID", &var, 0);
  812. if (SUCCEEDED(hr)){
  813. //printf("WaveIn ID: %d\n", var.lVal);
  814. VariantClear(&var);
  815. };
  816. hr = pPropBag->Read(L"DevicePath", &var, 0);
  817. if (SUCCEEDED(hr)){
  818. // The device path is not intended for display.
  819. if( tRec != NULL) {
  820. sprintf(tRec->DevicePath, "%S", var.bstrVal);
  821. };
  822. VariantClear(&var);
  823. };
  824. pPropBag->Release();
  825. pMoniker->Release();
  826. };
  827. pClsEnum->Release();
  828. };
  829. pDevEnum->Release();
  830. };
  831. if( initOk ) CoUninitialize();
  832. };
  833. return true;
  834. };
  835. #endif
  836. size_t escape_string( char *to, size_t to_length, const char *from, size_t length ){
  837. const char *to_start = to;
  838. const char *end, *to_end = to_start + (to_length ? to_length - 1 : 2 * length);
  839. bool overflow = false;
  840. for( end = from + length; from < end; from++ ) {
  841. char escape = 0;
  842. int tmp_length;
  843. /*
  844. If the next character appears to begin a multi-byte character, we
  845. escape that first byte of that apparent multi-byte character. (The
  846. character just looks like a multi-byte character -- if it were actually
  847. a multi-byte character, it would have been passed through in the test
  848. above.)
  849. Without this check, we can create a problem by converting an invalid
  850. multi-byte character into a valid one. For example, 0xbf27 is not
  851. a valid GBK character, but 0xbf5c is. (0x27 = ', 0x5c = \)
  852. */
  853. tmp_length = 0;
  854. if( tmp_length > 1 ){
  855. escape = *from;
  856. } else{
  857. switch (*from) {
  858. case 0: /* Must be escaped for 'mysql' */
  859. escape = '0';
  860. break;
  861. case '\n': /* Must be escaped for logs */
  862. escape = 'n';
  863. break;
  864. case '\r':
  865. escape = 'r';
  866. break;
  867. case '\\':
  868. escape = '\\';
  869. break;
  870. case '\'':
  871. escape = '\'';
  872. break;
  873. case '"': /* Better safe than sorry */
  874. escape = '"';
  875. break;
  876. case '\032': /* This gives problems on Win32 */
  877. escape = 'Z';
  878. break;
  879. };
  880. };
  881. if( escape ) {
  882. if (to + 2 > to_end) { overflow = true; break; };
  883. *to++ = '\\';
  884. *to++ = escape;
  885. } else {
  886. if (to + 1 > to_end) { overflow = true; break; };
  887. *to++ = *from;
  888. };
  889. };
  890. *to = 0;
  891. return overflow ? (size_t)-1 : (size_t)(to - to_start);
  892. };
  893. /* Read characters from 'fd' until a newline is encountered. If a newline
  894. character is not encountered in the first (n - 1) bytes, then the excess
  895. characters are discarded. The returned string placed in 'buf' is
  896. null-terminated and includes the newline character if it was read in the
  897. first (n - 1) bytes. The function return value is the number of bytes
  898. placed in buffer (which includes the newline character if encountered,
  899. but excludes the terminating null byte). */
  900. ssize_t readLine(int fd, char *buffer, size_t n) {
  901. ssize_t numRead; /* # of bytes fetched by last read() */
  902. size_t totRead; /* Total bytes read so far */
  903. char *buf;
  904. char ch;
  905. if (n <= 0 || buffer == NULL) {
  906. errno = EINVAL;
  907. return -1;
  908. }
  909. buf = buffer; /* No pointer arithmetic on "void *" */
  910. totRead = 0;
  911. for (;;) {
  912. numRead = read(fd, &ch, 1);
  913. if (numRead == -1) {
  914. if (errno == EINTR) /* Interrupted --> restart read() */
  915. continue;
  916. else
  917. return -1; /* Some other error */
  918. } else if (numRead == 0) { /* EOF */
  919. if (totRead == 0) /* No bytes read; return 0 */
  920. return 0;
  921. else /* Some bytes read; add '\0' */
  922. break;
  923. } else { /* 'numRead' must be 1 if we get here */
  924. if (totRead < n - 1) { /* Discard > (n - 1) bytes */
  925. totRead++;
  926. *buf++ = ch;
  927. }
  928. if (ch == '\n')
  929. break;
  930. }
  931. }
  932. *buf = '\0';
  933. return totRead;
  934. };
  935. ssize_t readLine( FILE *fd, char *buffer, size_t n) {
  936. ssize_t numRead; /* # of bytes fetched by last read() */
  937. size_t totRead; /* Total bytes read so far */
  938. char *buf;
  939. char ch;
  940. if (n <= 0 || buffer == NULL) {
  941. errno = EINVAL;
  942. return -1;
  943. }
  944. buf = buffer; /* No pointer arithmetic on "void *" */
  945. totRead = 0;
  946. for (;;) {
  947. numRead = fread(&ch, 1, 1, fd);
  948. if (numRead == -1) {
  949. if (errno == EINTR) /* Interrupted --> restart read() */
  950. continue;
  951. else
  952. return -1; /* Some other error */
  953. } else if (numRead == 0) { /* EOF */
  954. if (totRead == 0) /* No bytes read; return 0 */
  955. return 0;
  956. else /* Some bytes read; add '\0' */
  957. break;
  958. } else { /* 'numRead' must be 1 if we get here */
  959. if (totRead < n - 1) { /* Discard > (n - 1) bytes */
  960. totRead++;
  961. *buf++ = ch;
  962. }
  963. if (ch == '\n')
  964. break;
  965. }
  966. }
  967. *buf = '\0';
  968. return totRead;
  969. };
  970. localIpAddresses g_vLocalIpAdressess;
  971. bool g_bAdressesAreDiscovered = false;
  972. #ifdef __linux__
  973. bool isProcessUniq( CSTR pName ){
  974. char tB[256];
  975. FILE *fd;
  976. sprintf( tB, "pidof %s", pName );
  977. fd = popen( tB, "r" );
  978. if ( fd ) {
  979. int rs = readLine( fd, tB, 256 );
  980. if ( rs > 2 ) {
  981. fclose( fd );
  982. return false;
  983. };
  984. fclose( fd );
  985. };
  986. return true;
  987. };
  988. #include <linux/if.h>
  989. localIpAddresses *getLocalIP(){
  990. // Check for Filter LO and TUN Devices!
  991. if( !g_bAdressesAreDiscovered ) {
  992. struct ifaddrs *ifaddr, *ifa;
  993. localIp4Info *tIpAddress = NULL;
  994. if (getifaddrs(&ifaddr) != -1) {
  995. /* Walk through linked list, maintaining head pointer so we can free list later */
  996. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  997. // Filter Loopback if
  998. if( strstr( ifa->ifa_name, "lo" ) != NULL ) continue;
  999. // Filter vpn TUN if
  1000. if( strstr( ifa->ifa_name, "tun" ) != NULL ) continue;
  1001. char protocol[IFNAMSIZ] = {0};
  1002. if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET) continue;
  1003. tIpAddress = (localIp4Info*)malloc(sizeof(localIp4Info));
  1004. if(tIpAddress!=NULL){
  1005. sprintf( tIpAddress->address , "%d.%d.%d.%d",
  1006. (uint8_t)ifa->ifa_addr->sa_data[2],
  1007. (uint8_t)ifa->ifa_addr->sa_data[3],
  1008. (uint8_t)ifa->ifa_addr->sa_data[4],
  1009. (uint8_t)ifa->ifa_addr->sa_data[5]
  1010. );
  1011. sprintf( tIpAddress->netmask , "%d.%d.%d.%d",
  1012. (uint8_t)ifa->ifa_netmask->sa_data[2],
  1013. (uint8_t)ifa->ifa_netmask->sa_data[3],
  1014. (uint8_t)ifa->ifa_netmask->sa_data[4],
  1015. (uint8_t)ifa->ifa_netmask->sa_data[5]
  1016. );
  1017. sprintf( tIpAddress->broadcast , "%d.%d.%d.%d",
  1018. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[2],
  1019. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[3],
  1020. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[4],
  1021. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[5]
  1022. );
  1023. g_vLocalIpAdressess.push_back(tIpAddress);
  1024. };
  1025. };
  1026. freeifaddrs(ifaddr);
  1027. };
  1028. g_bAdressesAreDiscovered = true;
  1029. };
  1030. return &g_vLocalIpAdressess;
  1031. };
  1032. #else // !linux
  1033. //https://stackoverflow.com/questions/5213629/how-to-enumerate-all-available-network-interfaces
  1034. #include <iphlpapi.h>
  1035. #pragma comment(lib, "IPHLPAPI.lib")
  1036. localIpAddresses* getLocalIP() {
  1037. char psBuffer[128];
  1038. FILE* fd;
  1039. BYTE mB;
  1040. localIp4Info* tIpAddress;
  1041. if (!g_bAdressesAreDiscovered) {
  1042. ULONG outBufLen = 0;
  1043. DWORD dwRetVal = 0;
  1044. IP_ADAPTER_INFO* pAdapterInfos = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO));
  1045. // retry up to 5 times, to get the adapter infos needed
  1046. for (int i = 0; i < 5 && (dwRetVal == ERROR_BUFFER_OVERFLOW || dwRetVal == NO_ERROR); ++i) {
  1047. dwRetVal = GetAdaptersInfo(pAdapterInfos, &outBufLen);
  1048. if (dwRetVal == NO_ERROR) {
  1049. break;
  1050. } else {
  1051. if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
  1052. free(pAdapterInfos);
  1053. pAdapterInfos = (IP_ADAPTER_INFO*)malloc(outBufLen);
  1054. } else {
  1055. //pAdapterInfos = 0;
  1056. break;
  1057. };
  1058. };
  1059. };
  1060. if (dwRetVal == NO_ERROR) {
  1061. IP_ADAPTER_INFO* pAdapterInfo = pAdapterInfos;
  1062. while (pAdapterInfo) {
  1063. IP_ADDR_STRING* pIpAddress = &(pAdapterInfo->IpAddressList);
  1064. while (pIpAddress != 0) {
  1065. uint8_t A, B, C, D;
  1066. uint8_t M, O, S, K, G, U, T, E;
  1067. uint32_t Z1, Z2, Z3, Z4;
  1068. int RetVal;
  1069. RetVal = sscanf(pIpAddress->IpAddress.String, "%d.%d.%d.%d", &Z1, &Z2, &Z3, &Z4);
  1070. A = Z1 & 0xFF;
  1071. B = Z2 & 0xFF;
  1072. C = Z3 & 0xFF;
  1073. D = Z4 & 0xFF;
  1074. RetVal = sscanf(pIpAddress->IpMask.String, "%d.%d.%d.%d", &Z1, &Z2, &Z3, &Z4);
  1075. M = Z1 & 0xFF;
  1076. O = Z2 & 0xFF;
  1077. S = Z3 & 0xFF;
  1078. K = Z4 & 0xFF;
  1079. //
  1080. // <<<<
  1081. // here pAdapterInfo->Address should contain the MAC address
  1082. // >>>>
  1083. //
  1084. if (A > 0) {
  1085. tIpAddress = (localIp4Info*)malloc(sizeof(localIp4Info));
  1086. if (tIpAddress != NULL) {
  1087. sprintf(tIpAddress->address, "%d.%d.%d.%d", A, B, C, D );
  1088. sprintf(tIpAddress->netmask, "%d.%d.%d.%d", M, O, S, K );
  1089. G = (A & M) | (~M);
  1090. U = (B & O) | (~O);
  1091. T = (C & S) | (~S);
  1092. E = (D & K) | (~K);
  1093. sprintf(tIpAddress->broadcast, "%d.%d.%d.%d", G, U, T, E );
  1094. g_vLocalIpAdressess.push_back(tIpAddress);
  1095. };
  1096. };
  1097. pIpAddress = pIpAddress->Next;
  1098. };
  1099. pAdapterInfo = pAdapterInfo->Next;
  1100. };
  1101. };
  1102. free(pAdapterInfos);
  1103. };
  1104. return &g_vLocalIpAdressess;
  1105. };
  1106. #endif
  1107. #ifdef DEBUG
  1108. #undef URGENT_LOG
  1109. #endif