示例#1
0
int 
twrite(spdid_t spdid, td_t td, int cbid, int sz)
{
	struct connection *c = NULL;
	struct torrent *t;
	char *buf;
	int ret = -1;

	if (tor_isnull(td)) return -EINVAL;
	buf = cbuf2buf(cbid, sz);
	if (!buf) ERR_THROW(-EINVAL, done);

	LOCK();
	t = tor_lookup(td);
	if (!t) ERR_THROW(-EINVAL, unlock);
	if (!(t->flags & TOR_WRITE)) ERR_THROW(-EACCES, unlock);

	c = t->data;
	assert(c);

	lock_connection(c);
	UNLOCK();
	if (connection_parse_requests(c, buf, sz)) ERR_THROW(-EINVAL, release);
	unlock_connection(c);
	ret = sz;
done:
	return ret;
unlock:
	UNLOCK();
release:
	unlock_connection(c);
	goto done;
}
示例#2
0
int 
tread(spdid_t spdid, td_t td, int cbid, int sz)
{
	struct connection *c;
	struct torrent *t;
	char *buf;
	int ret;
	
	if (tor_isnull(td)) return -EINVAL;
	buf = cbuf2buf(cbid, sz);
	if (!buf) ERR_THROW(-EINVAL, done);

	LOCK();
	t = tor_lookup(td);
	if (!t) ERR_THROW(-EINVAL, unlock);
	assert(!tor_is_usrdef(td) || t->data);
	if (!(t->flags & TOR_READ)) ERR_THROW(-EACCES, unlock);
	c = t->data;

	lock_connection(c);
	UNLOCK();
	ret = connection_get_reply(c, buf, sz);
	unlock_connection(c);
done:	
	return ret;
unlock:
	UNLOCK();
	goto done;
}
示例#3
0
void
trelease(spdid_t spdid, td_t td)
{
	struct torrent *t;
	struct connection *c;

	if (!tor_is_usrdef(td)) return;

	LOCK();
	t = tor_lookup(td);
	if (!t) goto done;
	c = t->data;
	lock_connection(c);
	/* wait till others release the connection */
	unlock_connection(c);
	if (c) {
		http_free_connection(c);
		/* bookkeeping */
		http_conn_cnt++;
	}
	tor_free(t);
done:
	UNLOCK();
	return;
}
示例#4
0
/*接続*/
void connection_connect(CONNECTION_DATA* con){
	/*変数宣言*/
	TCPsocket *sock = &con->socket;
	char* str;
	int content_length = -1;
	int is_err = false;
	FILE* log_file;
	/*接続先IPを取得*/
	con->ip = SDLNet_TCP_GetPeerAddress(*sock);
	/*ビジー状態に設定*/
	if(!lock_connection(con)){
		SDLNet_TCP_Close(*sock);
		return;
	}
	/*ヘッダを判断する*/
	str = NetUtl_readLine(sock);
	if(str == null){
		/*通信終わり*/
		SDLNet_TCP_Close(*sock);
		/*接続終了フラグ*/
		unlock_connection(con);
		return;
	}
	if(strncmp(str,POST_HEADER,strlen(POST_HEADER)) != 0){
		is_err = true;
	}
	/*とりあえず最後まで受信する。*/
	if(is_err){//エラー
		/*ログに追加*/
		log_file = lock_log_file();
		time_output();
		ip_output(con->ip);
		fprintf(log_file,"%s\n",str);
		unlock_log_file();
		//最後まで受信するだけ
		while(*(str = NetUtl_readLine(sock)) != END_CHAR){
		}
	}else{//ヘッダを取得する
		while(*(str = NetUtl_readLine(sock)) != END_CHAR){
			if(content_length < 0){
				if(strncmp(	str,
							CONTENT_LENGTH_HEADER,
							strlen(CONTENT_LENGTH_HEADER)
						) == 0){
					sscanf(str,CONTENT_LENGTH_HEADER_F,&content_length);
				}
			}//else if(){}
		}
	}
	if(!is_err && content_length >= 0){/*とりあえず通信するに値する*/
		connection_do_request(con,content_length);
	}else{/*まったく関係ない*/
		connection_send_error(sock);
	}
	/*通信終わり*/
	SDLNet_TCP_Close(*sock);
	/*接続終了フラグ*/
	unlock_connection(con);
}
int push_connection(connection_t *connection, connection_t **connection_list) {
    int ctr = 1;
    connection_t *tmp;
    tmp = connection;

    /**
     * If connections wasn't initialized yet
     * connection became root of connections
     */
    if (*connection_list == NULL) {
        *connection_list = tmp;

        return ctr;
    }

    connection_t *current;
    current = *connection_list;

    /**
     * Find last connection in list
     */
    while (current->next != NULL) {
        current = current->next;
        ctr++;
    }

    /**
     * Set link between last connection
     * and the one we are trying to push
     */
    lock_connection(tmp);
    tmp->prev = current;
    unlock_connection(tmp);

    lock_connection(current);
    current->next = tmp;
    unlock_connection(current);

    return ctr;
}
int remove_connection(char *call_id, connection_t **connection_list) {
    connection_t *prev;
    connection_t *next;
    connection_t *connection = NULL;

    if (find_connection_by_call_id(call_id, &connection, connection_list) == 0) {
        prev = connection->prev;
        next = connection->next;

        if (prev == NULL && next == NULL) {
            *connection_list = NULL;
        } else if (prev == NULL) {
            *connection_list = next;
            lock_connection(next);
            next->prev = NULL;
            unlock_connection(next);
        } else if (next == NULL) {
            lock_connection(prev);
            prev->next = NULL;
            unlock_connection(prev);
        } else {
            lock_connection(prev);
            prev->next = next;
            unlock_connection(prev);

            lock_connection(next);
            next->prev = prev;
            unlock_connection(next);
        }

        destroy_connection(connection);

        return 0;
    }

    INFO("Connection with '%s' call id was not found.\n", call_id);
    return -1;
}
示例#7
0
int 
tread(spdid_t spdid, td_t td, int cbid, int sz)
{
	struct connection *c;
	struct torrent *t;
	char *buf;
	int ret;

	/* printc("connmgr reads https thd %d\n", cos_get_thd_id()); */
	if (tor_isnull(td)) return -EINVAL;
	buf = cbuf2buf(cbid, sz);
	if (!buf) ERR_THROW(-EINVAL, done);

	LOCK();
	t = tor_lookup(td);
	if (!t) ERR_THROW(-EINVAL, unlock);
	assert(!tor_is_usrdef(td) || t->data);
	if (!(t->flags & TOR_READ)) ERR_THROW(-EACCES, unlock);
	c = t->data;

	lock_connection(c);
	UNLOCK();

	/* // debug only */
	/* if (debug_buf && debug_amnt > 0) { */
	/* 	printc("use saved cbuf\n"); */
	/* 	memcpy(buf, debug_buf, sz); */
	/* 	ret = debug_amnt;		 */
	/* 	unlock_connection(c); */
	/* 	goto done; */
	/* } */

 	ret = connection_get_reply(c, buf, sz);

	/* // debug only */
	/* if (!debug_buf && debug_amnt == 0) { */
	/* 	if (!(debug_buf = cbuf_alloc(sz, &debug_cb))) BUG(); */
	/* 	printc("save the response cbuf\n"); */
	/* 	memcpy(debug_buf, buf, sz); */
	/* 	debug_amnt = ret; */
	/* } */

	unlock_connection(c);
done:	
	return ret;
unlock:
	UNLOCK();
	goto done;
}