int single_logout(single_login_t *sl, single_logout_func_t cb, char *ip, int port, void *param) { if (sl == NULL) return -1; char ip_port[32]; single_login_info_t *node; snprintf(ip_port, sizeof(ip_port), "%s:%d", ip, port); single_logout_func_t logout_func = (cb == NULL) ? default_logout_func : cb; pthread_mutex_lock(&sl->mtx); if (dlist_entry(sl->list, cmp_ipport, (void *)ip_port, (void **)&node) == -1) { pthread_mutex_unlock(&sl->mtx); return -1; } if (--node->user_num <= 0) dlist_del(&sl->list, cmp_ipport, (void *)ip_port, logout_func, param); pthread_mutex_unlock(&sl->mtx); return 0; }
int single_login(single_login_t *sl, single_login_func_t cb, char *ip, int port, char *user, char *pwd, void *param) { if (sl == NULL || ip == NULL || port <= 0 || port >= 65535) return -1; int loginid = -1; char ip_port[32] = ""; single_login_info_t *node; snprintf(ip_port, sizeof(ip_port), "%s:%d", ip, port); single_login_func_t login_func = (cb == NULL) ? default_login_func : cb; pthread_mutex_lock(&sl->mtx); /* lookup ip:port, if exist, return the loginid directly */ if (dlist_entry(sl->list, cmp_ipport, (void *)ip_port, (void **)&node) == 0) { loginid = node->loginid; node->user_num++; pthread_mutex_unlock(&sl->mtx); return loginid; } /* login */ loginid = login_func(ip, port, user, pwd, param); if (loginid == -1) { pthread_mutex_unlock(&sl->mtx); return -1; } node = calloc(1, sizeof(single_login_info_t)); if (node == NULL) { close(loginid); pthread_mutex_unlock(&sl->mtx); return -1; } snprintf(node->ip_port, sizeof(node->ip_port), "%s:%d", ip, port); node->loginid = loginid; node->user_num = 1; node->param = param; if (dlist_add(&sl->list, (void *)node) == -1) { close(loginid); free(node); pthread_mutex_unlock(&sl->mtx); return -1; } pthread_mutex_unlock(&sl->mtx); return loginid; }
inline int hash_entry(hash_table_t *table, void *data, ds_cmpfunc_t cmp, void *val, void **item) { unsigned int index; if (table == NULL || item == NULL) return -1; index = table->hashfunc(table->size, data); return dlist_entry(table->table[index], cmp, val, item); }
static device_t *get_device(driver_t *driver, int id) { device_t *device; if (dlist_entry(driver->devices, cmp_func_device, (void *)id, (void **)&device) == -1) { //printf("no device: %d-%d\n", driver->type, id); return NULL; } return device; }
static channel_t *get_channel(driver_t *driver, device_t *device, int id) { channel_t *channel; if (dlist_entry(device->channels, cmp_func_channel, (void *)id, (void **)&channel) == -1) { //printf("no channel: %d-%d-%d\n", // driver->type, device->id, id); return NULL; } return channel; }
static void free_i18n_strings(void) { struct dlist_head * entry; i18nstr_t * istr; while (!dlist_empty(&g_i18n_strings)) { entry = g_i18n_strings.next; dlist_del(entry); istr = dlist_entry(entry, i18nstr_t, list); if (istr->string) FREE(istr->string); FREE(istr); } }
void sealpac_puts(const char * string) { MD5_CTX ctx; i18nstr_t * istr; i18nstr_t * newstr; struct dlist_head * entry; int comp; if (!string) return; if (strlen(string)<=0) return; /* Allocate a new entry */ newstr = MALLOC(sizeof(i18nstr_t)); if (!newstr) { printf("%s: memory allocation failed!\n",__func__); return; } /* fill up this entry */ newstr->string = STRDUP(string); if (!newstr->string) { printf("%s: memory allocation failed!\n",__func__); return; } MD5Init(&ctx); MD5Update(&ctx, (uint8_t *)newstr->string, strlen(newstr->string)); MD5Final(newstr->digest, &ctx); /* Look for insert point */ entry = NULL; while ((entry = dlist_get_next(entry, &g_i18n_strings))) { istr = dlist_entry(entry, i18nstr_t, list); comp = compare_i18nstr(istr, newstr); if (comp > 0) /* istr > newstr */ { dlist_add_tail(&newstr->list, entry); if (o_verbose > 1) verbose("Add string '%s'\n",newstr->string); return; } else if (comp == 0) /* digest conflict */ { if (strcmp(newstr->string, istr->string)==0) { if (o_verbose > 1) verbose("Duplicated string : '%s'\n", string); } else { printf("ERROR!! Digest Conflict. Please change the string '%s'\n",string); } FREE(newstr->string); FREE(newstr); return; } } dlist_add_tail(&newstr->list, &g_i18n_strings); if (o_verbose > 1) verbose("Add string '%s'\n",newstr->string); return; }
static void dump_i18n_strings(void) { struct dlist_head * entry; i18nstr_t * istr; size_t i, num; uint32_t offset; sealpac_hdr sphdr; sealpac_str * spstr = NULL; FILE * fh = NULL; MD5_CTX ctx; /* dump the string list. */ if (o_verbose > 2) { entry = NULL; while ((entry = dlist_get_next(entry, &g_i18n_strings))) { istr = dlist_entry(entry, i18nstr_t, list); verbose("["); for (i=0; i<16; i++) verbose("%02x", istr->digest[i]); verbose("] : [%s]\n", istr->string); } } num = count_i18n_strings(); printf("Number of strings : %d\n", num); do { if (num == 0) break; if (o_targetpath == NULL) break; /* Setup sealpac header */ memset(&sphdr, 0, sizeof(sphdr)); sphdr.magic = htonl(SEALPAC_MAGIC); sphdr.num = htonl(num); strncpy(sphdr.langcode, o_langcode, sizeof(sphdr.langcode)); /* Allocate sealpac string head */ spstr = MALLOC(sizeof(sealpac_str) * num); if (!spstr) ERRBREAK("%s: Memory Allocation Failed!\n",__func__); memset(spstr, 0, sizeof(sealpac_str) * num); /* Walk through all the string to prepare sealpac string heads */ i = 0; offset = sizeof(sealpac_hdr) + sizeof(sealpac_str) * num; entry = NULL; while ((entry = dlist_get_next(entry, &g_i18n_strings))) { istr = dlist_entry(entry, i18nstr_t, list); memcpy(spstr[i].hash, istr->digest, 16); spstr[i].offset = htonl(offset); offset += (strlen(istr->string) + 1); i++; } /* Calculate the checksum (MD5 digest) */ MD5Init(&ctx); MD5Update(&ctx, (uint8_t *)spstr, sizeof(sealpac_str) * num); entry = NULL; while ((entry = dlist_get_next(entry, &g_i18n_strings))) { istr = dlist_entry(entry, i18nstr_t, list); MD5Update(&ctx, (uint8_t *)istr->string, strlen(istr->string)+1); } MD5Final(sphdr.checksum, &ctx); /* Open the file to write */ if (o_slpfile == NULL) ERRBREAK("No output file !\n"); fh = fopen(o_slpfile, "w+"); if (fh == NULL) ERRBREAK("ERROR!!! Unable to open output file '%s', %s\n", o_slpfile, strerror(errno)); /* write to the langpack file. */ fwrite(&sphdr, sizeof(sealpac_hdr), 1, fh); fwrite(spstr, sizeof(sealpac_str), num, fh); entry = NULL; while ((entry = dlist_get_next(entry, &g_i18n_strings))) { istr = dlist_entry(entry, i18nstr_t, list); fwrite(istr->string, sizeof(char), strlen(istr->string)+1, fh); } } while (0); if (spstr) FREE(spstr); if (fh) fclose(fh); }