package dns

  1. Overview
  2. Docs

DNS packet manipulation using the Cstruct library. Something of a catch-all for the time being.

  • author Richard Mortier <mort\@cantab.net>
  • author Anil Madhavapeddy anil\@recoil.org>
  • author Haris Rotsos
type digest_alg =
  1. | SHA1
  2. | SHA256
val digest_alg_to_string : digest_alg -> string
val string_to_digest_alg : string -> digest_alg option
val digest_alg_to_int : digest_alg -> int
val int_to_digest_alg : int -> digest_alg option
type gateway_tc
type pubkey_alg
type ipseckey_alg
type gateway
type hash_alg
type fp_type

Represent a DNSSEC algorithm, with the usual conversion functions.

type dnssec_alg =
  1. | RSAMD5
  2. | DH
  3. | DSA
  4. | ECC
  5. | RSASHA1
  6. | RSANSEC3
  7. | RSASHA256
  8. | RSASHA512
  9. | INDIRECT
  10. | PRIVATEDNS
  11. | PRIVATEOID
val dnssec_alg_to_string : dnssec_alg -> string
val string_to_dnssec_alg : string -> dnssec_alg option
val int_to_dnssec_alg : int -> dnssec_alg option
val dnssec_alg_to_int : dnssec_alg -> int
type q_type =
  1. | Q_A
  2. | Q_NS
  3. | Q_MD
  4. | Q_MF
  5. | Q_CNAME
  6. | Q_SOA
  7. | Q_MB
  8. | Q_MG
  9. | Q_MR
  10. | Q_NULL
  11. | Q_WKS
  12. | Q_PTR
  13. | Q_HINFO
  14. | Q_MINFO
  15. | Q_MX
  16. | Q_TXT
  17. | Q_RP
  18. | Q_AFSDB
  19. | Q_X25
  20. | Q_ISDN
  21. | Q_RT
  22. | Q_NSAP
  23. | Q_NSAPPTR
  24. | Q_SIG
  25. | Q_KEY
  26. | Q_PX
  27. | Q_GPOS
  28. | Q_AAAA
  29. | Q_LOC
  30. | Q_NXT
  31. | Q_EID
  32. | Q_NIMLOC
  33. | Q_SRV
  34. | Q_ATMA
  35. | Q_NAPTR
  36. | Q_KM
  37. | Q_CERT
  38. | Q_A6
  39. | Q_DNAME
  40. | Q_SINK
  41. | Q_OPT
  42. | Q_APL
  43. | Q_DS
  44. | Q_SSHFP
  45. | Q_IPSECKEY
  46. | Q_RRSIG
  47. | Q_NSEC
  48. | Q_DNSKEY
  49. | Q_NSEC3
  50. | Q_NSEC3PARAM
  51. | Q_SPF
  52. | Q_UINFO
  53. | Q_UID
  54. | Q_GID
  55. | Q_UNSPEC
  56. | Q_AXFR
  57. | Q_MAILB
  58. | Q_MAILA
  59. | Q_ANY_TYP
  60. | Q_TA
  61. | Q_DLV
  62. | Q_UNKNOWN of int

Represent the rr type, with the usual conversion functions.

val q_type_to_int : q_type -> int
type rr_type =
  1. | RR_UNUSED
  2. | RR_A
  3. | RR_NS
  4. | RR_MD
  5. | RR_MF
  6. | RR_CNAME
  7. | RR_SOA
  8. | RR_MB
  9. | RR_MG
  10. | RR_MR
  11. | RR_NULL
  12. | RR_WKS
  13. | RR_PTR
  14. | RR_HINFO
  15. | RR_MINFO
  16. | RR_MX
  17. | RR_TXT
  18. | RR_RP
  19. | RR_AFSDB
  20. | RR_X25
  21. | RR_ISDN
  22. | RR_RT
  23. | RR_NSAP
  24. | RR_NSAPPTR
  25. | RR_SIG
  26. | RR_KEY
  27. | RR_PX
  28. | RR_GPOS
  29. | RR_AAAA
  30. | RR_LOC
  31. | RR_NXT
  32. | RR_EID
  33. | RR_NIMLOC
  34. | RR_SRV
  35. | RR_ATMA
  36. | RR_NAPTR
  37. | RR_KM
  38. | RR_CERT
  39. | RR_A6
  40. | RR_DNAME
  41. | RR_SINK
  42. | RR_OPT
  43. | RR_APL
  44. | RR_DS
  45. | RR_SSHFP
  46. | RR_IPSECKEY
  47. | RR_RRSIG
  48. | RR_NSEC
  49. | RR_DNSKEY
  50. | RR_NSEC3
  51. | RR_NSEC3PARAM
  52. | RR_SPF
  53. | RR_UINFO
  54. | RR_UID
  55. | RR_GID
  56. | RR_UNSPEC
val string_to_rr_type : string -> rr_type option
val rr_type_to_string : rr_type -> string
val int_to_rr_type : int -> rr_type option
val rr_type_to_int : rr_type -> int
type type_bit_map
type type_bit_maps

Represent RDATA elements; a variant type to avoid collision with the compact Trie representation from RR.

type rdata =
  1. | A of Ipaddr.V4.t
  2. | AAAA of Ipaddr.V6.t
  3. | AFSDB of Cstruct.uint16 * Name.t
  4. | CNAME of Name.t
  5. | DNSKEY of Cstruct.uint16 * dnssec_alg * string
  6. | DS of Cstruct.uint16 * dnssec_alg * digest_alg * string
  7. | HINFO of string * string
  8. | IPSECKEY of Cstruct.byte * gateway_tc * ipseckey_alg * gateway * string
  9. | ISDN of string * string option
  10. | MB of Name.t
  11. | MD of Name.t
  12. | MF of Name.t
  13. | MG of Name.t
  14. | MINFO of Name.t * Name.t
  15. | MR of Name.t
  16. | MX of Cstruct.uint16 * Name.t
  17. | NS of Name.t
  18. | NSEC of Name.t * type_bit_maps
  19. | NSEC3 of hash_alg * Cstruct.byte * Cstruct.uint16 * Cstruct.byte * string * Cstruct.byte * string * type_bit_maps
  20. | NSEC3PARAM of hash_alg * Cstruct.byte * Cstruct.uint16 * Cstruct.byte * string
  21. | PTR of Name.t
  22. | RP of Name.t * Name.t
  23. | RRSIG of rr_type * dnssec_alg * Cstruct.byte * int32 * int32 * int32 * Cstruct.uint16 * Name.t * string
  24. | SIG of dnssec_alg * int32 * int32 * Cstruct.uint16 * Name.t * string
  25. | RT of Cstruct.uint16 * Name.t
  26. | SOA of Name.t * Name.t * int32 * int32 * int32 * int32 * int32
  27. | SRV of Cstruct.uint16 * Cstruct.uint16 * Cstruct.uint16 * Name.t
  28. | SSHFP of pubkey_alg * fp_type * string
  29. | TXT of string list
  30. | UNKNOWN of int * string
  31. | WKS of Ipaddr.V4.t * Cstruct.byte * string
  32. | X25 of string
  33. | EDNS0 of int * int * bool * (int * string) list
val hex_of_string : string -> string
val rdata_to_string : rdata -> string
val rdata_to_rr_type : rdata -> rr_type
val marshal_rdata : int Dns.Name.Map.t -> ?compress:bool -> int -> Cstruct.t -> rdata -> rr_type * int Dns.Name.Map.t * int

Marshal the RR data into the DNS binary format. Raises Not_implemented if the RR type is known but the logic is not implemented in the library yet.

val compare_rdata : rdata -> rdata -> int
exception Not_implemented
val parse_rdata : (int, Name.label) Hashtbl.t -> int -> rr_type -> int -> int32 -> Cstruct.t -> rdata

Parse an RDATA element from a packet, given the set of already encountered names, a starting index, and the type of the RDATA. Raises Not_implemented if the RR type is not recognized.

type rr_class =
  1. | RR_IN
  2. | RR_CS
  3. | RR_CH
  4. | RR_HS
  5. | RR_ANY

The class of a rr, and usual conversion functions.

val rr_class_to_string : rr_class -> string
type rr = {
  1. name : Name.t;
  2. cls : rr_class;
  3. flush : bool;
  4. ttl : int32;
  5. rdata : rdata;
}

A resource record, with usual conversion and parsing functions.

val rr_to_string : rr -> string
val marshal_rr : ?compress:bool -> (int Dns.Name.Map.t * int * Cstruct.t) -> rr -> int Dns.Name.Map.t * int * Cstruct.t
val parse_rr : (int, Name.label) Hashtbl.t -> int -> Cstruct.t -> rr * (int * Cstruct.t)
val q_type_matches_rr_type : q_type -> rr_type -> bool

A predicate to test if a q_type applies to an rr_type.

val q_type_to_string : q_type -> string

A question type, with the usual conversion functions.

val string_to_q_type : string -> q_type option
type q_class =
  1. | Q_IN
  2. | Q_CS
  3. | Q_CH
  4. | Q_HS
  5. | Q_NONE
  6. | Q_ANY_CLS

A question class, with the usual conversion functions.

val q_class_to_string : q_class -> string
val string_to_q_class : string -> q_class option
type q_unicast =
  1. | Q_Normal
  2. | Q_mDNS_Unicast

For normal DNS, only Q_Normal is valid. For mDNS, Q_Normal (called QM in RFC 6762) requests a multicast response or Q_mDNS_Unicast (called QU in RFC 6762) requests a unicast response.

val q_unicast_to_string : q_unicast -> string
type question = {
  1. q_name : Name.t;
  2. q_type : q_type;
  3. q_class : q_class;
  4. q_unicast : q_unicast;
}

A question, with the usual conversion functions. Use make_question if you want to take advantage of default values.

A convenience function to create a question record with default values for q_class (Q_IN) and q_unicast (Q_Normal).

val make_question : ?q_class:q_class -> ?q_unicast:q_unicast -> q_type -> Name.t -> question

A convenience function to create a question record with default values for q_class (Q_IN) and q_unicast (Q_Normal).

val question_to_string : question -> string
val parse_question : (int, Name.label) Hashtbl.t -> int -> Cstruct.t -> question * (int * Cstruct.t)
type qr =
  1. | Query
  2. | Response

The qr field from the DNS header detail.

type opcode =
  1. | Standard
  2. | Inverse
  3. | Status
  4. | Notify
  5. | Update
  6. | Reserved of int

A DNS opcode, with the usual conversion functions.

val opcode_to_string : opcode -> string
type rcode =
  1. | NoError
  2. | FormErr
  3. | ServFail
  4. | NXDomain
  5. | NotImp
  6. | Refused
  7. | YXDomain
  8. | YXRRSet
  9. | NXRRSet
  10. | NotAuth
  11. | NotZone
  12. | BadVers
  13. | BadKey
  14. | BadTime
  15. | BadMode
  16. | BadName
  17. | BadAlg

A DNS response code, with the usual conversion functions.

val rcode_to_string : rcode -> string
type detail = {
  1. qr : qr;
  2. opcode : opcode;
  3. aa : bool;
  4. tc : bool;
  5. rd : bool;
  6. ra : bool;
  7. rcode : rcode;
}

The detail field from the DNS header, with the usual conversion functions.

type t = {
  1. id : int;
  2. detail : detail;
  3. questions : question list;
  4. answers : rr list;
  5. authorities : rr list;
  6. additionals : rr list;
}

And finally, the DNS packet itself, with conversion functions.

val to_string : t -> string
val parse : Cstruct.t -> t
val marshal : ?alloc:(unit -> Cstruct.t) -> t -> Cstruct.t

The marshalling entry point, given a Dns structure.

  • returns

    the marshalled packet

OCaml

Innovation. Community. Security.