Example #1
0
func (a *Server) bind() (conn *net.UDPConn, err error) {
	if !a.NotMulticast && a.iface.Flags&net.FlagMulticast == net.FlagMulticast {
		gaddr, err := a.groupAddr()
		if err != nil {
			return nil, e.Forward(err)
		}
		conn, err = net.ListenMulticastUDP(a.Proto(), a.iface, gaddr)
		if err != nil {
			return nil, e.New(err)
		}
	} else {
		server, err := net.ResolveUDPAddr(a.Proto(), ":"+a.Port)
		if err != nil {
			return nil, e.New(err)
		}
		conn, err = net.ListenUDP(a.Proto(), server)
		if err != nil {
			return nil, e.New(err)
		}
	}
	a.ipver(conn.LocalAddr())
	_, a.Port, err = utilNet.SplitHostPort(conn.LocalAddr().String())
	if err != nil {
		return nil, e.Forward(err)
	}
	return
}
Example #2
0
File: url.go Project: fcavani/net
// OnlyHost returns the FQDN or the ip of the host in the url.
func OnlyHost(rawurl string) (string, error) {
	parsed, err := url.Parse(rawurl)
	if err != nil {
		return "", e.Forward(err)
	}
	host, _, err := utilNet.SplitHostPort(parsed.Host)
	if err != nil && !e.Equal(err, utilNet.ErrCantFindPort) {
		return "", e.Forward(err)
	}
	return host, nil
}
Example #3
0
func (c *Client) multicast(addr net.Addr) (*net.UDPAddr, error) {
	host, _, err := utilNet.SplitHostPort(addr.String())
	if err != nil {
		return nil, e.Forward(err)
	}
	if utilNet.IsValidIpv4(host) {
		addr, err := net.ResolveUDPAddr("udp", c.McIpv4+":"+c.Port)
		if err != nil {
			return nil, e.Forward(err)
		}
		return addr, nil
	} else if utilNet.IsValidIpv6(host) {
		addr, err := net.ResolveUDPAddr("udp", c.McIpv6+":"+c.Port)
		if err != nil {
			return nil, e.Forward(err)
		}
		return addr, nil
	} else {
		return nil, e.New("invalid ip address")
	}
}
Example #4
0
// Resolve simple resolver one host name to one ip
func Resolve(h string) (out string, err error) {
	host, port, err := utilNet.SplitHostPort(h)
	if err != nil && !e.Equal(err, utilNet.ErrCantFindPort) {
		return "", e.Forward(err)
	}

	addrs, err := lookuphost(host)
	if err != nil {
		return "", e.Forward(err)
	}
	if len(addrs) <= 0 {
		return "", e.New(ErrHostNotResolved)
	}

	if strings.Contains(addrs[0], ":") {
		out = "[" + addrs[0] + "]"
	} else {
		out = addrs[0]
	}
	if port != "" {
		out += ":" + port
	}
	return
}