diff --git a/include/names.h b/include/names.h index 4123d0b0..6fed5818 100644 --- a/include/names.h +++ b/include/names.h @@ -16,7 +16,8 @@ struct db_names { int max; }; -struct db_names *db_names_alloc(const char *path); +struct db_names *db_names_alloc(void); +int db_names_load(struct db_names *db, const char *path); void db_names_free(struct db_names *db); char *id_to_name(struct db_names *db, int id, char *name); diff --git a/ip/iplink.c b/ip/iplink.c index 5893ee40..e6f30e99 100644 --- a/ip/iplink.c +++ b/ip/iplink.c @@ -72,7 +72,7 @@ void iplink_usage(void) fprintf(stderr, " [ mtu MTU ]\n"); fprintf(stderr, " [ netns PID ]\n"); fprintf(stderr, " [ netns NAME ]\n"); - fprintf(stderr, " [ link-netnsid ID ]\n"); + fprintf(stderr, " [ link-netnsid ID ]\n"); fprintf(stderr, " [ alias NAME ]\n"); fprintf(stderr, " [ vf NUM [ mac LLADDR ]\n"); fprintf(stderr, " [ vlan VLANID [ qos VLAN-QOS ] ]\n"); diff --git a/lib/names.c b/lib/names.c index 93933f74..3b5b0b1e 100644 --- a/lib/names.c +++ b/lib/names.c @@ -11,8 +11,10 @@ #include #include #include +#include #include "names.h" +#include "utils.h" #define MAX_ENTRIES 256 #define NAME_MAX_LEN 512 @@ -48,48 +50,65 @@ static int read_id_name(FILE *fp, int *id, char *name) return 0; } -struct db_names *db_names_alloc(const char *path) +struct db_names *db_names_alloc(void) { struct db_names *db; - struct db_entry *entry; - FILE *fp; - int id; - char namebuf[NAME_MAX_LEN] = {0}; - int ret; - - fp = fopen(path, "r"); - if (!fp) { - fprintf(stderr, "Can't open file: %s\n", path); - return NULL; - } db = malloc(sizeof(*db)); + if (!db) + return NULL; + memset(db, 0, sizeof(*db)); db->size = MAX_ENTRIES; db->hash = malloc(sizeof(struct db_entry *) * db->size); memset(db->hash, 0, sizeof(struct db_entry *) * db->size); + return db; +} + +int db_names_load(struct db_names *db, const char *path) +{ + struct db_entry *entry; + FILE *fp; + int id; + char namebuf[NAME_MAX_LEN] = {0}; + int ret = -1; + + fp = fopen(path, "r"); + if (!fp) + return -ENOENT; + while ((ret = read_id_name(fp, &id, &namebuf[0]))) { if (ret == -1) { fprintf(stderr, "Database %s is corrupted at %s\n", path, namebuf); - fclose(fp); - return NULL; + goto Exit; } + ret = -1; if (id < 0) continue; entry = malloc(sizeof(*entry)); - entry->id = id; + if (!entry) + goto Exit; + entry->name = strdup(namebuf); + if (!entry->name) { + free(entry); + goto Exit; + } + + entry->id = id; entry->next = db->hash[id & (db->size - 1)]; db->hash[id & (db->size - 1)] = entry; } + ret = 0; +Exit: fclose(fp); - return db; + return ret; } void db_names_free(struct db_names *db) @@ -117,8 +136,12 @@ void db_names_free(struct db_names *db) char *id_to_name(struct db_names *db, int id, char *name) { - struct db_entry *entry = db->hash[id & (db->size - 1)]; + struct db_entry *entry; + if (!db) + return NULL; + + entry = db->hash[id & (db->size - 1)]; while (entry && entry->id != id) entry = entry->next; @@ -136,6 +159,9 @@ int name_to_id(struct db_names *db, int *id, const char *name) struct db_entry *entry; int i; + if (!db) + return -1; + if (db->cached && strcmp(db->cached->name, name) == 0) { *id = db->cached->id; return 0; @@ -145,6 +171,7 @@ int name_to_id(struct db_names *db, int *id, const char *name) entry = db->hash[i]; while (entry && strcmp(entry->name, name)) entry = entry->next; + if (entry) { db->cached = entry; *id = entry->id; diff --git a/man/man8/arpd.8 b/man/man8/arpd.8 index fc99b97e..5050a98b 100644 --- a/man/man8/arpd.8 +++ b/man/man8/arpd.8 @@ -35,7 +35,7 @@ Suppress sending broadcast queries by the kernel. This option only makes sense t Specifies the timeout of the negative cache. When resolution fails, arpd suppresses further attempts to resolve for this period. This option only makes sense together with option '-k'. This timeout should not be too much longer than the boot time of a typical host not supporting gratuitous ARP. Default value is 60 seconds. .TP -p