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

udp broadcast #526

Closed
gopherbot opened this issue Jan 13, 2010 · 11 comments
Closed

udp broadcast #526

gopherbot opened this issue Jan 13, 2010 · 11 comments

Comments

@gopherbot
Copy link
Contributor

by graycardinalster:

The enclosed program gives out an error

ListenUDP: dial udp: cannot assign requested address

Attachments:

  1. broken_bcast.go (405 bytes)
@gopherbot
Copy link
Contributor Author

Comment 1 by graycardinalster:

src/pkg/net/udpsock.go:76
The fourth argument should be the _pointer_ on a variable containing 1

@gopherbot
Copy link
Contributor Author

Comment 2 by graycardinalster:

C version
int setBroadcast (int fd) 
{
    int one = 1;
    return setsockopt (fd, SOL_SOCKET, SO_BROADCAST, &one, 4);
}

@rsc
Copy link
Contributor

rsc commented Jan 14, 2010

Comment 3:

The pointer part isn't the problem.  udpsock calls setsockoptInt,
which (in syscall/syscall_linux.go's SetsockoptInt) does in fact
pass a pointer to the system call.
I don't know what the problem is, though.  If you'd like to
investigate the source is in src/pkg/net and src/pkg/syscall.

Owner changed to r...@golang.org.

Status changed to HelpWanted.

@gopherbot
Copy link
Contributor Author

Comment 4 by dean.prichard:

I took a quick look, and for what it is worth, I get the above error only if 
ServerUDPaddr is not the broadcast for my local network. If it is, I don't see a
runtime error.

@gopherbot
Copy link
Contributor Author

Comment 5 by graycardinalster:

However the example does not work for me if to specify broadcast the address.
192.168.111.2 - works. 192.168.111.255 - not  
OS Linux RedFlag

@gopherbot
Copy link
Contributor Author

Comment 6 by graycardinalster:

Mmm.............
_,err = net.ListenUDP("udp4", laddr)
work :)

@gopherbot
Copy link
Contributor Author

Comment 7 by graycardinalster:

broken_bcast2.go:
dial udp4 192.168.111.255:666: permission denied

Attachments:

  1. broken_bcast2.go (345 bytes)

@gopherbot
Copy link
Contributor Author

Comment 8 by dean.prichard:

This seems to be linux specific.
for broken_bcast2.go strace suggests that this error is  
because connect() is called before SO_BROADCAST is set.
this is probably not the best solution, but you could try:
--- a/src/pkg/net/sock.go   Sat Jan 09 09:47:45 2010 -0800
+++ b/src/pkg/net/sock.go   Sat Jan 16 01:43:42 2010 +0000
@@ -35,6 +35,9 @@
    // Allow reuse of recently-used addresses.
    syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
 
+   // For UDP Broadcasts on linux
+   setsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)
+
    if la != nil {
        e = syscall.Bind(s, la)
        if e != 0 {

@gopherbot
Copy link
Contributor Author

Comment 9 by dean.prichard:

the following fixes it for me:
diff -r 1f997563b819 src/pkg/net/sock.go
--- a/src/pkg/net/sock.go   Fri Jan 15 14:02:53 2010 -0800
+++ b/src/pkg/net/sock.go   Sat Jan 16 07:56:41 2010 +0000
@@ -35,6 +35,11 @@
    // Allow reuse of recently-used addresses.
    syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
 
+   switch net {
+   case "udp", "udp4":
+       syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)
+   }
+
    if la != nil {
        e = syscall.Bind(s, la)
        if e != 0 {
diff -r 1f997563b819 src/pkg/net/udpsock.go
--- a/src/pkg/net/udpsock.go    Fri Jan 15 14:02:53 2010 -0800
+++ b/src/pkg/net/udpsock.go    Sat Jan 16 07:56:41 2010 +0000
@@ -71,11 +71,7 @@
    fd *netFD
 }
 
-func newUDPConn(fd *netFD) *UDPConn {
-   c := &UDPConn{fd}
-   setsockoptInt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)
-   return c
-}
+func newUDPConn(fd *netFD) *UDPConn { return &UDPConn{fd} }
 
 func (c *UDPConn) ok() bool { return c != nil && c.fd != nil }

@gopherbot
Copy link
Contributor Author

Comment 10 by graycardinalster:

Yes, it works. Thanks you.

@rsc
Copy link
Contributor

rsc commented Jan 18, 2010

Comment 11:

This issue was closed by revision 7c1bb00.

Status changed to Fixed.

Merged into issue #-.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc removed their assignment Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants