1 /*
2 * Copyright (c) 2008-2020, OARC, Inc.
3 * Copyright (c) 2007-2008, Internet Systems Consortium, Inc.
4 * Copyright (c) 2003-2007, The Measurement Factory, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. Neither the name of the copyright holder nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 #include "config.h"
38
39 #include "dns_ip_version_index.h"
40
41 /* This indexer is the same as ip_version_indexer but
42 applies only to DNS messages. */
43
44 static int largest = 0;
45
dns_ip_version_indexer(const dns_message * m)46 int dns_ip_version_indexer(const dns_message* m)
47 {
48 int i = (int)inXaddr_version(&m->tm->src_ip_addr);
49 if (i > largest)
50 largest = i;
51 return i;
52 }
53
54 static int next_iter = 0;
55
dns_ip_version_iterator(const char ** label)56 int dns_ip_version_iterator(const char** label)
57 {
58 static char label_buf[20];
59 if (NULL == label) {
60 next_iter = 0;
61 return largest + 1;
62 }
63 if (next_iter > largest)
64 return -1;
65 snprintf(label_buf, sizeof(label_buf), "IPv%d", next_iter);
66 *label = label_buf;
67 return next_iter++;
68 }
69
dns_ip_version_reset(void)70 void dns_ip_version_reset(void)
71 {
72 largest = 0;
73 }
74