Browse Source

Add a helper function for converting a struct sockaddr into a url and port

master
Con Kolivas 10 years ago
parent
commit
6d40647334
  1. 25
      src/libckpool.c
  2. 1
      src/libckpool.h

25
src/libckpool.c

@ -30,6 +30,7 @@
#include <time.h> #include <time.h>
#include <math.h> #include <math.h>
#include <poll.h> #include <poll.h>
#include <arpa/inet.h>
#include "libckpool.h" #include "libckpool.h"
#include "sha2.h" #include "sha2.h"
@ -406,6 +407,30 @@ bool extract_sockaddr(char *url, char **sockaddr_url, char **sockaddr_port)
return true; return true;
} }
/* Convert a sockaddr structure into a url and port. URL should be a string of
* INET6_ADDRSTRLEN size */
bool url_from_sockaddr(const struct sockaddr *addr, char *url, int *port_no)
{
switch(addr->sa_family) {
const struct sockaddr_in *inet4_in;
const struct sockaddr_in6 *inet6_in;
case AF_INET:
inet4_in = (struct sockaddr_in *)url;
inet_ntop(AF_INET, &inet4_in->sin_addr, url, INET6_ADDRSTRLEN);
*port_no = htons(inet4_in->sin_port);
break;
case AF_INET6:
inet6_in = (struct sockaddr_in6 *)url;
inet_ntop(AF_INET6, &inet6_in->sin6_addr, url, INET6_ADDRSTRLEN);
*port_no = htons(inet6_in->sin6_port);
break;
default:
return false;
}
return true;
}
void keep_sockalive(int fd) void keep_sockalive(int fd)
{ {
const int tcp_one = 1; const int tcp_one = 1;

1
src/libckpool.h

@ -422,6 +422,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, int *port_no);
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