Вратце - есть массив char out[4096]; который в хексе выглядит так:
Код: Выделить всё
for (i=0;i<bytelen;i++)
{
printf("%02x", out[i]);
}
printf("\n");
Код: Выделить всё
066e657473656309636f6c6f73746174650365647500
Код: Выделить всё
struct HDR_Message
{
u_int16_t ID;
u_int16_t VALUES;
u_int16_t QDCOUNT;
u_int16_t ANCOUNT;
u_int16_t NSCOUNT;
u_int16_t ARCOUNT;
} __attribute__((__packed__));
typedef struct HDR_Message header;
struct Question_Message
{
u_char *QNAME;
u_int16_t QTYPE;
u_int16_t QCLASS;
} __attribute__((__packed__));
typedef struct Question_Message question;
........................
// creating new header for DNS question
header *headerPtr = malloc(sizeof(header));
if (headerPtr == NULL)
{
fprintf(stderr, "Failed to allocate memory for header\n");
exit(1);
}
headerPtr->ID = htons(50);
headerPtr->VALUES = 0;
headerPtr->QDCOUNT = htons(1);
headerPtr->ANCOUNT = 0;
headerPtr->NSCOUNT = 0;
headerPtr->ARCOUNT = 0;
question *questionPtr = malloc(sizeof(question));
if (questionPtr == NULL)
{
fprintf(stderr, "Failed to allocate memory for question");
exit(1);
}
questionPtr->QNAME = malloc(sizeof(bytelen));
if (questionPtr->QNAME == NULL)
{
fprintf(stderr, "Failed to allocate memory for QNAME");
exit(1);
}
memcpy(questionPtr->QNAME, out, bytelen);
questionPtr->QTYPE = htons(48);
questionPtr->QCLASS = htons(1);
// calculate total number of bytes to send via socket
int bufsize = sizeof(header)+bytelen+sizeof(questionPtr->QTYPE)+sizeof(questionPtr->QCLASS);
printf("sizeof bufsize is %d\n", bufsize);
buf = malloc(bufsize);
// copy header structure
memcpy(buf, headerPtr, sizeof(header));
// copy byte order of domain
[b] memcpy(buf+sizeof(header), questionPtr->QNAME, bytelen);
//memcpy(buf+sizeof(header), out, bytelen);[/b]
memcpy(buf+sizeof(header)+bytelen, &questionPtr->QTYPE, sizeof(questionPtr->QTYPE));
memcpy(buf+sizeof(header)+bytelen+sizeof(questionPtr->QTYPE), &questionPtr->QCLASS, sizeof(questionPtr->QCLASS));
// debuggin
for (i=0;i<bufsize;i++)
{
printf("%02x", buf[i]);
}
printf("\n");
Ответ:
Код: Выделить всё
out:
066e657473656309636f6c6f73746174650365647500
Printing qname ...
066e657473656309636f6c6f73746174650365647500
sizeof bufsize is 38
003200000001000000000000066e657473656309636f6c6f3100000000320000000100300001
// memcpy(buf+sizeof(header), questionPtr->QNAME, bytelen);
memcpy(buf+sizeof(header), out, bytelen);
Код: Выделить всё
003200000001000000000000066e657473656309636f6c6f7374617465036564750000300001