CP_C_API void cp_unregister_ploader(cp_context_t *ctx, cp_plugin_loader_t *loader) { hnode_t *hnode; CHECK_NOT_NULL(ctx); CHECK_NOT_NULL(loader); cpi_lock_context(ctx); cpi_check_invocation(ctx, CPI_CF_ANY, __func__); hnode = hash_lookup(ctx->env->loaders_to_plugins, loader); if (hnode != NULL) { hash_t *loader_plugins = hnode_get(hnode); // Uninstall all plug-ins loaded by the loader while (!hash_isempty(loader_plugins)) { hscan_t hscan; hnode_t *hnode2; cp_status_t status; hash_scan_begin(&hscan, loader_plugins); hnode2 = hash_scan_next(&hscan); assert(hnode2 != NULL); status = cp_uninstall_plugin(ctx, hnode_getkey(hnode2)); assert(status == CP_OK); } hash_delete_free(ctx->env->loaders_to_plugins, hnode); assert(hash_isempty(loader_plugins)); hash_destroy(loader_plugins); cpi_debugf(ctx, N_("The plug-in loader %p was unregistered."), (void *) loader); } cpi_unlock_context(ctx); }
errorcode helper_fsm_start_peer_bday(connlist_t *list, connlist_item_t *peer, connlist_item_t *buddy) { /* declare local variables */ comm_msg_syn_flooded_t msg; /* error check arguments */ CHECK_NOT_NULL(list,ERROR_NULL_ARG_1); CHECK_NOT_NULL(peer,ERROR_NULL_ARG_2); CHECK_NOT_NULL(buddy,ERROR_NULL_ARG_3); /* do function */ /* receive message indicating that the flood happened */ CHECK_FAILED(readMsg(peer->info.socks.peer,COMM_MSG_SYN_FLOODED, &msg,sizeof(msg)),ERROR_NETWORK_READ); DEBUG(DBG_PROTOCOL,"PROTOCOL:received SYN_FLOODED\n"); /* set the value for the sequence number in the peer's info */ peer->info.bday.seq_num = msg.seq_num; peer->info.bday.seq_num_set = FLAG_SET; /* send message indicating the buddy is about to commence sending the * SYN/ACKs */ CHECK_FAILED(sendMsg(peer->info.socks.peer, COMM_MSG_BUDDY_SYN_ACK_FLOODED,NULL,0),ERROR_NETWORK_SEND); /* enter next state */ CHECK_FAILED(helper_fsm_end_peer_bday(list,peer,buddy), ERROR_CALLED_FUNCTION); return ERROR_1; }
CP_C_API cp_status_t cp_register_pcollection(cp_context_t *context, const char *dir) { cp_status_t status = CP_OK; CHECK_NOT_NULL(context); CHECK_NOT_NULL(dir); cpi_lock_context(context); cpi_check_invocation(context, CPI_CF_ANY, __func__); do { // Initialize plug-in loader if ((status = init_local_ploader(context)) != CP_OK) { break; } // Register directory status = cp_lpl_register_dir(context->env->local_loader, dir); } while (0); // Report error or success if (status != CP_OK) { cpi_errorf(context, N_("The plug-in collection in path %s could not be registered due to insufficient memory."), dir); } else { cpi_debugf(context, N_("The plug-in collection in path %s was registered."), dir); } cpi_unlock_context(context); return status; }
result_t add_module(char *file, size_t argc, u8_t *argv[]) { FILE *fp; void *tmp; size_t size; size_t result; printf("[%%] file: %s\n", file); tmp = malloc(FLAT_BINARY_FILE_MAX_SIZE); CHECK_NOT_NULL(tmp, "[-] unable to malloc space for the tmp module buffer\n"); fp = fopen(file, "r"); CHECK_NOT_NULL(fp, "[-] unable to open file\n"); size = fread(tmp, sizeof(u8_t), FLAT_BINARY_FILE_MAX_SIZE, fp); if(size <= 0) { printf("[-] unable to read file\n"); return FAILURE; } result = ldr_call_add_module(tmp, size, argc, argv); free(tmp); return result; }
errorcode helper_fsm_start_buddy_bday(connlist_t *list, connlist_item_t *peer, connlist_item_t *buddy) { /* declare local variables */ comm_msg_syn_ack_flood_seq_num_t msg; /* error check arguments */ CHECK_NOT_NULL(list,ERROR_NULL_ARG_1); CHECK_NOT_NULL(peer,ERROR_NULL_ARG_2); CHECK_NOT_NULL(buddy,ERROR_NULL_ARG_3); /* do function */ CHECK_FAILED(readMsg(peer->info.socks.peer, COMM_MSG_WAITING_TO_SYN_ACK_FLOOD,NULL,0),ERROR_NETWORK_READ); DEBUG(DBG_PROTOCOL,"PROTOCOL:received WAITING_TO_SYN_ACK_FLOOD\n"); /* as soon as the bday.seq_num_set flag is set, it is time for this * peer to flood synacks */ CHECK_FAILED(wait_for_buddy_syn_flood(&buddy->info),ERROR_1); /* make the message... */ msg.seq_num = buddy->info.bday.seq_num; /* ...and sent it */ CHECK_FAILED(sendMsg(peer->info.socks.peer, COMM_MSG_SYN_ACK_FLOOD_SEQ_NUM,&msg,sizeof(msg)), ERROR_NETWORK_SEND); DEBUG(DBG_PROTOCOL,"PROTOCOL:sent SYN_ACK_FLOOD_SEQ_NUM\n"); /* enter the next state */ CHECK_FAILED(helper_fsm_end_buddy_bday(list,peer,buddy), ERROR_CALLED_FUNCTION); return ERROR_1; }
CP_C_API cp_status_t cp_register_logger(cp_context_t *context, cp_logger_func_t logger, void *user_data, cp_log_severity_t min_severity) { logger_t l; logger_t *lh = NULL; lnode_t *node = NULL; cp_status_t status = CP_OK; CHECK_NOT_NULL(context); CHECK_NOT_NULL(logger); cpi_lock_context(context); cpi_check_invocation(context, CPI_CF_LOGGER, __func__); do { // Check if logger already exists and allocate new holder if necessary l.logger = logger; if ((node = list_find(context->env->loggers, &l, comp_logger)) == NULL) { lh = malloc(sizeof(logger_t)); node = lnode_create(lh); if (lh == NULL || node == NULL) { status = CP_ERR_RESOURCE; break; } lh->logger = logger; lh->plugin = context->plugin; list_append(context->env->loggers, node); } else { lh = lnode_get(node); } // Initialize or update the logger holder lh->user_data = user_data; lh->min_severity = min_severity; // Update global limits update_logging_limits(context); } while (0); // Report error if (status == CP_ERR_RESOURCE) { cpi_error(context, N_("Logger could not be registered due to insufficient memory.")); } else if (cpi_is_logged(context, CP_LOG_DEBUG)) { char owner[64]; /* TRANSLATORS: %s is the context owner */ cpi_debugf(context, N_("%s registered a logger."), cpi_context_owner(context, owner, sizeof(owner))); } cpi_unlock_context(context); // Release resources on error if (status != CP_OK) { if (node != NULL) { lnode_destroy(node); } if (lh != NULL) { free(lh); } } return status; }
int set_free(set **ptr) { CHECK_NOT_NULL(ptr); CHECK_NOT_NULL(*ptr); free(*ptr); *ptr = NULL; return SUCCESS; }
int set_dump(const set *ptr, FILE *file) { CHECK_NOT_NULL(ptr); CHECK_NOT_NULL(file); size_t total = sizeof(set) + ptr->size * sizeof(size_t) + ptr->capacity; size_t written = fwrite(ptr, total, 1, file); if (written < 1) return ERR_IO; return SUCCESS; }
CP_C_API cp_status_t cp_define_symbol(cp_context_t *context, const char *name, void *ptr) { cp_status_t status = CP_OK; CHECK_NOT_NULL(context); CHECK_NOT_NULL(name); CHECK_NOT_NULL(ptr); if (context->plugin == NULL) { cpi_fatalf(_("Only plug-ins can define context specific symbols.")); } cpi_lock_context(context); cpi_check_invocation(context, CPI_CF_LOGGER | CPI_CF_LISTENER, __func__); do { char *n; // Create a symbol hash if necessary if (context->plugin->defined_symbols == NULL) { if ((context->plugin->defined_symbols = hash_create(HASHCOUNT_T_MAX, (int (*)(const void *, const void *)) strcmp, NULL)) == NULL) { status = CP_ERR_RESOURCE; break; } } // Check for a previously defined symbol if (hash_lookup(context->plugin->defined_symbols, name) != NULL) { status = CP_ERR_CONFLICT; break; } // Insert the symbol into the symbol hash n = strdup(name); if (n == NULL || !hash_alloc_insert(context->plugin->defined_symbols, n, ptr)) { free(n); status = CP_ERR_RESOURCE; break; } } while (0); // Report error if (status != CP_OK) { switch (status) { case CP_ERR_RESOURCE: cpi_errorf(context, N_("Plug-in %s could not define symbol %s due to insufficient memory."), context->plugin->plugin->identifier, name); break; case CP_ERR_CONFLICT: cpi_errorf(context, N_("Plug-in %s tried to redefine symbol %s."), context->plugin->plugin->identifier, name); break; default: break; } } cpi_unlock_context(context); return status; }
int main(int argc,char** argv,char** envp) { void* h=CHECK_NOT_NULL(dlopen("libadd.so", RTLD_NOW)); void* sym=CHECK_NOT_NULL(dlsym(h,"func")); int (*f)(int, int); f=((int(*)(int, int))sym); int result=f(2,2); printf("2+2 is %d\n",result); CHECK_ZERO(dlclose(h)); return EXIT_SUCCESS; }
CP_C_API void cp_unregister_pcollection(cp_context_t *context, const char *dir) { CHECK_NOT_NULL(context); CHECK_NOT_NULL(dir); cpi_lock_context(context); cpi_check_invocation(context, CPI_CF_ANY, __func__); if (context->env->local_loader != NULL) { cp_lpl_unregister_dir(context->env->local_loader, dir); } cpi_debugf(context, N_("The plug-in collection in path %s was unregistered."), dir); cpi_unlock_context(context); }
CP_C_API void cp_release_symbol(cp_context_t *context, const void *ptr) { hnode_t *node; symbol_info_t *symbol_info; symbol_provider_info_t *provider_info; CHECK_NOT_NULL(context); CHECK_NOT_NULL(ptr); cpi_lock_context(context); cpi_check_invocation(context, CPI_CF_LOGGER | CPI_CF_LISTENER, __func__); do { // Look up the symbol if ((node = hash_lookup(context->resolved_symbols, ptr)) == NULL) { cpi_errorf(context, N_("Could not release unknown symbol at address %p."), ptr); break; } symbol_info = hnode_get(node); provider_info = symbol_info->provider_info; // Decrease usage count assert(symbol_info->usage_count > 0); symbol_info->usage_count--; assert(provider_info->usage_count > 0); provider_info->usage_count--; // Check if the symbol is not being used anymore if (symbol_info->usage_count == 0) { hash_delete_free(context->resolved_symbols, node); free(symbol_info); if (cpi_is_logged(context, CP_LOG_DEBUG)) { char owner[64]; /* TRANSLATORS: First %s is the context owner */ cpi_debugf(context, _("%s released the symbol at address %p defined by plug-in %s."), cpi_context_owner(context, owner, sizeof(owner)), ptr, provider_info->plugin->plugin->identifier); } } // Check if the symbol providing plug-in is not being used anymore if (provider_info->usage_count == 0) { node = hash_lookup(context->symbol_providers, provider_info->plugin); assert(node != NULL); hash_delete_free(context->symbol_providers, node); if (!provider_info->imported) { cpi_ptrset_remove(context->plugin->imported, provider_info->plugin); cpi_ptrset_remove(provider_info->plugin->importing, context->plugin); cpi_debugf(context, _("A dynamic dependency from plug-in %s to plug-in %s was removed."), context->plugin->plugin->identifier, provider_info->plugin->plugin->identifier); } free(provider_info); } } while (0); cpi_unlock_context(context); }
CP_C_API void cp_log(cp_context_t *context, cp_log_severity_t severity, const char *msg) { CHECK_NOT_NULL(context); CHECK_NOT_NULL(msg); cpi_lock_context(context); cpi_check_invocation(context, CPI_CF_LOGGER, __func__); if (severity < CP_LOG_DEBUG || severity > CP_LOG_ERROR) { cpi_fatalf(_("Illegal severity value in call to %s."), __func__); } if (cpi_is_logged(context, severity)) { do_log(context, severity, msg); } cpi_unlock_context(context); }
CP_C_API void cp_set_context_args(cp_context_t *ctx, char **argv) { int argc; CHECK_NOT_NULL(ctx); CHECK_NOT_NULL(argv); for (argc = 0; argv[argc] != NULL; argc++); if (argc < 1) { cpi_fatalf(_("At least one startup argument must be given in call to function %s."), __func__); } cpi_lock_context(ctx); ctx->env->argc = argc; ctx->env->argv = argv; cpi_unlock_context(ctx); }
errcode_t init_test(test_t * test, const char * programfile, const char * testfile) { errcode_t retval; FILE * in; uint16_t i; uint16_t next_gpio_value; char filename[256]; retval = init_vm(&test->uut); CHECK_OK(retval, "Failed to init vm\n"); retval = load_program(&test->uut, programfile); CHECK_OK(retval, "Failed to load program\n"); in = fopen(testfile, "r"); CHECK_NOT_NULL(in, IO, "Failed to open test file\n"); fscanf(in, "%u", &test->test_length); CHECK_NOT_FERROR(in); /* test->uart_in = fopen("uartin.dat", "w"); CHECK_NOT_NULL(test->uart_in, IO, "Failed to open UART in file\n"); test->uart_out = fopen("uartout.dat", "w"); CHECK_NOT_NULL(test->uart_out, IO, "Failed to open UART out file\n"); */ for (i = 0; i < TEST_PIN_COUNT; i++) { sprintf(filename, "gpio_%u.dat", i); test->gpio_files[i] = fopen(filename, "w"); CHECK_NOT_NULL(test->gpio_files[i], IO, "Failed to open GPIO file\n"); } test->input_size = 0; while (!feof(in)) { fscanf(in, "%u %u %hu", &test->gpio_input[test->input_size].time, &test->gpio_input[test->input_size].pin, &next_gpio_value); test->gpio_input[test->input_size].value = (uint8_t)next_gpio_value; test->input_size++; } test->input_size--; /* discard excessive read */ fclose(in); for (i = 0; i < test->uut.proc_table_size; i++) { sprintf(filename, "sched_%u.dat", i); test->schedules[i] = fopen(filename, "w"); CHECK_NOT_NULL(test->schedules[i], IO, "Failed to open schedule file\n"); } return OK; }
errorcode helper_fsm_end_peer_bday(connlist_t *list, connlist_item_t *peer, connlist_item_t *buddy) { /* declare local variables */ comm_msg_bday_success_port_t receive_msg; comm_msg_buddy_port_t send_msg; /* error check arguments */ CHECK_NOT_NULL(list,ERROR_NULL_ARG_1); CHECK_NOT_NULL(peer,ERROR_NULL_ARG_2); CHECK_NOT_NULL(buddy,ERROR_NULL_ARG_3); /* do function */ if (FAILED(readMsg(peer->info.socks.peer,COMM_MSG_BDAY_SUCCESS_PORT, &receive_msg,sizeof(receive_msg)))) { peer->info.bday.status = FLAG_FAILED; return ERROR_NETWORK_READ; } /* set the port value */ peer->info.bday.port = receive_msg.port; peer->info.bday.port_set = FLAG_SET; peer->info.bday.status = FLAG_SUCCESS; /* this location for the external port is no longer valid, since the * flag was already set, but I set it here just in case I screwed up * somewhere else in the code and look at it */ peer->info.port_alloc.ext_port = receive_msg.port; peer->info.port_alloc.ext_port_set = FLAG_SET; /* send a message with the buddy's port, to go back to the buddy port * state. there is no need to wait for the port value to be set, it * is obviously set, since it was set before */ send_msg.ext_port = buddy->info.port_alloc.ext_port; send_msg.bday = COMM_BDAY_NOT_NEEDED; CHECK_FAILED(sendMsg(peer->info.socks.peer,COMM_MSG_BUDDY_PORT, &send_msg,sizeof(send_msg)),ERROR_NETWORK_SEND); DEBUG(DBG_PROTOCOL,"PROTOCOL:sent BUDDY_PORT...again\n"); /* go to the start direct conncetion state now that all ports are * known */ CHECK_FAILED(helper_fsm_start_direct_conn(list,peer,buddy), ERROR_CALLED_FUNCTION); return SUCCESS; }
errorcode helper_fsm_hello(connlist_t *list, connlist_item_t *item) { /* declare variables */ comm_msg_hello_t hello; /* error check arguments */ CHECK_NOT_NULL(list,ERROR_NULL_ARG_1); CHECK_NOT_NULL(item,ERROR_NULL_ARG_2); /* do function */ /* this is a strange case, but it is OK if no acceptable message * is received on this read. It was probably a port prediction * second connection */ CHECK_FAILED(readMsg(item->info.socks.peer, COMM_MSG_HELLO, &hello, sizeof(hello)), ERROR_NETWORK_READ); DEBUG(DBG_PROTOCOL,"PROTOCOL:received HELLO\n"); /* save out info from the message */ item->info.peer.port = hello.peer_port; item->info.peer.ip = hello.peer_ip; item->info.peer.set = FLAG_SET; item->info.buddy.int_ip = hello.buddy_int_ip; item->info.buddy.int_port = hello.buddy_int_port; item->info.buddy.ext_ip = hello.buddy_ext_ip; item->info.buddy.identifier = FLAG_SET; DEBUG(DBG_VERBOSE,"VERBOSE:Information from peer hello\n"); DEBUG(DBG_VERBOSE,"VERBOSE:peer.............%s:%u\n", DBG_IP(item->info.peer.ip), DBG_PORT(item->info.peer.port)); DEBUG(DBG_VERBOSE,"VERBOSE:buddy internal...%s:%u\n", DBG_IP(item->info.buddy.int_ip), DBG_PORT(item->info.buddy.int_port)); DEBUG(DBG_VERBOSE,"VERBOSE:buddy external...%s\n", DBG_IP(item->info.buddy.ext_ip)); /* send the next message */ CHECK_FAILED(sendMsg(item->info.socks.peer, COMM_MSG_CONNECT_AGAIN, NULL, 0),ERROR_NETWORK_SEND); DEBUG(DBG_PROTOCOL,"PROTOCOL:sent CONNECT_AGAIN\n"); /* go to next state */ CHECK_FAILED(helper_fsm_conn2(list,item),ERROR_CALLED_FUNCTION); return SUCCESS; }
int set_load(set **ptr, void *mem, hasher_t func) { CHECK_NOT_NULL(ptr); CHECK_NOT_NULL(mem); CHECK_NULL(*ptr); CHECK_NOT_NULL(func); set *candidate = mem; size_t size = candidate->size; candidate->hasher = func; candidate->map = mem + sizeof(set); candidate->heap = mem + sizeof(set) + size * sizeof(size_t); candidate->tail = candidate->heap + 1; *ptr = candidate; return SUCCESS; }
errorcode list_node_remove(list_t *list,conn_list_item *item) { CHECK_NOT_NULL(list,ERROR_NULL_ARG); list_node *node; node = list->head; while(1) { if(node->info.Src.sin_addr.s_addr==item->info.Dest.sin_addr.s_addr && node->info.Dest.sin_addr.s_addr==item->info.Src.sin_addr.s_addr) { if(node->next != NULL) node->next->perv = node->perv; if(node->perv != NULL) node->perv->next = node->next; if(list->size == 1) list->head = NULL; free(node); list->size--; return LINKC_SUCCESS; } if(node->next == NULL) break; node = node->next; } return ERROR_NOT_FOUND; }
static unsigned int delete_node(unsigned int list_id, unsigned int index, data_t *pData) { pgeneric_list_t p_list = get_list_from_allotted_list(list_id); if (CHECK_NULL(p_list) || !(p_list->node_count) || (index != 0 && (index >= p_list->node_count))) return 0; plist_node_t temp = p_list->phead; if (index == (p_list->node_count - 1)) { p_list->phead = temp->next; } else { for (index = p_list->node_count - (index + 1); --index > 0; temp = temp->next) ; plist_node_t prev_node = temp; temp = temp->next; prev_node->next = temp->next; } if (CHECK_NOT_NULL(pData)) { (*pData).p = temp->data.p; (*pData).type_id = temp->data.type_id; } free(temp); p_list->node_count--; return 1; }
int set_find(const set *ptr, const void *needle, size_t len) { CHECK_NOT_NULL(ptr); CHECK_NOT_NULL(needle); CHECK_POSITIVE(len); size_t hash = ptr->hasher(needle, len); for (unsigned int i=0; i < ptr->size; i++) { size_t index = find_index(hash, i, ptr->size); if (!ptr->map[index]) return ERR_NOT_FOUND; // get offset, add to heap const void *candidate = ptr->heap + ptr->map[index]; if (!memcmp(needle, candidate, len)) return SUCCESS; } return ERR_NOT_FOUND; }
CP_C_API void cp_unregister_ploaders(cp_context_t *ctx) { int found; CHECK_NOT_NULL(ctx); cpi_lock_context(ctx); cpi_check_invocation(ctx, CPI_CF_ANY, __func__); do { hscan_t hscan; hnode_t *hnode; hash_scan_begin(&hscan, ctx->env->loaders_to_plugins); do { hnode = hash_scan_next(&hscan); } while (hnode != NULL && hnode_getkey(hnode) == ctx->env->local_loader); if (hnode != NULL) { cp_plugin_loader_t *loader = (cp_plugin_loader_t *) hnode_getkey(hnode); cp_unregister_ploader(ctx, loader); found = 1; } else { found = 0; } } while (found); cpi_unlock_context(ctx); }
static plist_node_t create_new_node(data_t data) { plist_node_t node = (plist_node_t) calloc((size_t) 1, sizeof (list_node_t)); if (CHECK_NOT_NULL(node)) { node->data.p = data.p; node->data.type_id = data.type_id; } return node; }
errorcode list_init(list_t *list) { CHECK_NOT_NULL(list,ERROR_NULL_ARG); list->head = NULL; list->size = 0; return LINKC_SUCCESS; }
CP_C_API int cp_is_logged(cp_context_t *context, cp_log_severity_t severity) { int is_logged; CHECK_NOT_NULL(context); cpi_lock_context(context); cpi_check_invocation(context, CPI_CF_LOGGER, __func__); is_logged = cpi_is_logged(context, severity); cpi_unlock_context(context); return is_logged; }
CP_C_API void cp_unregister_pcollections(cp_context_t *context) { CHECK_NOT_NULL(context); cpi_lock_context(context); cpi_check_invocation(context, CPI_CF_ANY, __func__); if (context->env->local_loader != NULL) { cp_lpl_unregister_dirs(context->env->local_loader); } cpi_debug(context, N_("All plug-in collections were unregistered.")); cpi_unlock_context(context); }
NTSTATUS FxFrameworkEntryUm( __in PWUDF_LOADER_FX_INTERFACE LoaderInterface, __in PVOID Context ) { NTSTATUS status; // // Basic validation. // if (LoaderInterface == NULL || LoaderInterface->Size < sizeof(WUDF_LOADER_FX_INTERFACE) || LoaderInterface->pUMDFPlatform == NULL) { status = STATUS_INVALID_PARAMETER; __Print(("Failed to validate loader interface parameters, " "status 0x%x\n", status)); goto Done; } // // Store platform interface. // g_IUMDFPlatform = LoaderInterface->pUMDFPlatform; // // Get the IWudfHost * from LoaderInterface. // FX_VERIFY(INTERNAL, CHECK_NOT_NULL(LoaderInterface->pIWudfHost)); HRESULT hrQI = LoaderInterface->pIWudfHost->QueryInterface( IID_IWudfHost2, (PVOID*)&g_IWudfHost2 ); FX_VERIFY(INTERNAL, CHECK_QI(hrQI, g_IWudfHost2)); g_IWudfHost2->Release(); // // Do first time init of this v2.x framework module. // In framework v1.x this is done as a side effect of invoking the // IUMDFramework->Initialize method. // status = LoaderInterface->RegisterLibrary(Context, &WdfLibraryInfo); if (!NT_SUCCESS(status)) { __Print(("RegisterLibrary failed, status 0x%x\n", status)); goto Done; } status = STATUS_SUCCESS; Done: return status; }
static unsigned int create_empty_list(void) { unsigned int index = 0; pgeneric_list_t p_list = (pgeneric_list_t) calloc((size_t) 1, sizeof (generic_list_t)); if (CHECK_NOT_NULL(p_list)) { if ((index = add_list_to_allotted_list(p_list)) == 0) free(p_list); } return index; }
int set_add(set *ptr, const void *element, size_t len) { CHECK_NOT_NULL(ptr); CHECK_NOT_NULL(element); CHECK_POSITIVE(len); if (!has_space(ptr, len)) return ERR_MEM; size_t hash = ptr->hasher(element, len); for (unsigned int i=0; i < ptr->size; i++) { size_t index = find_index(hash, i, ptr->size); if (!ptr->map[index]) { // insert ptr->map[index] = ptr->tail - ptr->heap; memcpy(ptr->tail, element, len); ptr->tail += len; return SUCCESS; } } return ERR_SIZE; }
errorcode helper_fsm_start_direct_conn(connlist_t *list, connlist_item_t *peer, connlist_item_t *buddy) { /* declare local variables */ comm_msg_buddy_syn_seq_t buddy_syn_msg; comm_msg_peer_syn_seq_t peer_syn_msg; /* error check arguments */ CHECK_NOT_NULL(list,ERROR_NULL_ARG_1); CHECK_NOT_NULL(peer,ERROR_NULL_ARG_2); CHECK_NOT_NULL(buddy,ERROR_NULL_ARG_3); /* do function */ /* get message from peer with buddy seq num */ CHECK_FAILED(readMsg(peer->info.socks.peer,COMM_MSG_BUDDY_SYN_SEQ, &buddy_syn_msg,sizeof(buddy_syn_msg)),ERROR_NETWORK_READ); DEBUG(DBG_PROTOCOL,"PROTOCOL:received BUDDY_SYN_SEQ\n"); DEBUG(DBG_VERBOSE,"VERBOSE:sequence number is %u\n", DBG_SEQ_NUM(buddy_syn_msg.seq_num)); /* fill in the information about this syn sequence number */ peer->info.buddy_syn.seq_num = buddy_syn_msg.seq_num; peer->info.buddy_syn.seq_num_set = FLAG_SET; /* make payload to send in next message. first wait for the seq num * and then fill it in the payload */ CHECK_FAILED(wait_for_buddy_syn_seq_num(&(buddy->info)),ERROR_1); peer_syn_msg.seq_num = buddy->info.buddy_syn.seq_num; /* send the message */ CHECK_FAILED(sendMsg(peer->info.socks.peer,COMM_MSG_PEER_SYN_SEQ, &peer_syn_msg,sizeof(peer_syn_msg)),ERROR_NETWORK_SEND); DEBUG(DBG_PROTOCOL,"PROTOCOL:sent PEER_SYN_SEQ\n"); /* enter next state */ CHECK_FAILED(helper_fsm_goodbye(list,peer,buddy), ERROR_CALLED_FUNCTION); return SUCCESS; }