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.
17 #include <netinet/in.h>
18 #include <arpa/nameser.h>
22 *pci_id_net_lookup(struct pci_access *a, int cat, int id1, int id2, int id3, int id4)
24 static int resolver_inited;
25 char name[256], dnsname[256], txt[256], *domain;
32 domain = pci_get_param(a, "net.domain");
33 if (!domain || !domain[0])
39 sprintf(name, "%04x", id1);
42 sprintf(name, "%04x.%04x", id2, id1);
45 sprintf(name, "%04x.%04x.%04x.%04x", id4, id3, id2, id1);
47 case ID_GEN_SUBSYSTEM:
48 sprintf(name, "%04x.%04x.s", id2, id1);
51 sprintf(name, "%02x.c", id1);
54 sprintf(name, "%02x.%02x.c", id2, id1);
57 sprintf(name, "%02x.%02x.%02x.c", id3, id2, id1);
62 sprintf(dnsname, "%s.%s", name, domain);
64 a->debug("Resolving %s\n", dnsname);
70 res = res_query(dnsname, ns_c_in, ns_t_txt, answer, sizeof(answer));
73 a->debug("\tfailed, h_errno=%d\n", _res.res_h_errno);
76 if (ns_initparse(answer, res, &m) < 0)
78 a->debug("\tinitparse failed\n");
81 for (i=0; ns_parserr(&m, ns_s_an, i, &rr) >= 0; i++)
83 a->debug("\tanswer %d (class %d, type %d)\n", i, ns_rr_class(rr), ns_rr_type(rr));
84 if (ns_rr_class(rr) != ns_c_in || ns_rr_type(rr) != ns_t_txt)
86 data = ns_rr_rdata(rr);
87 dlen = ns_rr_rdlen(rr);
89 while (j < dlen && j+1+data[j] <= dlen)
91 memcpy(txt, &data[j+1], data[j]);
94 a->debug("\t\t%s\n", txt);
95 if (txt[0] == 'i' && txt[1] == '=')
105 char *pci_id_net_lookup(struct pci_access *a UNUSED, int cat UNUSED, int id1 UNUSED, int id2 UNUSED, int id3 UNUSED, int id4 UNUSED)