Exemplo n.º 1
0
/**
 * Create new User Group
 *
 * @param id unique value which will point to UserGroup
 * @param name of new UserGroup
 * @param uid id of user assigned to group
 * @param type type of group as string
 * @return new UserGroup structure when success, otherwise NULL
 */
UserGroup *UserGroupNew( FULONG id, char *name, FULONG uid, char *type )
{
	UserGroup *ug = NULL;
	
	if( ( ug = FCalloc( 1, sizeof( UserGroup ) ) ) != NULL )
	{
		int len = strlen( name );
		
		ug->ug_ID = id;
		int len10 = len + 10;
		
		ug->ug_Name = FCalloc( len10, sizeof(char) );
		if( ug->ug_Name != NULL )
		{
			strncpy( ug->ug_Name, name, len );
		}
	
		ug->ug_UserID = uid;
	
		len = strlen( type );
		ug->ug_Type = FCalloc( len10, sizeof(char) );
		if( ug->ug_Type != NULL )
		{
			strncpy( ug->ug_Type, type, len );
		}
	}
	
	return ug;
}
Exemplo n.º 2
0
/**
 * Add a new event to the list of events to handle
 *
 * @param em pointer to the event manager structure
 * @param id ID of the event returned by EventGetNewID
 * @param thread pointer to the thread to send the message to
 * @param nextCall delay before sending the message
 * @param repeat number of repetitions
 * @return 0 when success, otherwise error number
 */
int EventAdd( EventManager *em, void *function, void *data, time_t nextCall, time_t deltaTime, int repeat )
{
	CoreEvent *nce = FCalloc( sizeof( CoreEvent ), 1 );
	if( nce != NULL )
	{
		//FThread *nth = ThreadNew( function, em->em_SB, FALSE );
		//CoreEvent *retEv = NULL;
		//nce->ce_Thread = thread;
		nce->ce_Function = function;
		nce->ce_ID = ++em->em_IDGenerator;
		nce->ce_Time = nextCall;
		nce->ce_RepeatTime = repeat;
		nce->ce_TimeDelta = deltaTime;
		nce->ce_Data = data;

		DEBUG("[EventManager] Add new event, ID: %lu\n", nce->ce_ID );

		nce->node.mln_Succ = (MinNode *) em->em_EventList;
		em->em_EventList = nce;
	}
	else
	{
		Log( FLOG_ERROR, "Cannot allocate memory for new Event\n");
		return -1;
	}

	return 0;
}
Exemplo n.º 3
0
/**
 * Check embedded headers in data
 *
 * @param data pointer to data where request is
 * @param dataLength length of source message
 * @param header pointer to http header which we want to get
 * @return pointer to content of requested header
 */
char *CheckEmbeddedHeaders( char *data, int dataLength, const char *header )
{
	// Setup the data - length minus end of headers
	int len = dataLength ? dataLength : (int)strlen( data ) - hsearchLene;
	if( len < 0 ) return NULL;
	
	int pos = FindEmbeddedHeaders( data, dataLength );
	
	// Error.. no headers
	if( pos == -1 ) return NULL;
	
	// Find the header and return the data
	unsigned int hlen = strlen( header );
	
	int i = pos; 
	for( ; i < len; i++ )
	{
		// Found our header
		if( strncmp( header, data + i, hlen ) == 0 )
		{
			i += hlen + 1;
			int mode = 0;
			int charPos = 0;
			int charLength = 0;
			int e = i;
			
			// Scan till newline to get our header value
			for( ; e < len; e++ )
			{
				if( mode == 0 && data[e] != ' ' )
				{
					mode = 1;
					charPos = e;
				}
				if( mode == 1 && data[e] != '\n' )
				{
					charLength++;
				}
				else if( mode == 1 && data[e] == '\n' )
				{
					break;
				}
			}
			
			if( charPos > 0 && charLength )
			{
				int block = charLength;
				char *value = FCalloc( block + 1, 1 );
				memcpy( value, data + charPos, block );
				return value;
			}
		}
		// Found header end
		if( strncmp( hsearche, data + i, hsearchLene ) == 0 ) 
		{
			return NULL;
		}
	}
	return NULL;
}
Exemplo n.º 4
0
char* session_id_generate(void){
	const unsigned int ENTROPY_SIZE = 256;

	unsigned char entropy[ENTROPY_SIZE];

	int status = my_getentropy(entropy, ENTROPY_SIZE);

	if (status != 0){
		return NULL;
	}

	char *hashed_string = FCalloc(2*SHA_DIGEST_LENGTH + 1, sizeof(char));
	if (hashed_string == NULL){
		return NULL;
	}

	unsigned char temp[SHA_DIGEST_LENGTH];
	if (SHA1(entropy, sizeof(entropy), temp) == NULL){
		return NULL;
	}

	char *c = hashed_string;
	for (unsigned int i = 0; i < SHA_DIGEST_LENGTH; i++){
		sprintf(c, "%02x", temp[i]);
		c += 2;
	}

	return hashed_string;
}
Exemplo n.º 5
0
void AddToList( List *list, void *data )
{
	// First data
	if( list->data == NULL )
	{
		list->data = data;
		list->next = NULL;
	}
	// More data
	else
	{
		List *tmp = list;
	
		// Go to end of list
		while( tmp->next )
		{
			tmp = tmp->next;
		}

		// Make new data
		tmp->next = FCalloc( 1, sizeof( List ) );
		if( tmp->next == NULL )
		{
			ERROR("Cannot allocate memory in Addtolist\n");
			return;
		}
	
		// add data and set terminator
		tmp->next->data = data;
		//tmp->next->next = NULL;
	}
}
Exemplo n.º 6
0
KeyValueList *KeyValueListNew( )
{
	KeyValueList *ne = FCalloc( 1, sizeof( KeyValueList ) );
	if( ne != NULL )
	{
	}
	return ne;
}
Exemplo n.º 7
0
BufString *BufStringNew()
{
	BufString *str = NULL;
		
	if( ( str = FCalloc( sizeof( BufString ), 1 ) ) != NULL )
	{
		str->bs_Size = 0;
		str->bs_Bufsize = BUF_STRING_MAX;
		str->bs_MAX_SIZE = BUF_STRING_MAX;
		
		str->bs_Buffer = FCalloc( str->bs_Bufsize+1, sizeof(char) );
		
		//printf("BufStr allocated\n");
		return str;
	}
		
	return NULL;
}
Exemplo n.º 8
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.º 9
0
KeyValueList *KeyValueListNewWithEntry( char *key, char *value )
{
	KeyValueList *ne = FCalloc( 1, sizeof( KeyValueList ) );
	if( ne != NULL )
	{
		ne->key = StringDuplicate( key );
		ne->value = StringDuplicate( value );
	}
	return ne;
}
Exemplo n.º 10
0
List* CreateList()
{
	List *l = FCalloc( 1, sizeof( List ) );
	if( l == NULL )
	{
		ERROR("Cannot allocate memory in CreateList\n");
		return NULL;
	}
	//l->data = NULL;
	//l->next = NULL;
	return l;
}
Exemplo n.º 11
0
bool PathHasColon( unsigned char *string )
{
	// No literal colon
	char *dec = FCalloc( 1, strlen( string ) + 1 );
	UrlDecode( dec, (const char *)string );
	DEBUG( "Decoded string for path: %s\n", dec );
	if( strchr( dec, ':' ) != NULL )
	{
		return TRUE;
	}
	return FALSE;
}
Exemplo n.º 12
0
Path* PathJoin( Path* path1, Path* path2 )
{
	unsigned int size = path1->rawSize + path2->rawSize + 1;
	char* newPath = FCalloc( (size + 1), sizeof(char) );
	memcpy( newPath, path1->raw, path1->rawSize );
	memcpy( newPath + path1->rawSize + 1, path2->raw, path2->rawSize );
	newPath[path1->rawSize] = '/'; // Doesn't matter if path1 already has this. It'll just be ignored, then :)
	newPath[size] = 0;
	Path* p = PathNew( newPath );
	free( newPath );
	return p;
}
Exemplo n.º 13
0
List* ListNew()
{
	List *l = FCalloc( 1, sizeof( List ) );
	if( l == NULL )
	{
		ERROR("Cannot allocate memory in ListNew\n");
		return NULL;
	}
	l->data = NULL;
	//l->next = NULL;
	return l;
}
Exemplo n.º 14
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.º 15
0
AppSession *AppSessionNew( void *sb, const char *authid, FUQUAD appid, UserSession *owner )
{
	AppSession *las = NULL;
	DEBUG("[AppSession] Create app session\n");
	
	if( ( las = FCalloc( 1, sizeof( AppSession ) ) ) != NULL )
	{
		strcpy( las->as_AuthID, authid );
		las->as_AppID = appid;
		
		DEBUG("[AppSession] AppSessionCreated, authid %s\n", authid );
		
		SASUList *ali =  FCalloc( 1, sizeof( SASUList ) );
		if( ali != NULL )
		{
			ali->ID = las->as_NumberGenerator++;
			ali->status = SASID_US_STATUS_NEW;
			ali->usersession = owner;
			strcpy( ali->authid, authid );
			DEBUG("[AppSession] ASN set %s pointer %p\n", ali->authid, ali );
			las->as_UserSessionList = ali;
			
			las->as_SASID = (FUQUAD)ali;//( rand() % ULLONG_MAX );
			las->as_Timer = time( NULL );
			
			las->as_UserNumber++;
			las->as_VariablesNumGenerator = 100;
			las->as_SB = sb;
			
			pthread_mutex_init( &las->as_SessionsMut, NULL );
			pthread_mutex_init( &las->as_VariablesMut, NULL );
		}
	}
	else
	{
		FERROR("Cannot allocate memory for AppSession\n");
	}
	
	return las;
}
Exemplo n.º 16
0
BOOL PathHasColon( char *string )
{
	// No literal colon
	int size = strlen( string ) + 1;
	char *dec = FCalloc( 1, size );
	UrlDecode( dec, (const char *)string );
	DEBUG( "[fsysphp] Decoded string for path: %s\n", dec );
	if( strchr( dec, ':' ) != NULL )
	{
		return TRUE;
	}
	return FALSE;
}
Exemplo n.º 17
0
void PathMake( Path* path )
{
	// Free the previous path, if any
	if( path->raw )
	{
		free( path->raw );
		path->raw = NULL;
	}

	// Count the path length
	unsigned int length = path->isAbsolute ? 1 : 0; // Absolute paths starts with /
	for( unsigned int i = 0; i < path->size; i++ )
	{
		length += strlen( path->parts[ i ] );
	}

	length += path->size - 1;     // All the /'s inbetween segments
	length += path->file ? 0 : 1; // Trailing / for directories

	path->raw = FCalloc( length + 1, sizeof( char ) );
	path->rawSize = length;
	path->raw[length] = '\0';

	// Create the path string
	char* raw = path->raw;

	if( path->isAbsolute )
	{
		*(raw++) = '/';
	}

	for( unsigned int i = 0; i < path->size; i++ )
	{
		unsigned int len = strlen( path->parts[i] );
		memcpy( raw, path->parts[i], len );
		raw += len;
		if( i < path->size - 1 )
		{
			*(raw++) = '/';
		}
	}

	// Don't create // if path is just / (has to be longer than 1 char)
	if( !path->file && length > 1 )
	{
		*(raw) = '/';
	}
}
Exemplo n.º 18
0
/**
 * Initialize authmodule
 *
 * @param l pointer to AuthMod
 * @param sb pointer to SystemBase
 * @return 0 when success, otherwise error number
 */
int libInit( AuthMod *l, void *sb )
{
	DEBUG("[FCDB] libinit\n");

	if( ( l->SpecialData = FCalloc( 1, sizeof( struct SpecialData ) ) ) == NULL )
	{
		FERROR("Cannot allocate memory for authmodule\n");
		return 1;
	}

	l->am_Name = LIB_NAME;
	l->am_Version = LIB_VERSION;
	l->sb = sb;

	return 0;
}
Exemplo n.º 19
0
// TODO: We already have StringDuplicate()
char* StringDup( const char* str )
{
	if( str == NULL)
	{
		DEBUG("[fsysphp] Cannot copy string!\n");
		return NULL;
	}
	
	int len = strlen( str );
	char *res = NULL;
	if( ( res = FCalloc( len + 1, sizeof(char) ) ) != NULL )
	{
		return strcpy( res, str );
	}
	return NULL;
}
Exemplo n.º 20
0
/**
 * Creates a new Event Manager structure and launches its thread
 *
 * @return pointer to the newly created event manager
 */
EventManager *EventManagerNew( void *sb )
{
	EventManager *em = FCalloc( sizeof( EventManager) , 1 );
	DEBUG("[EventManager] start\n");
	if( em != NULL )
	{
		em->lastID = 0xf;
		em->em_SB = sb;
		em->em_EventThread = ThreadNew( EventManagerLoopThread, em, TRUE, NULL );
	}
	else
	{
		Log( FLOG_FATAL, "Cannot allocate memory for EventManager!\n");
	}

	return em;
}
Exemplo n.º 21
0
BufString *Info( File *s, const char *path )
{
	DEBUG("Info!\n");
	
	BufString *bs = BufStringNew();
	int spath = strlen( path );
	int rspath = strlen( s->f_Path );
	
	DEBUG("Info!\n");
	
	// user is trying to get access to not his directory
	DEBUG("Check access for path '%s' in root path '%s'  name '%s'\n", path, s->f_Path, s->f_Name );
	
	int doub = strlen( s->f_Name );
	
	char *comm = NULL;
	
	if( ( comm = FCalloc( rspath + spath + 512, sizeof(char) ) ) != NULL )
	{
		strcpy( comm, s->f_Path );
		if( comm[ strlen( comm ) -1 ] != '/' )
		{
			strcat( comm, "/" );
		}
		strcat( comm, &(path[ doub+2 ]) );
		
		DEBUG("PATH created %s\n", comm );
	
		struct stat ls;
		
		if( stat( comm, &ls ) == 0 )
		{
			//FillStat( bs, &ls, s, comm );
		}
		else
		{
			BufStringAdd( bs, "{ \"ErrorMessage\": \"File or directory do not exist\"}" );
		}
		
		free( comm );
	}
	DEBUG("Info END\n");
	
	return bs;
}
Exemplo n.º 22
0
/**
 * Create new worker
 *
 * @param nr id of new worker
 * @return pointer to new Worker structure when success, otherwise NULL
 */
Worker *WorkerNew( int nr )
{
	Worker *wrk = ( Worker *)FCalloc( 1, sizeof( Worker ) );
	
	if( wrk != NULL )
	{
		DEBUG("[WorkerThread] Worker CREATED %d\n", nr );
		
		wrk->w_State = W_STATE_CREATED;
		wrk->w_Nr = nr;
		
		pthread_mutex_init( &(wrk->w_Mut), NULL );
		pthread_cond_init( &(wrk->w_Cond), NULL );
	}
	else
	{
		FERROR("Cannot allocate memory for worker\n");
		return NULL;
	}

	return wrk;
}
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 LocFile structure and read file data from BufString
 *
 * @param path pointer to char table with path
 * @param bs pointer to BufString with file data
 * @return pointer to new LocFile when success, otherwise NULL
 */
LocFile* LocFileNewFromBuf( char* path, BufString *bs )
{
	if( path == NULL )
	{
		FERROR("File path is null\n");
		return NULL;
	}
	if( bs == NULL )
	{
		FERROR("BufString is NULL\n" );
		return NULL;
	}
	
	LocFile* fo = (LocFile*) FCalloc( 1, sizeof(LocFile) );
	if( fo != NULL )
	{
		fo->lf_PathLength = strlen( path );
		fo->lf_Path = StringDuplicateN( path, fo->lf_PathLength );
		//fo->lf_Filename = StringDuplicateN( path, fo->lf_PathLength );//StringDuplicate( GetFileNamePtr( path, len ) );
		MURMURHASH3( fo->lf_Path, fo->lf_PathLength, fo->hash );
		
		DEBUG("PATH: %s \n", fo->lf_Path );

		fo->lf_FileSize = bs->bs_Size;
		
		if( ( fo->lf_Buffer = FMalloc( fo->lf_FileSize ) ) != NULL )
		{
			memcpy( fo->lf_Buffer, bs->bs_Buffer, fo->lf_FileSize );
		}
	}
	else
	{
		FERROR("Cannot allocate memory for LocFile\n");
	}
	
	return fo;
}
Exemplo n.º 25
0
/**
 * Read a block of memory from LocFile
 *
 * @param file pointer to LocFile from which data will be readed
 * @param offset number of bytes from which data will be readed
 * @param size number of bytes to read
 * @return number of bytes readed from file
 */
static inline int LocFileRead( LocFile* file, FILE *fp, long long offset, long long size )
{
	if( file == NULL )
	{
		FERROR("Cannot read file which doesnt exist!\n");
		return -1;
	}

	file->lf_Buffer = (char *)FCalloc( size + 1, sizeof( char ) );
	if( file->lf_Buffer == NULL )
	{
		DEBUG("Cannot allocate memory for file\n");
		return 0;
	}
	
	file->lf_FileSize = size;
	fseek( fp, offset, SEEK_SET );
	int result = fread( file->lf_Buffer, 1, size, fp );
	if( result < size )
	{
		return result; 
	}
	return 0;
}
Exemplo n.º 26
0
/**
 * Strip embedded headers from http request
 *
 * @param data pointer to 
 * @param dataLength length of source message
 * @return 0 if message was sent otherwise error number
 */
int StripEmbeddedHeaders( char **data, unsigned int dataLength )
{
	// Setup the data
	char *pdata = *data;
	
	int len = (int) ( dataLength ? dataLength : strlen( pdata ) ) - hsearchLene;
	int flen = (int) (dataLength ? dataLength : strlen( pdata ) );
	if( len < 0 ) return -2;
	
	int i = 0; for( ; i < len; i++ )
	{
		// Finding end
		if( strncmp( hsearche, pdata + i, hsearchLene ) == 0 )
		{
			int dataLength = flen - ( i + hsearchLene );
			
			char *result = FCalloc( dataLength + 1, 1 );
			memcpy( result, pdata + i + hsearchLene, dataLength );
			free( *data ); *data = result;
			return dataLength;
		}
	}
	return -1;
}
Exemplo n.º 27
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.º 28
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.º 29
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.º 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;
}