TODO distinguish between private and public addresses

    A valid machine_name or a machine is needed to make this work!

    !!!
    Guest prerequisite: GuestAddition
    !!!

    Thanks to Shrikant Havale for the StackOverflow answer http://stackoverflow.com/a/29335390

    More information on guest properties: https://www.virtualbox.org/manual/ch04.html#guestadd-guestprops

    @param machine_name:
    @type machine_name: str
    @param machine:
    @type machine: IMachine
    @return: All the IPv4 addresses we could get
    @rtype: str[]

def vb_get_network_addresses(machine_name=None, machine=None, wait_for_pattern=None):
    '''
    TODO distinguish between private and public addresses

    A valid machine_name or a machine is needed to make this work!

    !!!
    Guest prerequisite: GuestAddition
    !!!

    Thanks to Shrikant Havale for the StackOverflow answer http://stackoverflow.com/a/29335390

    More information on guest properties: https://www.virtualbox.org/manual/ch04.html#guestadd-guestprops

    @param machine_name:
    @type machine_name: str
    @param machine:
    @type machine: IMachine
    @return: All the IPv4 addresses we could get
    @rtype: str[]
    '''
    if machine_name:
        machine = vb_get_box().findMachine(machine_name)

    ip_addresses = []
    log.debug("checking for power on:")
    if machine.state == _virtualboxManager.constants.MachineState_Running:

        log.debug("got power on:")

        #wait on an arbitrary named property
        #for instance use a dhcp client script to set a property via VBoxControl guestproperty set dhcp_done 1
        if wait_for_pattern and not machine.getGuestPropertyValue(wait_for_pattern):
            log.debug("waiting for pattern:%s:", wait_for_pattern)
            return None

        _total_slots = machine.getGuestPropertyValue('/VirtualBox/GuestInfo/Net/Count')

        #upon dhcp the net count drops to 0 and it takes some seconds for it to be set again
        if not _total_slots:
            log.debug("waiting for net count:%s:", wait_for_pattern)
            return None

        try:
            total_slots = int(_total_slots)
            for i in range(total_slots):
                try:
                    address = machine.getGuestPropertyValue('/VirtualBox/GuestInfo/Net/{0}/V4/IP'.format(i))
                    if address:
                        ip_addresses.append(address)
                except Exception as e:
                    log.debug(e.message)
        except ValueError as e:
            log.debug(e.message)
            return None

    log.debug("returning ip_addresses:%s:", ip_addresses)
    return ip_addresses