Ejemplo n.º 1
0
int krip_init(dictionary *conf)
{
	char *svrname;
	char *vip, *ip, *port, *if_name;
	char *p;
	char *c;
	char file[1024], buf[512];
	FILE *fp;
	struct sockaddr_in sin;
	int listen;

	/* Parse the configuration file. ("HOSTNAME_krip" in the current working directory) */
	svrname = iniparser_getstring(conf, "KENS:server_name", "KENS");

	sprintf(file, "%s_krip", svrname);
	fp = fopen(file, "r");
	if (fp == NULL) {
		_enabled = 0;
		return 0;
	}

	/* Initialize system parameters. */
	_update_time = krip_get_mtime();

	c = iniparser_getstring(conf,"KENS:krip_update_interval","3000");
	_update_interval = atoi(c);

	c = iniparser_getstring(conf,"KENS:krip_timeout","7000");
	_timeout = atoi(c);

	_rip_info_list = list_open();
	_neighbor_info_list = list_open();

	/* Create a UDP socket for RIP communication. */
	if ((_sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
		perror("krip");
		return -1;
	}

	_enabled = 1;

	/* configuration example:
	 * listen 127.0.0.1:9501
	 * 10.1.0.1 seth0 127.0.0.1:9502
	 * 10.1.1.1 seth1 127.0.0.1:9503
	 */

	while (fgets(buf, 512, fp)) {
		p = strchr(buf, '#');
		if (p != NULL) *p = '\0';
		if (buf[strlen(buf) - 1] == '\n') buf[strlen(buf) - 1] = '\0';
		
		p = buf;
		p = eat_ws(p);
		if (p == NULL) continue;

		if (strncmp(buf, "listen", 6) == 0) {
			listen = 1;

			p += 6;
			p = eat_ws(p);
		}
		else {
			listen = 0;
			
			vip = p;
			p = eat_ipaddr(vip);
			*p++ = '\0';
			p = eat_ws(p);

			if_name = p;
			p = eat_alphanum(if_name);
			*p++ = '\0';
			p = eat_ws(p);
		}

		ip = p;
		p = eat_ipaddr(ip);
		*p++ = '\0';
		port = p;
		p = eat_digit(port);

		if (listen)
		{
			/* Setup the RIP listening socket. */
			L_ROUTE("krip_init(): bind to %s:%s", ip, port);
			sin.sin_family = AF_INET;
			inet_aton(ip, &sin.sin_addr);
			sin.sin_port = htons((in_port_t)atoi(port));
			if (bind(_sock, (struct sockaddr *)&sin, sizeof(sin))) {
				perror("krip");
				return -1;
			}
		}
		else
		{
			/* Setup the neighbor information for RIP clients. */
			L_ROUTE("krip_init(): register neighbor %s(%s:%s)", vip, ip, port);
			neighbor_info *ni = (neighbor_info *)malloc(sizeof(neighbor_info));
			inet_aton(vip, &ni->virtual_addr);
			ni->krip_addr.sin_family = AF_INET;
			inet_aton(ip, &ni->krip_addr.sin_addr);
			ni->krip_addr.sin_port = htons((in_port_t)atoi(port));
			ni->ifp = ifunit(if_name);
			if (!ni->ifp) {
				L_ROUTE("krip_init(): invalid interface name: %s", if_name);
				free(ni);
				continue;
			}

			list_add_tail(_neighbor_info_list, ni);
		}
	}

	fclose(fp);

	/* Fetch the routing table entries. */
	list rte_list = rt_query();
	list_position pos;
	uint32_t now = krip_get_mtime();
	for (pos = list_get_head_position(rte_list);
			pos; pos = list_get_next_position(pos)) {
		rtentry *rte = list_get_at(pos);
		for (; rte; rte = (rtentry *)((radix_node *)rte)->rn_dupedkey) {
			if (((radix_node *)rte)->rn_mask == NULL)
				continue;
			if (rte->dst.s_addr == 0x00000000) {
				L_ROUTE("krip_init(): default gw %s", inet_ntoa(rte->gw));
			}
			else if (rte->dst.s_addr == inet_addr("127.0.0.1")) {
			}
			else {
				L_ROUTE("krip_init(): dst %s", inet_ntoa(rte->dst));
				L_ROUTE("krip_init(): mask %s", inet_ntoa(rte->mask));
				L_ROUTE("krip_init(): gw %s", inet_ntoa(rte->gw));

				rip_info *ri = (rip_info *)malloc(sizeof(rip_info));
				ri->assoc_rte = rte;
				ri->metric = 1;
				ri->change_flag = 1;
				ri->timeout = 0;		/* The initial entries will not be expired. */
				ri->from = NULL;
				list_add_tail(_rip_info_list, ri);
			}
		}
	}

	/* Send the initial request packets */
	pos = list_get_head_position(_neighbor_info_list);
	for (; pos; pos = list_get_next_position(pos)) {
		neighbor_info *ni = list_get_at(pos);
		krip_send_request(ni);
	}

	return 0;
}
Ejemplo n.º 2
0
static void test_symtab( void )
{
	Symbol *sym;
	SymbolHash *symtab, *symtab2;
	
	list_open("test.lst");
	opts.symtable = TRUE;
	opts.list     = TRUE;
	
	warn("Create current module\n");	
	set_cur_module( new_module() );

	warn("Create symbol\n");	
	sym = Symbol_create(S("Var1"), 123, TYPE_CONSTANT, 0, NULL, NULL);
	dump_Symbol(sym);
	OBJ_DELETE(sym);

	sym = Symbol_create(S("Var1"), 123, TYPE_CONSTANT, 0, CURRENTMODULE, NULL);
	dump_Symbol(sym);
	CURRENTMODULE->modname = "MODULE";
	dump_Symbol(sym);
	
	warn("Delete symbol\n");	
	OBJ_DELETE(sym);
	
	warn("Global symtab\n");	
	dump_SymbolHash(global_symtab, "global");
	dump_SymbolHash(static_symtab, "static");
	
	warn("check case insensitive - CH_0024\n");
	symtab = OBJ_NEW(SymbolHash);
	assert( symtab );
	_define_sym(S("Var1"), 1, TYPE_CONSTANT, 0, NULL, NULL, &symtab); inc_page_nr();
	_define_sym(S("var1"), 2, TYPE_CONSTANT, 0, NULL, NULL, &symtab); inc_page_nr(); 
	_define_sym(S("VAR1"), 3, TYPE_CONSTANT, 0, NULL, NULL, &symtab); inc_page_nr();
	dump_SymbolHash(symtab, "tab1");
	
	assert( find_symbol(S("Var1"), symtab)->value == 1 );
	assert( find_symbol(S("var1"), symtab)->value == 2 );
	assert( find_symbol(S("VAR1"), symtab)->value == 3 );

	dump_SymbolHash(symtab, "tab1");
	
	warn("Concat symbol tables\n");	
	symtab = OBJ_NEW(SymbolHash);
	assert( symtab );
	_define_sym(S("Var1"),  1, TYPE_CONSTANT, 0, NULL, NULL, &symtab); inc_page_nr();
	_define_sym(S("Var2"),  2, TYPE_CONSTANT, 0, NULL, NULL, &symtab); inc_page_nr(); 
	_define_sym(S("Var3"), -3, TYPE_CONSTANT, 0, NULL, NULL, &symtab); inc_page_nr();
	dump_SymbolHash(symtab, "tab1");
	
	symtab2 = OBJ_NEW(SymbolHash);
	assert( symtab2 );
	_define_sym(S("Var3"), 3, TYPE_CONSTANT, 0, NULL, NULL, &symtab2); inc_page_nr();
	_define_sym(S("Var4"), 4, TYPE_CONSTANT, 0, NULL, NULL, &symtab2); inc_page_nr();
	_define_sym(S("Var5"), 5, TYPE_CONSTANT, 0, NULL, NULL, &symtab2); inc_page_nr();
	dump_SymbolHash(symtab2, "tab2");
	
	SymbolHash_cat( &symtab, symtab2 );
	dump_SymbolHash(symtab, "merged_tab");
	
	OBJ_DELETE( symtab );
	OBJ_DELETE( symtab2 );
	
	warn("Sort\n");	
	symtab = OBJ_NEW(SymbolHash);
	assert( symtab );
	_define_sym(S("One"), 	1, TYPE_CONSTANT, 0, NULL, NULL, &symtab); inc_page_nr();
	_define_sym(S("Two"),	2, TYPE_CONSTANT, 0, NULL, NULL, &symtab); inc_page_nr(); 
	_define_sym(S("Three"),	3, TYPE_CONSTANT, 0, NULL, NULL, &symtab); inc_page_nr();
	_define_sym(S("Four"),	4, TYPE_CONSTANT, 0, NULL, NULL, &symtab); inc_page_nr();
	dump_SymbolHash(symtab, "tab");
	
	SymbolHash_sort(symtab, SymbolHash_by_name);
	dump_SymbolHash(symtab, "tab by name");

	SymbolHash_sort(symtab, SymbolHash_by_value);
	dump_SymbolHash(symtab, "tab by value");

	OBJ_DELETE( symtab );

	warn("Use local symbol before definition\n");
	_define_sym(S("WIN32"), 1, TYPE_CONSTANT, 0, NULL, NULL, &static_symtab); inc_page_nr();
	SymbolHash_cat( & CURRENTMODULE->local_symtab, static_symtab ); inc_page_nr();
	_define_sym(S("PC"), 0, TYPE_CONSTANT, 0, NULL, NULL, &global_symtab); inc_page_nr();
	find_symbol( S("PC"), global_symtab )->value += 3; inc_page_nr();
	find_symbol( S("PC"), global_symtab )->value += 3; inc_page_nr();
	sym = get_used_symbol(S("NN")); inc_page_nr();
	assert( sym != NULL );
	assert( ! sym->is_defined );
	find_symbol( S("PC"), global_symtab )->value += 3; inc_page_nr();
	sym = get_used_symbol(S("NN")); inc_page_nr();
	assert( sym != NULL );
	assert( ! sym->is_defined );
	find_symbol( S("PC"), global_symtab )->value += 3; inc_page_nr();
	sym = define_symbol(S("NN"), find_symbol( "PC", global_symtab )->value, TYPE_ADDRESS); 
	sym->is_touched = TRUE;
	sym = get_used_symbol(S("NN")); inc_page_nr();
	assert( sym != NULL );
	dump_Symbol(sym);
	assert( sym->is_defined );
	
	dump_symtab();

	warn("Delete Local\n");	
	remove_all_local_syms();
	dump_symtab();
	
	warn("Delete Static\n");	
	remove_all_static_syms();
	dump_symtab();
	
	warn("Delete Global\n");	
	remove_all_global_syms();
	dump_symtab();
	
	warn("End\n");	

}