Commit 6d725e97 authored by Mikio Hara's avatar Mikio Hara

net: simplify ListenMulticastUDP

R=rsc, iant, dave
CC=golang-dev
https://golang.org/cl/6999053
parent c241f696
......@@ -211,25 +211,22 @@ func ListenMulticastUDP(net string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, e
return nil, UnknownNetworkError(net)
}
if gaddr == nil || gaddr.IP == nil {
return nil, &OpError{"listenmulticast", net, nil, errMissingAddress}
return nil, &OpError{"listen", net, nil, errMissingAddress}
}
fd, err := internetSocket(net, gaddr.toAddr(), nil, noDeadline, syscall.SOCK_DGRAM, 0, "listen", sockaddrToUDP)
if err != nil {
return nil, err
}
c := newUDPConn(fd)
ip4 := gaddr.IP.To4()
if ip4 != nil {
err := listenIPv4MulticastUDP(c, ifi, ip4)
if err != nil {
if ip4 := gaddr.IP.To4(); ip4 != nil {
if err := listenIPv4MulticastUDP(c, ifi, ip4); err != nil {
c.Close()
return nil, err
return nil, &OpError{"listen", net, &IPAddr{IP: ip4}, err}
}
} else {
err := listenIPv6MulticastUDP(c, ifi, gaddr.IP)
if err != nil {
if err := listenIPv6MulticastUDP(c, ifi, gaddr.IP); err != nil {
c.Close()
return nil, err
return nil, &OpError{"listen", net, &IPAddr{IP: gaddr.IP}, err}
}
}
return c, nil
......@@ -237,17 +234,14 @@ func ListenMulticastUDP(net string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, e
func listenIPv4MulticastUDP(c *UDPConn, ifi *Interface, ip IP) error {
if ifi != nil {
err := setIPv4MulticastInterface(c.fd, ifi)
if err != nil {
if err := setIPv4MulticastInterface(c.fd, ifi); err != nil {
return err
}
}
err := setIPv4MulticastLoopback(c.fd, false)
if err != nil {
if err := setIPv4MulticastLoopback(c.fd, false); err != nil {
return err
}
err = joinIPv4GroupUDP(c, ifi, ip)
if err != nil {
if err := joinIPv4Group(c.fd, ifi, ip); err != nil {
return err
}
return nil
......@@ -255,34 +249,15 @@ func listenIPv4MulticastUDP(c *UDPConn, ifi *Interface, ip IP) error {
func listenIPv6MulticastUDP(c *UDPConn, ifi *Interface, ip IP) error {
if ifi != nil {
err := setIPv6MulticastInterface(c.fd, ifi)
if err != nil {
if err := setIPv6MulticastInterface(c.fd, ifi); err != nil {
return err
}
}
err := setIPv6MulticastLoopback(c.fd, false)
if err != nil {
if err := setIPv6MulticastLoopback(c.fd, false); err != nil {
return err
}
err = joinIPv6GroupUDP(c, ifi, ip)
if err != nil {
if err := joinIPv6Group(c.fd, ifi, ip); err != nil {
return err
}
return nil
}
func joinIPv4GroupUDP(c *UDPConn, ifi *Interface, ip IP) error {
err := joinIPv4Group(c.fd, ifi, ip)
if err != nil {
return &OpError{"joinipv4group", c.fd.net, &IPAddr{IP: ip}, err}
}
return nil
}
func joinIPv6GroupUDP(c *UDPConn, ifi *Interface, ip IP) error {
err := joinIPv6Group(c.fd, ifi, ip)
if err != nil {
return &OpError{"joinipv6group", c.fd.net, &IPAddr{IP: ip}, err}
}
return nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment