Hi guy I’m following a tutorial about sockets on Linux/ubuntu using C.
When I want to config the hints.ai_flags with AI_PASSIVE vs code response that is undefined. I searched in linux terminal with man getaddrinfo
and it is in the manual. Anyone can help me with this?
I’ll shared part of my code:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
struct addrinfo
{
int ai_flags; // AI_PASSIVE, AI_CANONNAME, etc.
int ai_family; // AF_INET, AF_INET6, AF_UNSPEC
int ai_socktype; // SOCK_STREAM, SOCK_DGRAM
int ai_protocol; // use 0 for "any"
size_t ai_addrlen; // size of ai_addr in bytes
struct sockaddr *ai_addr; // struct sockaddr_in or _in6
char *ai_canonname; // full canonical hostname
struct addrinfo *ai_next; // linked list, next node
};
int getaddrinfo(const char *node, // e.g "www.example.com" or IP
const char *service, // e.g "http" or port number
const struct addrinfo *hints,
struct addrinfo **res);
int main()
{
int status;
struct addrinfo hints;
struct addrinfo *servinfo; // will point to the result
memset(&hints, 0, sizeof hints); // make sure the struct is empty
hints.ai_family = AF_UNSPEC; // don't care IPv4 IPv6
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
hints.ai_flags = AI_PASSIVE; // fill in my IP for me -> here appears undefined
}
If I declare AI_PASSIVE like this, disappear the error:
#ifndef AI_PASSIVE
#define AI_PASSIVE 0x01
#endif
New contributor
0xfsaymyname is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.