raw.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Package raw enables reading and writing data at the device driver level for
  2. // a network interface.
  3. package raw
  4. import (
  5. "errors"
  6. "net"
  7. "time"
  8. "golang.org/x/net/bpf"
  9. )
  10. var (
  11. // ErrNotImplemented is returned when certain functionality is not yet
  12. // implemented for the host operating system.
  13. ErrNotImplemented = errors.New("raw: not implemented")
  14. )
  15. var _ net.Addr = &Addr{}
  16. // Addr is a network address which can be used to contact other machines, using
  17. // their hardware addresses.
  18. type Addr struct {
  19. HardwareAddr net.HardwareAddr
  20. }
  21. // Network returns the address's network name, "raw".
  22. func (a *Addr) Network() string {
  23. return "raw"
  24. }
  25. // String returns the address's hardware address.
  26. func (a *Addr) String() string {
  27. return a.HardwareAddr.String()
  28. }
  29. var _ net.PacketConn = &Conn{}
  30. // Conn is an implementation of the net.PacketConn interface which can send
  31. // and receive data at the network interface device driver level.
  32. type Conn struct {
  33. // packetConn is the operating system-specific implementation of
  34. // a raw connection.
  35. p *packetConn
  36. }
  37. // ReadFrom implements the net.PacketConn ReadFrom method.
  38. func (c *Conn) ReadFrom(b []byte) (int, net.Addr, error) {
  39. return c.p.ReadFrom(b)
  40. }
  41. // WriteTo implements the net.PacketConn WriteTo method.
  42. func (c *Conn) WriteTo(b []byte, addr net.Addr) (int, error) {
  43. return c.p.WriteTo(b, addr)
  44. }
  45. // Close closes the connection.
  46. func (c *Conn) Close() error {
  47. return c.p.Close()
  48. }
  49. // LocalAddr returns the local network address.
  50. func (c *Conn) LocalAddr() net.Addr {
  51. return c.p.LocalAddr()
  52. }
  53. // SetDeadline implements the net.PacketConn SetDeadline method.
  54. func (c *Conn) SetDeadline(t time.Time) error {
  55. return c.p.SetDeadline(t)
  56. }
  57. // SetReadDeadline implements the net.PacketConn SetReadDeadline method.
  58. func (c *Conn) SetReadDeadline(t time.Time) error {
  59. return c.p.SetReadDeadline(t)
  60. }
  61. // SetWriteDeadline implements the net.PacketConn SetWriteDeadline method.
  62. func (c *Conn) SetWriteDeadline(t time.Time) error {
  63. return c.p.SetWriteDeadline(t)
  64. }
  65. var _ bpf.Setter = &Conn{}
  66. // SetBPF attaches an assembled BPF program to the connection.
  67. func (c *Conn) SetBPF(filter []bpf.RawInstruction) error {
  68. return c.p.SetBPF(filter)
  69. }
  70. // SetPromiscuous enables or disables promiscuous mode on the interface, allowing it
  71. // to receive traffic that is not addressed to the interface.
  72. func (c *Conn) SetPromiscuous(b bool) error {
  73. return c.p.SetPromiscuous(b)
  74. }
  75. // Stats contains statistics about a Conn.
  76. type Stats struct {
  77. // The total number of packets received.
  78. Packets uint64
  79. // The number of packets dropped.
  80. Drops uint64
  81. }
  82. // Stats retrieves statistics from the Conn.
  83. //
  84. // Only supported on Linux at this time.
  85. func (c *Conn) Stats() (*Stats, error) {
  86. return c.p.Stats()
  87. }
  88. // ListenPacket creates a net.PacketConn which can be used to send and receive
  89. // data at the network interface device driver level.
  90. //
  91. // ifi specifies the network interface which will be used to send and receive
  92. // data.
  93. //
  94. // proto specifies the protocol (usually the EtherType) which should be
  95. // captured and transmitted. proto, if needed, is automatically converted to
  96. // network byte order (big endian), akin to the htons() function in C.
  97. //
  98. // cfg specifies optional configuration which may be operating system-specific.
  99. // A nil Config is equivalent to the default configuration: send and receive
  100. // data at the network interface device driver level (usually raw Ethernet frames).
  101. func ListenPacket(ifi *net.Interface, proto uint16, cfg *Config) (*Conn, error) {
  102. // A nil config is an empty Config.
  103. if cfg == nil {
  104. cfg = &Config{}
  105. }
  106. p, err := listenPacket(ifi, proto, *cfg)
  107. if err != nil {
  108. return nil, err
  109. }
  110. return &Conn{
  111. p: p,
  112. }, nil
  113. }
  114. // A Config can be used to specify additional options for a Conn.
  115. type Config struct {
  116. // Linux only: call socket(7) with SOCK_DGRAM instead of SOCK_RAW.
  117. // Has no effect on other operating systems.
  118. LinuxSockDGRAM bool
  119. // Linux only: do not accumulate packet socket statistic counters. Packet
  120. // socket statistics are reset on each call to retrieve them via getsockopt,
  121. // but this package's default behavior is to continue accumulating the
  122. // statistics internally per Conn. To use the Linux default behavior of
  123. // resetting statistics on each call to Stats, set this value to true.
  124. NoCumulativeStats bool
  125. // Linux only: initial filter to apply to the connection. This avoids
  126. // capturing random packets before SetBPF is called.
  127. Filter []bpf.RawInstruction
  128. // BSD only: configure the BPF direction flag to allow selection of inbound
  129. // only (0 - default) or bidirectional (1) packet processing.
  130. // Has no effect on other operating systems.
  131. BPFDirection int
  132. }