Get detailed addressinfo from the parsed URL for future raw socket usage when possible. IPV4 only for now.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
diff --git a/cgminer.c b/cgminer.c
index 67197c3..000782c 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -554,7 +554,8 @@ static char *set_url(char *arg)
arg = get_proxy(arg, pool);
- extract_sockaddr(pool, arg);
+ if (!extract_sockaddr(pool, arg))
+ return "Failed to extract address from parsed url";
opt_set_charp(arg, &pool->rpc_url);
if (strncmp(arg, "http://", 7) &&
diff --git a/miner.h b/miner.h
index 175a3fc..572444b 100644
--- a/miner.h
+++ b/miner.h
@@ -813,7 +813,7 @@ struct pool {
struct cgminer_pool_stats cgminer_pool_stats;
SOCKETTYPE sock;
- struct sockaddr_in server, client;
+ struct sockaddr_in *server, client;
};
#define GETWORK_MODE_TESTPOOL 'T'
diff --git a/util.c b/util.c
index ef5232b..6ac8064 100644
--- a/util.c
+++ b/util.c
@@ -796,12 +796,12 @@ double tdiff(struct timeval *end, struct timeval *start)
return end->tv_sec - start->tv_sec + (end->tv_usec - start->tv_usec) / 1000000.0;
}
-void extract_sockaddr(struct pool *pool, char *url)
+bool extract_sockaddr(struct pool *pool, char *url)
{
- char *url_begin, *url_end, *url_address;
- char *port_start, port80[3] = "80";
+ char *url_begin, *url_end, *port_start;
+ char *url_address, *port;
struct addrinfo hints, *res;
- size_t url_len, port_len;
+ size_t url_len, port_len = 0;
url_begin = strstr(url, "//");
if (!url_begin)
@@ -810,19 +810,38 @@ void extract_sockaddr(struct pool *pool, char *url)
url_begin += 2;
url_end = strstr(url_begin, ":");
if (url_end) {
- url_len = url_end - 1 - url_begin;
+ url_len = url_end - url_begin;
port_len = strlen(url_begin) - url_len - 1;
- if (port_len <= 1)
- return;
+ if (port_len < 1)
+ return false;
port_start = url_end + 1;
- } else {
+ } else
url_len = strlen(url_begin);
- port_start = port80;
+
+ if (url_len < 1)
+ return false;
+
+ url_address = alloca(url_len + 1);
+ sprintf(url_address, "%.*s", url_len, url_begin);
+
+ if (port_len) {
+ port = alloca(port_len + 1);
+ sprintf(port, "%.*s", port_len, port_start);
+ } else {
+ port = alloca(4);
+ strcpy(port, "80");
}
- if (url_len <= 1)
- return;
+ memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
+
+ if (getaddrinfo(url_address, port, &hints, &res)) {
+ applog(LOG_DEBUG, "Failed to extract sock addr");
+ return false;
+ }
+
+ pool->server = (struct sockaddr_in *)res->ai_addr;
+ return true;
}
diff --git a/util.h b/util.h
index b8c062b..c99ecfd 100644
--- a/util.h
+++ b/util.h
@@ -109,6 +109,6 @@
#endif
#endif
struct pool;
-void extract_sockaddr(struct pool *pool, char *url);
+bool extract_sockaddr(struct pool *pool, char *url);
#endif /* __UTIL_H__ */