-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
178 lines (137 loc) · 3.38 KB
/
client.c
File metadata and controls
178 lines (137 loc) · 3.38 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
#define _POSIX_C_SOURCE 200809L
#include <sys/socket.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ev.h>
#include <tls.h>
#include "client.h"
#include "common.h"
#include "task.h"
static void child_cb(EV_P_ ev_io *w, int revents)
{
struct client *c = PTR_FROM_FIELD(struct client, watcher, w);
if (c->task > TASK_READ) {
if (!client_flush(c)) {
client_close(EV_A_ c);
return;
}
if (c->buffer_used)
return;
}
tasks[c->task].update(EV_A_ c, revents);
}
static void child_timeout(EV_P_ ev_timer *w, int revents)
{
struct client *c = PTR_FROM_FIELD(struct client, timeout, w);
(void)revents;
client_close(EV_A_ c);
}
struct client *client_new(EV_P_ int fd, struct sockaddr *addr, socklen_t addrlen
#ifdef USE_TLS
, struct tls *tlsctx
#endif
)
{
struct client *c = malloc(sizeof(*c));
if (!c)
return NULL;
c->fd = fd;
memcpy(&c->addr, addr, (c->addrlen = addrlen));
fcntl(c->fd, F_SETFL, O_NONBLOCK);
fcntl(c->fd, F_SETFD, FD_CLOEXEC);
c->buffer_used = 0;
c->task = TASK_READ;
#ifdef USE_TLS
c->tlsstate = UNKNOWN;
c->tlsctx = tlsctx;
#endif
ev_timer_init(&c->timeout, child_timeout, 60.0, 0);
ev_timer_start(EV_A_ &c->timeout);
ev_io_init(&c->watcher, child_cb, c->fd, EV_READ);
ev_io_start(EV_A_ &c->watcher);
return c;
}
bool client_printf(struct client *c, const char *fmt, ...)
{
int n = 0;
va_list args;
if (c->broken_client) {
n += snprintf(c->buffer + c->buffer_used, sizeof(c->buffer) - c->buffer_used, "+INFO: ");
if (c->buffer_used + n >= sizeof(c->buffer))
return false;
}
va_start(args, fmt);
n += vsnprintf(c->buffer + c->buffer_used + n, sizeof(c->buffer) - c->buffer_used - n, fmt, args);
va_end(args);
if (n < 0 || n + c->buffer_used >= sizeof(c->buffer))
return false;
c->buffer_used += n;
return true;
}
void client_error(EV_P_ struct client *c, const char *fmt, ...)
{
va_list args;
int n;
if (tasks[c->task].finish)
tasks[c->task].finish(EV_A_ c);
c->task = TASK_ERROR;
client_printf(c, "3");
va_start(args, fmt);
n = vsnprintf(c->buffer + c->buffer_used, sizeof(c->buffer) - c->buffer_used, fmt, args);
va_end(args);
if (c->buffer_used + n >= sizeof(c->buffer))
return;
c->buffer_used += n;
c->buffer_used += snprintf(c->buffer + c->buffer_used, sizeof(c->buffer) - c->buffer_used, "\t.\t.\t.\r\n.\r\n");
}
bool client_eos(struct client *c)
{
const char eos[] = ".\r\n";
if (c->buffer_used + sizeof(eos) - 1 > sizeof(c->buffer))
return false;
memcpy(c->buffer + c->buffer_used, eos, sizeof(eos) - 1);
c->buffer_used += sizeof(eos) - 1;
return true;
}
int client_write(struct client *c, void *buffer, size_t n)
{
#ifdef USE_TLS
if (c->tlsstate == READY)
return tls_write(c->tlsctx, buffer, n);
#endif
return write(c->fd, buffer, n);
}
bool client_flush(struct client *c)
{
int w;
if (!c->buffer_used)
return true;
w = client_write(c, c->buffer, c->buffer_used);
if (w <= 0)
return false;
if ((size_t)w < c->buffer_used) {
memmove(c->buffer, c->buffer + w, c->buffer_used - w);
c->buffer_used -= w;
} else {
c->buffer_used = 0;
}
return true;
}
void client_close(EV_P_ struct client *c)
{
tasks[c->task].finish(EV_A_ c);
#ifdef USE_TLS
if (c->tlsstate > PLAIN) {
tls_close(c->tlsctx);
tls_free(c->tlsctx);
}
#endif
ev_timer_stop(EV_A_ &c->timeout);
ev_io_stop(EV_A_ &c->watcher);
close(c->fd);
free(c);
}