Browse Source

Add extract sockaddr helper function

master
Con Kolivas 11 years ago
parent
commit
1427ac71ca
  1. 65
      src/libckpool.c
  2. 9
      src/libckpool.h

65
src/libckpool.c

@ -218,6 +218,71 @@ void _ck_wunlock(cklock_t *lock, const char *file, const char *func, const int l
_mutex_unlock(&lock->mutex, file, func, line);
}
bool extract_sockaddr(char *url, char **sockaddr_url, char **sockaddr_port)
{
char *url_begin, *url_end, *ipv6_begin, *ipv6_end, *port_start = NULL;
int url_len, port_len = 0;
char *url_address, *port;
size_t hlen;
if (!url) {
LOGWARNING("Null length url string passed to extract_sockaddr");
return false;
}
*sockaddr_url = url;
url_begin = strstr(url, "//");
if (!url_begin)
url_begin = url;
else
url_begin += 2;
/* Look for numeric ipv6 entries */
ipv6_begin = strstr(url_begin, "[");
ipv6_end = strstr(url_begin, "]");
if (ipv6_begin && ipv6_end && ipv6_end > ipv6_begin)
url_end = strstr(ipv6_end, ":");
else
url_end = strstr(url_begin, ":");
if (url_end) {
url_len = url_end - url_begin;
port_len = strlen(url_begin) - url_len - 1;
if (port_len < 1)
return false;
port_start = url_end + 1;
} else
url_len = strlen(url_begin);
if (url_len < 1) {
LOGWARNING("Null length URL passed to extract_sockaddr");
return false;
}
hlen = url_len + 1;
align_len(&hlen);
url_address = malloc(hlen);
if (unlikely(!url_address))
quit(1, "Failed to malloc url_address of length %d in extract_sockaddr", (int)hlen);
sprintf(url_address, "%.*s", url_len, url_begin);
port = malloc(8);
if (unlikely(!port))
quit(1, "Failed to malloc port in extract_sockaddr");
if (port_len) {
char *slash;
snprintf(port, 6, "%.*s", port_len, port_start);
slash = strchr(port, '/');
if (slash)
*slash = '\0';
} else
strcpy(port, "80");
*sockaddr_port = port;
*sockaddr_url = url_address;
return true;
}
void keep_sockalive(int fd)
{
const int tcp_one = 1;

9
src/libckpool.h

@ -101,6 +101,7 @@ void _mutex_init(pthread_mutex_t *lock, const char *file, const char *func, cons
void mutex_destroy(pthread_mutex_t *lock);
void _rwlock_init(pthread_rwlock_t *lock, const char *file, const char *func, const int line);
void rwlock_destroy(pthread_rwlock_t *lock);
void _cklock_init(cklock_t *lock, const char *file, const char *func, const int line);
void cklock_destroy(cklock_t *lock);
void _ck_rlock(cklock_t *lock, const char *file, const char *func, const int line);
@ -132,19 +133,23 @@ static inline bool interrupted(void)
return (errno == EINTR);
}
bool extract_sockaddr(char *url, char **sockaddr_url, char **sockaddr_port);
void keep_sockalive(int fd);
void noblock_socket(int fd);
void block_socket(int fd);
bool sock_connecting(void);
void align_len(size_t *len);
void __bin2hex(uchar *s, const uchar *p, size_t len);
void *bin2hex(const uchar *p, size_t len);
bool hex2bin(uchar *p, const uchar *hexstr, size_t len);
uchar *http_base64(const uchar *src);
void b58tobin(uchar *b58bin, const uchar *b58);
void address_to_pubkeytxn(uchar *pkh, const uchar *addr);
int ser_number(uchar *s, int32_t val);
bool fulltest(const uchar *hash, const uchar *target);
void copy_tv(tv_t *dest, const tv_t *src);
void ts_to_tv(tv_t *val, const ts_t *spec);
void tv_to_ts(ts_t *spec, const tv_t *val);
@ -154,6 +159,7 @@ void ms_to_ts(ts_t *spec, int64_t ms);
void ms_to_tv(tv_t *val, int64_t ms);
void tv_time(tv_t *tv);
void ts_time(ts_t *ts);
void cksleep_prepare_r(ts_t *ts);
void nanosleep_abstime(ts_t *ts_end);
void timeraddspec(ts_t *a, const ts_t *b);
@ -161,11 +167,14 @@ void cksleep_ms_r(ts_t *ts_start, int ms);
void cksleep_us_r(ts_t *ts_start, int64_t us);
void cksleep_ms(int ms);
void cksleep_us(int64_t us);
double us_tvdiff(tv_t *end, tv_t *start);
int ms_tvdiff(tv_t *end, tv_t *start);
double tvdiff(tv_t *end, tv_t *start);
void decay_time(double *f, double fadd, double fsecs, double interval);
void suffix_string(double val, char *buf, size_t bufsiz, int sigdigits);
double le256todouble(const uchar *target);
double diff_from_target(uchar *target);
double diff_from_header(uchar *header);

Loading…
Cancel
Save