pax_global_header00006660000000000000000000000064150171326120014510gustar00rootroot0000000000000052 comment=5c9eed3713fac61d0e42ee7bd3180a6b57b1b5d3 golang-github-hugelgupf-socketpair-0.0~git20240723.9246f21/000077500000000000000000000000001501713261200226665ustar00rootroot00000000000000golang-github-hugelgupf-socketpair-0.0~git20240723.9246f21/.gitignore000066400000000000000000000003001501713261200246470ustar00rootroot00000000000000# Binaries for programs and plugins *.exe *.exe~ *.dll *.so *.dylib # Test binary, build with `go test -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE *.out golang-github-hugelgupf-socketpair-0.0~git20240723.9246f21/LICENSE000066400000000000000000000027431501713261200237010ustar00rootroot00000000000000BSD 3-Clause License Copyright (c) 2019, Chris K All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. golang-github-hugelgupf-socketpair-0.0~git20240723.9246f21/README.md000066400000000000000000000010211501713261200241370ustar00rootroot00000000000000## socketpair [![Go Report Card](https://goreportcard.com/badge/github.com/hugelgupf/socketpair)](https://goreportcard.com/report/github.com/hugelgupf/socketpair) [![GoDoc](https://godoc.org/github.com/hugelgupf/socketpair?status.svg)](https://godoc.org/github.com/hugelgupf/socketpair) socketpair is a Go library that provides bidirectionally connected net.Conns, net.PacketConns made from socketpair(2) as well as bidirectionally connected net.TCPConns. socketpair is currently mostly used in testing Go protocol libraries. golang-github-hugelgupf-socketpair-0.0~git20240723.9246f21/go.mod000066400000000000000000000000601501713261200237700ustar00rootroot00000000000000module github.com/hugelgupf/socketpair go 1.13 golang-github-hugelgupf-socketpair-0.0~git20240723.9246f21/pipelistener.go000066400000000000000000000056411501713261200257260ustar00rootroot00000000000000// Copyright 2018 The gRPC Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package socketpair import ( "context" "fmt" "net" "sync" ) // Listener implements a listener that creates local, buffered net.Pipe-backed // net.Conns via its Accept and Dial methods. type Listener struct { mu sync.Mutex done chan struct{} conn chan net.Conn pipeFn func() (net.Conn, net.Conn, error) } // Listen returns a Listener that can only be contacted by its own Dialers and // creates net.Pipe-backed unbuffered connections between the two. func Listen() *Listener { return &Listener{ done: make(chan struct{}), conn: make(chan net.Conn), pipeFn: func() (net.Conn, net.Conn, error) { p1, p2 := net.Pipe() return p1, p2, nil }, } } // ListenFunc returns a Listener that can only be contacted by its own Dialers // and creates connections between the two using the supplied function. func ListenFunc(pipeFunc func() (net.Conn, net.Conn, error)) *Listener { return &Listener{ done: make(chan struct{}), conn: make(chan net.Conn), pipeFn: pipeFunc, } } // Accept blocks until Dial or Close are called. If Dial is called, it returns // a net.Conn for the server half of the connection. func (l *Listener) Accept() (net.Conn, error) { select { case <-l.done: return nil, fmt.Errorf("use of closed network connection") case conn := <-l.conn: return conn, nil } } // Close stops the listener. Close unblocks Accept and Dial. func (l *Listener) Close() error { l.mu.Lock() defer l.mu.Unlock() select { case <-l.done: // Already closed. break default: close(l.done) } return nil } func (l *Listener) Addr() net.Addr { return nil } // Dial creates an in-memory full-duplex network connection, unblocks Accept by // providing it the server half of the connection, and returns the client half // of the connection. func (l *Listener) Dial() (net.Conn, error) { return l.DialContext(context.Background()) } // DialContext creates an in-memory full-duplex network connection, unblocks // Accept by providing it the server half of the connection, and returns the // client half of the connection. If ctx is Done, returns ctx.Err() func (l *Listener) DialContext(ctx context.Context) (net.Conn, error) { p1, p2, err := l.pipeFn() if err != nil { return nil, err } select { case <-ctx.Done(): return nil, ctx.Err() case <-l.done: return nil, fmt.Errorf("closed") case l.conn <- p1: return p2, nil } } golang-github-hugelgupf-socketpair-0.0~git20240723.9246f21/pipelistener_test.go000066400000000000000000000007361501713261200267650ustar00rootroot00000000000000package socketpair import ( "strings" "sync" "testing" ) func TestListen(t *testing.T) { l := Listen() var wg sync.WaitGroup wg.Add(1) go func() { defer wg.Done() for { s, err := l.Accept() if err != nil { if !strings.Contains(err.Error(), "use of closed network connection") { t.Fatal(err) } return } s.Close() } }() defer wg.Wait() defer l.Close() client, err := l.Dial() if err != nil { t.Fatal(err) } client.Close() } golang-github-hugelgupf-socketpair-0.0~git20240723.9246f21/socket_linux.go000066400000000000000000000073531501713261200257340ustar00rootroot00000000000000// Copyright 2019 Chris Koch. All rights reserved // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Package socketpair provides bidirectionally connected net.Conns. // // Intended for testing usages of net.PacketConns and net.Conns. package socketpair import ( "net" "os" "syscall" "time" ) // TCPPair6 returns two bidirectionally connected TCPConns using tcp6. // // They will be on randomly assigned ports. func TCPPair6() (*net.TCPConn, *net.TCPConn, error) { return tcpPair("tcp6") } // TCPPair returns two bidirectionally connected TCPConns. // // They will be on randomly assigned ports. func TCPPair() (*net.TCPConn, *net.TCPConn, error) { return tcpPair("tcp4") } func tcpPair(network string) (*net.TCPConn, *net.TCPConn, error) { l, err := net.ListenTCP(network, nil) if err != nil { return nil, nil, err } serverAddr := l.Addr().(*net.TCPAddr) type acceptRet struct { c *net.TCPConn err error } serverConnCh := make(chan acceptRet) go func() { c, err := l.AcceptTCP() serverConnCh <- acceptRet{c: c, err: err} }() clientConn, err := net.DialTCP(network, nil, serverAddr) if err != nil { return nil, nil, err } accept := <-serverConnCh if accept.err != nil { return nil, nil, accept.err } return clientConn, accept.c, nil } // StreamSocketPair returns two bidirectionally connected net.Conns made from // socketpair(2). func StreamSocketPair() (net.Conn, net.Conn, error) { fds, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0) if err != nil { return nil, nil, err } if err := syscall.SetNonblock(int(fds[0]), true); err != nil { return nil, nil, err } if err := syscall.SetNonblock(int(fds[1]), true); err != nil { return nil, nil, err } c0, err := net.FileConn(os.NewFile(uintptr(fds[0]), "socketpair-0")) if err != nil { return nil, nil, err } c1, err := net.FileConn(os.NewFile(uintptr(fds[1]), "socketpair-1")) if err != nil { return nil, nil, err } return c0, c1, nil } // PacketSocketPair returns two bidirectionally connected PacketConns made from // socketpair(2). func PacketSocketPair() (net.PacketConn, net.PacketConn, error) { fds, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_DGRAM, 0) if err != nil { return nil, nil, err } if err := syscall.SetNonblock(int(fds[0]), true); err != nil { return nil, nil, err } if err := syscall.SetNonblock(int(fds[1]), true); err != nil { return nil, nil, err } f1 := os.NewFile(uintptr(fds[0]), "socket pair end 0") sc1, err := f1.SyscallConn() if err != nil { return nil, nil, err } f2 := os.NewFile(uintptr(fds[1]), "socket pair end 1") sc2, err := f2.SyscallConn() if err != nil { return nil, nil, err } n1 := &socketPair{ f: f1, rc: sc1, } n2 := &socketPair{ f: f2, rc: sc2, } return n1, n2, nil } type socketPair struct { f *os.File rc syscall.RawConn } func (s *socketPair) LocalAddr() net.Addr { return nil } func (s *socketPair) SetDeadline(t time.Time) error { return s.f.SetDeadline(t) } func (s *socketPair) SetReadDeadline(t time.Time) error { return s.f.SetReadDeadline(t) } func (s *socketPair) SetWriteDeadline(t time.Time) error { return s.f.SetWriteDeadline(t) } func (s *socketPair) Close() (err error) { return s.f.Close() } func (s *socketPair) ReadFrom(p []byte) (n int, addr net.Addr, err error) { cerr := s.rc.Read(func(fd uintptr) bool { n, err = syscall.Read(int(fd), p) return err != syscall.EAGAIN }) if err != nil { return n, nil, err } return n, nil, cerr } func (s *socketPair) WriteTo(p []byte, _ net.Addr) (n int, err error) { cerr := s.rc.Write(func(fd uintptr) bool { n, err = syscall.Write(int(fd), p) return err != syscall.EAGAIN }) if err != nil { return n, err } return 0, cerr } golang-github-hugelgupf-socketpair-0.0~git20240723.9246f21/socket_linux_test.go000066400000000000000000000006751501713261200267730ustar00rootroot00000000000000package socketpair import ( "log" "sync" "testing" "time" ) func TestHanging(t *testing.T) { pc1, _, err := PacketSocketPair() if err != nil { t.Fatal(err) } var wg sync.WaitGroup wg.Add(1) go func() { defer wg.Done() b := make([]byte, 1) log.Printf("reading...") pc1.ReadFrom(b) log.Printf("reading returned") }() time.Sleep(2 * time.Second) log.Printf("closing...") pc1.Close() log.Printf("closed") wg.Wait() }