1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
/*
-----------------------------------------------------------------------------
dns.c - Dynamic DNS update library
-----------------------------------------------------------------------------
$Id: dns.c,v 1.1 2009/11/20 16:53:37 jasminko Exp $
-----------------------------------------------------------------------------
Copyright (c) 2001-2007 gogo6 Inc. All rights reserved.
For license information refer to CLIENT-LICENSE.TXT
----------------------------------------------------------------------------------
*/
#include "platform.h"
#include "net.h"
#include "net_tcp6.h"
#include "dns.h"
#define ADD16(b,s) {\
register uint16_t *bb = (uint16_t *) b; \
*bb = htons(s); \
b += DNS16SZ; \
}
#define ADD32(b,s) {\
register uint32_t *bb = (uint32_t *) b; \
*bb = htonl(s); \
b += DNS32SZ; \
}
#define GET16(b,s) {\
s = ntohs(*((uint16_t *) b)); \
b += DNS16SZ; \
}
#define GET32(b,s) {\
s = ntohl(*((uint32_t *) b)); \
b += DNS32SZ; \
}
/*
* Generate a DNS id
*/
static uint16_t
DNSGenerateID(void)
{
static uint16_t id = 0;
struct timeval now;
pal_gettimeofday(&now);
if (!id)
id = (uint16_t) (0xffff & (now.tv_sec ^ now.tv_usec ^ pal_getpid()));
else
id += 1 + (uint16_t)(now.tv_usec % 263);
return id;
}
/*
* Encode a DNS name in a DNS binary format
*
* *Pos_pp is the location inside the DNS message buffer where the name
* must be written. At the end, it's updated to point to the location right
* after the written name.
*
* Offset is the compression offset to use. A value of zero indicates no
* compression.
*/
static tDNSRCode
DNSNameEncode(char **Pos_pp, char *Name, uint16_t Offset)
{
char *Pos_p;
char *Name_p;
char *Dot_p;
size_t Len;
if (NULL == Pos_pp ||
NULL == *Pos_pp ||
NULL == Name ||
strlen(Name) > DNS_NAME_SIZE)
return DNS_RCODE_FORMERR;
Pos_p = *Pos_pp;
Name_p = Name;
while ('\0' != *Name_p) {
/* Copy label */
Dot_p = strchr(Name_p, '.');
Len = (NULL != Dot_p) ? (size_t) (Dot_p - Name_p) : strlen(Name_p);
if (Len) {
/* Length of the label */
*Pos_p++ = (uint8_t) Len;
/* Label */
memcpy(Pos_p, Name_p, Len);
Pos_p += Len;
Name_p += Len;
}
if (NULL != Dot_p)
Name_p++; /* Skip dot */
}
if (Offset) {
/* Compression offset */
ADD16(Pos_p, 0xc000 | Offset);
} else
*Pos_p++ = 0;
*Pos_pp = Pos_p;
return DNS_RCODE_NOERROR;
}
/*
* Create a DNS message.
*
* If AAAA is non NULL:
* - create a AAAA update message for Name.Domain with value AAAA.
*
* If AAAA is NULL:
* - create an update message for the deletion of all RRsets that belongs
* to Name.Domain.
*
* *Len_p is set with the length of the created message.
*
*/
static tDNSRCode
DNSMessageCreate(char *Buffer, uint16_t DNSid, char *Domain, char *Name, uint32_t TTL, char *AAAA, size_t *Len_p)
{
char *p;
size_t Domain_len;
size_t Name_len;
struct in6_addr AAAA_addr;
struct in6_addr *AAAA_addr_p = NULL;
tDNSRCode ret;
if (NULL == Buffer ||
NULL == Name ||
NULL == Domain ||
NULL == Len_p)
return DNS_RCODE_FORMERR;
Name_len = strlen(Name);
Domain_len = strlen(Domain);
/* Validate Name.Domain length */
if (Name_len + 1 + Domain_len > DNS_NAME_SIZE ||
Name_len > DNS_LABEL_SIZE)
return DNS_RCODE_FORMERR;
/* Convert AAAA name */
if (NULL != AAAA) {
AAAA_addr_p = NetText2Addr6(AAAA, &AAAA_addr);
if (NULL == AAAA_addr_p)
return DNS_RCODE_FORMERR;
}
/* Skip TCPMSGLENGTH (16 bits) - message length no known yet */
p = Buffer + DNS16SZ;
/* DNS id (16 bits) */
ADD16(p, DNSid);
/*
* Fields QR (1 bit) = 0, Opcode (4 bits) = UPDATE,
* Reserved (7 bits) = 0, RCode (4 bits) = 0.
*/
ADD16(p, DNS_OPCODE_UPDATE << 11);
/* Zone count (16 bits) = 1 */
ADD16(p, 1);
/* Prerequisite count (16 bits) = 0 */
ADD16(p, 0);
/* Update count (16 bits) = 1 */
ADD16(p, 1);
/* Additionnal data count (16 bits) = 0 */
ADD16(p, 0);
/*
* ZONE section
*/
ret = DNSNameEncode(&p, Domain, 0); /* ZNAME */
if (DNS_RCODE_NOERROR != ret)
return ret;
ADD16(p, DNS_TYPE_SOA); /* ZTYPE */
ADD16(p, DNS_CLASS_IN); /* CLASS */
/*
* Ressource Record Section
*
* For the RRNAME, DNS compression is used.
* DNS_HEADER_SIZE is the offset of the Zone Name.
*/
ret = DNSNameEncode(&p, Name, DNS_HEADER_SIZE); /* RRNAME */
if (DNS_RCODE_NOERROR != ret)
return ret;
if ( NULL != AAAA_addr_p) {
/* Addition of AAAA record */
ADD16(p, DNS_TYPE_AAAA); /* RRTYPE */
ADD16(p, DNS_CLASS_IN); /* RRCLASS */
ADD32(p, TTL); /* RRTTL */
ADD16(p, DNS_TYPE_AAAA_SIZE); /* RRDLENGTH */
memcpy(p, AAAA_addr_p, DNS_TYPE_AAAA_SIZE); /* RRDATA */
p += DNS_TYPE_AAAA_SIZE;
} else {
/* Deletion of all RRSETS */
ADD16(p, DNS_TYPE_ANY); /* RRTYPE */
ADD16(p, DNS_CLASS_ANY); /* RRCLASS */
ADD32(p, 0); /* RRTTL */
ADD16(p, 0); /* RRDLENGTH */
}
/* Update *Len_p and the TCPMSGLENGTH field */
*Len_p = p - Buffer;
p = Buffer;
ADD16(p, (u_short)(*Len_p) - DNS16SZ);
return DNS_RCODE_NOERROR;
}
/*
* Perform a DNS update for Name.Domain.
*
* A non NULL value for AAAA indicates the addition of a AAAA record
* with value AAAA.
*
* A NULL value indicated the deletion of all Ressource Records associated
* with Name.Domain.
*/
static tDNSRCode
DNSUpdate(pal_socket_t Socket, char *Name, char *Domain, char *AAAA)
{
uint16_t DNSid;
uint16_t DNSidReply;
uint16_t Header;
uint16_t QR;
uint16_t OPCode;
uint16_t RCode;
tDNSRCode ret;
char Buffer[DNS_BUFFER_SIZE];
size_t Len;
char *p;
/* Create DNS id */
DNSid = DNSGenerateID();
/* Create DNS Update message */
if (DNS_RCODE_NOERROR != (ret = DNSMessageCreate(Buffer, DNSid,
Domain, Name,
DNS_DEFAULT_TTL,
AAAA, &Len)))
return ret;
/* Send request to server */
if (NetTCP6Write(Socket, Buffer, (sint32_t)Len) != (sint32_t)Len)
return DNS_RCODE_SERVFAIL;
/* Read response */
Len = NetTCP6Read(Socket, Buffer, sizeof(Buffer));
/*
* We expect the response to be sent in one packet.
* To be forward compatible, only the following is validated:
* - response must be at least the size of the TCPMSGLENGTH field
* plus the DNS header.
* - DNSid must match.
* - QR field must be set to 1.
* - Opcode must be UPDATE (5).
*/
if (Len < DNS16SZ + DNS_HEADER_SIZE)
return DNS_RCODE_SERVFAIL;
/*
* Skip TCPMSGLENGTH, in the future we might have answer larger than
* the buffer size.
*/
p = Buffer + DNS16SZ;
/* Extract DNSid */
GET16(p, DNSidReply);
/* Extract QR, Opcode, and Return Code */
GET16(p, Header);
QR = (Header & 0x8000) >> 15;
OPCode = (Header & 0x7800) >> 11;
RCode = Header & 0x000f;
/* Validation */
if (DNSid != DNSidReply ||
QR != 1 ||
OPCode != DNS_OPCODE_UPDATE)
return DNS_RCODE_SERVFAIL;
/* Return Return Code */
return (tDNSRCode) RCode;
}
/*
* Start a DNS update session.
*/
pal_socket_t DNSUpdateConnect(char *Server)
{
pal_socket_t sock;
if( NetTCP6Connect( &sock, Server, DNS_UPDATE_PORT) == 0 )
return sock;
return -1;
}
/*
* Close DNS update session.
*/
sint32_t
DNSUpdateClose(pal_socket_t Socket)
{
return NetTCP6Close(Socket);
}
/*
* Perform a DNS update to add a AAAA record for Name.Domain with value AAAA.
*/
tDNSRCode
DNSUpdateAddAAAA(pal_socket_t Socket, char *Name, char *Domain, char *AAAA)
{
return DNSUpdate(Socket, Name, Domain, AAAA);
}
/*
* Perform a DNS update to delete all Ressource Records that belongs
* to Name.Domain.
*/
tDNSRCode
DNSUpdateDelRRSets(pal_socket_t Socket, char *Name, char *Domain)
{
return DNSUpdate(Socket, Name, Domain, NULL);
}
|