forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin_interface.c
350 lines (278 loc) · 7.62 KB
/
bin_interface.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* Copyright (C) 2013 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* History:
* -------
* 2013-04-10: Created (Liviu)
*/
#include "bin_interface.h"
#include "config.h"
#include "daemonize.h"
#include "pt.h"
#include "net/net_udp.h"
struct socket_info *bin;
static char *send_buffer;
static char *cpos;
static char *rcv_buf;
static char *rcv_end;
static struct packet_cb_list *reg_modules;
/**
* bin_init - begins the construction of a new binary packet (header part):
*
* +-------------------+-----------------------------------------------------------------+
* | 12-byte HEADER | BODY max 65535 bytes |
* +-------------------+-----------------------------------------------------------------+
* | PK_MARKER |PGK LEN| Version | LEN | MOD_NAME | CMD | LEN | FIELD | LEN | FIELD |....|
* +-------------------+-----------------------------------------------------------------+
*
* @param: { LEN, MOD_NAME } + CMD
*/
short get_bin_pkg_version(void)
{
return *(short *)(rcv_buf + BIN_PACKET_MARKER_SIZE + PKG_LEN_FIELD_SIZE);
}
void set_len(char *send_buffer, char *cpos){
unsigned short len = cpos - send_buffer, *px;
px = (unsigned short *) (send_buffer + BIN_PACKET_MARKER_SIZE);
*px = len;
}
int bin_init(str *mod_name, int cmd_type, short version)
{
if (!send_buffer) {
send_buffer = pkg_malloc(BUF_SIZE);
if (!send_buffer) {
LM_ERR("No more pkg memory!\n");
return -1;
}
}
/* binary packet header: marker + pkg_len */
memcpy(send_buffer, BIN_PACKET_MARKER, BIN_PACKET_MARKER_SIZE);
cpos = send_buffer + BIN_PACKET_MARKER_SIZE + PKG_LEN_FIELD_SIZE;
/* bin version */
memcpy(cpos, &version, sizeof(version));
cpos += VERSION_FIELD_SIZE;
/* module name */
memcpy(cpos, &mod_name->len, LEN_FIELD_SIZE);
cpos += LEN_FIELD_SIZE;
memcpy(cpos, mod_name->s, mod_name->len);
cpos += mod_name->len;
memcpy(cpos, &cmd_type, sizeof(cmd_type));
cpos += sizeof(cmd_type);
set_len(send_buffer, cpos);
return 0;
}
/*
* copies the given string at the 'cpos' position in the buffer
* allows null strings (NULL content or NULL param)
*
* @return:
* > 0: success, number of added bytes
* < 0: internal buffer limit reached
*/
int bin_push_str(const str *info)
{
if (!cpos || (cpos - send_buffer + LEN_FIELD_SIZE + (info ? info->len : 0))
> BUF_SIZE)
return -1;
if (!info || info->len == 0 || !info->s) {
memset(cpos, 0, LEN_FIELD_SIZE);
cpos += LEN_FIELD_SIZE;
return (int)LEN_FIELD_SIZE;
}
memcpy(cpos, &info->len, LEN_FIELD_SIZE);
cpos += LEN_FIELD_SIZE;
memcpy(cpos, info->s, info->len);
cpos += info->len;
set_len(send_buffer, cpos);
return (int)LEN_FIELD_SIZE + info->len;
}
/*
* adds a new integer value at the 'cpos' position in the buffer
*
* @return:
* > 0: success, number of added bytes
* < 0: internal buffer limit reached
*/
int bin_push_int(int info)
{
if (!cpos || (cpos + sizeof(info) - send_buffer) > BUF_SIZE)
return -1;
memcpy(cpos, &info, sizeof(info));
cpos += sizeof(info);
set_len(send_buffer, cpos);
return sizeof(info);
}
int bin_get_buffer(str *buffer)
{
if (!buffer)
return -1;
buffer->s = send_buffer;
buffer->len = bin_send_size;
return 1;
}
/*
* skips @count integers from the current position in the received binary packet
*
* @return:
* >= 0: success, number of skipped bytes
* < 0: error, buffer limit reached
*/
int bin_skip_int(int count)
{
int i;
char *in = cpos;
for (i = 0; i < count; i++) {
if (cpos + LEN_FIELD_SIZE > rcv_end) {
LM_ERR("Receive binary packet buffer overflow");
return -1;
}
cpos += LEN_FIELD_SIZE;
}
return (int)(cpos - in);
}
/*
* skips @count strings from the current position in a received binary packet
*
* @return:
* >= 0: success, number of skipped bytes
* < 0: error, buffer limit reached
*/
int bin_skip_str(int count)
{
int i, len;
char *in = cpos;
for (i = 0; i < count; i++) {
if (cpos + LEN_FIELD_SIZE > rcv_end)
goto error;
memcpy(&len, cpos, LEN_FIELD_SIZE);
cpos += LEN_FIELD_SIZE;
if (cpos + len > rcv_end)
goto error;
cpos += len;
}
return (int)(cpos - in);
error:
LM_ERR("Receive binary packet buffer overflow");
return -1;
}
/*
* pops an str from the current position in the buffer
* @info: pointer to store the result
*
* @return:
* 0 (success): info retrieved
* 1 (success): nothing returned, all data has been consumed!
* < 0: error
*
* Note: The pointer returned in @info str is only valid for the duration of
* the callback. Don't forget to copy the info into a safe buffer!
*/
int bin_pop_str(str *info)
{
if (cpos == rcv_end)
return 1;
if (cpos + LEN_FIELD_SIZE > rcv_end)
goto error;
memcpy(&info->len, cpos, LEN_FIELD_SIZE);
cpos += LEN_FIELD_SIZE;
if (cpos + info->len > rcv_end)
goto error;
if (info->len == 0)
info->s = NULL;
else
info->s = cpos;
cpos += info->len;
LM_DBG("Popped: '%.*s' [%d]\n", info->len, info->s, info->len);
return 0;
error:
LM_ERR("Receive binary packet buffer overflow");
return -1;
}
/*
* pops an integer value from the current position in the buffer
* @info: pointer to store the result
*
* @return:
* 0 (success): info retrieved
* 1 (success): nothing returned, all data has been consumed!
* < 0: error
*/
int bin_pop_int(void *info)
{
if (cpos == rcv_end)
return 1;
if (cpos + sizeof(int) > rcv_end) {
LM_ERR("Receive binary packet buffer overflow");
return -1;
}
memcpy(info, cpos, sizeof(int));
cpos += sizeof(int);
return 0;
}
/**
* bin_register_cb - registers a module handler for specific packets
* @mod_name: used to classify the incoming packets
* @cb: the handler function, called once for each matched packet
*
* @return: 0 on success
*/
int bin_register_cb(char *mod_name, void (*cb)(int, struct receive_info *, void * atr), void *att)
{
struct packet_cb_list *new_mod;
new_mod = pkg_malloc(sizeof(*new_mod));
if (!new_mod) {
LM_ERR("No more pkg mem!\n");
return -1;
}
memset(new_mod, 0, sizeof(*new_mod));
new_mod->cbf = cb;
new_mod->module.len = strlen(mod_name);
new_mod->module.s = mod_name;
new_mod->att = att;
new_mod->next = reg_modules;
reg_modules = new_mod;
return 0;
}
/*
* main binary packet UDP receiver loop
*/
void call_callbacks(char* buffer, struct receive_info *rcv){
str name;
struct packet_cb_list *p;
rcv_buf = buffer;
get_name(rcv_buf, name);
rcv_end = rcv_buf + *(unsigned short*)(buffer + BIN_PACKET_MARKER_SIZE);
cpos = name.s + name.len + CMD_FIELD_SIZE;
/* packet will be now processed by a specific module */
for (p = reg_modules; p; p = p->next) {
if (p->module.len == name.len &&
memcmp(name.s, p->module.s, name.len) == 0) {
LM_DBG("binary Packet CMD: %d. Module: %.*s\n",
bin_rcv_type, name.len, name.s);
p->cbf(bin_rcv_type, rcv,p->att);
break;
}
}
}
/*
* called in the OpenSIPS initialization phase by the main process.
* forks the binary packet UDP receivers.
*
* @return: 0 on success
*/