summaryrefslogtreecommitdiff
path: root/cesar/maximus/python/lib/proto/fcall.py
blob: 0921e1025a86ad0d5ad0902af13a3a9904396f9a (plain)
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#! usr/bin/env python

#print __name__

import sys
base = '../../../../'
module = 'maximus/python/maximus/utils'
path = __file__[:__file__.find(".py") - len(__name__)] + base + module
sys.path.append(path)
module = 'maximus/python/maximus/ethernet'
path = __file__[:__file__.find(".py") - len(__name__)] + base + module
sys.path.append(path)

from exception import *
from format import *
from scapy import *

# Constants
FUNCTION_CALL_VERSION = 0x02
FUNCTION_CALL_FLAG_FAILED = 0x01
FUNCTION_CALL_TYPE_REQ = 0x01
FUNCTION_CALL_TYPE_RSP = 0x02
SIZE_OF_FUNCTION_CALL_HEADER = 8 # in octets
INTERFACE_MODULE_FCALL = 2
HPAV_MTYPE_MME = 0x88E1
HPAV_MMV = 1
INTERFACE_FCALL_MMTYPE = 0xABCD # TBD
SIZE_OF_DST = SIZE_OF_U48 # in octets
SIZE_OF_SRC = SIZE_OF_DST # in octets
SIZE_OF_TYPE = SIZE_OF_U16 # in octets
SIZE_OF_MMV = 1 # in octets
SIZE_OF_MMTYPE = 2 # in octets
SIZE_OF_FMI = 2 # in octets
INTERFACE_FCALL_PAYLOAD_OFFSET = 22 # in octets

# For unitary test purpose
FUNCTION_CALL_WRITE_FILE = "/tmp/proto_in"
FUNCTION_CALL_READ_FILE = "/tmp/proto_out"

class Param:

        def __init__(self, name, length, value):
            self.__name = name
            self.__length = htohp16(length)
            self.__value = value

        def get_name(self):
            return self.__name

        def get_value(self):
            return self.__value

        def get(self):
            return self.__name + '\0' + self.__length + self.__value

def recv_cb(fcall):
    print "=> recv_cb"
    fcall.rsp_recv = True

class Fcall:
        # Static attributes of the class
        __msg_id = 1
        cb_dict = {}
        
        def __init__(self, name, maximus):
            self.__name = name
            self.__maximus = maximus
            
            self.__param_list = []
            self.rsp_recv = False
            
            # Get a message ID
            if self.__class__.__msg_id < 0x7FFF:
                self.__class__.__msg_id += 1
            else:
                self.__class__.__msg_id = 0x0001
            self.__msg_id = self.__class__.__msg_id

        def add_param(self, name, value=None):
            if value is None:
                value = ''
            self.__param_list.append(Param(name, len(value), value))
            return self

        def add_param_bool(self, name, value):
            self.__param_list.append(Param(name, SIZE_OF_U8, htohp8(value)))
            return self

        def add_param_uchar(self, name, value):
            self.__param_list.append(Param(name, SIZE_OF_U8, htohp8(value)))
            return self

        def add_param_ushort(self, name, value):
            self.__param_list.append(Param(name, SIZE_OF_U16, htohp16(value)))
            return self

        def add_param_ulong(self, name, value):
            self.__param_list.append(Param(name, SIZE_OF_U32, htohp32(value)))
            return self

        def add_param_n_u8(self, name, tuple):
            value = ''
            for t in tuple:
                value += htohp8(t)
            self.__param_list.append(Param(name, SIZE_OF_U8 * len(value), value))
            return self

        def add_param_n_u16(self, name, tuple):
            value = ''
            for t in tuple:
                value += htohp16(t)
            self.__param_list.append(Param(name, SIZE_OF_U16 * len(value), value))
            return self

        def add_param_n_u32(self, name, tuple):
            value = ''
            for t in tuple:
                value += htohp32(t)
            self.__param_list.append(Param(name, SIZE_OF_U32 * len(value), value))
            return self

        def remove_param(self, name):
            for i in range (0, len(self.__param_list)):
                if self.__param_list[i].get_name() == name:
                    self.__param_list.pop(i)
                    break
            return self

        def set_cb(self, user_cb):
            self.__class__.cb_dict[self.__msg_id] = user_cb
            return self

        def remove_cb(self):
            del self.__class__.cb_dict[self.__msg_id]
            return self

        def set_sta(self, sta):
            self.__sta = sta
            return self

        def send_async(self, sta=None):
            if sta is not None:
                self.__sta = sta
            self.__write()

        def send(self, sta=None):
            if sta is not None:
                self.__sta = sta
            rsp = None
            self.__class__.cb_dict[self.__msg_id] = recv_cb
            self.__write()
            rsp = self.__maximus.process()
            while rsp is None or not rsp.rsp_recv:
                rsp = self.__maximus.process()
            rsp.rsp_recv = False
            return rsp

        def is_param(self, name):
            found = False
            for l in self.__param_list:
                if l.get_name() == name:
                    found = True
            return found

        def bind_param(self, name):
            param = None
            for l in self.__param_list:
                if l.get_name() == name:
                    param = l.get_value()
                    break
            return param

        def bind_param_string(self, name):
            param = None
            for l in self.__param_list:
                if l.get_name() == name:
                    param = l.get_value()
                    if param[len(param)-1] == '\0':
                        param = param[:-1]
                    break
            return param

        def bind_param_bool(self, name):
            param = None
            for l in self.__param_list:
                if l.get_name() == name:
                    param = hptoh8(l.get_value())
                    break
            return param

        def bind_param_ushort(self, name):
            param = None
            for l in self.__param_list:
                if l.get_name() == name:
                    param = hptoh16(l.get_value())
                    break
            return param

        def bind_param_ulong(self, name):
            param = None
            for l in self.__param_list:
                if l.get_name() == name:
                    param = hptoh32(l.get_value())
                    break
            return param

        def bind_param_n_u8(self, name):
            param = None
            for l in self.__param_list:
                if l.get_name() == name:
                    param = ()
                    for i in range (0, len(l.get_value()), SIZE_OF_U8):
                        param += hptoh8(l.get_value()[i:i+SIZE_OF_U8]),
                    break
            return param

        def bind_param_n_u16(self, name):
            param = None
            for l in self.__param_list:
                if l.get_name() == name:
                    param = ()
                    for i in range (0, len(l.get_value()), SIZE_OF_U16):
                        param += hptoh16(l.get_value()[i:i+SIZE_OF_U16]),
                    break
            return param

        def bind_param_n_u32(self, name):
            param = None
            for l in self.__param_list:
                if l.get_name() == name:
                    param = ()
                    for i in range (0, len(l.get_value()), SIZE_OF_U32):
                        param += hptoh32(l.get_value()[i:i+SIZE_OF_U32]),
                    break
            return param

        def __get(self):
            """Return the complete MM Entry into a string.
            """
            
            # FCALL data
            #
            fcall_data = self.__name + '\0'
            for l in self.__param_list:
                fcall_data += l.get()
            
            # FCALL header
            #
            # uint8_t version
            # uint8_t type
            # uint16_t msg_id
            # uint8_t param_nb
            # uint8_t flags
            # uint16_t reserved
            #
            fcall_hdr = htohp8(FUNCTION_CALL_VERSION)\
                        + htohp8(FUNCTION_CALL_TYPE_REQ)\
                        + htohp16(self.__msg_id)\
                        + htohp8(len(self.__param_list))\
                        + htohp8(0)\
                        + htohp16(0)
            
            # MM Entry
            #
            # Interface sub-module (1 octet)
            # Payload length (2 octets)
            #
            mmentry = htohp8(INTERFACE_MODULE_FCALL) + htohp16(len(fcall_hdr + fcall_data))
            
            return mmentry + fcall_hdr + fcall_data

        def __write(self):
            """Sends an MME.
            """
            mme = Ether()
            
            # Set the MM Header and the MM Entry
            #
            # ODA
            mme.dst = self.__sta.mac_address
            # OSA
            mme.src = self.__maximus.mac_address
            # MTYPE
            mme.type = HPAV_MTYPE_MME
            # MMV (1 octet)
            # MMTYPE (2 octets)
            # FMI (2 octets)
            mme.payload = htohp8(HPAV_MMV) + htohp16(INTERFACE_FCALL_MMTYPE) + htohp16(0) + self.__get()
            
            if not self.__maximus.UNIT_TEST:
                sendp(mme, iface=self.__maximus.network_interface)
            
            # In case of unitary test
            else:
                print "Sending an MME..."
                os.write(self.__maximus.write_file, str(mme))

def display(mme):
    try:
        hexdump(mme)
        mme.show()
    
    # In case of unitary test
    except:
        begin = 0
        end = 0
        print "MM Header:"
        print "ODA =", mme.dst
        print "OSA =", mme.src
        print "MTYPE =", hex(mme.type)
        end = SIZE_OF_MMV
        print "MMV =", hex(hptoh8(mme.payload.load[begin:end]))
        begin = end
        end += SIZE_OF_MMTYPE
        print "MMTYPE =", hex(hptoh16(mme.payload.load[begin:end]))
        begin = end
        end += SIZE_OF_FMI
        print "FMI =", hex(hptoh16(mme.payload.load[begin:end]))
        
        print "MM Entry:"
        begin = end
        end += SIZE_OF_U8
        print "Module =", hex(hptoh8(mme.payload.load[begin:end]))
        begin = end
        end += SIZE_OF_U16
        print "Length =", hptoh16(mme.payload.load[begin:end]), "(" + hex(hptoh16(mme.payload.load[begin:end])) + ")"
        
        print "Fcall Header:"
        begin = end
        end += SIZE_OF_U8
        print "version =", hex(hptoh8(mme.payload.load[begin:end]))
        begin = end
        end += SIZE_OF_U8
        print "type =", hex(hptoh8(mme.payload.load[begin:end]))
        begin = end
        end += SIZE_OF_U16
        print "msg id =", hex(hptoh16(mme.payload.load[begin:end]))
        begin = end
        end += SIZE_OF_U8
        print "param nb =", hex(hptoh8(mme.payload.load[begin:end]))
        begin = end
        end += SIZE_OF_U8
        print "flags =", hex(hptoh8(mme.payload.load[begin:end]))
        begin = end
        end += SIZE_OF_U16
        print "reserved =", hex(hptoh16(mme.payload.load[begin:end]))
        
        print "Fcall Payload:"
        print "payload =",
        for i in range(16, len(mme.payload.load)):
            print hex(hptoh8(mme.payload.load[i])),
        print

def read_name(data):
    name = ''
    index = 0
    while data[index] != '\0':
        index += 1
    return data[:index]

def read_param_name(data):
    return read_name(data)

def read_param_length(data):
    #return hptoh16(data[:SIZE_OF_U16])
    return ntoh16(data[:SIZE_OF_U16])

def read_param_value(data, length):
    return data[:length]

def read(mme, maximus):
    
    # MM Header
    #
    
    # ODA: Original Destination Address (DEFAULT_MAC_ADDRESS) - 6 octets
    
    # In case of unitary test
    if maximus.UNIT_TEST:
        mme.dst = mme.payload.load[:SIZE_OF_DST]
    
    # OSA: Original Source Address (Mac address of Gidel prototype) - 6 octets
    
    # In case of unitary test
    if maximus.UNIT_TEST:
        mme.src = mme.payload.load[SIZE_OF_DST:SIZE_OF_DST+SIZE_OF_SRC]
    
    # MTYPE: Ethertype (0x88E1) - 2 octets
    
    # In case of unitary test
    if maximus.UNIT_TEST:
        mme.type = ntoh16(mme.payload.load[SIZE_OF_DST+SIZE_OF_SRC:SIZE_OF_DST+SIZE_OF_SRC+SIZE_OF_TYPE])
    
    if mme.type != HPAV_MTYPE_MME:
        display(mme)
        raise Error("MM Header: bad MTYPE! (" + hex(mme.type) + ")")
    
    if maximus.UNIT_TEST:
        mme.payload.load = mme.payload.load[SIZE_OF_DST+SIZE_OF_SRC+SIZE_OF_TYPE:]
    
    # MMV: Management Message Version (0x01) - 1 octet
    begin = 0
    end = SIZE_OF_MMV
    if hptoh8(mme.payload.load[begin:end]) != HPAV_MMV:
        display(mme)
        raise Error("MM Header: bad MMV! (" + hex(hptoh8(mme.payload.load[begin:end])) + ")")
    
    # MMTYPE: Management Message Type - 2 octets
    begin = end
    end += SIZE_OF_MMTYPE
    if hptoh16(mme.payload.load[begin:end]) != INTERFACE_FCALL_MMTYPE:
        display(mme)
        raise Error("MM Header: bad MMTYPE! (" + hex(hptoh16(mme.payload.load[begin:end])) + ")")
    
    # FMI: Fragmentation Management Information (0x0000) - 2 octets
    begin = end
    end += SIZE_OF_FMI
    if hptoh16(mme.payload.load[begin:end]) != 0:
        display(mme)
        raise Error("MM Header: bad FMI! (" + hex(hptoh16(mme.payload.load[begin:end])) + ")")
    
    # MM ENTRY
    #
    
    # Module: Interface fcall sub-module (0x02) - 1 octet
    begin = end
    end += SIZE_OF_U8
    if hptoh8(mme.payload.load[begin:end]) != INTERFACE_MODULE_FCALL:
        display(mme)
        raise Error("MM Header: bad Module! (" + hex(hptoh8(mme.payload.load[begin:end])) + ")")
    
    # Length: Payload length (0 to 1492) - 2 octets
    begin = end
    end += SIZE_OF_U16
    length = hptoh16(mme.payload.load[begin:end])
    
    # FCALL header
    #
    # Version: Fcall message version (0x02) - 1 octet
    # Type: Fcall message type - 1 octet
        # 0x00 : NONE
        # 0x01 : REQ
        # 0x02 : RSP
    # MsgID: Unique message ID for each function call REQ - 2 octets
    # ParamNb: Number of function call  parameters - 1 octet
    # Flags: Function call flags - 1 octet
        # 0 : NONE
        # 1 : FAILED
    # Reserved: Fcall reserved parameter - 2 octets
    #
    begin = end
    end += SIZE_OF_FUNCTION_CALL_HEADER
    fcall_hdr = mme.payload.load[begin:end]
    if hptoh8(fcall_hdr[0]) != FUNCTION_CALL_VERSION: # uint8_t version
        display(mme)
        raise Error("FCALL header: bad version! (" + hex(hptoh8(fcall_hdr[0])) + ")")
    if hptoh8(fcall_hdr[1]) != FUNCTION_CALL_TYPE_RSP: # uint8_t type
        display(mme)
        raise Error("FCALL header: bad type! (" + hex(hptoh8(fcall_hdr[1])) + ")")
    #msg_id = hptoh16(fcall_hdr[2:4])
    msg_id = ntoh16(fcall_hdr[2:4])
    param_nb = hptoh8(fcall_hdr[4]) # uint8_t param_nb
    if hptoh8(fcall_hdr[5]) >= FUNCTION_CALL_FLAG_FAILED: # uint8_t flags
        display(mme)
        raise Error("FCALL header: flag failed! (" + hex(hptoh8(fcall_hdr[5])) + ")")
    
    # FCALL payload: function SCI message data
    #
    index = end
    name = read_name(mme.payload.load[index:])
    fcall = Fcall(name, maximus)
    length -= SIZE_OF_FUNCTION_CALL_HEADER + len(name) + 1
    index += len(name) + 1
    for n in range(0, param_nb):
        param_name = read_param_name(mme.payload.load[index:])
        index += len(param_name) + 1
        param_length = read_param_length(mme.payload.load[index:])
        index += SIZE_OF_U16
        param_value = read_param_value(mme.payload.load[index:], param_length)
        index += param_length
        fcall.add_param(param_name, param_value)
        length -= len(param_name) + 1 + SIZE_OF_U16 + param_length
        
        # In case of unitary test
        if maximus.UNIT_TEST:
            print "param", n+1, ":"
            print "name =", param_name
            print "length =", param_length
            print "value =", param_value
    
    if length != 0:
        display(mme)
        raise Error("length = " + str(length))
    
    # Callback
    if fcall.__class__.cb_dict.has_key(msg_id):
        fcall.__class__.cb_dict[msg_id](fcall)
        del fcall.__class__.cb_dict[msg_id]
    
    return fcall