Skip to content

Commit 3a99dd6

Browse files
author
wangyuntao
committed
resolve domain name asynchronously
1 parent 890fc7f commit 3a99dd6

28 files changed

+994
-471
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@
1818
/s5-1.0.tar.gz
1919

2020
/.#*
21-
/*.o
21+
/*.o
22+
23+
/core

Makefile.am

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
AM_CFLAGS = -pedantic -Wall -Werror # -std=c11 # TODO
33

44
SHARED_SRCS = \
5+
s5_type.h \
56
s5_server.c \
67
s5_server.h \
78
s5_conn.c \
@@ -12,6 +13,10 @@ SHARED_SRCS = \
1213
s5_conn_relay.h \
1314
s5_conn_shutdown.c \
1415
s5_conn_shutdown.h \
16+
s5_dns_resolver.c \
17+
s5_dns_resolver.h \
18+
s5_signal.c \
19+
s5_signal.h \
1520
s5_util.h \
1621
s5_util_epoll.c \
1722
s5_util_epoll.h \

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ AM_INIT_AUTOMAKE([-Wall -Werror foreign])
88

99
AC_PROG_CC
1010

11-
dnl AC_CHECK_LIB([pthread], [pthread_mutex_init])
11+
AC_CHECK_LIB([pthread], [pthread_mutex_init])
1212

1313
AC_CONFIG_FILES([Makefile])
1414

s5.c

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,47 @@
11
#include <stdlib.h>
2+
#include <unistd.h>
3+
#include "s5_dns_resolver.h"
24
#include "s5_server.h"
5+
#include "s5_signal.h"
36
#include "s5_util.h"
47

8+
static int parse_args(int argc, char** argv, int* port) {
9+
if (argc != 2) {
10+
return -1;
11+
}
12+
*port = atoi(argv[1]);
13+
return 0;
14+
}
15+
516
// https://tools.ietf.org/rfc/rfc1928
617
int main(int argc, char** argv) {
718
int err, port;
819

9-
if (argc != 2) {
10-
printf("Usage: ./s5 <port>\n");
20+
err = parse_args(argc, argv, &port);
21+
if (err) {
22+
printf("Usage: %s <port>\n", *argv);
1123
return -1;
1224
}
13-
port = atoi(argv[1]);
1425

15-
err = signal_ignore(SIGPIPE);
16-
if (err) {
17-
perror_and_exit("signal_ignore");
18-
}
26+
s5_signal_init();
1927

2028
s5_epoll_t* ep = s5_epoll_create();
2129
assert(ep);
2230

23-
s5_server_t* s = s5_server_create(port);
31+
s5_server_t* s = s5_server_create(port, ep);
2432
assert(s);
2533

26-
err = s5_server_listen(s);
27-
if (err) {
28-
perror_and_exit("s5_server_listen");
29-
}
34+
err = s5_dns_resolver_start(s, ep);
35+
assert(!err);
3036

31-
err = s5_epoll_add(ep, s->fd, s);
37+
err = s5_server_start(s, ep);
3238
if (err) {
33-
perror_and_exit("s5_epoll_add");
39+
perror_and_exit("s5_server_start");
3440
}
3541

3642
s5_epoll_run(ep);
37-
s5_epoll_destroy(ep);
3843

44+
// TODO destory other?
45+
s5_epoll_destroy(ep);
3946
return 0;
4047
}

s5_conn.c

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,98 @@
11
#include "s5_conn.h"
22
#include "s5_conn_negotiate.h"
33
#include "s5_conn_relay.h"
4+
#include "s5_conn_shutdown.h"
45

56
#define BUF_SIZE 4096 // 必须要足够大到放下每条negotiation消息
67
#define BUF_SIZE_MIN (256 * 2 + 1) // Username/Password Authentication Req
78

8-
static void s5_conn_close(void *p) {
9-
s5_conn_t *c = p;
10-
s5_epoll_item_null_hdlr(c);
11-
s5_epoll_item_null_hdlr(c->peer);
12-
}
9+
static s5_epoll_item_hdlr hdlrs[] = {
10+
// SELECT_METHOD_REQ
11+
h_select_method_req,
12+
NULL,
1313

14-
static void s5_conn_destroy0(s5_conn_t *c) {
15-
if (c->fd >= 0) {
16-
s5_close(c->fd);
17-
c->fd = -1;
18-
}
14+
// SELECT_METHOD_REPLY
15+
NULL,
16+
h_select_method_reply,
1917

20-
if (c->buf) {
21-
s5_buf_destroy(c->buf);
22-
c->buf = NULL;
23-
}
18+
// CONNECT_REQ
19+
h_connect_req,
20+
NULL,
2421

25-
c->peer = NULL;
26-
s5_epoll_item_free(c);
27-
}
22+
// RESOLVE_DOMAIN_NAME
23+
NULL,
24+
NULL,
2825

29-
static void s5_conn_destroy(void *p) {
30-
s5_conn_t *c1 = p;
31-
s5_conn_t *c2 = c1->peer;
26+
// CONNECT_TARGET
27+
NULL,
28+
h_target_connected,
3229

33-
assert(c2->peer == c1);
34-
S5_DEBUG("s5_conn_destroy: %p, %d -> %d\n", p, c1->fd, c2->fd);
30+
// CONNECT_REPLY
31+
NULL,
32+
NULL,
3533

36-
s5_conn_destroy0(c1);
37-
s5_conn_destroy0(c2);
38-
}
34+
// RELAY
35+
h_relay_in,
36+
h_relay_out,
37+
38+
// SHUTDOWN
39+
h_shutdown,
40+
h_shutdown,
41+
};
3942

40-
static s5_conn_t *s5_conn_create0(int fd) {
41-
s5_conn_t *c = s5_epoll_item_alloc(sizeof(s5_conn_t));
43+
static s5_conn_t *s5_conn_create0(int fd, s5_epoll_t *ep, s5_server_t *s) {
44+
s5_conn_t *c = s5_epoll_item_create(sizeof(s5_conn_t), hdlrs, -1, ep);
4245
if (!c) {
4346
return NULL;
4447
}
4548

46-
s5_epoll_item_init(c, h_select_method_req, NULL, s5_conn_close,
47-
s5_conn_destroy);
4849
c->fd = fd;
4950

5051
assert(BUF_SIZE >= BUF_SIZE_MIN);
5152
c->buf = s5_buf_create(BUF_SIZE);
5253
if (!c->buf) {
53-
s5_epoll_item_free(c);
54+
s5_epoll_item_destroy(c, ep);
5455
return NULL;
5556
}
5657

5758
c->peer = NULL;
59+
c->s = s;
60+
c->dn2ip_req_l = 0;
5861
return c;
5962
}
6063

61-
s5_conn_t *s5_conn_create(int fd) {
62-
s5_conn_t *c1 = s5_conn_create0(fd);
64+
static void s5_conn_destroy0(s5_conn_t *c, s5_epoll_t *ep) {
65+
if (c->fd >= 0) {
66+
s5_close(c->fd);
67+
c->fd = -1;
68+
}
69+
if (c->buf) {
70+
s5_buf_destroy(c->buf);
71+
c->buf = NULL;
72+
}
73+
c->peer = NULL;
74+
c->s = NULL;
75+
s5_epoll_item_destroy(c, ep);
76+
}
77+
78+
s5_conn_t *s5_conn_create(int fd, s5_epoll_t *ep, s5_server_t *s) {
79+
s5_conn_t *c1 = s5_conn_create0(fd, ep, s);
6380
if (!c1) {
6481
return NULL;
6582
}
6683

67-
s5_conn_t *c2 = s5_conn_create0(-1);
84+
s5_conn_t *c2 = s5_conn_create0(-1, ep, s);
6885
if (!c2) {
69-
s5_conn_destroy0(c1);
86+
s5_epoll_item_destroy(c1, NULL);
7087
return NULL;
7188
}
7289

7390
c1->peer = c2;
7491
c2->peer = c1;
7592
return c1;
7693
}
94+
95+
void s5_conn_destroy(s5_conn_t *c, s5_epoll_t *ep) {
96+
s5_conn_destroy0(c->peer, ep);
97+
s5_conn_destroy0(c, ep);
98+
}

s5_conn.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
#ifndef _S5_CONN_H
22
#define _S5_CONN_H
33

4+
#include "s5_type.h"
45
#include "s5_util.h"
56

6-
typedef struct s5_conn {
7+
struct s5_conn {
78
int fd;
89
s5_buf_t *buf;
9-
struct s5_conn *peer;
10-
} s5_conn_t;
10+
s5_conn_t *peer;
11+
s5_server_t *s;
12+
uintptr_t dn2ip_req_l;
13+
};
1114

12-
s5_conn_t *s5_conn_create(int fd);
15+
enum s5_conn_state {
16+
SELECT_METHOD_REQ,
17+
SELECT_METHOD_REPLY,
18+
CONNECT_REQ,
19+
RESOLVE_DOMAIN_NAME,
20+
CONNECT_TARGET,
21+
CONNECT_REPLY,
22+
RELAY,
23+
SHUTDOWN,
24+
};
25+
26+
s5_conn_t *s5_conn_create(int fdc, s5_epoll_t *ep, s5_server_t *s);
27+
void s5_conn_destroy(s5_conn_t *c, s5_epoll_t *ep);
1328

1429
#endif

0 commit comments

Comments
 (0)