Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how should a sockaddr struct be filled in for a normal web address?



On Oct 14, 2006, at 7:59 PM, Ben Dougall wrote:
I want to call this function:

Boolean SCNetworkCheckReachabilityByAddress (
    const struct sockaddr *address,
    int addrlen,
    SCNetworkConnectionFlags *flags );

A struct sockaddr is:

struct sockaddr {
	u_char sa_len;		/* total length */
	u_char sa_family;		/* address family */
	char sa_data[14];		/* actually longer; address value */
};

I don't know how the struct should be filled in in order to call the above function. Can anyone point me to some example code that fills that struct in for a normal web address e.g. amazon.com?

Others have answered this by pointing out a better function to use, but for the sake of completeness, here is the answer to your original question:


You don't use an actual struct sockaddr. What you want is a struct sockaddr_in; that's why there's an int addrlen parameter, because sockaddr is a generic struct that you don't actually use except as a placeholder for passing "any kind of socket address". struct sockaddr_in looks like:

struct sockaddr_in {
        __uint8_t       sin_len;
        sa_family_t     sin_family;
        in_port_t       sin_port;
        struct  in_addr sin_addr;
        char            sin_zero[8];            /* XXX bwg2001-004 */
};

sin_len - another duplication of the address structure length; set it to sizeof( struct sockaddr_in )

sin_family - PF_INET for IPv4. You can't use IPv6 addresses with this structure.

sin_port - The port on which you will be contacting the address. For HTTP, usually 80, but if using a URL, you should first check the protocol and port if any are given therein. Note that this value must given in big endian regardless of host machine.

sin_addr - A struct in_addr structure with one member: in_addr_t s_addr. This is the most important one, since it is a four-byte big- endian representation of the IPv4 address you wish to contact. You can't put a textual domain name such as "amazon.com" here; you must first resolve the hostname using the gethostbyname() family of APIs. These APIs are not reentrant and therefore can't be used safely in from multiple threads without an exclusive lock. Further, a single hostname can and often will resolve to more than one IPv4 address; a few may even resolve to IPv6 addresses that you can't pass here.

sin_zero - As the name suggests, set this to zero; it exists to make the length of struct sockaddr_in equal to that of struct sockaddr.

All in all, this is a great deal of unnecessary work that is better done by APIs designed for the purpose. You shouldn't have to deal with resolving names and filling out socket address structures unless you're doing your own socket I/O at the BSD level without help from Carbon or Cocoa.

-- Gwynne, daughter of the Code
"This whole world is an asylum for the incurable."
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/darwin-dev/email@hidden

This email sent to email@hidden
References: 
 >how should a sockaddr struct be filled in for a normal web address? (From: Ben Dougall <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.