| 1 | package dns |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "net" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | type ( |
| 13 | // Type is a DNS type. |
| 14 | Type uint16 |
| 15 | // Class is a DNS class. |
| 16 | Class uint16 |
| 17 | // Name is a DNS domain name. |
| 18 | Name string |
| 19 | ) |
| 20 | |
| 21 | // Packet formats |
| 22 | |
| 23 | // Wire constants and supported types. |
| 24 | const ( |
| 25 | // valid RR_Header.Rrtype and Question.qtype |
| 26 | |
| 27 | TypeNone uint16 = 0 |
| 28 | TypeA uint16 = 1 |
| 29 | TypeNS uint16 = 2 |
| 30 | TypeMD uint16 = 3 |
| 31 | TypeMF uint16 = 4 |
| 32 | TypeCNAME uint16 = 5 |
| 33 | TypeSOA uint16 = 6 |
| 34 | TypeMB uint16 = 7 |
| 35 | TypeMG uint16 = 8 |
| 36 | TypeMR uint16 = 9 |
| 37 | TypeNULL uint16 = 10 |
| 38 | TypePTR uint16 = 12 |
| 39 | TypeHINFO uint16 = 13 |
| 40 | TypeMINFO uint16 = 14 |
| 41 | TypeMX uint16 = 15 |
| 42 | TypeTXT uint16 = 16 |
| 43 | TypeRP uint16 = 17 |
| 44 | TypeAFSDB uint16 = 18 |
| 45 | TypeX25 uint16 = 19 |
| 46 | TypeISDN uint16 = 20 |
| 47 | TypeRT uint16 = 21 |
| 48 | TypeNSAPPTR uint16 = 23 |
| 49 | TypeSIG uint16 = 24 |
| 50 | TypeKEY uint16 = 25 |
| 51 | TypePX uint16 = 26 |
| 52 | TypeGPOS uint16 = 27 |
| 53 | TypeAAAA uint16 = 28 |
| 54 | TypeLOC uint16 = 29 |
| 55 | TypeNXT uint16 = 30 |
| 56 | TypeEID uint16 = 31 |
| 57 | TypeNIMLOC uint16 = 32 |
| 58 | TypeSRV uint16 = 33 |
| 59 | TypeATMA uint16 = 34 |
| 60 | TypeNAPTR uint16 = 35 |
| 61 | TypeKX uint16 = 36 |
| 62 | TypeCERT uint16 = 37 |
| 63 | TypeDNAME uint16 = 39 |
| 64 | TypeOPT uint16 = 41 // EDNS |
| 65 | TypeAPL uint16 = 42 |
| 66 | TypeDS uint16 = 43 |
| 67 | TypeSSHFP uint16 = 44 |
| 68 | TypeIPSECKEY uint16 = 45 |
| 69 | TypeRRSIG uint16 = 46 |
| 70 | TypeNSEC uint16 = 47 |
| 71 | TypeDNSKEY uint16 = 48 |
| 72 | TypeDHCID uint16 = 49 |
| 73 | TypeNSEC3 uint16 = 50 |
| 74 | TypeNSEC3PARAM uint16 = 51 |
| 75 | TypeTLSA uint16 = 52 |
| 76 | TypeSMIMEA uint16 = 53 |
| 77 | TypeHIP uint16 = 55 |
| 78 | TypeNINFO uint16 = 56 |
| 79 | TypeRKEY uint16 = 57 |
| 80 | TypeTALINK uint16 = 58 |
| 81 | TypeCDS uint16 = 59 |
| 82 | TypeCDNSKEY uint16 = 60 |
| 83 | TypeOPENPGPKEY uint16 = 61 |
| 84 | TypeCSYNC uint16 = 62 |
| 85 | TypeZONEMD uint16 = 63 |
| 86 | TypeSVCB uint16 = 64 |
| 87 | TypeHTTPS uint16 = 65 |
| 88 | TypeSPF uint16 = 99 |
| 89 | TypeUINFO uint16 = 100 |
| 90 | TypeUID uint16 = 101 |
| 91 | TypeGID uint16 = 102 |
| 92 | TypeUNSPEC uint16 = 103 |
| 93 | TypeNID uint16 = 104 |
| 94 | TypeL32 uint16 = 105 |
| 95 | TypeL64 uint16 = 106 |
| 96 | TypeLP uint16 = 107 |
| 97 | TypeEUI48 uint16 = 108 |
| 98 | TypeEUI64 uint16 = 109 |
| 99 | TypeNXNAME uint16 = 128 |
| 100 | TypeURI uint16 = 256 |
| 101 | TypeCAA uint16 = 257 |
| 102 | TypeAVC uint16 = 258 |
| 103 | TypeAMTRELAY uint16 = 260 |
| 104 | TypeRESINFO uint16 = 261 |
| 105 | |
| 106 | TypeTKEY uint16 = 249 |
| 107 | TypeTSIG uint16 = 250 |
| 108 | |
| 109 | // valid Question.Qtype only |
| 110 | TypeIXFR uint16 = 251 |
| 111 | TypeAXFR uint16 = 252 |
| 112 | TypeMAILB uint16 = 253 |
| 113 | TypeMAILA uint16 = 254 |
| 114 | TypeANY uint16 = 255 |
| 115 | |
| 116 | TypeTA uint16 = 32768 |
| 117 | TypeDLV uint16 = 32769 |
| 118 | TypeReserved uint16 = 65535 |
| 119 | |
| 120 | // valid Question.Qclass |
| 121 | ClassINET = 1 |
| 122 | ClassCSNET = 2 |
| 123 | ClassCHAOS = 3 |
| 124 | ClassHESIOD = 4 |
| 125 | ClassNONE = 254 |
| 126 | ClassANY = 255 |
| 127 | |
| 128 | // Message Response Codes, see https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml |
| 129 | RcodeSuccess = 0 // NoError - No Error [DNS] |
| 130 | RcodeFormatError = 1 // FormErr - Format Error [DNS] |
| 131 | RcodeServerFailure = 2 // ServFail - Server Failure [DNS] |
| 132 | RcodeNameError = 3 // NXDomain - Non-Existent Domain [DNS] |
| 133 | RcodeNotImplemented = 4 // NotImp - Not Implemented [DNS] |
| 134 | RcodeRefused = 5 // Refused - Query Refused [DNS] |
| 135 | RcodeYXDomain = 6 // YXDomain - Name Exists when it should not [DNS Update] |
| 136 | RcodeYXRrset = 7 // YXRRSet - RR Set Exists when it should not [DNS Update] |
| 137 | RcodeNXRrset = 8 // NXRRSet - RR Set that should exist does not [DNS Update] |
| 138 | RcodeNotAuth = 9 // NotAuth - Server Not Authoritative for zone [DNS Update] |
| 139 | RcodeNotZone = 10 // NotZone - Name not contained in zone [DNS Update/TSIG] |
| 140 | RcodeStatefulTypeNotImplemented = 11 // DSOTypeNI - DSO-TYPE not implemented [DNS Stateful Operations] https://www.rfc-editor.org/rfc/rfc8490.html#section-10.2 |
| 141 | RcodeBadSig = 16 // BADSIG - TSIG Signature Failure [TSIG] https://www.rfc-editor.org/rfc/rfc6895.html#section-2.3 |
| 142 | RcodeBadVers = 16 // BADVERS - Bad OPT Version [EDNS0] https://www.rfc-editor.org/rfc/rfc6895.html#section-2.3 |
| 143 | RcodeBadKey = 17 // BADKEY - Key not recognized [TSIG] |
| 144 | RcodeBadTime = 18 // BADTIME - Signature out of time window [TSIG] |
| 145 | RcodeBadMode = 19 // BADMODE - Bad TKEY Mode [TKEY] |
| 146 | RcodeBadName = 20 // BADNAME - Duplicate key name [TKEY] |
| 147 | RcodeBadAlg = 21 // BADALG - Algorithm not supported [TKEY] |
| 148 | RcodeBadTrunc = 22 // BADTRUNC - Bad Truncation [TSIG] |
| 149 | RcodeBadCookie = 23 // BADCOOKIE - Bad/missing Server Cookie [DNS Cookies] |
| 150 | |
| 151 | // Message Opcodes. There is no 3. |
| 152 | OpcodeQuery = 0 |
| 153 | OpcodeIQuery = 1 |
| 154 | OpcodeStatus = 2 |
| 155 | OpcodeNotify = 4 |
| 156 | OpcodeUpdate = 5 |
| 157 | OpcodeStateful = 6 |
| 158 | ) |
| 159 | |
| 160 | // Used in ZONEMD https://tools.ietf.org/html/rfc8976 |
| 161 | const ( |
| 162 | ZoneMDSchemeSimple = 1 |
| 163 | |
| 164 | ZoneMDHashAlgSHA384 = 1 |
| 165 | ZoneMDHashAlgSHA512 = 2 |
| 166 | ) |
| 167 | |
| 168 | // Used in IPSEC https://datatracker.ietf.org/doc/html/rfc4025#section-2.3 |
| 169 | const ( |
| 170 | IPSECGatewayNone uint8 = iota |
| 171 | IPSECGatewayIPv4 |
| 172 | IPSECGatewayIPv6 |
| 173 | IPSECGatewayHost |
| 174 | ) |
| 175 | |
| 176 | // Used in AMTRELAY https://datatracker.ietf.org/doc/html/rfc8777#section-4.2.3 |
| 177 | const ( |
| 178 | AMTRELAYNone = IPSECGatewayNone |
| 179 | AMTRELAYIPv4 = IPSECGatewayIPv4 |
| 180 | AMTRELAYIPv6 = IPSECGatewayIPv6 |
| 181 | AMTRELAYHost = IPSECGatewayHost |
| 182 | ) |
| 183 | |
| 184 | // Stateful types as defined in RFC 8490. |
| 185 | const ( |
| 186 | StatefulTypeKeepAlive uint16 = iota + 1 |
| 187 | StatefulTypeRetryDelay |
| 188 | StatefulTypeEncryptionPadding |
| 189 | ) |
| 190 | |
| 191 | var StatefulTypeToString = map[uint16]string{ |
| 192 | StatefulTypeKeepAlive: "KeepAlive", |
| 193 | StatefulTypeRetryDelay: "RetryDelay", |
| 194 | StatefulTypeEncryptionPadding: "EncryptionPadding", |
| 195 | } |
| 196 | |
| 197 | // Header is the wire format for the DNS packet header. |
| 198 | type Header struct { |
| 199 | Id uint16 |
| 200 | Bits uint16 |
| 201 | Qdcount, Ancount, Nscount, Arcount uint16 |
| 202 | } |
| 203 | |
| 204 | const ( |
| 205 | headerSize = 12 |
| 206 | |
| 207 | // Header.Bits |
| 208 | _QR = 1 << 15 // query/response (response=1) |
| 209 | _AA = 1 << 10 // authoritative |
| 210 | _TC = 1 << 9 // truncated |
| 211 | _RD = 1 << 8 // recursion desired |
| 212 | _RA = 1 << 7 // recursion available |
| 213 | _Z = 1 << 6 // Z |
| 214 | _AD = 1 << 5 // authenticated data |
| 215 | _CD = 1 << 4 // checking disabled |
| 216 | ) |
| 217 | |
| 218 | // Various constants used in the LOC RR. See RFC 1876. |
| 219 | const ( |
| 220 | LOC_EQUATOR = 1 << 31 // RFC 1876, Section 2. |
| 221 | LOC_PRIMEMERIDIAN = 1 << 31 // RFC 1876, Section 2. |
| 222 | LOC_HOURS = 60 * 1000 |
| 223 | LOC_DEGREES = 60 * LOC_HOURS |
| 224 | LOC_ALTITUDEBASE = 100000 |
| 225 | ) |
| 226 | |
| 227 | // Different Certificate Types, see RFC 4398, Section 2.1 |
| 228 | const ( |
| 229 | CertPKIX = 1 + iota |
| 230 | CertSPKI |
| 231 | CertPGP |
| 232 | CertIPIX |
| 233 | CertISPKI |
| 234 | CertIPGP |
| 235 | CertACPKIX |
| 236 | CertIACPKIX |
| 237 | CertURI = 253 |
| 238 | CertOID = 254 |
| 239 | ) |
| 240 | |
| 241 | // CertTypeToString converts the Cert Type to its string representation. |
| 242 | // See RFC 4398 and RFC 6944. |
| 243 | var CertTypeToString = map[uint16]string{ |
| 244 | CertPKIX: "PKIX", |
| 245 | CertSPKI: "SPKI", |
| 246 | CertPGP: "PGP", |
| 247 | CertIPIX: "IPIX", |
| 248 | CertISPKI: "ISPKI", |
| 249 | CertIPGP: "IPGP", |
| 250 | CertACPKIX: "ACPKIX", |
| 251 | CertIACPKIX: "IACPKIX", |
| 252 | CertURI: "URI", |
| 253 | CertOID: "OID", |
| 254 | } |
| 255 | |
| 256 | // Prefix for IPv4 encoded as IPv6 address |
| 257 | const ipv4InIPv6Prefix = "::ffff:" |
| 258 | |
| 259 | //go:generate go run types_generate.go |
| 260 | |
| 261 | // Question holds a DNS question. Usually there is just one. While the |
| 262 | // original DNS RFCs allow multiple questions in the question section of a |
| 263 | // message, in practice it never works. Because most DNS servers see multiple |
| 264 | // questions as an error, it is recommended to only have one question per |
| 265 | // message. |
| 266 | type Question struct { |
| 267 | Name string `dns:"cdomain-name"` // "cdomain-name" specifies encoding (and may be compressed) |
| 268 | Qtype uint16 |
| 269 | Qclass uint16 |
| 270 | } |
| 271 | |
| 272 | func (q *Question) len(off int, compression map[string]struct{}) int { |
| 273 | l := domainNameLen(q.Name, off, compression, true) |
| 274 | l += 2 + 2 |
| 275 | return l |
| 276 | } |
| 277 | |
| 278 | func (q *Question) String() (s string) { |
| 279 | // prefix with ; (as in dig) |
| 280 | s = ";" + sprintName(q.Name) + "\t" |
| 281 | s += Class(q.Qclass).String() + "\t" |
| 282 | s += " " + Type(q.Qtype).String() |
| 283 | return s |
| 284 | } |
| 285 | |
| 286 | // ANY is a wild card record. See RFC 1035, Section 3.2.3. ANY is named "*" there. |
| 287 | // The ANY records can be (ab)used to create resource records without any rdata, that |
| 288 | // can be used in dynamic update requests. Basic use pattern: |
| 289 | // |
| 290 | // a := &ANY{RR_Header{ |
| 291 | // Name: "example.org.", |
| 292 | // Rrtype: TypeA, |
| 293 | // Class: ClassINET, |
| 294 | // }} |
| 295 | // |
| 296 | // Results in an A record without rdata. |
| 297 | type ANY struct { |
| 298 | Hdr RR_Header |
| 299 | // Does not have any rdata. |
| 300 | } |
| 301 | |
| 302 | func (rr *ANY) String() string { return rr.Hdr.String() } |
| 303 | |
| 304 | func (*ANY) parse(c *zlexer, origin string) *ParseError { |
| 305 | return &ParseError{err: "ANY records do not have a presentation format"} |
| 306 | } |
| 307 | |
| 308 | // NULL RR. See RFC 1035. |
| 309 | type NULL struct { |
| 310 | Hdr RR_Header |
| 311 | Data string `dns:"any"` |
| 312 | } |
| 313 | |
| 314 | func (rr *NULL) String() string { |
| 315 | // There is no presentation format; prefix string with a comment. |
| 316 | return ";" + rr.Hdr.String() + rr.Data |
| 317 | } |
| 318 | |
| 319 | func (*NULL) parse(c *zlexer, origin string) *ParseError { |
| 320 | return &ParseError{err: "NULL records do not have a presentation format"} |
| 321 | } |
| 322 | |
| 323 | // NXNAME is a meta record. See https://www.iana.org/go/draft-ietf-dnsop-compact-denial-of-existence-04 |
| 324 | // Reference: https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml |
| 325 | type NXNAME struct { |
| 326 | Hdr RR_Header |
| 327 | // Does not have any rdata |
| 328 | } |
| 329 | |
| 330 | func (rr *NXNAME) String() string { return rr.Hdr.String() } |
| 331 | |
| 332 | func (*NXNAME) parse(c *zlexer, origin string) *ParseError { |
| 333 | return &ParseError{err: "NXNAME records do not have a presentation format"} |
| 334 | } |
| 335 | |
| 336 | // CNAME RR. See RFC 1034. |
| 337 | type CNAME struct { |
| 338 | Hdr RR_Header |
| 339 | Target string `dns:"cdomain-name"` |
| 340 | } |
| 341 | |
| 342 | func (rr *CNAME) String() string { return rr.Hdr.String() + sprintName(rr.Target) } |
| 343 | |
| 344 | // HINFO RR. See RFC 1034. |
| 345 | type HINFO struct { |
| 346 | Hdr RR_Header |
| 347 | Cpu string |
| 348 | Os string |
| 349 | } |
| 350 | |
| 351 | func (rr *HINFO) String() string { |
| 352 | return rr.Hdr.String() + sprintTxt([]string{rr.Cpu, rr.Os}) |
| 353 | } |
| 354 | |
| 355 | // MB RR. See RFC 1035. |
| 356 | type MB struct { |
| 357 | Hdr RR_Header |
| 358 | Mb string `dns:"cdomain-name"` |
| 359 | } |
| 360 | |
| 361 | func (rr *MB) String() string { return rr.Hdr.String() + sprintName(rr.Mb) } |
| 362 | |
| 363 | // MG RR. See RFC 1035. |
| 364 | type MG struct { |
| 365 | Hdr RR_Header |
| 366 | Mg string `dns:"cdomain-name"` |
| 367 | } |
| 368 | |
| 369 | func (rr *MG) String() string { return rr.Hdr.String() + sprintName(rr.Mg) } |
| 370 | |
| 371 | // MINFO RR. See RFC 1035. |
| 372 | type MINFO struct { |
| 373 | Hdr RR_Header |
| 374 | Rmail string `dns:"cdomain-name"` |
| 375 | Email string `dns:"cdomain-name"` |
| 376 | } |
| 377 | |
| 378 | func (rr *MINFO) String() string { |
| 379 | return rr.Hdr.String() + sprintName(rr.Rmail) + " " + sprintName(rr.Email) |
| 380 | } |
| 381 | |
| 382 | // MR RR. See RFC 1035. |
| 383 | type MR struct { |
| 384 | Hdr RR_Header |
| 385 | Mr string `dns:"cdomain-name"` |
| 386 | } |
| 387 | |
| 388 | func (rr *MR) String() string { |
| 389 | return rr.Hdr.String() + sprintName(rr.Mr) |
| 390 | } |
| 391 | |
| 392 | // MF RR. See RFC 1035. |
| 393 | type MF struct { |
| 394 | Hdr RR_Header |
| 395 | Mf string `dns:"cdomain-name"` |
| 396 | } |
| 397 | |
| 398 | func (rr *MF) String() string { |
| 399 | return rr.Hdr.String() + sprintName(rr.Mf) |
| 400 | } |
| 401 | |
| 402 | // MD RR. See RFC 1035. |
| 403 | type MD struct { |
| 404 | Hdr RR_Header |
| 405 | Md string `dns:"cdomain-name"` |
| 406 | } |
| 407 | |
| 408 | func (rr *MD) String() string { |
| 409 | return rr.Hdr.String() + sprintName(rr.Md) |
| 410 | } |
| 411 | |
| 412 | // MX RR. See RFC 1035. |
| 413 | type MX struct { |
| 414 | Hdr RR_Header |
| 415 | Preference uint16 |
| 416 | Mx string `dns:"cdomain-name"` |
| 417 | } |
| 418 | |
| 419 | func (rr *MX) String() string { |
| 420 | return rr.Hdr.String() + strconv.Itoa(int(rr.Preference)) + " " + sprintName(rr.Mx) |
| 421 | } |
| 422 | |
| 423 | // AFSDB RR. See RFC 1183. |
| 424 | type AFSDB struct { |
| 425 | Hdr RR_Header |
| 426 | Subtype uint16 |
| 427 | Hostname string `dns:"domain-name"` |
| 428 | } |
| 429 | |
| 430 | func (rr *AFSDB) String() string { |
| 431 | return rr.Hdr.String() + strconv.Itoa(int(rr.Subtype)) + " " + sprintName(rr.Hostname) |
| 432 | } |
| 433 | |
| 434 | // X25 RR. See RFC 1183, Section 3.1. |
| 435 | type X25 struct { |
| 436 | Hdr RR_Header |
| 437 | PSDNAddress string |
| 438 | } |
| 439 | |
| 440 | func (rr *X25) String() string { |
| 441 | return rr.Hdr.String() + rr.PSDNAddress |
| 442 | } |
| 443 | |
| 444 | // ISDN RR. See RFC 1183, Section 3.2. |
| 445 | type ISDN struct { |
| 446 | Hdr RR_Header |
| 447 | Address string |
| 448 | SubAddress string |
| 449 | } |
| 450 | |
| 451 | func (rr *ISDN) String() string { |
| 452 | return rr.Hdr.String() + sprintTxt([]string{rr.Address, rr.SubAddress}) |
| 453 | } |
| 454 | |
| 455 | // RT RR. See RFC 1183, Section 3.3. |
| 456 | type RT struct { |
| 457 | Hdr RR_Header |
| 458 | Preference uint16 |
| 459 | Host string `dns:"domain-name"` // RFC 3597 prohibits compressing records not defined in RFC 1035. |
| 460 | } |
| 461 | |
| 462 | func (rr *RT) String() string { |
| 463 | return rr.Hdr.String() + strconv.Itoa(int(rr.Preference)) + " " + sprintName(rr.Host) |
| 464 | } |
| 465 | |
| 466 | // NS RR. See RFC 1035. |
| 467 | type NS struct { |
| 468 | Hdr RR_Header |
| 469 | Ns string `dns:"cdomain-name"` |
| 470 | } |
| 471 | |
| 472 | func (rr *NS) String() string { |
| 473 | return rr.Hdr.String() + sprintName(rr.Ns) |
| 474 | } |
| 475 | |
| 476 | // PTR RR. See RFC 1035. |
| 477 | type PTR struct { |
| 478 | Hdr RR_Header |
| 479 | Ptr string `dns:"cdomain-name"` |
| 480 | } |
| 481 | |
| 482 | func (rr *PTR) String() string { |
| 483 | return rr.Hdr.String() + sprintName(rr.Ptr) |
| 484 | } |
| 485 | |
| 486 | // RP RR. See RFC 1138, Section 2.2. |
| 487 | type RP struct { |
| 488 | Hdr RR_Header |
| 489 | Mbox string `dns:"domain-name"` |
| 490 | Txt string `dns:"domain-name"` |
| 491 | } |
| 492 | |
| 493 | func (rr *RP) String() string { |
| 494 | return rr.Hdr.String() + sprintName(rr.Mbox) + " " + sprintName(rr.Txt) |
| 495 | } |
| 496 | |
| 497 | // SOA RR. See RFC 1035. |
| 498 | type SOA struct { |
| 499 | Hdr RR_Header |
| 500 | Ns string `dns:"cdomain-name"` |
| 501 | Mbox string `dns:"cdomain-name"` |
| 502 | Serial uint32 |
| 503 | Refresh uint32 |
| 504 | Retry uint32 |
| 505 | Expire uint32 |
| 506 | Minttl uint32 |
| 507 | } |
| 508 | |
| 509 | func (rr *SOA) String() string { |
| 510 | return rr.Hdr.String() + sprintName(rr.Ns) + " " + sprintName(rr.Mbox) + |
| 511 | " " + strconv.FormatInt(int64(rr.Serial), 10) + |
| 512 | " " + strconv.FormatInt(int64(rr.Refresh), 10) + |
| 513 | " " + strconv.FormatInt(int64(rr.Retry), 10) + |
| 514 | " " + strconv.FormatInt(int64(rr.Expire), 10) + |
| 515 | " " + strconv.FormatInt(int64(rr.Minttl), 10) |
| 516 | } |
| 517 | |
| 518 | // TXT RR. See RFC 1035. |
| 519 | type TXT struct { |
| 520 | Hdr RR_Header |
| 521 | Txt []string `dns:"txt"` |
| 522 | } |
| 523 | |
| 524 | func (rr *TXT) String() string { return rr.Hdr.String() + sprintTxt(rr.Txt) } |
| 525 | |
| 526 | func sprintName(s string) string { |
| 527 | var dst strings.Builder |
| 528 | |
| 529 | for i := 0; i < len(s); { |
| 530 | if s[i] == '.' { |
| 531 | if dst.Len() != 0 { |
| 532 | dst.WriteByte('.') |
| 533 | } |
| 534 | i++ |
| 535 | continue |
| 536 | } |
| 537 | |
| 538 | b, n := nextByte(s, i) |
| 539 | if n == 0 { |
| 540 | // Drop "dangling" incomplete escapes. |
| 541 | if dst.Len() == 0 { |
| 542 | return s[:i] |
| 543 | } |
| 544 | break |
| 545 | } |
| 546 | if isDomainNameLabelSpecial(b) { |
| 547 | if dst.Len() == 0 { |
| 548 | dst.Grow(len(s) * 2) |
| 549 | dst.WriteString(s[:i]) |
| 550 | } |
| 551 | dst.WriteByte('\\') |
| 552 | dst.WriteByte(b) |
| 553 | } else if b < ' ' || b > '~' { // unprintable, use \DDD |
| 554 | if dst.Len() == 0 { |
| 555 | dst.Grow(len(s) * 2) |
| 556 | dst.WriteString(s[:i]) |
| 557 | } |
| 558 | dst.WriteString(escapeByte(b)) |
| 559 | } else { |
| 560 | if dst.Len() != 0 { |
| 561 | dst.WriteByte(b) |
| 562 | } |
| 563 | } |
| 564 | i += n |
| 565 | } |
| 566 | if dst.Len() == 0 { |
| 567 | return s |
| 568 | } |
| 569 | return dst.String() |
| 570 | } |
| 571 | |
| 572 | func sprintTxtOctet(s string) string { |
| 573 | var dst strings.Builder |
| 574 | dst.Grow(2 + len(s)) |
| 575 | dst.WriteByte('"') |
| 576 | for i := 0; i < len(s); { |
| 577 | if i+1 < len(s) && s[i] == '\\' && s[i+1] == '.' { |
| 578 | dst.WriteString(s[i : i+2]) |
| 579 | i += 2 |
| 580 | continue |
| 581 | } |
| 582 | |
| 583 | b, n := nextByte(s, i) |
| 584 | if n == 0 { |
| 585 | i++ // dangling back slash |
| 586 | } else { |
| 587 | writeTXTStringByte(&dst, b) |
| 588 | } |
| 589 | i += n |
| 590 | } |
| 591 | dst.WriteByte('"') |
| 592 | return dst.String() |
| 593 | } |
| 594 | |
| 595 | func sprintTxt(txt []string) string { |
| 596 | var out strings.Builder |
| 597 | for i, s := range txt { |
| 598 | out.Grow(3 + len(s)) |
| 599 | if i > 0 { |
| 600 | out.WriteString(` "`) |
| 601 | } else { |
| 602 | out.WriteByte('"') |
| 603 | } |
| 604 | for j := 0; j < len(s); { |
| 605 | b, n := nextByte(s, j) |
| 606 | if n == 0 { |
| 607 | break |
| 608 | } |
| 609 | writeTXTStringByte(&out, b) |
| 610 | j += n |
| 611 | } |
| 612 | out.WriteByte('"') |
| 613 | } |
| 614 | return out.String() |
| 615 | } |
| 616 | |
| 617 | func writeTXTStringByte(s *strings.Builder, b byte) { |
| 618 | switch { |
| 619 | case b == '"' || b == '\\': |
| 620 | s.WriteByte('\\') |
| 621 | s.WriteByte(b) |
| 622 | case b < ' ' || b > '~': |
| 623 | s.WriteString(escapeByte(b)) |
| 624 | default: |
| 625 | s.WriteByte(b) |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | const ( |
| 630 | escapedByteSmall = "" + |
| 631 | `\000\001\002\003\004\005\006\007\008\009` + |
| 632 | `\010\011\012\013\014\015\016\017\018\019` + |
| 633 | `\020\021\022\023\024\025\026\027\028\029` + |
| 634 | `\030\031` |
| 635 | escapedByteLarge = `\127\128\129` + |
| 636 | `\130\131\132\133\134\135\136\137\138\139` + |
| 637 | `\140\141\142\143\144\145\146\147\148\149` + |
| 638 | `\150\151\152\153\154\155\156\157\158\159` + |
| 639 | `\160\161\162\163\164\165\166\167\168\169` + |
| 640 | `\170\171\172\173\174\175\176\177\178\179` + |
| 641 | `\180\181\182\183\184\185\186\187\188\189` + |
| 642 | `\190\191\192\193\194\195\196\197\198\199` + |
| 643 | `\200\201\202\203\204\205\206\207\208\209` + |
| 644 | `\210\211\212\213\214\215\216\217\218\219` + |
| 645 | `\220\221\222\223\224\225\226\227\228\229` + |
| 646 | `\230\231\232\233\234\235\236\237\238\239` + |
| 647 | `\240\241\242\243\244\245\246\247\248\249` + |
| 648 | `\250\251\252\253\254\255` |
| 649 | ) |
| 650 | |
| 651 | // escapeByte returns the \DDD escaping of b which must |
| 652 | // satisfy b < ' ' || b > '~'. |
| 653 | func escapeByte(b byte) string { |
| 654 | if b < ' ' { |
| 655 | return escapedByteSmall[b*4 : b*4+4] |
| 656 | } |
| 657 | |
| 658 | b -= '~' + 1 |
| 659 | // The cast here is needed as b*4 may overflow byte. |
| 660 | return escapedByteLarge[int(b)*4 : int(b)*4+4] |
| 661 | } |
| 662 | |
| 663 | // isDomainNameLabelSpecial returns true if |
| 664 | // a domain name label byte should be prefixed |
| 665 | // with an escaping backslash. |
| 666 | func isDomainNameLabelSpecial(b byte) bool { |
| 667 | switch b { |
| 668 | case '.', ' ', '\'', '@', ';', '(', ')', '"', '\\': |
| 669 | return true |
| 670 | } |
| 671 | return false |
| 672 | } |
| 673 | |
| 674 | func nextByte(s string, offset int) (byte, int) { |
| 675 | if offset >= len(s) { |
| 676 | return 0, 0 |
| 677 | } |
| 678 | if s[offset] != '\\' { |
| 679 | // not an escape sequence |
| 680 | return s[offset], 1 |
| 681 | } |
| 682 | switch len(s) - offset { |
| 683 | case 1: // dangling escape |
| 684 | return 0, 0 |
| 685 | case 2, 3: // too short to be \ddd |
| 686 | default: // maybe \ddd |
| 687 | if isDDD(s[offset+1:]) { |
| 688 | return dddToByte(s[offset+1:]), 4 |
| 689 | } |
| 690 | } |
| 691 | // not \ddd, just an RFC 1035 "quoted" character |
| 692 | return s[offset+1], 2 |
| 693 | } |
| 694 | |
| 695 | // SPF RR. See RFC 4408, Section 3.1.1. |
| 696 | type SPF struct { |
| 697 | Hdr RR_Header |
| 698 | Txt []string `dns:"txt"` |
| 699 | } |
| 700 | |
| 701 | func (rr *SPF) String() string { return rr.Hdr.String() + sprintTxt(rr.Txt) } |
| 702 | |
| 703 | // AVC RR. See https://www.iana.org/assignments/dns-parameters/AVC/avc-completed-template. |
| 704 | type AVC struct { |
| 705 | Hdr RR_Header |
| 706 | Txt []string `dns:"txt"` |
| 707 | } |
| 708 | |
| 709 | func (rr *AVC) String() string { return rr.Hdr.String() + sprintTxt(rr.Txt) } |
| 710 | |
| 711 | // SRV RR. See RFC 2782. |
| 712 | type SRV struct { |
| 713 | Hdr RR_Header |
| 714 | Priority uint16 |
| 715 | Weight uint16 |
| 716 | Port uint16 |
| 717 | Target string `dns:"domain-name"` |
| 718 | } |
| 719 | |
| 720 | func (rr *SRV) String() string { |
| 721 | return rr.Hdr.String() + |
| 722 | strconv.Itoa(int(rr.Priority)) + " " + |
| 723 | strconv.Itoa(int(rr.Weight)) + " " + |
| 724 | strconv.Itoa(int(rr.Port)) + " " + sprintName(rr.Target) |
| 725 | } |
| 726 | |
| 727 | // NAPTR RR. See RFC 2915. |
| 728 | type NAPTR struct { |
| 729 | Hdr RR_Header |
| 730 | Order uint16 |
| 731 | Preference uint16 |
| 732 | Flags string |
| 733 | Service string |
| 734 | Regexp string |
| 735 | Replacement string `dns:"domain-name"` |
| 736 | } |
| 737 | |
| 738 | func (rr *NAPTR) String() string { |
| 739 | return rr.Hdr.String() + |
| 740 | strconv.Itoa(int(rr.Order)) + " " + |
| 741 | strconv.Itoa(int(rr.Preference)) + " " + |
| 742 | "\"" + rr.Flags + "\" " + |
| 743 | "\"" + rr.Service + "\" " + |
| 744 | "\"" + rr.Regexp + "\" " + |
| 745 | rr.Replacement |
| 746 | } |
| 747 | |
| 748 | // CERT RR. See RFC 4398. |
| 749 | type CERT struct { |
| 750 | Hdr RR_Header |
| 751 | Type uint16 |
| 752 | KeyTag uint16 |
| 753 | Algorithm uint8 |
| 754 | Certificate string `dns:"base64"` |
| 755 | } |
| 756 | |
| 757 | func (rr *CERT) String() string { |
| 758 | var ( |
| 759 | ok bool |
| 760 | certtype, algorithm string |
| 761 | ) |
| 762 | if certtype, ok = CertTypeToString[rr.Type]; !ok { |
| 763 | certtype = strconv.Itoa(int(rr.Type)) |
| 764 | } |
| 765 | if algorithm, ok = AlgorithmToString[rr.Algorithm]; !ok { |
| 766 | algorithm = strconv.Itoa(int(rr.Algorithm)) |
| 767 | } |
| 768 | return rr.Hdr.String() + certtype + |
| 769 | " " + strconv.Itoa(int(rr.KeyTag)) + |
| 770 | " " + algorithm + |
| 771 | " " + rr.Certificate |
| 772 | } |
| 773 | |
| 774 | // DNAME RR. See RFC 2672. |
| 775 | type DNAME struct { |
| 776 | Hdr RR_Header |
| 777 | Target string `dns:"domain-name"` |
| 778 | } |
| 779 | |
| 780 | func (rr *DNAME) String() string { |
| 781 | return rr.Hdr.String() + sprintName(rr.Target) |
| 782 | } |
| 783 | |
| 784 | // A RR. See RFC 1035. |
| 785 | type A struct { |
| 786 | Hdr RR_Header |
| 787 | A net.IP `dns:"a"` |
| 788 | } |
| 789 | |
| 790 | func (rr *A) String() string { |
| 791 | if rr.A == nil { |
| 792 | return rr.Hdr.String() |
| 793 | } |
| 794 | return rr.Hdr.String() + rr.A.String() |
| 795 | } |
| 796 | |
| 797 | // AAAA RR. See RFC 3596. |
| 798 | type AAAA struct { |
| 799 | Hdr RR_Header |
| 800 | AAAA net.IP `dns:"aaaa"` |
| 801 | } |
| 802 | |
| 803 | func (rr *AAAA) String() string { |
| 804 | if rr.AAAA == nil { |
| 805 | return rr.Hdr.String() |
| 806 | } |
| 807 | |
| 808 | if rr.AAAA.To4() != nil { |
| 809 | return rr.Hdr.String() + ipv4InIPv6Prefix + rr.AAAA.String() |
| 810 | } |
| 811 | |
| 812 | return rr.Hdr.String() + rr.AAAA.String() |
| 813 | } |
| 814 | |
| 815 | // PX RR. See RFC 2163. |
| 816 | type PX struct { |
| 817 | Hdr RR_Header |
| 818 | Preference uint16 |
| 819 | Map822 string `dns:"domain-name"` |
| 820 | Mapx400 string `dns:"domain-name"` |
| 821 | } |
| 822 | |
| 823 | func (rr *PX) String() string { |
| 824 | return rr.Hdr.String() + strconv.Itoa(int(rr.Preference)) + " " + sprintName(rr.Map822) + " " + sprintName(rr.Mapx400) |
| 825 | } |
| 826 | |
| 827 | // GPOS RR. See RFC 1712. |
| 828 | type GPOS struct { |
| 829 | Hdr RR_Header |
| 830 | Longitude string |
| 831 | Latitude string |
| 832 | Altitude string |
| 833 | } |
| 834 | |
| 835 | func (rr *GPOS) String() string { |
| 836 | return rr.Hdr.String() + rr.Longitude + " " + rr.Latitude + " " + rr.Altitude |
| 837 | } |
| 838 | |
| 839 | // LOC RR. See RFC 1876. |
| 840 | type LOC struct { |
| 841 | Hdr RR_Header |
| 842 | Version uint8 |
| 843 | Size uint8 |
| 844 | HorizPre uint8 |
| 845 | VertPre uint8 |
| 846 | Latitude uint32 |
| 847 | Longitude uint32 |
| 848 | Altitude uint32 |
| 849 | } |
| 850 | |
| 851 | // cmToM takes a cm value expressed in RFC 1876 SIZE mantissa/exponent |
| 852 | // format and returns a string in m (two decimals for the cm). |
| 853 | func cmToM(x uint8) string { |
| 854 | m := x & 0xf0 >> 4 |
| 855 | e := x & 0x0f |
| 856 | |
| 857 | if e < 2 { |
| 858 | if e == 1 { |
| 859 | m *= 10 |
| 860 | } |
| 861 | |
| 862 | return fmt.Sprintf("0.%02d", m) |
| 863 | } |
| 864 | |
| 865 | s := fmt.Sprintf("%d", m) |
| 866 | for e > 2 { |
| 867 | s += "0" |
| 868 | e-- |
| 869 | } |
| 870 | return s |
| 871 | } |
| 872 | |
| 873 | func (rr *LOC) String() string { |
| 874 | s := rr.Hdr.String() |
| 875 | |
| 876 | lat := rr.Latitude |
| 877 | ns := "N" |
| 878 | if lat > LOC_EQUATOR { |
| 879 | lat = lat - LOC_EQUATOR |
| 880 | } else { |
| 881 | ns = "S" |
| 882 | lat = LOC_EQUATOR - lat |
| 883 | } |
| 884 | h := lat / LOC_DEGREES |
| 885 | lat = lat % LOC_DEGREES |
| 886 | m := lat / LOC_HOURS |
| 887 | lat = lat % LOC_HOURS |
| 888 | s += fmt.Sprintf("%02d %02d %0.3f %s ", h, m, float64(lat)/1000, ns) |
| 889 | |
| 890 | lon := rr.Longitude |
| 891 | ew := "E" |
| 892 | if lon > LOC_PRIMEMERIDIAN { |
| 893 | lon = lon - LOC_PRIMEMERIDIAN |
| 894 | } else { |
| 895 | ew = "W" |
| 896 | lon = LOC_PRIMEMERIDIAN - lon |
| 897 | } |
| 898 | h = lon / LOC_DEGREES |
| 899 | lon = lon % LOC_DEGREES |
| 900 | m = lon / LOC_HOURS |
| 901 | lon = lon % LOC_HOURS |
| 902 | s += fmt.Sprintf("%02d %02d %0.3f %s ", h, m, float64(lon)/1000, ew) |
| 903 | |
| 904 | alt := float64(rr.Altitude) / 100 |
| 905 | alt -= LOC_ALTITUDEBASE |
| 906 | if rr.Altitude%100 != 0 { |
| 907 | s += fmt.Sprintf("%.2fm ", alt) |
| 908 | } else { |
| 909 | s += fmt.Sprintf("%.0fm ", alt) |
| 910 | } |
| 911 | |
| 912 | s += cmToM(rr.Size) + "m " |
| 913 | s += cmToM(rr.HorizPre) + "m " |
| 914 | s += cmToM(rr.VertPre) + "m" |
| 915 | return s |
| 916 | } |
| 917 | |
| 918 | // SIG RR. See RFC 2535. The SIG RR is identical to RRSIG and nowadays only used for SIG(0), See RFC 2931. |
| 919 | type SIG struct { |
| 920 | RRSIG |
| 921 | } |
| 922 | |
| 923 | // RRSIG RR. See RFC 4034 and RFC 3755. |
| 924 | type RRSIG struct { |
| 925 | Hdr RR_Header |
| 926 | TypeCovered uint16 |
| 927 | Algorithm uint8 |
| 928 | Labels uint8 |
| 929 | OrigTtl uint32 |
| 930 | Expiration uint32 |
| 931 | Inception uint32 |
| 932 | KeyTag uint16 |
| 933 | SignerName string `dns:"domain-name"` |
| 934 | Signature string `dns:"base64"` |
| 935 | } |
| 936 | |
| 937 | func (rr *RRSIG) String() string { |
| 938 | s := rr.Hdr.String() |
| 939 | s += Type(rr.TypeCovered).String() |
| 940 | s += " " + strconv.Itoa(int(rr.Algorithm)) + |
| 941 | " " + strconv.Itoa(int(rr.Labels)) + |
| 942 | " " + strconv.FormatInt(int64(rr.OrigTtl), 10) + |
| 943 | " " + TimeToString(rr.Expiration) + |
| 944 | " " + TimeToString(rr.Inception) + |
| 945 | " " + strconv.Itoa(int(rr.KeyTag)) + |
| 946 | " " + sprintName(rr.SignerName) + |
| 947 | " " + rr.Signature |
| 948 | return s |
| 949 | } |
| 950 | |
| 951 | // NXT RR. See RFC 2535. |
| 952 | type NXT struct { |
| 953 | NSEC |
| 954 | } |
| 955 | |
| 956 | // NSEC RR. See RFC 4034 and RFC 3755. |
| 957 | type NSEC struct { |
| 958 | Hdr RR_Header |
| 959 | NextDomain string `dns:"domain-name"` |
| 960 | TypeBitMap []uint16 `dns:"nsec"` |
| 961 | } |
| 962 | |
| 963 | func (rr *NSEC) String() string { |
| 964 | s := rr.Hdr.String() + sprintName(rr.NextDomain) |
| 965 | for _, t := range rr.TypeBitMap { |
| 966 | s += " " + Type(t).String() |
| 967 | } |
| 968 | return s |
| 969 | } |
| 970 | |
| 971 | func (rr *NSEC) len(off int, compression map[string]struct{}) int { |
| 972 | l := rr.Hdr.len(off, compression) |
| 973 | l += domainNameLen(rr.NextDomain, off+l, compression, false) |
| 974 | l += typeBitMapLen(rr.TypeBitMap) |
| 975 | return l |
| 976 | } |
| 977 | |
| 978 | // DLV RR. See RFC 4431. |
| 979 | type DLV struct{ DS } |
| 980 | |
| 981 | // CDS RR. See RFC 7344. |
| 982 | type CDS struct{ DS } |
| 983 | |
| 984 | // DS RR. See RFC 4034 and RFC 3658. |
| 985 | type DS struct { |
| 986 | Hdr RR_Header |
| 987 | KeyTag uint16 |
| 988 | Algorithm uint8 |
| 989 | DigestType uint8 |
| 990 | Digest string `dns:"hex"` |
| 991 | } |
| 992 | |
| 993 | func (rr *DS) String() string { |
| 994 | return rr.Hdr.String() + strconv.Itoa(int(rr.KeyTag)) + |
| 995 | " " + strconv.Itoa(int(rr.Algorithm)) + |
| 996 | " " + strconv.Itoa(int(rr.DigestType)) + |
| 997 | " " + strings.ToUpper(rr.Digest) |
| 998 | } |
| 999 | |
| 1000 | // KX RR. See RFC 2230. |
| 1001 | type KX struct { |
| 1002 | Hdr RR_Header |
| 1003 | Preference uint16 |
| 1004 | Exchanger string `dns:"domain-name"` |
| 1005 | } |
| 1006 | |
| 1007 | func (rr *KX) String() string { |
| 1008 | return rr.Hdr.String() + strconv.Itoa(int(rr.Preference)) + |
| 1009 | " " + sprintName(rr.Exchanger) |
| 1010 | } |
| 1011 | |
| 1012 | // TA RR. See http://www.watson.org/~weiler/INI1999-19.pdf. |
| 1013 | type TA struct { |
| 1014 | Hdr RR_Header |
| 1015 | KeyTag uint16 |
| 1016 | Algorithm uint8 |
| 1017 | DigestType uint8 |
| 1018 | Digest string `dns:"hex"` |
| 1019 | } |
| 1020 | |
| 1021 | func (rr *TA) String() string { |
| 1022 | return rr.Hdr.String() + strconv.Itoa(int(rr.KeyTag)) + |
| 1023 | " " + strconv.Itoa(int(rr.Algorithm)) + |
| 1024 | " " + strconv.Itoa(int(rr.DigestType)) + |
| 1025 | " " + strings.ToUpper(rr.Digest) |
| 1026 | } |
| 1027 | |
| 1028 | // TALINK RR. See https://www.iana.org/assignments/dns-parameters/TALINK/talink-completed-template. |
| 1029 | type TALINK struct { |
| 1030 | Hdr RR_Header |
| 1031 | PreviousName string `dns:"domain-name"` |
| 1032 | NextName string `dns:"domain-name"` |
| 1033 | } |
| 1034 | |
| 1035 | func (rr *TALINK) String() string { |
| 1036 | return rr.Hdr.String() + |
| 1037 | sprintName(rr.PreviousName) + " " + sprintName(rr.NextName) |
| 1038 | } |
| 1039 | |
| 1040 | // SSHFP RR. See RFC 4255. |
| 1041 | type SSHFP struct { |
| 1042 | Hdr RR_Header |
| 1043 | Algorithm uint8 |
| 1044 | Type uint8 |
| 1045 | FingerPrint string `dns:"hex"` |
| 1046 | } |
| 1047 | |
| 1048 | func (rr *SSHFP) String() string { |
| 1049 | return rr.Hdr.String() + strconv.Itoa(int(rr.Algorithm)) + |
| 1050 | " " + strconv.Itoa(int(rr.Type)) + |
| 1051 | " " + strings.ToUpper(rr.FingerPrint) |
| 1052 | } |
| 1053 | |
| 1054 | // KEY RR. See RFC 2535. |
| 1055 | type KEY struct { |
| 1056 | DNSKEY |
| 1057 | } |
| 1058 | |
| 1059 | // CDNSKEY RR. See RFC 7344. |
| 1060 | type CDNSKEY struct { |
| 1061 | DNSKEY |
| 1062 | } |
| 1063 | |
| 1064 | // DNSKEY RR. See RFC 4034 and RFC 3755. |
| 1065 | type DNSKEY struct { |
| 1066 | Hdr RR_Header |
| 1067 | Flags uint16 |
| 1068 | Protocol uint8 |
| 1069 | Algorithm uint8 |
| 1070 | PublicKey string `dns:"base64"` |
| 1071 | } |
| 1072 | |
| 1073 | func (rr *DNSKEY) String() string { |
| 1074 | return rr.Hdr.String() + strconv.Itoa(int(rr.Flags)) + |
| 1075 | " " + strconv.Itoa(int(rr.Protocol)) + |
| 1076 | " " + strconv.Itoa(int(rr.Algorithm)) + |
| 1077 | " " + rr.PublicKey |
| 1078 | } |
| 1079 | |
| 1080 | // IPSECKEY RR. See RFC 4025. |
| 1081 | type IPSECKEY struct { |
| 1082 | Hdr RR_Header |
| 1083 | Precedence uint8 |
| 1084 | GatewayType uint8 |
| 1085 | Algorithm uint8 |
| 1086 | GatewayAddr net.IP `dns:"-"` // packing/unpacking/parsing/etc handled together with GatewayHost |
| 1087 | GatewayHost string `dns:"ipsechost"` |
| 1088 | PublicKey string `dns:"base64"` |
| 1089 | } |
| 1090 | |
| 1091 | func (rr *IPSECKEY) String() string { |
| 1092 | var gateway string |
| 1093 | switch rr.GatewayType { |
| 1094 | case IPSECGatewayIPv4, IPSECGatewayIPv6: |
| 1095 | gateway = rr.GatewayAddr.String() |
| 1096 | case IPSECGatewayHost: |
| 1097 | gateway = rr.GatewayHost |
| 1098 | case IPSECGatewayNone: |
| 1099 | fallthrough |
| 1100 | default: |
| 1101 | gateway = "." |
| 1102 | } |
| 1103 | |
| 1104 | return rr.Hdr.String() + strconv.Itoa(int(rr.Precedence)) + |
| 1105 | " " + strconv.Itoa(int(rr.GatewayType)) + |
| 1106 | " " + strconv.Itoa(int(rr.Algorithm)) + |
| 1107 | " " + gateway + |
| 1108 | " " + rr.PublicKey |
| 1109 | } |
| 1110 | |
| 1111 | // AMTRELAY RR. See RFC 8777. |
| 1112 | type AMTRELAY struct { |
| 1113 | Hdr RR_Header |
| 1114 | Precedence uint8 |
| 1115 | GatewayType uint8 // discovery is packed in here at bit 0x80 |
| 1116 | GatewayAddr net.IP `dns:"-"` // packing/unpacking/parsing/etc handled together with GatewayHost |
| 1117 | GatewayHost string `dns:"amtrelayhost"` |
| 1118 | } |
| 1119 | |
| 1120 | func (rr *AMTRELAY) String() string { |
| 1121 | var gateway string |
| 1122 | switch rr.GatewayType & 0x7f { |
| 1123 | case AMTRELAYIPv4, AMTRELAYIPv6: |
| 1124 | gateway = rr.GatewayAddr.String() |
| 1125 | case AMTRELAYHost: |
| 1126 | gateway = rr.GatewayHost |
| 1127 | case AMTRELAYNone: |
| 1128 | fallthrough |
| 1129 | default: |
| 1130 | gateway = "." |
| 1131 | } |
| 1132 | boolS := "0" |
| 1133 | if rr.GatewayType&0x80 == 0x80 { |
| 1134 | boolS = "1" |
| 1135 | } |
| 1136 | |
| 1137 | return rr.Hdr.String() + strconv.Itoa(int(rr.Precedence)) + |
| 1138 | " " + boolS + |
| 1139 | " " + strconv.Itoa(int(rr.GatewayType&0x7f)) + |
| 1140 | " " + gateway |
| 1141 | } |
| 1142 | |
| 1143 | // RKEY RR. See https://www.iana.org/assignments/dns-parameters/RKEY/rkey-completed-template. |
| 1144 | type RKEY struct { |
| 1145 | Hdr RR_Header |
| 1146 | Flags uint16 |
| 1147 | Protocol uint8 |
| 1148 | Algorithm uint8 |
| 1149 | PublicKey string `dns:"base64"` |
| 1150 | } |
| 1151 | |
| 1152 | func (rr *RKEY) String() string { |
| 1153 | return rr.Hdr.String() + strconv.Itoa(int(rr.Flags)) + |
| 1154 | " " + strconv.Itoa(int(rr.Protocol)) + |
| 1155 | " " + strconv.Itoa(int(rr.Algorithm)) + |
| 1156 | " " + rr.PublicKey |
| 1157 | } |
| 1158 | |
| 1159 | // NSAPPTR RR. See RFC 1348. |
| 1160 | type NSAPPTR struct { |
| 1161 | Hdr RR_Header |
| 1162 | Ptr string `dns:"domain-name"` |
| 1163 | } |
| 1164 | |
| 1165 | func (rr *NSAPPTR) String() string { return rr.Hdr.String() + sprintName(rr.Ptr) } |
| 1166 | |
| 1167 | // NSEC3 RR. See RFC 5155. |
| 1168 | type NSEC3 struct { |
| 1169 | Hdr RR_Header |
| 1170 | Hash uint8 |
| 1171 | Flags uint8 |
| 1172 | Iterations uint16 |
| 1173 | SaltLength uint8 |
| 1174 | Salt string `dns:"size-hex:SaltLength"` |
| 1175 | HashLength uint8 |
| 1176 | NextDomain string `dns:"size-base32:HashLength"` |
| 1177 | TypeBitMap []uint16 `dns:"nsec"` |
| 1178 | } |
| 1179 | |
| 1180 | func (rr *NSEC3) String() string { |
| 1181 | s := rr.Hdr.String() |
| 1182 | s += strconv.Itoa(int(rr.Hash)) + |
| 1183 | " " + strconv.Itoa(int(rr.Flags)) + |
| 1184 | " " + strconv.Itoa(int(rr.Iterations)) + |
| 1185 | " " + saltToString(rr.Salt) + |
| 1186 | " " + rr.NextDomain |
| 1187 | for _, t := range rr.TypeBitMap { |
| 1188 | s += " " + Type(t).String() |
| 1189 | } |
| 1190 | return s |
| 1191 | } |
| 1192 | |
| 1193 | func (rr *NSEC3) len(off int, compression map[string]struct{}) int { |
| 1194 | l := rr.Hdr.len(off, compression) |
| 1195 | l += 6 + len(rr.Salt)/2 + 1 + len(rr.NextDomain) + 1 |
| 1196 | l += typeBitMapLen(rr.TypeBitMap) |
| 1197 | return l |
| 1198 | } |
| 1199 | |
| 1200 | // NSEC3PARAM RR. See RFC 5155. |
| 1201 | type NSEC3PARAM struct { |
| 1202 | Hdr RR_Header |
| 1203 | Hash uint8 |
| 1204 | Flags uint8 |
| 1205 | Iterations uint16 |
| 1206 | SaltLength uint8 |
| 1207 | Salt string `dns:"size-hex:SaltLength"` |
| 1208 | } |
| 1209 | |
| 1210 | func (rr *NSEC3PARAM) String() string { |
| 1211 | s := rr.Hdr.String() |
| 1212 | s += strconv.Itoa(int(rr.Hash)) + |
| 1213 | " " + strconv.Itoa(int(rr.Flags)) + |
| 1214 | " " + strconv.Itoa(int(rr.Iterations)) + |
| 1215 | " " + saltToString(rr.Salt) |
| 1216 | return s |
| 1217 | } |
| 1218 | |
| 1219 | // TKEY RR. See RFC 2930. |
| 1220 | type TKEY struct { |
| 1221 | Hdr RR_Header |
| 1222 | Algorithm string `dns:"domain-name"` |
| 1223 | Inception uint32 |
| 1224 | Expiration uint32 |
| 1225 | Mode uint16 |
| 1226 | Error uint16 |
| 1227 | KeySize uint16 |
| 1228 | Key string `dns:"size-hex:KeySize"` |
| 1229 | OtherLen uint16 |
| 1230 | OtherData string `dns:"size-hex:OtherLen"` |
| 1231 | } |
| 1232 | |
| 1233 | // TKEY has no official presentation format, but this will suffice. |
| 1234 | func (rr *TKEY) String() string { |
| 1235 | s := ";" + rr.Hdr.String() + |
| 1236 | " " + rr.Algorithm + |
| 1237 | " " + TimeToString(rr.Inception) + |
| 1238 | " " + TimeToString(rr.Expiration) + |
| 1239 | " " + strconv.Itoa(int(rr.Mode)) + |
| 1240 | " " + strconv.Itoa(int(rr.Error)) + |
| 1241 | " " + strconv.Itoa(int(rr.KeySize)) + |
| 1242 | " " + rr.Key + |
| 1243 | " " + strconv.Itoa(int(rr.OtherLen)) + |
| 1244 | " " + rr.OtherData |
| 1245 | return s |
| 1246 | } |
| 1247 | |
| 1248 | // RFC3597 represents an unknown/generic RR. See RFC 3597. |
| 1249 | type RFC3597 struct { |
| 1250 | Hdr RR_Header |
| 1251 | Rdata string `dns:"hex"` |
| 1252 | } |
| 1253 | |
| 1254 | func (rr *RFC3597) String() string { |
| 1255 | // Let's call it a hack |
| 1256 | s := rfc3597Header(rr.Hdr) |
| 1257 | |
| 1258 | s += "\\# " + strconv.Itoa(len(rr.Rdata)/2) + " " + rr.Rdata |
| 1259 | return s |
| 1260 | } |
| 1261 | |
| 1262 | func rfc3597Header(h RR_Header) string { |
| 1263 | var s string |
| 1264 | |
| 1265 | s += sprintName(h.Name) + "\t" |
| 1266 | s += strconv.FormatInt(int64(h.Ttl), 10) + "\t" |
| 1267 | s += "CLASS" + strconv.Itoa(int(h.Class)) + "\t" |
| 1268 | s += "TYPE" + strconv.Itoa(int(h.Rrtype)) + "\t" |
| 1269 | return s |
| 1270 | } |
| 1271 | |
| 1272 | // URI RR. See RFC 7553. |
| 1273 | type URI struct { |
| 1274 | Hdr RR_Header |
| 1275 | Priority uint16 |
| 1276 | Weight uint16 |
| 1277 | Target string `dns:"octet"` |
| 1278 | } |
| 1279 | |
| 1280 | // rr.Target to be parsed as a sequence of character encoded octets according to RFC 3986 |
| 1281 | func (rr *URI) String() string { |
| 1282 | return rr.Hdr.String() + strconv.Itoa(int(rr.Priority)) + |
| 1283 | " " + strconv.Itoa(int(rr.Weight)) + " " + sprintTxtOctet(rr.Target) |
| 1284 | } |
| 1285 | |
| 1286 | // DHCID RR. See RFC 4701. |
| 1287 | type DHCID struct { |
| 1288 | Hdr RR_Header |
| 1289 | Digest string `dns:"base64"` |
| 1290 | } |
| 1291 | |
| 1292 | func (rr *DHCID) String() string { return rr.Hdr.String() + rr.Digest } |
| 1293 | |
| 1294 | // TLSA RR. See RFC 6698. |
| 1295 | type TLSA struct { |
| 1296 | Hdr RR_Header |
| 1297 | Usage uint8 |
| 1298 | Selector uint8 |
| 1299 | MatchingType uint8 |
| 1300 | Certificate string `dns:"hex"` |
| 1301 | } |
| 1302 | |
| 1303 | func (rr *TLSA) String() string { |
| 1304 | return rr.Hdr.String() + |
| 1305 | strconv.Itoa(int(rr.Usage)) + |
| 1306 | " " + strconv.Itoa(int(rr.Selector)) + |
| 1307 | " " + strconv.Itoa(int(rr.MatchingType)) + |
| 1308 | " " + rr.Certificate |
| 1309 | } |
| 1310 | |
| 1311 | // SMIMEA RR. See RFC 8162. |
| 1312 | type SMIMEA struct { |
| 1313 | Hdr RR_Header |
| 1314 | Usage uint8 |
| 1315 | Selector uint8 |
| 1316 | MatchingType uint8 |
| 1317 | Certificate string `dns:"hex"` |
| 1318 | } |
| 1319 | |
| 1320 | func (rr *SMIMEA) String() string { |
| 1321 | s := rr.Hdr.String() + |
| 1322 | strconv.Itoa(int(rr.Usage)) + |
| 1323 | " " + strconv.Itoa(int(rr.Selector)) + |
| 1324 | " " + strconv.Itoa(int(rr.MatchingType)) |
| 1325 | |
| 1326 | // Every Nth char needs a space on this output. If we output |
| 1327 | // this as one giant line, we can't read it can in because in some cases |
| 1328 | // the cert length overflows scan.maxTok (2048). |
| 1329 | sx := splitN(rr.Certificate, 1024) // conservative value here |
| 1330 | s += " " + strings.Join(sx, " ") |
| 1331 | return s |
| 1332 | } |
| 1333 | |
| 1334 | // HIP RR. See RFC 8005. |
| 1335 | type HIP struct { |
| 1336 | Hdr RR_Header |
| 1337 | HitLength uint8 |
| 1338 | PublicKeyAlgorithm uint8 |
| 1339 | PublicKeyLength uint16 |
| 1340 | Hit string `dns:"size-hex:HitLength"` |
| 1341 | PublicKey string `dns:"size-base64:PublicKeyLength"` |
| 1342 | RendezvousServers []string `dns:"domain-name"` |
| 1343 | } |
| 1344 | |
| 1345 | func (rr *HIP) String() string { |
| 1346 | s := rr.Hdr.String() + |
| 1347 | strconv.Itoa(int(rr.PublicKeyAlgorithm)) + |
| 1348 | " " + rr.Hit + |
| 1349 | " " + rr.PublicKey |
| 1350 | for _, d := range rr.RendezvousServers { |
| 1351 | s += " " + sprintName(d) |
| 1352 | } |
| 1353 | return s |
| 1354 | } |
| 1355 | |
| 1356 | // NINFO RR. See https://www.iana.org/assignments/dns-parameters/NINFO/ninfo-completed-template. |
| 1357 | type NINFO struct { |
| 1358 | Hdr RR_Header |
| 1359 | ZSData []string `dns:"txt"` |
| 1360 | } |
| 1361 | |
| 1362 | func (rr *NINFO) String() string { return rr.Hdr.String() + sprintTxt(rr.ZSData) } |
| 1363 | |
| 1364 | // NID RR. See RFC 6742. |
| 1365 | type NID struct { |
| 1366 | Hdr RR_Header |
| 1367 | Preference uint16 |
| 1368 | NodeID uint64 |
| 1369 | } |
| 1370 | |
| 1371 | func (rr *NID) String() string { |
| 1372 | s := rr.Hdr.String() + strconv.Itoa(int(rr.Preference)) |
| 1373 | node := fmt.Sprintf("%0.16x", rr.NodeID) |
| 1374 | s += " " + node[0:4] + ":" + node[4:8] + ":" + node[8:12] + ":" + node[12:16] |
| 1375 | return s |
| 1376 | } |
| 1377 | |
| 1378 | // L32 RR, See RFC 6742. |
| 1379 | type L32 struct { |
| 1380 | Hdr RR_Header |
| 1381 | Preference uint16 |
| 1382 | Locator32 net.IP `dns:"a"` |
| 1383 | } |
| 1384 | |
| 1385 | func (rr *L32) String() string { |
| 1386 | if rr.Locator32 == nil { |
| 1387 | return rr.Hdr.String() + strconv.Itoa(int(rr.Preference)) |
| 1388 | } |
| 1389 | return rr.Hdr.String() + strconv.Itoa(int(rr.Preference)) + |
| 1390 | " " + rr.Locator32.String() |
| 1391 | } |
| 1392 | |
| 1393 | // L64 RR, See RFC 6742. |
| 1394 | type L64 struct { |
| 1395 | Hdr RR_Header |
| 1396 | Preference uint16 |
| 1397 | Locator64 uint64 |
| 1398 | } |
| 1399 | |
| 1400 | func (rr *L64) String() string { |
| 1401 | s := rr.Hdr.String() + strconv.Itoa(int(rr.Preference)) |
| 1402 | node := fmt.Sprintf("%0.16X", rr.Locator64) |
| 1403 | s += " " + node[0:4] + ":" + node[4:8] + ":" + node[8:12] + ":" + node[12:16] |
| 1404 | return s |
| 1405 | } |
| 1406 | |
| 1407 | // LP RR. See RFC 6742. |
| 1408 | type LP struct { |
| 1409 | Hdr RR_Header |
| 1410 | Preference uint16 |
| 1411 | Fqdn string `dns:"domain-name"` |
| 1412 | } |
| 1413 | |
| 1414 | func (rr *LP) String() string { |
| 1415 | return rr.Hdr.String() + strconv.Itoa(int(rr.Preference)) + " " + sprintName(rr.Fqdn) |
| 1416 | } |
| 1417 | |
| 1418 | // EUI48 RR. See RFC 7043. |
| 1419 | type EUI48 struct { |
| 1420 | Hdr RR_Header |
| 1421 | Address uint64 `dns:"uint48"` |
| 1422 | } |
| 1423 | |
| 1424 | func (rr *EUI48) String() string { return rr.Hdr.String() + euiToString(rr.Address, 48) } |
| 1425 | |
| 1426 | // EUI64 RR. See RFC 7043. |
| 1427 | type EUI64 struct { |
| 1428 | Hdr RR_Header |
| 1429 | Address uint64 |
| 1430 | } |
| 1431 | |
| 1432 | func (rr *EUI64) String() string { return rr.Hdr.String() + euiToString(rr.Address, 64) } |
| 1433 | |
| 1434 | // CAA RR. See RFC 6844. |
| 1435 | type CAA struct { |
| 1436 | Hdr RR_Header |
| 1437 | Flag uint8 |
| 1438 | Tag string |
| 1439 | Value string `dns:"octet"` |
| 1440 | } |
| 1441 | |
| 1442 | // rr.Value Is the character-string encoding of the value field as specified in RFC 1035, Section 5.1. |
| 1443 | func (rr *CAA) String() string { |
| 1444 | return rr.Hdr.String() + strconv.Itoa(int(rr.Flag)) + " " + rr.Tag + " " + sprintTxtOctet(rr.Value) |
| 1445 | } |
| 1446 | |
| 1447 | // UID RR. Deprecated, IANA-Reserved. |
| 1448 | type UID struct { |
| 1449 | Hdr RR_Header |
| 1450 | Uid uint32 |
| 1451 | } |
| 1452 | |
| 1453 | func (rr *UID) String() string { return rr.Hdr.String() + strconv.FormatInt(int64(rr.Uid), 10) } |
| 1454 | |
| 1455 | // GID RR. Deprecated, IANA-Reserved. |
| 1456 | type GID struct { |
| 1457 | Hdr RR_Header |
| 1458 | Gid uint32 |
| 1459 | } |
| 1460 | |
| 1461 | func (rr *GID) String() string { return rr.Hdr.String() + strconv.FormatInt(int64(rr.Gid), 10) } |
| 1462 | |
| 1463 | // UINFO RR. Deprecated, IANA-Reserved. |
| 1464 | type UINFO struct { |
| 1465 | Hdr RR_Header |
| 1466 | Uinfo string |
| 1467 | } |
| 1468 | |
| 1469 | func (rr *UINFO) String() string { return rr.Hdr.String() + sprintTxt([]string{rr.Uinfo}) } |
| 1470 | |
| 1471 | // EID RR. See http://ana-3.lcs.mit.edu/~jnc/nimrod/dns.txt. |
| 1472 | type EID struct { |
| 1473 | Hdr RR_Header |
| 1474 | Endpoint string `dns:"hex"` |
| 1475 | } |
| 1476 | |
| 1477 | func (rr *EID) String() string { return rr.Hdr.String() + strings.ToUpper(rr.Endpoint) } |
| 1478 | |
| 1479 | // NIMLOC RR. See http://ana-3.lcs.mit.edu/~jnc/nimrod/dns.txt. |
| 1480 | type NIMLOC struct { |
| 1481 | Hdr RR_Header |
| 1482 | Locator string `dns:"hex"` |
| 1483 | } |
| 1484 | |
| 1485 | func (rr *NIMLOC) String() string { return rr.Hdr.String() + strings.ToUpper(rr.Locator) } |
| 1486 | |
| 1487 | // OPENPGPKEY RR. See RFC 7929. |
| 1488 | type OPENPGPKEY struct { |
| 1489 | Hdr RR_Header |
| 1490 | PublicKey string `dns:"base64"` |
| 1491 | } |
| 1492 | |
| 1493 | func (rr *OPENPGPKEY) String() string { return rr.Hdr.String() + rr.PublicKey } |
| 1494 | |
| 1495 | // CSYNC RR. See RFC 7477. |
| 1496 | type CSYNC struct { |
| 1497 | Hdr RR_Header |
| 1498 | Serial uint32 |
| 1499 | Flags uint16 |
| 1500 | TypeBitMap []uint16 `dns:"nsec"` |
| 1501 | } |
| 1502 | |
| 1503 | func (rr *CSYNC) String() string { |
| 1504 | s := rr.Hdr.String() + strconv.FormatInt(int64(rr.Serial), 10) + " " + strconv.Itoa(int(rr.Flags)) |
| 1505 | |
| 1506 | for _, t := range rr.TypeBitMap { |
| 1507 | s += " " + Type(t).String() |
| 1508 | } |
| 1509 | return s |
| 1510 | } |
| 1511 | |
| 1512 | func (rr *CSYNC) len(off int, compression map[string]struct{}) int { |
| 1513 | l := rr.Hdr.len(off, compression) |
| 1514 | l += 4 + 2 |
| 1515 | l += typeBitMapLen(rr.TypeBitMap) |
| 1516 | return l |
| 1517 | } |
| 1518 | |
| 1519 | // ZONEMD RR, from draft-ietf-dnsop-dns-zone-digest |
| 1520 | type ZONEMD struct { |
| 1521 | Hdr RR_Header |
| 1522 | Serial uint32 |
| 1523 | Scheme uint8 |
| 1524 | Hash uint8 |
| 1525 | Digest string `dns:"hex"` |
| 1526 | } |
| 1527 | |
| 1528 | func (rr *ZONEMD) String() string { |
| 1529 | return rr.Hdr.String() + |
| 1530 | strconv.Itoa(int(rr.Serial)) + |
| 1531 | " " + strconv.Itoa(int(rr.Scheme)) + |
| 1532 | " " + strconv.Itoa(int(rr.Hash)) + |
| 1533 | " " + rr.Digest |
| 1534 | } |
| 1535 | |
| 1536 | // RESINFO RR. See RFC 9606. |
| 1537 | |
| 1538 | type RESINFO struct { |
| 1539 | Hdr RR_Header |
| 1540 | Txt []string `dns:"txt"` |
| 1541 | } |
| 1542 | |
| 1543 | func (rr *RESINFO) String() string { return rr.Hdr.String() + sprintTxt(rr.Txt) } |
| 1544 | |
| 1545 | // APL RR. See RFC 3123. |
| 1546 | type APL struct { |
| 1547 | Hdr RR_Header |
| 1548 | Prefixes []APLPrefix `dns:"apl"` |
| 1549 | } |
| 1550 | |
| 1551 | // APLPrefix is an address prefix hold by an APL record. |
| 1552 | type APLPrefix struct { |
| 1553 | Negation bool |
| 1554 | Network net.IPNet |
| 1555 | } |
| 1556 | |
| 1557 | // String returns presentation form of the APL record. |
| 1558 | func (rr *APL) String() string { |
| 1559 | var sb strings.Builder |
| 1560 | sb.WriteString(rr.Hdr.String()) |
| 1561 | for i, p := range rr.Prefixes { |
| 1562 | if i > 0 { |
| 1563 | sb.WriteByte(' ') |
| 1564 | } |
| 1565 | sb.WriteString(p.str()) |
| 1566 | } |
| 1567 | return sb.String() |
| 1568 | } |
| 1569 | |
| 1570 | // str returns presentation form of the APL prefix. |
| 1571 | func (a *APLPrefix) str() string { |
| 1572 | var sb strings.Builder |
| 1573 | if a.Negation { |
| 1574 | sb.WriteByte('!') |
| 1575 | } |
| 1576 | |
| 1577 | switch len(a.Network.IP) { |
| 1578 | case net.IPv4len: |
| 1579 | sb.WriteByte('1') |
| 1580 | case net.IPv6len: |
| 1581 | sb.WriteByte('2') |
| 1582 | } |
| 1583 | |
| 1584 | sb.WriteByte(':') |
| 1585 | |
| 1586 | switch len(a.Network.IP) { |
| 1587 | case net.IPv4len: |
| 1588 | sb.WriteString(a.Network.IP.String()) |
| 1589 | case net.IPv6len: |
| 1590 | // add prefix for IPv4-mapped IPv6 |
| 1591 | if v4 := a.Network.IP.To4(); v4 != nil { |
| 1592 | sb.WriteString(ipv4InIPv6Prefix) |
| 1593 | } |
| 1594 | sb.WriteString(a.Network.IP.String()) |
| 1595 | } |
| 1596 | |
| 1597 | sb.WriteByte('/') |
| 1598 | |
| 1599 | prefix, _ := a.Network.Mask.Size() |
| 1600 | sb.WriteString(strconv.Itoa(prefix)) |
| 1601 | |
| 1602 | return sb.String() |
| 1603 | } |
| 1604 | |
| 1605 | // equals reports whether two APL prefixes are identical. |
| 1606 | func (a *APLPrefix) equals(b *APLPrefix) bool { |
| 1607 | return a.Negation == b.Negation && |
| 1608 | a.Network.IP.Equal(b.Network.IP) && |
| 1609 | bytes.Equal(a.Network.Mask, b.Network.Mask) |
| 1610 | } |
| 1611 | |
| 1612 | // copy returns a copy of the APL prefix. |
| 1613 | func (a *APLPrefix) copy() APLPrefix { |
| 1614 | return APLPrefix{ |
| 1615 | Negation: a.Negation, |
| 1616 | Network: copyNet(a.Network), |
| 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | // len returns size of the prefix in wire format. |
| 1621 | func (a *APLPrefix) len() int { |
| 1622 | // 4-byte header and the network address prefix (see Section 4 of RFC 3123) |
| 1623 | prefix, _ := a.Network.Mask.Size() |
| 1624 | return 4 + (prefix+7)/8 |
| 1625 | } |
| 1626 | |
| 1627 | // TimeToString translates the RRSIG's incep. and expir. times to the |
| 1628 | // string representation used when printing the record. |
| 1629 | // It takes serial arithmetic (RFC 1982) into account. |
| 1630 | func TimeToString(t uint32) string { |
| 1631 | mod := (int64(t)-time.Now().Unix())/year68 - 1 |
| 1632 | if mod < 0 { |
| 1633 | mod = 0 |
| 1634 | } |
| 1635 | ti := time.Unix(int64(t)-mod*year68, 0).UTC() |
| 1636 | return ti.Format("20060102150405") |
| 1637 | } |
| 1638 | |
| 1639 | // StringToTime translates the RRSIG's incep. and expir. times from |
| 1640 | // string values like "20110403154150" to an 32 bit integer. |
| 1641 | // It takes serial arithmetic (RFC 1982) into account. |
| 1642 | func StringToTime(s string) (uint32, error) { |
| 1643 | t, err := time.Parse("20060102150405", s) |
| 1644 | if err != nil { |
| 1645 | return 0, err |
| 1646 | } |
| 1647 | mod := t.Unix()/year68 - 1 |
| 1648 | if mod < 0 { |
| 1649 | mod = 0 |
| 1650 | } |
| 1651 | return uint32(t.Unix() - mod*year68), nil |
| 1652 | } |
| 1653 | |
| 1654 | // saltToString converts a NSECX salt to uppercase and returns "-" when it is empty. |
| 1655 | func saltToString(s string) string { |
| 1656 | if s == "" { |
| 1657 | return "-" |
| 1658 | } |
| 1659 | return strings.ToUpper(s) |
| 1660 | } |
| 1661 | |
| 1662 | func euiToString(eui uint64, bits int) (hex string) { |
| 1663 | switch bits { |
| 1664 | case 64: |
| 1665 | hex = fmt.Sprintf("%16.16x", eui) |
| 1666 | hex = hex[0:2] + "-" + hex[2:4] + "-" + hex[4:6] + "-" + hex[6:8] + |
| 1667 | "-" + hex[8:10] + "-" + hex[10:12] + "-" + hex[12:14] + "-" + hex[14:16] |
| 1668 | case 48: |
| 1669 | hex = fmt.Sprintf("%12.12x", eui) |
| 1670 | hex = hex[0:2] + "-" + hex[2:4] + "-" + hex[4:6] + "-" + hex[6:8] + |
| 1671 | "-" + hex[8:10] + "-" + hex[10:12] |
| 1672 | } |
| 1673 | return |
| 1674 | } |
| 1675 | |
| 1676 | // cloneSlice returns a shallow copy of s. |
| 1677 | func cloneSlice[E any, S ~[]E](s S) S { |
| 1678 | if s == nil { |
| 1679 | return nil |
| 1680 | } |
| 1681 | return append(S(nil), s...) |
| 1682 | } |
| 1683 | |
| 1684 | // copyNet returns a copy of a subnet. |
| 1685 | func copyNet(n net.IPNet) net.IPNet { |
| 1686 | return net.IPNet{ |
| 1687 | IP: cloneSlice(n.IP), |
| 1688 | Mask: cloneSlice(n.Mask), |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | // SplitN splits a string into N sized string chunks. |
| 1693 | // This might become an exported function once. |
| 1694 | func splitN(s string, n int) []string { |
| 1695 | if len(s) < n { |
| 1696 | return []string{s} |
| 1697 | } |
| 1698 | sx := []string{} |
| 1699 | p, i := 0, n |
| 1700 | for { |
| 1701 | if i <= len(s) { |
| 1702 | sx = append(sx, s[p:i]) |
| 1703 | } else { |
| 1704 | sx = append(sx, s[p:]) |
| 1705 | break |
| 1706 | |
| 1707 | } |
| 1708 | p, i = p+n, i+n |
| 1709 | } |
| 1710 | |
| 1711 | return sx |
| 1712 | } |