github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/net/internal/socktest/switch.go (about)

     1  // Copyright 2015 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  
     5  // Package socktest provides utilities for socket testing.
     6  package socktest
     7  
     8  import (
     9  	"github.com/shogo82148/std/sync"
    10  )
    11  
    12  // A Switch represents a callpath point switch for socket system
    13  // calls.
    14  type Switch struct {
    15  	once sync.Once
    16  
    17  	fmu   sync.RWMutex
    18  	fltab map[FilterType]Filter
    19  
    20  	smu   sync.RWMutex
    21  	sotab Sockets
    22  	stats stats
    23  }
    24  
    25  // Stats returns a list of per-cookie socket statistics.
    26  func (sw *Switch) Stats() []Stat
    27  
    28  // Sockets returns mappings of socket descriptor to socket status.
    29  func (sw *Switch) Sockets() Sockets
    30  
    31  // A Cookie represents a 3-tuple of a socket; address family, socket
    32  // type and protocol number.
    33  type Cookie uint64
    34  
    35  // Family returns an address family.
    36  func (c Cookie) Family() int
    37  
    38  // Type returns a socket type.
    39  func (c Cookie) Type() int
    40  
    41  // Protocol returns a protocol number.
    42  func (c Cookie) Protocol() int
    43  
    44  // A Status represents the status of a socket.
    45  type Status struct {
    46  	Cookie    Cookie
    47  	Err       error
    48  	SocketErr error
    49  }
    50  
    51  func (so Status) String() string
    52  
    53  // A Stat represents a per-cookie socket statistics.
    54  type Stat struct {
    55  	Family   int
    56  	Type     int
    57  	Protocol int
    58  
    59  	Opened    uint64
    60  	Connected uint64
    61  	Listened  uint64
    62  	Accepted  uint64
    63  	Closed    uint64
    64  
    65  	OpenFailed    uint64
    66  	ConnectFailed uint64
    67  	ListenFailed  uint64
    68  	AcceptFailed  uint64
    69  	CloseFailed   uint64
    70  }
    71  
    72  func (st Stat) String() string
    73  
    74  // A FilterType represents a filter type.
    75  type FilterType int
    76  
    77  const (
    78  	FilterSocket FilterType = iota
    79  	FilterConnect
    80  	FilterListen
    81  	FilterAccept
    82  	FilterGetsockoptInt
    83  	FilterClose
    84  )
    85  
    86  // A Filter represents a socket system call filter.
    87  //
    88  // It will only be executed before a system call for a socket that has
    89  // an entry in internal table.
    90  // If the filter returns a non-nil error, the execution of system call
    91  // will be canceled and the system call function returns the non-nil
    92  // error.
    93  // It can return a non-nil [AfterFilter] for filtering after the
    94  // execution of the system call.
    95  type Filter func(*Status) (AfterFilter, error)
    96  
    97  // An AfterFilter represents a socket system call filter after an
    98  // execution of a system call.
    99  //
   100  // It will only be executed after a system call for a socket that has
   101  // an entry in internal table.
   102  // If the filter returns a non-nil error, the system call function
   103  // returns the non-nil error.
   104  type AfterFilter func(*Status) error
   105  
   106  // Set deploys the socket system call filter f for the filter type t.
   107  func (sw *Switch) Set(t FilterType, f Filter)