C - dns query to structure

Posted by Bibo on Stack Overflow See other posts from Stack Overflow or by Bibo
Published on 2012-11-18T10:36:27Z Indexed on 2012/11/18 11:00 UTC
Read the original article Hit count: 233

Filed under:
|
|
|

I have these structures:

typedef struct dnsQuery {
  char header[12];
  struct dnsQuerySection *querySection;
} TdnsQuery;

typedef struct dnsQuerySection {
  unsigned char *name;
  struct dnsQueryQuestion *question;
} TdnsQuerySection;

typedef struct dnsQueryQuestion {
  unsigned short qtype;
  unsigned short qclass;
} TdnsQueryQuestion;

and I have dns query in byte array from recvfrom. I am trying to get structure from byte array like this:

TdnsQuery* dnsQuery = (TdnsQuery*)buf;

When I tried to access qtype like this:

printf("%u", dnsQuery->querySection->question.qtype);

I get seg fault 11.

Can someone help me with these structures? What´s wrong with them? I tried to add structure:

typedef struct udpPacket {
  char header[8];
  structr dnsQuery query;
}

and mapped this structure from byte array but it didn´t help. Can someone help me with these structures? How they should look like for dns query with udp protocol?

© Stack Overflow or respective owner

Related posts about c

    Related posts about dns