Пример #1
0
int main()
{
    struct list_t *root;
    size_t s;
    int a=43,b=63,c=99,d=70,e=25,f=17,g=57,h=69,i=111,j=120,k=70,l=73,m=75;
    /* char z[100]="test"; */
    /* z = "test"; */

    root = list_init();

	root = list_remove_rear(root);
    root = list_remove_any(root,&k);
    root = list_remove_front(root);

    list_insert_rear(root, &a);
    list_insert_rear(root, &b);
    
    root = list_insert_after(root,&c,3);
    list_insert_rear(root, &d);
    
    root = list_insert_front(root, &e);
    root = list_insert_front(root, &f);
    
    list_insert_rear(root, &g);
    root = list_remove_front(root);
    
    list_insert_rear(root, &h);
    
    root = list_insert_after(root,&i,5);
    root = list_insert_after(root,&j,120);
    
    root = list_remove_any(root,&k);

    root = list_remove_front(root);
    list_insert_rear(root, &l);
    root = list_remove_any(root,&l);

    list_insert_rear(root, &m);
    
    
    root = list_remove_rear(root);
    root = list_remove_rear(root);
    root = list_remove_rear(root);
    root = list_remove_rear(root);
    root = list_remove_rear(root);
    root = list_remove_rear(root);
    root = list_remove_rear(root);
    
    s = list_size(root);
    printf( "%d\n", (int)s);
    printf( "%d\n", search(root, &g) );

    list_destroy(root);

    return 0;
}
Пример #2
0
list* search_breadth_first(void* state,
                           void* state_world,
                           search_is_goal state_goal_func,
                           search_gen_successors state_gen_func,
                           search_link_parent state_link_func,
                           search_goal_backtrace state_back_func,
                           hash_func state_hash_alg,
                           generic_comp state_comp_func,
                           generic_cpy state_copy_func,
                           generic_op state_free_func) {
    int found;
    void* current_state, *successor_state;
    list* state_queue, *successor_list, *path;
    hash_table* state_closed_set;

    state_queue = list_create(NULL,
                              NULL,
                              state_free_func);

    state_closed_set = hash_table_create(89,
                                         .75,
                                         state_hash_alg,
                                         state_comp_func,
                                         state_copy_func,
                                         state_free_func);
    current_state = state;
    list_push_front(state_queue, current_state);
    hash_table_insert(state_closed_set, current_state, 0);
    path = NULL;
    found = 0;
    while(!list_is_empty(state_queue) && !found) {
        current_state = list_back(state_queue);
        list_deque(state_queue);
        if(state_goal_func(current_state, state_world)) {
            current_state = state_copy_func(current_state);
            path = state_back_func(current_state);
            found = 1;
        } else {
            successor_list = state_gen_func(current_state, state_world);
            while(!list_is_empty(successor_list)) {
                successor_state = list_front(successor_list);
                if(!hash_table_contains(state_closed_set, successor_state)) {
                    state_link_func(successor_state, current_state);
                    hash_table_insert(state_closed_set, successor_state, 0);
                    list_push_front(state_queue, successor_state);
                    list_pop(successor_list);
                } else {
                    list_remove_front(successor_list);
                }
            }
            list_kill(successor_list);
        }
    }
    hash_table_kill(state_closed_set);
    list_dissolve(state_queue);
    return path;
}
Пример #3
0
// TODO: APPLY NEW FIX
error_t page_cache_release_buffer(uint32 gfd, uint32 page)
{
	//if (gfd >= page_cache.cached_files.count || page_cache.cached_files[gfd].gfd == INVALID_FD)		// kinda erroneous gfd
	//{
	//	set_last_error(EBADF, PAGE_CACHE_INVALID, EO_PAGE_CACHE);
	//	return ERROR_OCCUR;
	//}

	uint32 index = -1;

	// remove index from page list
	auto list = &gft_get_table()->data[gfd].pages;
	auto prev = list->head;

	if (prev == 0)
	{
		DEBUG("Page cache release got zero length page list");
		set_last_error(EINVAL, PAGE_CACHE_BAD_PAGES, EO_PAGE_CACHE);
		return ERROR_OCCUR;
	}

	if (prev->data.page == page)
	{
		index = prev->data.buffer_index;
		list_remove_front(list);					// TODO: Check this line for errors
	}
	else
	{
		while (prev->next != 0)
		{
			if (prev->next->data.page == page)
			{
				index = prev->next->data.buffer_index;
				list_remove(list, prev);			// TODO: Check this line for errors
				break;
			}
			prev = prev->next;
		}
	}
	
	if (index == -1)
	{
		DEBUG("Page not found to release");
		set_last_error(EINVAL, PAGE_CACHE_PAGE_NOT_FOUND, EO_PAGE_CACHE);
		return ERROR_OCCUR;
	}

	page_cache_release_anonymous(page_cache_addr_by_index(index));

	return ERROR_OK;
}
Пример #4
0
void main(){
    puts("init stress");
    head = list_init();
    int i;
    for (i = 0; i < 100000; i++) {
        list_insert_rear(&head, &i);
    }
    list_print(head);
    for (i = 0; i < 100000; i++) {
        list_remove_front(&head);
    }
    free(head);

}
Пример #5
0
static struct lnode *liberar_no_atual(struct lnode *no)
{
	struct token *tk;

	if (list_node_old == NULL)
		tk = list_remove_front(*tokens);
	else
		tk = list_remove_node(*tokens, no, list_node_old);
	free_token(tk);
	if (list_node_old == NULL)
		return (*tokens)->front;
	else
		return list_node_old->next;
}
Пример #6
0
void core_termination_handler(int sig) {
	PRINT_IMPORTANT("**********Terminating *******");

	int i;

	//shutdown all module threads in backwards order of startup
	PRINT_IMPORTANT("modules: shutdown");
	for (i = MAX_MODULES - 1; i >= 0; i--) {
		if (overall->modules[i] != NULL) {
			overall->modules[i]->ops->shutdown(overall->modules[i]);
		}
	}

	//have each module free data & que/sem //TODO finish each of these
	PRINT_IMPORTANT("modules: release");
	for (i = MAX_MODULES - 1; i >= 0; i--) {
		if (overall->modules[i] != NULL) {
			overall->modules[i]->ops->release(overall->modules[i]);
		}
	}

	PRINT_IMPORTANT("admin: free");
	list_free(overall->admin_list, nop_func);

	PRINT_IMPORTANT("libraries: close");
	struct fins_library *library;
	while (!list_is_empty(overall->lib_list)) {
		library = (struct fins_library *) list_remove_front(overall->lib_list);

		PRINT_IMPORTANT("closing library: library=%p, name='%s'", library, library->name);
		dlclose(library->handle);
		free(library);
	}
	free(overall->lib_list);

	PRINT_IMPORTANT("Freeing links");
	list_free(overall->link_list, free);

	PRINT_IMPORTANT("Freeing environment");
	list_free(overall->envi->if_list, ifr_free);
	list_free(overall->envi->route_list, free);
	free(overall->envi);

	free(overall);
	sem_destroy(&control_serial_sem);

	PRINT_IMPORTANT("FIN");
	exit(-1);
}
Пример #7
0
/* delete all nodes where data == element */
struct list_t* list_remove_any(struct list_t* list, void *element)
{
    struct list_t *walker, *prev_item, *next_item;
    int need_to_del_first = 1;

    if ( is_empty_list(list) )
    {
        return list;
    }
    else if ( !is_empty_list(list) )
    {
        while ( need_to_del_first == 1)
        {
            if ( list->data == element )
            {
                list = list_remove_front(list);
                need_to_del_first = 1;
            }
            else
            {
                need_to_del_first = 0;
            }
        }
        walker = list;

        while ( walker->next != 0 )
        {
            prev_item = walker;
            walker = walker->next;
            if ( walker->data == element ) {
                next_item = walker->next;
                prev_item->next = next_item;
                
                free(walker);
                walker = prev_item;
            }
        }
        
    }

    return list;
}
Пример #8
0
void rtm_alert(struct fins_module *module, struct finsFrame *ff) {
	PRINT_DEBUG("Entered: module=%p, ff=%p, meta=%p", module, ff, ff->metaData);
	struct rtm_data *md = (struct rtm_data *) module->data;

	//Get from fcf: module index, opcode==CTRL_ALERT, param_id

	secure_sem_wait(&md->shared_sem);
	//search for consoles with type==listener/dual, & registered for module index / param_id
	struct linked_list *listening_list = list_find_all2(md->console_list, rtm_console_listening_test, &ff->ctrlFrame.sender_id, &ff->ctrlFrame.param_id);

	//for each console push the traffic
	struct rtm_console *console;
	while (!list_is_empty(listening_list)) {
		console = (struct rtm_console *) list_remove_front(listening_list);
		rtm_send_fd(console->fd, ff->ctrlFrame.data_len, ff->ctrlFrame.data);
	}
	sem_post(&md->shared_sem);
	free(listening_list);

	freeFinsFrame(ff);
}
Пример #9
0
void *worker_thread(void *local) {
	struct pool_worker *worker = (struct pool_worker *) local;
	PRINT_DEBUG("Entered: id=%u", worker->id);

	while (1) {
		secure_sem_wait(worker->inactive_sem);
		PRINT_DEBUG("queue=%p", worker->queue);
		if (list_is_empty(worker->queue)) {
			*worker->inactive_num += 1;
			worker->inactive = 1;
			PRINT_DEBUG("inactive: worker=%p, inactive_num=%u", worker, *worker->inactive_num);
			sem_post(worker->inactive_sem);

			secure_sem_wait(&worker->activate_sem);
			if (!worker->running) {
				break;
			}
		} else {
			if (worker->running) {
				struct pool_request *request = (struct pool_request *) list_remove_front(worker->queue);
				worker->work = request->work;
				worker->local = request->local;

				PRINT_DEBUG("Freeing: request=%p", request);
				free(request);
				sem_post(worker->inactive_sem);
			} else {
				sem_post(worker->inactive_sem);
				break;
			}
		}

		worker->work(worker->local);
	}

	PRINT_DEBUG("Exited: id=%u", worker->id);
	return NULL;
}
Пример #10
0
int main()
{
	struct hash *my_hash, *my_hash2;
	int x;

	/* Testing Hash with Linked List items */

	struct list_t *root, *root2, *root3;
    int a=43,b=63,c=99,d=70,e=25,f=17,g=57,h=69,i=111,j=120,k=70,l=73,m=75;

    root = list_init();
    root2 = list_init();
    root3 = list_init();

	root = list_remove_rear(root);
    root = list_remove_any(root,&k);
    root = list_remove_front(root);

    list_insert_rear(root, &a);
    list_insert_rear(root, &b);
    
    root = list_insert_after(root,&c,3);
    list_insert_rear(root, &d);
    
    root = list_insert_front(root, &e);
    root = list_insert_front(root, &f);
    
    list_insert_rear(root, &g);
    root = list_remove_front(root);
    
    list_insert_rear(root, &h);
    
    root = list_insert_after(root,&i,5);
    root = list_insert_after(root,&j,120);
    
    root = list_remove_any(root,&k);

    root = list_remove_front(root);
    list_insert_rear(root, &l);
    root = list_remove_any(root,&l);

    list_insert_rear(root, &m);
    
    
    root = list_remove_rear(root);
    root = list_remove_rear(root);
    root = list_remove_rear(root);
    root = list_remove_rear(root);
    root = list_remove_rear(root);
    root = list_remove_rear(root);
    root = list_remove_rear(root);
    

	my_hash = hash_init("list");

	hash_insert(my_hash, "key1", root);
	hash_insert(my_hash, "key2", root2);
	hash_insert(my_hash, "key3", root3);

	printf("%s\n",my_hash->hash_type);

	char* s_val = "key2";
	char* r_val = hash_get(my_hash, s_val);
	printf("%s - %s\n", s_val, r_val);
	hash_iterate(my_hash);
	hash_destroy(my_hash);

	/* Testing Hash with Binary Tree items */

	struct node *root_node = NULL, *root_node2 = NULL, *root_node3 = NULL;

	root_node = insert(root_node,5,NULL);
    root_node = delete_node(root_node,5);
    root_node = insert(root_node,7,NULL);
    root_node = insert(root_node,3,NULL);
    root_node = insert(root_node,6,NULL);
    root_node = insert(root_node,9,NULL);
    root_node = insert(root_node,12,NULL);
    root_node = insert(root_node,1,NULL);
    print_preorder(root_node);
    root_node = delete_node(root_node,7);

    my_hash2 = hash_init("tree");
	hash_insert(my_hash2, "key1", root_node);
	hash_insert(my_hash2, "key2", root_node2);
	hash_insert(my_hash2, "key3", root_node3);

	hash_iterate(my_hash2);
	hash_destroy(my_hash2);

	return 0;
}
Пример #11
0
/*=============================================================================
Function send_packet

Purpose:  sends the packets in the queue for the passed in interface
          adding slip encoding as it sends
          
Parameters:
    *i (IN) - a pointer to the interface to send a packet for
        
Returns:  nothing, the packets for interface *i are sent and then removed
=============================================================================*/
void *send_packet(void *i) {
  struct interface *iface = (struct interface *)i;

  int sd, addlen, qsize = 1; /* always at least 1 packet to send */
  struct sockaddr_in sin;
  struct protoent *ptrp;

  struct packet_entry *pe;
  char *p;
  int temp;
  memset((char *)&sin,0,sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_port = htons(iface->rport);
  sin.sin_addr.s_addr = htonl(iface->rip);

  if (((int)(ptrp = getprotobyname("udp"))) == 0)
    error("getprotobyname");

  if ((sd = socket(AF_INET, SOCK_DGRAM,ptrp->p_proto)) < 0)
    error("socket");

  while(qsize) {

    pe = list_peek_front(iface->sendq); /* leave on queue */
    p = pe->packet;
    #ifdef DEBUG
      printf("sending  (%d): ",pe->len);
      print_packet(p,pe->len);
    #endif
    /* sending an END at the start will flush any residual packet */
    temp = *p;
    *p = END;
    sendto(sd,p,1,0,(struct sockaddr *)(&sin),sizeof(sin));    
    *p = temp;
    while (pe->len--) {
      switch (*p) {
        case (char)END:
          *p = ESC;
          sendto(sd,p,1,0,(struct sockaddr *)(&sin),sizeof(sin));
          mysleep(0,SLEEP);
          *p = ESC_END;
          break;
        case (char)ESC:
          *p = ESC;
          sendto(sd,p,1,0,(struct sockaddr *)(&sin),sizeof(sin));
          mysleep(0,SLEEP);
          *p = ESC_ESC;
          break;
      }
      sendto(sd,p++,1,0,(struct sockaddr *)(&sin),sizeof(sin));
      mysleep(0,SLEEP);
    }
    *--p = END; /* END placed at end of packet and then sent */
    sendto(sd,p,1,0,(struct sockaddr *)(&sin),sizeof(sin));
    pthread_mutex_lock(iface->qmut);
    pe = list_remove_front(iface->sendq); /* now remove from queue */
    packet_entry_free(pe);
    qsize = iface->sendq->size; /* this will determine if thread continues */
    pthread_mutex_unlock(iface->qmut);
  }
}
Пример #12
0
void core_main(uint8_t *envi_name, uint8_t *stack_name, uint32_t seed) {
	PRINT_IMPORTANT("Core Initiation: Starting ************");

#ifdef BUILD_FOR_ANDROID
	library_dummies();
#endif

	register_to_signal(SIGRTMIN);
	if (seed == DEFAULT_SEED_NUM) {
		srand((unsigned int) time(NULL));
	} else {
		srand(seed);
	}

	sem_init(&global_control_serial_sem, 0, 1); //TODO remove after gen_control_serial_num() converted to RNG

	signal(SIGINT, core_termination_handler); //register termination handler

	int status;
	int i, j, k;
	metadata_element *list_elem;
	int list_num;
	metadata_element *elem;
	metadata_element *ip_elem;
	uint32_t ip_num;

	//######################################################################
	overall = (struct fins_overall *) secure_malloc(sizeof(struct fins_overall));
	sem_init(&overall->sem, 0, 1);

	//######################################################################
	overall->envi = (struct envi_record *) secure_malloc(sizeof(struct envi_record));

	PRINT_IMPORTANT("########################## loading environment: '%s'", (char *) envi_name);
	metadata *meta_envi = (metadata *) secure_malloc(sizeof(metadata));
	metadata_create(meta_envi);

	status = config_read_file(meta_envi, (char *) envi_name);
	if (status == META_FALSE) {
		PRINT_ERROR("file='%s', %s:%d - %s\n", envi_name, config_error_file(meta_envi), config_error_line(meta_envi), config_error_text(meta_envi));
		metadata_destroy(meta_envi);
		PRINT_ERROR("todo error");
		exit(-1);
	}

	//############# if_list
	PRINT_IMPORTANT("############# Configuring List of Interfaces");
	overall->envi->if_list = list_create(MAX_INTERFACES);

	list_elem = config_lookup(meta_envi, "environment.interfaces");
	if (list_elem == NULL) {
		PRINT_ERROR("todo error");
		exit(-1);
	}
	list_num = config_setting_length(list_elem);

	int32_t if_index;
	uint8_t *name;
	uint64_t mac;
	uint32_t mode;
	uint32_t mtu;
	uint32_t flags;

	struct if_record *ifr;

	for (i = 0; i < list_num; i++) {
		elem = config_setting_get_elem(list_elem, i);
		if (elem == NULL) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		status = config_setting_lookup_int(elem, "index", (int *) &if_index);
		if (status == META_FALSE) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		status = config_setting_lookup_string(elem, "name", (const char **) &name);
		if (status == META_FALSE) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		status = config_setting_lookup_int64(elem, "mac", (long long *) &mac);
		if (status == META_FALSE) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		status = config_setting_lookup_int(elem, "mode", (int *) &mode);
		if (status == META_FALSE) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		status = config_setting_lookup_int(elem, "mtu", (int *) &mtu);
		if (status == META_FALSE) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		status = config_setting_lookup_int(elem, "flags", (int *) &flags);
		if (status == META_FALSE) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		//#############
		ifr = (struct if_record *) list_find1(overall->envi->if_list, ifr_index_test, &if_index);
		if (ifr == NULL) {
			ifr = (struct if_record *) secure_malloc(sizeof(struct if_record));
			ifr->index = if_index;
			strcpy((char *) ifr->name, (char *) name);
			ifr->mac = mac;

			ifr->mode = (uint8_t) mode;
			ifr->mtu = mtu;
			ifr->flags = flags;

			ifr->addr_list = list_create(MAX_FAMILIES);

			if (list_has_space(overall->envi->if_list)) {
				PRINT_IMPORTANT("Adding interface: ifr=%p, index=%u, name='%s', mac=0x%012llx", ifr, ifr->index, ifr->name, ifr->mac);
				list_append(overall->envi->if_list, ifr);
			} else {
				//TODO error
				PRINT_ERROR("todo error");
				exit(-1);
			}

			if (flags & IFF_LOOPBACK) {
				overall->envi->if_loopback = ifr;
			}
		} else {
			PRINT_ERROR("todo error");
			exit(-1);
		}
	}
	PRINT_IMPORTANT("if_list: list=%p, max=%u, len=%u", overall->envi->if_list, overall->envi->if_list->max, overall->envi->if_list->len);

	//############# if_loopback
	PRINT_IMPORTANT("############# Configuring Loopback Interface");
	if (overall->envi->if_loopback != NULL) {
		PRINT_IMPORTANT("loopback: name='%s', addr_list->len=%u", overall->envi->if_loopback->name, overall->envi->if_loopback->addr_list->len);
	} else {
		PRINT_WARN("todo error");
	}

	//############# if_main
	PRINT_IMPORTANT("############# Configuring Main Interface");
	uint32_t if_main;

	status = config_lookup_int(meta_envi, "environment.main_interface", (int *) &if_main);
	if (status == META_FALSE) {
		PRINT_ERROR("todo error");
		exit(-1);
	}

	overall->envi->if_main = (struct if_record *) list_find1(overall->envi->if_list, ifr_index_test, &if_main);
	if (overall->envi->if_main != NULL) {
		PRINT_IMPORTANT("main interface: name='%s', addr_list->len=%u", overall->envi->if_main->name, overall->envi->if_main->addr_list->len);
		if (!ifr_running_test(overall->envi->if_main)) {
			PRINT_WARN("!!!!Selected main interface is NOT running: name='%s', flagx->len=0x%x", overall->envi->if_main->name, overall->envi->if_main->flags);
		}
	} else {
		PRINT_WARN("todo error");
	}

	//############# addr_list
	PRINT_IMPORTANT("############# Configuring List of Host Addresses");
	//overall->envi->addr_list = list_create(MAX_INTERFACES * MAX_FAMILIES); //TODO use?

	list_elem = config_lookup(meta_envi, "environment.addresses");
	if (list_elem == NULL) {
		PRINT_ERROR("todo error");
		exit(-1);
	}
	list_num = config_setting_length(list_elem);

	uint32_t family; //atm only AF_INET, but eventually also AF_INET6
	uint32_t ip[4]; //SIOCGIFADDR //ip
	uint32_t mask[4]; //SIOCGIFNETMASK //mask
	uint32_t gw[4]; //? //(ip & mask) | 1;
	uint32_t bdc[4]; //SIOCGIFBRDADDR //(ip & mask) | ~mask
	uint32_t dst[4]; //SIOCGIFDSTADDR //dst

	struct addr_record *addr;

	for (i = 0; i < list_num; i++) {
		elem = config_setting_get_elem(list_elem, i);
		if (elem == NULL) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		status = config_setting_lookup_int(elem, "if_index", (int *) &if_index);
		if (status == META_FALSE) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		status = config_setting_lookup_int(elem, "family", (int *) &family);
		if (status == META_FALSE) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		ip_elem = config_setting_get_member(elem, "ip");
		if (ip_elem == NULL) {
			PRINT_ERROR("todo error");
			exit(-1);
		}
		ip_num = config_setting_length(ip_elem);

		for (j = 0; j < ip_num; j++) {
			ip[j] = (uint32_t) config_setting_get_int_elem(ip_elem, j);
		}

		ip_elem = config_setting_get_member(elem, "mask");
		if (ip_elem == NULL) {
			PRINT_ERROR("todo error");
			exit(-1);
		}
		ip_num = config_setting_length(ip_elem);

		for (j = 0; j < ip_num; j++) {
			mask[j] = (uint32_t) config_setting_get_int_elem(ip_elem, j);
		}

		ip_elem = config_setting_get_member(elem, "gw");
		if (ip_elem == NULL) {
			PRINT_ERROR("todo error");
			exit(-1);
		}
		ip_num = config_setting_length(ip_elem);

		for (j = 0; j < ip_num; j++) {
			gw[j] = (uint32_t) config_setting_get_int_elem(ip_elem, j);
		}

		ip_elem = config_setting_get_member(elem, "bdc");
		if (ip_elem == NULL) {
			PRINT_ERROR("todo error");
			exit(-1);
		}
		ip_num = config_setting_length(ip_elem);

		for (j = 0; j < ip_num; j++) {
			bdc[j] = (uint32_t) config_setting_get_int_elem(ip_elem, j);
		}

		ip_elem = config_setting_get_member(elem, "dst");
		if (ip_elem == NULL) {
			PRINT_ERROR("todo error");
			exit(-1);
		}
		ip_num = config_setting_length(ip_elem);

		for (j = 0; j < ip_num; j++) {
			dst[j] = (uint32_t) config_setting_get_int_elem(ip_elem, j);
		}

		//############
		ifr = (struct if_record *) list_find1(overall->envi->if_list, ifr_index_test, &if_index);
		if (ifr != NULL) {
			if (ifr_running_test(ifr)) {
				if (family == AF_INET) {
					addr = (struct addr_record *) list_find(ifr->addr_list, addr_is_v4);
				} else {
					addr = (struct addr_record *) list_find(ifr->addr_list, addr_is_v6);
				}

				if (addr == NULL) {
					addr = (struct addr_record *) secure_malloc(sizeof(struct addr_record));
					addr->if_index = if_index;
					addr->family = AF_INET;

					if (family == AF_INET) {
						addr4_set_ip(&addr->ip, IP4_ADR_P2H(ip[0], ip[1], ip[2],ip[3]));
						addr4_set_ip(&addr->mask, IP4_ADR_P2H(mask[0], mask[1], mask[2],mask[3]));
						addr4_set_ip(&addr->gw, IP4_ADR_P2H(gw[0], gw[1], gw[2], gw[3]));
						addr4_set_ip(&addr->bdc, IP4_ADR_P2H(bdc[0], bdc[1], bdc[2], bdc[3]));
						addr4_set_ip(&addr->dst, IP4_ADR_P2H(dst[0], dst[1], dst[2], dst[3]));
					} else if (family == AF_INET6) {
						//TODO
						//addr_set_addr6(&addr->ip, ip);
						PRINT_WARN("todo");
					} else {
						//TODO error?
						PRINT_ERROR("todo error");
						exit(-1);
					}

					if (list_has_space(ifr->addr_list)) {
						PRINT_IMPORTANT(
								"Adding address: if_index=%d, family=%u, ip='%u.%u.%u.%u', mask='%u.%u.%u.%u', gw='%u.%u.%u.%u', bdc='%u.%u.%u.%u', dst='%u.%u.%u.%u'",
								if_index, family, ip[0], ip[1], ip[2], ip[3], mask[0], mask[1], mask[2], mask[3], gw[0], gw[1], gw[2], gw[3], bdc[0], bdc[1], bdc[2], bdc[3], dst[0], dst[1], dst[2], dst[3]);
						list_append(ifr->addr_list, addr);
					} else {
						//TODO error
						PRINT_ERROR("todo error");
						exit(-1);
					}
				} else {
					//TODO error
					PRINT_ERROR("todo: previous address found, replace or add new?");
				}
			} else {
				if (family == AF_INET) {
					PRINT_WARN(
							"Ignoring address, no active interface: if_index=%d, family=%u, ip='%u.%u.%u.%u', mask='%u.%u.%u.%u', gw='%u.%u.%u.%u', bdc='%u.%u.%u.%u', dst='%u.%u.%u.%u'",
							if_index, family, ip[0], ip[1], ip[2], ip[3], mask[0], mask[1], mask[2], mask[3], gw[0], gw[1], gw[2], gw[3], bdc[0], bdc[1], bdc[2], bdc[3], dst[0], dst[1], dst[2], dst[3]);
				} else if (family == AF_INET6) {
					//TODO
					PRINT_WARN("todo");
				} else {
					//TODO error?
					PRINT_ERROR("todo error");
					exit(-1);
				}
			}
		} else {
			//TODO error
			PRINT_ERROR("todo error");
			exit(-1);
		}
	}

	//############# route_list
	PRINT_IMPORTANT("############# Configuring List of Routes");
	overall->envi->route_list = list_create(MAX_ROUTES);

	list_elem = config_lookup(meta_envi, "environment.routes");
	if (list_elem == NULL) {
		PRINT_ERROR("todo error");
		exit(-1);
	}
	list_num = config_setting_length(list_elem);

	uint32_t metric; //SIOCGIFMETRIC
	uint32_t timeout;
	//struct timeval route_stamp;

	struct route_record *route;

	for (i = 0; i < list_num; i++) {
		elem = config_setting_get_elem(list_elem, i);
		if (elem == NULL) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		status = config_setting_lookup_int(elem, "if_index", (int *) &if_index);
		if (status == META_FALSE) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		status = config_setting_lookup_int(elem, "family", (int *) &family);
		if (status == META_FALSE) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		ip_elem = config_setting_get_member(elem, "dst");
		if (ip_elem == NULL) {
			PRINT_ERROR("todo error");
			exit(-1);
		}
		ip_num = config_setting_length(ip_elem);

		for (j = 0; j < ip_num; j++) {
			dst[j] = (uint32_t) config_setting_get_int_elem(ip_elem, j);
		}

		ip_elem = config_setting_get_member(elem, "mask");
		if (ip_elem == NULL) {
			PRINT_ERROR("todo error");
			exit(-1);
		}
		ip_num = config_setting_length(ip_elem);

		for (j = 0; j < ip_num; j++) {
			mask[j] = (uint32_t) config_setting_get_int_elem(ip_elem, j);
		}

		ip_elem = config_setting_get_member(elem, "gw");
		if (ip_elem == NULL) {
			PRINT_ERROR("todo error");
			exit(-1);
		}
		ip_num = config_setting_length(ip_elem);

		for (j = 0; j < ip_num; j++) {
			gw[j] = (uint32_t) config_setting_get_int_elem(ip_elem, j);
		}

		status = config_setting_lookup_int(elem, "metric", (int *) &metric);
		if (status == META_FALSE) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		status = config_setting_lookup_int(elem, "timeout", (int *) &timeout);
		if (status == META_FALSE) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		//############
		ifr = (struct if_record *) list_find1(overall->envi->if_list, ifr_index_test, &if_index);
		if (ifr != NULL) {
			if (ifr_running_test(ifr)) {
				route = (struct route_record *) secure_malloc(sizeof(struct route_record));
				route->if_index = if_index;
				route->family = family;

				if (family == AF_INET) {
					addr4_set_ip(&route->dst, IP4_ADR_P2H(dst[0], dst[1], dst[2], dst[3]));
					addr4_set_ip(&route->mask, IP4_ADR_P2H(mask[0], mask[1], mask[2],mask[3]));
					addr4_set_ip(&route->gw, IP4_ADR_P2H(gw[0], gw[1], gw[2], gw[3]));
					//addr4_set_addr(&route->ip, IP4_ADR_P2H(ip[0], ip[1], ip[2],ip[3]));
				} else if (family == AF_INET6) {
					//TODO
					//addr_set_addr6(&route->ip, ip);
				} else {
					//TODO error?
				}

				route->metric = metric;
				route->timeout = timeout;

				if (list_has_space(overall->envi->route_list)) {
					PRINT_IMPORTANT( "Adding route: if_index=%d, family=%u, dst='%u.%u.%u.%u', mask='%u.%u.%u.%u', gw='%u.%u.%u.%u', metric=%u, timeout=%u",
							route->if_index, route->family, dst[0], dst[1], dst[2], dst[3], mask[0], mask[1], mask[2], mask[3], gw[0], gw[1], gw[2], gw[3], metric, timeout);
					list_append(overall->envi->route_list, route);
				} else {
					//TODO error
					PRINT_ERROR("todo error");
					exit(-1);
				}
			} else {
				if (family == AF_INET) {
					PRINT_WARN(
							"Ignoring route, no active interface: if_index=%d, family=%u, dst='%u.%u.%u.%u', mask='%u.%u.%u.%u', gw='%u.%u.%u.%u', metric=%u, timeout=%u",
							if_index, family, dst[0], dst[1], dst[2], dst[3], mask[0], mask[1], mask[2], mask[3], gw[0], gw[1], gw[2], gw[3], metric, timeout);
				} else if (family == AF_INET6) {
					//TODO
					PRINT_WARN("todo");
				} else {
					//TODO error?
					PRINT_ERROR("todo error");
				}
			}
		}
	}
	PRINT_IMPORTANT("route_list: list=%p, max=%u, len=%u", overall->envi->route_list, overall->envi->route_list->max, overall->envi->route_list->len);
	metadata_destroy(meta_envi);

	//######################################################################
	PRINT_IMPORTANT("########################## loading stack: '%s'", (char *) stack_name);
	metadata *meta_stack = (metadata *) secure_malloc(sizeof(metadata));
	metadata_create(meta_stack);

	status = config_read_file(meta_stack, (char *) stack_name);
	if (status == META_FALSE) {
		PRINT_ERROR("file='%s', %s:%d - %s\n", stack_name, config_error_file(meta_stack), config_error_line(meta_stack), config_error_text(meta_stack));
		metadata_destroy(meta_stack);
		PRINT_ERROR("todo error");
		exit(-1);
	}

	//############# module_list
	PRINT_IMPORTANT("############# Configuring List of Modules");
	overall->lib_list = list_create(MAX_MODULES);
	memset(overall->modules, 0, MAX_MODULES * sizeof(struct fins_module *));
	overall->admin_list = list_create(MAX_MODULES);
	struct linked_list *mt_list = list_create(MAX_MODULES);

	uint8_t base_path[100];
	memset((char *) base_path, 0, 100);
#ifdef BUILD_FOR_ANDROID
	strcpy((char *) base_path, FINS_TMP_ROOT);
	//strcpy((char *) base_path, ".");
#else
	strcpy((char *) base_path, ".");
#endif

	metadata_element *mods_elem = config_lookup(meta_stack, "stack.modules");
	if (mods_elem == NULL) {
		PRINT_ERROR("todo error");
		exit(-1);
	}
	int mods_num = config_setting_length(mods_elem);

	metadata_element *mod_elem;
	uint32_t mod_id;
	uint8_t *mod_lib;
	uint8_t *mod_name;
	metadata_element *flows_elem;
	uint32_t mod_flows[MAX_MOD_FLOWS];
	uint32_t mod_flows_num;
	metadata_element *mod_params;
	metadata_element *mod_admin;

	struct fins_library *library;
	struct fins_module *module;
	struct fins_module_table *mt;

	for (i = 0; i < mods_num; i++) {
		mod_elem = config_setting_get_elem(mods_elem, i);
		if (mod_elem == NULL) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		status = config_setting_lookup_int(mod_elem, "id", (int *) &mod_id);
		if (status == META_FALSE) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		status = config_setting_lookup_string(mod_elem, "lib", (const char **) &mod_lib);
		if (status == META_FALSE) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		status = config_setting_lookup_string(mod_elem, "name", (const char **) &mod_name);
		if (status == META_FALSE) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		flows_elem = config_setting_get_member(mod_elem, "flows");
		if (flows_elem == NULL) {
			PRINT_ERROR("todo error");
			exit(-1);
		}
		mod_flows_num = config_setting_length(flows_elem);

		for (j = 0; j < mod_flows_num; j++) {
			mod_flows[j] = (uint32_t) config_setting_get_int_elem(flows_elem, j);
		}

		mod_params = config_setting_get_member(mod_elem, "params");
		if (mod_params == NULL) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		mod_admin = config_setting_get_member(mod_elem, "admin");
		PRINT_DEBUG("admin=%u", mod_admin != NULL);

		//############
		library = (struct fins_library *) list_find1(overall->lib_list, library_name_test, mod_lib);
		if (library == NULL) {
#ifdef BUILD_FOR_ANDROID
			library = library_fake_load(mod_lib, base_path);
#else
			//library = library_load(mod_lib, base_path);
			library = library_fake_load(mod_lib, base_path);
#endif
			if (library == NULL) {
				PRINT_ERROR("Failed in loading library: lib='%s', base_path='%s'", mod_lib, base_path);
				exit(-1);
			}

			if (list_has_space(overall->lib_list)) {
				PRINT_IMPORTANT("Adding library: library=%p, name='%s'", library, library->name);
				list_append(overall->lib_list, library);
			} else {
				PRINT_ERROR("Failed in init sequence, too many libraries: lib_list->len=%u", overall->lib_list->len);
				exit(-1);
			}
		}

		module = library->create(i, mod_id, mod_name);
		if (module == NULL) {
			//TODO error
			PRINT_ERROR("Failed to create module: library=%p, index=%u, id=%u, name='%s'", library, i, mod_id, mod_name);
			exit(-1);
		}
		library->num_mods++;

		//TODO move flow to update? or links here?
		status = module->ops->init(module, mod_params, overall->envi); //TODO merge init into create?
		if (status != 0) {
			overall->modules[i] = module;

			if (module->flows_max < mod_flows_num) {
				PRINT_ERROR("Loading module parameters failed, too many flows for this library: specified=%u, max=%u", mod_flows_num, module->flows_max);
				exit(-1);
			}

			mt = (struct fins_module_table *) secure_malloc(sizeof(struct fins_module_table));
			mt->flows_num = mod_flows_num;

			for (j = 0; j < mt->flows_num; j++) {
				mt->flows[j].link_id = mod_flows[j];
			}
			list_append(mt_list, mt);

			if (mod_admin != NULL) {
				PRINT_IMPORTANT("Adding admin module: module=%p, lib='%s', name='%s', id=%d, index=%u",
						module, module->lib, module->name, module->id, module->index);
				list_append(overall->admin_list, module);
			} else {
				PRINT_IMPORTANT("Adding module: module=%p, lib='%s', name='%s', id=%d, index=%u", module, module->lib, module->name, module->id, module->index);
			}
		} else {
			PRINT_ERROR("Initialization of module failed: module=%p, lib='%s', name='%s', flows_num=%u, flows=%p, params=%p, envi=%p",
					module, module->lib, module->name, mod_flows_num, mod_flows, mod_params, overall->envi);
			exit(-1);
		}

		//free(mod_lib); //don't free, string from libconfig points to metadata memory
		//free(mod_name);
	}

	//############# admin_list //TODO change to admin_list?
	list_for_each1(overall->admin_list, assign_overall, overall);

	//############# linking_list
	PRINT_IMPORTANT("############# Configuring Linking Table");
	overall->link_list = list_create(MAX_TABLE_LINKS);

	metadata_element *links_elem = config_lookup(meta_stack, "stack.links");
	if (links_elem == NULL) {
		PRINT_ERROR("todo error");
		exit(-1);
	}
	int links_num = config_setting_length(links_elem);

	metadata_element *link_elem;
	uint32_t link_id;
	uint32_t link_src;
	metadata_element *dsts_elem;
	uint32_t link_dsts[MAX_MODULES];
	int link_dsts_num;

	struct link_record *link;

	for (i = 0; i < links_num; i++) {
		link_elem = config_setting_get_elem(links_elem, i);
		if (link_elem == NULL) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		status = config_setting_lookup_int(link_elem, "id", (int *) &link_id);
		if (status == META_FALSE) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		status = config_setting_lookup_int(link_elem, "src", (int *) &link_src);
		if (status == META_FALSE) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		dsts_elem = config_setting_get_member(link_elem, "dsts");
		if (dsts_elem == NULL) {
			PRINT_ERROR("todo error");
			exit(-1);
		}
		link_dsts_num = config_setting_length(dsts_elem);

		for (j = 0; j < link_dsts_num; j++) {
			link_dsts[j] = (uint32_t) config_setting_get_int_elem(dsts_elem, j);
		}

		//############
		link = (struct link_record *) secure_malloc(sizeof(struct link_record));
		link->id = link_id;

		//module = (struct fins_module *) list_find1(overall->envi->module_list, mod_id_test, &link_src);
		link->src_index = -1;
		for (j = 0; j < MAX_MODULES; j++) {
			if (overall->modules[j] != NULL && overall->modules[j]->id == link_src) {
				link->src_index = overall->modules[j]->index;
			}
		}
		if (link->src_index == -1) {
			PRINT_ERROR("todo error");
			exit(-1);
		}

		link->dsts_num = link_dsts_num;
		for (j = 0; j < link_dsts_num; j++) {
			//module = (struct fins_module *) list_find1(overall->envi->module_list, mod_id_test, &link_dsts[j]);
			link->dsts_index[j] = -1;
			for (k = 0; k < MAX_MODULES; k++) {
				if (overall->modules[k] != NULL && overall->modules[k]->id == link_dsts[j]) {
					link->dsts_index[j] = overall->modules[k]->index;
				}
			}
			if (link->dsts_index[j] == (uint32_t) -1) {
				PRINT_ERROR("todo error");
				exit(-1);
			}
		}

		if (list_has_space(overall->link_list)) {
			uint8_t buf[1000];
			uint8_t *pt = buf;
			int ret;
			int i;
			for (i = 0; i < link->dsts_num; i++) {
				ret = sprintf((char *) pt, "%u, ", link->dsts_index[i]);
				pt += ret;
			}
			*pt = '\0';

			PRINT_IMPORTANT("Adding link: link=%p, id=%u, src_index=%u, dsts_num=%u, ['%s']", link, link->id, link->src_index, link->dsts_num, buf);
			list_append(overall->link_list, link);
		} else {
			//TODO error
			PRINT_ERROR("todo error");
			exit(-1);
		}
	}
	metadata_destroy(meta_stack);

	//######################################################################
	PRINT_IMPORTANT("############# Updating modules with correct flows & links");
	//send out subset of linking table to each module as update
	//TODO table subset update

	metadata *meta_update;
	struct finsFrame *ff_update;

	for (i = 0; i < MAX_MODULES; i++) {
		if (overall->modules[i] != NULL) {
			mt = (struct fins_module_table *) list_remove_front(mt_list);
			mt->link_list = list_filter1(overall->link_list, link_src_test, &overall->modules[i]->index, link_clone); //was link_involved_test, decide which better?
			PRINT_IMPORTANT("Module link table subset: name='%s' index=%d, link_list=%p, len=%d",
					overall->modules[i]->name, i, mt->link_list, mt->link_list->len);

			for (j = 0; j < mt->flows_num; j++) {
				mt->flows[j].link = (struct link_record *) list_find1(mt->link_list, link_id_test, &mt->flows[j].link_id);
			}

//#ifdef DEBUG
			uint8_t buf[1000];
			uint8_t *pt = buf;
			int ret;
			for (j = 0; j < mt->flows_num; j++) {
				ret = sprintf((char *) pt, "%u (%p), ", mt->flows[j].link_id, mt->flows[j].link);
				pt += ret;
			}
			*pt = '\0';
			PRINT_IMPORTANT("Module flows: num=%u, ['%s']", mt->flows_num, buf);

			list_for_each(mt->link_list, link_print);
//#endif

			meta_update = (metadata *) secure_malloc(sizeof(metadata));
			metadata_create(meta_update);

			ff_update = (struct finsFrame*) secure_malloc(sizeof(struct finsFrame));
			ff_update->dataOrCtrl = FF_CONTROL;
			ff_update->destinationID = i;
			ff_update->metaData = meta_update;

			ff_update->ctrlFrame.sender_id = 0;
			ff_update->ctrlFrame.serial_num = gen_control_serial_num();
			ff_update->ctrlFrame.opcode = CTRL_SET_PARAM;
			ff_update->ctrlFrame.param_id = MOD_SET_PARAM_DUAL;

			ff_update->ctrlFrame.data_len = sizeof(struct fins_module_table);
			ff_update->ctrlFrame.data = (uint8_t *) mt;

			module_to_switch(overall->modules[0], ff_update);
			//module_set_param_dual(overall->modules[i], ff_update);
		}
	}
	list_free(mt_list, free);

	//############ say by this point envi var completely init'd
	//assumed always connect/init to switch first

	pthread_attr_init(&overall->attr);
	pthread_attr_setdetachstate(&overall->attr, PTHREAD_CREATE_JOINABLE);

	PRINT_IMPORTANT("############# Calling run() for modules");

	for (i = 0; i < MAX_MODULES; i++) {
		if (overall->modules[i] != NULL) {
			overall->modules[i]->ops->run(overall->modules[i], &overall->attr);
		}
	}

	PRINT_IMPORTANT("Core Initiation: Finished ************");
}
Пример #13
0
/*=============================================================================
Function main

Purpose: main entry point of program, processes input and starts search
          
Parameters:
    argc (IN) - count of command line arguments
    *argv (IN) - array of pointers to command line arguments
        
Returns: nothing
=============================================================================*/
int main(int argc, char *argv[]) {
  int c;
  char *p;
  struct url_entry *ue; /* used for processing url_entries */
  /* first process command line optional arguments */
  while(--argc>0 && (*++argv)[0]=='-')
    while((c = *++argv[0]))
      switch(c) {
        case 'i':
          case_independent = 1;
          break;
        case 'l':
          only_show_url = 1;
          break;
        case 'v':
          show_non_matching = 1;
          break;
        case 'd':
          if (*++argv[0]!='=') {
            fprintf(stderr,"Illegal depth value, argument will be ignored\n");
            break;
          }
          search_depth = strtol(++argv[0],&p,0);
          if (search_depth<0 || errno==ERANGE || p==argv[0]) {
            fprintf(stderr,"Illegal depth value, argument will be ignored\n");
            search_depth = DEFAULT_SEARCH_DEPTH;
            argv[0] = p-1; /* above ++ will point to first unconverted */
            break;
          }
          argv[0] = p-1; /* so above ++ will point to first unconverted */
          break;
        case 't':
          if (*++argv[0]!='=') {
            fprintf(stderr,"Illegal connect timeout value, "
                           "argument will be ignored\n");
            break;
          }
          connect_timeout = strtol(++argv[0],&p,0);
          if (connect_timeout<0 || errno==ERANGE || p==argv[0]) {
            fprintf(stderr,"Illegal connect timeout value, "
                           "argument will be ignored\n");
            connect_timeout = DEFAULT_CONNECT_TIMEOUT;
            argv[0] = p-1; /* above ++ will point to first unconverted */
            break;
          }
          argv[0] = p-1; /* so above ++ will point to first unconverted */
          break;
        default:
          fprintf(stderr,"Unknown option %c ignored\n",c);
      }

  /* now process pattern URL+ if they are present */
  if (argc<2) {
    fprintf(stderr, "Usage: websearch [OPTION...] pattern URL+\n"
                    "valid options:\n"
                    "  -i\tspecifies case-independent matching\n"
                    "  -l\tspecifies show matching URLs, not lines\n"
                    "  -v\tspecifies show non-matching lines or URLs\n"
                    "  -d=N\tspecifies search depth, default = 0\n"
                    "  -t=N\tspecifies timeout in secs, default = 0 (no timeout)\n");
    exit(1);
  }

  pattern = argv[0]; /* store pattern */

  url_entries = list_init(); /* create url entries list */
  url_trie = trie_init(); /* create url trie */

  /* add all URLs to the URL entries with depth 0 */
  while (*++argv) {
    ue = url_entry_init(argv[0],0);
    list_insert_back(url_entries,ue);
  }

#ifdef DEBUG
  printf("Case independence: %d\n",case_independent);
  printf("Only show url:     %d\n",only_show_url);
  printf("Show non mathcing: %d\n",show_non_matching);
  printf("Search depth:      %d\n",search_depth);
  printf("Connect timeout:   %d\n",connect_timeout);
  printf("Pattern:           %s\n",pattern);
  printf("URL Count:         %d\n",url_entries->size);
#endif

  /* keep processing an entry at a time until the list is empty */
  while(url_entries->size>0) {
    ue = list_remove_front(url_entries);
    process_url_entry(ue);
    url_entry_free(ue);
  }

  return 0;

}
Пример #14
0
list* search_a_star(void* state,
                    void* state_world,
                    search_is_goal state_goal_func,
                    search_gen_successors state_gen_func,
                    search_link_parent state_link_func,
                    search_goal_backtrace state_back_func,
                    search_trans_cost state_trans_func,
                    search_heuristic state_heur_func,
                    search_set_f_cost state_f_cost_set_func,
                    hash_func state_hash_alg,
                    generic_comp state_comp_func,
                    generic_cpy state_copy_func,
                    generic_op state_free_func,
                    heap_comp state_heap_func) {
    int* g_cost_ptr, *f_cost_ptr, f_cost, tmp_f, g_cost, found;
    void* current_state, *successor_state, *heap_memory_location;
    list* states_overflow, *successor_list, *path;
    hash_table* states_closed_set, *states_open_set;
    hash_map* states_g_cost, *states_f_cost, *states_heap_index;
    heap* states_heap;

    states_overflow = list_create(NULL,
                                  NULL,
                                  state_free_func);

    states_closed_set = hash_table_create(89,
                                          .75,
                                          state_hash_alg,
                                          state_comp_func,
                                          state_copy_func,
                                          state_free_func);

    states_open_set = hash_table_create(89,
                                        .75,
                                        state_hash_alg,
                                        state_comp_func,
                                        state_copy_func,
                                        state_free_func);

    states_g_cost = hash_map_create(89,
                                    .75,
                                    state_hash_alg,
                                    state_comp_func,
                                    NULL,
                                    NULL,
                                    NULL,
                                    state_free_func,
                                    (generic_op)free);

    states_f_cost = hash_map_create(89,
                                    .75,
                                    state_hash_alg,
                                    state_comp_func,
                                    NULL,
                                    NULL,
                                    NULL,
                                    state_free_func,
                                    (generic_op)free);

    states_heap_index = hash_map_create(89,
                                        .75,
                                        state_hash_alg,
                                        state_comp_func,
                                        NULL,
                                        NULL,
                                        NULL,
                                        NULL,
                                        NULL);

    states_heap = heap_create(89,
                              state_heap_func,
                              state_comp_func,
                              state_copy_func,
                              state_free_func);
    current_state = state;
    f_cost = state_heur_func(current_state, NULL);
    state_f_cost_set_func(current_state, f_cost);
    g_cost = 0;
    g_cost_ptr = malloc(sizeof(int));
    *g_cost_ptr = g_cost;
    f_cost_ptr = malloc(sizeof(int));
    *f_cost_ptr = f_cost;
    hash_map_insert(states_g_cost, current_state, g_cost_ptr, 0);
    heap_memory_location = heap_add(states_heap, state_copy_func(current_state));
    hash_table_insert(states_open_set, state_copy_func(current_state), 0);
    hash_map_insert(states_f_cost, state_copy_func(current_state), f_cost_ptr, 0);
    hash_map_insert(states_heap_index, current_state, heap_memory_location, 1);
    path = NULL;
    found = 0;
    while(!heap_is_empty(states_heap) && !found) {
        current_state = state_copy_func(heap_peek(states_heap));
        heap_remove(states_heap);
        hash_table_remove(states_open_set, current_state);
        hash_map_remove(states_heap_index, current_state);
        if(state_goal_func(current_state, state_world)) {
            path = state_back_func(current_state);
            found = 1;
        } else {
            if(!hash_table_insert(states_closed_set, current_state, 0)) {
                list_push_front(states_overflow, current_state);
            }
            successor_list = state_gen_func(current_state, state_world);
            while(!list_is_empty(successor_list)) {
                successor_state = list_front(successor_list);
                g_cost = *(int*)hash_map_get(states_g_cost, current_state) +
                    state_trans_func(current_state, successor_state, state_world);
                f_cost = g_cost + state_heur_func(successor_state, state_world);
                tmp_f = hash_map_contains_key(states_f_cost, successor_state) ?
                    *(int*)hash_map_get(states_f_cost, successor_state) : UINT_MAX;
                if(hash_table_contains(states_closed_set, successor_state) && f_cost > tmp_f) {
                    list_remove_front(successor_list);
                    continue;
                }
                if(!hash_table_contains(states_open_set, successor_state) || f_cost < tmp_f) {
                    state_f_cost_set_func(successor_state, f_cost);
                    state_link_func(successor_state, current_state);
                    g_cost_ptr = malloc(sizeof(int));
                    f_cost_ptr = malloc(sizeof(int));
                    *g_cost_ptr = g_cost;
                    *f_cost_ptr = f_cost;
                    if(!hash_table_contains(states_open_set, successor_state)) {
                        hash_table_insert(states_open_set, successor_state, 0);
                        heap_memory_location = heap_add(states_heap, state_copy_func(successor_state));
                        hash_map_insert(states_heap_index, successor_state,  heap_memory_location, 1);
                    } else {
                        heap_memory_location = hash_map_get(states_heap_index, successor_state);
                        heap_up_mod_data(states_heap, heap_memory_location,  successor_state);
                    }
                    if(!hash_map_set(states_g_cost, successor_state, g_cost_ptr)) {
                        hash_map_insert(states_g_cost, state_copy_func(successor_state), g_cost_ptr, 0);
                    }
                    if(!hash_map_set(states_f_cost, successor_state, f_cost_ptr)) {
                        hash_map_insert(states_f_cost, state_copy_func(successor_state), f_cost_ptr, 0);
                    }
                    list_pop(successor_list);
                } else {
                    list_remove_front(successor_list);
                }
            }
            list_kill(successor_list);
        }
    }
    heap_kill(states_heap);
    list_kill(states_overflow);
    hash_map_kill(states_g_cost);
    hash_map_kill(states_f_cost);
    hash_table_kill(states_open_set);
    hash_table_kill(states_closed_set);
    hash_map_dissolve(states_heap_index);
    return path;
}
Пример #15
0
void udp_out_fdf(struct fins_module *module, struct finsFrame* ff) {
	struct udp_data *md = (struct udp_data *) module->data;
	//struct udp_metadata_parsed parsed_meta;

	//struct udp_packet packet_host;
	struct udp_packet *packet_netw;
	uint16_t packet_length;

	/* read the FDF and make sure everything is correct*/
	PRINT_DEBUG("UDP_out, ff=%p, meta=%p", ff, ff->metaData);

	//print_finsFrame(ff);

	packet_length = ff->dataFrame.pduLength + U_HEADER_LEN;

	if (packet_length > IP_MAXLEN) {
		PRINT_ERROR("todo error, data too long max 65536, len=%d", packet_length);
	}

	uint8_t *udp_dataunit = (uint8_t *) secure_malloc(packet_length);
	packet_netw = (struct udp_packet *) udp_dataunit;
	uint8_t *pdu = ff->dataFrame.pdu;

	/** constructs the UDP packet from the FDF and the meta data */

	//#########################
#ifdef DEBUG
	if (1) {
		uint8_t *temp = (uint8_t *) secure_malloc(ff->dataFrame.pduLength + 1);
		memcpy(temp, pdu, ff->dataFrame.pduLength);
		temp[ff->dataFrame.pduLength] = '\0';
		PRINT_DEBUG("pduLen=%d, pdu='%s'", ff->dataFrame.pduLength, temp);
		free(temp);
	}
#endif
	//#########################

	uint32_t dst_port;
	uint32_t src_port;

	uint32_t dst_ip;
	uint32_t src_ip;

	uint32_t family;
	secure_metadata_readFromElement(ff->metaData, "send_family", &family);
	secure_metadata_readFromElement(ff->metaData, "send_src_ipv4", &src_ip);
	secure_metadata_readFromElement(ff->metaData, "send_src_port", &src_port);
	secure_metadata_readFromElement(ff->metaData, "send_dst_ipv4", &dst_ip);
	secure_metadata_readFromElement(ff->metaData, "send_dst_port", &dst_port);

	uint32_t protocol = UDP_PROTOCOL;
	secure_metadata_writeToElement(ff->metaData, "send_protocol", &protocol, META_TYPE_INT32);

	/** fixing the values because of the conflict between uint16 type and
	 * the 32 bit META_INT_TYPE
	 */

	//packet_host.u_src = srcbuf16;
	//packet_host.u_dst = dstbuf16;
	//packet_host.u_len = packet_length;
	//packet_host.u_cksum = 0;
	packet_netw->u_src = htons((uint16_t) src_port);
	packet_netw->u_dst = htons((uint16_t) dst_port);
	packet_netw->u_len = htons(packet_length);
	packet_netw->u_cksum = 0;
	memcpy(packet_netw->u_data, pdu, ff->dataFrame.pduLength);

	PRINT_DEBUG("src=%u:%u, dst=%u:%u, pkt_len=%u", src_ip, (uint16_t)src_port, dst_ip, (uint16_t)dst_port, packet_length);

	uint16_t checksum = UDP_checksum(packet_netw, htonl(src_ip), htonl(dst_ip));
	packet_netw->u_cksum = htons(checksum);
	//packet_netw->u_cksum = 0;
	PRINT_DEBUG("checksum (h):0x%x", checksum);

	//PRINT_DEBUG("%u,%u", src_ip, dst_ip);

	PRINT_DEBUG("pkt_netw: %d,%d,%d,0x%x", packet_netw->u_src, packet_netw->u_dst, packet_netw->u_len, packet_netw->u_cksum);

	//ff->dataFrame.pdu = udp_dataunit;
	/* creates a new FDF to be sent out */
	PRINT_DEBUG("%p", udp_dataunit);

	ff->dataFrame.pduLength = packet_length;
	ff->dataFrame.pdu = udp_dataunit;

	md->stats.totalSent++;

	struct finsFrame *ff_clone = cloneFinsFrame(ff);

	//TODO add switch & move between ipv4/ipv6
	if (!module_send_flow(module, ff, UDP_FLOW_IPV4)) {
		PRINT_ERROR("send to switch error, ff=%p", ff);
		freeFinsFrame(ff);
	}
	struct udp_sent *sent = udp_sent_create(ff_clone, src_ip, src_port, dst_ip, dst_port);

	if (list_has_space(md->sent_packet_list)) {
		list_append(md->sent_packet_list, sent);
		PRINT_DEBUG("sent_packet_list=%p, len=%u, max=%u", md->sent_packet_list, md->sent_packet_list->len, md->sent_packet_list->max);

		gettimeofday(&sent->stamp, 0);
	} else {
		//PRINT_DEBUG("Clearing sent_packet_list");
		//udp_sent_list_gc(udp_sent_packet_list, UDP_MSL_TO_DEFAULT);

		//if (!udp_sent_list_has_space(udp_sent_packet_list)) {
		PRINT_DEBUG("Dropping head of sent_packet_list");
		struct udp_sent *old = (struct udp_sent *) list_remove_front(md->sent_packet_list);
		udp_sent_free(old);
		//}
		list_append(md->sent_packet_list, sent);
		PRINT_DEBUG("sent_packet_list=%p, len=%u, max=%u", md->sent_packet_list, md->sent_packet_list->len, md->sent_packet_list->max);

		gettimeofday(&sent->stamp, 0);
	}

	PRINT_DEBUG("Freeing: pdu=%p", pdu);
	free(pdu);
}