-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
connection.c
154 lines (132 loc) · 4.48 KB
/
connection.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
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
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <ev.h>
#include "connection.h"
#include "connection_p.h"
#include "dfa.h"
#include "globals.h"
#include "log.h"
#include "utils.h"
static void kill_connection(struct connection_t* conn, struct ev_loop* loop)
{
ev_io_stop(loop, &conn->io);
ev_timer_stop(loop, &conn->tmr);
ev_timer_stop(loop, &conn->delay);
shutdown(conn->io.fd, SHUT_RDWR);
close(conn->io.fd);
free(conn->buffer);
free(conn->auth_failed);
my_log(LOG_DAEMON | LOG_WARNING, "Closing connection for %s:%u", conn->ip, (unsigned int)conn->port);
free(conn);
}
static void connection_timeout(struct ev_loop* loop, ev_timer* w, int revents)
{
struct connection_t* conn = (struct connection_t*)w->data;
my_log(LOG_AUTH | LOG_WARNING, "Connection timed out for %s:%u", conn->ip, (unsigned int)conn->port);
kill_connection(conn, loop);
}
static void connection_callback(struct ev_loop* loop, ev_io* w, int revents)
{
struct connection_t* conn = (struct connection_t*)w->data;
int ok = 0;
ev_io_stop(loop, w);
ev_timer_again(loop, &conn->tmr);
switch (conn->state) {
case NEW_CONN:
ok = handle_new_connection(conn, revents);
break;
case WRITING_GREETING:
ok = handle_write(conn, revents, READING_AUTH);
break;
case READING_AUTH:
ok = handle_auth(conn, revents);
break;
case WRITING_ASR:
ok = handle_write(conn, revents, READING_ASR);
break;
case READING_ASR:
ok = handle_auth_asr(conn, revents);
break;
case SLEEPING:
ok = 1;
ev_timer_stop(loop, &conn->tmr);
break;
case WRITING_OOO:
case WRITING_AF:
ok = handle_write(conn, revents, DONE);
break;
case DONE:
/* Should not happen */
assert(0);
break;
}
if (!ok || (revents & EV_ERROR) || conn->state == DONE) {
kill_connection(conn, loop);
}
else {
ev_io_set(w, w->fd, ok);
ev_io_start(loop, w);
}
}
void new_connection(struct ev_loop* loop, struct ev_io* w, int revents)
{
struct sockaddr sa;
socklen_t len = sizeof(sa);
if (revents & EV_READ) {
#ifdef _GNU_SOURCE
int sock = accept4(w->fd, &sa, &len, SOCK_NONBLOCK);
#else
int sock = accept(w->fd, &sa, &len);
#endif
if (sock != -1) {
struct connection_t* conn;
#ifndef _GNU_SOURCE
if (-1 == make_nonblocking(sock)) {
my_log(LOG_DAEMON | LOG_WARNING, "new_connection(): failed to make the accept()'ed socket non-blocking: %s", strerror(errno));
close(sock);
return;
}
#endif
conn = calloc(1, sizeof(struct connection_t));
conn->loop = loop;
conn->state = NEW_CONN;
ev_io_init(&conn->io, connection_callback, sock, EV_READ | EV_WRITE);
ev_timer_init(&conn->tmr, connection_timeout, 0, 10);
ev_timer_init(&conn->delay, do_auth_failed, globals.delay ? globals.delay : 0.01, 0);
conn->io.data = conn;
conn->tmr.data = conn;
conn->delay.data = conn;
get_ip_port(&sa, conn->ip, &conn->port);
if (0 != getnameinfo(&sa, len, conn->host, sizeof(conn->host), NULL, 0, 0)) {
assert(INET6_ADDRSTRLEN < NI_MAXHOST);
memcpy(conn->host, conn->ip, INET6_ADDRSTRLEN);
}
len = sizeof(sa);
if (-1 != getsockname(sock, &sa, &len)) {
get_ip_port(&sa, conn->my_ip, &conn->my_port);
}
else {
my_log(LOG_DAEMON | LOG_WARNING, "WARNING: getsockname() failed: %s", strerror(errno));
conn->my_port = (uint16_t)atoi(globals.bind_port);
memcpy(conn->my_ip, "0.0.0.0", sizeof("0.0.0.0"));
}
my_log(
LOG_DAEMON | LOG_INFO,
"New connection from %s:%u [%s] to %s:%u",
conn->ip, (unsigned int)conn->port, conn->host,
conn->my_ip, (unsigned int)conn->my_port
);
ev_io_start(loop, &conn->io);
}
else {
my_log(LOG_DAEMON | LOG_WARNING, "accept() failed: %s", strerror(errno));
}
}
}