message.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package icmp provides basic functions for the manipulation of ICMP
  5. // message.
  6. package icmp
  7. import (
  8. "errors"
  9. "net"
  10. "golang.org/x/net/internal/iana"
  11. "golang.org/x/net/ipv4"
  12. "golang.org/x/net/ipv6"
  13. )
  14. // A Type represents an ICMP message type.
  15. type Type interface {
  16. Protocol() int
  17. }
  18. // A Message represents an ICMP message.
  19. type Message struct {
  20. Type Type // type, either ipv4.ICMPType or ipv6.ICMPType
  21. Code int // code
  22. Checksum int // checksum
  23. Body MessageBody // body
  24. }
  25. // Marshal returns the binary enconding of the ICMP message m.
  26. //
  27. // For ICMP for IPv4 message, the returned message always contains the
  28. // calculated checksum field.
  29. //
  30. // For ICMP for IPv6 message, the returned message contains the
  31. // calculated checksum field when psh is not nil, otherwise the kernel
  32. // will compute the checksum field during the message transmission.
  33. // When psh is not nil, it must be the pseudo header for IPv6.
  34. func (m *Message) Marshal(psh []byte) ([]byte, error) {
  35. var mtype int
  36. switch typ := m.Type.(type) {
  37. case ipv4.ICMPType:
  38. mtype = int(typ)
  39. case ipv6.ICMPType:
  40. mtype = int(typ)
  41. default:
  42. return nil, errors.New("invalid argument")
  43. }
  44. b := []byte{byte(mtype), byte(m.Code), 0, 0}
  45. if m.Type.Protocol() == iana.ProtocolIPv6ICMP && psh != nil {
  46. b = append(psh, b...)
  47. }
  48. if m.Body != nil && m.Body.Len() != 0 {
  49. mb, err := m.Body.Marshal()
  50. if err != nil {
  51. return nil, err
  52. }
  53. b = append(b, mb...)
  54. }
  55. if m.Type.Protocol() == iana.ProtocolIPv6ICMP {
  56. if psh == nil { // cannot calculate checksum here
  57. return b, nil
  58. }
  59. off, l := 2*net.IPv6len, len(b)-len(psh)
  60. b[off], b[off+1], b[off+2], b[off+3] = byte(l>>24), byte(l>>16), byte(l>>8), byte(l)
  61. }
  62. csumcv := len(b) - 1 // checksum coverage
  63. s := uint32(0)
  64. for i := 0; i < csumcv; i += 2 {
  65. s += uint32(b[i+1])<<8 | uint32(b[i])
  66. }
  67. if csumcv&1 == 0 {
  68. s += uint32(b[csumcv])
  69. }
  70. s = s>>16 + s&0xffff
  71. s = s + s>>16
  72. // Place checksum back in header; using ^= avoids the
  73. // assumption the checksum bytes are zero.
  74. b[len(psh)+2] ^= byte(^s)
  75. b[len(psh)+3] ^= byte(^s >> 8)
  76. return b[len(psh):], nil
  77. }
  78. // ParseMessage parses b as an ICMP message. Proto must be
  79. // iana.ProtocolICMP or iana.ProtocolIPv6ICMP.
  80. func ParseMessage(proto int, b []byte) (*Message, error) {
  81. if len(b) < 4 {
  82. return nil, errors.New("message too short")
  83. }
  84. var err error
  85. switch proto {
  86. case iana.ProtocolICMP:
  87. m := &Message{Type: ipv4.ICMPType(b[0]), Code: int(b[1]), Checksum: int(b[2])<<8 | int(b[3])}
  88. switch m.Type {
  89. case ipv4.ICMPTypeEcho, ipv4.ICMPTypeEchoReply:
  90. m.Body, err = parseEcho(b[4:])
  91. if err != nil {
  92. return nil, err
  93. }
  94. default:
  95. m.Body = &DefaultMessageBody{Data: b[4:]}
  96. }
  97. return m, nil
  98. case iana.ProtocolIPv6ICMP:
  99. m := &Message{Type: ipv6.ICMPType(b[0]), Code: int(b[1]), Checksum: int(b[2])<<8 | int(b[3])}
  100. switch m.Type {
  101. case ipv6.ICMPTypeEchoRequest, ipv6.ICMPTypeEchoReply:
  102. m.Body, err = parseEcho(b[4:])
  103. if err != nil {
  104. return nil, err
  105. }
  106. default:
  107. m.Body = &DefaultMessageBody{Data: b[4:]}
  108. }
  109. return m, nil
  110. default:
  111. return nil, errors.New("unknown protocol")
  112. }
  113. }