88 lines
1.9 KiB
C
88 lines
1.9 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include "constants.h"
|
|
#include "question.h"
|
|
#include "packet.h"
|
|
|
|
DNSQuestion * dns_question_create (const char *name, int type, int klass) {
|
|
DNSQuestion *question;
|
|
|
|
question = (DNSQuestion *) malloc (sizeof (DNSQuestion));
|
|
|
|
if (question == NULL) return NULL;
|
|
|
|
memset (question, 0, sizeof (DNSQuestion));
|
|
|
|
strncpy (question->qname, name, 255);
|
|
|
|
question->qtype = type;
|
|
question->qklass = klass;
|
|
|
|
return question;
|
|
}
|
|
|
|
void dns_question_pack_data (DNSQuestion *question, DNSPacket *packet) {
|
|
uint16_t t16;
|
|
|
|
dns_packet_compress_name (packet, question->qname);
|
|
|
|
t16 = htons (question->qtype);
|
|
memcpy (&packet->rdata[packet->rdlength], &t16, 2);
|
|
packet->rdlength += 2;
|
|
|
|
t16 = htons (question->qklass);
|
|
memcpy (&packet->rdata[packet->rdlength], &t16, 2);
|
|
packet->rdlength += 2;
|
|
}
|
|
|
|
void dns_question_free (DNSQuestion *question) {
|
|
free (question);
|
|
}
|
|
|
|
int dns_question_parse_question_from_packet (DNSPacket *packet) {
|
|
DNSQuestion *question;
|
|
uint16_t t16;
|
|
|
|
question = (DNSQuestion *) malloc (sizeof (DNSQuestion));
|
|
|
|
if (question == NULL) return -1;
|
|
|
|
memset (question, 0, sizeof (DNSQuestion));
|
|
|
|
if (dns_packet_expand_name (question->qname, packet, &packet->rdread) < 0) {
|
|
dns_question_free (question);
|
|
return -1;
|
|
}
|
|
|
|
if (packet->rdlength < packet->rdread + 4) {
|
|
dns_question_free (question);
|
|
return -1;
|
|
}
|
|
|
|
memcpy (&t16, &packet->rdata[packet->rdread], sizeof (t16));
|
|
question->qtype = ntohs (t16);
|
|
packet->rdread += 2;
|
|
|
|
memcpy (&t16, &packet->rdata[packet->rdread], sizeof (t16));
|
|
question->qklass = ntohs (t16);
|
|
packet->rdread += 2;
|
|
|
|
if (question->qklass != RR_CLASS_IN &&
|
|
question->qklass != RR_CLASS_CH &&
|
|
question->qklass != RR_CLASS_HS) {
|
|
dns_question_free (question);
|
|
return -1;
|
|
}
|
|
|
|
/* TODO: Validar el qtype */
|
|
|
|
/* Anexar a la lista de preguntas */
|
|
packet->questions = g_list_append (packet->questions, question);
|
|
return 0;
|
|
}
|
|
|