Example #1
0
static netio_http_conn_t *
netio_http_conn_lock(netio_http_conn_t *conn)
{
	if (conns && conn) {
		ship_lock(conns);
		conn = ship_list_find(conns, conn);
		if (conn) {
			ship_lock(conn);
			ship_restrict_locks(conn, conns);
		}
		ship_unlock(conns);
		return conn;
	}
	return NULL;
}
Example #2
0
/* performs an HTTP post to the given URL, with callback */
int
netio_http_post_host(char *host, char *path, char *urlstr, char *content_type, char *data, int data_len,
		     void (*func) (char *url, int respcode, char *data, int data_len, void *pkg),
		     void *pkg) 
{
	addr_t addr;
	int ret = -1;
	struct sockaddr *sa = NULL;
	socklen_t sa_len;
	netio_http_conn_t *http_conn = NULL;

	ASSERT_ZERO(ident_addr_str_to_addr_lookup(host, &addr), err);
	addr.type = IPPROTO_TCP;
	if (addr.port < 1)
		addr.port = 80;
	
	ASSERT_ZERO(ident_addr_addr_to_sa(&addr, &sa, &sa_len), err);
	
	/* new managed api: */
	if ((http_conn = netio_http_wait_new(&addr, urlstr, host, path, content_type, data, data_len, func, pkg))) {
		ship_lock(conns);
		http_conn->socket = netio_man_connto(sa, sa_len, http_conn, __netio_http_cb_conn, __netio_http_cb_data);
		if (http_conn->socket != -1) {
			netio_http_track_conn(http_conn, http_conn->socket);
			ret = 0;
			ship_unlock(http_conn);
		}
		ship_unlock(conns);
	}
err:
	freez(sa);
	return ret;
}
Example #3
0
/* dumps the current stats to json format .. */
void
stats_dump_json(char **str)
{
	void *ptr = 0;
	char *buf = 0;
	int buflen = 0, datalen = 0;
	char *tmp = 0;
	pdd_stat_t *stat = 0;
	
	ship_lock(done_stats);
	ASSERT_TRUE(buf = append_str("var p2pship_pdds = [\n", buf, &buflen, &datalen), err);
	while ((stat = ship_list_next(done_stats, &ptr))) {
		int len = strlen(stat->from) + strlen(stat->to) + strlen(stat->msg_type) + 128;
		
		ASSERT_TRUE(tmp = mallocz(len), err);
		sprintf(tmp, " [ \"%s\", \"%s\", \"%s\", \"%d\", \"%u\", \"%u\", \"%u\", \"%u\", \"%u\" ],\n",
			stat->from, stat->to, stat->msg_type, (int)stat->created, 
			(unsigned int)(stat->end - stat->start), (unsigned int)(stat->lookup_done - stat->lookup_start), 
			(unsigned int)(stat->connect_done - stat->connect_start),
			(unsigned int)(stat->connect_done? stat->sip_sent - stat->connect_done : 0),
			(unsigned int)(stat->remote_done - stat->remote_start));
		ASSERT_TRUE(buf = append_str(tmp, buf, &buflen, &datalen), err);
		freez(tmp);
	}
	ASSERT_TRUE(replace_end(buf, &buflen, &datalen, ",\n", "\n"), err);
	ASSERT_TRUE(buf = append_str("];\n", buf, &buflen, &datalen), err);
	*str = buf;
	buf = 0;
 err:
	ship_unlock(done_stats);
	freez(buf);
	freez(tmp);
}
Example #4
0
void
ac_packetfilter_stats_remote_event(char *local_aor, char *remote_aor, char *callid, unsigned long time, char *event)
{
	pdd_stat_t *stat;
	LOG_DEBUG("got remote event '%s' for %s => %s at %u\n", event, local_aor, remote_aor, time);
	
	/* find the relevant ones .. */
	ship_lock(stats);
	if ((stat = ship_ht_get_string(stats, callid)) ||
	    (stat = ship_ht_get_string(done_stats_ht, callid))) {
		
		unsigned long *val = 0;
		if (!strcmp(event, "remote_start"))
			val = &stat->remote_start;
		if (!strcmp(event, "remote_end"))
			val = &stat->remote_done;
		
		if (val && !(*val))
			*val = time;
		LOG_DEBUG("stats recorded for call (%s)\n", callid);
	} else {
		LOG_WARN("get stats for unknown call (%s)!\n", callid);
	}
	ship_unlock(stats);
}
Example #5
0
void
ac_packetfilter_stats_event(char *local_aor, char *remote_aor, char *event)
{
	unsigned long time = ship_systemtimemillis();
	void *ptr = 0;
	pdd_stat_t *stat;
	LOG_DEBUG("got event '%s' for %s => %s at %u\n", event, local_aor, remote_aor, time);
	
	/* find the relevant ones .. */
	if (!stats || !remote_aor)
		return;

	ship_lock(stats);
	while ((stat = ship_ht_next(stats, &ptr))) {
		if ((!local_aor || !strcmp(stat->from, local_aor)) &&
		    !strcmp(remote_aor, stat->to)) {
			unsigned long *val = 0;
			if (!strcmp(event, "lookup_start"))
				val = &stat->lookup_start;
			if (!strcmp(event, "lookup_end"))
				val = &stat->lookup_done;
			if (!strcmp(event, "conn_start"))
				val = &stat->connect_start;
			if (!strcmp(event, "conn_end"))
				val = &stat->connect_done;
			if (!strcmp(event, "sip_sent"))
				val = &stat->sip_sent;
			
			if (val && !(*val))
				*val = time;
		}
	}
	ship_unlock(stats);
}
Example #6
0
/* retrieve a list of contacts from the address book */
static int
addrbook_libebook_retrieve(ship_list_t *list)
{
	int ret = -1;
	contact_t *ct = 0;
	GError *error = 0;
	GList *contacts = 0, *loop;
	EBookQuery *query = 0;

	ship_lock(addrbook_lock);
	ASSERT_TRUE(query = e_book_query_any_field_contains(""), err);
	ASSERT_TRUE(e_book_get_contacts(book, query, &contacts, &error), err);
	ASSERT_ZERO(error, err);

	for (loop = contacts; loop; loop = g_list_next(loop)) {
		EContact *c = loop->data;
		char *name = 0;
		
		name = e_contact_get(c, E_CONTACT_OSSO_CONTACT_STATE);
		if (!name || strcmp(name, "DELETED")) {
			char **arrs = 0;

			ASSERT_TRUE(ct = ident_contact_new(), cerr);
			ASSERT_TRUE(ct->name = e_contact_get(c, E_CONTACT_FULL_NAME), cerr);
			
			ASSERT_TRUE(arrs = e_contact_get(c, E_CONTACT_SIP), cerr);
			ASSERT_TRUE(ct->sip_aor = addrbook_normalize_aor(arrs[0]), cerr);
			
			/* apparently arrs doesn't need to be free'd afterwards */
			g_list_foreach((GList*)arrs, (GFunc)g_free, NULL);
			ship_list_add(list, ct);
			ct = 0;
		cerr:
			ident_contact_free(ct);
		}
		if (name)
			g_free(name);
	}
	ret = 0;
 err:
	if (contacts) {
		g_list_free(contacts);
	}
	
	if (query) {
		e_book_query_unref(query);
	}

	if (error) {
		LOG_ERROR("Error getting contacts: %s\n", error->message);
		g_error_free(error);
	}
	ship_unlock(addrbook_lock);

	return ret;
}
Example #7
0
/* puts another piece into the orderer */
void
netio_http_packet_orderer_put(char *tracking_id, int piece, char *content, int len)
{
	ship_ht_t *l = 0;
	ship_lock(orderer);
	if ((l = ship_ht_get_string(orderer, tracking_id))) {
		ship_ht_put_int(l, piece, ship_lenbuf_create_copy(content, len));
	}
	ship_unlock(orderer);
}
Example #8
0
/* finds by callid */
sipp_media_proxy_t *
sipp_mp_find_by_callid(char *callid)
{
        sipp_media_proxy_t *mp = NULL;
        ship_lock(sipp_mps); {
                void *ptr = NULL;
                while (!mp && (mp = ship_list_next(sipp_mps, &ptr)))
                        if (strcmp(mp->callid, callid))
                                mp = NULL;
        } ship_unlock(sipp_mps);
        return mp;
}
Example #9
0
void
ac_close()
{
#ifdef AC_HTTP_PF
	freez(http_ac);
#endif
#ifdef DO_STATS
	if (stats) {
		ship_lock(stats);
		while (ship_list_first(stats)) {
			pdd_free_stat(ship_ht_pop(stats));
		}
		ship_ht_free(stats);
		stats = 0;
	}

	if (done_stats) {
		ship_lock(done_stats);
		while (ship_list_first(done_stats)) {
			pdd_free_stat(ship_list_pop(done_stats));
		}
		ship_list_free(done_stats);
		done_stats = 0;
	}
	ship_ht_free(done_stats_ht);
#endif
	
	if (white_list) {
		ship_ht_free(white_list);
		white_list = 0;
	}

	if (black_list) {
		ship_ht_free(black_list);
		black_list = 0;
	}

	ship_obj_list_free(filters);
	filters = NULL;
}
Example #10
0
/* calls to route traffic */
int
sipp_mp_route(char *source_aor, char *target_aor,
              char *source_addr, int source_port,
              char *target_addr, int target_port,
              char *callid,
              char *data, int datalen)
{
	int ret = -1;
	struct sockaddr *sa = 0;
	socklen_t sa_len;
	addr_t addr;
	addr_t *addrptr = 0;
	
	/* todo: we should somehow check whether the remote peer is
	   authorized to send to the given address packets */
	
	/* no accesscontrol here for now .. we can spam just about any
	   machine in our intranet through this tunnel stuff */
	
	/* we should search if we have a mediaproxy that listens to
	   the address that this one sends to */

	/* todo: this should sync around the mp after getting finding it */
        sipp_media_proxy_t *mp = NULL;
        ship_lock(sipp_mps); {
                void *ptr = NULL;
                while (!mp && (mp = ship_list_next(sipp_mps, &ptr))) {
			if (strcmp(mp->local_addr.addr, target_addr) || 
			    mp->local_addr.port != target_port)
                                mp = NULL;
		}
        } ship_unlock(sipp_mps);

	if (mp) {
		addrptr = &(mp->remote_addr);
	} else if (!ident_addr_str_to_addr(target_addr, &addr)) {
		addr.port = target_port;
		addrptr = &addr;
	}
	
	if (addrptr && !ident_addr_addr_to_sa(addrptr, &sa, &sa_len)) {			
		ret = netio_packet_anon_send(data, datalen, sa, sa_len);
		if (ret != -1)
			ret = 0;
	}
	freez(sa);

	if (ret) {
		LOG_WARN("invalid mediaproxy packet got\n");
	}
        return ret;
}
Example #11
0
/* finds by target */
sipp_media_proxy_t *
sipp_mp_find_by_source(char *addr, int port)
{
        sipp_media_proxy_t *mp = NULL;
        ship_lock(sipp_mps); {
                void *ptr = NULL;
                while (!mp && (mp = ship_list_next(sipp_mps, &ptr)))
                        if (mp->remote_addr.port != port || !strcmp(addr, mp->remote_addr.addr))
                                mp = NULL;
        } ship_unlock(sipp_mps);

        return mp;
}
Example #12
0
/* finds by socket */
sipp_media_proxy_t *
sipp_mp_find_by_socket(int socket)
{
        sipp_media_proxy_t *mp = NULL;
        ship_lock(sipp_mps); {
                void *ptr = NULL;
                while (!mp && (mp = ship_list_next(sipp_mps, &ptr)))
                        if (mp->socket != socket)
                                mp = NULL;
        } ship_unlock(sipp_mps);

        return mp;
}
Example #13
0
netio_http_conn_t *
netio_http_get_conn_by_id(const char *id)
{
	netio_http_conn_t *conn = 0;
	if (!conns)
		return NULL;
	ship_lock(conns); {
		if ((conn = ship_ht_get_string(conns_ht, id))) {
			conn = netio_http_conn_lock(conn);
		}
	} ship_unlock(conns);
	return conn;
}
Example #14
0
/* removes the orderer state for the given id */
void
netio_http_packet_orderer_close(char *tracking_id)
{
	ship_ht_t *l = 0;
	ship_lock(orderer);
	if ((l = ship_ht_remove_string(orderer, tracking_id))) {
		/* free up the stuff */
		ship_ht_remove_string(l, "next");
		ship_ht_empty_free_with(l, (void (*) (void *))ship_lenbuf_free);
		ship_ht_free(l);
	}
	ship_unlock(orderer);
}
Example #15
0
/* creates */
void
netio_http_packet_orderer_create(char *tracking_id)
{
	ship_ht_t *l = 0;
	ship_lock(orderer);
	if ((l = ship_ht_new())) {
		/* this is really ugly, using the ht to keep
		   netio_httptrack of the next element .. */
		ship_ht_put_string(l, "next", 0);
		ship_ht_put_string(orderer, tracking_id, l);
	}
	ship_unlock(orderer);
}
Example #16
0
void
netio_http_conn_close(netio_http_conn_t *conn)
{
	if (conn && conns && conn->added) {
		ship_unlock(conn);
		ship_lock(conns);
		conn = netio_http_conn_lock(conn);
	
		if (conn) {
			conn->added = 0;
			ship_ht_remove_string(conns_ht, conn->tracking_id);
			netio_http_packet_orderer_close(conn->tracking_id);
			conn = _ship_list_remove(0, conns, conn);
			ship_unlock(conn);
		}
		ship_unlock(conns);
	}

	if (conn) {
#ifdef CONFIG_WEBCACHE_ENABLED
		webcache_close_trackers(conn->tracking_id);
#endif

		// todo: if we don't OWN the socket, do not close it!!!
		// -> the ownership might have been moved to some other http_conn!
		if (conn->owns_socket && conn->socket != -1) {
			netio_man_close_socket(conn->socket);
			conn->socket = -1;
		}

		if (conn->forward_socket != -1) {
			netio_man_close_socket(conn->forward_socket);
			conn->forward_socket = -1;
		}

		if (conn->func) {
			conn->func(conn->fullurl, -1, NULL, -1, conn->pkg);
			conn->func = 0;
		}

		freez(conn->service_forward_to);
		freez(conn->service_forward_from);

		netio_conn_reset(conn);
		ship_ht_free(conn->params);
		ship_ht_free(conn->headers);		
		ship_lock_free(&conn->lock);
		freez(conn);
	}
}
Example #17
0
sipp_media_proxy_t *
sipp_mp_find(char *callid, addr_t *target_addr, int sendby)
{
	sipp_media_proxy_t * mp = 0;
        ship_lock(sipp_mps); {
                void *ptr = NULL;
                
                while (!mp && (mp = ship_list_next(sipp_mps, &ptr))) {
                        if (strcmp(mp->callid, callid) ||
			    mp->sendby != sendby ||
			    ident_addr_cmp(target_addr, &(mp->remote_addr))) {
				mp = 0;
			}
		}
	} ship_unlock(sipp_mps);

	return mp;
}
Example #18
0
static void
netio_http_track_conn(netio_http_conn_t *ret, int socket)
{
	if (socket != -1) {
		ship_lock(conns); {
			ship_list_add(conns, ret);
			ret->added = 1;
			netio_http_conn_lock(ret);
			
			/* create some sort of unique id for this connection */
			do {
				sprintf(ret->tracking_id, "http_conn:%d", rand());
			} while (ship_ht_get_string(conns_ht, ret->tracking_id));
			
			ship_ht_put_string(conns_ht, ret->tracking_id, ret);
		}
		ship_unlock(conns);
	}
}
Example #19
0
static netio_http_conn_t *
netio_http_get_conn_by_socket(int s, const int must_own)
{
	netio_http_conn_t *conn = 0;
	void *ptr = 0;
	if (!conns)
		return NULL;
	ship_lock(conns); {
		while (!conn && (conn = ship_list_next(conns, &ptr))) {
			// todo: check that it also owns the socket!
			if ((must_own && !conn->owns_socket) || conn->socket != s)
				conn = 0;
			else {
				conn = netio_http_conn_lock(conn);
			}
		}
	} ship_unlock(conns);
	return conn;
}
Example #20
0
/* cleans up all proxies for the given call */
void 
sipp_mp_clean_by_call(char* callid)
{
        ship_lock(sipp_mps); {
                void *ptr = NULL;
                void *prev = NULL;
                sipp_media_proxy_t *mp;
                
                while ((mp = ship_list_next(sipp_mps, &ptr))) {
                        if (!strcmp(mp->callid, callid)) {
                                ship_list_remove(sipp_mps, mp);
                                sipp_mp_close(mp);
                                ptr = prev;
                        } else {
                                prev = ptr;
                        }
                }
        } ship_unlock(sipp_mps);
}
Example #21
0
static int
sipp_mp_supports_fragmentation(char *remote_aor, addr_t *addr)
{
	int ret = 0;
	char *line, *tmp;
	void *ptr = 0;

	if (!(tmp = sipp_mp_create_mp_info_str(remote_aor, addr)))
		return 0;

	ship_lock(mp_infos);
	while (!ret && (line = ship_list_next(mp_infos, &ptr))) {
		if (!strcmp(line, tmp)) {
			ret = 1;
		}
	}
	ship_unlock(mp_infos);
	freez(tmp);
	return ret;
}
Example #22
0
/* does some periodic cleanups of dead proxies */
static void
sipp_mp_autoclean()
{
        void *ptr = NULL, *prev = NULL;
        sipp_media_proxy_t *mp;
	time_t now;
	time(&now);
	
        ship_lock(sipp_mps);
		
	while ((mp = ship_list_next(sipp_mps, &ptr))) {
		/* clean up dead ones */
		if ((now - mp->last) > MP_DEAD_TO) {
			ship_list_remove(sipp_mps, mp);
			sipp_mp_close(mp);
			ptr = prev;
		} else
			prev = ptr;
	}
        ship_unlock(sipp_mps);
}
Example #23
0
/* gets teh next piece, IF available. returns 0 if all ok (piece
   available & returned) */
int
netio_http_packet_orderer_pop_next(char *tracking_id, int *piece, char **content, int *len)
{
	int ret = -1;
	ship_ht_t *l = 0;
	ship_lock(orderer);
	if ((l = ship_ht_get_string(orderer, tracking_id))) {
		ship_lenbuf_t *val = 0;
		int next = (int)ship_ht_get_string(l, "next");
		if ((val = ship_ht_remove_int(l, next))) {
			(*content) = val->data;
			(*len) = val->len;
			val->data = 0;
			if (*content)
				ret = 0;
			ship_lenbuf_free(val);
			ship_ht_put_string(l, "next", (void*)(next+1));
		}
	}
	ship_unlock(orderer);
	return ret;
}
Example #24
0
/* loads up all the contacts we already have imported */
int
addrbook_load_imported(ship_list_t *list)
{
	int ret = -1;
	struct stat sdata;
	FILE *f = 0;
	
	if (stat(contacts_file, &sdata)) {
		LOG_WARN("Contacts log file %s does not exist\n", contacts_file);
		return -2;
	}
		
	/* load file .. */
	ship_lock(addrbook_lock);
	f = fopen(contacts_file, "r");
	if (f) {
		char *buf = NULL;
		size_t len = 0;
		ssize_t got = 0;
		while ((got = getline(&buf, &len, f)) > -1) {
			/* check what we've got.. */
			buf = trim(buf);
			switch (*buf) {
			case '#':
			case ';':
			case '/':
			case 0:
				/* skip empty / comments */
				break;
			default: {
				char **tokens = 0;
				int toklen = 0;
				int ct = 0;
				if (!ship_tokenize(buf, strlen(buf), &tokens, &toklen, ',')) {
					contact_t *c = ident_contact_new();
					if (c && toklen > 3) {
						ship_urldecode(tokens[0]);
						ship_urldecode(tokens[1]);
						ship_urldecode(tokens[2]);

						ASSERT_TRUE(c->sip_aor = strdup(tokens[0]), cerr);
						ASSERT_TRUE(c->name = strdup(tokens[1]), cerr);
						ASSERT_TRUE(c->db_id = strdup(tokens[2]), cerr);
						c->added = atoi(tokens[3]);

						/* todo: load the optional stuff */
						for (ct = 4; ct < toklen; ct++) {
							char *p = strchr(tokens[ct], '=');
							if (p) {
								p[0] = 0;
								p++;

								ship_urldecode(tokens[ct]);
								ship_urldecode(p);
								
								ship_ht_put_string(c->params, tokens[ct], strdup(p));
							}
						}

						LOG_DEBUG("Loaded %s <%s>\n", c->name, c->sip_aor);
						ship_list_add(list, c);
						c = 0;
					}
				cerr:
					ship_tokens_free(tokens, toklen);
					ident_contact_free(c);
				}
			} break;
			}
		}
		fclose(f);
		freez(buf);
		ret = 0;
	} else {
		USER_ERROR("Failed to open contacts file %s\n", contacts_file);
	}
	
	LOG_DEBUG("Loaded %d entries\n", ship_list_length(list));
	ship_unlock(addrbook_lock);
	return ret;
}
Example #25
0
/* Task objects cleanup */
void hit_task_cleanup_objects(){
	if(hit_task_mutex_created){
		hit_task_mutex_created = 0;
		rt_mutex_delete(&hit_task_mutex);
	}
}

void hit_task_init(){
	int i;
	for(i = 0; i < NB_WEAPONS; i++){
		if(weapons[i].weapon_type == GUN)
			weapons[i].timing_charge.now = weapons[i].timing_charge.max;
		else
			weapons[i].timing_charge.now = 0;

		weapons[i].timing_charge.last = 0;
		weapons[i].timing_charge.time_current = 0;

		weapons[i].timing_led.now = 0;
	}

	for(i = 0; i < NB_MAX_BULLETS; i++){
		bullets[i].weapon = NULL;
	}
	for(i = 0; i < NB_MAX_BOMBS; i++){
		bombs[i].weapon = NULL;
	}
}

//!*****************HIT TASK***********************/
void hit_task(void *cookie){

	invader_t *invader;
	bullet_t *bullet;
	int i, j;
	int16_t y;
	uint8_t removed; //TODO remove this and try
	uint8_t impact = 0; //!flag de collision pour la tache hit

	(void)cookie;
	//! On définit la période de la tache
	rt_task_set_periodic(NULL, TM_NOW, 50*MS);

	//! init
	hit_task_init();


	for (;;) {
		rt_task_wait_period(NULL);

		if(!game_break){
			//! On verrouille les bullets
			hit_lock();
			//! On verrouille le vaisseau
			ship_lock();
			//! On verrouille les invaders
			invaders_lock();

			//pour chaque bullet on va tester les collisions
			for (i=0;i<NB_MAX_BULLETS;i++){
				removed = 0;
				if(bullets[i].weapon != NULL){
					impact = 0;

					//!current object
					bullet = &bullets[i];

					// On déplace la bullet
					bullet->hitbox.y -= bullet->weapon->speed;
					y = bullet->hitbox.y;

					//suppression des bullets en haut de l'écran
					if(y <= 0){
						if(bullet->weapon->weapon_type != RAIL){
							// Gestion des points lors de la sortie d'un bullet
							if(bullet->weapon->weapon_type != WAVE && game_points >= 1){
								//game_points -= 1;
							}
							if(!removed){
								remove_bullet(*bullet, i);
								removed = 1;
							}
							// On met a jour les spef concernant la precision de tir
							game_bullet_used++;
							continue;
						}
					}

					//bullet : hit test avec les invaders
					for (j=0;j<wave.invaders_count;j++){
						if(wave.invaders[j].hp > 0){

							//!current object
							invader = &wave.invaders[j];

							//hit test
							if(hit_test(invader->hitbox, bullet->hitbox) == 0){
								impact = 1;

								//applique les dégats à l'invader
								if(invader->hp >= bullet->weapon->damage){
									// On met a jour les points de vie de l'invader
									invader->hp-= bullet->weapon->damage;
									// On met a jour les points
									game_points += 1;
								}
								else{
									// On met a jour les points de vie de l'invader
									invader->hp = 0;
									// On met a jour les points
									game_points += 10;
								}
								// On met a jour les spef concernant la precision de tir
								game_bullet_used++;
								game_bullet_kill++;	// La bullet à touché sa cible
							}//if hit test
						}
					}//pour chaque invader

					//bullet : hit test avec les bombes
					for(j=0;j<NB_MAX_BOMBS;j++){
						if(bombs[j].weapon != NULL){
							//hit test
							if(hit_test(bombs[j].hitbox, bullet->hitbox) == 0){
								impact = 1;
								//détruit la bombe
								remove_bullet(bombs[j], j);
							}
						}
					}

					//bullet : hit test avec les autres bullets
					for(j=0;j<NB_MAX_BULLETS;j++){
						if( 	(bullets[j].weapon != NULL) &&
								(&bullets[j] != bullet) && //Si différent de la bullet actuelle
								(bullets[j].weapon->weapon_type != RAIL) && //Si ce n'est pas le laser
								(bullets[j].weapon->weapon_type != WAVE) //Si ce n'est pas l'onde de choc
								){
							//hit test
							if(hit_test(bullets[j].hitbox, bullet->hitbox) == 0){
								impact = 1;
								//détruit la bullet
								if(!removed){
									remove_bullet(bullets[j], j);
									removed = 1;
								}
							}
						}
					}

					//si un impact a été detecté pour la bullet principale, on la supprime
					if(		impact &&
							!(bullet->weapon->weapon_type == WAVE) &&
							!(bullet->weapon->weapon_type == RAIL) ){
						if(!removed){
							remove_bullet(*bullet, i);
							removed = 1;
						}
					}

				}//if non null
			}//pour chaque bullet pirncipale

			//pour chaque bombe on va tester les collisions
			for(i=0;i<NB_MAX_BOMBS;i++){
				if(bombs[i].weapon != NULL){

					// On déplace les bombes
					bombs[i].hitbox.y += bombs[i].weapon->speed;

					//hit test avec le vaisseau
					if(hit_test(ship.hitbox, bombs[i].hitbox) == 0){
						//applique les dommages au vaisseau et supprime la bombe
						if(ship.hp > 0){
							ship.hp--;
							if(ship.hp==0){
								game_over = 1;
							}
						}
						remove_bullet(bombs[i], i);
					}
				}
			}

			//invaders : hit test avec le vaisseau
			for (i=0;i<wave.invaders_count;i++){
				if(wave.invaders[i].hp > 0 &&
				   hit_test(ship.hitbox, wave.invaders[i].hitbox) == 0){
					ship.hp = 0;
					game_over = 1;
				}
			}

			//traitement spécial pour le rail (animation)
			if(rail_id != -1 && rail_timeout <= 0){
				remove_bullet(bullets[rail_id],rail_id);
				rail_id = -1;
			}else
				rail_timeout--;

			// On deverrouille les invaders
			invaders_unlock();
			// On deverrouille le vaisseau
			ship_unlock();
			// On deverrouille les bullets
			hit_unlock();
		}else{

		}
	}

}//hit_task
Example #26
0
/* dumps the current status of all mediaproxies as a json blob */
int
sipp_mp_dump_json(char **msg)
{
	int buflen = 0, datalen = 0;
	char *buf = 0;
	void *ptr = 0, *ptr2 = 0;
        sipp_media_proxy_t *mp = NULL;
	char *tmpaddr1 = 0, *tmpaddr2 = 0, *tmp = 0;
	ship_list_t *callids = 0;
	char *str = 0;
	int ret = -1;
	ship_lock(sipp_mps);
	
	/* collect callids */
	ASSERT_TRUE(callids = ship_list_new(), err);
	while ((mp = ship_list_next(sipp_mps, &ptr))) {
		int found = 0;
		while (!found && (str = ship_list_next(callids, &ptr2))) {
			if (!strcmp(str, mp->callid))
				found = 1;
		}
		
		if (!found) {
			ship_list_add(callids, mp->callid);
		}
	}
	
	/* for each call id .. */
	ASSERT_TRUE(buf = append_str("var p2pship_mps = {\n", buf, &buflen, &datalen), err);
	ptr2 = 0;
	while ((str = ship_list_next(callids, &ptr2))) {
		ASSERT_TRUE(buf = append_str("     \"", buf, &buflen, &datalen), err);
		ASSERT_TRUE(buf = append_str(str, buf, &buflen, &datalen), err);
		ASSERT_TRUE(buf = append_str("\" : [\n", buf, &buflen, &datalen), err);

		ptr = 0;
		while ((mp = ship_list_next(sipp_mps, &ptr))) {
			int len = 0;
			
			if (!strcmp(mp->callid, str)) {
				ASSERT_ZERO(ident_addr_addr_to_str(&(mp->local_addr), &tmpaddr1), err);
				ASSERT_ZERO(ident_addr_addr_to_str(&(mp->remote_addr), &tmpaddr2), err);
				
				len = zstrlen(mp->sip_aor) + zstrlen(tmpaddr1) + zstrlen(mp->remote_aor) + zstrlen(tmpaddr2) + 
					zstrlen(mp->callid) + zstrlen(mp->mediatype) + 512;
				ASSERT_TRUE(tmp = mallocz(len), err);
				
				sprintf(tmp, "         [ \"%s\", \"%s\", \"%s\", \"%s\", \"%s\",\n           \"%s\", \"%s\", \"%d\", \"%d\", \"%d\", \"%d\" ],\n",
					mp->sip_aor, tmpaddr1, mp->remote_aor, tmpaddr2, sipp_mp_sendby_str(mp->sendby),
					mp->callid, mp->mediatype,
					mp->started, (int)mp->start_time, (int)mp->last, mp->counter);
				
				ASSERT_TRUE(buf = append_str(tmp, buf, &buflen, &datalen), err);
				freez(tmp);
				freez(tmpaddr1);
				freez(tmpaddr2);
			}
		}
		ASSERT_TRUE(replace_end(buf, &buflen, &datalen, ",\n", "\n"), err);
		ASSERT_TRUE(buf = append_str("     ],\n", buf, &buflen, &datalen), err);
	}

	ASSERT_TRUE(replace_end(buf, &buflen, &datalen, ",\n", "\n"), err);
	ASSERT_TRUE(buf = append_str("};\n", buf, &buflen, &datalen), err);
	*msg = buf;
	buf = 0;
	ret = 0;
 err:
	ship_unlock(sipp_mps);
	ship_list_free(callids);
	freez(buf);
	freez(tmpaddr1);
	freez(tmpaddr2);
	freez(tmp);
	return ret;
}
Example #27
0
/* calls to import contacts */
static int
addrbook_libebook_import(ship_list_t *imps, int *concount, int query)
{
	int ret = -1;
	void *ptr = 0;
	contact_t *c = 0;
	GError *error = 0;
	*concount = 0;

	/* query & import */
	ship_lock(addrbook_lock);
	if (ship_list_first(imps) && (!query || ui_query_import_contacts(imps) > 0)) {

		ptr = 0;
		while ((c = ship_list_next(imps, &ptr))) {
			EContact *contact = 0;
			EContactName name;
			char *arr[2], *ln;
			arr[1] = 0;
			bzero(&name, sizeof(name));
			ret = -1;

			/* create a new contact */
			ASSERT_TRUE(contact = e_contact_new(), cerr);
			
			/* create the name struct */
			ln = strchr(c->name, ' ');
			if (ln) {
				ASSERT_TRUE(name.given = strndup(c->name, ln - c->name), cerr);
				ASSERT_TRUE(name.family = strdup(ln+1), cerr);
			} else {
				ASSERT_TRUE(name.given = strdup(c->name), cerr);
			}

			/* simple strings */
			e_contact_set(contact, E_CONTACT_FULL_NAME, c->name);
			e_contact_set(contact, E_CONTACT_GIVEN_NAME, name.given);
			if (name.family)
				e_contact_set(contact, E_CONTACT_FAMILY_NAME, name.family);
 			if (ship_ht_get_string(c->params, "category"))
				e_contact_set(contact, E_CONTACT_CATEGORIES, ship_ht_get_string(c->params, "category"));
			
			/* EContactName */
			e_contact_set(contact, E_CONTACT_NAME, &name);

			/* array of strings */
 			if (ship_ht_get_string(c->params, "email")) {
				arr[0] = ship_ht_get_string(c->params, "email");
				e_contact_set(contact, E_CONTACT_EMAIL, arr);
			}
			arr[0] = c->sip_aor;
			e_contact_set(contact, E_CONTACT_SIP, arr);

			ASSERT_TRUE(e_book_add_contact(book, contact, &error), cerr);
			ret = 0;
		cerr:
			g_object_unref(contact);
			freez(name.family);
			freez(name.given);
			
			if (ret)
				goto err;

			/* save what we've done */
			c->added = time(NULL);
			(*concount)++;
		}
	}

	ret = 0;
 err:
	if (error) {
		if (query) 
			ui_print_error("Error importing the contacts: %s.\n", error->message);
		g_error_free(error);
	} else if (ret && query) {
		ui_print_error("An error occured while importing the contacts.\n");
	}
	ship_unlock(addrbook_lock);
	return ret;
}
Example #28
0
/* starts the media proxy */
int 
sipp_mp_start(sipp_media_proxy_t *mp, int remotely_got)
{
	sipp_media_proxy_t *mp2;
	void *ptr = NULL, *lastptr = NULL;

	sipp_mp_autoclean();

       	/* remove possible loops */

        ship_lock(sipp_mps);
	while ((mp2 = ship_list_next(sipp_mps, &ptr))) {
		if (!ident_addr_cmp(&mp->local_addr, &mp2->remote_addr)) {
			LOG_WARN("mediaproxy loop detected! local will be changed!\n");
			memcpy(&mp->local_addr, &mp2->local_addr, sizeof(addr_t));
		} else if (!ident_addr_cmp(&mp->remote_addr, &mp2->local_addr)) {
			LOG_WARN("mediaproxy loop detected! remote will be changed!\n");
			memcpy(&mp->remote_addr, &mp2->remote_addr, sizeof(addr_t));
		} else
			mp2 = NULL;

		if (mp2) {
			ship_list_remove(sipp_mps, mp2);
			sipp_mp_close(mp2);
			ptr = lastptr;
		}
		lastptr = ptr;
	}
	ship_unlock(sipp_mps);


	LOG_INFO("starting mp (type %d) for %s (%s:%d -> %s:%d)..\n", 
		 mp->sendby, mp->sip_aor, 
                 mp->local_addr.addr, mp->local_addr.port,
                 mp->remote_addr.addr, mp->remote_addr.port);

	if (remotely_got && sipp_mp_supports_fragmentation(mp->remote_aor, &(mp->remote_addr))) {
		mp->frag_support = 1;
		LOG_DEBUG("detected fragmentation-aware endpoint at %s:%d!\n",
			  mp->remote_addr.addr, mp->remote_addr.port);
	}
	
	/* start reading packets */
	switch (mp->sendby) {
	case SIPP_MP_SENDBY_DIRECT:
#ifdef USE_FF_DIRECT
		ASSERT_ZERO(netio_ff_add(mp->socket, &(mp->remote_addr), &(mp->counter),
					 mp->frag_support), err);
		break;
#endif
	default:
		ASSERT_ZERO(netio_packet_read(mp->socket, sipp_mp_cb_data_got), err);
	}
	
	mp->started = 1;
	time(&mp->start_time);

	if (!remotely_got) {
		LOG_DEBUG("sending fragmentation-support flag for the proxy at %s:%d\n", 
			  mp->local_addr.addr, mp->local_addr.port);
		
		sipp_mp_notify_fragmentation_support(mp);
	}
	return 0;
 err:
	return -1;
}
Example #29
0
static int
ac_packetfilter_stats(ac_sip_t *asip, void *data)
{
	osip_message_t* sip = asip->evt->sip;
	char *callid = 0;
	unsigned long now = ship_systemtimemillis();

	if (asip->remotely_got) {
		/* .. meaning remotely got */
		if (MSG_IS_RESPONSE(sip)) {
			pdd_stat_t *stat = 0;
			int code = osip_message_get_status_code(sip);
			
			/* check for ACK with code != 100 */
			callid = sipp_get_call_id(sip);
			ship_lock(stats) ;
			if (code != 100 && 
			    (stat = ship_ht_get_string(stats, callid)) &&
			    !stat->end) {
				
				stat->end = ship_systemtimemillis();
				LOG_INFO("Got PDD for %s %s -> %s (status %d) in %u.%03u seconds..\n",
					 stat->msg_type, stat->from, stat->to, code, 
					 (stat->end - stat->start) / 1000, 	
					 ((stat->end - stat->start) % 1000));

				/* ... if we are recording special, then do that! */
				if (pdd_log) {
					pdd_record_pdd(stat);
				}

				/* remove.. ? */
				ship_ht_remove_string(stats, callid);

				ship_lock(done_stats);
				while (ship_list_length(done_stats) > 20) {
					pdd_stat_t *s2 = ship_list_pop(done_stats);
					ship_ht_remove(done_stats_ht, s2);
					pdd_free_stat(s2);
				}
				ship_list_add(done_stats, stat);
				ship_ht_put_string(done_stats_ht, callid, stat);
				ship_unlock(done_stats);
			}
			ship_unlock(stats);
		} else if (MSG_IS_INVITE(sip) || MSG_IS_MESSAGE(sip)) {
			/* record the pdd for the other fellow .. */
			callid = sipp_get_call_id(sip);
			
			/* send 'stats' packet back, event remote_req */
			ac_send_stats(asip->remote, asip->local,
				      now, callid, "remote_start");
		}
	} else {
		/* if we get an invite or something like that, record the time .. and so on */
		if (MSG_IS_INVITE(sip) || MSG_IS_MESSAGE(sip)) {

			/* if not seen already & pdd mode, do some
			   funky stuff.. */
			if (pdd_reset_mode) {
				LOG_INFO("pdd measurement mode: clearing SAs and peer DB!\n");
#ifdef CONFIG_HIP_ENABLED
				hipapi_clear_sas();
#endif
				ident_reset_foreign_regs();
				conn_close_all();
			}

			callid = sipp_get_call_id(sip);
			ship_lock(stats);
			if (!ship_ht_get_string(stats, callid)) {
				pdd_stat_t *stat = pdd_new_stat(sip, asip->from, asip->to);
				if (stat) {
					stat->start = ship_systemtimemillis();
					ship_ht_put_string(stats, callid, stat);
				}
			}
			ship_unlock(stats);
		} else if (MSG_IS_RESPONSE(sip)) {
			int code = osip_message_get_status_code(sip);
			if (code != 100) {
				callid = sipp_get_call_id(sip);

				/* send 'stats' packet back, event remote_resp */
				ac_send_stats(asip->remote, asip->local,
					      now, callid, "remote_end");
			}
		}
	}
	
	freez(callid);
	return 1;
}
Example #30
0
/* saves the comma-separated list of contacts */
int
addrbook_save_imported(ship_list_t *list)
{
	int ret = -1;
	void *ptr = 0;
	contact_t *c = 0;
	FILE *f = NULL;
	ship_list_t *keys = 0;

	char *buf = 0, *tmp = 0, *tmp2 = 0;
	int len = 0, size = 0;
	
	while ((c = ship_list_next(list, &ptr))) {
		char tbuf[32];
		char *k = 0;

		if (c->sip_aor) {
			ASSERT_TRUE(tmp2 = ship_urlencode(c->sip_aor), err);
			ASSERT_TRUE((tmp = append_str(tmp2, buf, &size, &len)) && (buf = tmp), err);
			freez(tmp2);
		}
		ASSERT_TRUE((tmp = append_str(",", buf, &size, &len)) && (buf = tmp), err);
		
		if (c->name) {
			ASSERT_TRUE(tmp2 = ship_urlencode(c->name), err);
			ASSERT_TRUE((tmp = append_str(tmp2, buf, &size, &len)) && (buf = tmp), err);
			freez(tmp2);
		}
		ASSERT_TRUE((tmp = append_str(",", buf, &size, &len)) && (buf = tmp), err);

		if (c->db_id) {
			ASSERT_TRUE(tmp2 = ship_urlencode(c->db_id), err);
			ASSERT_TRUE((tmp = append_str(tmp2, buf, &size, &len)) && (buf = tmp), err);
			freez(tmp2);
		}
		ASSERT_TRUE((tmp = append_str(",", buf, &size, &len)) && (buf = tmp), err);
		
		sprintf(tbuf, "%d", (int)c->added);
		ASSERT_TRUE((tmp = append_str(tbuf, buf, &size, &len)) && (buf = tmp), err);

		/* add the optional stuff */
		ASSERT_TRUE(keys = ship_ht_keys(c->params), err);
		while ((k = ship_list_pop(keys))) {
			char *v = ship_ht_get_string(c->params, k);

			ASSERT_TRUE((tmp = append_str(",", buf, &size, &len)) && (buf = tmp), err);
			
			ASSERT_TRUE(tmp2 = ship_urlencode(k), err);
			ASSERT_TRUE((tmp = append_str(tmp2, buf, &size, &len)) && (buf = tmp), err);
			freez(tmp2);

			ASSERT_TRUE((tmp = append_str("=", buf, &size, &len)) && (buf = tmp), err);

			ASSERT_TRUE(tmp2 = ship_urlencode(v), err);
			ASSERT_TRUE((tmp = append_str(tmp2, buf, &size, &len)) && (buf = tmp), err);
			freez(tmp2);
			freez(k);
		}
		
		ASSERT_TRUE((tmp = append_str("\n", buf, &size, &len)) && (buf = tmp), err);
		ship_list_empty_free(keys);
		ship_list_free(keys);
		keys = 0;
	}

	ship_lock(addrbook_lock);
	if (!(f = fopen(contacts_file, "w"))) {
		LOG_ERROR("Could not open contacts log file %s\n", contacts_file);
		goto err;
	}
	if (len != fwrite(buf, sizeof(char), len, f))
		goto err;
	
	ret = 0;
 err:
	if (f)
		fclose(f);
	ship_unlock(addrbook_lock);

	freez(buf);
	freez(tmp2);

	if (keys) {
		ship_list_empty_free(keys);
		ship_list_free(keys);
		keys = 0;
	}

	return ret;
}