37#if OPENMRN_FEATURE_BSD_SOCKETS
77 const char *host,
const char *port_str)
81 memset(&hints, 0,
sizeof(hints));
87 if (
int ai_ret =
getaddrinfo(host, port_str, &hints, &addr) != 0 || !addr)
89 LOG_ERROR(
"getaddrinfo failed for '%s': %s", host,
103 LOG(
INFO,
"Connected to %s:%s. fd=%d", host ? host :
"mDNS", port_str,
111#if OPENMRN_FEATURE_BSD_SOCKETS_IGNORE_SIGPIPE
114 signal(SIGPIPE, SIG_IGN);
124 LOG_ERROR(
"socket: %s", strerror(errno));
131 LOG_ERROR(
"connect: %s", strerror(errno));
144 struct addrinfo *addr,
string *host,
int *port)
153 const char *n =
nullptr;
161 *port =
ntohs(sa->sin_port);
165#if OPENMRN_HAVE_BSD_SOCKETS_IPV6
168 auto *sa = (
struct sockaddr_in6 *)addr->
ai_addr;
169 *port =
ntohs(sa->sin6_port);
175 LOG(
INFO,
"unsupported address type.");
176 errno = EAFNOSUPPORT;
182 LOG(
INFO,
"Failed to convert sockaddr to string: %s", strerror(errno));
199 struct ifaddrs *ifa_free = ifa;
212 if (ai_addr_in->sin_addr.s_addr ==
213 if_addr_in->sin_addr.s_addr)
230 string hostname,
int port)
233 p->staticHost_ = std::move(hostname);
234 p->staticPort_ = port;
239 string hostname,
int port,
string mdns_service)
242 p->staticHost_ = std::move(hostname);
243 p->staticPort_ = port;
244 p->mdnsService_ = std::move(mdns_service);
int setsockopt(int socket, int level, int option_name, const void *option_value, socklen_t option_len)
Set the socket options.
int socket(int domain, int type, int protocol)
Create an unbound socket in a communications domain.
Default implementation that supplies parametrized values for static and mdns connection methods.
static std::unique_ptr< SocketClientParams > from_static(string hostname, int port)
static std::unique_ptr< SocketClientParams > from_static_and_mdns(string hostname, int port, string mdns_service)
static bool local_test(struct addrinfo *addr)
Test if a given address is local.
static bool address_to_string(struct addrinfo *addr, string *host, int *port)
Converts a struct addrinfo to a dotted-decimal notation IP address.
static int connect(const char *host, int port)
Connects a tcp socket to the specified remote host:port.
std::unique_ptr< struct addrinfo, AddrInfoDeleter > AddrinfoPtr
Custom unique pointer that knows how to delete a struct addrinfo.
static AddrinfoPtr string_to_address(const char *host, int port)
Converts a hostname string and port number to a struct addrinfo.
#define IPPROTO_TCP
TCP Raw Socket.
#define ntohs(x)
Converts a network endian short value to host endian.
#define ERRNOCHECK(where, x...)
Calls the function x, and if the return value is negative, prints errno as error message to stderr an...
#define LOG(level, message...)
Conditionally write a message to the logging output.
static const int INFO
Loglevel that is printed by default, reporting some status information.
#define LOG_ERROR(message...)
Shorthand for LOG(LEVEL_ERROR, message...). See LOG.
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
const char * gai_strerror(int __ecode)
see 'man gai_strerror'
int getifaddrs(struct ifaddrs **ifap)
Create a linked list of structures describing the network interfaces of the local system.
int getaddrinfo(const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res)
see 'man getaddrinfo'
const char * inet_ntop(int af, const void *src, char *dst, socklen_t size)
Convert the network address in src to a character string in src.
void freeifaddrs(struct ifaddrs *ifa)
Free a previously generated linked list of structures describing the network interfaces of the local ...
#define SOCK_STREAM
TCP Socket.
#define AF_INET
IPv4 Socket (UDP, TCP, etc...)
#define AF_INET6
IPv6 Socket (UDP, TCP, etc...)
int ConnectSocket(const char *host, int port)
Connects a tcp socket to the specified remote host:port.
Structure to contain information about address of a service provider.
struct sockaddr * ai_addr
Socket address for socket.
int ai_socktype
Socket type.
int ai_protocol
Protocol for socket.
socklen_t ai_addrlen
Length of socket address.
int ai_family
Protocol family for socket.
network interface address list member
struct ifaddrs * ifa_next
next item in list
struct sockaddr * ifa_addr
address of interface
Structure describing an Internet socket address.
uint16_t sa_family
address family (e.g.
#define TCP_NODELAY
don't delay send to coalesce packets