Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > design inquiry

Reply
Thread Tools

design inquiry

 
 
Bill Cunningham
Guest
Posts: n/a
 
      11-22-2010
Lew Pitcher wrote:
> On November 21, 2010 18:34, in comp.lang.c, http://www.velocityreviews.com/forums/(E-Mail Removed)lid
> wrote:
>
>> Lew Pitcher wrote:
>>
>> [snip]
>>
>>> Take an integer value of unknown range, and squeeze it into a 16bit
>>> value in network byte (aka "big endian") order, as two 8bit
>>> quantities.

> [snip]
>>
>> OK I think you're heading toward posix's ntoh() and hton() when
>> speaking about network bytes here right?
>>

>
> If you have those C language extensions at your disposal, then yes.
> You are looking for the htons() function ("host to network short").
>
> [snip]
>
>> So is there anything you could recommend I read such as bitwise
>> information to make all this sink in?

>
> I'm unsure of your meaning when you say "bitwise information".


The bitwise operators

> If you mean the details of manipulating integers into
> network-byte-order, then if you have C functions available to do some
> of the work (like htons()), use them. Otherwise, think about what you
> are trying to do in simple terms (hint: converting from one "byte
> order" to another is like performing gradeschool math; divisions and
> remainders).
>
> If you mean in general, then I suggest that you study the code that
> others have written; particularly in the same area as your intended
> program. For instance, after my last post, I looked (briefly) at the
> tftp client code (written by H. Peter Anvin) in the Slackware
> distribution library. I find that my off-the-top-of-my-head example
> code is very much like his program code, but where I used explicit
> loops (so you could see what was happening), he used standard (C and
> POSIX) calls.
>
> Here's an example:
>
> Here's his tftp header structure (trimmed to the subset that you are
> looking at):
> struct tftphdr {
> short th_opcode; /* packet type */
> char tu_stuff[STUFFSIZE]; /* request packet stuff */
> char th_data[DATASIZE]; /* data or error string */
> };
>


Can you kind of walk me through that code. It contains a dereference
which I still haven't quite figured out and the one ampersand. I don't quite
understand what that line does.

> And, here's his code for building the header
> static int makerequest(int request, const char *name,
> struct tftphdr *tp, const char *mode)
> {
> char *cp;
>
> tp->th_opcode = htons((u_short)request);
> cp = (char *) &(tp->th_stuff);
> strcpy(cp, name);
> cp += strlen(name);
> *cp++ = '\0';


dereference

> strcpy(cp, mode);
> cp += strlen(mode);
> *cp++ = '\0';
> return (cp - (char *)tp);
> }
>
> HTH



 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      11-22-2010
On November 22, 2010 17:44, in comp.lang.c, (E-Mail Removed)lid wrote:

> Lew Pitcher wrote:

[snip]
>> Here's an example:
>>
>> Here's his tftp header structure (trimmed to the subset that you are
>> looking at):
>> struct tftphdr {
>> short th_opcode; /* packet type */
>> char tu_stuff[STUFFSIZE]; /* request packet stuff */
>> char th_data[DATASIZE]; /* data or error string */
>> };
>>

>
> Can you kind of walk me through that code. It contains a dereference
> which I still haven't quite figured out and the one ampersand. I don't
> quite understand what that line does.
>
>> And, here's his code for building the header


The function makerequest() will build the tftp header from several supplied
values.

Note that the caller will provide both the values that will be populated
into the header /and/ the space for the header itself.


>> static int makerequest(int request, const char *name,
>> struct tftphdr *tp, const char *mode)


So, the caller will call makerequest(), giving it arguments
request - which is an int holding the value of the header opcode
name - which is a pointer to a C character string containing the
filename to be copied into the header filename
tp - which is a pointer to a tftphdr structure, where this
function will build the tftp header, and
mode - which is a pointer to a C character string containing the
modename to be copied into the header mode

The function will return an integer value, indicating the length of the
entire header built in the space pointed to by tp.

>> {
>> char *cp;


cp is a pointer to characters, currently uninitialized.

>> tp->th_opcode = htons((u_short)request);


First, use the htons() function to convert the request integer value into a
network-byte-order "short", and place that network-short in the tftp header
opcode (tp->th_opcode).

>> cp = (char *) &(tp->th_stuff);


cp now points to the first character in the "stuff" area of the tftp header
(tp->th_stuff)

>> strcpy(cp, name);


We copy the filename into the first characters of the "stuff" area. The
filename will be terminated with the '\0' as per all C strings.

>> cp += strlen(name);


cp used to point at the beginning of "stuff". Now, it points to the
character after the last significant character of the filename which was
copied into "stuff". This character /should be/ the '\0' terminating the
copied string.

>> *cp++ = '\0';


We write over that trailing '\0' with our own '\0' (just to be sure), and
increment cp so that now points to the first character /after/ the '\0'
which marks the end of the filename in the tftp header "stuff" area.

>> strcpy(cp, mode);


We copy the mode string into the next characters of the "stuff" area. The
mode string will be terminated with the '\0' as per all C strings.

>> cp += strlen(mode);


cp used to point at the first character following the filename in
the "stuff" area. Now, it points to the character after the last
significant character of the mode string which was copied into "stuff".
This character /should be/ the '\0' terminating the copied string.

>> *cp++ = '\0';


We write over that trailing '\0' with our own '\0' (just to be sure), and
increment cp so that now points to the first character /after/ the '\0'
which marks the end of the mode string in the tftp header "stuff" area.

>> return (cp - (char *)tp);


We compute the number of characters between the beginning of the tftp header
and the end, and return that number to the caller. This is, of course, the
length of the tftp header that we have constructed.

>> }


HTH
--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
---------- Slackware - Because I know what I'm doing. ------


 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      11-22-2010
Lew Pitcher wrote:

> And, here's his code for building the header
> static int makerequest(int request, const char *name,
> struct tftphdr *tp, const char *mode)
> {
> char *cp;
>
> tp->th_opcode = htons((u_short)request);
> cp = (char *) &(tp->th_stuff);
> strcpy(cp, name);
> cp += strlen(name);


Here'es where I get lost with the *cp

*cp++ = '\0';
> strcpy(cp, mode);
> cp += strlen(mode);
> *cp++ = '\0';
> return (cp - (char *)tp);
> }
>
> HTH



 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      11-23-2010
Lew Pitcher wrote:

> And, here's his code for building the header
> static int makerequest(int request, const char *name,
> struct tftphdr *tp, const char *mode)
> {
> char *cp;
>
> tp->th_opcode = htons((u_short)request);
> cp = (char *) &(tp->th_stuff);
> strcpy(cp, name);
> cp += strlen(name);
> *cp++ = '\0';
> strcpy(cp, mode);
> cp += strlen(mode);
> *cp++ = '\0';
> return (cp - (char *)tp);
> }


Don't quite understand the dereference. That's hurting me from being
able to read this.
*cp++=='\0'

cp*tp;
tp* struct tftphdr;

cp is set to point to tp which in turn pointers to struct tftphdr is the way
it looks to me.

Bill


 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      11-23-2010
On November 22, 2010 19:15, in comp.lang.c, (E-Mail Removed)lid wrote:

> Lew Pitcher wrote:
>
>> And, here's his code for building the header
>> static int makerequest(int request, const char *name,
>> struct tftphdr *tp, const char *mode)
>> {
>> char *cp;
>>
>> tp->th_opcode = htons((u_short)request);
>> cp = (char *) &(tp->th_stuff);
>> strcpy(cp, name);
>> cp += strlen(name);
>> *cp++ = '\0';
>> strcpy(cp, mode);
>> cp += strlen(mode);
>> *cp++ = '\0';
>> return (cp - (char *)tp);
>> }

>
> Don't quite understand the dereference. That's hurting me from being
> able to read this.
> *cp++=='\0'


OK, let's split this apart...

Instead of
*cp++=='\0'
try
*cp = '\0';
cp = cp + 1;

Better?

>
> cp*tp;
> tp* struct tftphdr;
>
> cp is set to point to tp which in turn pointers to struct tftphdr is the
> way it looks to me.


cp points to somewhere in the tp->th_stuff array of char



--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
---------- Slackware - Because I know what I'm doing. ------


 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      11-23-2010
On November 22, 2010 19:39, in comp.lang.c, (E-Mail Removed) wrote:

> On November 22, 2010 19:15, in comp.lang.c, (E-Mail Removed)lid wrote:
>
>> Lew Pitcher wrote:
>>
>>> And, here's his code for building the header
>>> static int makerequest(int request, const char *name,
>>> struct tftphdr *tp, const char *mode)
>>> {
>>> char *cp;
>>>
>>> tp->th_opcode = htons((u_short)request);
>>> cp = (char *) &(tp->th_stuff);
>>> strcpy(cp, name);
>>> cp += strlen(name);
>>> *cp++ = '\0';
>>> strcpy(cp, mode);
>>> cp += strlen(mode);
>>> *cp++ = '\0';
>>> return (cp - (char *)tp);
>>> }

>>
>> Don't quite understand the dereference. That's hurting me from being
>> able to read this.
>> *cp++=='\0'

>
> OK, let's split this apart...
>
> Instead of
> *cp++=='\0'


Oops. Let's not propogate Bill's typo.
The original statement was...

*cp++ = '\0';

> try
> *cp = '\0';
> cp = cp + 1;
>
> Better?
>
>>
>> cp*tp;
>> tp* struct tftphdr;
>>
>> cp is set to point to tp which in turn pointers to struct tftphdr is the
>> way it looks to me.

>
> cp points to somewhere in the tp->th_stuff array of char
>
>
>


--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
---------- Slackware - Because I know what I'm doing. ------


 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      11-23-2010
On November 21, 2010 17:51, in comp.lang.c, (E-Mail Removed) wrote:

> On November 21, 2010 17:38, in comp.lang.c, (E-Mail Removed)lid wrote:
>
>> Lew Pitcher wrote:
>>> On November 21, 2010 16:56, in comp.lang.c, (E-Mail Removed)lid
>>> wrote:
>>>
>>>> Lew Pitcher wrote:
>>>>
>>>>> So, what is it you are trying to do?
>>>>
>>>> From RFC 1350...
>>>>
>>>> Sollins
>>>> [Page 5]
>>>>
>>>> RFC 1350 TFTP Revision 2 July
>>>> 1992
>>>>
>>>>
>>>> 2 bytes string 1 byte string 1 byte
>>>> ------------------------------------------------
>>>> | Opcode | Filename | 0 | Mode | 0 |
>>>> ------------------------------------------------


Bill, just so you can see a more concrete example, let's consider this:

I want a TFTP header that
- specifies opcode 301 (integer 301)
- specifies a file named "File"
- specifies a mode named "netascii"

*As an array of char*, this header would look like....

0x01 0x2D 'F' 'i' 'l' 'e' 0x00 'n' 'e' 't' 'a' 's' 'c' 'i' 'i' 0x00
+----+----+---+---+---+---+----+---+---+---+---+---+---+---+---+----+
hdr +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15
========== ==================== ====================================
opcode filename mode


HTH
--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
---------- Slackware - Because I know what I'm doing. ------


 
Reply With Quote
 
Serve Laurijssen
Guest
Posts: n/a
 
      11-23-2010
have you ever finished a program?


"Bill Cunningham" <(E-Mail Removed)> schreef in bericht
news:4ce98ad3$0$14804$(E-Mail Removed) ...
> I have a header that is such and I am not quite sure what the "best
> way" in C to implement it is.
>
> opcode | filename | 1 byte pad | mode | 1 byte pad
>
> I have consider malloc() and a struct as such
>
> struct tftp{
> char padd="0";
> short opcode;
> char * mode;
> char *filename;
> };
>
> Now I don't like the way "mode" and "filename" is simply a string. Is
> this the best design or does anyone have any better ideas for a hobbyist
> wannabe? Xenophobes need not apply
>
> Bill
>
>


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      11-24-2010
Lew Pitcher wrote:
> On November 22, 2010 17:44, in comp.lang.c, (E-Mail Removed)lid
> wrote:
>
>> Lew Pitcher wrote:

> [snip]
>>> Here's an example:
>>>
>>> Here's his tftp header structure (trimmed to the subset that you are
>>> looking at):
>>> struct tftphdr {
>>> short th_opcode; /* packet type */
>>> char tu_stuff[STUFFSIZE]; /* request packet stuff */
>>> char th_data[DATASIZE]; /* data or error string */
>>> };
>>>

>>
>> Can you kind of walk me through that code. It contains a
>> dereference which I still haven't quite figured out and the one
>> ampersand. I don't quite understand what that line does.
>>
>>> And, here's his code for building the header

>
> The function makerequest() will build the tftp header from several
> supplied values.
>
> Note that the caller will provide both the values that will be
> populated into the header /and/ the space for the header itself.
>
>
>>> static int makerequest(int request, const char *name,
>>> struct tftphdr *tp, const char *mode)

>
> So, the caller will call makerequest(), giving it arguments
> request - which is an int holding the value of the header opcode
> name - which is a pointer to a C character string containing the
> filename to be copied into the header filename
> tp - which is a pointer to a tftphdr structure, where this
> function will build the tftp header, and
> mode - which is a pointer to a C character string containing the
> modename to be copied into the header mode
>
> The function will return an integer value, indicating the length of
> the entire header built in the space pointed to by tp.
>
>>> {
>>> char *cp;

>
> cp is a pointer to characters, currently uninitialized.
>
>>> tp->th_opcode = htons((u_short)request);

>
> First, use the htons() function to convert the request integer value
> into a network-byte-order "short", and place that network-short in
> the tftp header opcode (tp->th_opcode).
>
>>> cp = (char *) &(tp->th_stuff);

>
> cp now points to the first character in the "stuff" area of the tftp
> header (tp->th_stuff)
>
>>> strcpy(cp, name);

>
> We copy the filename into the first characters of the "stuff" area.
> The filename will be terminated with the '\0' as per all C strings.
>
>>> cp += strlen(name);

>
> cp used to point at the beginning of "stuff". Now, it points to the
> character after the last significant character of the filename which
> was copied into "stuff". This character /should be/ the '\0'
> terminating the copied string.
>
>>> *cp++ = '\0';

>
> We write over that trailing '\0' with our own '\0' (just to be sure),
> and increment cp so that now points to the first character /after/
> the '\0' which marks the end of the filename in the tftp header
> "stuff" area.
>
>>> strcpy(cp, mode);

>
> We copy the mode string into the next characters of the "stuff" area.
> The mode string will be terminated with the '\0' as per all C strings.
>
>>> cp += strlen(mode);

>
> cp used to point at the first character following the filename in
> the "stuff" area. Now, it points to the character after the last
> significant character of the mode string which was copied into
> "stuff". This character /should be/ the '\0' terminating the copied
> string.
>
>>> *cp++ = '\0';

>
> We write over that trailing '\0' with our own '\0' (just to be sure),
> and increment cp so that now points to the first character /after/
> the '\0' which marks the end of the mode string in the tftp header
> "stuff" area.
>
>>> return (cp - (char *)tp);

>
> We compute the number of characters between the beginning of the tftp
> header and the end, and return that number to the caller. This is, of
> course, the length of the tftp header that we have constructed.
>
>>> }

>
> HTH


Ok I might comes back to this later but I have a lot to digest. Sorry
for all the repeat posts. I posted and my post didn't show up so I tried a
couple more times and then they all posted.


Bill


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      11-24-2010
Lew Pitcher wrote:

[snip]

> cp used to point at the beginning of "stuff". Now, it points to the
> character after the last significant character of the filename which
> was copied into "stuff". This character /should be/ the '\0'
> terminating the copied string.
>
>>> *cp++ = '\0';


OK why the * before cp in this examples?

> We write over that trailing '\0' with our own '\0' (just to be sure),
> and increment cp so that now points to the first character /after/
> the '\0' which marks the end of the filename in the tftp header
> "stuff" area.
>
>>> strcpy(cp, mode);

>
> We copy the mode string into the next characters of the "stuff" area.
> The mode string will be terminated with the '\0' as per all C strings.
>
>>> cp += strlen(mode);

>
> cp used to point at the first character following the filename in
> the "stuff" area. Now, it points to the character after the last
> significant character of the mode string which was copied into
> "stuff". This character /should be/ the '\0' terminating the copied
> string.
>
>>> *cp++ = '\0';

>
> We write over that trailing '\0' with our own '\0' (just to be sure),
> and increment cp so that now points to the first character /after/
> the '\0' which marks the end of the mode string in the tftp header
> "stuff" area.
>
>>> return (cp - (char *)tp);

>
> We compute the number of characters between the beginning of the tftp
> header and the end, and return that number to the caller. This is, of
> course, the length of the tftp header that we have constructed.
>
>>> }

>
> HTH


Wow I had no idea.

Bill
WHere'd you find this from what version of slackware source?



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inquiry Death from Above MCSE 4 03-19-2006 08:47 AM
data relations and datasets inquiry Joe Van Meer ASP .Net 0 05-05-2004 01:30 PM
Response to Bilal's inquiry about the $2.25 M Bilal's cousin MCSE 3 02-13-2004 01:16 AM
Inquiry about a VHDL signal tracer tool... Debashish VHDL 3 08-08-2003 06:04 PM
Databinding Inquiry Leon Shaw ASP .Net 1 07-11-2003 07:11 PM



Advertisments