packetquery (char *buf, unsigned int len, char **q,
                        char qtype[2], char qclass[2], char id[2])
{
    char header[12];
    unsigned int pos = 0;

    errno = error_proto;
    pos = dns_packet_copy (buf, len, 0, header, 12);
    if (!pos)
        return 0;
    if (header[2] & 128)
        return 0; /* must not respond to responses */
    if (!(header[2] & 1))
        return 0; /* do not respond to non-recursive queries */
    if (header[2] & 120)
        return 0;
    if (header[2] & 2)
        return 0;
    if (byte_diff (header + 4, 2, "\0\1"))
        return 0;

    pos = dns_packet_getname (buf, len, pos, q);
    if (!pos)
        return 0;
    pos = dns_packet_copy (buf, len, pos, qtype, 2);
    if (!pos)
        return 0;
    pos = dns_packet_copy (buf, len, pos, qclass, 2);
    if (!pos)
        return 0;
    if (byte_diff (qclass, 2, DNS_C_IN) && byte_diff (qclass, 2, DNS_C_ANY))
        return 0;

    byte_copy (id, 2, header);

    return 1;
}
packetquery (char *buf, unsigned int len, char **q,
                        char qtype[2], char qclass[2], char id[2])
{
    char header[12];
    unsigned int pos = 0;

    errno = error_proto;
    pos = dns_packet_copy (buf, len, 0, header, 12);
    if (!pos)
        return 0;
    if (header[2] & 128)
        return 0;  
    if (!(header[2] & 1))
        return 0;  
    if (header[2] & 120)
        return 0;
    if (header[2] & 2)
        return 0;
    if (byte_diff (header + 4, 2, "\0\1"))
        return 0;

    pos = dns_packet_getname (buf, len, pos, q);
    if (!pos)
        return 0;
    pos = dns_packet_copy (buf, len, pos, qtype, 2);
    if (!pos)
        return 0;
    pos = dns_packet_copy (buf, len, pos, qclass, 2);
    if (!pos)
        return 0;
    if (byte_diff (qclass, 2, DNS_C_IN) && byte_diff (qclass, 2, DNS_C_ANY))
        return 0;

    byte_copy (id, 2, header);

    return 1;
}