metadata_t * fa_metadata_from_fctx(AVFormatContext *fctx, const char *url) { metadata_t *md = metadata_create(); fa_lavf_load_meta(md, fctx, url, NULL); return md; }
void module_create_structs(struct fins_module *module) { PRINT_DEBUG("Entered: module=%p, id=%d, name='%s'", module, module->id, module->name); char buf[MOD_NAME_SIZE + 10]; sprintf(buf, "switch_to_%s", module->name); module->input_queue = init_queue(buf, MAX_QUEUE_SIZE); module->input_sem = (sem_t *) secure_malloc(sizeof(sem_t)); sem_init(module->input_sem, 0, 1); sprintf(buf, "%s_to_switch", module->name); module->output_queue = init_queue(buf, MAX_QUEUE_SIZE); module->output_sem = (sem_t *) secure_malloc(sizeof(sem_t)); sem_init(module->output_sem, 0, 1); module->event_sem = (sem_t *) secure_malloc(sizeof(sem_t)); sem_init(module->event_sem, 0, 0); module->knobs = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(module->knobs); /*metadata_element *root = config_root_setting(module->knobs); secure_config_setting_add(root, OP_EXEC_STR, META_TYPE_GROUP); secure_config_setting_add(root, OP_GET_STR, META_TYPE_GROUP); secure_config_setting_add(root, OP_SET_STR, META_TYPE_GROUP); secure_config_setting_add(root, OP_LISTEN_STR, META_TYPE_GROUP); */ }
metadata_t * fa_probe_metadata(const char *url, char *errbuf, size_t errsize, const char *filename) { AVFormatContext *fctx; fa_handle_t *fh = fa_open_ex(url, errbuf, errsize, FA_BUFFERED_SMALL, NULL); if(fh == NULL) return NULL; metadata_t *md = metadata_create(); #if ENABLE_LIBGME if(gme_probe(md, url, fh)) return md; #endif #if ENABLE_XMP if(xmp_probe(md, url, fh)) return md; #endif fa_seek(fh, 0, SEEK_SET); if(fa_probe_header(md, url, fh, filename)) { fa_close(fh); return md; } AVIOContext *avio = fa_libav_reopen(fh); if((fctx = fa_libav_open_format(avio, url, errbuf, errsize, NULL)) == NULL) { fa_libav_close(avio); metadata_destroy(md); return NULL; } fa_lavf_load_meta(md, fctx, filename); fa_libav_close_format(fctx); return md; }
/** * Probe a directory */ metadata_t * fa_probe_dir(const char *url) { metadata_t *md = metadata_create(); char path[URL_MAX]; struct fa_stat fs; md->md_contenttype = CONTENT_DIR; fa_pathjoin(path, sizeof(path), url, "VIDEO_TS"); if(fa_stat(path, &fs, NULL, 0) == 0 && fs.fs_type == CONTENT_DIR) { md->md_contenttype = CONTENT_DVD; return md; } fa_pathjoin(path, sizeof(path), url, "video_ts"); if(fa_stat(path, &fs, NULL, 0) == 0 && fs.fs_type == CONTENT_DIR) { md->md_contenttype = CONTENT_DVD; return md; } return md; }
void core_main() { PRINT_IMPORTANT("Entered"); register_to_signal(SIGRTMIN); sem_init(&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"); metadata *meta_envi = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta_envi); status = config_read_file(meta_envi, "envi.cfg"); if (status == META_FALSE) { PRINT_ERROR("%s:%d - %s\n", 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("interface list"); 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); uint32_t if_index; uint8_t *name; uint64_t mac; uint32_t type; uint32_t if_status; 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, "type", (int *) &type); if (status == META_FALSE) { PRINT_ERROR("todo error"); exit(-1); } status = config_setting_lookup_int(elem, "status", (int *) &if_status); 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->type = (uint16_t) type; ifr->status = (uint8_t) if_status; ifr->mtu = mtu; ifr->flags = flags; ifr->addr_list = list_create(MAX_FAMILIES); if (list_has_space(overall->envi->if_list)) { 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); } } //############# if_main PRINT_IMPORTANT("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_ERROR("todo"); } //############# addr_list PRINT_IMPORTANT("address list"); //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->flags & IFF_RUNNING) { 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_addr(&addr->ip, IP4_ADR_P2H(ip[0], ip[1], ip[2],ip[3])); addr4_set_addr(&addr->mask, IP4_ADR_P2H(mask[0], mask[1], mask[2],mask[3])); addr4_set_addr(&addr->gw, IP4_ADR_P2H(gw[0], gw[1], gw[2], gw[3])); addr4_set_addr(&addr->bdc, IP4_ADR_P2H(bdc[0], bdc[1], bdc[2], bdc[3])); addr4_set_addr(&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_ERROR("todo"); } else { //TODO error? PRINT_ERROR("todo error"); exit(-1); } if (list_has_space(ifr->addr_list)) { list_append(ifr->addr_list, addr); } else { //TODO error PRINT_ERROR("todo error"); exit(-1); } } else { //TODO error PRINT_ERROR("todo: replace or add new?"); } } else { //TODO error PRINT_ERROR("todo: decide just drop or add?"); } } else { //TODO error PRINT_ERROR("todo error"); exit(-1); } } //############# route_list PRINT_IMPORTANT("route list"); 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); } ////ip = IP4_ADR_P2H(192,168,1,5); 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->flags & IFF_RUNNING) { route = (struct route_record *) secure_malloc(sizeof(struct route_record)); route->if_index = if_index; route->family = family; if (family == AF_INET) { addr4_set_addr(&route->dst, IP4_ADR_P2H(dst[0], dst[1], dst[2], dst[3])); addr4_set_addr(&route->mask, IP4_ADR_P2H(mask[0], mask[1], mask[2],mask[3])); addr4_set_addr(&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)) { list_append(overall->envi->route_list, route); } else { //TODO error PRINT_ERROR("todo error"); exit(-1); } } else { //TODO error PRINT_ERROR("todo: decide just drop or add?"); } } } metadata_destroy(meta_envi); //###################################################################### PRINT_IMPORTANT("loading stack"); metadata *meta_stack = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta_stack); status = config_read_file(meta_stack, "stack.cfg"); if (status == META_FALSE) { PRINT_ERROR("%s:%d - %s\n", 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("module list"); overall->lib_list = list_create(MAX_MODULES); memset(overall->modules, 0, MAX_MODULES * sizeof(struct fins_module *)); overall->admin_list = list_create(MAX_MODULES); uint8_t base_path[100]; memset((char *) base_path, 0, 100); strcpy((char *) base_path, "."); 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; 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, lib_name_test, mod_lib); if (library == NULL) { library = library_load(mod_lib, base_path); if (library == NULL) { PRINT_ERROR("todo error"); exit(-1); } if (list_has_space(overall->lib_list)) { list_append(overall->lib_list, library); } else { PRINT_ERROR("todo error"); exit(-1); } } module = library->create(i, mod_id, mod_name); if (module == NULL) { //TODO error PRINT_ERROR("todo error"); exit(-1); } library->num_mods++; //TODO move flow to update? or links here? status = module->ops->init(module, mod_flows_num, mod_flows, mod_params, overall->envi); //TODO merge init into create? if (status != 0) { overall->modules[i] = module; if (mod_admin != NULL) { list_append(overall->admin_list, module); } } else { PRINT_ERROR("todo error"); 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_each(overall->admin_list, register_all); list_for_each1(overall->admin_list, assign_overall, overall); //############# linking_list PRINT_IMPORTANT("link list"); 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)) { list_append(overall->link_list, link); } else { //TODO error PRINT_ERROR("todo error"); exit(-1); } } metadata_destroy(meta_stack); //###################################################################### PRINT_IMPORTANT("update modules"); //send out subset of linking table to each module as update //TODO table subset update struct linked_list *link_subset_list; metadata *meta_update; struct finsFrame *ff_update; for (i = 0; i < MAX_MODULES; i++) { if (overall->modules[i] != NULL) { link_subset_list = list_filter1(overall->link_list, link_involved_test, &overall->modules[i]->index, link_clone); //TODO is mem leak PRINT_IMPORTANT("i=%d, link_subset_list=%p, len=%d", i, link_subset_list, link_subset_list->len); meta_update = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta_update); //TODO decide on metadata params? //uint32_t host_ip = IP4_ADR_P2H(192,168,1,8); //secure_metadata_writeToElement(meta_update, "send_src_ip", &host_ip, META_TYPE_INT32); 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_LINKS; ff_update->ctrlFrame.data_len = sizeof(struct linked_list); ff_update->ctrlFrame.data = (uint8_t *) link_subset_list; module_to_switch(overall->modules[0], ff_update); } } //############ say by this point envi var completely init'd //assumed always connect/init to switch first pthread_attr_t attr; pthread_attr_init(&attr); PRINT_IMPORTANT("modules: run"); for (i = 0; i < MAX_MODULES; i++) { if (overall->modules[i] != NULL) { overall->modules[i]->ops->run(overall->modules[i], &attr); } } //############ mini test //sleep(5); char recv_data[4000]; //while (1) { PRINT_IMPORTANT("waiting..."); gets(recv_data); if (0) { metadata *meta = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta); uint32_t host_ip = IP4_ADR_P2H(192,168,1,8); uint32_t host_port = 55454; uint32_t dst_ip = IP4_ADR_P2H(192,168,1,3); uint32_t dst_port = 44444; uint32_t ttl = 64; uint32_t tos = 64; secure_metadata_writeToElement(meta, "send_src_ip", &host_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_src_port", &host_port, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_dst_ip", &dst_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_dst_port", &dst_port, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_ttl", &ttl, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_tos", &tos, META_TYPE_INT32); struct finsFrame *ff = (struct finsFrame *) secure_malloc(sizeof(struct finsFrame)); ff->dataOrCtrl = FF_DATA; ff->destinationID = 1; ff->metaData = meta; ff->dataFrame.directionFlag = DIR_UP; ff->dataFrame.pduLength = 10; ff->dataFrame.pdu = (uint8_t *) secure_malloc(10); PRINT_IMPORTANT("sending: ff=%p, meta=%p, src='%s' to dst='%s'", ff, meta, overall->modules[0]->name, overall->modules[1]->name); module_to_switch(overall->modules[0], ff); } if (0) { PRINT_DEBUG("Sending ARP req"); metadata *meta_req = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta_req); uint32_t dst_ip = IP4_ADR_P2H(192, 168, 1, 1); //uint32_t dst_ip = IP4_ADR_P2H(172, 31, 54, 169); uint32_t src_ip = IP4_ADR_P2H(192, 168, 1, 20); //uint32_t src_ip = IP4_ADR_P2H(172, 31, 50, 160); secure_metadata_writeToElement(meta_req, "dst_ip", &dst_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta_req, "src_ip", &src_ip, META_TYPE_INT32); struct finsFrame *ff_req = (struct finsFrame*) secure_malloc(sizeof(struct finsFrame)); ff_req->dataOrCtrl = FF_CONTROL; ff_req->destinationID = 1; //arp ff_req->metaData = meta_req; ff_req->ctrlFrame.sender_id = 4; //ipv4 ff_req->ctrlFrame.serial_num = gen_control_serial_num(); ff_req->ctrlFrame.opcode = CTRL_EXEC; ff_req->ctrlFrame.param_id = 0; //EXEC_ARP_GET_ADDR; ff_req->ctrlFrame.data_len = 0; ff_req->ctrlFrame.data = NULL; PRINT_IMPORTANT("sending: ff=%p, meta=%p, src='%s' to dst='%s'", ff_req, meta_req, overall->modules[0]->name, overall->modules[1]->name); module_to_switch(overall->modules[0], ff_req); } if (0) { PRINT_DEBUG("Sending data"); metadata *meta_req = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta_req); uint32_t ether_type = 0x0800; //ipv4 if_index = 3; //wlan0 uint32_t src_ip = IP4_ADR_P2H(192, 168, 1, 5); //wlan0 uint32_t dst_ip = IP4_ADR_P2H(192, 168, 1, 1); //gw secure_metadata_writeToElement(meta_req, "send_ether_type", ðer_type, META_TYPE_INT32); secure_metadata_writeToElement(meta_req, "send_if_index", &if_index, META_TYPE_INT32); secure_metadata_writeToElement(meta_req, "send_src_ipv4", &src_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta_req, "send_dst_ipv4", &dst_ip, META_TYPE_INT32); struct finsFrame *ff = (struct finsFrame*) secure_malloc(sizeof(struct finsFrame)); ff->dataOrCtrl = FF_DATA; ff->destinationID = 1; //arp ff->metaData = meta_req; ff->dataFrame.directionFlag = DIR_DOWN; ff->dataFrame.pduLength = 100; ff->dataFrame.pdu = (uint8_t *) secure_malloc(ff->dataFrame.pduLength); memset(ff->dataFrame.pdu, 59, ff->dataFrame.pduLength); PRINT_IMPORTANT("sending: ff=%p, meta=%p, src='%s' to dst='%s'", ff, meta_req, overall->modules[0]->name, overall->modules[1]->name); module_to_switch(overall->modules[0], ff); } if (0) { PRINT_DEBUG("Sending data"); metadata *meta = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta); uint32_t src_ip = IP4_ADR_P2H(192, 168, 1, 5); //wlan0 uint32_t src_port = 6666; uint32_t dst_ip = IP4_ADR_P2H(192, 168, 1, 1); //gw uint32_t dst_port = 5555; secure_metadata_writeToElement(meta, "send_src_ipv4", &src_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_src_port", &src_port, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_dst_ipv4", &dst_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_dst_port", &dst_port, META_TYPE_INT32); uint32_t dst_index = 4; struct finsFrame *ff = (struct finsFrame*) secure_malloc(sizeof(struct finsFrame)); ff->dataOrCtrl = FF_DATA; ff->destinationID = dst_index; //arp ff->metaData = meta; ff->dataFrame.directionFlag = DIR_DOWN; ff->dataFrame.pduLength = 10; ff->dataFrame.pdu = (uint8_t *) secure_malloc(ff->dataFrame.pduLength); memset(ff->dataFrame.pdu, 65, ff->dataFrame.pduLength); PRINT_IMPORTANT("sending: ff=%p, meta=%p, src='%s' to dst='%s'", ff, meta, overall->modules[0]->name, overall->modules[dst_index]->name); module_to_switch(overall->modules[0], ff); } if (1) { PRINT_DEBUG("Sending data"); metadata *meta = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta); uint32_t src_ip = IP4_ADR_P2H(192, 168, 1, 5); //wlan0 uint32_t dst_ip = IP4_ADR_P2H(192, 168, 1, 1); //gw secure_metadata_writeToElement(meta, "send_src_ipv4", &src_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_dst_ipv4", &dst_ip, META_TYPE_INT32); uint32_t dst_index = 4; struct finsFrame *ff = (struct finsFrame*) secure_malloc(sizeof(struct finsFrame)); ff->dataOrCtrl = FF_DATA; ff->destinationID = dst_index; ff->metaData = meta; ff->dataFrame.directionFlag = DIR_DOWN; ff->dataFrame.pduLength = 10; ff->dataFrame.pdu = (uint8_t *) secure_malloc(ff->dataFrame.pduLength); memset(ff->dataFrame.pdu, 65, ff->dataFrame.pduLength); PRINT_IMPORTANT("sending: ff=%p, meta=%p, src='%s' to dst='%s'", ff, meta, overall->modules[0]->name, overall->modules[dst_index]->name); module_to_switch(overall->modules[0], ff); } while (1) ; sleep(5); PRINT_IMPORTANT("waiting..."); char recv_data2[4000]; gets(recv_data2); //############ terminating core_termination_handler(0); }
int jinni_UDP_to_fins(u_char *dataLocal,int len,uint16_t dstport,uint32_t dst_IP_netformat, uint16_t hostport,uint32_t host_IP_netformat) { struct finsFrame *ff= (struct finsFrame *) malloc(sizeof (struct finsFrame)); metadata *udpout_meta = (metadata *)malloc(sizeof(metadata)); PRINT_DEBUG(); metadata_create(udpout_meta); if ( udpout_meta == NULL ) { PRINT_DEBUG("metadata creation failed"); free(ff); exit(1); } /** metadata_writeToElement() set the value of an element if it already exist * or it creates the element and set its value in case it is new */ PRINT_DEBUG("%d, %d, %d, %d", dstport,dst_IP_netformat,hostport, host_IP_netformat); uint32_t dstprt= dstport; uint32_t hostprt = hostport; metadata_writeToElement(udpout_meta,"dstport",&dstport,META_TYPE_INT); metadata_writeToElement(udpout_meta,"srcport",&hostport,META_TYPE_INT); metadata_writeToElement(udpout_meta,"dstip",&dst_IP_netformat,META_TYPE_INT); metadata_writeToElement(udpout_meta,"srcip",&host_IP_netformat,META_TYPE_INT); ff->dataOrCtrl = DATA; /**TODO get the address automatically by searching the local copy of the * switch table */ ff->destinationID.id = UDPID; ff->destinationID.next = NULL; (ff->dataFrame).directionFlag = DOWN; (ff->dataFrame).pduLength = len; (ff->dataFrame).pdu = dataLocal; (ff->dataFrame).metaData = udpout_meta ; /**TODO insert the frame into jinni_to_switch queue * check if insertion succeeded or not then * return 1 on success, or -1 on failure * */ PRINT_DEBUG(""); sem_wait(&Jinni_to_Switch_Qsem); if (write_queue(ff,Jinni_to_Switch_Queue)) { sem_post(&Jinni_to_Switch_Qsem); PRINT_DEBUG(""); return(1); } sem_post(&Jinni_to_Switch_Qsem); PRINT_DEBUG(""); return(0); }
void *accept_console(void *local) { struct fins_module *module = (struct fins_module *) local; PRINT_DEBUG("Entered: module=%p", module); PRINT_IMPORTANT("Thread started: module=%p, index=%u, id=%u, name='%s'", module, module->index, module->id, module->name); struct rtm_data *md = (struct rtm_data *) module->data; int32_t addr_size = sizeof(struct sockaddr_un); struct sockaddr_un *addr; int console_fd; struct rtm_console *console; int i; secure_sem_wait(&md->shared_sem); while (module->state == FMS_RUNNING) { if (list_has_space(md->console_list)) { sem_post(&md->shared_sem); addr = (struct sockaddr_un *) secure_malloc(addr_size); while (module->state == FMS_RUNNING) { sleep(1); console_fd = accept(md->server_fd, (struct sockaddr *) addr, (socklen_t *) &addr_size); if (console_fd > 0 || (errno != EAGAIN && errno != EWOULDBLOCK)) { break; } } if (module->state != FMS_RUNNING) { free(addr); secure_sem_wait(&md->shared_sem); break; } if (console_fd < 0) { PRINT_ERROR("accept error: server_fd=%d, console_fd=%d, errno=%u, str='%s'", md->server_fd, console_fd, errno, strerror(errno)); free(addr); secure_sem_wait(&md->shared_sem); continue; } secure_sem_wait(&md->shared_sem); console = (struct rtm_console *) secure_malloc(sizeof(struct rtm_console)); console->id = md->console_counter++; console->fd = console_fd; console->addr = addr; console->type = RTM_TYPE_DEFAULT; console->listeners = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(console->listeners); PRINT_IMPORTANT("Console created: id=%u, fd=%d, addr='%s', type=%u", console->id, console->fd, console->addr->sun_path, console->type); list_append(md->console_list, console); for (i = 0; i < MAX_CONSOLES; i++) { if (md->console_fds[i] == 0) { md->console_fds[i] = console_fd; break; } } } else { sem_post(&md->shared_sem); sleep(5); secure_sem_wait(&md->shared_sem); } } sem_post(&md->shared_sem); PRINT_IMPORTANT("Thread exited: module=%p, index=%u, id=%u, name='%s'", module, module->index, module->id, module->name); PRINT_DEBUG("Exited: module=%p", module); return NULL; }
void core_tests(void) { int i = 0; while (1) { PRINT_IMPORTANT("waiting..."); //sleep(10); //char recv_data[4000]; //gets(recv_data); fgetc(stdin); //wait until user enters PRINT_IMPORTANT("active"); i++; if (i == 1) { metadata *meta = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta); uint32_t host_ip = IP4_ADR_P2H(192,168,1,8); uint32_t host_port = 55454; uint32_t dst_ip = IP4_ADR_P2H(192,168,1,3); uint32_t dst_port = 44444; uint32_t ttl = 64; uint32_t tos = 64; secure_metadata_writeToElement(meta, "send_src_ip", &host_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_src_port", &host_port, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_dst_ip", &dst_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_dst_port", &dst_port, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_ttl", &ttl, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_tos", &tos, META_TYPE_INT32); uint32_t src_index = 1; uint32_t dst_index = 2; struct finsFrame *ff = (struct finsFrame *) secure_malloc(sizeof(struct finsFrame)); ff->dataOrCtrl = FF_DATA; ff->destinationID = dst_index; ff->metaData = meta; ff->dataFrame.directionFlag = DIR_UP; ff->dataFrame.pduLength = 10; ff->dataFrame.pdu = (uint8_t *) secure_malloc(10); PRINT_IMPORTANT("sending: ff=%p, meta=%p, src='%s' to dst='%s'", ff, meta, overall->modules[src_index]->name, overall->modules[dst_index]->name); module_to_switch(overall->modules[src_index], ff); } if (0) { PRINT_DEBUG("Sending ARP req"); metadata *meta_req = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta_req); uint32_t dst_ip = IP4_ADR_P2H(192, 168, 1, 1); //uint32_t dst_ip = IP4_ADR_P2H(172, 31, 54, 169); uint32_t src_ip = IP4_ADR_P2H(192, 168, 1, 20); //uint32_t src_ip = IP4_ADR_P2H(172, 31, 50, 160); secure_metadata_writeToElement(meta_req, "dst_ip", &dst_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta_req, "src_ip", &src_ip, META_TYPE_INT32); struct finsFrame *ff_req = (struct finsFrame*) secure_malloc(sizeof(struct finsFrame)); ff_req->dataOrCtrl = FF_CONTROL; ff_req->destinationID = 1; //arp ff_req->metaData = meta_req; ff_req->ctrlFrame.sender_id = 4; //ipv4 ff_req->ctrlFrame.serial_num = gen_control_serial_num(); ff_req->ctrlFrame.opcode = CTRL_EXEC; ff_req->ctrlFrame.param_id = 0; //EXEC_ARP_GET_ADDR; ff_req->ctrlFrame.data_len = 0; ff_req->ctrlFrame.data = NULL; PRINT_IMPORTANT("sending: ff=%p, meta=%p, src='%s' to dst='%s'", ff_req, meta_req, overall->modules[0]->name, overall->modules[1]->name); module_to_switch(overall->modules[0], ff_req); } if (i == 2) { PRINT_DEBUG("Sending data"); metadata *meta_req = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta_req); uint32_t ether_type = 0x0800; //ipv4 int32_t if_index = 3; //wlan0 uint32_t src_ip = IP4_ADR_P2H(192, 168, 1, 5); //wlan0 uint32_t dst_ip = IP4_ADR_P2H(192, 168, 1, 1); //gw uint32_t src_index = 2; uint32_t dst_index = 1; secure_metadata_writeToElement(meta_req, "send_ether_type", ðer_type, META_TYPE_INT32); secure_metadata_writeToElement(meta_req, "send_if_index", &if_index, META_TYPE_INT32); secure_metadata_writeToElement(meta_req, "send_src_ipv4", &src_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta_req, "send_dst_ipv4", &dst_ip, META_TYPE_INT32); struct finsFrame *ff = (struct finsFrame*) secure_malloc(sizeof(struct finsFrame)); ff->dataOrCtrl = FF_DATA; ff->destinationID = dst_index; //arp ff->metaData = meta_req; ff->dataFrame.directionFlag = DIR_DOWN; ff->dataFrame.pduLength = 100; ff->dataFrame.pdu = (uint8_t *) secure_malloc(ff->dataFrame.pduLength); memset(ff->dataFrame.pdu, 59, ff->dataFrame.pduLength); PRINT_IMPORTANT("sending: ff=%p, meta=%p, src='%s' to dst='%s'", ff, meta_req, overall->modules[src_index]->name, overall->modules[dst_index]->name); module_to_switch(overall->modules[src_index], ff); } if (0) { PRINT_DEBUG("Sending data"); metadata *meta = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta); uint32_t src_ip = IP4_ADR_P2H(192, 168, 1, 4); //wlan0 uint32_t src_port = 6666; uint32_t dst_ip = IP4_ADR_P2H(192, 168, 1, 1); //gw uint32_t dst_port = 5555; secure_metadata_writeToElement(meta, "send_src_ipv4", &src_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_src_port", &src_port, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_dst_ipv4", &dst_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_dst_port", &dst_port, META_TYPE_INT32); uint32_t dst_index = 8; struct finsFrame *ff = (struct finsFrame*) secure_malloc(sizeof(struct finsFrame)); ff->dataOrCtrl = FF_DATA; ff->destinationID = dst_index; //arp ff->metaData = meta; ff->dataFrame.directionFlag = DIR_DOWN; ff->dataFrame.pduLength = 10; ff->dataFrame.pdu = (uint8_t *) secure_malloc(ff->dataFrame.pduLength); memset(ff->dataFrame.pdu, 65, ff->dataFrame.pduLength); PRINT_IMPORTANT("sending: ff=%p, meta=%p, src='%s' to dst='%s'", ff, meta, overall->modules[0]->name, overall->modules[dst_index]->name); module_to_switch(overall->modules[0], ff); } if (0) { PRINT_DEBUG("Sending data"); metadata *meta = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta); uint32_t family = AF_INET; uint32_t src_ip = IP4_ADR_P2H(192, 168, 1, 15); //wlan0 uint32_t dst_ip = IP4_ADR_P2H(172, 168, 1, 1); //gw secure_metadata_writeToElement(meta, "send_family", &family, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_src_ipv4", &src_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_dst_ipv4", &dst_ip, META_TYPE_INT32); uint32_t dst_index = 4; struct finsFrame *ff = (struct finsFrame*) secure_malloc(sizeof(struct finsFrame)); ff->dataOrCtrl = FF_DATA; ff->destinationID = dst_index; ff->metaData = meta; ff->dataFrame.directionFlag = DIR_DOWN; ff->dataFrame.pduLength = 10; ff->dataFrame.pdu = (uint8_t *) secure_malloc(ff->dataFrame.pduLength); memset(ff->dataFrame.pdu, 65, ff->dataFrame.pduLength); PRINT_IMPORTANT("sending: ff=%p, meta=%p, src='%s' to dst='%s'", ff, meta, overall->modules[0]->name, overall->modules[dst_index]->name); module_to_switch(overall->modules[0], ff); } //break; } }
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 ************"); }