Есть ли способ отправить сообщение UDP на MAC-адрес в Python? - PullRequest
0 голосов
/ 13 апреля 2019

Я пытаюсь сделать DNS-сервер и хочу отправить DNS-запросы на Mac-адрес. Есть ли способ сделать это?

Я пытался просто запросить ipv4, но я не получил правильного ответа, он просто перенес меня на сервер имен.

def readAnswer(answer, message):
    looking = answer[len(message):]
    while looking:
        resptype = looking[:1]
        looking = looking[1:]
        if(resptype == 'c'):
            offset = looking[:3]
            qType = looking[3:7]
            cType = looking[7:11]
            ttl = looking[11:19]
            length = looking[19:23]
            print(looking[:23])
            looking = looking[23:]
            if int(length, 16) == 4 and int(cType, 16) == 1:
                temp = ''
                for x in range(0 , 4): # x is useless
                    temp += str(int(looking[:2], 16))
                    temp += '.'
                    looking = looking[2:]
                temp = temp[:len(temp)-1]
                myQueue.enqueue(temp)
            # elif int(length, 16) == 6 and int(cType, 16) == 1:
            #     temp = ''
            #     for x in range(0,6):
            #         temp += looking[:2]
            #         temp += ':'
            #         looking = looking[2:]
            #     temp = temp[:len(temp)-1]
            #     myQueue.enqueue(temp)
            else:
                looking = looking[2*int(length, 16):]

def sendMessage(name, recursive, address):
    if name in cache:
        return cache[name]
    message = ID
    lookup = hexName(name)
    addr = address
    port = 53
    if recursive:
        message += RECURSIVE
        message += QDCOUNT
        message += ANCOUNT
        message += NSCOUNT
        message += ARCOUNT
        message += lookup
        message += QTYPE
        message += QCLASS
        message = message.replace(' ', '').replace('\n', '')
        serverAddress = (addr, port)
        mySocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        try:
            mySocket.sendto(binascii.unhexlify(message), serverAddress)
            data, _ = mySocket.recvfrom(4096)
        finally:
            mySocket.close()
        alpha = binascii.hexlify(data).decode('utf-8')
        beta = alpha[len(message)+24:]
        temp = ''
        temptxt = beta[0] + beta[1]
        temp += str(int(temptxt, 16))
        temp += '.'
        temptxt = beta[2] + beta[3]
        temp += str(int(temptxt, 16))
        temp += '.'
        temptxt = beta[4] + beta[5]
        temp += str(int(temptxt, 16))
        temp += '.'
        temptxt = beta[6] + beta[7]
        temp += str(int(temptxt, 16))
        cache.update( {name : temp} )
        return temp
    else:
        message += ITERATIVE
        message += QDCOUNT
        message += ANCOUNT
        message += NSCOUNT
        message += ARCOUNT
        message += lookup
        message += QTYPE
        message += QCLASS
        message = message.replace(' ', '').replace('\n', '')
        mySocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        mySocket.settimeout(30)
        myQueue.enqueue(address)
        while not myQueue.isEmpty():
            addr = myQueue.dequeue()
            if name in cache and cache[name] == addr:
                return addr
            cache.update({name : addr})
            serverAddress = (addr, port)
            print(serverAddress)
            mySocket.sendto(binascii.unhexlify(message), serverAddress)
            try:
                data, _ = mySocket.recvfrom(4096)
            except:
                print('error')
                continue
            alpha = binascii.hexlify(data).decode('utf-8')
            readAnswer(alpha, message)
        print(cache)
        mySocket.close()
        return cache[name]

Мой ожидаемый результат - итеративный ответ на мой DNS-запрос, но я получаю ответ:

('03: 61: 6e: 79: c0: 29', 53) ошибка номер 11001 ошибка получения информации об адресе

...