Compile the 'sock' program in the book 'TCP/IP Illustrated Vol.1'

Discussion in 'Linux Networking' started by Steven Woody, Nov 20, 2005.

  1. Steven Woody

    Steven Woody Guest

    hi,

    i am running Linux and want to compile and use the 'sock' program comes with
    Stevens's book 'TCP/IP Illustrated Vol.1'. but the code simply can't pass the
    compiler with following error,

    ,----
    | bash-3.00$ make
    | gcc -ansi -Wall -Dsun -D__STDC__=0 -c -o loop.o loop.c
    | <command line>:5:1: warning: "__STDC__" redefined
    | loop.c: In function `loop':
    | loop.c:117: error: `caddr_t' undeclared (first use in this function)
    | loop.c:117: error: (Each undeclared identifier is reported only once
    | loop.c:117: error: for each function it appears in.)
    | loop.c:127: error: parse error before numeric constant
    | make: *** [loop.o] Error 1
    `----

    in fact, in the Linux system, there is a sys/types.h which defined the caddr_t
    type and this file seems have been already included in the source code, so i
    don't know why the type still can not be recognized.

    had anyone here compiled the code with success? or, is there any other similar
    tool freely available ?

    thanks.

    --
    steven woody (id: narke)

    Jesse: You want to know why I wrote that stupid book?
    Celine: Why?
    Jesse: So that you might come to a reading in Paris and I could walk
    up to you and ask, "Where the **** were you?"
    Celine: [laughing] No - you thought I'd be here today?
    Jesse: I'm serious. I think I wrote it, in a way, to try to find you.
    Celine: Okay, that's - I know that's not true, but that's sweet of you
    to say.
    Jesse: I think it is true.

    - Before Sunset (2004)
     
    Steven Woody, Nov 20, 2005
    #1
    1. Advertisements

  2. Why are you defining __STDC__ at all, much less to 0? __STDC__
    should always be defined by the implementation. If this is from the
    makefile that you got with the source for sock, be aware that _TCP/IP
    Illustrated_ v1, while an excellent book, is more than ten years old,
    and the makefile might require some reworking for newer systems.
    (Though even in 1994 defining __STDC__ explicitly was almost certain-
    ly wrong.)
    Because it hasn't been defined. Including sys/types.h very likely
    doesn't guarantee that a particular typedef in it has been processed,
    because in most implementations sys/types.h is full of conditional-
    inclusion directives (#if and friends).

    In the Linux sys/types.h I'm looking at now, caddr_t is only defined
    if the macro __USE_BSD is defined. Note that this is an identifier
    reserved to the implementation, so you shouldn't be defining it
    (unless instructed to by the implementation's documentation - and
    that would be a poor choice by the implementor).

    As it turns out, __USE_BSD is defined for you (in features.h, which
    is automatically included by a raft of other headers) if the "feature
    test macro" _BSD_SOURCE is defined. You define _BSD_SOURCE and you
    get caddr_t. The GCC default on Linux (and, I think, most Unix
    platforms, using the stock GCC build) is to define _BSD_SOURCE unless
    you use the -ansi flag. If you do use -ansi (or an equivalent), you
    have to define _BSD_SOURCE to get the BSD types, macros, and so on.

    You can read more about feature test macros in the Single Unix
    Specification, which is available free of charge at [1].

    So as a first attempt, I suggest getting rid of that bogus definition
    of __STDC__ and substituting a -D_BSD_SOURCE.
    Stevens mentions some similar tools right in _TCP/IP Illustrated_, in
    the appendix that discusses the sock program. One he doesn't mention
    (I believe it appeared later) is netcat, which you may already have
    as "nc".


    1. http://www.opengroup.org/
     
    Michael Wojcik, Nov 21, 2005
    #2
    1. Advertisements

  3. Steven Woody

    Steven Woody Guest

    thank you and i will try. but 'nc' you mentioned is enough to me.
     
    Steven Woody, Nov 21, 2005
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.