-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsocks5_authenticator.c
81 lines (68 loc) · 2.17 KB
/
socks5_authenticator.c
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
#include <errno.h>
#include <stdlib.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include "logging.h"
#include "socks5_authenticator.h"
struct authenticator *authenticator_new(uint8_t auth_method,
struct authenticator_imp ops, void *inner,
struct bufferevent *underlying_bev)
{
struct evbuffer *input = evbuffer_new();
struct evbuffer *output = evbuffer_new();
struct authenticator *authenticator = NULL;
if (input == NULL || output == NULL) {
error("evbuffer_new() failed: %s (errno=%d)", strerror(errno), errno);
goto FAIL;
}
authenticator = malloc(sizeof(struct authenticator));
if (authenticator == NULL) {
error("malloc() for authenticator failed: %s (errno=%d)", strerror(errno), errno);
goto FAIL;
}
authenticator->auth_method = auth_method;
authenticator->ops = ops;
authenticator->underlying_bev = underlying_bev;
authenticator->input = input;
authenticator->output = output;
authenticator->inner = inner;
return authenticator;
FAIL:
if (authenticator)
free(authenticator);
if (input)
evbuffer_free(input);
if (output)
evbuffer_free(output);
if (inner)
ops.inner_free(inner);
return NULL;
}
void authenticator_free(struct authenticator *authenticator)
{
evbuffer_free(authenticator->input);
evbuffer_free(authenticator->output);
authenticator->ops.inner_free(authenticator->inner);
free(authenticator);
}
int authenticator_do_authenticate(struct authenticator *authenticator)
{
return authenticator->ops.authenticate(authenticator);
}
struct evbuffer *authenticator_get_input_buffer(struct authenticator *authenticator)
{
int n = authenticator->ops.poll_input(authenticator);
if (n == -1) {
error("authenticator->ops.poll_input failed: %s (errno=%d)", strerror(errno), errno);
return NULL;
}
return authenticator->input;
}
struct evbuffer *authenticator_get_output_buffer(struct authenticator *authenticator)
{
return authenticator->output;
}
int authenticator_flush(struct authenticator *authenticator)
{
return authenticator->ops.flush_output(authenticator);
}