Esempio n. 1
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;
}
Esempio n. 2
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;
}
Esempio n. 3
0
int
twmeta(spdid_t spdid, td_t td, const char *key, unsigned int klen, const char *val, unsigned int vlen)
{
	/* spdid is not used ? */

	struct torrent *t;

	LOCK();
	t = tor_lookup(td);
	if (!t) {UNLOCK(); return -1;}

	if (strlen(key) != klen) return -1;
	if (strlen(val) != vlen) return -1;

	if(strncmp(key, META_TD, klen) == 0) {
		t->td = atoi(val); // type of td need to be confirmed
	}
	else if(strncmp(key, META_OFFSET, klen) == 0) {
		t->offset = atoi(val);
	}
	else if(strncmp(key, META_FLAGS, klen) == 0) {
		t->flags = atoi(val); // type of flags need to be confirmed
	}
	else if(strncmp(key, META_EVTID, klen) == 0) {
		t->evtid = atol(val); // type need to be confirment
	}
	else { UNLOCK(); return -1;}

	UNLOCK();
	return 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;
}
Esempio n. 5
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;
}
Esempio n. 6
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;
}
Esempio n. 7
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;
}
Esempio n. 8
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;
}
Esempio n. 9
0
File: netif.c Progetto: 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;
}
Esempio n. 10
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;
}
Esempio n. 11
0
int
trmeta(spdid_t spdid, td_t td, const char *key, unsigned int klen, char *retval, unsigned int max_rval_len)
{
	/* spdid is not used ? */

	struct torrent *t;

	LOCK();
	t = tor_lookup(td);
	if (!t) {UNLOCK(); return -1;} // we need to have a unified return point which include an UNLOCK()

	if (strlen(key) != klen) return -1;

	if (strncmp(key, META_TD, klen) == 0) {
		sprintf(retval, "%d", t->td);
	}
	else if (strncmp(key, META_OFFSET, klen) == 0) {
		sprintf(retval, "%ld", (long)t->offset);
	}
	else if (strncmp(key, META_FLAGS, klen) == 0) {
		sprintf(retval, "%d", t->flags);
	}
	else if (strncmp(key, META_EVTID, klen) == 0) {
		sprintf(retval, "%ld", t->evtid);
	}
	else {UNLOCK(); return -1;}

	UNLOCK();
	if (strlen(retval) > max_rval_len) return -1;

	return strlen(retval);
}
Esempio n. 12
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;
}
Esempio n. 13
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;
}
Esempio n. 14
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;
}
Esempio n. 15
0
File: netif.c Progetto: songjiguo/C3
void
trelease(spdid_t spdid, td_t td)
{
	struct torrent *t;

	if (!tor_is_usrdef(td)) return;
	t = tor_lookup(td);
	if (!t) goto done;
	netif_event_release(cos_spd_id());
	tor_free(t);
done:
	return;
}
Esempio n. 16
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;
}
Esempio n. 17
0
void
trelease(spdid_t spdid, td_t td)
{
	struct torrent *t;
	td_t ntd;

	if (!tor_is_usrdef(td)) return;
	t = tor_lookup(td);
	if (!t) goto done;
	ntd = (td_t)t->data;
	parent_trelease(cos_spd_id(), ntd);
	tor_free(t);
done:
	return;
}
Esempio n. 18
0
void
trelease(spdid_t spdid, td_t td)
{
	struct torrent *t;

	if (!tor_is_usrdef(td)) return;

	LOCK();
	t = tor_lookup(td);
	if (!t) goto done;
	fsobj_release((struct fsobj *)t->data);
	tor_free(t);
done:
	UNLOCK();
	return;
}
Esempio n. 19
0
void
trelease(spdid_t spdid, td_t td)
{
	struct torrent *t;

	if (!tor_is_usrdef(td)) return;
	LOCK();
	t = tor_lookup(td);
	if (!t) goto done;
	assert(t->data);
	((struct channel_info *)t->data)->t = NULL;
	tor_free(t);
done:
	UNLOCK();
	return;
}
Esempio n. 20
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;
}
Esempio n. 21
0
int 
tmerge(spdid_t spdid, td_t td, td_t td_into, char *param, int len)
{
	struct torrent *t;
	int ret = 0;

	LOCK();
	if (!tor_is_usrdef(td)) ERR_THROW(-EINVAL, done);
	t = tor_lookup(td);
	if (!t) ERR_THROW(-EINVAL, done);
	/* currently only allow deletion */
	if (td_into != td_null) ERR_THROW(-EINVAL, done);
	assert(t->data);
	((struct channel_info *)t->data)->t = NULL;
	tor_free(t);
done:
	UNLOCK();
	return ret;
}
Esempio n. 22
0
void
trelease(spdid_t spdid, td_t td)
{
	struct torrent *t;
	net_connection_t nc;

	if (!tor_is_usrdef(td)) return;

	NET_LOCK_TAKE();
	t = tor_lookup(td);
	if (!t) goto done;
	nc = (net_connection_t)t->data;
	if (nc) net_close(spdid, nc);
	tor_free(t);
done:
	NET_LOCK_RELEASE();
	assert(lock_contested(&net_lock) != cos_get_thd_id());
	return;
}
Esempio n. 23
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;
}
Esempio n. 24
0
File: netif.c Progetto: 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;
}