-
Notifications
You must be signed in to change notification settings - Fork 1
/
myts.c
274 lines (249 loc) · 6.95 KB
/
myts.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* Copyright (C) 2010 Luigi Rizzo, Universita' di Pisa
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* $Id: myts.c 7972 2010-12-05 07:31:05Z luigi $
Framework for event based programming.
Each application supplies a descriptor with init, parse, start
routine and a pointer to app-specific data.
Each application is then expected to record handlers that are called before
and after a select() to define which descriptors to poll, and when
a timeout is due.
*/
#include "myts.h"
#include <sys/wait.h>
/* ugly to include the C source, but this simplifies use with tcc -run */
#ifndef SPLIT
#include "dynstring.c"
#include "cp437.c"
/*#include "http.c"*/
#include "terminal.c"
#include "config.c"
#include "launchpad.c"
#include "screen.c"
#include "pixop.c"
#endif
int verbose;
struct my_args __me;
/* the list of applications to use */
extern struct app lpad, sip_app;
struct app *all_apps[] = {
&lpad,
// &sip_app,
NULL,
};
/* add a millisecond value to a timer */
void timeradd_ms(const struct timeval *src, int ms, struct timeval *dst)
{
dst->tv_usec = src->tv_usec + (ms*1000);
dst->tv_sec = src->tv_sec + dst->tv_usec / 1000000;
dst->tv_usec %= 1000000;
}
/* set dst to the min of the two -- if cur is unset, ignore */
void timersetmin(struct timeval *dst, const struct timeval *cur)
{
if (timerisset(cur) && timercmp(cur, dst, <))
*dst = *cur;
}
/* returns true if dst set and <= 'now' */
int timerdue(const struct timeval *dst, const struct timeval *now)
{
return (timerisset(dst) && timercmp(dst, now, <=));
}
/* generic socket open routine. */
int opensock(struct sockaddr_in sa, int udp, int client)
{
int fd;
int i;
DBG(2, "open %s %s socket %s %d\n", client ? "client" : "server",
udp ? "UDP" : "TCP", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port));
fd = socket(sa.sin_family, udp ? SOCK_DGRAM : SOCK_STREAM, 0);
if (fd < 0) {
perror(" cannot create socket");
return -1;
}
fcntl(fd, F_SETFD, 1 ); // close on exec
i = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i) ) < 0 ) {
perror(" cannot reuseaddr");
goto error;
}
if (client) {
if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in))) {
perror("connect");
goto error;
}
} else {
if (bind(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in))) {
perror("bind");
goto error;
}
if (!udp) {
if (listen(fd, 20) < 0 ) {
perror("listen");
goto error;
}
}
}
return fd;
error:
if (fd >= 0)
close(fd);
return -1;
}
/*
* Generic session creation routine.
* size is the size of the descriptor, fd is the main file descriptor
* on which to work (ignored if -2, causes an error if -1),
* cb is the callback function and arg a session-specific argument.
* NB: the new session is stored in a temporary list, otherwise
* it might interfere with the scanning of the main list.
* Lists are merged at the beginning of each mainloop.
*/
void *new_sess(int size, int fd, cb_fn cb, void *arg)
{
struct sess *s;
if (fd != -2) { /* ignore fd in case -2 */
if (fd < 0)
return NULL;
fcntl(fd, F_SETFL, O_NONBLOCK);
}
s = calloc(1, size);
if (!s) {
close(fd);
DBG(0, "alloc failed\n");
return NULL;
}
s->cb = cb;
s->arg = arg;
s->fd = fd;
s->next = __me.tmp_sess;
__me.tmp_sess = s;
return s;
}
/*
* Main loop implementing connection handling
*/
int mainloop(struct my_args *me)
{
for (;;) {
int n;
struct sess *s, *nexts, **ps;
fd_set r, w;
struct cb_args a = {
.maxfd = 0,
.r = &r, .w = &w,
.run = 0 /* prepare select */
};
FD_ZERO(&r);
FD_ZERO(&w);
gettimeofday(&a.now, NULL);
a.due = a.now;
a.due.tv_sec += 1000;
/* prepare for select */
if (me->tmp_sess) {
for (n = 1, s = me->tmp_sess; s->next; s = s->next)
n++;
DBG(2, "merging %d new sessions\n", n);
s->next = me->sess;
me->sess = me->tmp_sess;
me->tmp_sess = NULL;
}
for (n = 0, s = me->sess; s; s = s->next) {
n++;
me->cur = s;
me->app = s->app;
if (s->cb(s, &a) && a.maxfd < s->fd)
a.maxfd = s->fd;
}
gettimeofday(&a.now, NULL);
a.due.tv_sec -= a.now.tv_sec;
a.due.tv_usec -= a.now.tv_usec;
if (a.due.tv_usec < 0) {
a.due.tv_usec += 1000000;
a.due.tv_sec--;
}
if (a.due.tv_sec > 100)
a.due.tv_sec = 100;
if (a.due.tv_sec < 0)
a.due.tv_sec = a.due.tv_usec = 0;
DBG(2, "%d sessions due in %d.%06d\n", n, (int)a.due.tv_sec, (int)a.due.tv_usec);
n = select(a.maxfd + 1, &r, &w, NULL, &a.due);
gettimeofday(&a.now, NULL);
if (n <= 0) {
FD_ZERO(&r);
FD_ZERO(&w);
DBG(2, "select returns %d\n", n);
/* still call handlers on timeouts and signals */
}
for (n = 0; wait3(NULL, WNOHANG, NULL) >0; n++) ;
if (n)
DBG(1, "%d children terminated\n", n);
a.run = 1; /* now execute the handlers */
for (ps = &me->sess; (s = *ps) ;) {
DBG(2, "handle session %p\n", s);
me->cur = s;
me->app = s->app;
nexts = s->next;
if (s->cb(s, &a)) /* socket dead, unlink */
*ps = nexts;
else
ps = &s->next;
}
}
return 0;
}
int main(int argc, char *argv[])
{
struct app **app, *a;
int i;
memset(&__me, 0, sizeof(__me));
__me.all_apps = all_apps;
/* main program arguments */
for (i = 1 ; i < argc; i++) {
char *optval, *opt = argv[i];
/* options without arguments */
if (!strcmp(opt, "-v") || !strcmp(opt, "--verbose")) {
verbose ++;
__me.verbose ++;
continue;
}
if (argc < 3)
break;
/* options with argument */
optval = argv[i+1];
break;
}
for (app = all_apps; (a = *app); app++) {
__me.app = a;
if ( a->init)
a->init();
if ( a->parse)
a->parse(&argc, argv);
if ( a->start)
a->start();
}
mainloop(&__me);
return 0;
}