2 * The PCI Library -- Resolving ID's via DNS
4 * Copyright (c) 2007--2008 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
11 #include <netinet/in.h>
12 #include <arpa/nameser.h>
19 *pci_id_net_lookup(struct pci_access *a, int cat, int id1, int id2, int id3, int id4)
21 char name[256], dnsname[256], txt[256];
31 sprintf(name, "%04x", id1);
34 sprintf(name, "%04x.%04x", id2, id1);
37 sprintf(name, "%04x.%04x.%04x.%04x", id4, id3, id2, id1);
39 case ID_GEN_SUBSYSTEM:
40 sprintf(name, "%04x.%04x.s", id2, id1);
43 sprintf(name, "%02x.c", id1);
46 sprintf(name, "%02x.%02x.c", id2, id1);
49 sprintf(name, "%02x.%02x.%02x.c", id3, id2, id1);
54 sprintf(dnsname, "%s.%s", name, a->id_domain);
56 a->debug("Resolving %s\n", dnsname);
58 res = res_query(dnsname, ns_c_in, ns_t_txt, answer, sizeof(answer));
61 a->debug("\tfailed, h_errno=%d\n", _res.res_h_errno);
64 if (ns_initparse(answer, res, &m) < 0)
66 a->debug("\tinitparse failed\n");
69 for (i=0; ns_parserr(&m, ns_s_an, i, &rr) >= 0; i++)
71 a->debug("\tanswer %d (class %d, type %d)\n", i, ns_rr_class(rr), ns_rr_type(rr));
72 if (ns_rr_class(rr) != ns_c_in || ns_rr_type(rr) != ns_t_txt)
74 data = ns_rr_rdata(rr);
75 dlen = ns_rr_rdlen(rr);
77 while (j < dlen && j+1+data[j] <= dlen)
79 memcpy(txt, &data[j+1], data[j]);
82 a->debug("\t\t%s\n", txt);
83 if (txt[0] == 'i' && txt[1] == '=')
92 pci_set_net_domain(struct pci_access *a, char *name, int to_be_freed)
94 if (a->free_id_domain)
97 a->free_id_domain = to_be_freed;