forked from xtaci/kcp-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kaite
committed
Jul 11, 2022
1 parent
e65665c
commit 752abec
Showing
11 changed files
with
225 additions
and
21 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package kcp | ||
|
||
import ( | ||
"net" | ||
) | ||
|
||
func ResolveAddr(network, address string) (net.Addr, error) { | ||
switch network { | ||
default: | ||
return nil, net.UnknownNetworkError(network) | ||
case "ip", "ip4", "ip6": | ||
return net.ResolveIPAddr(network, address) | ||
case "tcp", "tcp4", "tcp6": | ||
return net.ResolveTCPAddr(network, address) | ||
case "udp", "udp4", "udp6": | ||
return net.ResolveUDPAddr(network, address) | ||
case "unix", "unixgram", "unixpacket": | ||
return net.ResolveUnixAddr(network, address) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//go:build !plan9 && !windows && !wasm | ||
// +build !plan9,!windows,!wasm | ||
|
||
package kcp | ||
|
||
import ( | ||
"syscall" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func GetBindToDeviceControl(device string) func(network, address string, c syscall.RawConn) error { | ||
if device == "" { | ||
return Control | ||
} | ||
return func(network, address string, c syscall.RawConn) error { | ||
var err error | ||
c.Control(func(fd uintptr) { | ||
err = unix.SetsockoptString(int(fd), unix.SOL_SOCKET, unix.SO_BINDTODEVICE, device) | ||
if err != nil { | ||
return | ||
} | ||
|
||
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 | ||
} | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Package reuseport provides Listen and Dial functions that set socket | ||
// options in order to be able to reuse ports. You should only use this | ||
// package if you know what SO_REUSEADDR and SO_REUSEPORT are. | ||
// | ||
// For example: | ||
// | ||
// // listen on the same port. oh yeah. | ||
// l1, _ := reuse.Listen("tcp", "127.0.0.1:1234") | ||
// l2, _ := reuse.Listen("tcp", "127.0.0.1:1234") | ||
// | ||
// // dial from the same port. oh yeah. | ||
// l1, _ := reuse.Listen("tcp", "127.0.0.1:1234") | ||
// l2, _ := reuse.Listen("tcp", "127.0.0.1:1235") | ||
// c, _ := reuse.Dial("tcp", "127.0.0.1:1234", "127.0.0.1:1235") | ||
// | ||
// Note: cant dial self because tcp/ip stacks use 4-tuples to identify connections, | ||
// and doing so would clash. | ||
package kcp | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
) | ||
|
||
// Available returns whether or not SO_REUSEPORT or equivalent behaviour is | ||
// available in the OS. | ||
func Available() bool { | ||
return true | ||
} | ||
|
||
// var listenConfig = net.ListenConfig{ | ||
// Control: Control, | ||
// } | ||
|
||
func newListenerConfig(device string) net.ListenConfig { | ||
return net.ListenConfig{ | ||
Control: GetBindToDeviceControl(device), | ||
} | ||
} | ||
|
||
// Listen listens at the given network and address. see net.Listen | ||
// Returns a net.Listener created from a file discriptor for a socket | ||
// with SO_REUSEPORT and SO_REUSEADDR option set. | ||
func ReUseListen(device, network, address string) (net.Listener, error) { | ||
listenConfig := newListenerConfig(device) | ||
return listenConfig.Listen(context.Background(), network, address) | ||
} | ||
|
||
// ListenPacket listens at the given network and address. see net.ListenPacket | ||
// Returns a net.Listener created from a file discriptor for a socket | ||
// with SO_REUSEPORT and SO_REUSEADDR option set. | ||
func ListenPacket(device, network, address string) (net.PacketConn, error) { | ||
listenConfig := newListenerConfig(device) | ||
return listenConfig.ListenPacket(context.Background(), network, address) | ||
} | ||
|
||
// Dial dials the given network and address. see net.Dialer.Dial | ||
// Returns a net.Conn created from a file descriptor for a socket | ||
// with SO_REUSEPORT and SO_REUSEADDR option set. | ||
func ReUseDial(device, network, laddr, raddr string) (net.Conn, error) { | ||
nla, err := ResolveAddr(network, laddr) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to resolve local addr: %w", err) | ||
} | ||
d := net.Dialer{ | ||
Control: GetBindToDeviceControl(device), | ||
LocalAddr: nla, | ||
} | ||
return d.Dial(network, raddr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters