Exemplo n.º 1
0
static int update_delegation(struct kr_request *req, struct kr_query *qry, knot_pkt_t *answer, bool has_nsec3)
{
	struct kr_zonecut *cut = &qry->zone_cut;

	/* RFC4035 3.1.4. authoritative must send either DS or proof of non-existence.
	 * If it contains neither, the referral is bogus (or an attempted downgrade attack).
	 */

	unsigned section = KNOT_ANSWER;
	if (!knot_wire_get_aa(answer->wire)) { /* Referral */
		section = KNOT_AUTHORITY;
	} else if (knot_pkt_qtype(answer) == KNOT_RRTYPE_DS) { /* Subrequest */
		section = KNOT_ANSWER;
	} else { /* N/A */
		return kr_ok();
	}

	int ret = 0;
	const knot_dname_t *proved_name = knot_pkt_qname(answer);
	/* Aggregate DS records (if using multiple keys) */
	knot_rrset_t *new_ds = update_ds(cut, knot_pkt_section(answer, section));
	if (!new_ds) {
		/* No DS provided, check for proof of non-existence. */
		if (!has_nsec3) {
			if (!knot_wire_get_aa(answer->wire)) {
				/* Referral, check if it is referral to unsigned, rfc4035 5.2 */
				ret = kr_nsec_ref_to_unsigned(answer);
			} else {
				/* No-data answer */
				ret = kr_nsec_existence_denial(answer, KNOT_AUTHORITY, proved_name, KNOT_RRTYPE_DS);
			}
		} else {
			if (!knot_wire_get_aa(answer->wire)) {
				/* Referral, check if it is referral to unsigned, rfc5155 8.9 */
				ret = kr_nsec3_ref_to_unsigned(answer);
			} else {
				/* No-data answer, QTYPE is DS, rfc5155 8.6 */
				ret = kr_nsec3_no_data(answer, KNOT_AUTHORITY, proved_name, KNOT_RRTYPE_DS);
			}
			if (ret == kr_error(DNSSEC_NOT_FOUND)) {
				/* Not bogus, going insecure due to optout */
				ret = 0;
			}
		}
		if (ret != 0) {
			DEBUG_MSG(qry, "<= bogus proof of DS non-existence\n");
			qry->flags |= QUERY_DNSSEC_BOGUS;
		} else {
			DEBUG_MSG(qry, "<= DS doesn't exist, going insecure\n");
			qry->flags &= ~QUERY_DNSSEC_WANT;
			qry->flags |= QUERY_DNSSEC_INSECURE;
		}
		return ret;
	}

	/* Extend trust anchor */
	DEBUG_MSG(qry, "<= DS: OK\n");
	cut->trust_anchor = new_ds;
	return ret;
}
Exemplo n.º 2
0
void DIRECTION::identify_walls(bool &opennorth,bool &opensouth,bool &openeast, bool &openwest){
	update_ds();

	bool front = true,back = true,left = true,right = true;

	if(ds_left_10 > DS_TRASHOLD_BIG
	|| ds_right_10 > DS_TRASHOLD_BIG){
		front = false;
	}

	if(ds_right_90 > DS_TRASHOLD_BIG){
		right = false;
	}

	if(ds_left_90 > DS_TRASHOLD_BIG){
		left  = false;
	}

	//Maybe yes, maybe not
	back = true;

	if(orientation == DIR_NORTH){
		opennorth = front;
		openeast  = right;
		openwest  = left;
		opensouth = back;
	}
	else
	if(orientation == DIR_SOUTH){
		opennorth = back;
		openeast  = left;
		openwest  = right;
		opensouth = front;
	}
	else
	if(orientation == DIR_EAST){
		opennorth = left;
		openeast  = front;
		openwest  = back;
		opensouth = right;
	}
	else
	if(orientation == DIR_WEST){
		opennorth = right;
		openeast  = back;
		openwest  = front;
		opensouth = left;
	}
}
Exemplo n.º 3
0
void DIRECTION::run(){

	reset_modifiers();

	//State machine
 	if(     curr_state == STATE_FOLLOW_CORIDOR){

		update_ds();
		wall_repeller();

		//std::cout << "left=" << ve_aux.get_left(diff_wheels) << std::endl;
		//std::cout << "right=" << ve_aux.get_right(diff_wheels) << std::endl;

		//Daca exista un perete in fata
		//TODO Decomenteaza
		if(ds_left_10 >= DS_ROTATE180_TRIGGER
			|| ds_right_10 >= DS_ROTATE180_TRIGGER){
				switch_state(STATE_INTERSECTION);
			}

		//Compute total modifier
		total_delta_right =  - wr_delta;
		total_delta_left  =  + wr_delta;
	}
	else if(curr_state == STATE_MOVE_TO_INTERSECT_CENTER){

		// update_ds();
		//
		// wall_repeller_aux();
		//
		// total_delta_right =  - wr_delta;
		// total_delta_left  =  + wr_delta;


		total_delta_right =  0;
		total_delta_left  =  0;

		if(ve_aux.get_left(diff_wheels) > CTOI_DISTANCE
		   || ve_aux.get_right(diff_wheels) > CTOI_DISTANCE){

			 switch_state(STATE_INTERSECTION);

		 }
	}
	else if(curr_state == STATE_MOVE_TO_CORIDOR){

		//  update_ds();
		//  wall_repeller_noenc();
		//  total_delta_right =  - wr_delta;
 	// 	 total_delta_left  =  + wr_delta;

	total_delta_left  = 0;
	total_delta_right = 0;

		//
		 //wall_repeller_aux2();
		//
		// total_delta_right =  - wr_delta;
		// total_delta_left  =  + wr_delta;




		//
		// std::cout << "abs_left=" << diff_wheels.getLeftEncoder() << std::endl;
		// std::cout << "abs_right=" << diff_wheels.getRightEncoder() << std::endl;
		// std::cout << "left=" << ve_aux.get_left(diff_wheels) << std::endl;
		// std::cout << "right=" << ve_aux.get_right(diff_wheels) << std::endl;


		if(ve_aux.get_left(diff_wheels) > ITOC_DISTANCE
		   || ve_aux.get_right(diff_wheels) > ITOC_DISTANCE ){
			 switch_state(STATE_FOLLOW_CORIDOR);
		 }
	}
	else if(curr_state == STATE_ROTATE){

		if(times_rotate != 0){
			total_delta_left = direction_rotate*-ROTATE_SPEED -BASE_SPEED;
			total_delta_right = direction_rotate*ROTATE_SPEED -BASE_SPEED;

		}
		else{
			switch_state(STATE_MOVE_TO_CORIDOR);
		}


		if((abs(ve_rotate.get_right(diff_wheels)) >= ROTATE_VAL
			  && times_rotate  == 1)
		||((abs(ve_rotate.get_right(diff_wheels)) >= ROTATE_VAL_180
		    && times_rotate  == 2))){
				switch_state(STATE_MOVE_TO_CORIDOR);
		}
	}
	else if(curr_state == STATE_STOP){
		total_delta_left  = -BASE_SPEED;
		total_delta_right = -BASE_SPEED;
		//TODO Mai simplu daca se cal viteza direct?
	}




	/*

		-Adauga un nod nou in graf sau o muchie
		(nod nou, daca intersectia nu a mai fost vizitate
		si muchie daca intersectia a mai fost vizitate)
		-Determina in ce directie poate sa
		mearga.
		-Alege o directie

	*/
	else if(curr_state == STATE_INTERSECTION){

		//Nu se deplaseaza
		total_delta_left  = -BASE_SPEED;
		total_delta_right = -BASE_SPEED;



		//Calculeaza distanta
		int avg_dist = (ve_node_to_node.get_left(diff_wheels)
									 + ve_node_to_node.get_right(diff_wheels))
									 /2;


		//Actualizeaza coordonatele
		if(orientation == DIR_NORTH){
			y -= avg_dist;
		}
		else if(orientation == DIR_SOUTH){
			y += avg_dist;
		}
		else if(orientation == DIR_EAST){
			x += avg_dist;
		}
		else if(orientation == DIR_WEST){
			x -= avg_dist;
		}

		//TODO Detectare daca un drum este deschis
		//Analizeaza pozitia curenta
		//Ia date de la senzori

		bool opennorth, opensouth, openeast, openwest;

		identify_walls(opennorth, opensouth, openeast, openwest);

		//Starea wall-urilor
		std::cout<<"north="<<opennorth<<std::endl;
		std::cout<<"south="<<opensouth<<std::endl;
		std::cout<<"east="<<openeast<<std::endl;
		std::cout<<"west="<<openwest<<std::endl;


		//TODO Fix
		curr_node = graph.on_intersection(x, y, orientation, opennorth, opensouth, openeast, openwest);

		std::cout<<"cnode_north="<<curr_node->open_on_dir[DIR_NORTH]<<std::endl;
		std::cout<<"cnode_south="<<curr_node->open_on_dir[DIR_SOUTH]<<std::endl;
		std::cout<<"cnode_east="<<curr_node->open_on_dir[DIR_EAST]<<std::endl;
		std::cout<<"cnode_west="<<curr_node->open_on_dir[DIR_WEST]<<std::endl;

		std::cout<<"cnode_north_viz="<<curr_node->visited_on_dir[DIR_NORTH]<<std::endl;
		std::cout<<"cnode_south_viz="<<curr_node->visited_on_dir[DIR_SOUTH]<<std::endl;
		std::cout<<"cnode_east_viz="<<curr_node->visited_on_dir[DIR_EAST]<<std::endl;
		std::cout<<"cnode_west_viz="<<curr_node->visited_on_dir[DIR_WEST]<<std::endl;

		old_orientation = orientation;

		//Pregateste o decizie dupa semnalare
		if(curr_node->open_on_dir[DIR_NORTH]
		&& !curr_node->visited_on_dir[DIR_NORTH]){
			direction_rotate = get_rotate_direction(orientation,DIR_NORTH);
			times_rotate = get_rotate_times(orientation,DIR_NORTH);
			orientation = DIR_NORTH;
		}
		else
		if(curr_node->open_on_dir[DIR_SOUTH]
		&& !curr_node->visited_on_dir[DIR_SOUTH]){
			direction_rotate = get_rotate_direction(orientation,DIR_SOUTH);
			times_rotate = get_rotate_times(orientation,DIR_SOUTH);
			orientation = DIR_SOUTH;
		}
		else
		if(curr_node->open_on_dir[DIR_EAST]
		&& !curr_node->visited_on_dir[DIR_EAST]){
			direction_rotate = get_rotate_direction(orientation,DIR_EAST);
			times_rotate = get_rotate_times(orientation,DIR_EAST);
			orientation = DIR_EAST;
		}
		else
		if(curr_node->open_on_dir[DIR_WEST]
		&& !curr_node->visited_on_dir[DIR_WEST]){
			direction_rotate = get_rotate_direction(orientation,DIR_WEST);
			times_rotate = get_rotate_times(orientation,DIR_WEST);
			orientation = DIR_WEST;
		}
		else if(curr_node->open_on_dir[DIR_NORTH]){
			direction_rotate = get_rotate_direction(orientation,DIR_NORTH);
			times_rotate = get_rotate_times(orientation,DIR_NORTH);
			orientation = DIR_NORTH;
		}
		else
		if(curr_node->open_on_dir[DIR_SOUTH]){
			direction_rotate = get_rotate_direction(orientation,DIR_SOUTH);
			times_rotate = get_rotate_times(orientation,DIR_SOUTH);
			orientation = DIR_SOUTH;
		}
		else
		if(curr_node->open_on_dir[DIR_EAST]){
			direction_rotate = get_rotate_direction(orientation,DIR_EAST);
			times_rotate = get_rotate_times(orientation,DIR_EAST);
			orientation = DIR_EAST;
		}
		else
		if(curr_node->open_on_dir[DIR_WEST]){
			direction_rotate = get_rotate_direction(orientation,DIR_WEST);
			times_rotate = get_rotate_times(orientation,DIR_WEST);
			orientation = DIR_WEST;
		}
		else{
			//TODO daca nodul curent a fost explorat in toate
			//directile, se merge la cel mai apropiat nod care
			//nu a fost explorat complet

			std::cout<<"No place to go\n";
			switch_state(STATE_STOP);
			return;
		}


		//std::cout<<"Willgo="<<orientation;

		//Semnalizeaza directile in care poate merge
		switch_state(STATE_SIGNAL);
	}
	/*Semnalizeaza */
	else if(curr_state == STATE_SIGNAL){
		/*
			 Pentru determinarea directiilor accesibile fizic
			 si a celor vizitate:
			 curr_node->open_on_dir[DIR_NORTH]
			 curr_node->open_on_dir[DIR_SOUTH]
			 curr_node->open_on_dir[DIR_EAST]
			 curr_node->open_on_dir[DIR_WEST]
			 curr_node->visited_on_dir[DIR_NORTH]
			 curr_node->visited_on_dir[DIR_SOUTH]
			 curr_node->visited_on_dir[DIR_EAST]
			 curr_node->visited_on_dir[DIR_WEST]
		*/

		if(curr_node != NULL){

			//Determina valorile relative lalabirint

			dir abs_north,abs_south,abs_east,abs_west;

			if(old_orientation == DIR_NORTH){
				abs_north = DIR_NORTH;
				abs_south = DIR_SOUTH;
				abs_east  = DIR_EAST;
				abs_west  = DIR_WEST;
			}
			else if(old_orientation == DIR_SOUTH){
				abs_north = DIR_SOUTH;
				abs_south = DIR_NORTH;
				abs_east  = DIR_WEST;
				abs_west  = DIR_EAST;
			}
			else if(old_orientation == DIR_EAST){
				abs_north = DIR_EAST;
				abs_south = DIR_WEST;
				abs_east  = DIR_SOUTH;
				abs_west  = DIR_NORTH;
			}
			else if(old_orientation == DIR_WEST){
				abs_north = DIR_WEST;
				abs_south = DIR_EAST;
				abs_east  = DIR_NORTH;
				abs_west  = DIR_SOUTH;
			}

			// if(curr_node->open_on_dir[abs_north]
			// && !curr_node->visited_on_dir[abs_north]){
			//
			// 	if(leds_on) led[0]->set(1);
			// 	else 			  led[0]->set(0);
			// 	//led[0]->set(1);
			// }
			// else
			if(curr_node->open_on_dir[abs_north]){
				led[0]->set(1);
			}

			// if(curr_node->open_on_dir[abs_south]
			// && !curr_node->visited_on_dir[abs_south]){
			// 	if(leds_on) led[4]->set(1);
			// 	else 			  led[4]->set(0);
			// 	//led[4]->set(1);
			// }
			// else
			if(curr_node->open_on_dir[abs_south]){
				led[4]->set(1);
			}

			// if(curr_node->open_on_dir[abs_east]
			// && !curr_node->visited_on_dir[abs_east]){
			// 	if(leds_on) led[2]->set(1);
			// 	else 			  led[2]->set(0);
			// 	//led[2]->set(1);
			// }
			// else
			if(curr_node->open_on_dir[abs_east]){
				led[2]->set(1);
			}


			// if(curr_node->open_on_dir[abs_west]
			// && !curr_node->visited_on_dir[abs_west]){
			// 	if(leds_on) led[6]->set(1);
			// 	else 			  led[6]->set(0);
			// 	//led[6]->set(1);
			// }
			// else
			if(curr_node->open_on_dir[abs_west]){
				led[6]->set(1);
			}

		}
		else{
			std::cout << "ERR: STATE_SIGNAL curr_node == NULL" << std::endl;
		}

		//Daca a stat in aceasta stare 3 secunde
		if(time(NULL) - timer >= 1){

			//std::cout << "Time to switch\n" << std::endl;

			set_all_leds_off();//TODO~mod

			switch_state(STATE_ROTATE);
		}
		else{
			//std::cout << "time_diff="<< time(NULL) - timer << std::endl;
		}

		if(time(NULL) - timer_blink_led >= 1 ){
			leds_on = !leds_on;
			timer_blink_led = time(NULL);
			//std::cout<<"switching leds\n";
		}

}

	//Set speeds
	speed_right = constrain(BASE_SPEED + total_delta_right);
	speed_left  = constrain(BASE_SPEED + total_delta_left);
	diff_wheels.setSpeed(speed_left,speed_right);
}