// Copyright 2015 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.
// Package socktest provides utilities for socket testing.
package socktestimport ()// A Switch represents a callpath point switch for socket system// calls.typeSwitchstruct { once sync.Once fmu sync.RWMutex fltab map[FilterType]Filter smu sync.RWMutex sotab Sockets stats stats}func ( *Switch) () { .fltab = make(map[FilterType]Filter) .sotab = make(Sockets) .stats = make(stats)}// Stats returns a list of per-cookie socket statistics.func ( *Switch) () []Stat {var []Stat .smu.RLock()for , := range .stats { := * = append(, ) } .smu.RUnlock()return}// Sockets returns mappings of socket descriptor to socket status.func ( *Switch) () Sockets { .smu.RLock() := make(Sockets, len(.sotab))for , := range .sotab { [] = } .smu.RUnlock()return}// A Cookie represents a 3-tuple of a socket; address family, socket// type and protocol number.typeCookieuint64// Family returns an address family.func ( Cookie) () int { returnint( >> 48) }// Type returns a socket type.func ( Cookie) () int { returnint( << 16 >> 32) }// Protocol returns a protocol number.func ( Cookie) () int { returnint( & 0xff) }func cookie(, , int) Cookie {returnCookie()<<48 | Cookie()&0xffffffff<<16 | Cookie()&0xff}// A Status represents the status of a socket.typeStatusstruct { Cookie Cookie Err error// error status of socket system call SocketErr error// error status of socket by SO_ERROR}func ( Status) () string {returnfmt.Sprintf("(%s, %s, %s): syscallerr=%v socketerr=%v", familyString(.Cookie.Family()), typeString(.Cookie.Type()), protocolString(.Cookie.Protocol()), .Err, .SocketErr)}// A Stat represents a per-cookie socket statistics.typeStatstruct { Family int// address family Type int// socket type Protocol int// protocol number Opened uint64// number of sockets opened Connected uint64// number of sockets connected Listened uint64// number of sockets listened Accepted uint64// number of sockets accepted Closed uint64// number of sockets closed OpenFailed uint64// number of sockets open failed ConnectFailed uint64// number of sockets connect failed ListenFailed uint64// number of sockets listen failed AcceptFailed uint64// number of sockets accept failed CloseFailed uint64// number of sockets close failed}func ( Stat) () string {returnfmt.Sprintf("(%s, %s, %s): opened=%d connected=%d listened=%d accepted=%d closed=%d openfailed=%d connectfailed=%d listenfailed=%d acceptfailed=%d closefailed=%d", familyString(.Family), typeString(.Type), protocolString(.Protocol), .Opened, .Connected, .Listened, .Accepted, .Closed, .OpenFailed, .ConnectFailed, .ListenFailed, .AcceptFailed, .CloseFailed)}type stats map[Cookie]*Statfunc ( stats) ( Cookie) *Stat { , := []if ! { = &Stat{Family: .Family(), Type: .Type(), Protocol: .Protocol()} [] = }return}// A FilterType represents a filter type.typeFilterTypeintconst (FilterSocketFilterType = iota// for SocketFilterConnect// for Connect or ConnectExFilterListen// for ListenFilterAccept// for Accept, Accept4 or AcceptExFilterGetsockoptInt// for GetsockoptIntFilterClose// for Close or Closesocket)// A Filter represents a socket system call filter.//// It will only be executed before a system call for a socket that has// an entry in internal table.// If the filter returns a non-nil error, the execution of system call// will be canceled and the system call function returns the non-nil// error.// It can return a non-nil [AfterFilter] for filtering after the// execution of the system call.typeFilterfunc(*Status) (AfterFilter, error)func ( Filter) ( *Status) (AfterFilter, error) {if == nil {returnnil, nil }return ()}// An AfterFilter represents a socket system call filter after an// execution of a system call.//// It will only be executed after a system call for a socket that has// an entry in internal table.// If the filter returns a non-nil error, the system call function// returns the non-nil error.typeAfterFilterfunc(*Status) errorfunc ( AfterFilter) ( *Status) error {if == nil {returnnil }return ()}// Set deploys the socket system call filter f for the filter type t.func ( *Switch) ( FilterType, Filter) { .once.Do(.init) .fmu.Lock() .fltab[] = .fmu.Unlock()}
The pages are generated with Goldsv0.7.0-preview. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.