forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bin_interface.h
170 lines (145 loc) · 4.58 KB
/
bin_interface.h
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
/*
* 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)
*/
#ifndef __BINARY_INTERFACE__
#define __BINARY_INTERFACE__
#include "ip_addr.h"
#include "crc.h"
#define BIN_PACKET_MARKER "P4CK"
#define BIN_PACKET_MARKER_SIZE 4
#define PKG_LEN_FIELD_SIZE 2
#define VERSION_FIELD_SIZE 2
#define LEN_FIELD_SIZE sizeof(int)
#define CMD_FIELD_SIZE sizeof(int)
#define HEADER_SIZE (BIN_PACKET_MARKER_SIZE + PKG_LEN_FIELD_SIZE + VERSION_FIELD_SIZE)
#define MIN_BIN_PACKET_SIZE \
(HEADER_SIZE + LEN_FIELD_SIZE + CMD_FIELD_SIZE + 2) /* e.g. >tm<.so */
#define bin_send_type \
(*(int *)(send_buffer + HEADER_SIZE + LEN_FIELD_SIZE + \
*(int *)(send_buffer + HEADER_SIZE)))
#define bin_rcv_type \
(*(int *)(rcv_buf + HEADER_SIZE + LEN_FIELD_SIZE + \
*(int *)(rcv_buf + HEADER_SIZE)))
#define bin_send_size ((int)(cpos - send_buffer))
#define bin_rcv_size ((int)(rcv_end - rcv_buf))
#define is_valid_bin_packet(_p) \
(memcmp(_p, BIN_PACKET_MARKER, BIN_PACKET_MARKER_SIZE) == 0)
#define get_name(_p, name) \
do { \
name.len = *(int *)(_p + HEADER_SIZE); \
name.s = _p + HEADER_SIZE + LEN_FIELD_SIZE; \
} while (0)
struct packet_cb_list {
str module; /* registered module */
void (*cbf)(int packet_type, /* module callback */
struct receive_info *ri, void *att);
void *att;
struct packet_cb_list *next;
};
/**
returns the version of the bin protocol from the received message
*/
short get_bin_pkg_version(void);
/**
calls all the registered functions
@buffer: buffer containing a complete bin message
@rcv: information about the sender of the message
*/
void call_callbacks(char* buffer, struct receive_info *rcv);
/*
* registers a callback function to be triggered on a received
* binary packet marked with the @mod_name module name
*/
int bin_register_cb(char *mod_name, void (*)(int packet_type, struct receive_info *ri, void * atr), void *att);
/**
* first function called when building a binary packet
*
* @mod_name: module specific string
* @cmd_type: module specific identifier for this new packet
*
* @return: 0 on success
*/
int bin_init(str *mod_name, int packet_type, short version);
/*
* adds a new string value to the packet being currently built
* @info: may also be NULL
*
* @return:
* > 0: success, the size of the buffer
* < 0: internal buffer limit reached
*/
int bin_push_str(const str *info);
/*
* adds a new integer value to the packet being currently built
*
* @return:
* > 0: success, the size of the buffer
* < 0: internal buffer limit reached
*/
int bin_push_int(int info);
/* TODO - comment, lol */
int bin_get_buffer(str *buffer);
/*
* pops a str structure from a received binary packet
* @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 is only valid for the duration of
* the callback. Don't forget to copy the data into a safe buffer!
*
* Note2: Information is retrieved in the same order it was stored
*/
int bin_pop_str(str *info);
/*
* pops an integer from a received binary packet
* @info: pointer to store the result
*
* @return:
* 0 (success): info retrieved
* 1 (success): nothing returned, all data has been consumed!
* < 0: error
*
* Note: Information is retrieved in the same order it was stored
*/
int bin_pop_int(void *info);
/*
* skips @count integers from a received binary packet
*
* @return:
* >= 0: success, number of skipped bytes
* < 0: error, buffer limit reached
*/
int bin_skip_int(int count);
/*
* skips @count strings from a received binary packet
*
* @return:
* >= 0: success, number of skipped bytes
* < 0: error, buffer limit reached
*/
int bin_skip_str(int count);
#endif /* __BINARY_INTERFACE__ */