Commit 156f67c5 authored by Steven Allen's avatar Steven Allen

initial commit

parents
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f h1:JOrtw2xFKzlg+cbHpyrpLDmnN1HqhBfnX7WDiW7eG2c=
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/gxed/hashland/keccakpg v0.0.1 h1:wrk3uMNaMxbXiHibbPO4S0ymqJMm41WiudyFSs7UnsU=
github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU=
github.com/gxed/hashland/murmur3 v0.0.1 h1:SheiaIt0sda5K+8FLz952/1iWS9zrnKsEJaOJu4ZbSc=
github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48=
github.com/ipfs/go-log v0.0.1 h1:9XTUN/rW64BCG1YhPK9Hoy3q8nr4gOmHHBpgFdfw6Lc=
github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
github.com/mattn/go-isatty v0.0.5 h1:tHXDdz1cpzGaovsTB+TVB8q90WEokoVmfMqoVcrLUgw=
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ=
github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16 h1:5W7KhL8HVF3XCFOweFD3BNESdnO8ewyYTFT2R+/b8FQ=
github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U=
github.com/mr-tron/base58 v1.1.0 h1:Y51FGVJ91WBqCEabAi5OPUz38eAx8DakuAm5svLcsfQ=
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
github.com/multiformats/go-multiaddr v0.1.0 h1:fkISCUNDb3xIpCcI6BGlPsQE+ywcxzimOsUnHWnrE74=
github.com/multiformats/go-multiaddr v0.1.0/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44=
github.com/multiformats/go-multiaddr-net v0.1.0 h1:ZepO8Ezwovd+7b5XPPDhQhayk1yt0AJpzQBpq9fejx4=
github.com/multiformats/go-multiaddr-net v0.1.0/go.mod h1:5JNbcfBOP4dnhoZOv10JJVkJO0pCCEf8mTnipAo2UZQ=
github.com/multiformats/go-multihash v0.0.1 h1:HHwN1K12I+XllBCrqKnhX949Orn4oawPkegHMu2vDqQ=
github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U=
github.com/opentracing/opentracing-go v1.0.2 h1:3jA2P6O1F9UOrWVpwrIo17pu01KWvNWg4X946/Y5Zwg=
github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc h1:9lDbC6Rz4bwmou+oE6Dt4Cb2BGMur5eR/GYptkKUVHo=
github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM=
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67 h1:ng3VDlRp5/DHpSWl02R4rM9I+8M2rhmsuLwAMmkLQWE=
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/net v0.0.0-20190227160552-c95aed5357e7 h1:C2F/nMkR/9sfUTpvR3QrjBuTdvMUC/cFajkphs1YLQo=
golang.org/x/net v0.0.0-20190227160552-c95aed5357e7/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sys v0.0.0-20190219092855-153ac476189d/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
package sockets
import (
"sync"
logging "github.com/ipfs/go-log"
manet "github.com/multiformats/go-multiaddr-net"
)
var (
mu sync.Mutex
log = logging.Logger("socket-activation")
initFn = func() {}
initialized = false
listeners = make(map[string][]manet.Listener)
packetConns = make(map[string][]manet.PacketConn)
)
func initialize() {
if !initialized {
initFn()
initialized = true
}
}
// TakeSockets takes the listeners associated with the given name.
func TakeListeners(name string) ([]manet.Listener, error) {
mu.Lock()
defer mu.Unlock()
initialize()
s := listeners[name]
delete(listeners, name)
return s, nil
}
// ListListeners lists the names of the available listeners.
func ListListeners() ([]string, error) {
mu.Lock()
defer mu.Unlock()
initialize()
names := make([]string, 0, len(listeners))
for name := range listeners {
names = append(names, name)
}
return names, nil
}
// TakePacketConns takes the packet connections associated with the given name.
func TakePacketConns(name string) ([]manet.PacketConn, error) {
mu.Lock()
defer mu.Unlock()
initialize()
s := packetConns[name]
delete(packetConns, name)
return s, nil
}
// ListListeners lists the names of the available packet connections.
func ListPacketConns() ([]string, error) {
mu.Lock()
defer mu.Unlock()
initialize()
names := make([]string, 0, len(packetConns))
for name := range packetConns {
names = append(names, name)
}
return names, nil
}
// +build linux
package sockets
import (
"fmt"
"net"
"os"
"syscall"
activation "github.com/coreos/go-systemd/activation"
manet "github.com/multiformats/go-multiaddr-net"
)
func isListening(file *os.File) (bool, error) {
ret, err := syscall.GetsockoptInt(int(file.Fd()), syscall.SOL_SOCKET, syscall.SO_ACCEPTCONN)
return ret == 1, err
}
func registerFile(file *os.File) error {
listening, err := isListening(file)
if err != nil {
return fmt.Errorf("failed determine the socket type: %s", err)
}
if listening {
listener, err := net.FileListener(file)
if err != nil {
return fmt.Errorf("failed to convert listener: %s", err)
}
mal, err := manet.WrapNetListener(listener)
if err != nil {
return fmt.Errorf("failed to wrap net listener: %s", err)
}
listeners[file.Name()] = append(listeners[file.Name()], mal)
} else {
pc, err := net.FilePacketConn(file)
if err != nil {
return fmt.Errorf("failed to convert to a packet conn: %s", err)
}
mapc, err := manet.WrapPacketConn(pc)
if err != nil {
return fmt.Errorf("failed to wrap net packet conn: %s", err)
}
packetConns[file.Name()] = append(packetConns[file.Name()], mapc)
}
return nil
}
func init() {
// Initialize lazily as this isn't _free_.
initFn = func() {
for _, file := range activation.Files(true) {
err := registerFile(file)
file.Close()
if err != nil {
log.Errorf("when handling the socket %s: %s", file.Name(), err)
}
}
}
}
package sockets
import (
"io/ioutil"
"net"
"os"
"strconv"
"testing"
)
func TestActivation(t *testing.T) {
lt, err := net.ListenTCP("tcp", &net.TCPAddr{IP: net.IPv6loopback})
if err != nil {
t.Fatal(err)
}
if c, err := lt.SyscallConn(); err == nil {
var fd uintptr
c.Control(func(nfd uintptr) {
fd = nfd
})
if fd != 3 {
t.Fatal("expected fd to be 3")
}
} else {
t.Fatal(err)
}
lu, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv6loopback})
if err != nil {
t.Fatal(err)
}
if c, err := lu.SyscallConn(); err == nil {
var fd uintptr
c.Control(func(nfd uintptr) {
fd = nfd
})
if fd != 4 {
t.Fatal("expected fd to be 3")
}
} else {
t.Fatal(err)
}
os.Setenv("LISTEN_FDS", "2")
os.Setenv("LISTEN_PID", strconv.Itoa(os.Getpid()))
os.Setenv("LISTEN_FDNAMES", "tcp:udp")
tcp, err := TakeListeners("tcp")
if err != nil {
t.Fatal(err)
}
if len(tcp) != 1 {
t.Fatalf("expected 1 listener, got %d", len(tcp))
}
go func() {
l := tcp[0]
c, err := l.Accept()
if err != nil {
t.Error(err)
return
}
c.Write([]byte("foobar"))
c.Close()
}()
c, err := net.DialTCP("tcp", nil, lt.Addr().(*net.TCPAddr))
if err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadAll(c)
if err != nil {
t.Error(err)
}
if string(b) != "foobar" {
t.Error("wrong message")
}
udp, err := TakePacketConns("udp")
if err != nil {
t.Fatal(err)
}
if len(udp) != 1 {
t.Fatalf("expected 1 listener, got %d", len(tcp))
}
if udp[0].LocalAddr().String() != lu.LocalAddr().String() {
t.Fatal("got the wrong udp listener")
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment