-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathproxy.c
More file actions
225 lines (191 loc) · 5.64 KB
/
proxy.c
File metadata and controls
225 lines (191 loc) · 5.64 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* Copyright 37pool.com. All rights reserved.
*
* Maintainer:
* Huang Le <4tar@37pool.com>
*
* The software is published under GNU Affero General Public License version 3,
* or GNU AGPL v3. The GNU AGPL is based on the GNU GPL, but has an additional
* term to allow users who interact with the licensed software over a network to
* receive the source for that program. For more information, please refer to:
*
* http://www.gnu.org/licenses/agpl.html
* http://www.gnu.org/licenses/why-affero-gpl.html
*
* WARN: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "defs.h"
#ifndef WIN32
#include <netinet/in.h> /* INET6_ADDRSTRLEN */
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef INET6_ADDRSTRLEN
# define INET6_ADDRSTRLEN 63
#endif
typedef struct {
uv_getaddrinfo_t gar_req;
pool_config *conf;
} gar_state;
static void pool_resolved( uv_getaddrinfo_t *req, int status,
struct addrinfo *addrs )
{
gar_state *state;
pool_config *conf;
struct addrinfo *ai;
const void *addrv;
uv_loop_t *loop;
pool_ctx *px;
int n;
union {
struct sockaddr addr;
struct sockaddr_in addr4;
struct sockaddr_in6 addr6;
} s;
state = CONTAINER_OF(req, gar_state, gar_req);
conf = state->conf;
loop = conf->conf->loop;
if (status) {
pr_err("getaddrinfo(%s): %s", conf->host, uv_strerror(status));
uv_freeaddrinfo(addrs);
return;
}
for (n = 0, ai = addrs; ai != NULL; ai = ai->ai_next)
if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6)
++n;
if (!n) {
pr_err("%s has no IPv4/6 addresses", conf->host);
uv_freeaddrinfo(addrs);
return;
}
conf->px = xmalloc(sizeof(*conf->px));
memset(conf->px, 0, sizeof(*conf->px));
n = rand() % n;
for (ai = addrs; ai != NULL; ai = ai->ai_next) {
if (ai->ai_family == AF_INET) {
s.addr4 = *(const struct sockaddr_in *)ai->ai_addr;
s.addr4.sin_port = htons(conf->port);
addrv = &s.addr4.sin_addr;
} else if (ai->ai_family == AF_INET6) {
s.addr6 = *(const struct sockaddr_in6 *)ai->ai_addr;
s.addr6.sin6_port = htons(conf->port);
addrv = &s.addr6.sin6_addr;
} else
continue;
if (n-- == 0) {
px = conf->px;
uv_inet_ntop(s.addr.sa_family, addrv, px->addr, sizeof(px->addr));
px->conf = conf;
px->loop = loop;
pool_connect(px, &s.addr);
break;
}
}
uv_freeaddrinfo(addrs);
}
extern void miner_connected( uv_stream_t *proxy_tcp_handle, int status );
pool_ctx *pool_pickup( proxy_config *conf )
{
pool_ctx *px, *cpx;
unsigned short i;
for (px = NULL, i = 0; i < conf->count; ++i) {
cpx = conf->pools[i].px;
if (cpx->status == p_working && cpx->count < countof(cpx->mx) &&
(!px || (px->conf->priority > cpx->conf->priority) ||
(px->conf->priority == cpx->conf->priority &&
px->count > cpx->count)))
px = cpx;
}
return px;
}
void attach_miner_to_pool( pool_ctx *px, miner_ctx *mx )
{
unsigned short i, j;
mx->px = px;
for (i = px->count, j = 0;
px->mx[i] && j < countof(px->mx);
++j, i = (i + 1) % countof(px->mx));
px->mx[i] = mx;
++px->count;
memcpy(&mx->sctx, &px->sctx, STRATUM_SESSION_SIZE);
memset(STRATUM_SESSION_POS(&mx->sctx), 0,
sizeof(mx->sctx) - STRATUM_SESSION_SIZE);
mx->sctx.xn1size += 2;
mx->sctx.xn2size -= 2;
mx->sctx.isServer = mx->sctx.authorized = 0;
mx->sctx.jobUpdated = mx->sctx.diffUpdated = 0;
mx->sctx.sdiff = mx->sctx.shareCount = mx->sctx.denyCount = 0;
mx->sctx.jobLen = 0;
#ifdef WORDS_BIGENDIAN
sprintf(&mx->sctx.xn1[px->sctx.xn1size * 2], "%04x", i);
#else
sprintf(&mx->sctx.xn1[px->sctx.xn1size * 2], "%02x%02x", i & 0xff, i >> 8);
#endif
mx->sctx.cx = mx;
mx->pxreconn = 0;
mx->writeShareLen = mx->lastShareLen = mx->shareLen = 0;
}
void detach_miner_from_pool( miner_ctx *mx )
{
unsigned short i;
pool_ctx *px = mx->px;
mx->px = NULL;
hex2bin((unsigned char *)&i, &mx->sctx.xn1[px->sctx.xn1size * 2], 4);
ASSERT(px->mx[i] == mx);
px->mx[i] = NULL;
--px->count;
}
int proxy_run( proxy_config *conf )
{
struct addrinfo hints;
gar_state p_state[8];
int i, err;
union {
struct sockaddr addr;
struct sockaddr_in addr4;
struct sockaddr_in6 addr6;
} s;
if (!uv_ip4_addr(conf->host, conf->port, &s.addr4));
else if (!(err = uv_ip6_addr(conf->host, conf->port, &s.addr6)));
else {
pr_err("Invalid proxy listen addr: %s:%hu", conf->host, conf->port);
return err;
}
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = 0;
for (i = 0; i < (int)conf->count; ++i) {
p_state[i].conf = &conf->pools[i];
err = uv_getaddrinfo(conf->loop, &p_state[i].gar_req, pool_resolved,
conf->pools[i].host, NULL, &hints);
if (err) {
pr_err("getaddrinfo: %s", uv_strerror(err));
return err;
}
}
uv_tcp_init(conf->loop, &conf->handle.tcp);
if ((err = uv_tcp_bind(&conf->handle.tcp, &s.addr, 0))) {
pr_err("uv_tcp_bind(%s:%hu): %s", conf->host, conf->port,
uv_strerror(err));
return err;
}
if ((err = uv_listen(&conf->handle.stream, 128, miner_connected))) {
pr_err("uv_tcp_bind(%s:%hu): %s", conf->host, conf->port,
uv_strerror(err));
return err;
}
pr_info("Listening on %s:%hu", conf->host, conf->port);
if (uv_run(conf->loop, UV_RUN_DEFAULT))
abort();
uv_loop_delete(conf->loop);
for (i = 0; i < (int)conf->count; ++i)
free(conf->pools[i].px);
return 0;
}