-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathtinync.c
208 lines (180 loc) · 5.51 KB
/
tinync.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <stddef.h>
/* coroutine status values */
enum {
CR_BLOCKED = 0,
CR_FINISHED = 1,
};
/* Helper macros to generate unique labels */
#define __cr_line3(name, line) _cr_##name##line
#define __cr_line2(name, line) __cr_line3(name, line)
#define __cr_line(name) __cr_line2(name, __LINE__)
struct cr {
void *label;
int status;
};
#define cr_context_name(name) __cr_context_##name
#define cr_context(name) struct cr cr_context_name(name)
#define cr_context_init() \
{ \
.label = NULL, .status = CR_BLOCKED \
}
#define cr_func_name(name) __cr_func_##name
#define cr_proto(name, ...) cr_func_name(name)(struct cr * ctx, ##__VA_ARGS__)
#define cr_run(name, ...) \
cr_func_name(name)(&cr_context_name(name), ##__VA_ARGS__)
#define cr_local static
#define cr_begin() \
do { \
if ((ctx)->status == CR_FINISHED) \
return; \
if ((ctx)->label) \
goto *(ctx)->label; \
} while (0)
#define cr_label(o, stat) \
do { \
(o)->status = (stat); \
__cr_line(label) : (o)->label = &&__cr_line(label); \
} while (0)
#define cr_end() cr_label(ctx, CR_FINISHED)
#define cr_status(name) cr_context_name(name).status
#define cr_wait(cond) \
do { \
cr_label(ctx, CR_BLOCKED); \
if (!(cond)) \
return; \
} while (0)
#define cr_exit(stat) \
do { \
cr_label(ctx, stat); \
return; \
} while (0)
#define cr_queue(T, size) \
struct { \
T buf[size]; \
size_t r, w; \
}
#define cr_queue_init() \
{ \
.r = 0, .w = 0 \
}
#define cr_queue_len(q) (sizeof((q)->buf) / sizeof((q)->buf[0]))
#define cr_queue_cap(q) ((q)->w - (q)->r)
#define cr_queue_empty(q) ((q)->w == (q)->r)
#define cr_queue_full(q) (cr_queue_cap(q) == cr_queue_len(q))
#define cr_queue_push(q, el) \
(!cr_queue_full(q) && ((q)->buf[(q)->w++ % cr_queue_len(q)] = (el), 1))
#define cr_queue_pop(q) \
(cr_queue_empty(q) ? NULL : &(q)->buf[(q)->r++ % cr_queue_len(q)])
/* Wrap system calls and other functions that return -1 and set errno */
#define cr_sys(call) \
cr_wait((errno = 0) || \
!(((call) == -1) && (errno == EAGAIN || errno == EWOULDBLOCK || \
errno == EINPROGRESS || errno == EINTR)))
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <unistd.h>
typedef cr_queue(uint8_t, 4096) byte_queue_t;
static void cr_proto(stdin_loop, byte_queue_t *out)
{
cr_local uint8_t b;
cr_local int r;
cr_begin();
for (;;) {
cr_sys(r = read(STDIN_FILENO, &b, 1));
if (r == 0) {
cr_wait(cr_queue_empty(out));
cr_exit(1);
}
cr_wait(!cr_queue_full(out));
cr_queue_push(out, b);
}
cr_end();
}
static void cr_proto(socket_write_loop, byte_queue_t *in, int fd)
{
cr_local uint8_t *b;
cr_begin();
for (;;) {
cr_wait(!cr_queue_empty(in));
b = cr_queue_pop(in);
cr_sys(send(fd, b, 1, 0));
}
cr_end();
}
static void cr_proto(socket_read_loop, int fd)
{
cr_local uint8_t b;
cr_local int r;
cr_begin();
for (;;) {
cr_sys(r = recv(fd, &b, 1, 0));
if (r == 0)
cr_exit(1);
cr_sys(write(STDOUT_FILENO, &b, 1));
}
cr_end();
}
static int nonblock(int fd)
{
int flags = fcntl(fd, F_GETFL, 0);
if (flags == -1)
return -1;
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}
int main(int argc, char *argv[])
{
if (argc != 3) {
fprintf(stderr, "USAGE: %s <ip> <port>\n", argv[0]);
return 1;
}
char *host = argv[1];
int port = atoi(argv[2]);
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
perror("socket()");
return 1;
}
if (nonblock(fd) < 0) {
perror("nonblock() socket");
return 1;
}
if (nonblock(STDIN_FILENO) < 0) {
perror("nonblock() stdin");
return 1;
}
if (nonblock(STDOUT_FILENO) < 0) {
perror("nonblock() stdout");
return 1;
}
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_addr = {.s_addr = inet_addr(host)},
.sin_port = htons(port),
};
connect(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
byte_queue_t queue = cr_queue_init();
cr_context(stdin_loop) = cr_context_init();
cr_context(socket_read_loop) = cr_context_init();
cr_context(socket_write_loop) = cr_context_init();
while (cr_status(stdin_loop) == CR_BLOCKED &&
cr_status(socket_read_loop) == CR_BLOCKED) {
if (cr_queue_empty(&queue)) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds);
FD_SET(fd, &fds);
select(fd + 1, &fds, NULL, NULL, NULL);
}
cr_run(socket_read_loop, fd);
cr_run(socket_write_loop, &queue, fd);
cr_run(stdin_loop, &queue);
}
close(fd);
return 0;
}