// Test put and get functions int test_hmap_2() { hmap_handle* hmap; hmap = hmap_create(); int* data1; int* data2; int* data3; data1 = new(int); *data1 = 0; hmap_put(hmap, (long) (*data1), data1); data2 = new(int); *data2 = 1; hmap_put(hmap, (long) (*data2), data2); data3 = new(int); *data3 = 2; hmap_put(hmap, (long) (*data3), data3); if (hmap_get(hmap, (long) (*data1)) != data1 || hmap_get(hmap, (long) (*data2)) != data2 || hmap_get(hmap, (long) (*data3)) != data3) { kfree(data1); kfree(data2); kfree(data3); hmap_free(hmap); return TEST_FAIL; } return TEST_OK; }
void main() { hashmap *hMap; hMap = hmap_creat(int_hash_fn, int_eq_fn, int_del_fn); if(!hmap_put(hMap, (void*)"test", (void*)"value")) return; printf( "%s\n", (char*)hmap_get(hMap, (void*)"test") ); free_hmap(hMap); hMap = NULL; }
// Test remove function int test_hmap_3() { hmap_handle* hmap; hmap = hmap_create(); int* data1; int* data2; int* data3; data1 = new(int); *data1 = 0; hmap_put(hmap, (long) (*data1), data1); data2 = new(int); *data2 = 1; hmap_put(hmap, (long) (*data2), data2); data3 = new(int); *data3 = 2; hmap_put(hmap, (long) (*data3), data3); int* tmp1 = hmap_remove(hmap, (long) (*data1)); int* tmp2 = hmap_remove(hmap, (long) (*data2)); int* tmp3 = hmap_remove(hmap, (long) (*data3)); if (tmp1 != data1 || tmp2 != data2 || tmp3 != data3 || hmap_count(hmap) != 0) { kfree(tmp1); kfree(tmp2); kfree(tmp3); hmap_free(hmap); return TEST_FAIL; } kfree(tmp1); kfree(tmp2); kfree(tmp3); hmap_free(hmap); return TEST_OK; }
struct variable *vm_def(struct ymd_mach *vm, void *o, const char *field) { struct variable k, *v; switch (gcx(o)->type) { case T_HMAP: setv_kstr(&k, kstr_fetch(vm, field, -1)); v = hmap_put(vm, o, &k); break; case T_SKLS: setv_kstr(&k, kstr_fetch(vm, field, -1)); v = skls_put(vm, o, &k); break; default: assert(!"No reached."); break; } return v; }
static void __hmap_rehash(hmap_handle* hmap) { long size = hmap->size; hmap_entry* table = hmap->table; hmap->size = __hmap_find_prime_greater_than(size << 1); hmap->table = (hmap_entry*) kmalloc(sizeof(hmap_entry) * hmap->size); os_memset(hmap->table, 0, sizeof(hmap_entry) * hmap->size); hmap->count = 0; while (--size >= 0) { if (table[size].flags == ACTIVE) { hmap_put(hmap, table[size].key, table[size].data); } } kfree(table); }
/* This function tries to make a connection to a user, or ask them to * connect to us. * * Before calling this function, make sure we are not connected to the * user already. * * This function makes sure an unanswerred (Rev)ConnectToMe hasn't been * sent previously. * * This function is the only place that is allowed to send $ConnectToMe. * $RevConnectToMe may be set by one other place (when $RevConnectToMe * was received and we did not previously send $RevConnectToMe). */ bool hub_connect_user(DCUserInfo *ui) { char *hub_my_nick; char *hub_ui_nick; bool connect = false; hub_my_nick = main_to_hub_string(my_nick); hub_ui_nick = main_to_hub_string(ui->nick); if (is_active) { if (ui->active_state == DC_ACTIVE_SENT_ACTIVE) { warn(_("ConnectToMe already sent to user %s. Waiting.\n"), ui->nick); connect = true; goto cleanup; } if (!hub_putf("$ConnectToMe %s %s:%u|", hub_ui_nick, inet_ntoa(local_addr.sin_addr), listen_port)) goto cleanup; ui->active_state = DC_ACTIVE_SENT_ACTIVE; } else { if (ui->active_state == DC_ACTIVE_SENT_PASSIVE) { warn(_("RevConnectToMe already sent to user %s. Waiting.\n"), quotearg(ui->nick)); connect = true; goto cleanup; } if (ui->active_state == DC_ACTIVE_RECEIVED_PASSIVE) { warn(_("User %s is also passive. Cannot communicate.\n"), quotearg(ui->nick)); connect = true; goto cleanup; } if (!hub_putf("$RevConnectToMe %s %s|", hub_my_nick, hub_ui_nick)) { goto cleanup; } ui->active_state = DC_ACTIVE_SENT_PASSIVE; } /* hmap_put returns the old value */ if (hmap_put(pending_userinfo, ui->nick, ui) == NULL) ui->refcount++; connect = true; cleanup: free(hub_ui_nick); free(hub_my_nick); return connect; }
static void update_user_connection_name(DCUserConn *uc, const char *format, ...) { char *newname; va_list args; va_start(args, format); newname = xvasprintf(format, args); va_end(args); hmap_remove(user_conns, uc->name); flag_putf(DC_DF_CONNECTIONS, _("User connection %s renamed to %s.\n"), quote_n(0, uc->name), quote_n(1, newname)); if (strchr(uc->name, '|') == NULL) ptrv_append(user_conn_unknown_free, uc->name); else free(uc->name); uc->name = newname; hmap_put(user_conns, uc->name, uc); }
//------------------------------------------------------------------------ // Generic mapping functions: // ----------------------------------------------------------------------- struct variable *vm_put(struct ymd_mach *vm, struct variable *var, const struct variable *key) { struct ymd_context *l = ioslate(vm); if (is_nil(key)) ymd_panic(l, "No any key will be `nil'"); switch (ymd_type(var)) { case T_SKLS: return skls_put(vm, skls_x(var), key); case T_HMAP: return hmap_put(vm, hmap_x(var), key); case T_DYAY: return dyay_get(dyay_x(var), int4of(l, key)); case T_MAND: if (!mand_x(var)->proto) ymd_panic(l, "Management memory has no metatable yet"); return mand_put(vm, mand_x(var), key); default: ymd_panic(l, "Variable can not be put"); break; } return NULL; }
static void hub_handle_command(char *buf, uint32_t len) { char *hub_my_nick; /* XXX */ hub_my_nick = main_to_hub_string(my_nick); if (len >= 6 && strncmp(buf, "$Lock ", 6) == 0) { char *key; if (!check_state(buf, (DCUserState) DC_HUB_LOCK)) goto hub_handle_command_cleanup; key = (char*) memmem(buf+6, len-6, " Pk=", 4); if (key == NULL) { warn(_("Invalid $Lock message: Missing Pk value\n")); key = buf+len; } key = decode_lock(buf+6, key-buf-6, DC_CLIENT_BASE_KEY); if (strleftcmp("EXTENDEDPROTOCOL", buf+6) == 0) { if (!hub_putf("$Supports TTHSearch NoGetINFO NoHello|")) { free(key); goto hub_handle_command_cleanup; } } if (!hub_putf("$Key %s|", key)) { free(key); goto hub_handle_command_cleanup; } free(key); if (!hub_putf("$ValidateNick %s|", hub_my_nick)) goto hub_handle_command_cleanup; hub_state = DC_HUB_HELLO; } else if (len >= 10 && strncmp(buf, "$Supports ", 10) == 0) { char *p0, *p1; hub_extensions = 0; for (p0 = buf+10; (p1 = strchr(p0, ' ')) != NULL; p0 = p1+1) { *p1 = '\0'; parse_hub_extension(p0); } if (*p0 != '\0') parse_hub_extension(p0); } else if (strcmp(buf, "$GetPass") == 0) { if (my_password == NULL) { screen_putf(_("Hub requires password.\n")); hub_disconnect(); goto hub_handle_command_cleanup; } screen_putf(_("Sending password to hub.\n")); if (!hub_putf("$MyPass %s|", my_password)) goto hub_handle_command_cleanup; } else if (strcmp(buf, "$BadPass") == 0) { warn(_("Password not accepted.\n")); hub_disconnect(); } else if (strcmp(buf, "$LogedIn") == 0) { screen_putf(_("You have received operator status.\n")); } else if (len >= 9 && strncmp(buf, "$HubName ", 9) == 0) { free(hub_name); hub_name = hub_to_main_string(buf + 9); screen_putf(_("Hub name is %s.\n"), quotearg(hub_name)); } else if (strcmp(buf, "$GetNetInfo") == 0) { hub_putf("$NetInfo %d$1$%c|", my_ul_slots, is_active ? 'A' : 'P'); } else if (strcmp(buf, "$ValidateDenide") == 0) { if (!check_state(buf, (DCUserState) DC_HUB_HELLO)) goto hub_handle_command_cleanup; /* DC++ disconnects immediately if this is received. * But shouldn't we give the client a chance to change the nick? * Also what happens if we receive this when correctly logged in? */ warn(_("Hub did not accept nick. Nick may be in use.\n")); hub_disconnect(); } else if (len >= 7 && strncmp(buf, "$Hello ", 7) == 0) { DCUserInfo *ui; char *conv_nick; conv_nick = hub_to_main_string(buf + 7); if (hub_state == DC_HUB_HELLO) { if (strcmp(buf+7, hub_my_nick) == 0) { screen_putf(_("Nick accepted. You are now logged in.\n")); } else { /* This probably won't happen, but better safe... */ free(my_nick); my_nick = xstrdup(conv_nick); free(hub_my_nick); hub_my_nick = xstrdup(buf + 7); screen_putf(_("Nick accepted but modified to %s. You are now logged in.\n"), quotearg(my_nick)); } ui = user_info_new(conv_nick); ui->info_quered = true; /* Hub is sending this automaticly */ hmap_put(hub_users, ui->nick, ui); free (conv_nick); if (!hub_putf("$Version 1,0091|")) goto hub_handle_command_cleanup; if (!hub_putf("$GetNickList|")) goto hub_handle_command_cleanup; if (!send_my_info()) goto hub_handle_command_cleanup; hub_state = DC_HUB_LOGGED_IN; } else { flag_putf(DC_DF_JOIN_PART, _("User %s logged in.\n"), quotearg(conv_nick)); ui = user_info_new(conv_nick); hmap_put(hub_users, ui->nick, ui); free (conv_nick); if ((hub_extensions & HUB_EXT_NOGETINFO) == 0) { if (!hub_putf("$GetINFO %s %s|", buf+7, hub_my_nick)) goto hub_handle_command_cleanup; ui->info_quered = true; } } } else if (len >= 8 && strncmp(buf, "$MyINFO ", 8) == 0) { DCUserInfo *ui; char *token; uint32_t len; char* conv_buf; char *work_buf; buf += 8; work_buf = conv_buf = hub_to_main_string(buf); token = strsep(&work_buf, " "); if (strcmp(token, "$ALL") != 0) { warn(_("Invalid $MyINFO message: Missing $ALL parameter, ignoring\n")); free(conv_buf); goto hub_handle_command_cleanup; } token = strsep(&work_buf, " "); if (token == NULL) { warn(_("Invalid $MyINFO message: Missing nick parameter, ignoring\n")); free(conv_buf); goto hub_handle_command_cleanup; } ui = (DCUserInfo*) hmap_get(hub_users, token); if (ui == NULL) { /* * if the full buf has not been converted from hub to local charset, * we should try to convert nick only */ /* char *conv_nick = hub_to_main_string(token); if ((ui = hmap_get(hub_users, conv_nick)) == NULL) { */ ui = user_info_new(token); ui->info_quered = true; hmap_put(hub_users, ui->nick, ui); /* } free(conv_nick); */ } token = strsep(&work_buf, "$"); if (token == NULL) { warn(_("Invalid $MyINFO message: Missing description parameter, ignoring\n")); free(conv_buf); goto hub_handle_command_cleanup; } free(ui->description); ui->description = xstrdup(token); token = strsep(&work_buf, "$"); if (token == NULL) { warn(_("Invalid $MyINFO message: Missing description separator, ignoring\n")); free(conv_buf); goto hub_handle_command_cleanup; } token = strsep(&work_buf, "$"); if (token == NULL) { warn(_("Invalid $MyINFO message: Missing connection speed, ignoring\n")); free(conv_buf); goto hub_handle_command_cleanup; } len = strlen(token); free(ui->speed); if (len == 0) { ui->speed = xstrdup(""); ui->level = 0; /* XXX: or 1? acceptable level? */ } else { ui->speed = xstrndup(token, len-1); ui->level = token[len-1]; } token = strsep(&work_buf, "$"); if (token == NULL) { warn(_("Invalid $MyINFO message: Missing e-mail address, ignoring\n")); free(conv_buf); goto hub_handle_command_cleanup; } free(ui->email); ui->email = xstrdup(token); token = strsep(&work_buf, "$"); if (token == NULL) { warn(_("Invalid $MyINFO message: Missing share size, ignoring\n")); free(conv_buf); goto hub_handle_command_cleanup; } if (!parse_uint64(token, &ui->share_size)) { warn(_("Invalid $MyINFO message: Invalid share size, ignoring\n")); free(conv_buf); goto hub_handle_command_cleanup; } if (ui->active_state == DC_ACTIVE_RECEIVED_PASSIVE || ui->active_state == DC_ACTIVE_KNOWN_ACTIVE) ui->active_state = DC_ACTIVE_UNKNOWN; /* XXX: Now that user's active_state may have changed, try queue again? */ free(conv_buf); } else if (strcmp(buf, "$HubIsFull") == 0) { warn(_("Hub is full.\n")); /* DC++ does not disconnect immediately. So I guess we won't either. */ /* Maybe we should be expecting an "hub follow" message? */ /* hub_disconnect(); */ } else if (len >= 3 && (buf[0] == '<' || strncmp(buf, " * ", 3) == 0)) { char *head; char *tail; char *msg; bool first = true; /* int scrwidth; size_t firstlen; size_t otherlen; screen_get_size(NULL, &scrwidth); firstlen = scrwidth - strlen(_("Public:")); otherlen = scrwidth - strlen(_(" | ")); */ msg = prepare_chat_string_for_display(buf); for (head = msg; (tail = strchr(head, '\n')) != NULL; head = tail+1) { /*PtrV *wrapped;*/ if (tail[-1] == '\r') /* end > buf here, buf[0] == '<' or ' ' */ tail[-1] = '\0'; else tail[0] = '\0'; /*wrapped = wordwrap(quotearg(buf), first ? firstlen : otherlen, otherlen); for (c = 0; c < wrapped->cur; c++) flag_putf(DC_DF_PUBLIC_CHAT, first ? _("Public: %s\n") : _(" | %s\n"), ); ptrv_foreach(wrapped, free); ptrv_free(wrapped);*/ flag_putf(DC_DF_PUBLIC_CHAT, first ? _("Public: %s\n") : _(" | %s\n"), quotearg(head)); first = false; } flag_putf(DC_DF_PUBLIC_CHAT, first ? _("Public: %s\n") : _(" | %s\n"), quotearg(head)); free(msg); } else if (len >= 5 && strncmp(buf, "$To: ", 5) == 0) { char *msg; char *tail; char *frm; char *head; bool first = true; msg = strchr(buf+5, '$'); if (msg == NULL) { warn(_("Invalid $To message: Missing text separator, ignoring\n")); goto hub_handle_command_cleanup; } *msg = '\0'; msg++; /* FIXME: WTF is this? Remove multiple "From: "? Why!? */ frm = buf+5; while ((tail = strstr(msg, "From: ")) != NULL && tail < msg) frm = tail+6; msg = prepare_chat_string_for_display(msg); frm = prepare_chat_string_for_display(frm); for (head = msg; (tail = strchr(head, '\n')) != NULL; head = tail+1) { if (tail[-1] == '\r') /* tail > buf here because head[0] == '<' or ' ' */ tail[-1] = '\0'; else tail[0] = '\0'; if (first) { screen_putf(_("Private: [%s] %s\n"), quotearg_n(0, frm), quotearg_n(1, head)); first = false; } else { screen_putf(_(" | %s\n"), quotearg(head)); } } if (first) { screen_putf(_("Private: [%s] %s\n"), quotearg_n(0, frm), quotearg_n(1, head)); } else { screen_putf(_(" | %s\n"), quotearg(head)); } free(msg); free(frm); } else if (len >= 13 && strncmp(buf, "$ConnectToMe ", 13) == 0) { struct sockaddr_in addr; buf += 13; if (strsep(&buf, " ") == NULL) { warn(_("Invalid $ConnectToMe message: Missing or invalid nick\n")); goto hub_handle_command_cleanup; } if (!parse_ip_and_port(buf, &addr, 0)) { warn(_("Invalid $ConnectToMe message: Invalid address specification.\n")); goto hub_handle_command_cleanup; } flag_putf(DC_DF_CONNECTIONS, _("Connecting to user on %s\n"), sockaddr_in_str(&addr)); user_connection_new(&addr, -1); } else if (len >= 16 && strncmp(buf, "$RevConnectToMe ", 16) == 0) { char *nick; char *local_nick; DCUserInfo *ui; nick = strtok(buf+16, " "); if (nick == NULL) { warn(_("Invalid $RevConnectToMe message: Missing nick parameter\n")); goto hub_handle_command_cleanup; } if (strcmp(nick, hub_my_nick) == 0) { warn(_("Invalid $RevConnectToMe message: Remote nick is our nick\n")); goto hub_handle_command_cleanup; } local_nick = hub_to_main_string(nick); ui = (DCUserInfo*) hmap_get(hub_users, local_nick); if (ui == NULL) { warn(_("Invalid $RevConnectToMe message: Unknown user %s, ignoring\n"), quotearg(local_nick)); free(local_nick); goto hub_handle_command_cleanup; } free(local_nick); if (ui->conn_count >= DC_USER_MAX_CONN) { warn(_("No more connections to user %s allowed.\n"), quotearg(ui->nick)); goto hub_handle_command_cleanup; } if (!is_active) { if (ui->active_state == DC_ACTIVE_SENT_PASSIVE) { warn(_("User %s is also passive. Cannot establish connection.\n"), quotearg(ui->nick)); /* We could set this to DC_ACTIVE_UNKNOWN too. This would mean * we would keep sending RevConnectToMe next time the download * queue is modified or some timer expired and told us to retry * download. The remote would then send back RevConnectToMe * and this would happen again. This way we would try again - * maybe remote has become active since last time we checked? * But no - DC++ only replies with RevConnectToMe to our first * RevConnectToMe. After that it ignores them all. */ ui->active_state = DC_ACTIVE_RECEIVED_PASSIVE; if (hmap_remove(pending_userinfo, ui->nick) != NULL) ui->refcount--; goto hub_handle_command_cleanup; } if (ui->active_state != DC_ACTIVE_RECEIVED_PASSIVE) { /* Inform remote that we are also passive. */ if (!hub_putf("$RevConnectToMe %s %s|", hub_my_nick, nick)) goto hub_handle_command_cleanup; } } ui->active_state = DC_ACTIVE_RECEIVED_PASSIVE; if (!hub_connect_user(ui)) goto hub_handle_command_cleanup; } else if ( (len >= 10 && strncmp(buf, "$NickList ", 10) == 0) || (len >= 8 && strncmp(buf, "$OpList ", 8) == 0) ) { char *nick; char *end; int oplist; char *conv_nick; if ( strncmp(buf, "$NickList ", 10) == 0) { nick = buf + 10; oplist = 0; } else { nick = buf + 8; oplist = 1; } for (; (end = strstr(nick, "$$")) != NULL; nick = end+2) { DCUserInfo *ui; *end = '\0'; conv_nick = hub_to_main_string(nick); ui = (DCUserInfo*) hmap_get(hub_users, conv_nick); if (ui == NULL) { ui = user_info_new(conv_nick); hmap_put(hub_users, ui->nick, ui); } free(conv_nick); if (!ui->info_quered && (hub_extensions & HUB_EXT_NOGETINFO) == 0) { if (!hub_putf("$GetINFO %s %s|", nick, hub_my_nick)) { goto hub_handle_command_cleanup; } ui->info_quered = true; } if (oplist) ui->is_operator = true; } } else if (len >= 6 && strncmp(buf, "$Quit ", 6) == 0) { DCUserInfo *ui; char *conv_nick = hub_to_main_string(buf+6); // I don't want these messages. //flag_putf(DC_DF_JOIN_PART, "User %s quits.\n", quotearg(conv_nick)); ui = (DCUserInfo*) hmap_remove(hub_users, conv_nick); if (ui == NULL) { /* Some hubs print quit messages for users that never joined, * so print this as debug only. */ flag_putf(DC_DF_DEBUG, _("Invalid $Quit message: Unknown user %s.\n"), quotearg(conv_nick)); } else user_info_free(ui); free(conv_nick); } else if (len >= 8 && strncmp(buf, "$Search ", 8) == 0) { char *source; int parse_result = 0; DCSearchSelection sel; sel.patterns = NULL; buf += 8; source = strsep(&buf, " "); if (source == NULL) { warn(_("Invalid $Search message: Missing source specification.\n")); goto hub_handle_command_cleanup; } /* charset handling is in parse_search_selection */ parse_result = parse_search_selection(buf, &sel); if (parse_result != 1) { if (sel.patterns != NULL) { int i = 0; for (i = 0; i < sel.patterncount; i++) { search_string_free(sel.patterns+i); } free(sel.patterns); } if (parse_result == 0) warn(_("Invalid $Search message: %s: Invalid search specification.\n"), buf); goto hub_handle_command_cleanup; } if (strncmp(source, "Hub:", 4) == 0) { DCUserInfo *ui; char *conv_nick = hub_to_main_string(source+4); ui = (DCUserInfo*) hmap_get(hub_users, conv_nick); if (ui == NULL) { warn(_("Invalid $Search message: Unknown user %s.\n"), quotearg(conv_nick)); if (sel.patterns != NULL) { int i = 0; for (i = 0; i < sel.patterncount; i++) { search_string_free(sel.patterns+i); } free(sel.patterns); } free(conv_nick); goto hub_handle_command_cleanup; } free(conv_nick); if (strcmp(ui->nick, my_nick) == 0) { if (sel.patterns != NULL) { int i = 0; for (i = 0; i < sel.patterncount; i++) { search_string_free(sel.patterns+i); } free(sel.patterns); } goto hub_handle_command_cleanup; } perform_inbound_search(&sel, ui, NULL); if (sel.patterns != NULL) { int i = 0; for (i = 0; i < sel.patterncount; i++) { search_string_free(sel.patterns+i); } free(sel.patterns); } } else { struct sockaddr_in addr; if (!parse_ip_and_port(source, &addr, DC_CLIENT_UDP_PORT)) { warn(_("Invalid $Search message: Invalid address specification.\n")); if (sel.patterns != NULL) { int i = 0; for (i = 0; i < sel.patterncount; i++) { search_string_free(sel.patterns+i); } free(sel.patterns); } goto hub_handle_command_cleanup; } if (local_addr.sin_addr.s_addr == addr.sin_addr.s_addr && listen_port == ntohs(addr.sin_port)) { if (sel.patterns != NULL) { int i = 0; for (i = 0; i < sel.patterncount; i++) { search_string_free(sel.patterns+i); } free(sel.patterns); } goto hub_handle_command_cleanup; } perform_inbound_search(&sel, NULL, &addr); if (sel.patterns != NULL) { int i = 0; for (i = 0; i < sel.patterncount; i++) { search_string_free(sel.patterns+i); } free(sel.patterns); } } } else if (len >= 4 && strncmp(buf, "$SR ", 4) == 0) { handle_search_result(buf, len); } else if (len == 0) { /* Ignore empty commands. */ } hub_handle_command_cleanup: free(hub_my_nick); }
struct variable *vm_putg(struct ymd_mach *vm, const char *field) { struct variable k, *v; setv_kstr(&k, kstr_fetch(vm, field, -1)); v = hmap_put(vm, vm->global, &k); return v; }
// Test count function int test_hmap_4() { hmap_handle* hmap; hmap = hmap_create(); int* data1; int* data2; int* data3; int* data4; data1 = new(int); *data1 = 0; hmap_put(hmap, (long) (*data1), data1); if (hmap_count(hmap) != 1) { kfree(data1); hmap_free(hmap); return TEST_FAIL; } data2 = new(int); *data2 = 1; hmap_put(hmap, (long) (*data2), data2); if (hmap_count(hmap) != 2) { kfree(data1); kfree(data2); hmap_free(hmap); return TEST_FAIL; } data3 = new(int); *data3 = 2; hmap_put(hmap, (long) (*data3), data3); if (hmap_count(hmap) != 3) { kfree(data1); kfree(data2); kfree(data3); hmap_free(hmap); return TEST_FAIL; } kfree(hmap_remove(hmap, (long) (*data2))); if (hmap_count(hmap) != 2) { kfree(data1); kfree(data3); hmap_free(hmap); return TEST_FAIL; } data4 = new(int); *data4 = 3; hmap_put(hmap, (long) (*data4), data4); if (hmap_count(hmap) != 3) { kfree(data1); kfree(data3); kfree(data4); hmap_free(hmap); return TEST_FAIL; } kfree(data1); kfree(data2); kfree(data3); kfree(data4); hmap_free(hmap); return TEST_OK; }
DCUserConn * user_connection_new(struct sockaddr_in *addr, int user_socket) { DCUserConn *uc; int get_fd[2] = { -1, -1 }; int put_fd[2] = { -1, -1 }; pid_t pid; if (pipe(get_fd) != 0 || pipe(put_fd) != 0) { warn(_("Cannot create pipe pair - %s\n"), errstr); goto cleanup; } pid = fork(); if (pid < 0) { warn(_("Cannot create process - %s\n"), errstr); goto cleanup; } if (pid == 0) user_main(put_fd, get_fd, addr, user_socket); if (close(get_fd[1]) != 0 || close(put_fd[0]) != 0) warn(_("Cannot close pipe - %s\n"), errstr); /* Non-blocking mode is not required, but there may be some latency otherwise. */ if (!fd_set_nonblock_flag(get_fd[0], true) || !fd_set_nonblock_flag(put_fd[1], true)) warn(_("Cannot set non-blocking flag - %s\n"), errstr); /* The user socket is only used by the newly created user process. */ if (user_socket >= 0 && close(user_socket) < 0) warn(_("Cannot close socket - %s\n"), errstr); uc = (DCUserConn*) xmalloc(sizeof(DCUserConn)); uc->pid = pid; uc->info = NULL; uc->occupied_slot = 0; uc->occupied_minislot = 0; uc->dir = DC_DIR_UNKNOWN; uc->transfer_file = NULL; uc->local_file = NULL; uc->transfer_start = 0; uc->transfer_pos = 0; uc->transfer_total = 0; uc->transfer_time = (time_t) -1; uc->transferring = false; uc->queue_pos = 0; uc->queued_valid = false; /* uc->we_connected = (user_socket < 0); */ uc->get_mq = msgq_new(get_fd[0]); uc->put_mq = msgq_new(put_fd[1]); FD_SET(uc->get_mq->fd, &read_fds); if (user_conn_unknown_free->cur > 0) { uc->name = (char*) ptrv_remove_first(user_conn_unknown_free); } else { /* TRANSLATORS: This represents the connection name used when * the user name is not yet known. It must not contains '|', * because that's used to distinguish between 'unknown' and * (perhaps partially) identified connections. */ uc->name = xasprintf(_("unknown%" PRIu32), user_conn_unknown_last+1); user_conn_unknown_last++; } hmap_put(user_conns, uc->name, uc); return uc; cleanup: if (get_fd[0] != -1) close(get_fd[0]); if (get_fd[1] != -1) close(get_fd[1]); if (put_fd[0] != -1) close(put_fd[0]); if (put_fd[1] != -1) close(put_fd[1]); return NULL; }