/* dnscli.c */
#include <dnscli.h>

void zero(int8 *dst, int16 size) {
    int16 n;
    int8 *p;

    for (p=dst, n=size; n; n--, p++)
        *p = 0;
    
    return;
}

int16 count(int8 *src) {
    int16 n;
    int8 *p;

    for (p=src, n=0; *p; p++, n++);

    return n;
}

packet *mkpacket(int8 *ip, int8 *host) {
    int16 size;
    int16 len;
    packet *p;
    packet pkt;

    len = count(host);

    pkt = (packet){
        .e = {
            .dst = $1 "\x00\x0c\x29\xa8\xd1\xee",
            .src = $1 "\x00\x0c\x29\x9f\x80\x7c",
            .type = TypeIPv4
        },
        .i = {
            .version = 4,
            .ihl = 5,
            .dscp = 0,
            .ecn = 0,
            .length = (50+len),
            .id = 0,
            .flags = 0,
            .offset = 0,
            .ttl = 250,
            .protocol = TypeUDP,
            .checksum = 0,
            .src = inet_addr("10.1.2.5"),
            .dst = inet_addr(ip)
        },
        .u = {
            .src = 53,
            .dst = 53,
            .length = (30+len),
            .checksum = 0
        },
        .d = {
            .id = 0,
            .qr = false,
            .opcode = 0,
            .aa = false,
            .tc = false,
            .rd = true,
            .ra = false,
            .z = false,
            .ad = false,
            .cd = true,
            .rcode = 0,
            .num = {
                .questions = 1,
                .answers = 0,
                .authorityrrs = 1,
                .additionalrrs = 0
            }
        }
    };

    size = (sizeof(struct s_packet) + 10 + len);
    p = (packet *)alloc(size);
    assert(p);
    zero($1 p, size);
    *p = pkt;

    // ...

    return p;
}
