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
|
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "forward.h"
#include "log.h"
int fwd_init_ctx(forward_ctx **ctx, init_cb init_fn)
{
assert(ctx || init_fn);
if (!*ctx)
*ctx = (forward_ctx *) malloc(sizeof(**ctx));
assert(*ctx);
memset(*ctx, 0, sizeof(**ctx));
if (init_fn(*ctx))
return 1;
return 0;
}
int fwd_setup(forward_ctx *ctx, const char *host, const char *port)
{
int s;
struct addrinfo *fwd_addr = NULL;
assert(ctx);
s = socket_init_in(host, port, &fwd_addr);
if (s) {
E_GAIERR(s, "Could not initialise forward socket");
return 1;
}
if (!ctx->fwd_cbs.on_listen)
return 1;
if (ctx->fwd_cbs.on_listen(ctx, host, port))
return 1;
if (socket_connect_in(&ctx->sock, &fwd_addr)) {
E_STRERR("Connection to forward socket");
return 1;
}
s = socket_addrtostr_in(&ctx->sock, ctx->host_buf, ctx->service_buf);
if (s) {
E_GAIERR(s, "Convert forward socket address to string");
return 1;
}
if (socket_close(&ctx->sock)) {
E_STRERR("Forward socket close");
return 1;
}
return 0;
}
int fwd_validate_ctx(const forward_ctx *ctx)
{
assert(ctx);
assert(ctx->fwd_cbs.on_listen && ctx->fwd_cbs.on_shutdown);
assert(ctx->sock.addr_len > 0);
assert(strnlen(ctx->host_buf, NI_MAXHOST) > 0);
assert(strnlen(ctx->service_buf, NI_MAXSERV) > 0);
return 0;
}
int fwd_connect(forward_ctx *ctx, psocket *fwd_client)
{
assert(ctx);
socket_clone(&ctx->sock, fwd_client);
return socket_reconnect_in(fwd_client);
}
|