github.com/phuslu/fastdns@v0.8.3-0.20240310041952-69506fc67dd1/types.go (about)

     1  package fastdns
     2  
     3  // Rcode denotes a 4bit field that specifies the response
     4  // code for a query.
     5  type Rcode byte
     6  
     7  // Message Response Codes, see https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml
     8  const (
     9  	RcodeNoError   Rcode = 0  // No Error                          [DNS]
    10  	RcodeFormErr   Rcode = 1  // Format Error                      [DNS]
    11  	RcodeServFail  Rcode = 2  // Server Failure                    [DNS]
    12  	RcodeNXDomain  Rcode = 3  // Non-Existent Domain               [DNS]
    13  	RcodeNotImp    Rcode = 4  // Not Implemented                   [DNS]
    14  	RcodeRefused   Rcode = 5  // Query Refused                     [DNS]
    15  	RcodeYXDomain  Rcode = 6  // Name Exists when it should not    [DNS Update]
    16  	RcodeYXRRSet   Rcode = 7  // RR Set Exists when it should not  [DNS Update]
    17  	RcodeNXRRSet   Rcode = 8  // RR Set that should exist does not [DNS Update]
    18  	RcodeNotAuth   Rcode = 9  // Server Not Authoritative for zone [DNS Update]
    19  	RcodeNotZone   Rcode = 10 // Name not contained in zone        [DNS Update/TSIG]
    20  	RcodeBADSIG    Rcode = 16 // TSIG Signature Failure            [TSIG]
    21  	RcodeBADVERS   Rcode = 16 // Bad OPT Version                   [EDNS0]
    22  	RcodeBADKEY    Rcode = 17 // Key not recognized                [TSIG]
    23  	RcodeBADTIME   Rcode = 18 // Signature out of time window      [TSIG]
    24  	RcodeBADMODE   Rcode = 19 // Bad TKEY Mode                     [TKEY]
    25  	RcodeBADNAME   Rcode = 20 // Duplicate key name                [TKEY]
    26  	RcodeBADALG    Rcode = 21 // Algorithm not supported           [TKEY]
    27  	RcodeBADTRUNC  Rcode = 22 // Bad Truncation                    [TSIG]
    28  	RcodeBADCOOKIE Rcode = 23 // Bad/missing Server Cookie         [DNS Cookies]
    29  )
    30  
    31  func (c Rcode) String() string {
    32  	switch c {
    33  	case RcodeNoError:
    34  		return "NoError"
    35  	case RcodeFormErr:
    36  		return "FormErr"
    37  	case RcodeServFail:
    38  		return "ServFail"
    39  	case RcodeNXDomain:
    40  		return "NXDomain"
    41  	case RcodeNotImp:
    42  		return "NotImp"
    43  	case RcodeRefused:
    44  		return "Refused"
    45  	case RcodeYXDomain:
    46  		return "YXDomain"
    47  	case RcodeYXRRSet:
    48  		return "YXRRSet"
    49  	case RcodeNXRRSet:
    50  		return "NXRRSet"
    51  	case RcodeNotAuth:
    52  		return "NotAuth"
    53  	case RcodeNotZone:
    54  		return "NotZone"
    55  	case RcodeBADSIG: // RcodeBADVERS
    56  		return "BadSig/BadVers"
    57  	case RcodeBADKEY:
    58  		return "BadKey"
    59  	case RcodeBADTIME:
    60  		return "BadTime"
    61  	case RcodeBADMODE:
    62  		return "BadMode"
    63  	case RcodeBADNAME:
    64  		return "BadName"
    65  	case RcodeBADALG:
    66  		return "BadAlg"
    67  	case RcodeBADTRUNC:
    68  		return "BadTrunc"
    69  	case RcodeBADCOOKIE:
    70  		return "BadCookie"
    71  	}
    72  	return ""
    73  }
    74  
    75  // Opcode denotes a 4bit field that specified the query type.
    76  type Opcode byte
    77  
    78  // Wire constants and supported types.
    79  const (
    80  	OpcodeQuery  Opcode = 0
    81  	OpcodeIQuery Opcode = 1
    82  	OpcodeStatus Opcode = 2
    83  	OpcodeNotify Opcode = 4
    84  	OpcodeUpdate Opcode = 5
    85  )
    86  
    87  func (c Opcode) String() string {
    88  	switch c {
    89  	case OpcodeQuery:
    90  		return "Query"
    91  	case OpcodeIQuery:
    92  		return "IQuery"
    93  	case OpcodeStatus:
    94  		return "Status"
    95  	case OpcodeNotify:
    96  		return "Notify"
    97  	case OpcodeUpdate:
    98  		return "Update"
    99  	}
   100  	return ""
   101  }
   102  
   103  // Flags is an arbitrary 16bit represents QR, Opcode, AA, TC, RD, RA, Z and RCODE.
   104  //
   105  //   0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
   106  // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
   107  // |QR|   Opcode  |AA|TC|RD|RA|   Z    |   RCODE   |
   108  // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
   109  type Flags uint16
   110  
   111  // QR is QR bit in Flags
   112  func (f Flags) QR() byte {
   113  	return byte(f >> 15)
   114  }
   115  
   116  // Opcode is Opcode in Flags
   117  func (f Flags) Opcode() Opcode {
   118  	return Opcode((f & 0b0111111111111111) >> 11)
   119  }
   120  
   121  // AA is AA bit in Flags
   122  func (f Flags) AA() byte {
   123  	return byte((f & 0b0000010000000000) >> 10)
   124  }
   125  
   126  // TC is TC bit in Flags
   127  func (f Flags) TC() byte {
   128  	return byte((f & 0b0000001000000000) >> 9)
   129  }
   130  
   131  // RD is RD bit in Flags
   132  func (f Flags) RD() byte {
   133  	return byte((f & 0b0000000100000000) >> 8)
   134  }
   135  
   136  // RA is RA bit in Flags
   137  func (f Flags) RA() byte {
   138  	return byte((f & 0b0000000010000000) >> 7)
   139  }
   140  
   141  // Z is Z bits in Flags
   142  func (f Flags) Z() byte {
   143  	return byte((f & 0b0000000001110000) >> 4)
   144  }
   145  
   146  // Rcode is Rcode in Flags
   147  func (f Flags) Rcode() Rcode {
   148  	return Rcode((f & 0b0000000000001111))
   149  }
   150  
   151  // Class is a DNS class.
   152  type Class uint16
   153  
   154  // Wire constants and supported types.
   155  const (
   156  	ClassINET   Class = 1
   157  	ClassCSNET  Class = 2
   158  	ClassCHAOS  Class = 3
   159  	ClassHESIOD Class = 4
   160  	ClassNONE   Class = 254
   161  	ClassANY    Class = 255
   162  )
   163  
   164  func (c Class) String() string {
   165  	switch c {
   166  	case ClassINET:
   167  		return "IN"
   168  	case ClassCSNET:
   169  		return "CS"
   170  	case ClassCHAOS:
   171  		return "CH"
   172  	case ClassHESIOD:
   173  		return "HS"
   174  	case ClassNONE:
   175  		return "NONE"
   176  	case ClassANY:
   177  		return "ANY"
   178  	}
   179  	return ""
   180  }
   181  
   182  // Type is a DNS type.
   183  type Type uint16
   184  
   185  // Wire constants and supported types.
   186  const (
   187  	TypeNone       Type = 0
   188  	TypeA          Type = 1
   189  	TypeNS         Type = 2
   190  	TypeMD         Type = 3
   191  	TypeMF         Type = 4
   192  	TypeCNAME      Type = 5
   193  	TypeSOA        Type = 6
   194  	TypeMB         Type = 7
   195  	TypeMG         Type = 8
   196  	TypeMR         Type = 9
   197  	TypeNULL       Type = 10
   198  	TypePTR        Type = 12
   199  	TypeHINFO      Type = 13
   200  	TypeMINFO      Type = 14
   201  	TypeMX         Type = 15
   202  	TypeTXT        Type = 16
   203  	TypeRP         Type = 17
   204  	TypeAFSDB      Type = 18
   205  	TypeX25        Type = 19
   206  	TypeISDN       Type = 20
   207  	TypeRT         Type = 21
   208  	TypeNSAPPTR    Type = 23
   209  	TypeSIG        Type = 24
   210  	TypeKEY        Type = 25
   211  	TypePX         Type = 26
   212  	TypeGPOS       Type = 27
   213  	TypeAAAA       Type = 28
   214  	TypeLOC        Type = 29
   215  	TypeNXT        Type = 30
   216  	TypeEID        Type = 31
   217  	TypeNIMLOC     Type = 32
   218  	TypeSRV        Type = 33
   219  	TypeATMA       Type = 34
   220  	TypeNAPTR      Type = 35
   221  	TypeKX         Type = 36
   222  	TypeCERT       Type = 37
   223  	TypeDNAME      Type = 39
   224  	TypeOPT        Type = 41 // EDNS
   225  	TypeAPL        Type = 42
   226  	TypeDS         Type = 43
   227  	TypeSSHFP      Type = 44
   228  	TypeRRSIG      Type = 46
   229  	TypeNSEC       Type = 47
   230  	TypeDNSKEY     Type = 48
   231  	TypeDHCID      Type = 49
   232  	TypeNSEC3      Type = 50
   233  	TypeNSEC3PARAM Type = 51
   234  	TypeTLSA       Type = 52
   235  	TypeSMIMEA     Type = 53
   236  	TypeHIP        Type = 55
   237  	TypeNINFO      Type = 56
   238  	TypeRKEY       Type = 57
   239  	TypeTALINK     Type = 58
   240  	TypeCDS        Type = 59
   241  	TypeCDNSKEY    Type = 60
   242  	TypeOPENPGPKEY Type = 61
   243  	TypeCSYNC      Type = 62
   244  	TypeZONEMD     Type = 63
   245  	TypeSVCB       Type = 64
   246  	TypeHTTPS      Type = 65
   247  	TypeSPF        Type = 99
   248  	TypeUINFO      Type = 100
   249  	TypeUID        Type = 101
   250  	TypeGID        Type = 102
   251  	TypeUNSPEC     Type = 103
   252  	TypeNID        Type = 104
   253  	TypeL32        Type = 105
   254  	TypeL64        Type = 106
   255  	TypeLP         Type = 107
   256  	TypeEUI48      Type = 108
   257  	TypeEUI64      Type = 109
   258  	TypeURI        Type = 256
   259  	TypeCAA        Type = 257
   260  	TypeAVC        Type = 258
   261  	TypeTKEY       Type = 249
   262  	TypeTSIG       Type = 250
   263  	TypeIXFR       Type = 251
   264  	TypeAXFR       Type = 252
   265  	TypeMAILB      Type = 253
   266  	TypeMAILA      Type = 254
   267  	TypeANY        Type = 255
   268  	TypeTA         Type = 32768
   269  	TypeDLV        Type = 32769
   270  	TypeReserved   Type = 65535
   271  )
   272  
   273  func (t Type) String() string {
   274  	switch t {
   275  	case TypeNone:
   276  		return "None"
   277  	case TypeA:
   278  		return "A"
   279  	case TypeNS:
   280  		return "NS"
   281  	case TypeMD:
   282  		return "MD"
   283  	case TypeMF:
   284  		return "MF"
   285  	case TypeCNAME:
   286  		return "CNAME"
   287  	case TypeSOA:
   288  		return "SOA"
   289  	case TypeMB:
   290  		return "MB"
   291  	case TypeMG:
   292  		return "MG"
   293  	case TypeMR:
   294  		return "MR"
   295  	case TypeNULL:
   296  		return "NULL"
   297  	case TypePTR:
   298  		return "PTR"
   299  	case TypeHINFO:
   300  		return "HINFO"
   301  	case TypeMINFO:
   302  		return "MINFO"
   303  	case TypeMX:
   304  		return "MX"
   305  	case TypeTXT:
   306  		return "TXT"
   307  	case TypeRP:
   308  		return "RP"
   309  	case TypeAFSDB:
   310  		return "AFSDB"
   311  	case TypeX25:
   312  		return "X25"
   313  	case TypeISDN:
   314  		return "ISDN"
   315  	case TypeRT:
   316  		return "RT"
   317  	case TypeNSAPPTR:
   318  		return "NSAPPTR"
   319  	case TypeSIG:
   320  		return "SIG"
   321  	case TypeKEY:
   322  		return "KEY"
   323  	case TypePX:
   324  		return "PX"
   325  	case TypeGPOS:
   326  		return "GPOS"
   327  	case TypeAAAA:
   328  		return "AAAA"
   329  	case TypeLOC:
   330  		return "LOC"
   331  	case TypeNXT:
   332  		return "NXT"
   333  	case TypeEID:
   334  		return "EID"
   335  	case TypeNIMLOC:
   336  		return "NIMLOC"
   337  	case TypeSRV:
   338  		return "SRV"
   339  	case TypeATMA:
   340  		return "ATMA"
   341  	case TypeNAPTR:
   342  		return "NAPTR"
   343  	case TypeKX:
   344  		return "KX"
   345  	case TypeCERT:
   346  		return "CERT"
   347  	case TypeDNAME:
   348  		return "DNAME"
   349  	case TypeOPT:
   350  		return "OPT"
   351  	case TypeAPL:
   352  		return "APL"
   353  	case TypeDS:
   354  		return "DS"
   355  	case TypeSSHFP:
   356  		return "SSHFP"
   357  	case TypeRRSIG:
   358  		return "RRSIG"
   359  	case TypeNSEC:
   360  		return "NSEC"
   361  	case TypeDNSKEY:
   362  		return "DNSKEY"
   363  	case TypeDHCID:
   364  		return "DHCID"
   365  	case TypeNSEC3:
   366  		return "NSEC3"
   367  	case TypeNSEC3PARAM:
   368  		return "NSEC3PARAM"
   369  	case TypeTLSA:
   370  		return "TLSA"
   371  	case TypeSMIMEA:
   372  		return "SMIMEA"
   373  	case TypeHIP:
   374  		return "HIP"
   375  	case TypeNINFO:
   376  		return "NINFO"
   377  	case TypeRKEY:
   378  		return "RKEY"
   379  	case TypeTALINK:
   380  		return "TALINK"
   381  	case TypeCDS:
   382  		return "CDS"
   383  	case TypeCDNSKEY:
   384  		return "CDNSKEY"
   385  	case TypeOPENPGPKEY:
   386  		return "OPENPGPKEY"
   387  	case TypeCSYNC:
   388  		return "CSYNC"
   389  	case TypeZONEMD:
   390  		return "ZONEMD"
   391  	case TypeSVCB:
   392  		return "SVCB"
   393  	case TypeHTTPS:
   394  		return "HTTPS"
   395  	case TypeSPF:
   396  		return "SPF"
   397  	case TypeUINFO:
   398  		return "UINFO"
   399  	case TypeUID:
   400  		return "UID"
   401  	case TypeGID:
   402  		return "GID"
   403  	case TypeUNSPEC:
   404  		return "UNSPEC"
   405  	case TypeNID:
   406  		return "NID"
   407  	case TypeL32:
   408  		return "L32"
   409  	case TypeL64:
   410  		return "L64"
   411  	case TypeLP:
   412  		return "LP"
   413  	case TypeEUI48:
   414  		return "EUI48"
   415  	case TypeEUI64:
   416  		return "EUI64"
   417  	case TypeURI:
   418  		return "URI"
   419  	case TypeCAA:
   420  		return "CAA"
   421  	case TypeAVC:
   422  		return "AVC"
   423  	case TypeTKEY:
   424  		return "TKEY"
   425  	case TypeTSIG:
   426  		return "TSIG"
   427  	case TypeIXFR:
   428  		return "IXFR"
   429  	case TypeAXFR:
   430  		return "AXFR"
   431  	case TypeMAILB:
   432  		return "MAILB"
   433  	case TypeMAILA:
   434  		return "MAILA"
   435  	case TypeANY:
   436  		return "ANY"
   437  	case TypeTA:
   438  		return "TA"
   439  	case TypeDLV:
   440  		return "DLV"
   441  	case TypeReserved:
   442  		return "Reserved"
   443  	}
   444  	return ""
   445  }
   446  
   447  // ParseType converts a question type string into a question type value.
   448  func ParseType(s string) (t Type) {
   449  	switch s {
   450  	case "A", "a":
   451  		t = TypeA
   452  	case "NS", "ns":
   453  		t = TypeNS
   454  	case "MD", "md":
   455  		t = TypeMD
   456  	case "MF", "mf":
   457  		t = TypeMF
   458  	case "CNAME", "cname":
   459  		t = TypeCNAME
   460  	case "SOA", "soa":
   461  		t = TypeSOA
   462  	case "MB", "mb":
   463  		t = TypeMB
   464  	case "MG", "mg":
   465  		t = TypeMG
   466  	case "MR", "mr":
   467  		t = TypeMR
   468  	case "NULL", "null":
   469  		t = TypeNULL
   470  	case "PTR", "ptr":
   471  		t = TypePTR
   472  	case "HINFO", "hinfo":
   473  		t = TypeHINFO
   474  	case "MINFO", "minfo":
   475  		t = TypeMINFO
   476  	case "MX", "mx":
   477  		t = TypeMX
   478  	case "TXT", "txt":
   479  		t = TypeTXT
   480  	case "RP", "rp":
   481  		t = TypeRP
   482  	case "AFSDB", "afsdb":
   483  		t = TypeAFSDB
   484  	case "X25", "x25":
   485  		t = TypeX25
   486  	case "ISDN", "isdn":
   487  		t = TypeISDN
   488  	case "RT", "rt":
   489  		t = TypeRT
   490  	case "NSAPPTR", "nsapptr":
   491  		t = TypeNSAPPTR
   492  	case "SIG", "sig":
   493  		t = TypeSIG
   494  	case "KEY", "key":
   495  		t = TypeKEY
   496  	case "PX", "px":
   497  		t = TypePX
   498  	case "GPOS", "gpos":
   499  		t = TypeGPOS
   500  	case "AAAA", "aaaa":
   501  		t = TypeAAAA
   502  	case "LOC", "loc":
   503  		t = TypeLOC
   504  	case "NXT", "nxt":
   505  		t = TypeNXT
   506  	case "EID", "eid":
   507  		t = TypeEID
   508  	case "NIMLOC", "nimloc":
   509  		t = TypeNIMLOC
   510  	case "SRV", "srv":
   511  		t = TypeSRV
   512  	case "ATMA", "atma":
   513  		t = TypeATMA
   514  	case "NAPTR", "naptr":
   515  		t = TypeNAPTR
   516  	case "KX", "kx":
   517  		t = TypeKX
   518  	case "CERT", "cert":
   519  		t = TypeCERT
   520  	case "DNAME", "dname":
   521  		t = TypeDNAME
   522  	case "OPT", "opt":
   523  		t = TypeOPT
   524  	case "APL", "apl":
   525  		t = TypeAPL
   526  	case "DS", "ds":
   527  		t = TypeDS
   528  	case "SSHFP", "sshfp":
   529  		t = TypeSSHFP
   530  	case "RRSIG", "rrsig":
   531  		t = TypeRRSIG
   532  	case "NSEC", "nsec":
   533  		t = TypeNSEC
   534  	case "DNSKEY", "dnskey":
   535  		t = TypeDNSKEY
   536  	case "DHCID", "dhcid":
   537  		t = TypeDHCID
   538  	case "NSEC3", "nsec3":
   539  		t = TypeNSEC3
   540  	case "NSEC3PARAM", "nsec3param":
   541  		t = TypeNSEC3PARAM
   542  	case "TLSA", "tlsa":
   543  		t = TypeTLSA
   544  	case "SMIMEA", "smimea":
   545  		t = TypeSMIMEA
   546  	case "HIP", "hip":
   547  		t = TypeHIP
   548  	case "NINFO", "ninfo":
   549  		t = TypeNINFO
   550  	case "RKEY", "rkey":
   551  		t = TypeRKEY
   552  	case "TALINK", "talink":
   553  		t = TypeTALINK
   554  	case "CDS", "cds":
   555  		t = TypeCDS
   556  	case "CDNSKEY", "cdnskey":
   557  		t = TypeCDNSKEY
   558  	case "OPENPGPKEY", "openpgpkey":
   559  		t = TypeOPENPGPKEY
   560  	case "CSYNC", "csync":
   561  		t = TypeCSYNC
   562  	case "ZONEMD", "zonemd":
   563  		t = TypeZONEMD
   564  	case "SVCB", "svcb":
   565  		t = TypeSVCB
   566  	case "HTTPS", "https":
   567  		t = TypeHTTPS
   568  	case "SPF", "spf":
   569  		t = TypeSPF
   570  	case "UINFO", "uinfo":
   571  		t = TypeUINFO
   572  	case "UID", "uid":
   573  		t = TypeUID
   574  	case "GID", "gid":
   575  		t = TypeGID
   576  	case "UNSPEC", "unspec":
   577  		t = TypeUNSPEC
   578  	case "NID", "nid":
   579  		t = TypeNID
   580  	case "L32", "l32":
   581  		t = TypeL32
   582  	case "L64", "l64":
   583  		t = TypeL64
   584  	case "LP", "lp":
   585  		t = TypeLP
   586  	case "EUI48", "eui48":
   587  		t = TypeEUI48
   588  	case "EUI64", "eui64":
   589  		t = TypeEUI64
   590  	case "URI", "uri":
   591  		t = TypeURI
   592  	case "CAA", "caa":
   593  		t = TypeCAA
   594  	case "AVC", "avc":
   595  		t = TypeAVC
   596  	case "TKEY", "tkey":
   597  		t = TypeTKEY
   598  	case "TSIG", "tsig":
   599  		t = TypeTSIG
   600  	case "IXFR", "ixfr":
   601  		t = TypeIXFR
   602  	case "AXFR", "axfr":
   603  		t = TypeAXFR
   604  	case "MAILB", "mailb":
   605  		t = TypeMAILB
   606  	case "MAILA", "maila":
   607  		t = TypeMAILA
   608  	case "ANY", "any":
   609  		t = TypeANY
   610  	case "TA", "ta":
   611  		t = TypeTA
   612  	case "DLV", "dlv":
   613  		t = TypeDLV
   614  	case "Reserved", "reserved":
   615  		t = TypeReserved
   616  	}
   617  	return
   618  }