#!/usr/bin/env python3
import socket
import struct
import time
import sys

def send_query(qtype, service="_http._tcp.local", target="224.0.0.251", port=5353):
    # qtype: 12=PTR, 16=TXT, 33=SRV
    header = struct.pack('>HHHHHH', 0x1234, 0x0000, 1, 0, 0, 0)
    labels = service.split('.')
    name = b''.join([bytes([len(l)]) + l.encode() for l in labels]) + b'\x00'
    question = name + struct.pack('>HH', qtype, 0x0001)  # qtype, class IN
    packet = header + question
    
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.settimeout(2)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    try:
        sock.sendto(packet, (target, port))
        typename = {12: "PTR", 16: "TXT", 33: "SRV"}.get(qtype, str(qtype))
        print(f"[*] Sent {typename} query to {target}:{port}")
    except Exception as e:
        print(f"[!] Error: {e}")
    finally:
        sock.close()

if __name__ == "__main__":
    target = sys.argv[1] if len(sys.argv) > 1 else "127.0.0.1"
    
    # Send TXT query (type 16)
    print("[*] Sending TXT query...")
    send_query(16, "_http._tcp.local", target)
    time.sleep(0.5)
    
    # Send SRV query (type 33)
    print("[*] Sending SRV query...")
    send_query(33, "_http._tcp.local", target)
    time.sleep(0.5)
    
    # Send PTR query (type 12) - original repro
    print("[*] Sending PTR query...")
    send_query(12, "_http._tcp.local", target)
    time.sleep(0.5)
