Browse Source

Make url from sockaddr take port as a char

master
Con Kolivas 10 years ago
parent
commit
e5f26443af
  1. 11
      src/libckpool.c
  2. 2
      src/libckpool.h

11
src/libckpool.c

@ -408,9 +408,11 @@ bool extract_sockaddr(char *url, char **sockaddr_url, char **sockaddr_port)
} }
/* Convert a sockaddr structure into a url and port. URL should be a string of /* Convert a sockaddr structure into a url and port. URL should be a string of
* INET6_ADDRSTRLEN size */ * INET6_ADDRSTRLEN size, port at least a string of 6 bytes */
bool url_from_sockaddr(const struct sockaddr *addr, char *url, int *port_no) bool url_from_sockaddr(const struct sockaddr *addr, char *url, char *port)
{ {
int port_no = 0;
switch(addr->sa_family) { switch(addr->sa_family) {
const struct sockaddr_in *inet4_in; const struct sockaddr_in *inet4_in;
const struct sockaddr_in6 *inet6_in; const struct sockaddr_in6 *inet6_in;
@ -418,16 +420,17 @@ bool url_from_sockaddr(const struct sockaddr *addr, char *url, int *port_no)
case AF_INET: case AF_INET:
inet4_in = (struct sockaddr_in *)url; inet4_in = (struct sockaddr_in *)url;
inet_ntop(AF_INET, &inet4_in->sin_addr, url, INET6_ADDRSTRLEN); inet_ntop(AF_INET, &inet4_in->sin_addr, url, INET6_ADDRSTRLEN);
*port_no = htons(inet4_in->sin_port); port_no = htons(inet4_in->sin_port);
break; break;
case AF_INET6: case AF_INET6:
inet6_in = (struct sockaddr_in6 *)url; inet6_in = (struct sockaddr_in6 *)url;
inet_ntop(AF_INET6, &inet6_in->sin6_addr, url, INET6_ADDRSTRLEN); inet_ntop(AF_INET6, &inet6_in->sin6_addr, url, INET6_ADDRSTRLEN);
*port_no = htons(inet6_in->sin6_port); port_no = htons(inet6_in->sin6_port);
break; break;
default: default:
return false; return false;
} }
sprintf(port, "%d", port_no);
return true; return true;
} }

2
src/libckpool.h

@ -422,7 +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); bool url_from_sockaddr(const struct sockaddr *addr, char *url, char *port);
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