-
Notifications
You must be signed in to change notification settings - Fork 2
/
messages.py
executable file
·453 lines (370 loc) · 14 KB
/
messages.py
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
**************************************
messages.py (part of freespeech.py)
**************************************
'''
__author__ = 'Tzury Bar Yochay'
__version__ = '0.1'
__license__ = 'GPLv3'
__all__ = [
'Parser',
'Framer',
'BaseMessage',
'HangupRequest',
'HangupRequestAck',
'ClientInvite',
'ClientAnswer',
'ClientInviteAck',
'ClientRTP',
'CommMessage',
'KeepAlive',
'KeepAliveAck',
'LoginReply',
'LoginRequest',
'Logout',
'SignalingMessage',
'ServerForwardInvite',
'ServerForwardRing',
'ServerOverloaded',
'ServerRejectInvite',
'ShortResponse',
'MessageTypes',
'string_to_ctx',
]
import struct
from ctypes import create_string_buffer, c_int
from utils import Storage
from logger import log
from messagefields import *
def string_to_ctx(*args):
v = ''.join((str(arg) for arg in args))
# ensure we use 32 bit integer on 64 bit CPU
h = c_int(hash(v)).value
log.debug('hashing %s as %d' % (repr(v), h))
return h
class Parser(object):
def __init__(self):
pass
def parse_type(self, msg):
'''parses the type (bytes of typecode)'''
t = msg[Framer.TYPE_POS[0]:Framer.TYPE_POS[1]]
return t in MessageTypes and t
def bof(self, msg):
return Framer.BOF == msg[:Framer.BOF_LEN]
def eof(self, msg):
return Framer.EOF == msg[-Framer.EOF_LEN:]
def length(self, msg):
try:
return struct.unpack(
'!h', msg[Framer.LEN_POS[0]:Framer.LEN_POS[1]])[0]
except:
return -1
def valid(self, msg):
return (self.bof(msg)
and self.eof(msg)
and self.length(msg) == len(self._body(msg))
and self.parse_type(msg))
def _body(self, msg):
buf = create_string_buffer(self.length(msg))
buf.raw = msg[Framer.LEN_POS[1] : -Framer.EOF_LEN]
return buf
def body(self, msg):
'''returns a tuple (msg_type, msg_buffer)'''
if self.valid(msg):
return (self.parse_type(msg), self._body(msg))
else:
return None
class Framer(object):
BOF = '\xab\xcd'
EOF = '\xdc\xba'
TYPE_POS = (2,4)
LEN_POS = (4, 6)
BOF_LEN = len(BOF)
EOF_LEN = len(EOF)
@staticmethod
def frame(type_code, buf):
length = struct.pack('!h', len(buf))
return ''.join([Framer.BOF, type_code, length, buf, Framer.EOF])
class CommMessage(object):
'''Wrapps message with additional data.
Encapsulates the address, the type of the message(Class)
and the context fields for the message (client, call)'''
addr = msg_type = body = msg_type = client_ctx = None
def __init__(self, addr, msg_type, body):
self.addr = addr
self.msg_type = msg_type
self.body = body
self.msg = msg_type(buf=body)
self.client_ctx = None
# for login request create new context,
# for others extract from the message
if (getattr(self.msg, 'client_ctx', None)):
self.client_ctx = self.msg.client_ctx.value
elif isinstance(self.msg, (LoginRequest,)):
client_ctx = string_to_ctx(self.msg.username.value)
log.info('a new client_ctx ',
'username: %s ctx: %s' % (
self.msg.username.value ,repr(client_ctx)))
self.client_ctx = client_ctx
self.call_ctx = getattr(self.msg, 'call_ctx', None) \
and self.msg.call_ctx.value
def __repr__(self):
return 'from %s <%s>, type %s, msg %s' % (
self.addr, self.client_ctx, self.msg_type, repr(self.msg))
class BaseMessage(object):
seq = [] # the sequence of fields stored in the buffer
buf = None
def __init__(self, *args, **kwargs):
if 'buf' in kwargs:
self.deserialize(kwargs['buf'])
elif 'length' in kwargs:
self.buf = create_string_buffer(kwargs['length'])
self.type_code = MessageTypes.keyof(self)
def _init_buffer(self, newbuffer=None):
try:
if not self.buf and not newbuffer:
length = sum((self.__dict__[field[0]].length
for field in self.seq))
self.buf = self._create_buffer(length)
elif newbuffer:
# self.buf never initialized
if not self.buf:
self.buf = self._create_buffer(len(newbuffer))
# assign or copy the string-value into self.buf
if hasattr(newbuffer, 'raw'):
self.buf = newbuffer
elif isinstance(newbuffer,str):
self.buf.raw = newbuffer
except:
log.exception('exception')
def _create_buffer(self, length=0):
try:
'''alocates a writeable buffer'''
return create_string_buffer(length)
except:
log.exception('exception')
def deserialize(self, buf=None):
try:
if buf:
self._init_buffer(buf)
self._set_values(self.seq)
except:
log.exception('exception')
def set_values(self, **kwargs):
try:
items = (p for p in self.seq if p[0] in kwargs)
self._set_values(items, kwargs)
except:
log.exception('exception')
def dict_fields(self):
try:
'''all fields as dict {name: value, ...}'''
x = dict(
((self.__dict__[field[0]].name,
self.__dict__[field[0]].value) for field in self.seq))
return x
except:
log.exception('exception')
def _set_values(self, items, values_dict=None):
try:
start = 0 # first field position
for params in items:
key, ctr = params[0], params[1]
format = len(params) == 3 and params[2]
args = [start]
if format:
if hasattr(format, '__call__'):
format = format()
args.append(format)
# key -> field.name
args.append(key)
# set a property as field-name and its value as Field instance.
self.__dict__[key] = ctr(*args)
# set the value either to a supplied argument
# or extract from the buffer
if values_dict:
self.__dict__[key].value = values_dict[key]
else:
self.__dict__[key].unpack_from(self.buf)
#next field starting point
start = self.__dict__[key].end
except:
log.exception('exception')
def _pack_values(self):
try:
'''packs all the values into the buffer'''
self._init_buffer()
for v in self.seq:
self.__dict__[v[0]].pack_into(self.buf)
except:
log.exception('exception')
def serialize(self):
try:
'''packs all values into the buffer and returns the buffer'''
self._pack_values()
return self.buf.raw
except:
log.exception('exception')
def pack(self):
try:
'''packs the buffer and make it ready to ship'''
return Framer.frame(self.type_code, self.serialize())
except:
log.exception('exception')
class ShortResponse(BaseMessage):
def __init__(self, *args, **kwargs):
self.seq = [
('client_ctx', IntField),
('result', ShortField)]
BaseMessage.__init__(self, *args, **kwargs)
class LoginRequest(BaseMessage):
def __init__(self, *args, **kwargs):
self.seq = [
('username_length', ByteField),
('username', StringField,
lambda: '!%dc' % self.username_length.value ),
('password', StringField, '!20c'),
('local_ip', IPField),
('local_port', IntField)]
BaseMessage.__init__(self, *args, **kwargs)
class LoginReply(BaseMessage):
def __init__(self, *args, **kwargs):
self.seq = [
('client_ctx', IntField),
('client_public_ip', IPField),
('client_public_port', IntField),
('ctx_expire', IntField),
('num_of_codecs', ByteField),
('codec_list', StringField,
lambda: '!%dc' % self.num_of_codecs.value)]
BaseMessage.__init__(self, *args, **kwargs)
class AlternateServerMessage(BaseMessage):
pass
class Logout(BaseMessage):
def __init__(self, *args, **kwargs):
self.seq = [('client_ctx', IntField)]
BaseMessage.__init__(self, *args, **kwargs)
class KeepAlive(BaseMessage):
def __init__(self, *args, **kwargs):
self.seq = [
('client_ctx', IntField),
('client_public_ip', IPField),
('client_public_port', IntField)]
BaseMessage.__init__(self, *args, **kwargs)
class KeepAliveAck(BaseMessage):
def __init__(self, *args, **kwargs):
self.seq = [
('client_ctx', IntField),
('expire', IntField),
('refresh_contact_list', ByteField)]
BaseMessage.__init__(self, *args, **kwargs)
class SignalingMessage(BaseMessage):
def __init__(self, *args, **kwargs):
BaseMessage.__init__(self, *args, **kwargs)
class ClientInvite(SignalingMessage):
def __init__(self, *args, **kwargs):
self.seq = [
('client_ctx', IntField),
('calle_name_length', ByteField),
('calle_name', StringField,
lambda: '!%dc' % self.calle_name_length.value),
('num_of_codecs', ByteField),
('codec_list', StringField,
lambda: '!%dc' % self.num_of_codecs.value)]
SignalingMessage.__init__(self, *args, **kwargs)
class ServerRejectInvite(ShortResponse):
def __init__(self, *args, **kwargs):
ShortResponse.__init__(self, *args, **kwargs)
class ServerForwardInvite(SignalingMessage):
def __init__(self, *args, **kwargs):
self.seq = [
('client_ctx', IntField),
('call_ctx', IntField),
('call_type', ByteField),
('client_name_length', ByteField),
('client_name', StringField,
lambda: '!%dc' % self.client_name_length.value),
('client_public_ip', IPField),
('client_public_port', IntField),
('num_of_codecs', ByteField),
('codec_list', StringField,
lambda: '!%dc' % self.num_of_codecs.value)]
SignalingMessage.__init__(self, *args, **kwargs)
class ClientInviteAck(SignalingMessage):
def __init__(self, *args, **kwargs):
self.seq = [
('client_ctx', IntField),
('call_ctx', IntField),
('client_status', CharField),
('client_public_ip', IPField),
('client_public_port', IntField)]
SignalingMessage.__init__(self, *args, **kwargs)
class ServerForwardRing(SignalingMessage):
def __init__(self, *args, **kwargs):
self.seq = [
('client_ctx', IntField),
('call_ctx', IntField),
('client_status', CharField),
('call_type', ByteField),
('client_public_ip', IPField),
('client_public_port', IntField)]
SignalingMessage.__init__(self, *args, **kwargs)
class ClientAnswer(SignalingMessage):
def __init__(self, *args, **kwargs):
self.seq = [
('client_ctx', IntField),
('call_ctx', IntField),
('codec', CharField)]
SignalingMessage.__init__(self, *args, **kwargs)
class ClientRTP(BaseMessage):
def __init__(self, *args, **kwargs):
self.seq = [
('client_ctx', IntField),
('call_ctx', IntField),
('sequence', IntField),
('rtp_bytes_length', ShortField),
('rtp_bytes', StringField,
lambda: '!%dc' % self.rtp_bytes_length.value)]
BaseMessage.__init__(self, *args, **kwargs)
class HangupRequest(SignalingMessage):
def __init__(self, *args, **kwargs):
self.seq = [
('client_ctx', IntField),
('call_ctx', IntField)]
SignalingMessage.__init__(self, *args, **kwargs)
class HangupRequestAck(SignalingMessage):
def __init__(self, *args, **kwargs):
self.seq = [
('client_ctx', IntField),
('call_ctx', IntField)]
SignalingMessage.__init__(self, *args, **kwargs)
class ServerOverloaded(BaseMessage):
def __init__(self, *args, **kwargs):
self.seq = [('alternate_ip', IPField)]
BaseMessage.__init__(self, *args, **kwargs)
MessageTypes = Storage({
'\x00\x01': ShortResponse,
'\x00\x02': LoginRequest,
'\x00\x03': LoginReply,
'\x00\x04': AlternateServerMessage,
'\x00\x05': Logout,
'\x00\x06': KeepAlive,
'\x00\x07': KeepAliveAck,
'\x00\x10': ClientInvite,
'\x00\x11': ServerRejectInvite,
'\x00\x12': ServerForwardInvite,
'\x00\x13': ClientInviteAck,
'\x00\x14': ServerForwardRing,
'\x00\x15': ClientAnswer,
'\x00\x20': ClientRTP,
'\x00\x40': HangupRequest,
'\x00\x41': HangupRequestAck,
'\x00\xa0': ServerOverloaded
})
def keyof(_v):
for k,v in MessageTypes.iteritems():
if _v == v or isinstance(_v, v):
return k
MessageTypes.keyof = keyof