Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Go Control API #56

Merged
merged 14 commits into from
Jan 4, 2019
Merged
2 changes: 1 addition & 1 deletion .gx/lastpubver
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.19: QmXD921xzL9EDRpD6gRm1cb7Khm8VEpZ3NT3nPK7uTX6Fq
0.1.19: QmXpK6aZZpAG38T6aSDBiYPU8wvDoHSczWmXurJzr82j4j
anacrolix marked this conversation as resolved.
Show resolved Hide resolved
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![travisbadge](https://travis-ci.org/libp2p/go-reuseport.svg)](https://travis-ci.org/libp2p/go-reuseport)

This package enables listening and dialing from _the same_ TCP or UDP port.
This means that the following sockopts are set:
This means that the following sockopts may be set:

```
SO_REUSEADDR
Expand All @@ -12,12 +12,10 @@ SO_REUSEPORT

- godoc: https://godoc.org/github.com/libp2p/go-reuseport

This is a simple package to get around the problem of reusing addresses.
The go `net` package (to my knowledge) does not allow setting socket options.
This is particularly problematic when attempting to do TCP NAT holepunching,
which requires a process to both Listen and Dial on the same TCP port.
This package makes this possible for me. It is a pretty narrow use case, but
perhaps this package can grow to be more general over time.
This is a simple package to help with address reuse. This is particularly
important when attempting to do TCP NAT holepunching, which requires a process
to both Listen and Dial on the same TCP port. This package provides some
utilities around enabling this behaviour on various OS.

## Examples

Expand All @@ -39,4 +37,4 @@ c, _ := reuse.Dial("tcp", "127.0.0.1:1234", "127.0.0.1:1235")

## Tested

Tested on `darwin` and `linux`.
Tested on `darwin`, `linux`, and `windows`.
47 changes: 0 additions & 47 deletions available_unix.go

This file was deleted.

25 changes: 25 additions & 0 deletions control_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// +build !windows

package reuseport

import (
"syscall"

"golang.org/x/sys/unix"
)

func Control(network, address string, c syscall.RawConn) error {
var err error
c.Control(func(fd uintptr) {
err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
if err != nil {
return
}

err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
if err != nil {
return
}
})
return err
}
13 changes: 13 additions & 0 deletions control_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package reuseport

import (
"syscall"

"golang.org/x/sys/windows"
)

func Control(network, address string, c syscall.RawConn) (err error) {
return c.Control(func(fd uintptr) {
err = windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
})
}
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/libp2p/go-reuseport

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pkg/errors v0.8.0
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2
golang.org/x/sys v0.0.0-20190102155601-82a175fd1598
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20190102155601-82a175fd1598 h1:S8GOgffXV1X3fpVG442QRfWOt0iFl79eHJ7OPt725bo=
golang.org/x/sys v0.0.0-20190102155601-82a175fd1598/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Loading