package goupnp

import (
	
	

	
)

// httpuClient creates a HTTPU client that multiplexes to all multicast-capable
// IPv4 addresses on the host. Returns a function to clean up once the client is
// no longer required.
func httpuClient() (httpu.ClientInterfaceCtx, func(), error) {
	,  := localIPv4MCastAddrs()
	if  != nil {
		return nil, nil, ctxError(, "requesting host IPv4 addresses")
	}

	 := make([]io.Closer, 0, len())
	 := make([]httpu.ClientInterfaceCtx, 0, len())
	for ,  := range  {
		,  := httpu.NewHTTPUClientAddr()
		if  != nil {
			return nil, nil, ctxErrorf(,
				"creating HTTPU client for address %s", )
		}
		 = append(, )
		 = append(, )
	}

	 := func() {
		for ,  := range  {
			.Close()
		}
	}

	return httpu.NewMultiClientCtx(), , nil
}

// localIPv2MCastAddrs returns the set of IPv4 addresses on multicast-able
// network interfaces.
func localIPv4MCastAddrs() ([]string, error) {
	,  := net.Interfaces()
	if  != nil {
		return nil, ctxError(, "requesting host interfaces")
	}

	// Find the set of addresses to listen on.
	var  []string
	for ,  := range  {
		if .Flags&net.FlagMulticast == 0 || .Flags&net.FlagLoopback != 0 || .Flags&net.FlagUp == 0 {
			// Does not support multicast or is a loopback address.
			continue
		}
		,  := .Addrs()
		if  != nil {
			return nil, ctxErrorf(,
				"finding addresses on interface %s", .Name)
		}
		for ,  := range  {
			,  := .(*net.IPNet)
			if ! {
				// Not an IPNet address.
				continue
			}
			if .IP.To4() == nil {
				// Not IPv4.
				continue
			}
			 = append(, .IP.String())
		}
	}

	return , nil
}