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 8124873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:36:42+00:00 2026-06-06T06:36:42+00:00

I am designing a DNS parsing library in C++. A DNS Packet has a

  • 0

I am designing a DNS parsing library in C++. A DNS Packet has a set of standard fields followed by a list of Resource Records that again has a set of standard fields followed by a RData field. The RData field is parsed based on the type field. Now, I specify a hierarchy for DNSRData, to handle various types. The code looks like something like this:

class DNSRData {
  virtual void ToString() = 0;
  virtual void Parse() = 0;
}

class DNSRData_A : public DNSRData {
  void ToString();
  void Parse();
  uint32_t GetIP();
}

class DNSRData_CNAME : public DNSRData {
  void ToString();
  void Parse();
   const char* GetAlias();
}

class DNSResourceRecord {
  /* Standard Fields 
   ..... */
  int      type_; // Specifies the format for rdata_
  DNSRData *rdata_; 
}

class DNSPacket {
  /* Standard Fields
  ....*/
  vector<DNSResourceRecord *> rr_list_;
}

Now this is the issue I have, each DNSRData record might have different fields. I do not want to add accessors for all the fields in the Base class, as they are present in some derived class and not in others e.g. IP address is present only in DNSRData_A and not in any other.

So, when I want to perform any operations on the DNSRData, I lookup the type and perform a downcast from DNSRData* to DNSRData_A*.

DNSRData *rdata = packet->GetResourceRecord().front(); //not really necessary for this example
if(resource_record.type == RR_CNAME) {
  DNSRData_CNAME *cname = (DNSRData_CNAME*)rdata;
} 

This can cause tons of issues later on, and as we add more types it is quickly becoming an unholy mess. Any ideas on how to solve this problem without adding all accessors to the Base class ?

EDIT:

Some more context, this is part of a high performance DNS trace parse library. A lot of the operations are done as we see packets on the wire. So, what would be an operation that messes up the design, lets say we get a DNSPacket and we parse it now we want to decide how to process it further based on the type.

if(type == RR_CNAME) {
  DNSRData_CNAME *cname = dynamic_cast<DNSRData_CNAME*>(&rdata);
  char *alias = cname->GetAlias();
}else if (type = RR_A) {
  DNSRData_A *a = dynamic_cast<DNSRData_A*>(&rdata);
  uint32_t ip = a->GetIP();
}

As you see there is a downcast involved, from the base type RData to a more specific RData type. I want to avoid this downcast and use maybe a design pattern to solve this problem.

  • 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-06T06:36:45+00:00Added an answer on June 6, 2026 at 6:36 am

    if i understand you correctly i think that the Visitor pattern is what you are looking for

    Visitor pattern.

    class GetInfoVisitor
      {
        void visit(DNSRData_A* a)
        {
            uint32_t ip = a->GetIP();
        }
        void visit(DNSRData_CNAME * cname) 
        {        
            char *alias = cname->GetAlias();
        }   
      }
    
    class DNSRData
    {
    
        void action(Visitor& visitor) 
        {
            visitor.visit(this);
        }
    }
    
    int main()
    {
     ...
     GetInfoVisitor getInfoVisitor;
     DNSRData *rdata = packet->GetResourceRecord().front();
     rdata->action(getInfoVisitor);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Designing forms has always been fun, but getting them to send email on the
When designing a C API for configuring a library/utility, I have a co-worker who
When designing a web service that will allow the consumer of the service to
for designing purposes i need to truncate all DB which has lots of FK's.
In designing class with multiple/nested has-a relationships, who should catch exceptions? I guess this
I'm designing a system in java which utilizes a dns lookup class. My question
Designing my stratified sample library(survey) design <- svydesign(id=~1,strata=~Category, data=billa, fpc=~fpc) So far so good,
Designing a simple HTML table that I am going to implement alternate-row coloring on...
In designing systems with hierarchical relationships, I often run into a problem that begs
When designing tables, I've developed a habit of having one column that is unique

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.