rfc3596.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9#include "squid.h"
10#include "dns/rfc2671.h"
11#include "dns/rfc3596.h"
12#include "util.h"
13
14#if HAVE_UNISTD_H
15#include <unistd.h>
16#endif
17#if HAVE_MEMORY_H
18#include <memory.h>
19#endif
20#include <cassert>
21#if HAVE_NETINET_IN_H
22#include <netinet/in.h>
23#endif
24#if HAVE_STRINGS_H
25#include <strings.h>
26#endif
27
28#ifndef SQUID_RFC1035_H
29#error RFC3596 Library depends on RFC1035
30#endif
31
32/*
33 * Low level DNS protocol routines
34 *
35 * Provides RFC3596 functions to handle purely IPv6 DNS.
36 * Adds AAAA and IPv6 PTR records.
37 * Other IPv6 records are not mentioned by this RFC.
38 *
39 * IPv4 equivalents are taken care of by the RFC1035 library.
40 * Where one protocol lookup must be followed by another, the caller
41 * is responsible for the order and handling of the lookups.
42 *
43 * KNOWN BUGS:
44 *
45 * UDP replies with TC set should be retried via TCP
46 */
47
56ssize_t
57rfc3596BuildHostQuery(const char *hostname, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, int qtype, ssize_t edns_sz)
58{
59 static rfc1035_message h;
60 size_t offset = 0;
61 memset(&h, '\0', sizeof(h));
62 h.id = qid;
63 h.qr = 0;
64 h.rd = 1;
65 h.opcode = 0; /* QUERY */
66 h.qdcount = (unsigned int) 1;
67 h.arcount = (edns_sz > 0 ? 1 : 0);
68 offset += rfc1035HeaderPack(buf + offset, sz - offset, &h);
69 offset += rfc1035QuestionPack(buf + offset,
70 sz - offset,
71 hostname,
72 qtype,
74 if (edns_sz > 0)
75 offset += rfc2671RROptPack(buf + offset, sz - offset, edns_sz);
76
77 if (query) {
78 query->qtype = qtype;
79 query->qclass = RFC1035_CLASS_IN;
80 xstrncpy(query->name, hostname, sizeof(query->name));
81 }
82
83 assert(offset <= sz);
84 return offset;
85}
86
95ssize_t
96rfc3596BuildAQuery(const char *hostname, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, ssize_t edns_sz)
97{
98 return rfc3596BuildHostQuery(hostname, buf, sz, qid, query, RFC1035_TYPE_A, edns_sz);
99}
100
109ssize_t
110rfc3596BuildAAAAQuery(const char *hostname, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, ssize_t edns_sz)
111{
112 return rfc3596BuildHostQuery(hostname, buf, sz, qid, query, RFC1035_TYPE_AAAA, edns_sz);
113}
114
123ssize_t
124rfc3596BuildPTRQuery4(const struct in_addr addr, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, ssize_t edns_sz)
125{
126 static char rev[RFC1035_MAXHOSTNAMESZ];
127 unsigned int i;
128
129 i = (unsigned int) ntohl(addr.s_addr);
130 snprintf(rev, RFC1035_MAXHOSTNAMESZ, "%u.%u.%u.%u.in-addr.arpa.",
131 i & 255,
132 (i >> 8) & 255,
133 (i >> 16) & 255,
134 (i >> 24) & 255);
135
136 return rfc3596BuildHostQuery(rev, buf, sz, qid, query, RFC1035_TYPE_PTR, edns_sz);
137}
138
139ssize_t
140rfc3596BuildPTRQuery6(const struct in6_addr addr, char *buf, size_t sz, unsigned short qid, rfc1035_query * query, ssize_t edns_sz)
141{
142 static char rev[RFC1035_MAXHOSTNAMESZ];
143 const uint8_t* r = addr.s6_addr;
144 char* p = rev;
145 int i; /* NP: MUST allow signed for loop termination. */
146
147 /* work from the raw addr field. anything else may have representation changes. */
148 /* The sin6_port and sin6_addr members shall be in network byte order. */
149 for (i = 15; i >= 0; i--, p+=4) {
150 snprintf(p, 5, "%1x.%1x.", ((r[i])&0xf), (r[i]>>4)&0xf );
151 }
152
153 snprintf(p,10,"ip6.arpa.");
154
155 return rfc3596BuildHostQuery(rev, buf, sz, qid, query, RFC1035_TYPE_PTR, edns_sz);
156}
157
#define assert(EX)
Definition: assert.h:17
int rfc1035QuestionPack(char *buf, const size_t sz, const char *name, const unsigned short type, const unsigned short _class)
Definition: rfc1035.cc:149
int rfc1035HeaderPack(char *buf, size_t sz, rfc1035_message *hdr)
Definition: rfc1035.cc:56
#define RFC1035_TYPE_PTR
Definition: rfc1035.h:95
#define RFC1035_TYPE_A
Definition: rfc1035.h:93
#define RFC1035_CLASS_IN
Definition: rfc1035.h:96
#define RFC1035_MAXHOSTNAMESZ
Definition: rfc1035.h:32
int rfc2671RROptPack(char *buf, size_t sz, ssize_t edns_sz)
Definition: rfc2671.cc:14
ssize_t rfc3596BuildAQuery(const char *hostname, char *buf, size_t sz, unsigned short qid, rfc1035_query *query, ssize_t edns_sz)
Definition: rfc3596.cc:96
ssize_t rfc3596BuildHostQuery(const char *hostname, char *buf, size_t sz, unsigned short qid, rfc1035_query *query, int qtype, ssize_t edns_sz)
Definition: rfc3596.cc:57
ssize_t rfc3596BuildPTRQuery4(const struct in_addr addr, char *buf, size_t sz, unsigned short qid, rfc1035_query *query, ssize_t edns_sz)
Definition: rfc3596.cc:124
ssize_t rfc3596BuildPTRQuery6(const struct in6_addr addr, char *buf, size_t sz, unsigned short qid, rfc1035_query *query, ssize_t edns_sz)
Definition: rfc3596.cc:140
ssize_t rfc3596BuildAAAAQuery(const char *hostname, char *buf, size_t sz, unsigned short qid, rfc1035_query *query, ssize_t edns_sz)
Definition: rfc3596.cc:110
#define RFC1035_TYPE_AAAA
Definition: rfc3596.h:53
unsigned int rd
Definition: rfc1035.h:61
unsigned short qdcount
Definition: rfc1035.h:64
unsigned int opcode
Definition: rfc1035.h:58
unsigned short id
Definition: rfc1035.h:56
unsigned int qr
Definition: rfc1035.h:57
unsigned short arcount
Definition: rfc1035.h:67
unsigned short qclass
Definition: rfc1035.h:51
char name[RFC1035_MAXHOSTNAMESZ]
Definition: rfc1035.h:49
unsigned short qtype
Definition: rfc1035.h:50
int unsigned int
Definition: stub_fd.cc:19
char * xstrncpy(char *dst, const char *src, size_t n)
Definition: xstring.cc:37

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors