diff options
author | mannol <eniz_vukovic@hotmail.com> | 2014-01-25 01:32:33 +0100 |
---|---|---|
committer | mannol <eniz_vukovic@hotmail.com> | 2014-01-25 01:32:33 +0100 |
commit | 65d320e31daa4709bb48b7f2a52c269dde0927e9 (patch) | |
tree | 45081a96be413d850a837d6afcee19fcfbfe7aca /toxmsi/toxmsi_message.c | |
parent | 51d8c41390be853a13693476802a834daf8d156a (diff) |
Done with encryption and core adaptations.
Diffstat (limited to 'toxmsi/toxmsi_message.c')
-rw-r--r-- | toxmsi/toxmsi_message.c | 267 |
1 files changed, 0 insertions, 267 deletions
diff --git a/toxmsi/toxmsi_message.c b/toxmsi/toxmsi_message.c deleted file mode 100644 index 72d7ee01..00000000 --- a/toxmsi/toxmsi_message.c +++ /dev/null | |||
@@ -1,267 +0,0 @@ | |||
1 | #ifdef HAVE_CONFIG_H | ||
2 | #include "config.h" | ||
3 | #endif /* HAVE_CONFIG_H */ | ||
4 | |||
5 | #include "toxmsi_message.h" | ||
6 | #include <stdlib.h> | ||
7 | #include <string.h> | ||
8 | #include "../toxrtp/toxrtp_helper.h" | ||
9 | #include <assert.h> | ||
10 | #include <stdlib.h> | ||
11 | |||
12 | #define ALLOCATE_HEADER(_header_type, _var, _m_header_value) \ | ||
13 | _var = calloc( sizeof(_header_type), 1 ); \ | ||
14 | assert(_var); \ | ||
15 | _var->_header_value = t_strallcpy((const uint8_t*)_m_header_value); | ||
16 | |||
17 | #define DEALLOCATE_HEADER(_header) \ | ||
18 | if ( _header && _header->_header_value ) { \ | ||
19 | free( _header->_header_value ); \ | ||
20 | free( _header ); } | ||
21 | |||
22 | #define SET_HEADER(_header_type, _var, _m_header_value) \ | ||
23 | if ( _var ){ \ | ||
24 | free(_var->_header_value); \ | ||
25 | free(_var);} \ | ||
26 | ALLOCATE_HEADER( _header_type, _var, _m_header_value ) | ||
27 | |||
28 | |||
29 | /* Sets empty message | ||
30 | */ | ||
31 | void set_msg ( msi_msg_t* _msg ) | ||
32 | { | ||
33 | _msg->_call_type = NULL; | ||
34 | _msg->_version = NULL; | ||
35 | _msg->_request = NULL; | ||
36 | _msg->_response = NULL; | ||
37 | _msg->_friend_id = NULL; | ||
38 | _msg->_user_agent = NULL; | ||
39 | _msg->_call_id = NULL; | ||
40 | _msg->_reason = NULL; | ||
41 | _msg->_info = NULL; | ||
42 | _msg->_next = NULL; | ||
43 | _msg->_headers = NULL; | ||
44 | } | ||
45 | |||
46 | void msi_free_msg ( msi_msg_t* _msg ) | ||
47 | { | ||
48 | assert(_msg); | ||
49 | |||
50 | DEALLOCATE_HEADER(_msg->_call_type); | ||
51 | DEALLOCATE_HEADER(_msg->_friend_id); | ||
52 | DEALLOCATE_HEADER(_msg->_request); | ||
53 | DEALLOCATE_HEADER(_msg->_response); | ||
54 | DEALLOCATE_HEADER(_msg->_user_agent); | ||
55 | DEALLOCATE_HEADER(_msg->_version); | ||
56 | DEALLOCATE_HEADER(_msg->_info); | ||
57 | DEALLOCATE_HEADER(_msg->_reason); | ||
58 | DEALLOCATE_HEADER(_msg->_call_id); | ||
59 | |||
60 | free(_msg); | ||
61 | } | ||
62 | |||
63 | void append_header_to_string ( uint8_t* _dest, const uint8_t* _header_field, const uint8_t* _header_value ) | ||
64 | { | ||
65 | assert(_dest); | ||
66 | assert(_header_value); | ||
67 | assert(_header_field); | ||
68 | |||
69 | size_t _dest_len = t_memlen(_dest); | ||
70 | |||
71 | uint8_t* _storage_iterator = _dest + _dest_len; | ||
72 | const uint8_t* _header_fit = _header_field; | ||
73 | const uint8_t* _header_val = _header_value; | ||
74 | const uint8_t* _term_it = (const uint8_t*) _RAW_TERMINATOR; | ||
75 | |||
76 | while ( *_header_fit ){ | ||
77 | *_storage_iterator = *_header_fit; | ||
78 | ++_header_fit; | ||
79 | ++_storage_iterator; | ||
80 | } | ||
81 | |||
82 | *_storage_iterator = ' '; /* place space */ | ||
83 | ++_storage_iterator; | ||
84 | |||
85 | while ( *_header_val ){ | ||
86 | *_storage_iterator = *_header_val; | ||
87 | ++_header_val; | ||
88 | ++_storage_iterator; | ||
89 | } | ||
90 | |||
91 | while ( *_term_it ){ | ||
92 | *_storage_iterator = *_term_it; | ||
93 | ++_term_it; | ||
94 | ++_storage_iterator; | ||
95 | } | ||
96 | } | ||
97 | |||
98 | msi_msg_t* msi_parse_msg ( const uint8_t* _data ) | ||
99 | { | ||
100 | assert(_data); | ||
101 | |||
102 | msi_msg_t* _retu = calloc ( sizeof ( msi_msg_t ), 1 ); | ||
103 | assert(_retu); | ||
104 | |||
105 | set_msg(_retu); | ||
106 | |||
107 | _retu->_headers = msi_parse_raw_data ( _data ); | ||
108 | |||
109 | if ( msi_parse_headers (_retu) == FAILURE ) { | ||
110 | msi_free_msg(_retu); | ||
111 | return NULL; | ||
112 | } | ||
113 | |||
114 | if ( !_retu->_version || strcmp((const char*)_retu->_version->_header_value, VERSION_STRING) != 0 ){ | ||
115 | msi_free_msg(_retu); | ||
116 | return NULL; | ||
117 | } | ||
118 | |||
119 | return _retu; | ||
120 | } | ||
121 | |||
122 | |||
123 | uint8_t* msi_msg_to_string ( msi_msg_t* _msg ) | ||
124 | { | ||
125 | assert(_msg); | ||
126 | |||
127 | uint8_t* _retu = calloc(sizeof(uint8_t), MSI_MAXMSG_SIZE ); | ||
128 | assert(_retu); | ||
129 | |||
130 | t_memset(_retu, '\0', MSI_MAXMSG_SIZE); | ||
131 | |||
132 | if ( _msg->_version ){ | ||
133 | append_header_to_string(_retu, (const uint8_t*)_VERSION_FIELD, _msg->_version->_header_value); | ||
134 | } | ||
135 | |||
136 | if ( _msg->_request ){ | ||
137 | append_header_to_string(_retu, (const uint8_t*)_REQUEST_FIELD, _msg->_request->_header_value); | ||
138 | } | ||
139 | |||
140 | if ( _msg->_response ){ | ||
141 | append_header_to_string(_retu, (const uint8_t*)_RESPONSE_FIELD, _msg->_response->_header_value); | ||
142 | } | ||
143 | |||
144 | if ( _msg->_friend_id ){ | ||
145 | append_header_to_string(_retu, (const uint8_t*)_FRIENDID_FIELD, _msg->_friend_id->_header_value); | ||
146 | } | ||
147 | |||
148 | if ( _msg->_call_type ){ | ||
149 | append_header_to_string(_retu, (const uint8_t*)_CALLTYPE_FIELD, _msg->_call_type->_header_value); | ||
150 | } | ||
151 | |||
152 | if ( _msg->_user_agent ){ | ||
153 | append_header_to_string(_retu, (const uint8_t*)_USERAGENT_FIELD, _msg->_user_agent->_header_value); | ||
154 | } | ||
155 | |||
156 | if ( _msg->_info ){ | ||
157 | append_header_to_string(_retu, (const uint8_t*)_INFO_FIELD, _msg->_info->_header_value); | ||
158 | } | ||
159 | |||
160 | if ( _msg->_call_id ){ | ||
161 | append_header_to_string(_retu, (const uint8_t*)_CALL_ID_FIELD, _msg->_call_id->_header_value); | ||
162 | } | ||
163 | |||
164 | if ( _msg->_reason ){ | ||
165 | append_header_to_string(_retu, (const uint8_t*)_REASON_FIELD, _msg->_reason->_header_value); | ||
166 | } | ||
167 | |||
168 | return _retu; | ||
169 | } | ||
170 | |||
171 | msi_msg_t* msi_msg_new ( uint8_t _type, const uint8_t* _typeid ) | ||
172 | { | ||
173 | msi_msg_t* _retu = calloc ( sizeof ( msi_msg_t ), 1 ); | ||
174 | assert(_retu); | ||
175 | |||
176 | set_msg(_retu); | ||
177 | |||
178 | if ( _type == TYPE_REQUEST ){ | ||
179 | ALLOCATE_HEADER( msi_header_request_t, _retu->_request, _typeid ) | ||
180 | _retu->_response = NULL; | ||
181 | |||
182 | } else if ( _type == TYPE_RESPONSE ) { | ||
183 | ALLOCATE_HEADER( msi_header_response_t, _retu->_response, _typeid ) | ||
184 | _retu->_request = NULL; | ||
185 | |||
186 | } else { | ||
187 | msi_free_msg(_retu); | ||
188 | return NULL; | ||
189 | } | ||
190 | |||
191 | |||
192 | ALLOCATE_HEADER( msi_header_version_t, _retu->_version, VERSION_STRING) | ||
193 | |||
194 | _retu->_friend_id = NULL; | ||
195 | _retu->_call_type = NULL; | ||
196 | _retu->_user_agent = NULL; | ||
197 | _retu->_info = NULL; | ||
198 | |||
199 | _retu->_next = NULL; | ||
200 | |||
201 | return _retu; | ||
202 | } | ||
203 | |||
204 | uint8_t* msi_genterate_call_id ( uint8_t* _storage, size_t _len ) | ||
205 | { | ||
206 | assert(_storage); | ||
207 | |||
208 | static const char _alphanum[] = | ||
209 | "0123456789" | ||
210 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
211 | "abcdefghijklmnopqrstuvwxyz"; /* avoids warning */ | ||
212 | |||
213 | uint8_t _val = 0; | ||
214 | size_t _it; | ||
215 | |||
216 | /* Generate random values 1-255 */ | ||
217 | for ( _it = 0; _it < _len; _it ++ ) { | ||
218 | while ( !_val ) _val = (uint8_t) _alphanum[ t_random(61) ]; | ||
219 | |||
220 | _storage[_it] = _val; | ||
221 | _val = 0; | ||
222 | } | ||
223 | |||
224 | return _storage; | ||
225 | } | ||
226 | |||
227 | /* HEADER SETTING | ||
228 | */ | ||
229 | |||
230 | void msi_msg_set_call_type ( msi_msg_t* _msg, const uint8_t* _header_field ) | ||
231 | { | ||
232 | assert(_msg); | ||
233 | assert(_header_field); | ||
234 | |||
235 | SET_HEADER(msi_header_call_type_t, _msg->_call_type, _header_field) | ||
236 | } | ||
237 | void msi_msg_set_user_agent ( msi_msg_t* _msg, const uint8_t* _header_field ) | ||
238 | { | ||
239 | assert(_msg); | ||
240 | assert(_header_field); | ||
241 | |||
242 | SET_HEADER(msi_header_user_agent_t, _msg->_user_agent, _header_field) | ||
243 | } | ||
244 | void msi_msg_set_friend_id ( msi_msg_t* _msg, const uint8_t* _header_field ) | ||
245 | { | ||
246 | assert(_msg); | ||
247 | assert(_header_field); | ||
248 | |||
249 | SET_HEADER(msi_header_friendid_t, _msg->_friend_id, _header_field) | ||
250 | } | ||
251 | void msi_msg_set_info ( msi_msg_t* _msg, const uint8_t* _header_field ) | ||
252 | { | ||
253 | } | ||
254 | void msi_msg_set_reason ( msi_msg_t* _msg, const uint8_t* _header_field ) | ||
255 | { | ||
256 | assert(_msg); | ||
257 | assert(_header_field); | ||
258 | |||
259 | SET_HEADER(msi_header_reason_t, _msg->_reason, _header_field) | ||
260 | } | ||
261 | void msi_msg_set_call_id ( msi_msg_t* _msg, const uint8_t* _header_field ) | ||
262 | { | ||
263 | assert(_msg); | ||
264 | assert(_header_field); | ||
265 | |||
266 | SET_HEADER(msi_header_call_id_t, _msg->_call_id, _header_field) | ||
267 | } | ||