472,146 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

Parsing a Section of Binary String Data!

Hi,
The scenario is like this :
struct ns_rr {

const u_char* rdata;
};

The rdata field contains some fields such as :

char * flags;
char * services;
char * regexp;
char * replacement;

I want to parse the rdata section and obtain the individual string
fields as shown above. Can anyone suggest an efficient method? Are
these strings terminiated by '\0' within the rdata section?
Thanks and Regards,

Timmy Jose.

Mar 22 '06 #1
29 4074
"zoltan" <zo*********@gmail.com> writes:
Hi,
The scenario is like this :
struct ns_rr {

const u_char* rdata;
};
Is u_char a typedef for unsigned char? (If so, just using "unsigned
char" would be a lot clearer.)
The rdata field contains some fields such as :

char * flags;
char * services;
char * regexp;
char * replacement;
No, the rdata member (not field) is a pointer; it doesn't contain
anything. Are you talking about something that rdata points to?
I want to parse the rdata section and obtain the individual string
fields as shown above. Can anyone suggest an efficient method? Are
these strings terminiated by '\0' within the rdata section?


You want to parse the rdata section of what? Is this coming from a
file? Are we supposed to know what an "rdata section" is? A Google
search indicates that there is such a thing (or perhaps more than one
such things), but it's not part of the C programming language.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 22 '06 #2

zoltan wrote:
Hi,
The scenario is like this :
struct ns_rr {

const u_char* rdata;
};

The rdata field contains some fields such as :

char * flags;
char * services;
char * regexp;
char * replacement;

I want to parse the rdata section and obtain the individual string
fields as shown above. Can anyone suggest an efficient method? Are
these strings terminiated by '\0' within the rdata section?
Thanks and Regards,

Timmy Jose.


look at this:
struct ns_rr * a;
puts(a->rdata->flags);

Mar 22 '06 #3
Keith Thompson <ks***@mib.org> writes:
"zoltan" <zo*********@gmail.com> writes:
Hi,
The scenario is like this :
struct ns_rr {

const u_char* rdata;
};


Is u_char a typedef for unsigned char? (If so, just using "unsigned
char" would be a lot clearer.)
The rdata field contains some fields such as :

char * flags;
char * services;
char * regexp;
char * replacement;


No, the rdata member (not field) is a pointer; it doesn't contain
anything. Are you talking about something that rdata points to?

[...]

I realize that my assumption that u_char is a typedef for unsigned
char may have been unwarranted. If u_char is a typedef for a struct
type that contains the flags, services, regexp, and replacement
members, the question makes a bit more sense. (Though the name
"u_char" certainly doesn't point in that direction.)

But it would still be very helful to show us the actual declarations
and a better idea of what you want to do with them.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 22 '06 #4

Keith Thompson wrote:
"zoltan" <zo*********@gmail.com> writes:
Hi,
The scenario is like this :
struct ns_rr {

const u_char* rdata;
};


Is u_char a typedef for unsigned char? (If so, just using "unsigned
char" would be a lot clearer.)
The rdata field contains some fields such as :

char * flags;
char * services;
char * regexp;
char * replacement;


No, the rdata member (not field) is a pointer; it doesn't contain
anything. Are you talking about something that rdata points to?
I want to parse the rdata section and obtain the individual string
fields as shown above. Can anyone suggest an efficient method? Are
these strings terminiated by '\0' within the rdata section?


You want to parse the rdata section of what? Is this coming from a
file? Are we supposed to know what an "rdata section" is? A Google
search indicates that there is such a thing (or perhaps more than one
such things), but it's not part of the C programming language.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

ns_rr is a structure which defines the type of the Resource Record (RR)
as used for DNS Queries. The structure is defined in <arpa/nameser.h> .

The rdata "member" points to memory where the various mentioned fields
are stored. This is because the contents of rdata depend on the type of
Resource Record.

So suppose I have a structure of this type ns_rr, how can I extract the
individual fields of the rdata member? Simple query.

Mar 22 '06 #5

do*********@126.com wrote:
zoltan wrote:
Hi,
The scenario is like this :
struct ns_rr {

const u_char* rdata;
};

The rdata field contains some fields such as :

char * flags;
char * services;
char * regexp;
char * replacement;

I want to parse the rdata section and obtain the individual string
fields as shown above. Can anyone suggest an efficient method? Are
these strings terminiated by '\0' within the rdata section?
Thanks and Regards,

Timmy Jose.

look at this:
struct ns_rr * a;
puts(a->rdata->flags);


The problem is this :

ns_rr is the Standard type which is used to store the data.

I know that the various fields ( flag, services etc) are in the rdata
member. So to extract them, I define my own structure like this :

struct NAPTR
{

char * flags;
char * services;
char * regexp;
char * replacement;
};

So the compiler has no idea that there is a field called "flags" inside
the rdata member!!! That is the trouble... Any ideas?

Mar 22 '06 #6
"zoltan" <zo*********@gmail.com> writes:
Keith Thompson wrote:

[...]
You want to parse the rdata section of what? Is this coming from a
file? Are we supposed to know what an "rdata section" is? A Google
search indicates that there is such a thing (or perhaps more than one
such things), but it's not part of the C programming language.


ns_rr is a structure which defines the type of the Resource Record (RR)
as used for DNS Queries. The structure is defined in <arpa/nameser.h> .


You need to ask in a system-specific newsgroup, (perhaps
comp.unix.programmer).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 22 '06 #7
zoltan wrote:
do*********@126.com wrote:
zoltan wrote:
The scenario is like this :

struct ns_rr {
const u_char* rdata;
};

The rdata field contains some fields such as :

char * flags;
char * services;
char * regexp;
char * replacement;

I want to parse the rdata section and obtain the individual string
fields as shown above. Can anyone suggest an efficient method? Are
these strings terminiated by '\0' within the rdata section?

you tell me.

<snip>
The problem is this :

ns_rr is the Standard type which is used to store the data.

I know that the various fields ( flag, services etc) are in the rdata
member. So to extract them, I define my own structure like this :

struct NAPTR
{

char * flags;
char * services;
char * regexp;
char * replacement;
};

So the compiler has no idea that there is a field called "flags" inside
the rdata member!!! That is the trouble... Any ideas?


I've read your posts and I still havn't a clue what you're talking
about.
Could you post a small, complete, compilable example. If it doesn't do
what you want then explain what you expected it to do and what it
actually does. Please include definitions of all data structures used.
--
Nick Keighley

Mar 22 '06 #8
zoltan wrote:
Hi,
The scenario is like this :
struct ns_rr {

const u_char* rdata;
};
What is u_char?
I'll assume it's a struct with (possibly among others) these members:
The rdata field contains some fields such as :

char * flags;
char * services;
char * regexp;
char * replacement;

I want to parse the rdata section and obtain the individual string
fields as shown above. Can anyone suggest an efficient method? Are
these strings terminiated by '\0' within the rdata section?


I have absolutely no idea. If they are, you can use standard string
handling functions to mess with them; if they are not you should take
extra care when dealing with those members.

Here's a short program that tries to put together the information you
provided and uses the individual members of `rdata`.
#include <stdio.h>

struct u_char { /* bad choice of name */
char * flags;
char * services;
char * regexp;
char * replacement;
};

struct ns_rr {
const struct u_char * rdata;
};

int main(void) {
struct u_char dummy = {"flags", "services", "regexp", "replacement"};
struct ns_rr x;

x.rdata = &dummy;
printf("x.rdata->flags is '%s'.\n", x.rdata->flags);
printf("x.rdata->services is '%s'.\n", x.rdata->services);
printf("x.rdata->regexp is '%s'.\n", x.rdata->regexp);
printf("x.rdata->replacement is '%s'.\n", x.rdata->replacement);

return 0;
}
--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Mar 22 '06 #9

Nick Keighley wrote:
zoltan wrote:
do*********@126.com wrote:
zoltan wrote: The scenario is like this :
>
> struct ns_rr {
> const u_char* rdata;
> };
>
> The rdata field contains some fields such as :
>
> char * flags;
> char * services;
> char * regexp;
> char * replacement;
>
> I want to parse the rdata section and obtain the individual string
> fields as shown above. Can anyone suggest an efficient method? Are
> these strings terminiated by '\0' within the rdata section?


you tell me.

<snip>
The problem is this :

ns_rr is the Standard type which is used to store the data.

I know that the various fields ( flag, services etc) are in the rdata
member. So to extract them, I define my own structure like this :

struct NAPTR
{

char * flags;
char * services;
char * regexp;
char * replacement;
};

So the compiler has no idea that there is a field called "flags" inside
the rdata member!!! That is the trouble... Any ideas?


I've read your posts and I still havn't a clue what you're talking
about.
Could you post a small, complete, compilable example. If it doesn't do
what you want then explain what you expected it to do and what it
actually does. Please include definitions of all data structures used.
--
Nick Keighley


Okay, Here are the actual structures :

I am supposed to implement a DNS Query. The Information to be processed
resides in RRs ( Resource Records). The header <arpa/nameser.h>
contains the following structure to store the data of these RRs as :

typedef struct __ns_rr {
char name[NS_MAXDNAME];
uint16_t type;
uint16_t rr_class;
uint32_t ttl;
uint16_t rdlength;
const uchar_t *rdata;
} ns_rr;

Now, the rdata part can have several fields depending on the specific
type of Record.
For instance, the SRV type has only the following fields : priority,
weight, port ( all of unsigned int type) and a target string ( the
hostname).

For my requirement, I have to use the data stored in a NAPTR record.
The "rdata" contains the following fields for this type of record :

order, preference ( unsigned int) and
flags, services, regexp and replacement ( all of type char *).

Now, because the definition of the rdata in the ns_rr structure is
generic, I have defined ( as I need to ) a structure as follows, to
store the actual data :

struct NAPTR
{
unsigned int order;
unsigned int preference;
char * flags;
char * services;
char * regexp;
char * replacement;
};
So my question is this : How can I extract the string fields from the
rdata portion of the ns_rr structure and store them into the respective
fields in my user-defined structure?

The integer fields ( order and preference ) are easy enough, being of
fixed size. What about the strings which can be of variable size?

I hope that is clear enough to elicit a suggestion or a positive
response in the least!

Regards,

Timmy Jose.

Mar 22 '06 #10

Pedro Graca wrote:
zoltan wrote:
Hi,
The scenario is like this :
struct ns_rr {

const u_char* rdata;
};


What is u_char?
I'll assume it's a struct with (possibly among others) these members:
The rdata field contains some fields such as :

char * flags;
char * services;
char * regexp;
char * replacement;

I want to parse the rdata section and obtain the individual string
fields as shown above. Can anyone suggest an efficient method? Are
these strings terminiated by '\0' within the rdata section?


I have absolutely no idea. If they are, you can use standard string
handling functions to mess with them; if they are not you should take
extra care when dealing with those members.

Here's a short program that tries to put together the information you
provided and uses the individual members of `rdata`.
#include <stdio.h>

struct u_char { /* bad choice of name */
char * flags;
char * services;
char * regexp;
char * replacement;
};

struct ns_rr {
const struct u_char * rdata;
};

int main(void) {
struct u_char dummy = {"flags", "services", "regexp", "replacement"};
struct ns_rr x;

x.rdata = &dummy;
printf("x.rdata->flags is '%s'.\n", x.rdata->flags);
printf("x.rdata->services is '%s'.\n", x.rdata->services);
printf("x.rdata->regexp is '%s'.\n", x.rdata->regexp);
printf("x.rdata->replacement is '%s'.\n", x.rdata->replacement);

return 0;
}
--
If you're posting through Google read <http://cfaj.freeshell.org/google>


Hi Pedro,
Thanks for the effort taken to write the program! I'm afraid that it
doesn't quite answer my question....

u_char is unsigned char ( I found it used in Solaris systems... yes, I
know those people are weird!)

so it is just a character pointer basically....

I have explained the whole problem in clearer terms in the reply to the
previous post. Kindly see that!

Regards,

Timmy Jose.

Mar 22 '06 #11
zoltan wrote:
Nick Keighley wrote:
zoltan wrote:
do*********@126.com wrote:
zoltan wrote:
> The scenario is like this :
>
> struct ns_rr {
> const u_char* rdata;
> };
>
> The rdata field contains some fields such as :
>
> char * flags;
> char * services;
> char * regexp;
> char * replacement;
>
> I want to parse the rdata section and obtain the individual
> string fields as shown above. Can anyone suggest an efficient
> method? Are these strings terminiated by '\0' within the rdata
> section?


you tell me.

<snip>
The problem is this :

ns_rr is the Standard type which is used to store the data.

I know that the various fields ( flag, services etc) are in the
rdata member. So to extract them, I define my own structure like
this :

struct NAPTR
{

char * flags;
char * services;
char * regexp;
char * replacement;
};

So the compiler has no idea that there is a field called "flags"
inside the rdata member!!! That is the trouble... Any ideas?


I've read your posts and I still havn't a clue what you're talking
about.
Could you post a small, complete, compilable example. If it doesn't
do what you want then explain what you expected it to do and what it
actually does. Please include definitions of all data structures
used.
--
Nick Keighley


Okay, Here are the actual structures :

I am supposed to implement a DNS Query. The Information to be
processed resides in RRs ( Resource Records). The header
<arpa/nameser.h> contains the following structure to store the data
of these RRs as :

typedef struct __ns_rr {
char name[NS_MAXDNAME];
uint16_t type;
uint16_t rr_class;
uint32_t ttl;
uint16_t rdlength;
const uchar_t *rdata;
} ns_rr;

Now, the rdata part can have several fields depending on the specific
type of Record.
For instance, the SRV type has only the following fields : priority,
weight, port ( all of unsigned int type) and a target string ( the
hostname).

For my requirement, I have to use the data stored in a NAPTR record.
The "rdata" contains the following fields for this type of record :

order, preference ( unsigned int) and
flags, services, regexp and replacement ( all of type char *).

Now, because the definition of the rdata in the ns_rr structure is
generic, I have defined ( as I need to ) a structure as follows, to
store the actual data :

struct NAPTR
{
unsigned int order;
unsigned int preference;
char * flags;
char * services;
char * regexp;
char * replacement;
};
So my question is this : How can I extract the string fields from the
rdata portion of the ns_rr structure and store them into the
respective fields in my user-defined structure?

The integer fields ( order and preference ) are easy enough, being of
fixed size. What about the strings which can be of variable size?

I hope that is clear enough to elicit a suggestion or a positive
response in the least!

Can't say that I've really seen anything other than people trying to help
you here - and asking you to clarify what it is that's the problem.

But for a suggestion ...

Googling for

priority weight port SRV regexp

turned up

http://www.cisco.com/en/US/products/...080155722.html
This in turn contains stuff on NAPTR

Which gives some examples.

Then googling for

parse NAPTR

Turns up what could be a useful page for you - see these on
http://www.asterisk.org/doxygen/enum_8c.html

static int parse_ie (char *data, int maxdatalen, char *src, int srclen)
Parse NAPTR record information elements.

static int parse_naptr (char *dst, int dstsize, char *tech, int techsize,
char *answer, int len, char *naptrinput) Parse DNS NAPTR record used in
ENUM ---.
Does that help??
--
==============
Not a pedant
==============
Mar 22 '06 #12

pemo wrote:
zoltan wrote:
Nick Keighley wrote:
zoltan wrote:
do*********@126.com wrote:
> zoltan wrote:

>> The scenario is like this :
>>
>> struct ns_rr {
>> const u_char* rdata;
>> };
>>
>> The rdata field contains some fields such as :
>>
>> char * flags;
>> char * services;
>> char * regexp;
>> char * replacement;
>>
>> I want to parse the rdata section and obtain the individual
>> string fields as shown above. Can anyone suggest an efficient
>> method? Are these strings terminiated by '\0' within the rdata
>> section?

you tell me.

<snip>

The problem is this :

ns_rr is the Standard type which is used to store the data.

I know that the various fields ( flag, services etc) are in the
rdata member. So to extract them, I define my own structure like
this :

struct NAPTR
{

char * flags;
char * services;
char * regexp;
char * replacement;
};

So the compiler has no idea that there is a field called "flags"
inside the rdata member!!! That is the trouble... Any ideas?

I've read your posts and I still havn't a clue what you're talking
about.
Could you post a small, complete, compilable example. If it doesn't
do what you want then explain what you expected it to do and what it
actually does. Please include definitions of all data structures
used.
--
Nick Keighley


Okay, Here are the actual structures :

I am supposed to implement a DNS Query. The Information to be
processed resides in RRs ( Resource Records). The header
<arpa/nameser.h> contains the following structure to store the data
of these RRs as :

typedef struct __ns_rr {
char name[NS_MAXDNAME];
uint16_t type;
uint16_t rr_class;
uint32_t ttl;
uint16_t rdlength;
const uchar_t *rdata;
} ns_rr;

Now, the rdata part can have several fields depending on the specific
type of Record.
For instance, the SRV type has only the following fields : priority,
weight, port ( all of unsigned int type) and a target string ( the
hostname).

For my requirement, I have to use the data stored in a NAPTR record.
The "rdata" contains the following fields for this type of record :

order, preference ( unsigned int) and
flags, services, regexp and replacement ( all of type char *).

Now, because the definition of the rdata in the ns_rr structure is
generic, I have defined ( as I need to ) a structure as follows, to
store the actual data :

struct NAPTR
{
unsigned int order;
unsigned int preference;
char * flags;
char * services;
char * regexp;
char * replacement;
};
So my question is this : How can I extract the string fields from the
rdata portion of the ns_rr structure and store them into the
respective fields in my user-defined structure?

The integer fields ( order and preference ) are easy enough, being of
fixed size. What about the strings which can be of variable size?

I hope that is clear enough to elicit a suggestion or a positive
response in the least!

Can't say that I've really seen anything other than people trying to help
you here - and asking you to clarify what it is that's the problem.

But for a suggestion ...

Googling for

priority weight port SRV regexp

turned up

http://www.cisco.com/en/US/products/...080155722.html
This in turn contains stuff on NAPTR

Which gives some examples.

Then googling for

parse NAPTR

Turns up what could be a useful page for you - see these on
http://www.asterisk.org/doxygen/enum_8c.html

static int parse_ie (char *data, int maxdatalen, char *src, int srclen)
Parse NAPTR record information elements.

static int parse_naptr (char *dst, int dstsize, char *tech, int techsize,
char *answer, int len, char *naptrinput) Parse DNS NAPTR record used in
ENUM ---.
Does that help??
--
==============
Not a pedant
==============


Hi,
The second link was quite helpful. It at least gives some idea
about the alternatives. Thanks for the Googling.

Regards,

Timmy Jose.

Mar 22 '06 #13
zoltan wrote:
Okay, Here are the actual structures :

I am supposed to implement a DNS Query. The Information to be processed
resides in RRs ( Resource Records). The header <arpa/nameser.h>
contains the following structure to store the data of these RRs as :

typedef struct __ns_rr {
char name[NS_MAXDNAME];
uint16_t type;
uint16_t rr_class;
uint32_t ttl;
uint16_t rdlength;
const uchar_t *rdata;
} ns_rr;

Now, the rdata part can have several fields depending on the specific
type of Record.
For instance, the SRV type has only the following fields : priority,
weight, port ( all of unsigned int type) and a target string ( the
hostname).

For my requirement, I have to use the data stored in a NAPTR record.
The "rdata" contains the following fields for this type of record :

order, preference ( unsigned int) and
flags, services, regexp and replacement ( all of type char *).

Now, because the definition of the rdata in the ns_rr structure is
generic, I have defined ( as I need to ) a structure as follows, to
store the actual data :

struct NAPTR
{
unsigned int order;
unsigned int preference;
char * flags;
char * services;
char * regexp;
char * replacement;
};
So my question is this : How can I extract the string fields from the
rdata portion of the ns_rr structure and store them into the respective
fields in my user-defined structure?
It depends on how the data in rdata is structured.

rdata is a pointer to unsigned char, hiding the real data it holds.
You have to come up with a way to get that data into suitable C objects.

I didn't follow the links posted else-thread (where probably the rdata
structure is documented) so I'll just make it up:
rdata = "\x01\x02\x03\x04" /* order (32-bit unsigned int) */
"\x05\x06\x07\x08" /* preference (32-bit unsigned int) */
"\x41\x00" /* flags (NUL terminated string) */
"\x54\x43\x50\x00" /* services (NUL terminated string) */
"\x28\x2E\x2A\x29\x00" /* regexp (NUL terminated string) */
"\x3d\x00"; /* replacement (NUL terminated string) */

struct NAPTR data;
/* assuming `unsigned int` bit representation is LSB first */
data.order = rdata[0] + (rdata[1]<<8) + (rdata[2]<<16) + (rdata[3]<<24);
data.preference = rdata[4] + (rdata[5]<<8) + (rdata[6]<<16) + (rdata[7]<<24);
data.flags = rdata+8;
data.services = data.flags + strlen(data.flags) + 1;
data.regexp = data.services + strlen(data.services) + 1;
data.replacement = data.regexp + strlen(data.regexp) + 1;
The integer fields ( order and preference ) are easy enough, being of
fixed size. What about the strings which can be of variable size?


--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Mar 22 '06 #14
Pedro Graca failed to state the assumptions:
I'll just make it up:
Assuming CHAR_BIT == 8
and sizeof(unsigned int) == 4

assert(CHAR_BIT == 8);
assert(sizeof(unsigned int) == 4);
rdata = "\x01\x02\x03\x04" /* order (32-bit unsigned int) */

[snip]

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Mar 22 '06 #15
zoltan wrote:
Nick Keighley wrote:
zoltan wrote:
do*********@126.com wrote:
> zoltan wrote: > > The scenario is like this :
> >
> > struct ns_rr {
> > const u_char* rdata;
> > };
> >
> > The rdata field contains some fields such as :
> >
> > char * flags;
> > char * services;
> > char * regexp;
> > char * replacement;
> >
> > I want to parse the rdata section and obtain the individual string
> > fields as shown above. Can anyone suggest an efficient method? Are
> > these strings terminiated by '\0' within the rdata section?


you tell me.

<snip>
The problem is this :

ns_rr is the Standard type which is used to store the data.

I know that the various fields ( flag, services etc) are in the rdata
member. So to extract them, I define my own structure like this :

struct NAPTR
{

char * flags;
char * services;
char * regexp;
char * replacement;
};

So the compiler has no idea that there is a field called "flags" inside
the rdata member!!! That is the trouble... Any ideas?


I've read your posts and I still havn't a clue what you're talking
about.
Could you post a small, complete, compilable example. If it doesn't do
what you want then explain what you expected it to do and what it
actually does. Please include definitions of all data structures used.
--
Nick Keighley


Okay, Here are the actual structures :

I am supposed to implement a DNS Query. The Information to be processed
resides in RRs ( Resource Records). The header <arpa/nameser.h>
contains the following structure to store the data of these RRs as :

typedef struct __ns_rr {
char name[NS_MAXDNAME];
uint16_t type;
uint16_t rr_class;
uint32_t ttl;
uint16_t rdlength;
const uchar_t *rdata;
} ns_rr;

Now, the rdata part can have several fields depending on the specific
type of Record.
For instance, the SRV type has only the following fields : priority,
weight, port ( all of unsigned int type) and a target string ( the
hostname).

For my requirement, I have to use the data stored in a NAPTR record.
The "rdata" contains the following fields for this type of record :

order, preference ( unsigned int) and
flags, services, regexp and replacement ( all of type char *).

Now, because the definition of the rdata in the ns_rr structure is
generic, I have defined ( as I need to ) a structure as follows, to
store the actual data :

struct NAPTR
{
unsigned int order;
unsigned int preference;
char * flags;
char * services;
char * regexp;
char * replacement;
};
So my question is this : How can I extract the string fields from the
rdata portion of the ns_rr structure and store them into the respective
fields in my user-defined structure?

The integer fields ( order and preference ) are easy enough, being of
fixed size. What about the strings which can be of variable size?

I hope that is clear enough to elicit a suggestion or a positive
response in the least!


I gather uchar_t is an unsigned char. How about:-

struct NAPTR *naptr;
ns_rr ns_rr_data;

/* load somehow ns_rr_data */

naptr = (struct NAPTR*)ns_rr_data.rdata;
printf ("services = \"%s\"\n", naptr->services);

this assumes your structure is laid out in the same way as the data in
ns_rr.
Check if they define something like your struct NAPTR and use that.
--
Nick Keighley

Mar 23 '06 #16

Nick Keighley wrote:
zoltan wrote:
Nick Keighley wrote:
zoltan wrote:
> do*********@126.com wrote:
> > zoltan wrote: > > The scenario is like this :
> > >
> > > struct ns_rr {
> > > const u_char* rdata;
> > > };
> > >
> > > The rdata field contains some fields such as :
> > >
> > > char * flags;
> > > char * services;
> > > char * regexp;
> > > char * replacement;
> > >
> > > I want to parse the rdata section and obtain the individual string
> > > fields as shown above. Can anyone suggest an efficient method? Are
> > > these strings terminiated by '\0' within the rdata section?

you tell me.

<snip>

> The problem is this :
>
> ns_rr is the Standard type which is used to store the data.
>
> I know that the various fields ( flag, services etc) are in the rdata
> member. So to extract them, I define my own structure like this :
>
> struct NAPTR
> {
>
> char * flags;
> char * services;
> char * regexp;
> char * replacement;
> };
>
> So the compiler has no idea that there is a field called "flags" inside
> the rdata member!!! That is the trouble... Any ideas?

I've read your posts and I still havn't a clue what you're talking
about.
Could you post a small, complete, compilable example. If it doesn't do
what you want then explain what you expected it to do and what it
actually does. Please include definitions of all data structures used.
--
Nick Keighley


Okay, Here are the actual structures :

I am supposed to implement a DNS Query. The Information to be processed
resides in RRs ( Resource Records). The header <arpa/nameser.h>
contains the following structure to store the data of these RRs as :

typedef struct __ns_rr {
char name[NS_MAXDNAME];
uint16_t type;
uint16_t rr_class;
uint32_t ttl;
uint16_t rdlength;
const uchar_t *rdata;
} ns_rr;

Now, the rdata part can have several fields depending on the specific
type of Record.
For instance, the SRV type has only the following fields : priority,
weight, port ( all of unsigned int type) and a target string ( the
hostname).

For my requirement, I have to use the data stored in a NAPTR record.
The "rdata" contains the following fields for this type of record :

order, preference ( unsigned int) and
flags, services, regexp and replacement ( all of type char *).

Now, because the definition of the rdata in the ns_rr structure is
generic, I have defined ( as I need to ) a structure as follows, to
store the actual data :

struct NAPTR
{
unsigned int order;
unsigned int preference;
char * flags;
char * services;
char * regexp;
char * replacement;
};
So my question is this : How can I extract the string fields from the
rdata portion of the ns_rr structure and store them into the respective
fields in my user-defined structure?

The integer fields ( order and preference ) are easy enough, being of
fixed size. What about the strings which can be of variable size?

I hope that is clear enough to elicit a suggestion or a positive
response in the least!


I gather uchar_t is an unsigned char. How about:-

struct NAPTR *naptr;
ns_rr ns_rr_data;

/* load somehow ns_rr_data */

naptr = (struct NAPTR*)ns_rr_data.rdata;
printf ("services = \"%s\"\n", naptr->services);

this assumes your structure is laid out in the same way as the data in
ns_rr.
Check if they define something like your struct NAPTR and use that.
--
Nick Keighley


Yes Nick, the data is laid out in the pattern defined in the
corresponding RFC - RFC 2915. So it can be safely assumed that the
data is laid out in the same way in rdata.

This actually seems like a very good option. The testing machine is
down, so I will have to wait a bit to try it out... but thanks so much
for the suggestion!! Will post bacl here with the results...

Thanks and regards,

Timmy Jose.

Mar 24 '06 #17

Nick Keighley wrote:
zoltan wrote:
Nick Keighley wrote:
zoltan wrote:
> do*********@126.com wrote:
> > zoltan wrote: > > The scenario is like this :
> > >
> > > struct ns_rr {
> > > const u_char* rdata;
> > > };
> > >
> > > The rdata field contains some fields such as :
> > >
> > > char * flags;
> > > char * services;
> > > char * regexp;
> > > char * replacement;
> > >
> > > I want to parse the rdata section and obtain the individual string
> > > fields as shown above. Can anyone suggest an efficient method? Are
> > > these strings terminiated by '\0' within the rdata section?

you tell me.

<snip>

> The problem is this :
>
> ns_rr is the Standard type which is used to store the data.
>
> I know that the various fields ( flag, services etc) are in the rdata
> member. So to extract them, I define my own structure like this :
>
> struct NAPTR
> {
>
> char * flags;
> char * services;
> char * regexp;
> char * replacement;
> };
>
> So the compiler has no idea that there is a field called "flags" inside
> the rdata member!!! That is the trouble... Any ideas?

I've read your posts and I still havn't a clue what you're talking
about.
Could you post a small, complete, compilable example. If it doesn't do
what you want then explain what you expected it to do and what it
actually does. Please include definitions of all data structures used.
--
Nick Keighley


Okay, Here are the actual structures :

I am supposed to implement a DNS Query. The Information to be processed
resides in RRs ( Resource Records). The header <arpa/nameser.h>
contains the following structure to store the data of these RRs as :

typedef struct __ns_rr {
char name[NS_MAXDNAME];
uint16_t type;
uint16_t rr_class;
uint32_t ttl;
uint16_t rdlength;
const uchar_t *rdata;
} ns_rr;

Now, the rdata part can have several fields depending on the specific
type of Record.
For instance, the SRV type has only the following fields : priority,
weight, port ( all of unsigned int type) and a target string ( the
hostname).

For my requirement, I have to use the data stored in a NAPTR record.
The "rdata" contains the following fields for this type of record :

order, preference ( unsigned int) and
flags, services, regexp and replacement ( all of type char *).

Now, because the definition of the rdata in the ns_rr structure is
generic, I have defined ( as I need to ) a structure as follows, to
store the actual data :

struct NAPTR
{
unsigned int order;
unsigned int preference;
char * flags;
char * services;
char * regexp;
char * replacement;
};
So my question is this : How can I extract the string fields from the
rdata portion of the ns_rr structure and store them into the respective
fields in my user-defined structure?

The integer fields ( order and preference ) are easy enough, being of
fixed size. What about the strings which can be of variable size?

I hope that is clear enough to elicit a suggestion or a positive
response in the least!


I gather uchar_t is an unsigned char. How about:-

struct NAPTR *naptr;
ns_rr ns_rr_data;

/* load somehow ns_rr_data */

naptr = (struct NAPTR*)ns_rr_data.rdata;
printf ("services = \"%s\"\n", naptr->services);

this assumes your structure is laid out in the same way as the data in
ns_rr.
Check if they define something like your struct NAPTR and use that.
--
Nick Keighley

Hi,
To test the hypothesis, I wrote this sample program.... Can u tell
me why it does not work? The program segment faults... ( after the
printf("%s",record.rdata); statement).
// Program to test the rdata parsing capability.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <malloc.h>

struct NAPTR
{

unsigned int order;
unsigned int preference;
char * flags;
char * services;
char * regexp;
char * replacement;
};

struct nsrr
{

char name[255];
unsigned type;
unsigned class;
unsigned ttl;
unsigned rdlength;
char *rdata;
};
int main(void)
{

struct NAPTR *naptr;
struct nsrr record;
record.rdata=(char*)malloc(1024);

strcpy(record.name,"www.foo.com");
record.type=1;
record.class=1;
record.ttl=8640;
strcpy(record.rdata,"100 10 s sip+e2u !http://[^/:](.*)!\1!i
www.foo.com");
record.rdlength=sizeof(record.rdata);

printf("%s",record.rdata);

naptr=(struct NAPTR*)(record.rdata);

printf("\nThe Record contents are : ");

printf("%s",naptr->flags);
printf("\n%s",naptr->services);
printf("\n%s",naptr->regexp);
printf("\n%s",naptr->replacement);
return 0;
}

Thanks and Regards,

Timmy Jose.

Mar 24 '06 #18
zoltan wrote:
Nick Keighley wrote:
zoltan wrote:
Nick Keighley wrote:
> zoltan wrote:
> > do*********@126.com wrote:
> > > zoltan wrote:
<snip>
I am supposed to implement a DNS Query. The Information to be processed
resides in RRs ( Resource Records). The header <arpa/nameser.h>
contains the following structure to store the data of these RRs as :

typedef struct __ns_rr {
char name[NS_MAXDNAME];
uint16_t type;
uint16_t rr_class;
uint32_t ttl;
uint16_t rdlength;
const uchar_t *rdata;
} ns_rr;

Now, the rdata part can have several fields depending on the specific
type of Record.
For instance, the SRV type has only the following fields : priority,
weight, port ( all of unsigned int type) and a target string ( the
hostname).

For my requirement, I have to use the data stored in a NAPTR record.
The "rdata" contains the following fields for this type of record :

order, preference ( unsigned int) and
flags, services, regexp and replacement ( all of type char *).

Now, because the definition of the rdata in the ns_rr structure is
generic, I have defined ( as I need to ) a structure as follows, to
store the actual data :

struct NAPTR
{
unsigned int order;
unsigned int preference;
char * flags;
char * services;
char * regexp;
char * replacement;
};
So my question is this : How can I extract the string fields from the
rdata portion of the ns_rr structure and store them into the respective
fields in my user-defined structure?

The integer fields ( order and preference ) are easy enough, being of
fixed size. What about the strings which can be of variable size?

I hope that is clear enough to elicit a suggestion or a positive
response in the least!
I gather uchar_t is an unsigned char. How about:-

struct NAPTR *naptr;
ns_rr ns_rr_data;

/* load somehow ns_rr_data */

naptr = (struct NAPTR*)ns_rr_data.rdata;
printf ("services = \"%s\"\n", naptr->services);

this assumes your structure is laid out in the same way as the data in
ns_rr.
Check if they define something like your struct NAPTR and use that.


To test the hypothesis, I wrote this sample program.... Can u tell
me why it does not work? The program segment faults... ( after the
printf("%s",record.rdata); statement).
// Program to test the rdata parsing capability.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <malloc.h>

struct NAPTR
{

unsigned int order;
unsigned int preference;
char * flags;
char * services;
char * regexp;
char * replacement;
};

struct nsrr
{

char name[255];
unsigned type;
unsigned class;
unsigned ttl;
unsigned rdlength;
char *rdata;
};
int main(void)
{

struct NAPTR *naptr;
struct nsrr record;

record.rdata=(char*)malloc(1024);


cast not needed. Test for malloc failure
strcpy(record.name,"www.foo.com");

record.type=1;
record.class=1;
record.ttl=8640;
strcpy(record.rdata,"100 10 s sip+e2u !http://[^/:](.*)!\1!i
www.foo.com");
record.rdlength=sizeof(record.rdata);
try your debugger (I can't believe I said that...). The rdata doesn't
seem
to be in the same format as NAPTR...

printf("%s",record.rdata);

naptr=(struct NAPTR*)(record.rdata);
this trick is dangerous if the data is not formatted as struct NAPTR.
and yours isn't
printf("\nThe Record contents are : ");

printf("%s",naptr->flags);
printf("\n%s",naptr->services);
printf("\n%s",naptr->regexp);
printf("\n%s",naptr->replacement);
return 0;
}

--
Nick Keighley

Mar 24 '06 #19

Nick Keighley wrote:
zoltan wrote:
Nick Keighley wrote:
zoltan wrote:
> Nick Keighley wrote:
> > zoltan wrote:
> > > do*********@126.com wrote:
> > > > zoltan wrote:
<snip>
I am supposed to implement a DNS Query. The Information to be processed
> resides in RRs ( Resource Records). The header <arpa/nameser.h>
> contains the following structure to store the data of these RRs as :
>
> typedef struct __ns_rr {
> char name[NS_MAXDNAME];
> uint16_t type;
> uint16_t rr_class;
> uint32_t ttl;
> uint16_t rdlength;
> const uchar_t *rdata;
> } ns_rr;
>
> Now, the rdata part can have several fields depending on the specific
> type of Record.
> For instance, the SRV type has only the following fields : priority,
> weight, port ( all of unsigned int type) and a target string ( the
> hostname).
>
> For my requirement, I have to use the data stored in a NAPTR record.
> The "rdata" contains the following fields for this type of record :
>
> order, preference ( unsigned int) and
> flags, services, regexp and replacement ( all of type char *).
>
> Now, because the definition of the rdata in the ns_rr structure is
> generic, I have defined ( as I need to ) a structure as follows, to
> store the actual data :
>
> struct NAPTR
> {
> unsigned int order;
> unsigned int preference;
> char * flags;
> char * services;
> char * regexp;
> char * replacement;
> };
>
>
> So my question is this : How can I extract the string fields from the
> rdata portion of the ns_rr structure and store them into the respective
> fields in my user-defined structure?
>
> The integer fields ( order and preference ) are easy enough, being of
> fixed size. What about the strings which can be of variable size?
>
> I hope that is clear enough to elicit a suggestion or a positive
> response in the least!

I gather uchar_t is an unsigned char. How about:-

struct NAPTR *naptr;
ns_rr ns_rr_data;

/* load somehow ns_rr_data */

naptr = (struct NAPTR*)ns_rr_data.rdata;
printf ("services = \"%s\"\n", naptr->services);

this assumes your structure is laid out in the same way as the data in
ns_rr.
Check if they define something like your struct NAPTR and use that.


To test the hypothesis, I wrote this sample program.... Can u tell
me why it does not work? The program segment faults... ( after the
printf("%s",record.rdata); statement).
// Program to test the rdata parsing capability.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <malloc.h>

struct NAPTR
{

unsigned int order;
unsigned int preference;
char * flags;
char * services;
char * regexp;
char * replacement;
};

struct nsrr
{

char name[255];
unsigned type;
unsigned class;
unsigned ttl;
unsigned rdlength;
char *rdata;
};
int main(void)
{

struct NAPTR *naptr;
struct nsrr record;

record.rdata=(char*)malloc(1024);


cast not needed. Test for malloc failure
strcpy(record.name,"www.foo.com");

record.type=1;
record.class=1;
record.ttl=8640;
strcpy(record.rdata,"100 10 s sip+e2u !http://[^/:](.*)!\1!i
www.foo.com");
record.rdlength=sizeof(record.rdata);


try your debugger (I can't believe I said that...). The rdata doesn't
seem
to be in the same format as NAPTR...

printf("%s",record.rdata);

naptr=(struct NAPTR*)(record.rdata);


this trick is dangerous if the data is not formatted as struct NAPTR.
and yours isn't
printf("\nThe Record contents are : ");

printf("%s",naptr->flags);
printf("\n%s",naptr->services);
printf("\n%s",naptr->regexp);
printf("\n%s",naptr->replacement);
return 0;
}

--
Nick Keighley


I suppose u are referring to the fact that I havent' mentioned anything
about the Domain Name, class etc.

The structure of the NAPTR record acc to RFC 2915 is :

Domain TTL Type Class Order Preference Flags Services Regexp
Replacement. The ns_rr structure defined is <arpa/nameser.h> actually
has separate fields for Domain, TTL, Type and Class. These are
considered as the Header. The remaining fields form the rdata portion.

I just require to extract the fields in the rdata section, so I wrote
this simple program. How is the RDATA section not the same as the one
in the example?

Thanks and Regards,

Timmy Jose.

Mar 24 '06 #20

Nick Keighley wrote:
zoltan wrote:
Nick Keighley wrote:
zoltan wrote:
> Nick Keighley wrote:
> > zoltan wrote:
> > > do*********@126.com wrote:
> > > > zoltan wrote:
<snip>
I am supposed to implement a DNS Query. The Information to be processed
> resides in RRs ( Resource Records). The header <arpa/nameser.h>
> contains the following structure to store the data of these RRs as :
>
> typedef struct __ns_rr {
> char name[NS_MAXDNAME];
> uint16_t type;
> uint16_t rr_class;
> uint32_t ttl;
> uint16_t rdlength;
> const uchar_t *rdata;
> } ns_rr;
>
> Now, the rdata part can have several fields depending on the specific
> type of Record.
> For instance, the SRV type has only the following fields : priority,
> weight, port ( all of unsigned int type) and a target string ( the
> hostname).
>
> For my requirement, I have to use the data stored in a NAPTR record.
> The "rdata" contains the following fields for this type of record :
>
> order, preference ( unsigned int) and
> flags, services, regexp and replacement ( all of type char *).
>
> Now, because the definition of the rdata in the ns_rr structure is
> generic, I have defined ( as I need to ) a structure as follows, to
> store the actual data :
>
> struct NAPTR
> {
> unsigned int order;
> unsigned int preference;
> char * flags;
> char * services;
> char * regexp;
> char * replacement;
> };
>
>
> So my question is this : How can I extract the string fields from the
> rdata portion of the ns_rr structure and store them into the respective
> fields in my user-defined structure?
>
> The integer fields ( order and preference ) are easy enough, being of
> fixed size. What about the strings which can be of variable size?
>
> I hope that is clear enough to elicit a suggestion or a positive
> response in the least!

I gather uchar_t is an unsigned char. How about:-

struct NAPTR *naptr;
ns_rr ns_rr_data;

/* load somehow ns_rr_data */

naptr = (struct NAPTR*)ns_rr_data.rdata;
printf ("services = \"%s\"\n", naptr->services);

this assumes your structure is laid out in the same way as the data in
ns_rr.
Check if they define something like your struct NAPTR and use that.
To test the hypothesis, I wrote this sample program.... Can u tell
me why it does not work? The program segment faults... ( after the
printf("%s",record.rdata); statement).
// Program to test the rdata parsing capability.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <malloc.h>

struct NAPTR
{

unsigned int order;
unsigned int preference;
char * flags;
char * services;
char * regexp;
char * replacement;
};

struct nsrr
{

char name[255];
unsigned type;
unsigned class;
unsigned ttl;
unsigned rdlength;
char *rdata;
};
int main(void)
{

struct NAPTR *naptr;
struct nsrr record;

record.rdata=(char*)malloc(1024);


cast not needed. Test for malloc failure
strcpy(record.name,"www.foo.com");

record.type=1;
record.class=1;
record.ttl=8640;
strcpy(record.rdata,"100 10 s sip+e2u !http://[^/:](.*)!\1!i
www.foo.com");
record.rdlength=sizeof(record.rdata);


try your debugger (I can't believe I said that...). The rdata doesn't
seem
to be in the same format as NAPTR...

printf("%s",record.rdata);

naptr=(struct NAPTR*)(record.rdata);


this trick is dangerous if the data is not formatted as struct NAPTR.
and yours isn't
printf("\nThe Record contents are : ");

printf("%s",naptr->flags);
printf("\n%s",naptr->services);
printf("\n%s",naptr->regexp);
printf("\n%s",naptr->replacement);
return 0;
}

--


- Show quoted text -
I suppose u are referring to the fact that I havent' mentioned anything

about the Domain Name, class etc.
The structure of the NAPTR record acc to RFC 2915 is :
Domain TTL Class Type Order Preference Flags Services Regexp
Replacement. ( in this order ).

The ns_rr structure defined is <arpa/nameser.h> actually
has separate fields for Domain, TTL, Type and Class. These are
considered as the Header. The remaining fields form the rdata portion.
I just require to extract the fields in the rdata section, so I wrote
this simple program. How is the RDATA section not the same as the one
in the example?
Thanks and Regards,
Timmy Jose. Nick Keighley


Mar 24 '06 #21

Nick Keighley wrote:
zoltan wrote:
Nick Keighley wrote:
zoltan wrote:
> Nick Keighley wrote:
> > zoltan wrote:
> > > do*********@126.com wrote:
> > > > zoltan wrote:
<snip>
I am supposed to implement a DNS Query. The Information to be processed
> resides in RRs ( Resource Records). The header <arpa/nameser.h>
> contains the following structure to store the data of these RRs as :
>
> typedef struct __ns_rr {
> char name[NS_MAXDNAME];
> uint16_t type;
> uint16_t rr_class;
> uint32_t ttl;
> uint16_t rdlength;
> const uchar_t *rdata;
> } ns_rr;
>
> Now, the rdata part can have several fields depending on the specific
> type of Record.
> For instance, the SRV type has only the following fields : priority,
> weight, port ( all of unsigned int type) and a target string ( the
> hostname).
>
> For my requirement, I have to use the data stored in a NAPTR record.
> The "rdata" contains the following fields for this type of record :
>
> order, preference ( unsigned int) and
> flags, services, regexp and replacement ( all of type char *).
>
> Now, because the definition of the rdata in the ns_rr structure is
> generic, I have defined ( as I need to ) a structure as follows, to
> store the actual data :
>
> struct NAPTR
> {
> unsigned int order;
> unsigned int preference;
> char * flags;
> char * services;
> char * regexp;
> char * replacement;
> };
>
>
> So my question is this : How can I extract the string fields from the
> rdata portion of the ns_rr structure and store them into the respective
> fields in my user-defined structure?
>
> The integer fields ( order and preference ) are easy enough, being of
> fixed size. What about the strings which can be of variable size?
>
> I hope that is clear enough to elicit a suggestion or a positive
> response in the least!

I gather uchar_t is an unsigned char. How about:-

struct NAPTR *naptr;
ns_rr ns_rr_data;

/* load somehow ns_rr_data */

naptr = (struct NAPTR*)ns_rr_data.rdata;
printf ("services = \"%s\"\n", naptr->services);

this assumes your structure is laid out in the same way as the data in
ns_rr.
Check if they define something like your struct NAPTR and use that.
To test the hypothesis, I wrote this sample program.... Can u tell
me why it does not work? The program segment faults... ( after the
printf("%s",record.rdata); statement).
// Program to test the rdata parsing capability.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <malloc.h>

struct NAPTR
{

unsigned int order;
unsigned int preference;
char * flags;
char * services;
char * regexp;
char * replacement;
};

struct nsrr
{

char name[255];
unsigned type;
unsigned class;
unsigned ttl;
unsigned rdlength;
char *rdata;
};
int main(void)
{

struct NAPTR *naptr;
struct nsrr record;

record.rdata=(char*)malloc(1024);


cast not needed. Test for malloc failure
strcpy(record.name,"www.foo.com");

record.type=1;
record.class=1;
record.ttl=8640;
strcpy(record.rdata,"100 10 s sip+e2u !http://[^/:](.*)!\1!i
www.foo.com");
record.rdlength=sizeof(record.rdata);


try your debugger (I can't believe I said that...). The rdata doesn't
seem
to be in the same format as NAPTR...

printf("%s",record.rdata);

naptr=(struct NAPTR*)(record.rdata);


this trick is dangerous if the data is not formatted as struct NAPTR.
and yours isn't
printf("\nThe Record contents are : ");

printf("%s",naptr->flags);
printf("\n%s",naptr->services);
printf("\n%s",naptr->regexp);
printf("\n%s",naptr->replacement);
return 0;
}

--


- Show quoted text -
I suppose u are referring to the fact that I havent' mentioned anything

about the Domain Name, class etc.
The structure of the NAPTR record acc to RFC 2915 is :
Domain TTL Class Type Order Preference Flags Services Regexp
Replacement. ( in this order ).

The ns_rr structure defined is <arpa/nameser.h> actually
has separate fields for Domain, TTL, Type and Class. These are
considered as the Header. The remaining fields form the rdata portion.
I just require to extract the fields in the rdata section, so I wrote
this simple program. How is the RDATA section not the same as the one
in the example?
Thanks and Regards,
Timmy Jose. Nick Keighley


Mar 24 '06 #22
zoltan wrote:
Nick Keighley wrote:
zoltan wrote:
Nick Keighley wrote:
> zoltan wrote:
<snip>
> > I am supposed to implement a DNS Query. The Information to be processed
> > resides in RRs ( Resource Records). The header <arpa/nameser.h>
> > contains the following structure to store the data of these RRs as :
> >
> > typedef struct __ns_rr {
> > char name[NS_MAXDNAME];
> > uint16_t type;
> > uint16_t rr_class;
> > uint32_t ttl;
> > uint16_t rdlength;
> > const uchar_t *rdata;
> > } ns_rr;
> >
> > Now, the rdata part can have several fields depending on the specific
> > type of Record.
> > For instance, the SRV type has only the following fields : priority,
> > weight, port ( all of unsigned int type) and a target string ( the
> > hostname).
> >
> > For my requirement, I have to use the data stored in a NAPTR record.
> > The "rdata" contains the following fields for this type of record :
> >
> > order, preference ( unsigned int) and
> > flags, services, regexp and replacement ( all of type char *).
> >
> > Now, because the definition of the rdata in the ns_rr structure is
> > generic, I have defined ( as I need to ) a structure as follows, to
> > store the actual data :
> >
> > struct NAPTR
> > {
> > unsigned int order;
> > unsigned int preference;
> > char * flags;
> > char * services;
> > char * regexp;
> > char * replacement;
> > };
> >
> >
> > So my question is this : How can I extract the string fields from the
> > rdata portion of the ns_rr structure and store them into the respective
> > fields in my user-defined structure?
> >
> > The integer fields ( order and preference ) are easy enough, being of
> > fixed size. What about the strings which can be of variable size?
> >
> > I hope that is clear enough to elicit a suggestion or a positive
> > response in the least!
>
> I gather uchar_t is an unsigned char. How about:-
>
> struct NAPTR *naptr;
> ns_rr ns_rr_data;
>
> /* load somehow ns_rr_data */
>
> naptr = (struct NAPTR*)ns_rr_data.rdata;
> printf ("services = \"%s\"\n", naptr->services);
>
> this assumes your structure is laid out in the same way as the data in
> ns_rr.
> Check if they define something like your struct NAPTR and use that.

To test the hypothesis, I wrote this sample program.... Can u tell
me why it does not work? The program segment faults... ( after the
printf("%s",record.rdata); statement).
// Program to test the rdata parsing capability.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <malloc.h>
don't use malloc.h. The correct header is stdlib.h

I refer to this later as FRAGMENT-1
struct NAPTR
{
unsigned int order;
unsigned int preference;
char * flags;
char * services;
char * regexp;
char * replacement;
};

struct nsrr
{

char name[255];
unsigned type;
unsigned class;
unsigned ttl;
unsigned rdlength;
char *rdata;
};
int main(void)
{
struct NAPTR *naptr;
struct nsrr record;

record.rdata=(char*)malloc(1024);
cast not needed. Test for malloc failure
strcpy(record.name,"www.foo.com");
record.type=1;
record.class=1;
record.ttl=8640;
FRAGMENT-2 strcpy(record.rdata,"100 10 s sip+e2u !http://[^/:](.*)!\1!iwww.foo.com"); record.rdlength=sizeof(record.rdata);


try your debugger (I can't believe I said that...). The rdata doesn't
seem to be in the same format as NAPTR...
printf("%s",record.rdata);
FRAGMENT-3 naptr=(struct NAPTR*)(record.rdata);


this trick is dangerous if the data is not formatted as struct NAPTR.
and yours isn't
printf("\nThe Record contents are : ");

printf("%s",naptr->flags);
printf("\n%s",naptr->services);
printf("\n%s",naptr->regexp);
printf("\n%s",naptr->replacement);
return 0;
}


I suppose u are referring to the fact that I havent' mentioned anything


this is not a text message please write "you" rather than "u"
about the Domain Name, class etc.
no. What's "Domain Name"? I don't remeber you mentioning it before.
You seem to think I know something about DNS lookup or whatever it
is you are trying. I am only answering questions about standard C.

Look, you define your structure NAPTR (it is unusual to use all upper
case for a type or type alias) as 2 unsigned int followed by 4 char*
(see FRAGMENT-1) then you stuff a single char* into the field rdata
(see FRAGMENT-2) then you cast rdata into an NPTR (see FRAGMENT-3)
and then you try to read the various fields. And it crashes. Well, duh!

Prtinting the unsigned ints may "work" but treating a char as a char*
is
Really Bad.

I'm sure one of is confused and I'm pretty sure it's not me... Which
means about 12 clc regulars are about to tell me how much tosh I've
been talking for the last few posts.
The structure of the NAPTR record acc to RFC 2915 is :
I don't care.
Domain TTL Class Type Order Preference Flags Services Regexp
Replacement. ( in this order ).

The ns_rr structure defined is <arpa/nameser.h> actually
has separate fields for Domain, TTL, Type and Class. These are
considered as the Header. The remaining fields form the rdata portion.

I just require to extract the fields in the rdata section, so I wrote
this simple program. How is the RDATA section not the same as the one
in the example?


<snip>
--
Nick Keighley

Mar 25 '06 #23

Nick Keighley wrote:
zoltan wrote:
Nick Keighley wrote:
zoltan wrote:
> Nick Keighley wrote:
> > zoltan wrote:
<snip>
> > I am supposed to implement a DNS Query. The Information to be processed
> > > resides in RRs ( Resource Records). The header <arpa/nameser.h>
> > > contains the following structure to store the data of these RRs as :
> > >
> > > typedef struct __ns_rr {
> > > char name[NS_MAXDNAME];
> > > uint16_t type;
> > > uint16_t rr_class;
> > > uint32_t ttl;
> > > uint16_t rdlength;
> > > const uchar_t *rdata;
> > > } ns_rr;
> > >
> > > Now, the rdata part can have several fields depending on the specific
> > > type of Record.
> > > For instance, the SRV type has only the following fields : priority,
> > > weight, port ( all of unsigned int type) and a target string ( the
> > > hostname).
> > >
> > > For my requirement, I have to use the data stored in a NAPTR record.
> > > The "rdata" contains the following fields for this type of record :
> > >
> > > order, preference ( unsigned int) and
> > > flags, services, regexp and replacement ( all of type char *).
> > >
> > > Now, because the definition of the rdata in the ns_rr structure is
> > > generic, I have defined ( as I need to ) a structure as follows, to
> > > store the actual data :
> > >
> > > struct NAPTR
> > > {
> > > unsigned int order;
> > > unsigned int preference;
> > > char * flags;
> > > char * services;
> > > char * regexp;
> > > char * replacement;
> > > };
> > >
> > >
> > > So my question is this : How can I extract the string fields from the
> > > rdata portion of the ns_rr structure and store them into the respective
> > > fields in my user-defined structure?
> > >
> > > The integer fields ( order and preference ) are easy enough, being of
> > > fixed size. What about the strings which can be of variable size?
> > >
> > > I hope that is clear enough to elicit a suggestion or a positive
> > > response in the least!
> >
> > I gather uchar_t is an unsigned char. How about:-
> >
> > struct NAPTR *naptr;
> > ns_rr ns_rr_data;
> >
> > /* load somehow ns_rr_data */
> >
> > naptr = (struct NAPTR*)ns_rr_data.rdata;
> > printf ("services = \"%s\"\n", naptr->services);
> >
> > this assumes your structure is laid out in the same way as the data in
> > ns_rr.
> > Check if they define something like your struct NAPTR and use that.
>
> To test the hypothesis, I wrote this sample program.... Can u tell
> me why it does not work? The program segment faults... ( after the
> printf("%s",record.rdata); statement).
>
>
> // Program to test the rdata parsing capability.
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <sys/types.h>
> #include <malloc.h>
don't use malloc.h. The correct header is stdlib.h
Touche

I refer to this later as FRAGMENT-1
struct NAPTR
> {
> unsigned int order;
> unsigned int preference;
> char * flags;
> char * services;
> char * regexp;
> char * replacement;
> };

struct nsrr
> {
>
> char name[255];
> unsigned type;
> unsigned class;
> unsigned ttl;
> unsigned rdlength;
> char *rdata;
> };
>
>
> int main(void)
> {
> struct NAPTR *naptr;
> struct nsrr record;
>
> record.rdata=(char*)malloc(1024);

cast not needed. Test for malloc failure

> strcpy(record.name,"www.foo.com");
> record.type=1;
> record.class=1;
> record.ttl=8640;
FRAGMENT-2 > strcpy(record.rdata,"100 10 s sip+e2u !http://[^/:](.*)!\1!iwww.foo.com"); record.rdlength=sizeof(record.rdata);

try your debugger (I can't believe I said that...). The rdata doesn't
seem to be in the same format as NAPTR...

> printf("%s",record.rdata);
FRAGMENT-3 > naptr=(struct NAPTR*)(record.rdata);

this trick is dangerous if the data is not formatted as struct NAPTR.
and yours isn't

> printf("\nThe Record contents are : ");
>
> printf("%s",naptr->flags);
> printf("\n%s",naptr->services);
> printf("\n%s",naptr->regexp);
> printf("\n%s",naptr->replacement);
> return 0;
> }
I suppose u are referring to the fact that I havent' mentioned anything


this is not a text message please write "you" rather than "u"
about the Domain Name, class etc.


no. What's "Domain Name"? I don't remeber you mentioning it before.
You seem to think I know something about DNS lookup or whatever it
is you are trying. I am only answering questions about standard C.


Well I had supposed that anyone who uses the net in this century would
surely know what a "domain name" is. It is nothing but a URL ( like
www.google.com ). My apologies.
Look, you define your structure NAPTR (it is unusual to use all upper
case for a type or type alias) as 2 unsigned int followed by 4 char*
(see FRAGMENT-1) then you stuff a single char* into the field rdata
(see FRAGMENT-2) then you cast rdata into an NPTR (see FRAGMENT-3)
and then you try to read the various fields. And it crashes. Well, duh! Isn't that what you had suggested? I suggest that you check your own
post.
Prtinting the unsigned ints may "work" but treating a char as a char*
is
Really Bad.
I suppose "s" is not a string but a char?

I'm sure one of is confused and I'm pretty sure it's not me... Which
means about 12 clc regulars are about to tell me how much tosh I've
been talking for the last few posts.
Well I know I am confused that's why I thought I'd find some actual
help here and not some grammar lessons! Well, seems it's just as murky
in here.
The structure of the NAPTR record acc to RFC 2915 is :
I don't care.


Frankly I don't either. However, that was just additional information
to clear up your confusion whether it was in the right format or not!
Domain TTL Class Type Order Preference Flags Services Regexp
Replacement. ( in this order ).

The ns_rr structure defined is <arpa/nameser.h> actually
has separate fields for Domain, TTL, Type and Class. These are
considered as the Header. The remaining fields form the rdata portion.

I just require to extract the fields in the rdata section, so I wrote
this simple program. How is the RDATA section not the same as the one
in the example?


<snip>
--
Nick Keighley


Thanks lot for the verbal help but no real solution? I will surely
post the solution to this problem soon.

Thanks and regards,

Timmy Jose.

Mar 27 '06 #24
zoltan wrote:

Nick Keighley wrote:
zoltan wrote:
Nick Keighley wrote:
> zoltan wrote:
> > Nick Keighley wrote:
> > > zoltan wrote:


Could you guys start trimming your quotes? I had to scroll through
screens worth of up to six deep quotes to get to any new material.

Brian
Mar 27 '06 #25

Default User wrote:
zoltan wrote:

Nick Keighley wrote:
zoltan wrote:
> Nick Keighley wrote:
> > zoltan wrote:
> > > Nick Keighley wrote:
> > > > zoltan wrote:


Could you guys start trimming your quotes? I had to scroll through
screens worth of up to six deep quotes to get to any new material.


normally I would, but in this case I want to refer to things he posted.
--
Nick Keighley

Mar 27 '06 #26
zoltan wrote:
Nick Keighley wrote:
zoltan wrote:
Nick Keighley wrote:
> zoltan wrote:
> > Nick Keighley wrote:
> > > zoltan wrote: > > > > I am supposed to implement a DNS Query. The Information to be processed
> > > > resides in RRs ( Resource Records). The header <arpa/nameser.h>
> > > > contains the following structure to store the data of these RRs as :
> > > >
> > > > typedef struct __ns_rr {
> > > > char name[NS_MAXDNAME];
> > > > uint16_t type;
> > > > uint16_t rr_class;
> > > > uint32_t ttl;
> > > > uint16_t rdlength;
> > > > const uchar_t *rdata;
> > > > } ns_rr;
> > > >
> > > > Now, the rdata part can have several fields depending on the specific
> > > > type of Record.
<snip>
> > > > For my requirement, I have to use the data stored in a NAPTR record.
> > > > The "rdata" contains the following fields for this type of record :
> > > >
> > > > order, preference ( unsigned int) and
> > > > flags, services, regexp and replacement ( all of type char *).
> > > >
> > > > Now, because the definition of the rdata in the ns_rr structure is
> > > > generic, I have defined ( as I need to ) a structure as follows, to
> > > > store the actual data :
> > > >
> > > > struct NAPTR
> > > > {
> > > > unsigned int order;
> > > > unsigned int preference;
> > > > char * flags;
> > > > char * services;
> > > > char * regexp;
> > > > char * replacement;
> > > > };
> > > >
> > > >
> > > > So my question is this : How can I extract the string fields from the
> > > > rdata portion of the ns_rr structure and store them into the respective
> > > > fields in my user-defined structure?
> > > >
> > > > The integer fields ( order and preference ) are easy enough, being of
> > > > fixed size. What about the strings which can be of variable size?
<snip>
> > > I gather uchar_t is an unsigned char. How about:-
> > >
> > > struct NAPTR *naptr;
> > > ns_rr ns_rr_data;
> > >
> > > /* load somehow ns_rr_data */
note this step. I didn't say "stuff anything I have handy into the data

structure". I expected you to (somehow) load valid data into the
structure.
The reason I keep stressing I don't know anything about your
application,
is because I *don't*. I don't know how you are supposed to load
ns_rr_data.
Try reading the documentation?

> > > naptr = (struct NAPTR*)ns_rr_data.rdata;
> > > printf ("services = \"%s\"\n", naptr->services);
> > >
> > > this assumes your structure is laid out in the same way as the data in
> > > ns_rr.
Note this well
> > > Check if they define something like your struct NAPTR and use that.
> >
> > To test the hypothesis, I wrote this sample program.... Can u tell
> > me why it does not work? The program segment faults... ( after the
> > printf("%s",record.rdata); statement).
<snip>
> > #include <stdio.h>
> > #include <stdlib.h>
> > #include <string.h>
> > #include <sys/types.h>
> > #include <malloc.h>
don't use malloc.h. The correct header is stdlib.h


Touche


I thought you wanted help with your program?
I refer to this later as FRAGMENT-1
> > struct NAPTR
> > {
> > unsigned int order;
> > unsigned int preference;
> > char * flags;
> > char * services;
> > char * regexp;
> > char * replacement;
> > };

> > struct nsrr
> > {
> >
> > char name[255];
> > unsigned type;
> > unsigned class;
> > unsigned ttl;
> > unsigned rdlength;
> > char *rdata;
> > };
> >
> > int main(void)
> > {
> > struct NAPTR *naptr;
> > struct nsrr record;
> >
> > record.rdata=(char*)malloc(1024);
>
> cast not needed. Test for malloc failure
>
> > strcpy(record.name,"www.foo.com");
> > record.type=1;
> > record.class=1;
> > record.ttl=8640;


FRAGMENT-2
> > strcpy(record.rdata,"100 10 s sip+e2u !http://[^/:](.*)!\1!iwww.foo.com");

> > record.rdlength=sizeof(record.rdata);
>
> try your debugger (I can't believe I said that...). The rdata doesn't
> seem to be in the same format as NAPTR...
hello!

> > printf("%s",record.rdata);


FRAGMENT-3
> > naptr=(struct NAPTR*)(record.rdata);
>
> this trick is dangerous if the data is not formatted as struct NAPTR.
> and yours isn't
>
> > printf("\nThe Record contents are : ");
> >
> > printf("%s",naptr->flags);
> > printf("\n%s",naptr->services);
> > printf("\n%s",naptr->regexp);
> > printf("\n%s",naptr->replacement);
> > return 0;
> > }

I suppose u are referring to the fact that I havent' mentioned anything


this is not a text message please write "you" rather than "u"
about the Domain Name, class etc.


no. What's "Domain Name"? I don't remeber you mentioning it before.
You seem to think I know something about DNS lookup or whatever it
is you are trying. I am only answering questions about standard C.


Well I had supposed that anyone who uses the net in this century would
surely know what a "domain name" is. It is nothing but a URL ( like
www.google.com ). My apologies.


perhaps I do, but I'm trying to answer questions about standard C

Look, you define your structure NAPTR (it is unusual to use all upper
case for a type or type alias) as 2 unsigned int followed by 4 char*
(see FRAGMENT-1) then you stuff a single char* into the field rdata
(see FRAGMENT-2) then you cast rdata into an NPTR (see FRAGMENT-3)
and then you try to read the various fields. And it crashes. Well, duh!


Isn't that what you had suggested? I suggest that you check your own
post.


I suggest you re-read the post. I (twice) stated that the data had to
be in the
right format.
Prtinting the unsigned ints may "work" but treating a char as a char*
is Really Bad.


I suppose "s" is not a string but a char?


what's "s"? Look if you tell printf() you've given it a char* (which is
an address)
and you've really given it a char then it will break.
I'm sure one of is confused and I'm pretty sure it's not me... Which
means about 12 clc regulars are about to tell me how much tosh I've
been talking for the last few posts.


Well I know I am confused that's why I thought I'd find some actual
help here and not some grammar lessons!


what grammar lesson. I just suggested you didn't use text-speak
abbreviations (i'm not the only one on this ng that doesn't like them).
Well, seems it's just as murky in here.
and there was thinking I was giving you help..
The structure of the NAPTR record acc to RFC 2915 is :


I don't care.


Frankly I don't either. However, that was just additional information
to clear up your confusion whether it was in the right format or not!


how did it help?

<snip>
The ns_rr structure defined is <arpa/nameser.h> actually
has separate fields for Domain, TTL, Type and Class. These are
considered as the Header. The remaining fields form the rdata portion.

I just require to extract the fields in the rdata section, so I wrote
this simple program. How is the RDATA section not the same as the one
in the example?


<snip>
Thanks lot for the verbal help but no real solution?
you want me to write the code for you?
I will surely post the solution to this problem soon.


I think you should read my replies carefully. Then consult a good book
and r
e-read the section on pointers. I suspect you just one "ah! now I see"
from
working this out, but at the moment we just seem to be talking across
each other.
--
Nick Keighley

Mar 27 '06 #27
Nick Keighley wrote:

Default User wrote:
zoltan wrote:

Nick Keighley wrote:
> zoltan wrote:
> > Nick Keighley wrote:
> > > zoltan wrote:
> > > > Nick Keighley wrote:
> > > > > zoltan wrote:


Could you guys start trimming your quotes? I had to scroll through
screens worth of up to six deep quotes to get to any new material.


normally I would, but in this case I want to refer to things he
posted.


Things three screens up? I didn't see ANY reference to that stuff at
all. If you were responding to it, your reply should have been
immediately following that particular line or paragraph.

Brian

Mar 27 '06 #28
On 22 Mar 2006 13:17:02 GMT, Pedro Graca <he****@dodgeit.com> wrote:
zoltan wrote: <snip>
So my question is this : How can I extract the string fields from the
rdata portion of the ns_rr structure and store them into the respective
fields in my user-defined structure?


It depends on how the data in rdata is structured.

rdata is a pointer to unsigned char, hiding the real data it holds.
You have to come up with a way to get that data into suitable C objects.

I didn't follow the links posted else-thread (where probably the rdata
structure is documented) so I'll just make it up:
rdata = "\x01\x02\x03\x04" /* order (32-bit unsigned int) */
"\x05\x06\x07\x08" /* preference (32-bit unsigned int) */
"\x41\x00" /* flags (NUL terminated string) */
"\x54\x43\x50\x00" /* services (NUL terminated string) */
"\x28\x2E\x2A\x29\x00" /* regexp (NUL terminated string) */
"\x3d\x00"; /* replacement (NUL terminated string) */

struct NAPTR data;
/* assuming `unsigned int` bit representation is LSB first */


Guesses wrong. (All nontextual?) Internet protocols like DNS use MSB
first, aka "network byte order". And DNS represents character-string
items with a prefix length byte/octet and no terminator. Although
domain names, and 'replacement' is actually a domain name, are
terminated by a null _label_ which consists of a single zero
byte/octet, looking sort of like a C character string. (Unless
compressed, and newer stds like 2915 prohibit compression.)

Aside: your data was only an example, but nowhere in IP is "TCP"
called a service; it's a protocol, particularly a next-layer protocol
from IP or ESP etc. Generally IP "services" are things that map to
WKports, like TELNET, FTP, NTP, etc. But the "services" in 2915 are
separate and distinct from those and off in their own little world.
data.order = rdata[0] + (rdata[1]<<8) + (rdata[2]<<16) + (rdata[3]<<24);
data.preference = rdata[4] + (rdata[5]<<8) + (rdata[6]<<16) + (rdata[7]<<24);
ObTopic: the left operand of << is implicitly promoted only to int or
in very rare systems unsigned int. If int is 16 bits, as is allowed,
this won't work; if int is 32 bits but signed << doesn't handle
overflow, as is allowed, ditto. You need to cast or otherwise convert
to (at least) uint32_least_t (C99) or unsigned long _before_ shifting.
data.flags = rdata+8;
data.services = data.flags + strlen(data.flags) + 1;
data.regexp = data.services + strlen(data.services) + 1;
data.replacement = data.regexp + strlen(data.regexp) + 1;


This data sharing depended on your mistaken assumption, without which
you can't just point into the rdata unless you (can afford to) clobber
bits of it; instead the OP will need to allocate space and copy these.
Or change the declaration to preallocate space in his struct directly.

- David.Thompson1 at worldnet.att.net
Apr 3 '06 #29
Dave Thompson wrote:
On 22 Mar 2006 13:17:02 GMT, Pedro Graca <he****@dodgeit.com> wrote:
rdata is a pointer to unsigned char [...]

rdata = "\x01\x02\x03\x04" /* order (32-bit unsigned int) */ /* a few lines of bad assumptions snipped */ "\x3d\x00"; /* replacement (NUL terminated string) */

struct NAPTR data;
[snip internet protocols details for which I thank you]
data.order = rdata[0] + (rdata[1]<<8) + (rdata[2]<<16) + (rdata[3]<<24);


ObTopic: the left operand of << is implicitly promoted only to int or
in very rare systems unsigned int. If int is 16 bits, as is allowed,
this won't work; if int is 32 bits but signed << doesn't handle
overflow, as is allowed, ditto. You need to cast or otherwise convert
to (at least) uint32_least_t (C99) or unsigned long _before_ shifting.


Wow! "With C you *really* can shoot yourself in the foot easily".
Thank you for catching and, especially, for showing the error in that
construction.

[snip code with mistaken assumptions and your attention calling for it]

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Apr 4 '06 #30

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Hemang Shah | last post: by
9 posts views Thread by Stargate4004 | last post: by
3 posts views Thread by rh0dium | last post: by
9 posts views Thread by ankitdesai | last post: by
3 posts views Thread by Cuong.Tong | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.