Пример #1
0
/**
 * Each line consists of a key and a data string, which are comma separated.
 * Both key and data could contain non-printable characters encoded as \nnn;
 * they are written as-is in the output, but for offset calculation the byte
 * count required for it will be needed.
 *
 * @param s  Input line (starting with the key)
 * @param key_len  (output) Byte count of the key
 * @param data_ptr  (output) Start of data part of the line
 * @param data_len  (output) Byte count of the data
 * @return  0 on success, -1 otherwise
 */
static int _preprocess_line(char *s, int *key_len, char **data_ptr,
    int *data_len, int line)
{
    char *pos;
    size_t len;

    // chop off trailing newline and spaces
    len = strlen(s);
    while (len > 0 && s[len-1] <= ' ')
	len--;
    s[len] = 0;

    // split into key and data parts
    pos = strchr(s, ',');
    if (!pos) {
	fprintf(stderr, "%d: line without comma, ignoring\n",
	    line);
	return -1;
    }

    *pos++ = 0;
    *data_ptr = pos;

    // determine byte requirement of both key and data
    *key_len = special_strlen(s);
    *data_len = special_strlen(pos);

    return 0;
}
static uint16_t populate_record(const char *name,
                                uint16_t qtype, uint16_t qclass,
                                uint32_t ttl, const void *data,
                                uint16_t datalen,
                                struct pbuf *dest)
{
    int title_len = special_strlen(name);
    int msglen = title_len  + sizeof(struct record) + datalen;
    struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, msglen, PBUF_RAM);

    memcpy(p->payload, name, title_len);
    char *end = ((char *)p->payload) + title_len;

    struct record *rec = (struct record *) end;
    rec->qtype = htons(qtype);
    rec->qclass = htons(qclass);
    rec->ttl = htonl(ttl);
    rec->data_length = htons(datalen);
    memcpy(rec->data, data, datalen);

    uint16_t ret = dest->tot_len + title_len + sizeof(*rec);

    pbuf_cat(dest, p);

    return htons(DATA_POINTER | ret);
}