-
Notifications
You must be signed in to change notification settings - Fork 57
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
Add support for SocketOptions #36
Conversation
How about passing a callback function that will get called before listening/connecting like |
@ishidawataru is this more what you had in mind? |
@spikecurtis Thank you. It's getting close. Can we use the same function signature as For example, it'd be better if we can pass this to the Listen API. Also I'm wondering if we can even reuse |
I believe doing that would require me to implement the whole |
https://golang.org/pkg/syscall/#RawConn How about keeping |
Implementing |
How about making
which uses Control and InitMsg if needed for listening/dialing? |
@ishidawataru Ok, done. Note that we want |
thanks. Can you change like below? diff --git a/sctp.go b/sctp.go
index 993071c..730b67a 100644
--- a/sctp.go
+++ b/sctp.go
@@ -715,7 +715,7 @@ type SocketConfig struct {
// If Control is not nil it is called after the socket is created but before
// it is bound or connected. fd is the file descriptor representing the
// socket in the OS.
- Control func(fd uintptr)
+ Control func(network, address string, c syscall.RawConn) error
// InitMsg is the options to send in the initial SCTP message
InitMsg InitMsg
diff --git a/sctp_linux.go b/sctp_linux.go
index ce57e0b..47b2263 100644
--- a/sctp_linux.go
+++ b/sctp_linux.go
@@ -172,7 +172,7 @@ func ListenSCTPExt(network string, laddr *SCTPAddr, options InitMsg) (*SCTPListe
}
// listenSCTPExtConfig - start listener on specified address/port with given SCTP options and socket configuration
-func listenSCTPExtConfig(network string, laddr *SCTPAddr, options InitMsg, control func(fd uintptr)) (*SCTPListener, error) {
+func listenSCTPExtConfig(network string, laddr *SCTPAddr, options InitMsg, control func(network, address string, c syscall.RawConn) error) (*SCTPListener, error) {
af, ipv6only := favoriteAddrFamily(network, laddr, nil, "listen")
sock, err := syscall.Socket(
af,
@@ -193,7 +193,12 @@ func listenSCTPExtConfig(network string, laddr *SCTPAddr, options InitMsg, contr
return nil, err
}
if control != nil {
- control(uintptr(sock))
+ c := rawConn{
+ sockfd: sock,
+ }
+ if err = control(network, laddr.String(), c); err != nil {
+ return nil, err
+ }
}
err = setInitOpts(sock, options)
if err != nil {
@@ -250,7 +255,7 @@ func DialSCTPExt(network string, laddr, raddr *SCTPAddr, options InitMsg) (*SCTP
}
// dialSCTPExtConfig - same as DialSCTP but with given SCTP options and socket configuration
-func dialSCTPExtConfig(network string, laddr, raddr *SCTPAddr, options InitMsg, control func(fd uintptr)) (*SCTPConn, error) {
+func dialSCTPExtConfig(network string, laddr, raddr *SCTPAddr, options InitMsg, control func(network, address string, c syscall.RawConn) error) (*SCTPConn, error) {
af, ipv6only := favoriteAddrFamily(network, laddr, raddr, "dial")
sock, err := syscall.Socket(
af,
@@ -271,7 +276,12 @@ func dialSCTPExtConfig(network string, laddr, raddr *SCTPAddr, options InitMsg,
return nil, err
}
if control != nil {
- control(uintptr(sock))
+ c := rawConn{
+ sockfd: sock,
+ }
+ if err = control(network, laddr.String(), c); err != nil {
+ return nil, err
+ }
}
err = setInitOpts(sock, options)
if err != nil { Then, we can do like this // test.go
package main
import (
"fmt"
"github.com/ishidawataru/sctp"
"github.com/libp2p/go-reuseport"
)
func main() {
c := sctp.SocketConfig{
Control: reuseport.Control,
}
laddr := &sctp.SCTPAddr{}
ln, err := c.Listen("sctp", laddr)
fmt.Println(ln, err)
}
|
Nice! I've made those changes |
Thanks. LGTM. Please squash into one commit. |
Squashed |
Signed-off-by: Spike Curtis <spike@tigera.io>
Thanks! merged. |
Is there an example or UT of this new usage? Can we modify, extend the ListenSCTPExt() and DialSCTPExt() to include all of the socket options described in RFC6458? For an application using SCTP, many of these parameters need to be configurable and I need an API to be able to modify them. |
Signed-off-by: Spike Curtis spike@tigera.io
I'm finding myself needing to set
SO_REUSEADDR
andSO_REUSEPORT
in my application. WDYT of this way of passing them to the library?