Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8868613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:18:43+00:00 2026-06-14T17:18:43+00:00

Possible Duplicate: C – byte array to structure (dns query) I have these structures:

  • 0

Possible Duplicate:
C – byte array to structure (dns query)

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 a DNS query in a byte array buf 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?

Edit: My structures now looks like this:

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

 typedef struct {

      dnsQueryQuestion *question;
      unsigned char *data[0];
 } dnsQuerySection;

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

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

I added parse function:

void parse(unsigned char *data, unsigned short *qtype, unsigned short *qclass) {
    int i = 0;
    while (data[i]) {
        int len = data[i];
        i += len + 1;
    }
    *qtype = (unsigned short) data[i+1];
    *qclass = (unsigned short) data[i+3];
    return;
} 

and tried to parse:

TudpPacket udpPack = (TudpPacket)buf;
parse(udpPack.query.querySection.data, &(udpPack.query.querySection.question.qtype), &(udpPack.query.querySection.question.qclass));
printf("%u\n", udpPack.query.querySection.question.qtype);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:18:44+00:00Added an answer on June 14, 2026 at 5:18 pm

    First you can start out by changing dnsQuery to this:

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

    See the usage of zero-length array here: http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html

    However, because the QNAME part in a DNS question is variable-length, you may not be able to do it separately from QTYPE and QCLASS by using struct directly. For example,

    Header
    
    00 01   - ID = 1
    01 00   - RD = 1
    00 01   - QD = 1
    00 00   - AN
    00 00   - NS
    00 00   - NR
    
       Question  for www.google.com
    
       03 77   - 3 w
       77 77   - w w
       06 67   - 6 g
       6f 6f   - o o
       67 6c   - g l
       65 03   - e 3
       63 6f   - c o
       6d 00   - m 0
       00 01   - QTYPE
       00 01   - QCLASS
    

    If you can guarantee there’s only one question in the incoming request, try this:

    typedef struct dnsQuerySection {
      unsigned char *data[0];
    } TdnsQuerySection;
    

    and write a function to parse it.

    EDIT: Below is a sketch for the parse function, just for your convenience.

    char* parse(unsigned char *data, unsigned short *qtype, unsigned short *qclass) {
        int i = 0;
        while (data[i]) {
            int len = data[i];
            i += len + 1;
        }
        /* data[i] == 0 */
        /* you may need to loop data[] again and do strcpy to get the domain name */
        *qtype = (unsigned short) data[i+1];
        *qclass = (unsigned short) data[i+3];
        return /* domain name */;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: oracle PL/SQL: sort rows I run this query: Select a.product, sum( case
Possible Duplicate: array_splice() for associative arrays How to add an array value to the
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: How to call a JavaScript function from PHP? I have a php
Possible Duplicate: How do I determine if an array is initialized in VB6? I
Possible Duplicate: Why does ReSharper want to use 'var' for everything? I have ReSharper

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.