Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the "hints" mean for the addrinfo name in socket programming

Tags:

c

sockets

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, to better understand the struct.

Thanks in advance.


Thanks for the answers. Maybe I wasn't clear:

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

like image 818
Ricky Avatar asked Apr 16 '09 09:04

Ricky


People also ask

What is Ai_addrlen?

ai_addrlen. The length of the socket address structure pointed to by the ai_addr field. The value returned in this field can be used as the arguments for the connect() or bind() call with this socket type, according to the AI_PASSIVE flag.

What is the use of Getaddrinfo?

The getaddrinfo() function translates the name of a service location (for example, a host name) and/or service name and returns a set of socket addresses and associated information to be used in creating a socket with which to address the specified service.

What is Ai_family?

ptr->ai_family is just an integer, a member of a struct addrinfo. ( And if you are wondering about the particular syntax of ptr-> , you can go through this question ), it will have a value of either AF_INET or AF_INET6 (Or in theory any other supported protocol)

What is Af_unspec?

A value of AF_UNSPEC for ai_family indicates the caller will accept any protocol family. This value can be used to return both IPv4 and IPv6 addresses for the host name pointed to by the pNodeName parameter.


2 Answers

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.

like image 72
Brian Campbell Avatar answered Sep 18 '22 19:09

Brian Campbell


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.

like image 39
PaulJWilliams Avatar answered Sep 18 '22 19:09

PaulJWilliams