/* Loads a diceware dictionary file. Returns 0 on success and -1 on error. */ int load_dictionary(const char *filename, struct htable *dict) { FILE *fp; int rc = -1; struct htable_entry *entry; char line[256]; if ((fp = fopen(filename, "r")) == NULL) { fprintf(stderr, "could not open %s: %s\n", filename, strerror(errno)); goto cleanup; } while (1) { if (!fgets(line, sizeof(line), fp)) break; /* Skip invalid lines. */ if ((entry = parse_line(line)) == NULL) continue; htable_put(dict, entry); } rc = 0; cleanup: if (fp) fclose(fp); return rc; }
static void map_write_dimension(zval *obj, zval *offset, zval *value) { Map *map = Z_MAP_P(obj); if (offset == NULL) { ARRAY_ACCESS_PUSH_NOT_SUPPORTED(); return; } htable_put(map->table, offset, value); }
int span_table_put(struct span_table *st, struct htrace_span *span) { struct htable *ht = (struct htable *)st; int res; res = htable_put(ht, span->desc, span); if (res) { htrace_span_free(span); return res; } return 0; }
static struct htable *htable_from_str(const char *str) { struct htable *ht; char *cstr = NULL, *saveptr = NULL, *tok; int ret = ENOMEM; ht = htable_alloc(8, ht_hash_string, ht_compare_string); if (!ht) { goto done; } if (!str) { ret = 0; goto done; } cstr = strdup(str); if (!cstr) { goto done; } for (tok = strtok_r(cstr, ";", &saveptr); tok; tok = strtok_r(NULL, ";", &saveptr)) { char *key = NULL, *val = NULL; ret = parse_key_value(tok, &key, &val); if (ret) { goto done; } ret = htable_put(ht, key, val); if (ret) { goto done; } } ret = 0; done: if (ret) { htable_free(ht); ht = NULL; } free(cstr); return ht; }
//if there is no entry, then an entry is added and 1 is returned //if the entry in the routing table is already better, 0 is returned //if the given information is better, the table is updated and 1 is returned. //NOTE: this DOES NOT lock the routing table mutex. //printmode=0 to print a full line ("route: this happened"), 1 to print shortened line. int update_routing_table(struct in_addr destAddress, uint8_t cost, interface_t *supplier_int, int printmode) { routeentry_t *curentry, *newentry; if (cost >= ROUTE_INFINITY) cost = ROUTE_INFINITY; curentry = (routeentry_t *)htable_get(g_routeTable, destAddress.s_addr); if (curentry != NULL) { //if the supplier is the same... if (curentry->interface == supplier_int) { //if the cost is the same, we're verifying an existing entry. if (curentry->cost == cost) { if (printmode==0) dbg(DBG_ROUTE, "route: Refreshing entry for %s, cost still %d.\n", inet_ntoa_host(destAddress), curentry->cost); else dbg(DBG_ROUTE, "refreshed to %d.\n", curentry->cost); curentry->lastRefreshTime = time(NULL); return 0; } //otherwise, we're updating it - a route changed / link went down, etc. //NOTE: This seems to fix problems with split horizon //since if a node you connected to suddenly lost route, you also assume //you lose route unless you get an update from someone else. if (curentry->cost > cost) { if (printmode==0) dbg(DBG_ROUTE, "route: Found better route to %s from %d to %d.\n", inet_ntoa_host(destAddress), curentry->cost, cost); else dbg(DBG_ROUTE, "improved cost to %d\n", curentry->cost); curentry->cost = cost; curentry->lastRefreshTime = time(NULL); return 1; } /*if (curentry->cost == 1) { dbg(DBG_ROUTE, "route: Not worsening route of cost 1 - direct link.\n"); return 0; }*/ if (printmode==0) dbg(DBG_ROUTE, "route: Route to %s worsened from %d to %d\n", inet_ntoa_host(destAddress), curentry->cost, cost); else dbg(DBG_ROUTE, "worsened from %d\n", curentry->cost); curentry->cost = cost; curentry->lastRefreshTime = time(NULL); return 1; } //if it's not, maybe we have a better route from somewhere else. if (curentry->cost > cost) { if (printmode==0) dbg(DBG_ROUTE, "route: Found better route to %s from a different interface, cost from %d to %d.\n", inet_ntoa_host(destAddress), curentry->cost, cost); else dbg(DBG_ROUTE, "improved from %d (diff route)\n", curentry->cost); //we found a better route // Replace the old value with the new value curentry->cost = cost; curentry->interface = supplier_int; curentry->lastRefreshTime = time(NULL); return 1; } else { if (printmode==0) dbg(DBG_ROUTE, "route: Not updating with entry from different interfaces, ours is <= it.\n"); else dbg(DBG_ROUTE, ">= ours, not updating\n"); } } else { if (printmode==0) dbg(DBG_ROUTE, "route: Found new route to %s, cost=%d.\n", inet_ntoa_host(destAddress), cost); else dbg(DBG_ROUTE, "new route\n"); //we found a new route. // Put in a new value free(curentry); newentry = (routeentry_t *)malloc(sizeof(routeentry_t)); newentry->interface = supplier_int; newentry->cost = cost; newentry->lastRefreshTime = time(NULL); htable_put(g_routeTable, destAddress.s_addr, newentry); return 1; } return 0; }