Exemplo n.º 1
0
int
tread(spdid_t spdid, td_t td, int cbid, int sz)
{
	net_connection_t nc;
	struct torrent *t;
	char *buf;
	int ret;
	
	buf = cbuf2buf(cbid, sz);
	if (!buf)           return -EINVAL;
	if (tor_isnull(td)) return -EINVAL;

	NET_LOCK_TAKE();
	t = tor_lookup(td);
	if (!t) ERR_THROW(-EINVAL, done);
	if (!(t->flags & TOR_READ)) ERR_THROW(-EACCES, done);

	assert(t->data);
	nc = (net_connection_t)t->data;

	ret = net_recv(spdid, nc, buf, sz);
done:
	NET_LOCK_RELEASE();
	assert(lock_contested(&net_lock) != cos_get_thd_id());
	return ret;
}
Exemplo n.º 2
0
td_t 
tsplit(spdid_t spdid, td_t td, char *param, 
       int len, tor_flags_t tflags, long evtid) 
{
	td_t ret = -1;
	struct torrent *t, *nt;
	struct fsobj *fso, *fsc, *parent; /* obj, child, and parent */
	char *subpath;

	if (tor_isnull(td)) return -EINVAL;
	LOCK();
	t = tor_lookup(td);
	if (!t) ERR_THROW(-EINVAL, done);
	fso = t->data;

	fsc = fsobj_path2obj(param, len, fso, &parent, &subpath);
	if (!fsc) return -ENOENT;

	fsobj_take(fsc);
	nt = tor_alloc(fsc, tflags);
	if (!nt) ERR_THROW(-ENOMEM, done);
	ret = nt->td;

	/* If we created the torrent, then trigger an event as we have data! */
	evt_trigger(cos_spd_id(), evtid);
done:
	UNLOCK();
	return ret;
}
Exemplo n.º 3
0
int 
tread(spdid_t spdid, td_t td, int cbid, int sz)
{
	int ret = -1, left;
	struct torrent *t;
	struct fsobj *fso;
	char *buf;

	if (tor_isnull(td)) return -EINVAL;

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

	fso = t->data;
	assert(fso->size <= fso->allocated);
	assert(t->offset <= fso->size);
	if (!fso->size) ERR_THROW(0, done);

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

	left = fso->size - t->offset;
	ret  = left > sz ? sz : left;

	assert(fso->data);
	memcpy(buf, fso->data + t->offset, ret);
	t->offset += ret;
	cbuf_free(cbid);
done:	
	UNLOCK();
	return ret;
}
Exemplo n.º 4
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;
}
Exemplo n.º 5
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;
}
Exemplo n.º 6
0
int 
twrite(spdid_t spdid, td_t td, int cbid, int sz)
{
	td_t ntd;
	struct torrent *t;
	char *buf, *nbuf;
	int ret = -1;
	cbuf_t ncbid;

	if (tor_isnull(td)) return -EINVAL;
	t = tor_lookup(td);
	if (!t) ERR_THROW(-EINVAL, done);
	if (!(t->flags & TOR_WRITE)) ERR_THROW(-EACCES, done);

	assert(t->data);
	ntd = (td_t)t->data;

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

	nbuf = cbuf_alloc(sz, &ncbid);
	assert(nbuf);
	memcpy(nbuf, buf, sz);
	ret = parent_twrite(cos_spd_id(), ntd, ncbid, sz);
	cbuf_free(ncbid);
done:
	return ret;
}
Exemplo n.º 7
0
Arquivo: netif.c Projeto: songjiguo/C3
int 
twrite(spdid_t spdid, td_t td, int cbid, int sz)
{
	struct torrent *t;
	char *buf;
	int ret = -1;

	if (tor_isnull(td)) return -EINVAL;
	t = tor_lookup(td);
	if (!t) ERR_THROW(-EINVAL, done);
	if (!(t->flags & TOR_WRITE)) ERR_THROW(-EACCES, done);

	buf = cbuf2buf(cbid, sz);
	if (!buf) ERR_THROW(-EINVAL, done);
	ret = netif_event_xmit(spdid, buf, sz);

	/* // debug only */
	/* cbuf_t debug_cb; */
	/* if (debug_first == 0) { */
	/* 	debug_first = 1; */
	/* 	if (!(debug_buf = cbuf_alloc(sz, &debug_cb))) BUG(); */
	/* 	memcpy(debug_buf, buf, sz); */
	/* 	debug_amnt = sz; */
	/* } */
done:
	return ret;
}
Exemplo n.º 8
0
int 
twrite(spdid_t spdid, td_t td, int cbid, int sz)
{
	int ret = -1;
	struct channel_info *channel;
	struct torrent *t;
	char *buf;

	if (tor_isnull(td)) return -EINVAL;

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

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

	channel = (struct channel_info*)t->data;
	ret = cringbuf_produce(&channels->rb, buf, sz);
	cos_trans_cntl(COS_TRANS_TRIGGER, 0, 0, 0);

	t->offset += ret;
done:	
	UNLOCK();
	return ret;
}
Exemplo n.º 9
0
int 
tread(spdid_t spdid, td_t td, int cbid, int sz)
{
	int ret = -1;
	struct channel_info *channel;
	struct torrent *t;
	char *buf;

	if (tor_isnull(td)) return -EINVAL;

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

	buf = cbuf2buf(cbid, sz);
	if (!buf) goto done;

	channel = (struct channel_info*)t->data;
	ret = cringbuf_consume(&channel->rb, buf, sz);
done:	
	UNLOCK();
	return ret;
}
Exemplo n.º 10
0
int 
tread(spdid_t spdid, td_t td, int cbid, int sz)
{
	td_t ntd;
	struct torrent *t;
	char *buf, *nbuf;
	int ret = -1;
	cbuf_t ncbid;

	if (tor_isnull(td)) return -EINVAL;
	t = tor_lookup(td);
	if (!t)                      ERR_THROW(-EINVAL, done);
	if (!(t->flags & TOR_WRITE)) ERR_THROW(-EACCES, done);

	assert(t->data);
	ntd = (td_t)t->data;

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

	nbuf = cbuf_alloc(sz, &ncbid);
	assert(nbuf);
	/* printc("tip_tif_tread (thd %d)\n", cos_get_thd_id()); */
	ret = server_tread(cos_spd_id(), ntd, ncbid, sz);
	if (ret < 0) goto free;
	/* ip_tread_cnt++; */
	memcpy(buf, nbuf, ret);
free:
	/* cbufp_deref(ncbid); */  // should keep this cbufp alive in netif for FT purpose?  Jiguo
	cbuf_free(ncbid);
done:
	return ret;
}
Exemplo n.º 11
0
td_t
tsplit(spdid_t spdid, td_t tid, char *param, int len,
       tor_flags_t tflags, long evtid)
{
	td_t ret = -EINVAL;
	struct torrent *t;
	net_connection_t nc = 0;
	int accept = 0;

	if (tor_isnull(tid)) return -EINVAL;

	NET_LOCK_TAKE();
	/* creating a new connection */
	if (tid == td_root || len == 0 || strstr(param, "accept")) {
		if (tid == td_root) { 	/* new connection */
			nc = net_create_tcp_connection(spdid, cos_get_thd_id(), evtid);
			if (nc <= 0) ERR_THROW(-ENOMEM, done);
		} else { /* len == 0 || strstr(param, "accept"), accept on connection */
			t = tor_lookup(tid);
			if (!t) goto done;
			nc = net_accept(spdid, (net_connection_t)t->data);
			if (nc == -EAGAIN) {
				/* printc("net accept return EAGAIN\n"); */
				ERR_THROW(-EAGAIN, done);
			}
			if (nc < 0) ERR_THROW(-EINVAL, done);
			if (0 < net_accept_data(spdid, nc, evtid)) BUG();
			accept = 1;
		}
		t = tor_alloc((void*)nc, tflags);
		if (!t) ERR_THROW(-ENOMEM, free);
		ret = t->td;
	} else { 		/* modifying an existing connection */
		t = tor_lookup(tid);
		if (!t) goto done;
		nc = (net_connection_t)t->data;
	}

	if (!accept && len != 0) {
		int r;

		NET_LOCK_RELEASE();
		r = modify_connection(spdid, nc, param, len);
		if (r < 0) ret = r;
		NET_LOCK_TAKE();
	}
done:
	NET_LOCK_RELEASE();
	assert(lock_contested(&net_lock) != cos_get_thd_id());
	return ret;
free:
	net_close(spdid, nc);
	goto done;
}
Exemplo n.º 12
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;
}
Exemplo n.º 13
0
int 
twrite(spdid_t spdid, td_t td, int cbid, int sz)
{
	td_t ntd;
	struct torrent *t;
	int ret = -1;

	if (tor_isnull(td)) return -EINVAL;
	t = tor_lookup(td);
	if (!t) ERR_THROW(-EINVAL, done);
	if (!(t->flags & TOR_WRITE)) ERR_THROW(-EACCES, done);

	assert(t->data);
	ntd = (td_t)t->data;
	ret = parent_twrite(cos_spd_id(), ntd, cbid, sz);
done:
	return ret;
}
Exemplo n.º 14
0
td_t 
tsplit(spdid_t spdid, td_t td, char *param, 
       int len, tor_flags_t tflags, long evtid) 
{
	td_t ret = -1;
	struct torrent *t, *nt;
	int channel, direction;

	LOCK();
	if (tor_isnull(td)) ERR_THROW(-EINVAL, done);
	t = tor_lookup(td);
	if (!t) ERR_THROW(-EINVAL, done);
	if (len > 1) ERR_THROW(-EINVAL, done);

	channel = (int)(*param - '0');
	if (channel > 9 || channel < 0) ERR_THROW(-EINVAL, done);
	if (!channels[channel].exists)  ERR_THROW(-ENOENT, done);

	nt = tor_alloc(&channels[channel], tflags);
	if (!nt) ERR_THROW(-ENOMEM, done);
	ret = nt->td;

	direction = channels[channel].direction;
	if (direction == COS_TRANS_DIR_LTOC) {
		if (tflags != TOR_READ)  ERR_THROW(-EINVAL, free);
		if (channels[channel].t) ERR_THROW(-EBUSY, free);
	}
	if (direction == COS_TRANS_DIR_CTOL && tflags != TOR_WRITE) ERR_THROW(-EINVAL, free);
	if (direction == COS_TRANS_DIR_LTOC) {
		nt->evtid = evtid;
		channels[channel].t = nt;
	} else {
		nt->evtid = 0;
	}
done:
	UNLOCK();
	return ret;
free:
	tor_free(nt);
	goto done;
}
Exemplo n.º 15
0
td_t 
tsplit(spdid_t spdid, td_t tid, char *param, int len, 
       tor_flags_t tflags, long evtid)
{
	td_t ret = -1;
	struct torrent *t;
	struct connection *c;

	if (tor_isnull(tid)) return -EINVAL;
	
	LOCK();
	c = http_new_connection(0, evtid);
	if (!c) ERR_THROW(-ENOMEM, err);
	/* ignore the param for now */
	t = tor_alloc(c, tflags);
	if (!t) ERR_THROW(-ENOMEM, free);
	c->conn_id = ret = t->td;
err:
	UNLOCK();
	return ret;
free:
	http_free_connection(c);
	goto err;
}
Exemplo n.º 16
0
Arquivo: netif.c Projeto: songjiguo/C3
int 
tread(spdid_t spdid, td_t td, int cbid, int sz)
{
	struct torrent *t;
	char *buf;
	int ret = -1;

	if (tor_isnull(td)) return -EINVAL;
	t = tor_lookup(td);
	if (!t)                      ERR_THROW(-EINVAL, done);
	if (!(t->flags & TOR_WRITE)) ERR_THROW(-EACCES, done);

	buf = cbuf2buf(cbid, sz);
	if (!buf) ERR_THROW(-EINVAL, done);
	ret = netif_event_wait(spdid, buf, sz);
	
	/* // debug ?  */
	/* if (debug_first == 1) { */
	/* 	ret = netif_event_xmit(spdid, debug_buf, debug_amnt); */
	/* } */

done:
	return ret;
}