Setting SO_BINDTODEVICE to a TCPConn

895 views
Skip to first unread message

Elazar Leibovich

unread,
Mar 4, 2014, 3:07:00 PM3/4/14
to golan...@googlegroups.com
Some socket options must be set before calling "connect" on the socket.

How can I produce a TCPConn whose socket options are set.

For example, how can I create a TCPConn whose socket have SO_BINDTODEVICE set? Since TCPConn is private, I cannot produce it on my own, and the only way to do that (without reimplementing the interface), is to use Dial(), which will already connect the socket to its destination.

What's the best way to solve this issue?

C Banning

unread,
Mar 4, 2014, 4:10:33 PM3/4/14
to golan...@googlegroups.com

Elazar Leibovich

unread,
Mar 4, 2014, 4:19:26 PM3/4/14
to C Banning, golang-nuts
Thanks, but I'd rather not patch Go if possible.


--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/ez7iAmT7ddE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Kyle Lemons

unread,
Mar 4, 2014, 4:31:05 PM3/4/14
to Elazar Leibovich, golang-nuts
I think you should be able to make all of the syscalls yourself to set up the socket and then when you want it to become a net.Conn you call http://godoc.org/net/#FileConn


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Elazar Leibovich

unread,
Mar 4, 2014, 4:37:46 PM3/4/14
to Kyle Lemons, golang-nuts

This is a good idea, thanks.

Though, I'll have to replicate the logic in Dial.

Mikio Hara

unread,
Mar 4, 2014, 4:44:16 PM3/4/14
to Elazar Leibovich, Kyle Lemons, golang-nuts
On Wed, Mar 5, 2014 at 6:37 AM, Elazar Leibovich <ela...@gmail.com> wrote:

> Though, I'll have to replicate the logic in Dial.

Unfortunately, but SO_BINDTODEVICE (via syscall.BindToDeivice) is
Linux-specific stuff and also the behavior is a bit strange; different
btw IPv4 and IPv6, trying to override source/destination address
selection on IP packet routing. I have no clue how we can bring this
sort of feature into package net without unnecessary confusion.

Elazar Leibovich

unread,
Mar 4, 2014, 4:49:52 PM3/4/14
to Mikio Hara, golang-nuts, Kyle Lemons

Very simply, allow a callback accepting fd to the dialer struct.

Dialer{FDCallback:func(fd int){}}.Dial(...)

Mikio Hara

unread,
Mar 4, 2014, 4:53:27 PM3/4/14
to Elazar Leibovich, golang-nuts, Kyle Lemons
a nit. ;)

On Wed, Mar 5, 2014 at 6:49 AM, Elazar Leibovich <ela...@gmail.com> wrote:

> Dialer{FDCallback:func(fd int){}}.Dial(...)

We cannot drop windows support, and are not keen on exposing raw
socket descriptor or windows handle.

amba...@defend7.com

unread,
Aug 20, 2014, 9:16:36 PM8/20/14
to golan...@googlegroups.com
Hi Elazar,
     I just ran into this problem. Used introspection to get access to private variables....

Here is quick writeup I did about how I did the setsockopt.....


Do let me know if this helps.

Ambarish

Elazar Leibovich

unread,
Aug 22, 2014, 5:37:09 AM8/22/14
to amba...@defend7.com, golang-nuts
Thanks,

This would work, but is obviously due to breakage in any Go release. Reminds me this story about Facebook app using a similar "solution":



--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/ez7iAmT7ddE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages