Exemple #1
0
/**
 * @fn tnet_startup
 * This is probably the most important function. You MUST call this function to initialize the network stack before calling any <b>tnet_*</b> function. 
 *			You MUST call @ref tnet_cleanup to cleanup the network stack.
 *
 * @sa @ref tnet_cleanup.
 * @return	Zero if succeed and error code otherwise. 
**/
int tnet_startup()
{
	int err = 0;
	short word = 0x4321;

	if(__tnet_started){
		goto bail;
	}

	// rand()
	srand((unsigned int) tsk_time_epoch());

	// endianness
	tnet_isBigEndian = ((*(int8_t *)&word) != 0x21);
#if TNET_UNDER_WINDOWS
	if(tnet_isBigEndian){
		TSK_DEBUG_ERROR("Big endian on Windows machine. Is it right?");
	}
#endif

#if TNET_UNDER_WINDOWS
	{
		WORD wVersionRequested;
		WSADATA wsaData;

		wVersionRequested = MAKEWORD(2, 2);

		err = WSAStartup(wVersionRequested, &wsaData);
		if (err != 0) {
			TSK_DEBUG_FATAL("WSAStartup failed with error: %d\n", err);
			return -1;
		}

		if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2){
			TSK_DEBUG_FATAL("Could not find a usable version of Winsock.dll\n");
			tnet_cleanup();
			return -2;
		}
		else{
			__tnet_started = tsk_true;
			TSK_DEBUG_INFO("The Winsock 2.2 dll was found okay\n");
		}
	}
#else
	__tnet_started = tsk_true;
#endif /* TNET_UNDER_WINDOWS */
	
bail:
	return err;
}
Exemple #2
0
/**@ingroup tsk_uuid_group
*/
int tsk_uuidgenerate(tsk_uuidstring_t *result)
{
	/* From wikipedia
	*	Version 5 UUIDs use a scheme with SHA-1 hashing, otherwise it is the same idea as in version 3. 
	*	RFC 4122 states that version 5 is preferred over version 3 name based UUIDs. 
	*	Note that the 160 bit SHA-1 hash is truncated to 128 bits to make the length work out.
	*/
	tsk_sha1string_t sha1result;
	tsk_istr_t epoch;
	unsigned i, k;
	static char HEX[] = "0123456789abcdef";

	tsk_itoa(tsk_time_epoch(), &epoch);
	tsk_sha1compute(epoch, sizeof(epoch), &sha1result);

	/* XOR the SHA-1 result with random numbers. */
	for(i=0; i<(TSK_UUID_DIGEST_SIZE*2); i+=4){
#if 0
		*((uint32_t*)&sha1result[i]) ^= rand();
#else
		k = rand();
		sha1result[i] ^= k, sha1result[i + 1] ^= k,
		sha1result[i + 2] ^= k, sha1result[i + 3] ^= k;
#endif
		
		for(k=0; k<sizeof(uint32_t); k++){
			sha1result[i+k] = HEX[sha1result[i+k] & 0x0F]; /* To hexa. */
		}
	}

	/* f47ac10b-58cc-4372-a567-0e02b2c3d479 */
	memcpy(&(*result)[0], &sha1result[0], 8);
	(*result)[8] = '-';

	memcpy(&(*result)[9], &sha1result[8], 4);
	(*result)[13] = '-';

	memcpy(&(*result)[14], &sha1result[12], 4);
	(*result)[18] = '-';

	memcpy(&(*result)[19], &sha1result[16], 4);
	(*result)[23] = '-';

	memcpy(&(*result)[24], &sha1result[20], 12);
	(*result)[36] = '\0';

	return 0;
}
Exemple #3
0
// /!\ NOT CURRENT TIME
// only make sense when comparing two values (e.g. for duration)
uint64_t tsk_time_now()
{
#if TSK_UNDER_WINDOWS
	static int __cpu_count = 0;
	if(__cpu_count == 0){
		SYSTEM_INFO SystemInfo;
#	if TSK_UNDER_WINDOWS_RT
		GetNativeSystemInfo(&SystemInfo);
#	else
		GetSystemInfo(&SystemInfo);
#	endif
		__cpu_count = SystemInfo.dwNumberOfProcessors;
	}
	if(__cpu_count == 1){
		static LARGE_INTEGER __liFrequency = {0};
		LARGE_INTEGER liPerformanceCount;
		if(!__liFrequency.QuadPart){
			QueryPerformanceFrequency(&__liFrequency);
		}
		QueryPerformanceCounter(&liPerformanceCount);
		return (uint64_t)(((double)liPerformanceCount.QuadPart/(double)__liFrequency.QuadPart)*1000.0);
	}
	else{
#	if TSK_UNDER_WINDOWS_RT
		return tsk_time_epoch();
#	else
		return timeGetTime();
#	endif
	}
#elif HAVE_CLOCK_GETTIME || _POSIX_TIMERS > 0
	struct timespec ts;
	clock_gettime(CLOCK_MONOTONIC, &ts);
	return (((uint64_t)ts.tv_sec)*(uint64_t)1000) + (((uint64_t)ts.tv_nsec)/(uint64_t)1000000);
#elif defined(__APPLE__)
    static mach_timebase_info_data_t __apple_timebase_info = {0, 0};
    if (__apple_timebase_info.denom == 0) {
        (void) mach_timebase_info(&__apple_timebase_info);
    }
    return (uint64_t)((mach_absolute_time() * __apple_timebase_info.numer) / (1e+6 * __apple_timebase_info.denom));
#else
	struct timeval tv;
	gettimeofday(&tv, tsk_null); 
	return (((uint64_t)tv.tv_sec)*(uint64_t)1000) + (((uint64_t)tv.tv_usec)/(uint64_t)1000);
#endif
}
Exemple #4
0
tsk_semaphore_handle_t* tsk_semaphore_create_2(int initial_val)
{
    SEMAPHORE_T handle = tsk_null;
    
#if TSK_UNDER_WINDOWS
#	if TSK_UNDER_WINDOWS_RT
    handle = CreateSemaphoreEx(NULL, initial_val, 0x7FFFFFFF, NULL, 0x00000000, SEMAPHORE_ALL_ACCESS);
#	else
    handle = CreateSemaphore(NULL, initial_val, 0x7FFFFFFF, NULL);
#	endif
#else
    handle = tsk_calloc(1, sizeof(SEMAPHORE_S));
    
#if TSK_USE_NAMED_SEM
    named_sem_t * nsem = (named_sem_t*)handle;
    snprintf(nsem->name, (sizeof(nsem->name)/sizeof(nsem->name[0])) - 1, "/sem/%llu/%d.", tsk_time_epoch(), rand() ^ rand());
    if ((nsem->sem = sem_open(nsem->name, O_CREAT /*| O_EXCL*/, S_IRUSR | S_IWUSR, initial_val)) == SEM_FAILED) {
#else
        if (sem_init((SEMAPHORE_T)handle, 0, initial_val)) {
#endif
            TSK_FREE(handle);
            TSK_DEBUG_ERROR("Failed to initialize the new semaphore (errno=%d).", errno);
        }
#endif
        if (!handle) {
            TSK_DEBUG_ERROR("Failed to create new semaphore");
        }
        return handle;
    }
    
    /**@ingroup tsk_semaphore_group
     * Increments a semaphore.
     * @param handle The semaphore to increment.
     * @retval Zero if succeed and otherwise the function returns -1 and sets errno to indicate the error.
     * @sa @ref tsk_semaphore_decrement.
     */
    int tsk_semaphore_increment(tsk_semaphore_handle_t* handle)
    {
        int ret = EINVAL;
        if (handle) {
#if TSK_UNDER_WINDOWS
            if((ret = ReleaseSemaphore((SEMAPHORE_T)handle, 1L, NULL) ? 0 : -1))
#else
            if((ret = sem_post((SEMAPHORE_T)GET_SEM(handle))))
#endif
            {
                TSK_DEBUG_ERROR("sem_post function failed: %d", ret);
            }
        }
        return ret;
    }
Exemple #5
0
/**
 * @fn tnet_startup
 * This is probably the most important function. You MUST call this function to initialize the network stack before calling any <b>tnet_*</b> function. 
 *			You MUST call @ref tnet_cleanup to cleanup the network stack.
 *
 * @sa @ref tnet_cleanup.
 * @return	Zero if succeed and error code otherwise. 
**/
int tnet_startup()
{
	int err = 0;
	short word = 0x4321;

	if(__tnet_started){
		goto bail;
	}

	// rand()
	srand((unsigned int) tsk_time_epoch());

	// endianness
	tnet_isBigEndian = ((*(int8_t *)&word) != 0x21);
#if TNET_UNDER_WINDOWS
	if(tnet_isBigEndian){
		TSK_DEBUG_ERROR("Big endian on Windows machine. Is it right?");
	}
#endif

#if TNET_UNDER_WINDOWS
	{
		WORD wVersionRequested;
		WSADATA wsaData;

		wVersionRequested = MAKEWORD(2, 2);

		err = WSAStartup(wVersionRequested, &wsaData);
		if (err != 0) {
			fprintf(stderr, "WSAStartup failed with error: %d\n", err);
			return -1;
		}

		if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2){
			fprintf(stderr, "Could not find a usable version of Winsock.dll\n");
			tnet_cleanup();
			return -2;
		}
		else{
			fprintf(stdout, "The Winsock 2.2 dll was found okay\n");
		}
	}
#endif /* TNET_UNDER_WINDOWS */

#if HAVE_OPENSSL
	//fprintf(stdout, "SSL is enabled :)\n");
	SSL_library_init();
	OpenSSL_add_all_algorithms();
	SSL_load_error_strings();

	//fprintf(stdout, "DTLS supported: %s\n", tnet_dtls_is_supported() ? "yes" : "no");
	//fprintf(stdout, "DTLS-SRTP supported: %s\n", tnet_dtls_is_srtp_supported() ? "yes" : "no");
#else
	//fprintf(stderr, "SSL is disabled :(\n");
#endif
	
	__tnet_started = tsk_true;

bail:
	return err;
}