-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdriver.c
215 lines (165 loc) · 3.95 KB
/
driver.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
#include "re.h"
#include "tcsip/tcsip.h"
#include "tcsip/tcsipuser.h"
#include "tcsip/tcsipreg.h"
#include "tcsip/tcsipcall.h"
#include "ipc/tcipc.h"
#include "x509/x509util.h"
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <openssl/engine.h>
#include <srtp/srtp.h>
#include <msgpack.h>
#include "driver.h"
#if __APPLE__
#include "sound/apple/sound.h"
#endif
struct cli_app {
struct tcsip *sip;
struct sip_handlers *handlers;
int control_fd;
int client_fd;
struct msgpack_unpacker *up;
};
static void signal_handler(int sig)
{
re_cancel();
}
int svc_make(const char *pathname)
{
struct sockaddr_un sun;
int fd, err;
unlink(pathname);
/* create server socket */
fd = socket(PF_UNIX, SOCK_STREAM, 0);
/* bind it */
memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_UNIX;
strcpy(sun.sun_path, pathname);
err = bind(fd, (struct sockaddr *) &sun, sizeof(sun));
if(err) {
close(fd);
return -1;
}
listen(fd, 10);
return fd;
}
int write_fd(void* data, const char* buf, unsigned int len)
{
struct cli_app *app = data;
if(app->client_fd < 0) return len;
return (int)write(app->client_fd, buf, len);
}
static void read_cb(int flags, void *arg)
{
size_t rsize;
struct cli_app *app = arg;
msgpack_object ob;
struct msgpack_unpacker *up = app->up;
if(!(flags & FD_READ))
return;
msgpack_unpacker_reserve_buffer(up, 256);
rsize = read(app->client_fd,
msgpack_unpacker_buffer(up),
msgpack_unpacker_buffer_capacity(up)
);
if(rsize == 0) {
shutdown(app->client_fd, 1);
close(app->client_fd);
fd_close(app->client_fd);
app->client_fd = -1;
}
if(rsize <= 0)
goto none;
if(rsize > 0)
msgpack_unpacker_buffer_consumed(up, rsize);
restart:
if(!msgpack_unpacker_execute(up))
goto none;
ob = msgpack_unpacker_data(up);
msgpack_unpacker_reset(up);
tcsip_ob_cmd(app->sip, ob);
goto restart;
none:
return;
}
static void accept_cb(int flags, void *arg)
{
int fd;
struct sockaddr addr;
socklen_t addrlen;
addrlen = sizeof(addr);
struct cli_app *app = arg;
fd = accept(app->control_fd, &addr, &addrlen);
if(app->client_fd >= 0) {
shutdown(fd, 1);
close(fd);
return;
}
app->client_fd = fd;
fd_listen(fd, FD_READ, read_cb, app);
}
int libresip_driver(char *sock_path) {
int err, sock;
struct tcsip *sip;
struct cli_app *app;
struct msgpack_zone *mempool;
struct msgpack_unpacker *up;
struct msgpack_packer *packer;
libre_init();
#if __APPLE__
err = apple_sound_init();
#endif
err = srtp_init();
mempool = msgpack_zone_new(2048);
up = msgpack_unpacker_new(128);
sock = svc_make(sock_path);
if(sock < 0) {
fprintf(stderr, "failed to bind\n");
return sock;
}
app = mem_zalloc(sizeof(struct cli_app), NULL);
if(!app) {
err = -ENOMEM;
goto fail;
}
app->control_fd = sock;
app->client_fd = -1;
app->up = up;
packer = msgpack_packer_new(app, write_fd);
tcsip_alloc(&app->sip, 1, packer);
if(!app->sip) {
fprintf(stderr, "failed to create driver instance\n");
return 1;
}
sip = app->sip;
fd_listen(sock, FD_READ, accept_cb, app);
re_main(signal_handler);
shutdown(sock, 1);
close(sock);
fd_close(sock);
if(app->client_fd >= 0) {
shutdown(app->client_fd, 1);
close(app->client_fd);
fd_close(app->client_fd);
}
mem_deref(sip);
mem_deref(app);
msgpack_packer_free(packer);
msgpack_unpacker_free(up);
msgpack_zone_free(mempool);
#if __APPLE__
apple_sound_deinit();
#endif
tmr_debug();
mem_debug();
ENGINE_cleanup();
fail:
libre_close();
return 0;
}