tags:

views:

768

answers:

3

When doing socket programming, people always name the addrinfo struct like this:

struct addrinfo hints;
// get ready to connect
status = getaddrinfo("www.example.net", "3490", &hints, &servinfo);

I'd like to know what it stands for for better understanding.

Thanks in advance.


Thanks for the answers. Maybe I didn't make me clear.

But I want to know whether the variable name "hints" is some abbreviation for some words? Or it's just the word "hints" means it only give some address info and let the getaddrinfo() function to fill in the rest ones?

A: 

struct addrinfo hints; is just a variable declaration. The struct itself is defined in a library and included via sys/socket.h.

dsm
+2  A: 

From http://linux.die.net/man/3/getaddrinfo

The hints parameter specifies the preferred socket type, or protocol. A NULL hints specifies that any network address or protocol is acceptable. If this parameter is not NULL it points to an addrinfo structure whose ai_family, ai_socktype, and ai_protocol members specify the preferred socket type. AF_UNSPEC in ai_family specifies any protocol family (either IPv4 or IPv6, for example). 0 in ai_socktype or ai_protocol specifies that any socket type or protocol is acceptable as well. The ai_flags member specifies additional options, defined below. Multiple flags are specified by logically OR-ing them together. All the other members in the hints parameter must contain either 0, or a null pointer.

Visage
So from this, it follows that the example code is incorrect as shown; it fails to actually initialize the hints.ai_family field.
unwind
A: 

From the FreeBSD man page:

hints is an optional pointer to a struct addrinfo, as defined by <netdb.h> ... This structure can be used to provide hints concerning the type of socket that the caller supports or wishes to use.

It's called "hints" because it can be used to provide, well, hints (in the sense of a tip; a suggestion that might come in useful but could be ignored). This indicates things like what protocol family (IPv4 vs. IPv6, for example) the caller wants, what socket type (datagram vs. straming), what protocol (TCP vs. UDP), etc. You can pass NULL in for hints and thus indicate that you don't care what protocol family, socket type, or protocol you get back.

Brian Campbell