Exemplo n.º 1
0
void BufStringDelete( BufString *bs )
{
	if( bs != NULL )
	{
		if( bs->bs_Buffer )
		{
			FFree( bs->bs_Buffer );
		}
		FFree( bs );
	}
}
Exemplo n.º 2
0
/**
 * Destroys a running event manager and the associated list of events
 *
 * @param em pointer to the EventManager structure
 */
void EventManagerDelete( EventManager *em )
{
	DEBUG("[EventManager] Delete\n");
	// remove long time events
	if( em != NULL )
	{
		em->em_EventThread->t_Quit = TRUE;
		while( TRUE )
		{
			if( em->em_EventThread->t_Launched == FALSE )
			{
				break;
			}
			usleep( 5000 );
		}
		
		if( em->em_EventThread != NULL )
		{
			DEBUG("[EventManager] Delete thread\n");
			ThreadDelete( em->em_EventThread );
		}
		DEBUG("[EventManager] thread removed\n");
		
		// waiting till all functions died
		
		while( TRUE )
		{
			if( threadsNo <= 0 )
			{
				break;
			}
			DEBUG("[EventManager] Not all threads were closed properly, waiting. ThreadsNo: %d\n", threadsNo );
			usleep( 500 );
		}
		
		CoreEvent *locnce = em->em_EventList;
		while( locnce != NULL )
		{
			CoreEvent *rem = locnce;
			locnce = (CoreEvent *)locnce->node.mln_Succ;

			FFree( rem );
		}
		
		FFree( em );
	}
	DEBUG("[EventManager] Delete end\n");
}
Exemplo n.º 3
0
void test1(void) {
    int i;
    assert(MAX >= 100);
    InitFMem(BasicAlloc, BasicFree, NULL, NULL);
    memset(a, 0, sizeof(a));
    FAlloc(0);
    FFree(0);
    for (i = 0; i < MAX-50; i++) {
        AddElem(i, rand() % 50 + 1);
    }
    for (i = MAX-50; i < MAX; i++) {
        AddElem(i, rand() % 10 + 253);
    }

    for (i = 0; i < MAX; i++) {
        FreeElem(i);
    }

    for (i = 0; i < 10000; i++) {
        if (rand() % 2 && numInA < MAX) {
            AddRandElem(rand() % 20 + 1);
        } else if (numInA > 0) {
            FreeRandElem();
        }
    }
    FreeAllElems();
    AddRandElem(20);
    AddRandElem(40);
    AddRandElem(255);
    AddRandElem(256);
    FiniFMem(FMEM_CHECK);
    assert(g_memUsed == 0);
    printf("Test 1 -- Success.\n");
    printMemStats();
}
Exemplo n.º 4
0
void HashedString ( char **str )
{
	unsigned char temp[SHA_DIGEST_LENGTH];
	memset( temp, 0x0, SHA_DIGEST_LENGTH );
	
	//char *buf = FMallocAlign( ( SHIFT_LEFT( SHA_DIGEST_LENGTH, 1) ) + 1 );
	char *buf = FCallocAlign( ( SHIFT_LEFT( SHA_DIGEST_LENGTH, 1) ) + 1, 1 );
	//char *buf = FCalloc( ( SHIFT_LEFT( SHA_DIGEST_LENGTH, 1) ) + 1, sizeof( char ) );

	if( buf != NULL )
	{
		SHA1( ( unsigned char *)*str, strlen( *str ), temp);

		int i = 0;
		for ( ; i < SHA_DIGEST_LENGTH; i++ )
		{
			sprintf( (char*)&(buf[ SHIFT_LEFT( i, 1) ]), "%02x", temp[i] );
		}

		if ( *str ) 
		{
			FFree ( *str );
		}
		DEBUG ( "[HashedString] Hashing\n" );
		*str = buf;
		DEBUG ( "[HashedString] Hashed\n" );
	}
	else
	{
		FERROR("Cannot allocate memory for hashed string\n");
	}
}
Exemplo n.º 5
0
/**
 * Delete UserGroup
 *
 * @param ug pointer to UserGroup which will be deleted
 * @return 0 when success, otherwise error number
 */
int UserGroupDelete( UserGroup *ug )
{
	if( ug != NULL )
	{
		if( ug->ug_Name != NULL )
		{
			FFree( ug->ug_Name );
		}
		
		if( ug->ug_Type != NULL )
		{
			FFree( ug->ug_Type );
		}
	}
	return 0;
}
Exemplo n.º 6
0
/**
 * De-initialize authmodule
 *
 * @param l pointer to AuthMod
 */
void libClose( struct AuthMod *l )
{
	if( l->SpecialData != NULL )
	{
		FFree( l->SpecialData );
	}
	
	DEBUG("[FCDB] close\n");
}
Exemplo n.º 7
0
int KeyValueListDelete( KeyValueList *list )
{
	if( list != NULL )
	{
		if( list->key != NULL )
		{
			FFree( list->key );
		}
		
		if( list->value != NULL )
		{
			FFree( list->value );
		}
		
		FFree( list );
	}
	return 0;
}
Exemplo n.º 8
0
void AppSessionDelete( AppSession *as )
{
	DEBUG("[AppSession] Delete app session\n");
	if( as != NULL )
	{
		pthread_mutex_lock( &as->as_SessionsMut );
		
		SASUList *ali = as->as_UserSessionList;
		SASUList *rml = ali;
		
		while( ali != NULL )
		{
			rml = ali;
			ali = (SASUList *) ali->node.mln_Succ;
			
			FFree( rml );
			rml = NULL;
		}
		
		pthread_mutex_unlock( &as->as_SessionsMut );
		
		pthread_mutex_destroy( &as->as_SessionsMut );
		
		pthread_mutex_lock( &as->as_VariablesMut );
		
		INVAREntry *le = as->as_Variables;
		INVAREntry *re = le;
		while( le != NULL )
		{
			re = le;
			le = (INVAREntry *) le->node.mln_Succ;
			
			INVAREntryDelete( re );
		}
		
		pthread_mutex_unlock( &as->as_VariablesMut );
		
		pthread_mutex_destroy( &as->as_VariablesMut );
		
		FFree( as );
		as = NULL;
	}
	DEBUG("[AppSession] App sessions deleted\n");
}
Exemplo n.º 9
0
int KeyValueListSetValues( KeyValueList *list, char *key, char *value )
{
	if( list != NULL )
	{
		if( list->key != NULL )
		{
			FFree( list->key );
		}
		
		if( list->value != NULL )
		{
			FFree( list->value );
		}
		
		list->key = StringDuplicate( key );
		list->value = StringDuplicate( value );
	}
	return 0;
}
Exemplo n.º 10
0
static inline BYTE *readFormFromSocket( Socket_t *s, ULONG *length )
{
	BYTE *retData = NULL;
	char buffer[ MAX_SIZE ];
	int readSize;
	
	readSize = SocketRead( s, buffer, MAX_SIZE, 0 );
	DataForm *ldf = (DataForm *)buffer;
	
	// its our packet
	
	if( ldf->df_ID == ID_FCRE )
	{
		// message is bigger then buffer
		// we must read it all
		
		if( ldf->df_Size > MAX_SIZE )
		{
			unsigned int readedBytes = readSize;
			BufString *bs = BufStringNew();
			BufStringAddSize( bs, buffer, readSize );
			
			// reading bytes till end
			
			while( readedBytes <= ldf->df_Size )
			{
				readSize = SocketRead( s, buffer, MAX_SIZE, 0 );
				BufStringAddSize( bs, buffer, readSize );
				readedBytes += readSize;
			}
			
			FFree( bs );
			*length = bs->bs_Size;
			
			return (BYTE *)bs->bs_Buffer;
		}
		// message fits buffer
		else
		{
			if( ( retData = FCalloc( readSize, sizeof(BYTE) ) ) != NULL )
			{
				memcpy( retData, buffer, readSize );
				*length = readSize;
			}
			else
			{
 				ERROR("Cannot allocate memory for message buffer\n");
			}
		}
	}
	
	return retData;
}
Exemplo n.º 11
0
/**
 * Delete LocFile structure
 *
 * @param file pointer to LocFile which will be deleted
 */
void LocFileDelete( LocFile* file )
{
	if( file == NULL )
	{
		FERROR("Cannot free file which doesnt exist\n");
	}
	
	if( file->lf_Filename != NULL )
	{
		FFree( file->lf_Filename );
	}
	/*
	if( file->lf_Fp )
	{
		fclose( file->lf_Fp );
		file->lf_Fp = NULL;
	}
	*/
	if( file->lf_Path )
	{
		FFree( file->lf_Path );
		file->lf_Path = NULL;
	}
	if( file->lf_Buffer )
	{
#if LOCFILE_USE_MMAP == 0
		FFree( file->lf_Buffer );
#else
		munmap(file->lf_Buffer, file->lf_FileSize);
#endif
		file->lf_Buffer = NULL;
	}
	if( file->lf_Mime != NULL )
	{
		FFree( file->lf_Mime );
		file->lf_Mime = NULL;
	}

	FFree( file );	
}
Exemplo n.º 12
0
int SafeString ( char **string, int length )
{
	if ( length == 0 ) length = strlen ( *string );
	if ( ( *string )[length-1] != '\0' )
	{
		char *newStr = MakeString ( length );
		sprintf ( newStr, "%.*s", length, *string );
		FFree ( *string );
		*string = newStr;
		return length + 1;
	}
	return length;
}
Exemplo n.º 13
0
/**
 * Websocket delete function
 *
 * @param ws pointer to WebSocket structure which will be deleted
 */
void WebSocketDelete( WebSocket* ws )
{
	if( ws != NULL )
	{
		ws->ws_Quit = TRUE;
		DEBUG("[WS] Websocket close in progress\n");
		int tries = 0;
		
#ifdef ENABLE_WEBSOCKETS_THREADS
		while( TRUE )
		{
			if( WSThreadNum <= 0 && ws->ws_Thread->t_Launched == FALSE )
			{
				break;
			}
			DEBUG("[WS] Closeing WS. Threads: %d\n", WSThreadNum );
			sleep( 1 );
			
			tries++;
			if( tries > 50 )
			{
				//WorkerManagerDebug( SLIB );
				tries = 0;
			}
		}
#endif
		Log( FLOG_DEBUG, "[WS] Closeing thread\n");
		
		if( ws->ws_Thread )
		{
			ThreadDelete( ws->ws_Thread );
		}
		
		Log( FLOG_DEBUG, "[WS] Thread closed\n");
		
		if( ws->ws_Context != NULL )
		{
			lws_context_destroy( ws->ws_Context );
			ws->ws_Context = NULL;
		}
		
		if( ws->ws_CertPath != NULL )
		{
		}
			
		FFree( ws );
	}
	
	pthread_mutex_destroy( &WSThreadMutex );
}
Exemplo n.º 14
0
int Release( struct FHandler *s, void *f )
{
	if( f != NULL )
	{
		DEBUG("[fsysphp] Release filesystem\n");
		File *lf = (File*)f;
		
		if( lf->f_SpecialData )
		{
			SpecialData *sd = (SpecialData *)lf->f_SpecialData;
		
			// Free up active device information
			if( sd->module ){ FFree( sd->module ); sd->module = NULL; }
			//if( lf->f_SessionID ){ FFree( lf->f_SessionID ); lf->f_SessionID = NULL; }
			if( sd->type ){ FFree( sd->type ); sd->type = NULL; }
			FFree( lf->f_SpecialData );
		}
		
		// Free up active device information
		if( lf->f_Name ){ FFree( lf->f_Name ); }
		if( lf->f_Path ){ FFree( lf->f_Path ); }
	}
	return 0;
}
Exemplo n.º 15
0
void StringSecureFree( char* str )
{
	char *ptr;
	if( !str )
	{
		return;
	}
	ptr = str;
	while( *ptr )
	{
		*(ptr++) = 0;
	}
	FFree( str );
	return;
}
Exemplo n.º 16
0
void SSHServerDelete( SSHServer *ts )
{
	if( ts != NULL )
	{
		ts->sshs_Quit = TRUE;
		
#ifdef ENABLE_SSH	
		if( ts->sshs_Thread )
		{
			ThreadDelete( ts->sshs_Thread );
		}
		ssh_finalize();
#endif		
		FFree( ts );
	}
}
Exemplo n.º 17
0
void FreeElem(unsigned num) {
    int i;
    int size;
    char *p;
    assert(num < MAX);
    assert(a[num].ptr != NULL);
    assert(numInA > 0);
    p = a[num].ptr;
    size = a[num].size;
    for (i = 0; i < size; i++) {
        assert(p[i] == (char) size);
    }
    FFree(p);
    numInA--;
    a[num].ptr = NULL;
    a[num].size = 0;
}
Exemplo n.º 18
0
void test2(void) {
    clock_t timer = clock();
    int top = 0;
    int i;
    for (i = 0; i < 500000; i++) {
        if (rand() % 2) {
            if (top < MAX) {
                a[top++].ptr = FAlloc(rand() % 20 + 1);
            }
        } else if (top > 0) {
            FFree(a[--top].ptr);
        }
    }
    FiniFMem(FMEM_NO_CHECK);
    timer = clock() - timer;
    printf("Time spent: %ld\n", timer);
    printf("Test 2 -- Success.\n");
}
Exemplo n.º 19
0
BufString *BufStringNewSize( int bufsize )
{
	BufString *str = NULL;
		
	if( ( str = FCalloc( sizeof( BufString ), 1 ) ) != NULL )
	{
		str->bs_Size = 0;
		str->bs_Bufsize = bufsize;
		str->bs_MAX_SIZE = bufsize;
		
		if( ( str->bs_Buffer = FCalloc( str->bs_Bufsize + 1, sizeof( char ) ) ) != NULL )
		{
			return str;
		}
		FFree( str );
	}
		
	return NULL;
}
Exemplo n.º 20
0
int AppSessionRemUsersession( AppSession *as, UserSession *u )
{
	if( as != NULL )
	{
		DEBUG("[AppSession] AppSessionRemUsersession %s\n", u->us_SessionID );
		
		pthread_mutex_lock( &as->as_SessionsMut );
		
		SASUList *ali = (SASUList *)as->as_UserSessionList->node.mln_Succ; // we cannot remove owner
		SASUList *prevali = as->as_UserSessionList;
		
		while( ali != NULL )
		{
			if( u ==  ali->usersession  )
			{
				if( ali == as->as_UserSessionList )
				{
					as->as_UserSessionList =(SASUList *)ali->node.mln_Succ;
				}
				else
				{
					prevali->node.mln_Succ = ali->node.mln_Succ;
				}
				
				as->as_UserNumber--;
				DEBUG("[AppSession] Session removed, sessions %d\n", as->as_UserNumber );
				
				FFree( ali );
			
				break;
			}
		
			prevali = ali;
			ali = (SASUList *) ali->node.mln_Succ;
		}
		
		pthread_mutex_unlock( &as->as_SessionsMut );
		as->as_Timer = time( NULL );
	}
	
	return -1;
}
Exemplo n.º 21
0
int UnMount( struct FHandler *s, void *f )
{
	if( f != NULL )
	{
		DEBUG("[fsysphp] Unmount filesystem\n");
		File *lf = (File*)f;
		
		if( lf->f_SpecialData )
		{
			SpecialData *sd = (SpecialData *)lf->f_SpecialData;
		
			char command[ 2048 ];	// maybe we should count that...

			sprintf( command, "php \"modules/system/module.php\" \"command=unmount&devname=%s&module=%s&sessionid=%s\";",
				lf->f_Name,  sd->module, lf->f_SessionID
			);
		
			int answerLength;
			ListString *result = PHPCall( command, &answerLength );
			if( result && result->ls_Size >= 0 )
			{
				if( strncmp( result->ls_Data, "fail", 4 ) == 0 )
				{
					DEBUG( "[fsysphp] Failed to unmount device %s..\n", lf->f_Name );
				}
			}
			else
			{
				DEBUG( "[fsysphp] Unknown error unmounting device %s..\n", lf->f_Name );
			}
		
			// we should parse result to get information about success
		
			if( result ) ListStringDelete( result );
			
			// Free up active device information
			if( sd->module ) FFree( sd->module );
			if( lf->f_SessionID ) FFree( lf->f_SessionID );
			if( sd->type ) FFree( sd->type );
			FFree( lf->f_SpecialData );
		}
		
		// Free up active device information
		if( lf->f_Name ){ FFree( lf->f_Name ); }
		if( lf->f_Path ){ FFree( lf->f_Path ); }

	}
	return 0;
}
void FriendCoreManagerDelete( FriendCoreManager *fcm )
{
	DEBUG("FriendCoreManager END - start\n");
	if( fcm != NULL )
	{
		
		DEBUG("FriendCoreManager: close event\n");
		if( fcm->fcm_EventManager != NULL )
		{
			EventManagerDelete( fcm->fcm_EventManager );
		}
		
		DEBUG("FriendCoreManager Shutdown\n");
		FriendCoreShutdown( fcm->fcm_FriendCores );
		DEBUG("FriendCoreManager shutdown finished\n");

		DEBUG("FriendCoreManager Close client\n");
		CommServiceDelete( fcm->fcm_CommServiceClient );
		DEBUG("FriendCoreManager Close server\n");
		CommServiceDelete( fcm->fcm_CommServiceServer );
		
		#ifdef ENABLE_WEBSOCKETS
		WebSocketFree( fcm->fcm_WebSocket );
		#endif
		
		DEBUG("FriendCoreManager Close services\n");
		if( fcm->fcm_ServiceManager != NULL )
		{
			ServiceManagerDelete( fcm->fcm_ServiceManager );
		}
		
		DEBUG("FriendCoreManager Close SSH Server\n");
		SSHServerDelete( fcm->fcm_SSHServer );
		
		FFree( fcm );
	}
	
	DEBUG("FriendCoreManager END - Reads: %d Writes: %d\n", _reads, _writes );
}
Exemplo n.º 23
0
int AppSessionSendOwnerMessage( AppSession *as, UserSession *sender, char *msg, int length )
{
	int msgsndsize = 0;
	if( as == NULL )
	{
		DEBUG("[AppSession] AppSession parameter is empty\n");
		return -1;
	}
	
	time_t ntime = time( NULL );
	DEBUG("[AppSession] OLD TIME %lld NEW TIME %lld\n", (long long)as->as_Timer, (long long)ntime );
	if( ( ntime - as->as_Timer ) > TIMEOUT_APP_SESSION )
	{
		as->as_Obsolete = TRUE;
	}
	as->as_Timer = ntime;

	DEBUG("[AppSession] Send message %s\n", msg );
	
	char *newmsg = NULL;
	
	if( ( newmsg = FCalloc( length+256, sizeof(char) ) ) != NULL )
	{
		User *usend = sender->us_User;
		DEBUG("[AppSession] AS POINTER %p SENDER %p\n", as, usend );
		int newmsgsize = sprintf( newmsg, WS_MESSAGE_TEMPLATE_USER, as->as_AuthID, as->as_SASID, usend->u_Name, msg );
		
		msgsndsize += WebSocketSendMessageInt( as->as_UserSessionList->usersession, newmsg, newmsgsize );
		DEBUG("[AppSession] FROM %s  TO %s  MESSAGE SIZE %d\n", usend->u_Name, as->as_UserSessionList->usersession->us_User->u_Name, msgsndsize );

		FFree( newmsg );
	}
	else
	{
		FERROR("Cannot allocate memory for message\n");
	}
	
	return msgsndsize;
}
Exemplo n.º 24
0
/**
 * Create new worker
 *
 * @param nr id of new worker
 * @return pointer to new Worker structure when success, otherwise NULL
 */
void WorkerDelete( Worker *w )
{
	if( w != NULL )
	{
		if( w->w_Thread )
		{
			int count = 0;
			w->w_Quit = TRUE;

			while( w->w_State != W_STATE_TO_REMOVE )
			{
				if( pthread_mutex_lock( &(w->w_Mut) ) == 0 )
				{
					pthread_cond_signal( &(w->w_Cond) ); // <- wake up!!
					pthread_mutex_unlock( &(w->w_Mut) );
				}
				
				DEBUG("[WorkerThread] State %d quit %d WRKID %d\n", w->w_State, w->w_Quit, w->w_Nr );
				if( count++ > 1 )
				{
					usleep( 10000 );
				}
			}
			ThreadDelete( w->w_Thread );
		}
		
		// Free up the mutex elements
		if( w->w_State )
		{
			pthread_cond_destroy( &(w->w_Cond) );
			pthread_mutex_destroy( &(w->w_Mut) );
		}
		
		DEBUG("[WorkerThread] Worker deleted: %d\n", w->w_Nr );
		FFree( w );
	}
}
Exemplo n.º 25
0
/**
 * Reload content from file
 *
 * @param file pointer to LocFile structure 
 * @param path pointer to path from which data will be reloaded
 * @return 0 when success, otherwise error number
 */
int LocFileReload( LocFile *file, char *path )
{
	DEBUG("File %s will be reloaded\n", path );
	
	if( file->lf_Buffer )
	{
		FFree( file->lf_Buffer );
		file->lf_Buffer = NULL;
	}
	
	FILE* fp = fopen( path, "rb" );
	if( fp == NULL )
	{
		FERROR("Cannot open file %s (file does not exist?)..\n", path );
		return -1;
	}
	
	struct stat st;
	if( stat( path, &st ) < 0 )
	{
		FERROR("Cannot run stat on file: %s!\n", path);
		return -2;
	}
	memcpy(  &(file->lf_Info),  &st, sizeof(stat) );
	
	fseek( fp, 0, SEEK_END );
	long fsize = ftell( fp );
	fseek( fp, 0, SEEK_SET );  //same as rewind(f);
	file->lf_FileSize = fsize;

	LocFileRead( file, fp, 0, file->lf_FileSize );
	
	fclose( fp );
	
	return 0;
}
Exemplo n.º 26
0
void FilesystemDelete( Filesystem *fs )
{
	if( fs->fs_Name != NULL )
	{
		FFree( fs->fs_Name );
	}
	
	if( fs->fs_Type != NULL )
	{
		FFree( fs->fs_Type );
	}

	if( fs->fs_ShortDescription != NULL )
	{
		FFree( fs->fs_ShortDescription );
	}
	
	if( fs->fs_Server )
	{
		FFree( fs->fs_Server );
	}

	if( fs->fs_Path != NULL )
	{
		FFree( fs->fs_Path );
	}
	
	if( fs->fs_Username != NULL )
	{
		FFree( fs->fs_Username );
	}
	
	if( fs->fs_Password != NULL )
	{
		FFree( fs->fs_Password );
	}
}
Exemplo n.º 27
0
/**
 * Create WebSocket structure
 *
 * @param sb pointer to SystemBase
 * @param port port on which WS will work
 * @param sslOn TRUE when WS must be secured through SSL, otherwise FALSE
 * @return pointer to new WebSocket structure, otherwise NULL
 */
WebSocket *WebSocketNew( void *sb,  int port, FBOOL sslOn )
{
	WebSocket *ws = NULL;
	SystemBase *lsb = (SystemBase *)sb;
	
	DEBUG1("[WS] New websocket\n");
	
	pthread_mutex_init( &WSThreadMutex, NULL );
	
	if( ( ws = FCalloc( 1, sizeof( WebSocket ) ) ) != NULL )
	{
		char *fhome = getenv( "FRIEND_HOME" );
		ws->ws_FCM = lsb->fcm;
		
		ws->ws_Port = port;
		ws->ws_UseSSL = sslOn;
		ws->ws_OldTime = 0;
		ws->ws_InterfaceName[ 0 ] = 0;
		memset( &(ws->ws_Info), 0, sizeof ws->ws_Info );
		ws->ws_Opts = 0;
		ws->ws_Interface = NULL;
		
		if( ws->ws_UseSSL == TRUE )
		{
			INFO("[WS] WebSocket: SSL Enabled\n");
			
			ws->ws_CertPath = lsb->RSA_SERVER_CERT;
			ws->ws_KeyPath = lsb->RSA_SERVER_KEY;
			
			DEBUG1("[WS] server cert %s keycert %s\n", ws->ws_CertPath, ws->ws_KeyPath );
		
			//sprintf( ws->ws_CertPath, "%s%s", fhome, "/libwebsockets-test-server.pem" );
			//sprintf( ws->ws_KeyPath, "%s%s", fhome, "/libwebsockets-test-server.key.pem" );
			//ws->ws_Opts |= LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS;
		}
		
		if( ws->ws_AllowNonSSL == TRUE )
		{
			 //ws->ws_Opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
		}
		
			/*
		case 'k':
			opts = LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK;
			break;
			*/
		ws->ws_Info.port = ws->ws_Port;
		ws->ws_Info.protocols = protocols;
		ws->ws_Info.iface = ws->ws_Interface;
		ws->ws_Info.gid = -1;
		ws->ws_Info.uid = -1;
		ws->ws_Info.extensions = NULL;
		ws->ws_Info.ssl_cert_filepath = ws->ws_CertPath;
		ws->ws_Info.ssl_private_key_filepath = ws->ws_KeyPath;
		ws->ws_Info.options = ws->ws_Opts;// | LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT;
		if( ws->ws_UseSSL == TRUE ) 
		{
			ws->ws_Info.options |= LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS|LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
		}
		
		ws->ws_Info.user = ws;
		
		//ws->ws_Info.extensions = lws_get_internal_extensions();
		//ws->ws_Info.extensions->per_context_private_data = ws;
		ws->ws_Info.ssl_cipher_list = "ECDHE-ECDSA-AES256-GCM-SHA384:"
			       "ECDHE-RSA-AES256-GCM-SHA384:"
			       "DHE-RSA-AES256-GCM-SHA384:"
			       "ECDHE-RSA-AES256-SHA384:"
			       "HIGH:!aNULL:!eNULL:!EXPORT:"
			       "!DES:!MD5:!PSK:!RC4:!HMAC_SHA1:"
			       "!SHA1:!DHE-RSA-AES128-GCM-SHA256:"
			       "!DHE-RSA-AES128-SHA256:"
			       "!AES128-GCM-SHA256:"
			       "!AES128-SHA256:"
			       "!DHE-RSA-AES256-SHA256:"
			       "!AES256-GCM-SHA384:"
			       "!AES256-SHA256";
		
		ws->ws_CountPollfds = 0;
		
		//ws->ws_Info.ka_time = 15;
		//lws_set_log_level( 0, lwsl_emit_syslog);
		
		ws->ws_Context = lws_create_context( &ws->ws_Info );
		if( ws->ws_Context == NULL )
		{
			FERROR( "Libwebsocket init failed, cannot create context\n" );
			FFree( ws );
			return NULL;
		}
		
		INFO("[WS] NEW Websockets ptr %p context %p\n", ws, ws->ws_Context);

		ws->ws_Buf[ LWS_SEND_BUFFER_PRE_PADDING ] = 'x';
			
	}
	else
	{
		FERROR("[WS] Cannot allocate memory for WebSocket\n");
	}
	
	DEBUG1("[WS] Websocket created\n");
	
	return ws;
}
Exemplo n.º 28
0
void *FileOpen( struct File *s, const char *path, char *mode )
{
	int spath = strlen( path );
	char *tmppath = FCalloc( spath+10, sizeof(char) );
	memcpy( tmppath, path, spath );
	
	File *locfil = NULL;
	DEBUG("File open\n");
	
	SpecialData *srd  = (SpecialData *)s->f_SpecialData;
	int error = 0;
	
	// we are takeing filename from path, path will be used to make directories only
	char *nameptr = (char *)path;
	int i;
	for( i=spath ; i >= 0 ; i-- )
	{
		if( path[ i ] == '/' )
		{
			nameptr = (char *)&path[i+1];
			tmppath[ i ] = 0;
			break;
		}
	}
	
	DEBUG("File open 1  path %s  tmppath %s  nameptr %s\n", path, tmppath, nameptr );
    
	INRAMFile *directory = NULL;
	
	if( strcmp( path, nameptr ) != 0 )
	{
		directory = INRAMFileMakedirPath( srd->root, tmppath, &error );
	}
	else
	{
		directory = srd->root;
	}
	
	if( directory != NULL )
	{
		DEBUG("Directory where file will be create/exist %s\n", directory->nf_Name );
		
		INRAMFile *nf = NULL;
		
		nf = INRAMFileGetChildByName( directory, nameptr );
		// read
		DEBUG("\nINRAM opened file for %s\n\n", mode );
		
		if( mode[ 0 ] == 'r' )
		{
			if( nf == NULL )
			{
				free( tmppath );
				FERROR("Cannot open file %s\n", path );
				return NULL;
			}
			nf->nf_Offset = 0;
		}
		else	// write
		{
			if( nf == NULL )
			{
				nf = INRAMFileNew( INRAM_FILE, tmppath, nameptr );
				if( INRAMFileAddChild( directory, nf ) == 0 )
				{
					
				}
			}
			else
			{
				
			}
		}
		
		// Ready the file structure
		if( ( locfil = calloc( sizeof( File ), 1 ) ) != NULL )
		{
			locfil->f_Path = StringDup( path );
			DEBUG("Fileopen, path duplicated %s\n", path );
			
			locfil->f_SpecialData = calloc( 1, sizeof( SpecialData ) );
			SpecialData *sd = (SpecialData *)locfil->f_SpecialData;
		
			if( sd )
			{
				sd->fp = nf;
			}
			DEBUG("\nOffset set to %ld\n\n", nf->nf_Offset );
			
			DEBUG("File open, descriptor returned\n");
			
			return locfil;
		}
	}

	FFree( tmppath );
	DEBUG("File open end\n");

	return NULL;
}
Exemplo n.º 29
0
int BufStringAdd( BufString *bs, const char *ntext )
{
	if( ntext == NULL )
	{
		return 1;
	}
	
	int addsize = strlen( ntext );//+1;
	if( bs->bs_Size == 0 )
	{
		// buffer is too small to handle data, we must extend it
		
		if( addsize > bs->bs_MAX_SIZE )
		{
			int allsize = ( (addsize / bs->bs_MAX_SIZE) + 1) * bs->bs_MAX_SIZE;
			char *tmp;
			
			if( ( tmp = FCalloc( allsize+1, sizeof(char) ) ) != NULL )
			{
				strcpy( tmp, ntext );
				bs->bs_Bufsize = allsize;
				bs->bs_Size = addsize;
			}
			else
			{
				ERROR("Cannot allocate memory for BUFString\n");
				return -1;
			}
		}
		else	// buffer is enough to hold data, just copy it
		{
			strcpy( bs->bs_Buffer, ntext );
			bs->bs_Size = strlen( ntext );
		}
		return 0;
	}
	
	//int modsize = (bs->bs_Size / bs->bs_MAX_SIZE) * bs->bs_MAX_SIZE;
	int newsize = (bs->bs_Size + addsize);
	
	if( newsize > bs->bs_Bufsize )
	{
		char *tmp;
		int allsize = ( (newsize / bs->bs_MAX_SIZE) + 1) * bs->bs_MAX_SIZE;
		
		if( ( tmp = FCalloc( allsize+1, sizeof(char) ) ) != NULL )
		{
			bs->bs_Bufsize = allsize;
			bs->bs_Size = newsize;
			
			strcpy( tmp, bs->bs_Buffer );
			strcat( tmp, ntext );
			
			FFree( bs->bs_Buffer );
			bs->bs_Buffer = tmp;
		}
		else
		{
			ERROR("Cannot allocate memory for buffer!\n");
			return -1;
		}
		// there is no space in the buffer, we must extend it
	}
	else
	{
		// there is some space in buffer, we can put information there
		strcat( bs->bs_Buffer, ntext );
		bs->bs_Size = newsize;
	}
	
	return 0;
}
Exemplo n.º 30
0
int BufStringAddSize( BufString *bs, const char *ntext, int len )
{
	if( ntext == NULL )
	{
		ERROR("Cannot add NULL text!\n");
		return 1;
	}
	
	if( bs->bs_Size == 0 )
	{
		// buffer is too small to handle data, we must extend it
		
		if( len > bs->bs_MAX_SIZE )
		{
			DEBUG( "Too big buffer! (len: %d, max: %d)\n", len, bs->bs_MAX_SIZE );
			int allsize = ( (len / bs->bs_MAX_SIZE) + 1) * bs->bs_MAX_SIZE;
			char *tmp;
			
			if( ( tmp = FCalloc( allsize + 10, sizeof(char) ) ) != NULL )
			{
				memcpy( tmp, ntext, len );
				bs->bs_Bufsize = allsize;
				bs->bs_Size = len;
				
				FFree( bs->bs_Buffer );
				bs->bs_Buffer = tmp;
			}
			else
			{
				ERROR("Cannot allocate memory for BUFString\n");
				return -1;
			}
		}
		else	// buffer is enough to hold data, just copy it
		{
			memcpy( bs->bs_Buffer, ntext, len );
			bs->bs_Size = len;
		}
		return 0;
	}
	
	int addsize = len;
//	int modsize = (bs->bs_Size / bs->bs_MAX_SIZE) * bs->bs_MAX_SIZE;
	int newsize = (bs->bs_Size + addsize);
	//DEBUG("Add memory for buffer   addsize %d modsize %d newsize %d current %d\n", addsize, modsize, newsize, bs->bs_Size );
	
	if( newsize > bs->bs_Bufsize )
	{
		char *tmp;
		int allsize = ( (newsize / bs->bs_MAX_SIZE) + 1) * bs->bs_MAX_SIZE;
		//DEBUG("Allocated mem size %d\n", allsize );
		
		if( ( tmp = FCalloc( allsize+10, sizeof(char) ) ) != NULL )
		{
			memcpy( tmp, bs->bs_Buffer, bs->bs_Size );
			//DEBUG("copy from %d len %d\n", bs->bs_Size, len );
			memcpy( &(tmp[ bs->bs_Size ]), ntext, len );
			
			bs->bs_Bufsize = allsize;
			bs->bs_Size = newsize;
			
			FFree( bs->bs_Buffer );
			bs->bs_Buffer = tmp;
		}
		else
		{
			ERROR("Cannot allocate memory for buffer!\n");
			return -1;
		}
		// there is no space in the buffer, we must extend it
	}
	else
	{
		// there is some space in buffer, we can put information there
		memcpy( &(bs->bs_Buffer[ bs->bs_Size ] ), ntext, len );
		bs->bs_Size = newsize;
	}
	
	return 0;
}