listen_posix.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2014 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. // +build darwin dragonfly freebsd linux netbsd openbsd solaris windows
  5. package icmp
  6. import (
  7. "net"
  8. "os"
  9. "runtime"
  10. "syscall"
  11. "golang.org/x/net/internal/iana"
  12. "golang.org/x/net/ipv4"
  13. "golang.org/x/net/ipv6"
  14. )
  15. const sysIP_STRIPHDR = 0x17 // for now only darwin supports this option
  16. // ListenPacket listens for incoming ICMP packets addressed to
  17. // address. See net.Dial for the syntax of address.
  18. //
  19. // For non-privileged datagram-oriented ICMP endpoints, network must
  20. // be "udp4" or "udp6". The endpoint allows to read, write a few
  21. // limited ICMP messages such as echo request and echo reply.
  22. // Currently only Darwin and Linux support this.
  23. //
  24. // Examples:
  25. // ListenPacket("udp4", "192.168.0.1")
  26. // ListenPacket("udp4", "0.0.0.0")
  27. // ListenPacket("udp6", "fe80::1%en0")
  28. // ListenPacket("udp6", "::")
  29. //
  30. // For privileged raw ICMP endpoints, network must be "ip4" or "ip6"
  31. // followed by a colon and an ICMP protocol number or name.
  32. //
  33. // Examples:
  34. // ListenPacket("ip4:icmp", "192.168.0.1")
  35. // ListenPacket("ip4:1", "0.0.0.0")
  36. // ListenPacket("ip6:ipv6-icmp", "fe80::1%en0")
  37. // ListenPacket("ip6:58", "::")
  38. func ListenPacket(network, address string) (*PacketConn, error) {
  39. var family, proto int
  40. switch network {
  41. case "udp4":
  42. family, proto = syscall.AF_INET, iana.ProtocolICMP
  43. case "udp6":
  44. family, proto = syscall.AF_INET6, iana.ProtocolIPv6ICMP
  45. default:
  46. i := last(network, ':')
  47. switch network[:i] {
  48. case "ip4":
  49. proto = iana.ProtocolICMP
  50. case "ip6":
  51. proto = iana.ProtocolIPv6ICMP
  52. }
  53. }
  54. var cerr error
  55. var c net.PacketConn
  56. switch family {
  57. case syscall.AF_INET, syscall.AF_INET6:
  58. s, err := syscall.Socket(family, syscall.SOCK_DGRAM, proto)
  59. if err != nil {
  60. return nil, os.NewSyscallError("socket", err)
  61. }
  62. defer syscall.Close(s)
  63. if runtime.GOOS == "darwin" && family == syscall.AF_INET {
  64. if err := syscall.SetsockoptInt(s, iana.ProtocolIP, sysIP_STRIPHDR, 1); err != nil {
  65. return nil, os.NewSyscallError("setsockopt", err)
  66. }
  67. }
  68. sa, err := sockaddr(family, address)
  69. if err != nil {
  70. return nil, err
  71. }
  72. if err := syscall.Bind(s, sa); err != nil {
  73. return nil, os.NewSyscallError("bind", err)
  74. }
  75. f := os.NewFile(uintptr(s), "datagram-oriented icmp")
  76. defer f.Close()
  77. c, cerr = net.FilePacketConn(f)
  78. default:
  79. c, cerr = net.ListenPacket(network, address)
  80. }
  81. if cerr != nil {
  82. return nil, cerr
  83. }
  84. switch proto {
  85. case iana.ProtocolICMP:
  86. return &PacketConn{c: c, p4: ipv4.NewPacketConn(c)}, nil
  87. case iana.ProtocolIPv6ICMP:
  88. return &PacketConn{c: c, p6: ipv6.NewPacketConn(c)}, nil
  89. default:
  90. return &PacketConn{c: c}, nil
  91. }
  92. }