Solved

caddr_t

Posted on 1998-08-26
14
3,164 Views
Last Modified: 2013-12-26
In Stevens' book "Unix Network Programming" is the source code for an Internet ping client. The following part of the code, however, puzzles me:

struct sockaddr_in      dest;
...

host = gethostbyname(argv[0]);
...

bcopy(host->h_addr, (caddr_t) &dest.sin_addr, host->h_length);

What I don't understand is why the h_addr member of the structure pointed to by host is copied to the sin_addr member of the dest structure; I'd have thought that it would be copied to dest.sin_addr.s_addr instead. Or does the caddr_t cast enable the programmer to ignore the fact that sin_addr is itself a structure? Also, what exactly is caddr_t? Kernighan and Ritchie's book makes no mention of that data type.


The program also makes reference to a function called setlinebuf. I can't find it elsewhere in the program; is it a standard library routine?

Thanks,

David King.
0
Comment
Question by:sevrin
14 Comments
 

Expert Comment

by:izar
Comment Utility
caddr_t is a typedef ("_t") to a char ("c") pointer ("addr"). That also explains the bcopy.

setlinebuf should be defined in your stdlib.h and be on yout man
pages.


0
 

Author Comment

by:sevrin
Comment Utility
Thanks for answering... But you don't really answer the question.
0
 

Expert Comment

by:izar
Comment Utility
uh? what is left to answer ?
0
 
LVL 3

Expert Comment

by:braveheart
Comment Utility
typedef char *   caddr_t;   from /usr/include/sys/types.h

It is used as a core (i.e. memory) address pointer.

bcopy takes "void *" arguments, i.e. anything so it is essential
to cast them before calling.
0
 
LVL 2

Expert Comment

by:JYoungman
Comment Utility
IIRC the "c" in caddr_t stands for "core".  caddr_t used to be used for what void* is used for nowadays.   caddr_t was the type used to hold the memory address of some random object.  IIRC this usage was invented by the MIT X Consortium implementors for use in Xlib.

0
 

Author Comment

by:sevrin
Comment Utility
Thanks... this answers part of what I asked, but what about what I said above:

What I don't understand is why the h_addr member of the structure pointed to by host is copied to the sin_addr
    member of the dest structure; I'd have thought that it would be copied to dest.sin_addr.s_addr instead.
0
 

Expert Comment

by:izar
Comment Utility
look into netinet/in.h. You'll see that dest.sin_addr is a in_addr structure, and that s_addr is a define for S_un.S_addr,
so you are right in your thinking - this is simply a compatibility/readability thing.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:sevrin
Comment Utility
I know that dest.sin_addr is an in_addr struct. But s_addr is a member of the in_addr struct - how is what you're saying relevant? More importantly - and I repeat my question - how can an address be copied to dest.sin_addr rather than to dest.sin_addr.s_addr?
0
 

Expert Comment

by:izar
Comment Utility
the s_addr is an "alias", a define, for the sin_addr.s_addr
field. it is just syntactic sugar. Check out the include files.

0
 

Author Comment

by:sevrin
Comment Utility
But in the program I'm interested in, s_addr doesn't enter the picture at all... That's what I don't understand. The address is being copied into sin_addr, ignoring the fact that sin_addr itself contains a struct.
0
 

Expert Comment

by:izar
Comment Utility
oh.ok.
struct in_addr {
        union {
                struct { u_char s_b1, s_b2, s_b3, s_b4; } S_un_b;
                struct { u_short s_w1, s_w2; } S_un_w;
                u_long S_addr;
        } S_un;

struct sockaddr_in {
        short   sin_family;
        u_short sin_port;
        struct  in_addr sin_addr;
        char    sin_zero[8];
};

in_addr is a struc, right, but with a union as its single field. The bcopy works as it should, copying 4 bytes in place. The union and the aliases I was referring to are more usefull when getting data out of the struct.
sorry for the misunderstanding.
0
 

Author Comment

by:sevrin
Comment Utility
For a start, your prototypes are different from mine (probably just reflecting slight differences in operating system though). I have

struct sockaddr_in {
      short            sin_family;
      u_short            sin_port;
      struct in_addr      sin_addr;
      char            sin_zero[8];
};

This, obviously, is exactly what you have.... However, I have:

struct in_addr {
      u_long            s_addr;
};

You, on the other hand, have:

struct in_addr {
              union {
                      struct { u_char s_b1, s_b2, s_b3, s_b4; } S_un_b;
                      struct { u_short s_w1, s_w2; } S_un_w;
                      u_long S_addr;
              } S_un;

But this doesn't change the point I'm making, which is, according to the rules of C, the bcopy, on your system, should copy the address to dest.sin_addr.S_addr (because S_addr is the address field, and it's a member of a union (on your system), which, in turn, is a member of a struct.) But in the program I'm talking about it doesn't copy to this; it seems to pretend that in_addr simply contains an integer field, rather than a union, and blithely copies it without even referring to the union.
0
 
LVL 3

Accepted Solution

by:
dhm earned 60 total points
Comment Utility
Your hunch that something's wrong with the bcopy() is correct; the code is type-sloppy.  It doesn't make any difference in this case because the underlying data that you're trying to copy is just a four-contiguous-byte IP address.  C was/is a tough language to be meticulous about types in; my system headers define a struct in_addr as the union you had in your last comment, but the h_addr field of a struct hostent is a char *(!).  There's no way to reconcile those two, other than casting them to whatever you know they really are.  C++ has a more refined type system, but on many machines, you're still stuck with the old system header files so you can't really be type-clean.

caddr_t is, as others have said, either a "char address" or a "core address".  I don't know which.  The type is used when you want to clue the user in to the fact that the function will be reading from or writing to a block of memory, but there are no particular requirements on the block's size or contents.  It fits bcopy() well: the function just copies bytes from one place to another.  It doesn't care what it's copying.  In more current usage, void* indicates the same thing -- an address that will probably be used for reading from or writing to, but there are *no* requirements on the contents of the memory.

Setlinebuf is mentioned in my IRIX man pages:
---
Setbuffer and setlinebuf are provided for compatibility with 4.3BSD.  Setbuffer, an alternate form of setbuf, is used after a stream unit has been opened but before it is read or written. The character array buf whose size is determined by the size argument is used instead of an automatically allocated buffer.  If buf is the constant pointer NULL, input/output will be completely unbuffered.

Setlinebuf is used to change stdout or stderr from fully buffered or unbuffered to line buffered.  Unlike the other routines, it can be used at any time that the file descriptor is active.
---
I don't know any more about it than that; stdio is pretty crufty, and if you're finding that you need to tweak its behavior by setting buffering modes or whatever, you're often better off writing the code to do exactly what you want.
0
 

Author Comment

by:sevrin
Comment Utility
Thanks, dhm! I had a hunch it was sloppy coding, but I couldn't seem to get others to confirm it! I'll get back to your response to my other question when I get back from work.

Best,

David
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now