YamlFormat.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2013-2014 by Farsight Security, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package dnstap
  17. import (
  18. "bytes"
  19. "fmt"
  20. "net"
  21. "strconv"
  22. "strings"
  23. "time"
  24. "github.com/miekg/dns"
  25. )
  26. const yamlTimeFormat = "2006-01-02 15:04:05.999999999"
  27. func yamlConvertMessage(m *Message, s *bytes.Buffer) {
  28. s.WriteString(fmt.Sprint(" type: ", m.Type, "\n"))
  29. if m.QueryTimeSec != nil && m.QueryTimeNsec != nil {
  30. t := time.Unix(int64(*m.QueryTimeSec), int64(*m.QueryTimeNsec)).UTC()
  31. s.WriteString(fmt.Sprint(" query_time: !!timestamp ", t.Format(yamlTimeFormat), "\n"))
  32. }
  33. if m.ResponseTimeSec != nil && m.ResponseTimeNsec != nil {
  34. t := time.Unix(int64(*m.ResponseTimeSec), int64(*m.ResponseTimeNsec)).UTC()
  35. s.WriteString(fmt.Sprint(" response_time: !!timestamp ", t.Format(yamlTimeFormat), "\n"))
  36. }
  37. if m.SocketFamily != nil {
  38. s.WriteString(fmt.Sprint(" socket_family: ", m.SocketFamily, "\n"))
  39. }
  40. if m.SocketProtocol != nil {
  41. s.WriteString(fmt.Sprint(" socket_protocol: ", m.SocketProtocol, "\n"))
  42. }
  43. if m.QueryAddress != nil {
  44. s.WriteString(fmt.Sprint(" query_address: ", net.IP(m.QueryAddress), "\n"))
  45. }
  46. if m.ResponseAddress != nil {
  47. s.WriteString(fmt.Sprint(" response_address: ", net.IP(m.ResponseAddress), "\n"))
  48. }
  49. if m.QueryPort != nil {
  50. s.WriteString(fmt.Sprint(" query_port: ", *m.QueryPort, "\n"))
  51. }
  52. if m.ResponsePort != nil {
  53. s.WriteString(fmt.Sprint(" response_port: ", *m.ResponsePort, "\n"))
  54. }
  55. if m.QueryZone != nil {
  56. name, _, err := dns.UnpackDomainName(m.QueryZone, 0)
  57. if err != nil {
  58. fmt.Fprintf(s, " # query_zone: parse failed: %v\n", err)
  59. } else {
  60. s.WriteString(fmt.Sprint(" query_zone: ", strconv.Quote(name), "\n"))
  61. }
  62. }
  63. if m.QueryMessage != nil {
  64. msg := new(dns.Msg)
  65. err := msg.Unpack(m.QueryMessage)
  66. if err != nil {
  67. fmt.Fprintf(s, " # query_message: parse failed: %v\n", err)
  68. } else {
  69. s.WriteString(" query_message: |\n")
  70. s.WriteString(" " + strings.Replace(strings.TrimSpace(msg.String()), "\n", "\n ", -1) + "\n")
  71. }
  72. }
  73. if m.ResponseMessage != nil {
  74. msg := new(dns.Msg)
  75. err := msg.Unpack(m.ResponseMessage)
  76. if err != nil {
  77. fmt.Fprintf(s, " # response_message: parse failed: %v\n", err)
  78. } else {
  79. s.WriteString(" response_message: |\n")
  80. s.WriteString(" " + strings.Replace(strings.TrimSpace(msg.String()), "\n", "\n ", -1) + "\n")
  81. }
  82. }
  83. s.WriteString("---\n")
  84. }
  85. // YamlFormat renders a dnstap message in YAML format. Any encapsulated DNS
  86. // messages are rendered as strings in a format similar to 'dig' output.
  87. func YamlFormat(dt *Dnstap) (out []byte, ok bool) {
  88. var s bytes.Buffer
  89. s.WriteString(fmt.Sprint("type: ", dt.Type, "\n"))
  90. if dt.Identity != nil {
  91. s.WriteString(fmt.Sprint("identity: ", strconv.Quote(string(dt.Identity)), "\n"))
  92. }
  93. if dt.Version != nil {
  94. s.WriteString(fmt.Sprint("version: ", strconv.Quote(string(dt.Version)), "\n"))
  95. }
  96. if *dt.Type == Dnstap_MESSAGE {
  97. s.WriteString("message:\n")
  98. yamlConvertMessage(dt.Message, &s)
  99. }
  100. return s.Bytes(), true
  101. }