summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane/client_utils/scapy_packet_builder_test.py
blob: 7894c3366592695246d12545369344a1ddcab6a6 (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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
#import packet_builder
import random
import string
import struct
import socket       
import json
import yaml
import binascii


from scapy.all import *



class CTRexPacketBuildException(Exception):
    """
    This is the general Packet Building error exception class.
    """
    def __init__(self, code, message):
        self.code = code
        self.message = message

    def __str__(self):
        return self.__repr__()

    def __repr__(self):
        return u"[errcode:%r] %r" % (self.code, self.message)

################################################################################################

def ipv4_str_to_num (ipv4_buffer):

    assert(type(ipv4_buffer)==str);
    assert(len(ipv4_buffer)==4);
    res=0;
    shift=24
    for i in ipv4_buffer:
       res = res + (ord(i)<<shift);
       shift =shift -8
    return res



def is_valid_ipv4(ip_addr):
    """
    return buffer in network order 
    """
    if  type(ip_addr)==str and len(ip_addr) == 4:
        return ip_addr

    if  type(ip_addr)==int :
        ip_addr = socket.inet_ntoa(struct.pack("!I", ip_addr)) 

    try:
        return socket.inet_pton(socket.AF_INET, ip_addr)
    except AttributeError:  # no inet_pton here, sorry
        return socket.inet_aton(ip_addr)
    except socket.error:  # not a valid address
        raise CTRexPacketBuildException(-10,"not valid ipv4 format");


class CTRexScriptsBase(object):
    """
    VM Script base class 
    """
    def clone (self):
        return copy.deepcopy(self)


class CTRexScFieldRangeBase(CTRexScriptsBase):

    FILED_TYPES = ["inc","dec","rand"]  

    def __init__(self, field_name,
                       field_type
                            ):
        super(CTRexScFieldRangeBase, self).__init__()
        self.field_name =field_name
        self.field_type =field_type
        if not self.field_type in CTRexScFieldRangeBase.FILED_TYPES :
            raise CTRexPacketBuildException(-12,"field type should be in [inc,dec,rand] ");


class CTRexScFieldRangeValue(CTRexScFieldRangeBase):
    """
    range of field value
    """
    def __init__(self, field_name, 
                       field_type,
                       min_val,
                       max_val
                           ):
        super(CTRexScFieldRangeValue, self).__init__(field_name,field_type)
        self.min_val =min_val;
        self.max_val =max_val;
        if min_val > max_val:
            raise CTRexPacketBuildException(-12,"min is greater than max ");
        if min_val == max_val:
            raise CTRexPacketBuildException(-13,"min value is equal to max value, you can't use this type of range ");


class CTRexScIpv4SimpleRange(CTRexScFieldRangeBase):
    """
    range of ipv4 ip
    """
    def __init__(self, field_name,field_type,min_ip, max_ip):
        super(CTRexScIpv4SimpleRange, self).__init__(field_name,field_type)
        self.min_ip = min_ip
        self.max_ip = max_ip
        mmin=ipv4_str_to_num (is_valid_ipv4(min_ip))
        mmax=ipv4_str_to_num (is_valid_ipv4(max_ip))
        if  mmin > mmax :
            raise CTRexPacketBuildException(-11,"CTRexScIpv4SimpleRange m_min ip is bigger than max ");


class CTRexScIpv4TupleGen(CTRexScriptsBase):
    """
    range tuple 
    """
    FLAGS_ULIMIT_FLOWS =1

    def __init__(self, min_ipv4, max_ipv4,num_flows=100000,min_port=1025,max_port=65535,flags=0):
        super(CTRexScIpv4TupleGen, self).__init__()
        self.min_ip = min_ipv4
        self.max_ip = max_ipv4
        mmin=ipv4_str_to_num (is_valid_ipv4(min_ipv4))
        mmax=ipv4_str_to_num (is_valid_ipv4(max_ipv4))
        if  mmin > mmax :
            raise CTRexPacketBuildException(-11,"CTRexScIpv4SimpleRange m_min ip is bigger than max ");

        self.num_flows=num_flows;

        self.min_port =min_port
        self.max_port =max_port
        self.flags =   flags


class CTRexScTrimPacketSize(CTRexScriptsBase):
    """
    trim packet size. field type is CTRexScFieldRangeBase.FILED_TYPES = ["inc","dec","rand"]  
    """
    def __init__(self,field_type="rand",min_pkt_size=None, max_pkt_size=None):
        super(CTRexScTrimPacketSize, self).__init__()
        self.field_type = field_type
        self.min_pkt_size = min_pkt_size
        self.max_pkt_size = max_pkt_size
        if max_pkt_size != None and min_pkt_size !=None :
            if min_pkt_size == max_pkt_size:
                raise CTRexPacketBuildException(-11,"CTRexScTrimPacketSize min_pkt_size is the same as max_pkt_size ");

            if min_pkt_size > max_pkt_size:
                raise CTRexPacketBuildException(-11,"CTRexScTrimPacketSize min_pkt_size is bigger than max_pkt_size ");

class CTRexScRaw(CTRexScriptsBase):
    """
    raw instructions 
    """
    def __init__(self,list_of_commands=None):
        super(CTRexScRaw, self).__init__()
        if list_of_commands==None:
            self.commands =[]
        else:
            self.commands = list_of_commands

    def add_cmd (self,cmd):
        self.commands.append(cmd)



################################################################################################
# VM raw instructions 
################################################################################################

class CTRexVmInsBase(object):
    """
    instruction base
    """
    def __init__(self,ins_type):
        self.type = ins_type
        assert(type(ins_type)==str);

class CTRexVmInsFixIpv4(CTRexVmInsBase):
    def __init__(self,offset):
        super(CTRexVmInsFixIpv4, self).__init__("fix_checksum_ipv4")
        self.offset = offset
        assert(type(offset)==int);


class CTRexVmInsFlowVar(CTRexVmInsBase):
    #TBD add more validation tests

    OPERATIONS =["inc", "dec", "random"]
    VALID_SIZES =[1,2,4,8]

    def __init__(self,fv_name,size,op,init_val,min_val,max_val):
        super(CTRexVmInsFlowVar, self).__init__("flow_var")
        self.name =fv_name;
        assert(type(fv_name)==str);
        self.size =size
        self.op =op
        self.init_val=init_val
        assert(type(init_val)==int);
        self.min_val=min_val
        assert(type(min_val)==int);
        self.max_val=max_val
        assert(type(max_val)==int);

class CTRexVmInsWrFlowVar(CTRexVmInsBase):
    def __init__(self,fv_name,pkt_offset,add_val=0,is_big=True):
        super(CTRexVmInsWrFlowVar, self).__init__("write_flow_var")
        self.name =fv_name
        assert(type(fv_name)==str);
        self.pkt_offset =pkt_offset
        assert(type(pkt_offset)==int);
        self.add_val =add_val
        assert(type(add_val)==int);
        self.is_big =is_big;
        assert(type(is_big)==bool);

class CTRexVmInsTrimPktSize(CTRexVmInsBase):
    def __init__(self,fv_name):
        super(CTRexVmInsTrimPktSize, self).__init__("trim_pkt_size")
        self.fv_name =fv_name
        assert(type(fv_name)==str);

class CTRexVmInsTupleGen(CTRexVmInsBase):
    def __init__(self,fv_name,ip_min,ip_max,port_min,port_max,limit_flows,flags=0):
        super(CTRexVmInsTupleGen, self).__init__("tuple_flow_var")
        self.name =fv_name
        assert(type(fv_name)==str);
        self.ip_min = ip_min;
        self.ip_max = ip_max;
        self.port_min = port_min;
        self.port_max = port_max;
        self.limit_flows = limit_flows;
        self.flags       =flags;


################################################################################################
# 
class CTRexVmEngine(object):

       def __init__(self):
            """
            inlcude list of instruction
            """
            super(CTRexVmEngine, self).__init__()
            self.ins=[]
            self.split_by_var = ''

       # return as json 
       def get_json (self):
           inst_array = [];
           # dump it as dict
           for obj in self.ins:
               inst_array.append(obj.__dict__);

           return {'instructions': inst_array, 'split_by_var': self.split_by_var}

       def add_ins (self,ins):
           #assert issubclass(ins, CTRexVmInsBase)
           self.ins.append(ins);

       def dump (self):
           cnt=0;
           for obj in self.ins:
               print "ins",cnt
               cnt = cnt +1
               print obj.__dict__

       def dump_bjson (self):
          print json.dumps(self.get_json(), sort_keys=True, indent=4)

       def dump_as_yaml (self):
          print yaml.dump(self.get_json(), default_flow_style=False)



################################################################################################

class CTRexScapyPktUtl(object):

    def __init__(self,scapy_pkt):
        self.pkt = scapy_pkt

    def pkt_iter (self):
        p=self.pkt;
        while True:
            yield p
            p=p.payload
            if p ==None or isinstance(p,NoPayload):
                break;

    def get_list_iter (self):
        l=list(self.pkt_iter())
        return l


    def get_pkt_layers (self):
        """
        return string 'IP:UDP:TCP'
        """
        l=self.get_list_iter ();
        l1=map(lambda p: p.name,l );
        return ":".join(l1);

    def _layer_offset(self,name,cnt=0):
        """
        return offset of layer e.g 'IP',1 will return offfset of layer ip:1 
        """
        save_cnt=cnt
        for pkt in self.pkt_iter ():
            if pkt.name == name:
                if cnt==0:
                    return (pkt, pkt.offset)
                else:
                    cnt=cnt -1

        raise CTRexPacketBuildException(-11,("no layer %s-%d" % (name,save_cnt)));


    def layer_offset(self,name,cnt=0):
        """
        return offset of layer e.g 'IP',1 will return offfset of layer ip:1 
        """
        save_cnt=cnt
        for pkt in self.pkt_iter ():
            if pkt.name == name:
                if cnt==0:
                    return pkt.offset
                else:
                    cnt=cnt -1

        raise CTRexPacketBuildException(-11,("no layer %s-%d" % (name,save_cnt)));

    def get_field_offet(self,layer,layer_cnt,field_name):
        """
        return offset of layer e.g 'IP',1 will return offfset of layer ip:1 
        """
        t=self._layer_offset(layer,layer_cnt);
        l_offset=t[1];
        layer_pkt=t[0]

        #layer_pkt.dump_fields_offsets ()

        for f in layer_pkt.fields_desc:
            if f.name == field_name:
                return (l_offset+f.offset,f.get_size_bytes ());

        raise CTRexPacketBuildException(-11,("no layer %s-%d." % (name,save_cnt,field_name)));

    def  get_layer_offet_by_str(self,layer_des):
        """
        return layer offset by string  

        IP:0
        IP:1
        return offset

        """
        l1=layer_des.split(":")
        layer=""
        layer_cnt=0;

        if len(l1)==1:
            layer=l1[0];
        else:
            layer=l1[0];
            layer_cnt=int(l1[1]);

        return self.layer_offset(layer,layer_cnt)



    def  get_field_offet_by_str(self,field_des):
        """
        return field_des (offset,size) layer:cnt.field 
        for example 

        IP.src
        IP:0.src  (first IP.src like IP.src)
        for example IP:1.src  for internal IP

        return (offset, size) as tuple 

        """

        s=field_des.split(".");
        if len(s)!=2:
            raise CTRexPacketBuildException(-11,("field desription should be layer:cnt.field e.g IP.src or IP:1.src "));


        layer_ex = s[0]
        field = s[1]

        l1=layer_ex.split(":")
        layer=""
        layer_cnt=0;

        if len(l1)==1:
            layer=l1[0];
        else:
            layer=l1[0];
            layer_cnt=int(l1[1]);

        return self.get_field_offet(layer,layer_cnt,field)

    def has_IPv4 (self):
        return self.pkt.has_layer("IP");

    def has_IPv6 (self):
        return self.pkt.has_layer("IPv6");

    def has_UDP (self):
        return self.pkt.has_layer("UDP");

################################################################################################

class CTRexVmDescBase(object):
    """
    instruction base
    """
    def __init__(self):
       pass;

    def get_obj (self):
        return self;

    def get_json (self):
        return self.get_obj().__dict__

    def dump_bjson (self):
       print json.dumps(self.get_json(), sort_keys=True, indent=4)

    def dump_as_yaml (self):
       print yaml.dump(self.get_json(), default_flow_style=False)


    def get_var_name(self):
        '''
          virtual function return the varible name if exists
        ''' 
        return None

    def compile(self,parent): 
        '''
          virtual function to take parent than has function name_to_offset
        ''' 
        pass;


def valid_fv_size (size):
    if not (size in CTRexVmInsFlowVar.VALID_SIZES):
        raise CTRexPacketBuildException(-11,("flow var has not valid size %d ") % size  );

def valid_fv_ops (op):
    if not (op in CTRexVmInsFlowVar.OPERATIONS):
        raise CTRexPacketBuildException(-11,("flow var does not have a valid op %s ") % op  );

def convert_val (val):
    if type(val) == int:
        return val
    else:
        if type(val) == str:
          return ipv4_str_to_num (is_valid_ipv4(val))
        else:
            raise CTRexPacketBuildException(-11,("init val not valid %s ") % val  );


class CTRexVmDescFlowVar(CTRexVmDescBase):
    def __init__(self,name,init_val=0,min_val=0,max_val=255,size=4,op="inc"):
        super(CTRexVmDescFlowVar, self).__init__()
        self.name = name;
        assert(type(name)==str);
        self.size =size
        valid_fv_size(size)
        self.op =op
        valid_fv_ops (op)
        self.init_val = convert_val (init_val)
        self.min_val  = convert_val (min_val);
        self.max_val  = convert_val (max_val)

        if self.min_val > self.max_val :
            raise CTRexPacketBuildException(-11,("max %d is lower than min %d ") % (self.max_val,self.min_val)  );

    def get_obj (self):
        return CTRexVmInsFlowVar(self.name,self.size,self.op,self.init_val,self.min_val,self.max_val);

    def get_var_name(self):
        return self.name


class CTRexVmDescFixIpv4(CTRexVmDescBase):
    def __init__(self,offset):
        super(CTRexVmDescFixIpv4, self).__init__()
        self.offset = offset; # could be a name of offset 

    def get_obj (self):
        return CTRexVmInsFixIpv4(self.offset);

    def compile(self,parent): 
        if type(self.offset)==str:
            self.offset = parent._pkt_layer_offset(self.offset);

class CTRexVmDescWrFlowVar(CTRexVmDescBase):
    def __init__(self,fv_name,pkt_offset,add_val=0,is_big=True):
        super(CTRexVmDescWrFlowVar, self).__init__()
        self.name =fv_name
        assert(type(fv_name)==str);
        self.pkt_offset =pkt_offset
        self.add_val =add_val
        assert(type(add_val)==int);
        self.is_big =is_big;
        assert(type(is_big)==bool);

    def get_obj (self):
            return  CTRexVmInsWrFlowVar(self.name,self.pkt_offset,self.add_val,self.is_big)

    def compile(self,parent): 
        if type(self.pkt_offset)==str:
            t=parent._name_to_offset(self.pkt_offset)
            self.pkt_offset = t[0]


################################################################################################


class CScapyTRexPktBuilder(object):

    """
    This class defines the TRex API of building a packet using dpkt package.
    Using this class the user can also define how TRex will handle the packet by specifying the VM setting.
    """
    def __init__(self):
        """
        Instantiate a CTRexPktBuilder object

        :parameters:
             None

        """
        super(CScapyTRexPktBuilder, self).__init__()
        self._pkt = None # scapy packet 
        self.vm_scripts = [] # list of high level instructions
        self.vm_low_level = None
        self.metadata=""

    def get_vm_data(self):
        """
        Dumps the instructions 

        :parameters:
            None

        :return:
            + json object of instructions

        """

        return self.vm_low_level.get_json () 

    def dump_pkt(self):
        """
        Dumps the packet as a decimal array of bytes (each item x gets value between 0-255)

        :parameters:
            None

        :return:
            + packet representation as array of bytes

        :raises:
            + :exc:`CTRexPktBuilder.EmptyPacketError`, in case packet is empty.

        """

        assert(self.pkt);

        pkt_in_hex = binascii.hexlify(str(self.pkt))
        return {"binary": [int(pkt_in_hex[i:i+2], 16)
                           for i in range(0, len(pkt_in_hex), 2)],
                "meta": self.metadata}


    def add_command (self,script):
        self.vm_scripts.append(script.clone());

    def dump_scripts (self):
        self.vm_low_level.dump_as_yaml ()

    def set_packet (self,pkt):
        """
        Scapy packet   Ether()/IP(src="16.0.0.1",dst="48.0.0.1")/UDP(dport=12,sport=1025)/IP()/"A"*10
        """
        self.pkt =pkt;


    def compile (self):
        self.vm_low_level=CTRexVmEngine()
        assert(self.pkt);
        self.pkt.build();

        
        for sc in self.vm_scripts:
            if isinstance(sc, CTRexScRaw):
                self._compile_raw (sc)

        #for obj in self.vm_scripts:
        #    # tuple gen script 
        #    if isinstance(obj, CTRexScIpv4TupleGen)
        #        self._add_tuple_gen(tuple_gen)

    ####################################################
    # private 

    def _compile_raw (self,obj):

        # make sure we have varibles once 
        vars={};
        for desc in obj.commands:
            print desc.__dict__
            var_name =  desc.get_var_name()
            print var_name
            if var_name :
                if vars.has_key(var_name):
                    raise CTRexPacketBuildException(-11,("varible %s define twice ") % (var_name)  );
                else:
                    vars[var_name]=1
            desc.compile(self);

        for desc in obj.commands:
            self.vm_low_level.add_ins(desc.get_obj());


    def _pkt_layer_offset (self,layer_name):
        assert(self.pkt != None);
        p_utl=CTRexScapyPktUtl(self.pkt);
        return p_utl.get_layer_offet_by_str(layer_name)

    def _name_to_offset(self,field_name):
        assert(self.pkt != None);
        p_utl=CTRexScapyPktUtl(self.pkt);
        return p_utl.get_field_offet_by_str(field_name)

    def  _add_tuple_gen(self,tuple_gen):

        pass;



################################################################################################
# tests 
################################################################################################
# what should I do 
# 1. iterator on the headers 
# 2. do I have one IP? 
# 3. offset of the UDP/TCP?
# offset of each field inside the packet ? 

def test_vm1 ():
    a=CTRexVmDescFlowVar(name="a",min_val=0,max_val=255,init_val=0,size=4,op="inc");
    a.dump_as_yaml ()
    a.dump_bjson()

    a=CTRexVmDescFlowVar(name="a",min_val="10.0.0.1",max_val="10.0.0.10",init_val="10.0.0.1",size=4,op="inc");
    a.dump_as_yaml ()
    a.dump_bjson()

    raw = CTRexScRaw()
    raw.add_cmd( CTRexVmDescFlowVar(name="a",min_val=0,max_val=255,init_val=0,size=4,op="inc") )
    raw.add_cmd( CTRexVmDescFixIpv4(offset = "IP") )
    raw.add_cmd( CTRexVmDescWrFlowVar (fv_name="a",pkt_offset= "IP.src") )


    raw1 = CTRexScRaw( [ CTRexVmDescFlowVar(name="a",min_val="16.0.0.1",max_val="16.0.0.10",init_val="16.0.0.1",size=4,op="inc"),
                          CTRexVmDescWrFlowVar (fv_name="a",pkt_offset= "IP.src"),
                          CTRexVmDescFixIpv4(offset = "IP")]
                      );

    pkt_builder = CScapyTRexPktBuilder();

    py='5'*128
    pkt=Ether()/ \
             IP(src="16.0.0.1",dst="48.0.0.1")/ \
             UDP(dport=12,sport=1025)/IP()/py

    # set packet 
    pkt_builder.set_packet(pkt);
    pkt_builder.add_command ( raw1 )
    pkt_builder.compile();

    pkt_builder.dump_scripts ()

    print pkt_builder.dump_pkt()
    print pkt_builder.get_vm_data()

    print "hey"







def test_udp ():
    py='5'*128
    p1=Ether()/ \
             IP(src="16.0.0.1",dst="48.0.0.1")/ \
             UDP(dport=12,sport=1025)/IP()/py

    p1.build();
    #print p1.haslayer("UDP.tos:1");

    #pkt_utl=CTRexScapyPktUtl(p1);
    #pkt_utl.has_layer("IP");

    p_utl=CTRexScapyPktUtl(p1);
    for p in p_utl.pkt_iter():
        print p.name,":"

    print p_utl.get_pkt_layers();

    print p_utl.layer_offset("IP")
    #print p_utl.layer_offset("IP",2)
    t=p_utl.get_field_offet("IP",0,"src")
    print t
    t=p_utl.get_field_offet("IP",0,"dst")
    print t
    t=p_utl.get_field_offet_by_str("IP:0.src")
    print t

    t=p_utl.get_field_offet_by_str("IP.src")
    print t






    #p1.dump_layers_offset()
    #p1.show2();
    #hexdump(p1);

    #wrpcap("ipv4.pcap", p1);


def test5 ():
    # build packet
    pass;


def test4 ():
    print "test4"
    a = CScapyTRexPktBuilder()
    a.add_command ( CTRexScIpv4TupleGen("16.0.0.1","16.0.0.5",flags=CTRexScIpv4TupleGen.FLAGS_ULIMIT_FLOWS))
    a.dump_scripts ()
    a.compile()


def convert_to_hex (buffer):
    for i in buffer:
         print hex(ord(i))


def test3 ():

    vm=CTRexVmEngine()
    vm.add_ins(CTRexVmInsFixIpv4(12))
    vm.add_ins(CTRexVmInsFixIpv4(15))
    #vm.dump();
    vm.dump_bjson ()
    vm.dump_as_yaml ()



def test2 ():
   vm_ins= CTRexVmInsTupleGen("a",0x12000001,0x12000007,1025,1027,limit_flows=10000)
   print vm_ins.__dict__



def test1 ():
    a  = CTRexScFieldRangeValue("ip.src","inc",12,14)
    print a.__dict__
    b  = CTRexScIpv4SimpleRange("ip.src","inc","16.0.0.1","16.0.0.254")
    print b.__dict__
    c= CTRexScIpv4TupleGen("16.0.0.1","16.0.0.5");
    print c.__dict__

    d= CTRexScIpv4TupleGen("16.0.0.1","16.0.0.5",flags=CTRexScIpv4TupleGen.FLAGS_ULIMIT_FLOWS);
    print d.__dict__

    e= CTRexScTrimPacketSize();
    print e.__dict__

    e1= CTRexScTrimPacketSize(min_pkt_size=120, max_pkt_size=130);
    print e1.__dict__




def main ():
    test_vm1 ();
    return 
    test_udp ()
    return 

    test4 ();
    return 

    test3 ();
    return 
    test2 ();
    return 
    test1 ();
    return
    #a=CTRexScIpv4SimpleRange("16.0.0.1","16.0.0.16");
    #s=is_valid_ipv4("16.0.0.1");
    #s=is_valid_ipv4("\x16\x01\x01\x01");
    #s=is_valid_ipv4(0x16000101);
    print ipv4_str_to_num("\x10\x01\x01\x01")
    return 

    print ipv4_str_to_num (s)


    print s
    print type(s);
    len(s);
    for i in s:
         print hex(ord(i))



main();