コード例 #1
0
ファイル: tcp.c プロジェクト: torrentkino/tumbleweed
void tcp_worker( struct epoll_event *events, int nfds, int thrd_id ) {
	ITEM *listItem = NULL;
	int i;

	mutex_block( _main->work->mutex );
	_main->work->active++;
	mutex_unblock( _main->work->mutex );

	for( i=0; i<nfds; i++ ) {
		if( events[i].data.fd == _main->tcp->sockfd ) {
			tcp_newconn();
		} else {
			listItem = events[i].data.ptr;

			if( events[i].events & EPOLLIN ) {
				tcp_input( listItem );
			} else if( events[i].events & EPOLLOUT ) {
				tcp_output( listItem );
			}

			/* Close, Input or Output next? */
			tcp_gate( listItem );
		}
	}

	mutex_block( _main->work->mutex );
	_main->work->active--;
	mutex_unblock( _main->work->mutex );
}
コード例 #2
0
ファイル: tcp.c プロジェクト: wwttt2004/torrentkino
void *tcp_thread( void *arg ) {
	struct epoll_event events[CONF_EPOLL_MAX_EVENTS];
	int nfds;
	int id = 0;

	mutex_block( _main->work->mutex );
	id = _main->work->id++;
	mutex_unblock( _main->work->mutex );
	
	info( NULL, 0, "Thread[%i] - Max events: %i", id, CONF_EPOLL_MAX_EVENTS );

	for( ;; ) {
		nfds = epoll_wait( _main->tcp->epollfd, events, CONF_EPOLL_MAX_EVENTS, CONF_EPOLL_WAIT );

		if( status == RUMBLE && nfds == -1 ) {
			if( errno != EINTR ) {
				info( NULL, 500, "epoll_wait() failed" );
				fail( strerror( errno ) );
			}
		} else if( status == RUMBLE && nfds == 0 ) {
			/* Timeout wakeup */
			if( id == 0 ) {
				tcp_cron();
			}
		} else if( status == RUMBLE && nfds > 0 ) {
			tcp_worker( events, nfds, id );
		} else {
			/* Shutdown server */
			break;
		}
	}

	pthread_exit( NULL );
}
コード例 #3
0
ファイル: mime.c プロジェクト: wwttt2004/torrentkino
void mime_magic( char *filename, char *key ) {
	struct obj_mime *tuple = NULL;
	magic_t magic = NULL;
	const char *mime = NULL;

	if( ! file_isreg( filename) ) {
		return;
	}

	if( (magic = magic_open( MAGIC_ERROR|MAGIC_MIME)) == NULL ) {
		return;
	}
  
	if( magic_load( magic,NULL) != 0 ) {
		magic_close( magic );
		return;
	}

	if( (mime = magic_file( magic, filename)) == NULL ) {
		magic_close( magic );
		return;
	}
	
	/* Cache mime type in memory */
	mutex_block( _main->mime->mutex );
	tuple = mime_add( key, mime );
	mutex_unblock( _main->mime->mutex );
	
	/* Clear handle */
	magic_close( magic );

	info( NULL, 0, "New MIME: \"%s\" -> \"%s\"", tuple->key, tuple->val );
}
コード例 #4
0
ファイル: aufs.c プロジェクト: flylixiaolong/Learning
static int aufs_create_by_name(const char *name, modt_t mode,
		struct dentry *parent, struct dentry **dentry)
{
	int error=0;
	if(!parent)
	{
		if(aufs_mount && aufs_mount->mnt_sb)
		{
			parent = aufs_mount->mnt_sb->s_root;
		}
	}
	if(!parent)
	{
		printk("Ah! can not find a parent!\n");
		return -EFAULT;
	}
	*dentry = NULL;
	mutex_block(&parent->d_inode->i_mutex);
	*dentry = lookup_on_len(name, parent, strlen(name));
	if(!IS_ERR(dentry))
	{
		if((mode & S_IFMT)==S_IFDIR)
			error = aufs_mkdir(parent->d_inode, *dentry, mode);
		else
			error = aufs_create(parent->d_inode, *dentry, mode);
	}
	else
	{
		error = PTR_ERR(dentry);
	}
	mutex_unblock(&parent->d_inode->i_mutex);
	return error;
}
コード例 #5
0
ファイル: masala-web.c プロジェクト: houstar/masala
void web_lookup( CALLBACK *callback, void* ctx, UCHAR *id ) {
	IP *addr;

	/* Check my own DB for that node. */
	mutex_block( _main->p2p->mutex );
	addr = db_address( id );
	mutex_unblock( _main->p2p->mutex );

	if( addr != NULL ) {
		callback( ctx, id, (UCHAR *) &addr->sin6_addr );
		return;
	}

	/* Start find process */
	mutex_block( _main->p2p->mutex );
	lkp_put( id, callback, ctx );
	mutex_unblock( _main->p2p->mutex );
}
コード例 #6
0
ファイル: masala-nss.c プロジェクト: houstar/masala
void nss_lookup( int sockfd, IP *clientaddr, UCHAR *node_id ) {
	IP *node_addr;

	/* Check my own DB for that node. */
	mutex_block( _main->p2p->mutex );
	node_addr = db_address( node_id );
	mutex_unblock( _main->p2p->mutex );

	if( node_addr != NULL ) {
		nss_reply( sockfd, clientaddr, node_id, node_addr );
		return;
	}

	/* Start find process */
	mutex_block( _main->p2p->mutex );
	lkp_put( node_id, NULL, NULL );
	mutex_unblock( _main->p2p->mutex );
}
コード例 #7
0
ファイル: tcp.c プロジェクト: wwttt2004/torrentkino
void tcp_cron( void ) {
	mutex_block( _main->work->mutex );

	/* Do some jobs while no worker is running */
	if( _main->work->active == 0 ) {
		node_cleanup();
	}
	mutex_unblock( _main->work->mutex );
}
コード例 #8
0
ファイル: p2p.c プロジェクト: tempbottle/torrentkino
void p2p_decode(UCHAR * bencode, size_t bensize, IP * from)
{
	BEN *packet = NULL;
	BEN *y = NULL;

	/* Parse request */
	packet = ben_dec(bencode, bensize);
	if (packet == NULL) {
		info(_log, from, "Decoding UDP packet failed:");
		return;
	} else if (packet->t != BEN_DICT) {
		info(_log, from, "UDP packet is not a dictionary:");
		ben_free(packet);
		return;
	}

	/* Type of message */
	y = ben_dict_search_str(packet, "y");
	if (!ben_is_str(y) || ben_str_i(y) != 1) {
		info(_log, from, "Message type missing or broken:");
		ben_free(packet);
		return;
	}

	mutex_block(_main->work->mutex);

	switch (*y->v.s->s) {

	case 'q':
		p2p_request(packet, from);
		break;
	case 'r':
		p2p_reply(packet, from);
		break;
	case 'e':
		p2p_error(packet, from);
		break;
	default:
		info(_log, from, "Drop invalid message type '%c' from",
		     *y->v.s->s);
	}

	mutex_unblock(_main->work->mutex);

	ben_free(packet);
}
コード例 #9
0
ファイル: p2p.c プロジェクト: tempbottle/torrentkino
void p2p_parse(UCHAR * bencode, size_t bensize, IP * from)
{
	/* Tick Tock */
	mutex_block(_main->work->mutex);
	gettimeofday(&_main->p2p->time_now, NULL);
	mutex_unblock(_main->work->mutex);

	/* UDP packet too small */
	if (bensize < 1) {
		info(_log, from, "Zero size packet from");
		return;
	}

	/* Ignore link-local address */
	if (ip_is_linklocal(from)) {
		info(_log, from, "Drop LINK-LOCAL message from");
		return;
	}

	/* Validate bencode */
	if (!ben_validate(bencode, bensize)) {
		info(_log, from, "Received broken bencode from");
		return;
	}

	/* Encrypted message or plaintext message */
#ifdef POLARSSL
	if (_main->conf->bool_encryption && !ip_is_localhost(from)) {
		p2p_decrypt(bencode, bensize, from);
	} else {
		p2p_decode(bencode, bensize, from);
	}
#else
	p2p_decode(bencode, bensize, from);
#endif
}