Professional Web Applications Themes

Casting structure pointers - UNIX Programming

I have read in books about networking in Unix that pointers to struct sockaddr and struct sockaddr_in can be casted one to another. This involves assumptions about alignment and basic type sizes that are not guaranteed by the C standard. Why those casts work in Unix? -- fxn...

  1. #1

    Default Casting structure pointers

    I have read in books about networking in Unix that pointers to struct
    sockaddr and struct sockaddr_in can be casted one to another. This
    involves assumptions about alignment and basic type sizes that are not
    guaranteed by the C standard.

    Why those casts work in Unix?

    -- fxn
    Xavier Guest

  2. #2

    Default Re: Casting structure pointers

    On 19 Feb 2004 07:09:43 -0800,
    Xavier Noria <com> wrote:

     


    That works because sockaddr and sockaddr_in structures are defined
    that way. Remember you can pass other sockaddr types to the bind() call
    such as sockaddr_un for defining local socket addresses (aka unix socket)
    or sockaddr_ipx for defining IPX socket addresses.

    Villy
    Villy Guest

  3. #3

    Default Re: Casting structure pointers

    ohout.pharmapartners.nl (Villy Kruse) wrote in message news:<ohout.pharmapartners.nl>... 
    > That works because sockaddr and sockaddr_in structures are defined
    > that way.[/ref]

    Are defined which way?

    The underlying problem is that given this code:

    struct foo { char c[2]; };
    struct bar { char c0; char c1; };

    struct foo f;
    struct bar b, *bp;

    f.c[1] = 'a';
    bp = (struct bar*) &f;

    you just don't know whether bp->c1 holds 'a' in ANSI C. So we are
    relying on something that happens "in Unix", whatever that means,
    albeit it is not guaranteed by the standard.

    I guess that might be some well-known assumption that holds "in Unix"
    because even Steven's book does not mention that issue. My question
    is: what are we relying on?

    -- fxn
    Xavier Guest

  4. #4

    Default Re: Casting structure pointers


    "Xavier Noria" <com> wrote in message
    news:google.com...

     

     [/ref]

     
    >> That works because sockaddr and sockaddr_in structures are defined
    >> that way.[/ref]
    >
    > Are defined which way?[/ref]


    Are defined so that one is a more specific form of the other.

     

    Right.
     


    It is guaranteed by POSIX.

     


    We are relying upon the guarantees provided by the POSIX 1003.1 and
    SuSv3 standards. Here are some excerpts from SuSv3 (Single Unix
    Specification, Version 3) that articulate parts of this requirement:

    When a sockaddr_storage structure is cast as a sockaddr structure, the
    ss_family field of the sockaddr_storage structure shall map onto the
    sa_family field of the sockaddr structure. When a sockaddr_storage structure
    is cast as a protocol-specific address structure, the ss_family field shall
    map onto a field of that structure that is of type sa_family_t and that
    identifies the protocol's address family.

    The sockaddr_storage structure defined in <sys/socket.h> shall be large
    enough to accommodate a sockaddr_in structure (see the <netinet/in.h> header
    defined in the Base Definitions volume of IEEE Std 1003.1-2001, Chapter 13,
    Headers) and shall be aligned at an appropriate boundary so that pointers to
    it can be cast as pointers to sockaddr_in structures and used to access the
    fields of those structures without alignment problems. When a
    sockaddr_storage structure is cast as a sockaddr_in structure, the ss_family
    field maps onto the sin_family field.

    The sockaddr_in structure is used to store addresses for the Internet
    address family. Values of this type shall be cast by applications to struct
    sockaddr for use with socket functions.

    DS



    David Guest

Similar Threads

  1. Replies: 1
    Last Post: May 1st, 03:24 PM
  2. Pointers on how to do the following
    By Jay in forum PHP Development
    Replies: 2
    Last Post: January 10th, 10:21 PM
  3. Process folder structure into new structure
    By Bryan in forum PERL Beginners
    Replies: 3
    Last Post: April 27th, 04:46 AM
  4. ruby and pointers
    By Elias in forum Ruby
    Replies: 5
    Last Post: February 4th, 06:10 PM
  5. Pointers
    By Eric Walker in forum PERL Beginners
    Replies: 11
    Last Post: November 22nd, 04:10 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139