memset(a, 0, sizeof(*a));
pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0);
- pci_set_net_domain(a, PCI_ID_DOMAIN, 0);
+#ifdef PCI_USE_DNS
+ pci_define_param(a, "net.domain", PCI_ID_DOMAIN);
+ pci_define_param(a, "net.cache_path", "~/.pciids-cache");
a->id_lookup_mode = PCI_LOOKUP_CACHE;
+#endif
for(i=0; i<PCI_ACCESS_MAX; i++)
if (pci_methods[i] && pci_methods[i]->config)
pci_methods[i]->config(a);
}
int
-pci_set_param(struct pci_access *acc, char *param, char *value)
+pci_set_param_internal(struct pci_access *acc, char *param, char *value, int copy)
{
struct pci_param *p;
{
if (p->value_malloced)
pci_mfree(p->value);
- p->value_malloced = 1;
- p->value = pci_strdup(acc, value);
+ p->value_malloced = copy;
+ if (copy)
+ p->value = pci_strdup(acc, value);
+ else
+ p->value = value;
return 0;
}
return -1;
}
+int
+pci_set_param(struct pci_access *acc, char *param, char *value)
+{
+ return pci_set_param_internal(acc, param, value, 1);
+}
+
static void
pci_free_params(struct pci_access *acc)
{
pci_free_name_list(a);
pci_free_params(a);
pci_set_name_list_path(a, NULL, 0);
- pci_set_net_domain(a, NULL, 0);
- pci_set_id_cache(a, NULL, 0);
pci_mfree(a);
}
static const char cache_version[] = "#PCI-CACHE-1.0";
+static char *get_cache_name(struct pci_access *a)
+{
+ char *name, *buf;
+
+ name = pci_get_param(a, "net.cache_path");
+ if (!name || name[0])
+ return NULL;
+ if (strncmp(name, "~/", 2))
+ return name;
+
+ uid_t uid = getuid();
+ struct passwd *pw = getpwuid(uid);
+ if (!pw)
+ return name;
+
+ buf = pci_malloc(a, strlen(pw->pw_dir) + strlen(name+1) + 1);
+ sprintf(buf, "%s%s", pw->pw_dir, name+1);
+ pci_set_param_internal(a, "net.cache_path", buf, 0);
+ return buf;
+}
+
int
pci_id_cache_load(struct pci_access *a, int flags)
{
char *name;
char line[MAX_LINE];
- const char default_name[] = "/.pciids-cache";
FILE *f;
int lino;
a->id_cache_status = 1;
- if (!a->id_cache_file)
- {
- /* Construct the default ID cache name */
- uid_t uid = getuid();
- struct passwd *pw = getpwuid(uid);
- if (!pw)
- return 0;
- name = pci_malloc(a, strlen(pw->pw_dir) + sizeof(default_name));
- sprintf(name, "%s%s", pw->pw_dir, default_name);
- pci_set_id_cache(a, name, 1);
- }
- a->debug("Using cache %s\n", a->id_cache_file);
+ name = get_cache_name(a);
+ if (!name)
+ return 0;
+ a->debug("Using cache %s\n", name);
if (flags & PCI_LOOKUP_REFRESH_CACHE)
{
a->debug("Not loading cache, will refresh everything\n");
return 0;
}
- f = fopen(a->id_cache_file, "rb");
+ f = fopen(name, "rb");
if (!f)
{
a->debug("Cache file does not exist\n");
}
}
}
- a->warning("Malformed cache file %s (line %d), ignoring", a->id_cache_file, lino);
+ a->warning("Malformed cache file %s (line %d), ignoring", name, lino);
break;
}
if (ferror(f))
- a->warning("Error while reading %s", a->id_cache_file);
+ a->warning("Error while reading %s", name);
fclose(f);
return 1;
}
FILE *f;
unsigned int h;
struct id_entry *e, *e2;
- char hostname[256], *tmpname;
+ char hostname[256], *tmpname, *name;
int this_pid;
a->id_cache_status = 0;
if (orig_status < 2)
return;
- if (!a->id_cache_file)
+ name = get_cache_name(a);
+ if (!name)
return;
this_pid = getpid();
hostname[0] = 0;
else
hostname[sizeof(hostname)-1] = 0;
- tmpname = pci_malloc(a, strlen(a->id_cache_file) + strlen(hostname) + 64);
- sprintf(tmpname, "%s.tmp-%s-%d", a->id_cache_file, hostname, this_pid);
+ tmpname = pci_malloc(a, strlen(name) + strlen(hostname) + 64);
+ sprintf(tmpname, "%s.tmp-%s-%d", name, hostname, this_pid);
f = fopen(tmpname, "wb");
if (!f)
{
- a->warning("Cannot write to %s: %s", a->id_cache_file, strerror(errno));
+ a->warning("Cannot write to %s: %s", name, strerror(errno));
pci_mfree(tmpname);
return;
}
- a->debug("Writing cache to %s\n", a->id_cache_file);
+ a->debug("Writing cache to %s\n", name);
fprintf(f, "%s\n", cache_version);
for (h=0; h<HASH_SIZE; h++)
fflush(f);
if (ferror(f))
- a->warning("Error writing %s", a->id_cache_file);
+ a->warning("Error writing %s", name);
fclose(f);
- if (rename(tmpname, a->id_cache_file) < 0)
+ if (rename(tmpname, name) < 0)
{
- a->warning("Cannot rename %s to %s: %s", tmpname, a->id_cache_status, strerror(errno));
+ a->warning("Cannot rename %s to %s: %s", tmpname, name, strerror(errno));
unlink(tmpname);
}
pci_mfree(tmpname);
if (a->id_cache_status >= 1)
a->id_cache_status = 2;
}
-
-void
-pci_set_id_cache(struct pci_access *a, char *name, int to_be_freed)
-{
- if (a->free_id_cache_file)
- free(a->id_cache_file);
- a->id_cache_file = name;
- a->free_id_cache_file = to_be_freed;
-}
char
*pci_id_net_lookup(struct pci_access *a, int cat, int id1, int id2, int id3, int id4)
{
- char name[256], dnsname[256], txt[256];
+ char name[256], dnsname[256], txt[256], *domain;
byte answer[4096];
const byte *data;
int res, i, j, dlen;
ns_msg m;
ns_rr rr;
+ domain = pci_get_param(a, "net.domain");
+ if (!domain || !domain[0])
+ return NULL;
+
switch (cat)
{
case ID_VENDOR:
default:
return NULL;
}
- sprintf(dnsname, "%s.%s", name, a->id_domain);
+ sprintf(dnsname, "%s.%s", name, domain);
a->debug("Resolving %s\n", dnsname);
res_init();
}
#endif
-
-void
-pci_set_net_domain(struct pci_access *a, char *name, int to_be_freed)
-{
- if (a->free_id_domain)
- free(a->id_domain);
- a->id_domain = name;
- a->free_id_domain = to_be_freed;
-}
int numeric_ids; /* Enforce PCI_LOOKUP_NUMERIC (>1 => PCI_LOOKUP_MIXED) */
unsigned int id_lookup_mode; /* pci_lookup_mode flags which are set automatically */
- /* Default: PCI_LOOKUP_CACHE */
- char *id_domain; /* DNS domain used for the lookups (use pci_set_net_domain()) */
- int free_id_domain; /* Set if id_domain is malloced */
- char *id_cache_file; /* Name of the ID cache file (use pci_set_net_cache()) */
- int free_id_cache_file; /* Set if id_cache_file is malloced */
+ /* Default: PCI_LOOKUP_CACHE */
int debugging; /* Turn on debugging messages */
int pci_load_name_list(struct pci_access *a); /* Called automatically by pci_lookup_*() when needed; returns success */
void pci_free_name_list(struct pci_access *a); /* Called automatically by pci_cleanup() */
void pci_set_name_list_path(struct pci_access *a, char *name, int to_be_freed);
-void pci_set_net_domain(struct pci_access *a, char *name, int to_be_freed);
-void pci_set_id_cache(struct pci_access *a, char *name, int to_be_freed);
void pci_id_cache_flush(struct pci_access *a);
enum pci_lookup_mode {