Browse Source

Add a helper function for getting an addinfo from a url and port

master
Con Kolivas 10 years ago
parent
commit
02dfda6814
  1. 17
      src/libckpool.c
  2. 2
      src/libckpool.h

17
src/libckpool.c

@ -434,6 +434,23 @@ bool url_from_sockaddr(const struct sockaddr *addr, char *url, char *port)
return true; return true;
} }
bool addrinfo_from_url(const char *url, const char *port, struct addrinfo *addrinfo)
{
struct addrinfo *servinfo, hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
servinfo = addrinfo;
if (getaddrinfo(url, port, &hints, &servinfo) != 0)
return false;
if (!servinfo)
return false;
memcpy(addrinfo, servinfo->ai_addr, sizeof(struct addrinfo));
freeaddrinfo(servinfo);
return true;
}
void keep_sockalive(int fd) void keep_sockalive(int fd)
{ {
const int tcp_one = 1; const int tcp_one = 1;

2
src/libckpool.h

@ -14,6 +14,7 @@
#include <errno.h> #include <errno.h>
#include <jansson.h> #include <jansson.h>
#include <netdb.h>
#include <pthread.h> #include <pthread.h>
#include <signal.h> #include <signal.h>
#include <stdbool.h> #include <stdbool.h>
@ -423,6 +424,7 @@ static inline bool sock_timeout(void)
bool extract_sockaddr(char *url, char **sockaddr_url, char **sockaddr_port); bool extract_sockaddr(char *url, char **sockaddr_url, char **sockaddr_port);
bool url_from_sockaddr(const struct sockaddr *addr, char *url, char *port); bool url_from_sockaddr(const struct sockaddr *addr, char *url, char *port);
bool addrinfo_from_url(const char *url, const char *port, struct addrinfo *addrinfo);
void keep_sockalive(int fd); void keep_sockalive(int fd);
void nolinger_socket(int fd); void nolinger_socket(int fd);
void noblock_socket(int fd); void noblock_socket(int fd);

Loading…
Cancel
Save