Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

101 wiersze
2.7 KiB

  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. if runtime.GOOS == "darwin" && family == syscall.AF_INET {
  63. if err := syscall.SetsockoptInt(s, iana.ProtocolIP, sysIP_STRIPHDR, 1); err != nil {
  64. syscall.Close(s)
  65. return nil, os.NewSyscallError("setsockopt", err)
  66. }
  67. }
  68. sa, err := sockaddr(family, address)
  69. if err != nil {
  70. syscall.Close(s)
  71. return nil, err
  72. }
  73. if err := syscall.Bind(s, sa); err != nil {
  74. syscall.Close(s)
  75. return nil, os.NewSyscallError("bind", err)
  76. }
  77. f := os.NewFile(uintptr(s), "datagram-oriented icmp")
  78. c, cerr = net.FilePacketConn(f)
  79. f.Close()
  80. default:
  81. c, cerr = net.ListenPacket(network, address)
  82. }
  83. if cerr != nil {
  84. return nil, cerr
  85. }
  86. switch proto {
  87. case iana.ProtocolICMP:
  88. return &PacketConn{c: c, p4: ipv4.NewPacketConn(c)}, nil
  89. case iana.ProtocolIPv6ICMP:
  90. return &PacketConn{c: c, p6: ipv6.NewPacketConn(c)}, nil
  91. default:
  92. return &PacketConn{c: c}, nil
  93. }
  94. }