]> mj.ucw.cz Git - pciutils.git/blob - lib/names-net.c
Bump the API version (the new API is not cast in stone yet, however).
[pciutils.git] / lib / names-net.c
1 /*
2  *      The PCI Library -- Resolving ID's via DNS
3  *
4  *      Copyright (c) 2007--2008 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL.
7  */
8
9 #include <string.h>
10 #include <stdlib.h>
11
12 #include "internal.h"
13 #include "names.h"
14
15 #ifdef PCI_USE_DNS
16
17 #include <netinet/in.h>
18 #include <arpa/nameser.h>
19 #include <resolv.h>
20
21 char
22 *pci_id_net_lookup(struct pci_access *a, int cat, int id1, int id2, int id3, int id4)
23 {
24   char name[256], dnsname[256], txt[256], *domain;
25   byte answer[4096];
26   const byte *data;
27   int res, i, j, dlen;
28   ns_msg m;
29   ns_rr rr;
30
31   domain = pci_get_param(a, "net.domain");
32   if (!domain || !domain[0])
33     return NULL;
34
35   switch (cat)
36     {
37     case ID_VENDOR:
38       sprintf(name, "%04x", id1);
39       break;
40     case ID_DEVICE:
41       sprintf(name, "%04x.%04x", id2, id1);
42       break;
43     case ID_SUBSYSTEM:
44       sprintf(name, "%04x.%04x.%04x.%04x", id4, id3, id2, id1);
45       break;
46     case ID_GEN_SUBSYSTEM:
47       sprintf(name, "%04x.%04x.s", id2, id1);
48       break;
49     case ID_CLASS:
50       sprintf(name, "%02x.c", id1);
51       break;
52     case ID_SUBCLASS:
53       sprintf(name, "%02x.%02x.c", id2, id1);
54       break;
55     case ID_PROGIF:
56       sprintf(name, "%02x.%02x.%02x.c", id3, id2, id1);
57       break;
58     default:
59       return NULL;
60     }
61   sprintf(dnsname, "%s.%s", name, domain);
62
63   a->debug("Resolving %s\n", dnsname);
64   res_init();
65   res = res_query(dnsname, ns_c_in, ns_t_txt, answer, sizeof(answer));
66   if (res < 0)
67     {
68       a->debug("\tfailed, h_errno=%d\n", _res.res_h_errno);
69       return NULL;
70     }
71   if (ns_initparse(answer, res, &m) < 0)
72     {
73       a->debug("\tinitparse failed\n");
74       return NULL;
75     }
76   for (i=0; ns_parserr(&m, ns_s_an, i, &rr) >= 0; i++)
77     {
78       a->debug("\tanswer %d (class %d, type %d)\n", i, ns_rr_class(rr), ns_rr_type(rr));
79       if (ns_rr_class(rr) != ns_c_in || ns_rr_type(rr) != ns_t_txt)
80         continue;
81       data = ns_rr_rdata(rr);
82       dlen = ns_rr_rdlen(rr);
83       j = 0;
84       while (j < dlen && j+1+data[j] <= dlen)
85         {
86           memcpy(txt, &data[j+1], data[j]);
87           txt[data[j]] = 0;
88           j += 1+data[j];
89           a->debug("\t\t%s\n", txt);
90           if (txt[0] == 'i' && txt[1] == '=')
91             return strdup(txt+2);
92         }
93     }
94
95   return NULL;
96 }
97
98 #else
99
100 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)
101 {
102   return NULL;
103 }
104
105 #endif