import struct


def dns_name_to_wire(domain_name, origin=None):
    """Transform a domain name to wire-format"""
    if not domain_name.endswith('.'):
        assert origin is not None
        assert origin.endswith('.')
        domain_name += '.' + origin.lstrip('.')
    if domain_name == '.':
        return b'\0'
    labels = domain_name.encode('ascii').lower().split(b'.')
    return b''.join(struct.pack('B', len(x)) + x for x in labels)