dnsjson

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2025 License: MIT Imports: 9 Imported by: 0

README

build coverage goreport Docs

dnsjson

JSON (un)marshalling for https://github.com/miekg/dns

This is not RFC 8427, rather it is a structured human-readable format.

package main

import (
	"encoding/json"
	"fmt"

	"github.com/linkdata/dnsjson"
	"github.com/miekg/dns"
)

func main() {
	msg := new(dns.Msg)
	msg.SetQuestion("example.com.", dns.TypeA)
	msg.Id = 1234
	jbytes, err := json.MarshalIndent((*dnsjson.Msg)(msg), "", " ")
	if err == nil {
		fmt.Println(string(jbytes))
		var msg2 dnsjson.Msg
		if err = json.Unmarshal(jbytes, &msg2); err == nil {
			fmt.Println(dns.Msg(msg2).Question[0].Name)
		}
	}
	if err != nil {
		fmt.Println(err)
	}

	// Output:
	// {
	//  "id": 1234,
	//  "msgHdr": {
	//   "opcode": "QUERY",
	//   "rd": true,
	//   "rcode": "NOERROR"
	//  },
	//  "question": [
	//   {
	//    "name": "example.com.",
	//    "qtype": "A",
	//    "qclass": "IN"
	//   }
	//  ]
	// }
	// example.com.
}

Documentation

Overview

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/linkdata/dnsjson"
	"github.com/miekg/dns"
)

func main() {
	msg := new(dns.Msg)
	msg.SetQuestion("example.com.", dns.TypeA)
	msg.Id = 1234
	jbytes, err := json.MarshalIndent((*dnsjson.Msg)(msg), "", " ")
	if err == nil {
		fmt.Println(string(jbytes))
		var msg2 dnsjson.Msg
		if err = json.Unmarshal(jbytes, &msg2); err == nil {
			fmt.Println(dns.Msg(msg2).Question[0].Name)
		}
	}
	if err != nil {
		fmt.Println(err)
	}

}
Output:

{
 "id": 1234,
 "msgHdr": {
  "opcode": "QUERY",
  "rd": true,
  "rcode": "NOERROR"
 },
 "question": [
  {
   "name": "example.com.",
   "qtype": "A",
   "qclass": "IN"
  }
 ]
}
example.com.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyInput          = errors.New("empty input")
	ErrInvalidJSON         = errors.New("invalid JSON")
	ErrInvalidMessage      = errors.New("invalid message")
	ErrQuestionQType       = errors.New("question qtype")
	ErrQuestionQClass      = errors.New("question qclass")
	ErrAnswerSection       = errors.New("answer")
	ErrNsSection           = errors.New("ns")
	ErrExtraSection        = errors.New("extra")
	ErrUnknownType         = errors.New("unknown type")
	ErrUnknownClass        = errors.New("unknown class")
	ErrInvalidStringSlice  = errors.New("invalid string slice")
	ErrEDNSOptionEntry     = errors.New("edns option entry type")
	ErrEDNSOption          = errors.New("edns option")
	ErrEDNSOptionsNotArray = errors.New("opt options must be array")
	ErrEDNSOptionNoCode    = errors.New("opt option missing code")
	ErrUint8SliceType      = errors.New("uint8 slice type")
	ErrUint8SliceElement   = errors.New("uint8 slice element")
	ErrUint8SliceRange     = errors.New("uint8 slice range")
	ErrNegativeValue       = errors.New("negative value")
	ErrInvalidNumber       = errors.New("invalid number")
	ErrInvalidNumberType   = errors.New("invalid number type")
)

Functions

This section is empty.

Types

type MessageJSON

type MessageJSON struct {
	ID       uint16         `json:"id"`
	MsgHdr   MsgHdrJSON     `json:"msgHdr"`
	Question []QuestionJSON `json:"question"`
	Answer   []RRJSON       `json:"answer,omitempty"`
	Ns       []RRJSON       `json:"ns,omitempty"`
	Extra    []RRJSON       `json:"extra,omitempty"`
}

MessageJSON is the top-level JSON shape for dns.Msg.

type Msg

type Msg dns.Msg

func (*Msg) MarshalJSON

func (m *Msg) MarshalJSON() (b []byte, err error)

func (*Msg) UnmarshalJSON

func (msg *Msg) UnmarshalJSON(data []byte) (err error)

type MsgHdrJSON added in v1.0.0

type MsgHdrJSON struct {
	QR     bool   `json:"qr,omitempty"`
	Opcode string `json:"opcode"`
	AA     bool   `json:"aa,omitempty"`
	TC     bool   `json:"tc,omitempty"`
	RD     bool   `json:"rd,omitempty"`
	RA     bool   `json:"ra,omitempty"`
	Z      bool   `json:"z,omitempty"`
	AD     bool   `json:"ad,omitempty"`
	CD     bool   `json:"cd,omitempty"`
	Rcode  string `json:"rcode"`
}

type QuestionJSON added in v1.0.0

type QuestionJSON struct {
	Name   string `json:"name"`
	Qtype  string `json:"qtype"`
	Qclass string `json:"qclass"`
}

type RRJSON

type RRJSON struct {
	Name  string         `json:"name"`
	Type  string         `json:"type"`
	Class string         `json:"class"`
	TTL   uint32         `json:"ttl"`
	Data  map[string]any `json:"data"`
}

RRJSON contains common RR header fields plus a per-type data map.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL