-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIP_MAP_Calculator.py
1630 lines (1478 loc) · 68.4 KB
/
IP_MAP_Calculator.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
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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import PySimpleGUI as sg
import ipaddress as ip
import os
import sys
'''IP_MAP_Calculator.py: Calculates the results of IP MAP Rule parameters'''
# IP_MAP_ADDRESS_CALCULATOR v0.12.02 - 01/07/2025 - D. Scott Freemire
# Window theme and frame variables
#-------------------------------------#
sg.theme('TanBlue') # Tan is #E5DECF, Blue is #09348A
windowfont=('Helvetica', 13)
name_tooltip = 'Enter unique rule name'
v6_tooltip = 'Format similar to 2008:8cd:0::/xx'
v6m_tooltip = 'Enter IPv6 mask bits'
v4_tooltip = 'Format similar to 192.168.2.0/24'
v4m_tooltip = 'Enter IPv4 mask bits'
ea_tooltip = 'Max 2 digits'
#psid_tooltip = 'Max 1 digit'
rulestring_tooltip = ('Name|IPv6 Prefix|IPv6 Pfx Len|IPv4 Prefix|'
'IPv4 Pfx Len|EA Len|PSID Offset')
fmr_tooltip = 'Create FMR option string (forwarding)'
v4mask = [n for n in range(16, 33)] # for edit rule Combo
v6mask = [n for n in range(32, 65)] # for edit rule Combo
psidoff = [n for n in range(16)] # for edit rule Combo
eabits = [n for n in range(33)] # for edit rule Combo
# Big headings for visibility in text editor "minimap"
'''
██ ██ ██ ██ █████ ██ ██ ██████ ██ ██ ████████ ███████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ███████ ████ ██ ██ ██ ██ ██ ███████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ███████ ██ ██ ██ ██████ ██████ ██ ███████
'''
# sg.Push() is like a spring between elements
# sg.Sizer(h_pixels=0, v_pixels=0) is like an adjustable block between elements
# expand_x=True causes an element to expand to the width of its container
# expand_y=True causes element to expand to the height of its container
# Main Display (top frame) - Calculated Values
#----------------------------------------------#
display_col1 = [
[sg.Text('Uniq v4 IPs', font=('Arial', 14, 'bold'))],
[sg.Text('', font=('Arial', 16, 'bold'), justification='centered',
size=(7, 1), background_color='#fdfdfc', border_width=4,
relief='ridge', key='-IPS_DSPLY-')]
]
display_col2 = [
[sg.Text('Sharing', font=('Arial', 14, 'bold'))],
[sg.Text('', font=('Arial', 16, 'bold'), justification='centered',
size=(7, 1), background_color='#fdfdfc', border_width=4,
relief='ridge', key='-RATIO_DSPLY-')]
]
display_col3 = [
[sg.Text('Users', font=('Arial', 14, 'bold'))],
[sg.Text('', font=('Arial', 16, 'bold'), justification='centered',
size=(7, 1), background_color='#fdfdfc', border_width=4,
relief='ridge', key='-USERS_DSPLY-')]
]
display_col4 = [
[sg.Text('Ports/User', font=('Arial', 14, 'bold'))],
[sg.Text('', font=('Arial', 16, 'bold'), justification='centered',
size=(7, 1), background_color='#fdfdfc', border_width=4,
relief='ridge', key='-PORTS_DSPLY-')]
]
display_col5 = [
[sg.Text('Excluded Ports', font=('Arial', 14, 'bold'))],
[sg.Text('', font=('Arial', 16, 'bold'), justification='centered',
size=(7, 1), background_color='#fdfdfc', border_width=4,
relief='ridge', key='-EXCL_PORTS_DSPLY-')]
]
# Top frame with results fields
display_layout = [
[sg.Column(display_col1, element_justification='centered'),
sg.Text('x', pad=((0, 0), (20, 0))),
sg.Column(display_col2, element_justification='centered'),
sg.Text('=', pad=((0, 0), (20, 0))),
sg.Column(display_col3, element_justification='centered'),
sg.Text(':', font=('Arial', 16, 'bold'), pad=((0, 0), (20, 0))),
sg.Column(display_col4, element_justification='centered'),
sg.Column(display_col5, element_justification='centered')],
[sg.Text('BMR', font=('Arial', 14, 'bold')),
sg.Input('', font=('Courier', 15, 'bold'),
# use_readonly... with disabled creates display field that can be
# selected and copied with cursor, but not edited (review need for this)
justification='centered', size=(60, 1), use_readonly_for_disable=True,
disabled=True, pad=((0, 8), (0, 0)), background_color='#fdfdfc',
key='-BMR_STRING_DSPLY-'),
sg.Button('Save', font=('Helvetica', 11), key='-SAVE-')],
[sg.Push(),
sg.Text('Select and copy, or click Save', font=('Helvetica', 13, 'italic'),
justification='centered', pad=((5, 5), (0, 5))),
sg.Push()],
]
# Parameter Editing Display (2nd frame)
#---------------------------------------#
param_edit_col1 = [
[sg.Text('Name:', font=('Arial', 14, 'bold')),
sg.Input('', font=('Arial', 14, 'bold'), size=(20, 1),
enable_events=True, tooltip=name_tooltip, border_width=2,
pad=((89, 5), (5, 5)), background_color='#fdfdfc', key='-RULENAME-'),
sg.Push(),
sg.Text('', text_color='red', font='None 14 bold', key='-PARAM_MESSAGES-'),
sg.Push()],
[sg.Text('IPv6 Prefix/Length:', font=('Arial', 14, 'bold')),
sg.Input('', font=('Arial', 14, 'bold'), size=(20, 1), enable_events=True,
tooltip=v6_tooltip, border_width=2, background_color='#fdfdfc',
key='-R6PRE-'),
sg.Text('/'),
sg.Combo(v6mask, readonly=True, font=('Helvetica', 14, 'bold'),
enable_events=True, background_color='#fdfdfc', key='-R6LEN-')],
[sg.Text('IPv4 Prefix/Length:', font=('Arial', 14, 'bold')),
sg.Input('', font=('Arial', 14, 'bold'), size=(20, 1), enable_events=True,
tooltip=v4_tooltip, border_width=2, background_color='#fdfdfc',
key='-R4PRE-'),
sg.Text('/'),
sg.Combo(v4mask, readonly=True, font=('Helvetica', 14, 'bold'),
enable_events=True, key='-R4LEN-',)],
[sg.Text('EA Bits Length', font=('Helvetica', 13, 'bold')),
sg.Sizer(h_pixels=32, v_pixels=0),
sg.Combo(eabits, readonly=True, font=('Helvetica', 14, 'bold'),
background_color='#fdfdfc', enable_events=True, key='-EABITS-',),
sg.Sizer(h_pixels=23, v_pixels=0),
sg.Text('PSID Offset Bits', font=('Helvetica', 13, 'bold')),
sg.Combo(psidoff, readonly=True, font=('Helvetica', 13, 'bold'),
background_color='#fdfdfc', enable_events=True, key='-OFFSET-'),
sg.Sizer(h_pixels=202, v_pixels=0),
sg.Button('Enter', font=('Helvetica', 11), key='-ENTER_PARAMS-')],
[sg.Sizer(h_pixels=0, v_pixels=5)],
[sg.HorizontalSeparator()],
[sg.Sizer(h_pixels=0, v_pixels=5)],
[sg.Text('Rule String:', font=(None, 14, 'italic', 'bold'),
pad=((5, 5), (5, 0))),
sg.Input('', font=('Courier', 14, 'bold'), size=(60, 1),
justification='centered', pad=((5, 5), (5, 0)), enable_events=True,
background_color='#fdfdfc', tooltip=rulestring_tooltip, key='-STRING_IN-'),
sg.Button('Enter', font='Helvetica 11', pad=((5, 5), (5, 0)),
key='-ENTER_STRING-')],
[sg.Push(),
sg.Text('Type or paste saved string and click Enter',
font=('Helvetica', 13, 'italic'), justification='centered',
pad=((5, 5), (0, 5))),
sg.Push()]
]
editor_layout = [
[sg.Column(param_edit_col1, expand_x=True)],
]
# Binary Display (3rd frame)
#-------------------------------------#
multiline1_layout = [
[sg.Multiline(size=(83, 13), auto_size_text=True,
font=('Courier', 14, 'bold'), background_color='#fdfdfc',
expand_x=True, disabled=True, # horizontal_scroll = True,
no_scrollbar=True, key='MLINE_BIN_1')]
]
multiline2_layout = [
[sg.Multiline(size=(83, 7), auto_size_text=True,
font=('Courier', 14, 'bold'), background_color='#fdfdfc',
expand_x=True, horizontal_scroll = False, disabled=True,
no_scrollbar=True, key='MLINE_BIN_2')]
]
bin_display_col1 = [
[sg.Sizer(h_pixels=0, v_pixels=6)],
[sg.Frame(
'BMR Prefix, User Prefix, & IPv4 Prefix - IPv4 Host & Port Calculation',
multiline1_layout, expand_x=True, border_width=1, relief='ridge',
font=('Helvetica', 13, 'bold'), title_location=sg.TITLE_LOCATION_TOP,)],
]
bin_display_col2 = [
[
sg.Sizer(37, 0),
sg.Text('IPv6 Prefix Length:', font=('Helvetica', 13, 'bold'),
pad=((0, 1), (5, 0))),
sg.Slider(range=(32, 64), default_value=32, orientation='h',
disable_number_display=False, enable_events=True,
size=(16, 8), trough_color='white', font=('Helvetica', 13, 'bold'),
text_color=None, disabled=True, key='-V6PFX_LEN_SLDR-'),
# sg.Push(),
sg.Sizer(8, 0),
sg.VerticalSeparator(),
sg.Sizer(8, 0),
# sg.Push(),
sg.Text('EA Length:', font=('Helvetica', 13, 'bold'),
pad=((0, 14), (5, 0))),
# sg.Sizer(h_pixels=5, v_pixels=0),
sg.Slider(range=(0, 32), default_value=32, orientation='h', # Per RFC 7598 (dhcp) 0-48 is valid.
disable_number_display=False, enable_events=True,
size=(19, 8), trough_color='white', font=('Helvetica', 13, 'bold'),
text_color=None, disabled=True, key='-EA_LEN_SLDR-'),
# sg.Sizer(40, 0),
],
[
sg.Sizer(37, 0),
sg.Text('IPv4 Prefix Length:', font=('Helvetica', 13, 'bold'),
pad=((0, 1), (5, 0))),
sg.Slider(range=(16, 32), default_value=16, orientation='h',
disable_number_display=False, enable_events=True,
size=(16, 8), trough_color='white', font=('Helvetica', 13, 'bold'),
disabled=True, text_color=None,
key='-V4PFX_LEN_SLDR-'),
# sg.Push(),
sg.Sizer(8, 0),
sg.VerticalSeparator(),
sg.Sizer(8, 0),
# sg.Push(),
sg.Text('PSID Offset:', font=('Helvetica', 13, 'bold'),
pad=((0, 5), (0, 5))),
sg.Slider(range=(0, 15), default_value=0, orientation='h',
disable_number_display=False, enable_events=True,
size=(19, 8), trough_color='white', font=('Helvetica', 13, 'bold'),
pad=((5, 5), (0, 10)), disabled=True, key='-PSID_OFST_SLDR-'),
# sg.Push()
],
[sg.Sizer(h_pixels=0, v_pixels=9)],
[sg.Push(),
sg.Text('Source Port Index:', font=('Helvetica', 13, 'bold')),
sg.Text('', font=('Arial', 14, 'bold'), justification='centered',
size=(16, 1), background_color='#fdfdfc', border_width=4,
relief='ridge', key='-SP_INDEX-'),
# sg.Input('', size=(16, 1), justification='centered',
# use_readonly_for_disable=True,disabled=True, key='-SP_INDEX-'),
sg.Button(' <<', font='Helvetica 11', key='-P_IDX_FIRST-'),
sg.Button(' + 1', font='Helvetica 11', key='-P_IDX_UP_1-'),
sg.Button(' + 10', font='Helvetica 11', key='-P_IDX_UP_10-'),
sg.Button(' + 100', font='Helvetica 11', key='-P_IDX_UP_100-'),
sg.Button(' >>', font='Helvetica 11', key='-P_IDX_LAST-'),
sg.Text(f'= Port', font=('Helvetica', 14, 'bold')),
sg.Text('', font=('Helvetica', 14, 'bold'), justification='centered',
size=(7, 1), background_color='#fdfdfc', border_width=4,
relief='ridge', key='-SP_INT-'),
# sg.Input('', size=(7, 1), justification='centered',
# use_readonly_for_disable=True, disabled=True, key='-SP_INT-'),
sg.Push()],
[sg.Sizer(0, 9)],
[sg.HorizontalSeparator()],
[sg.Sizer(h_pixels=0, v_pixels=3)],
[sg.Sizer(10, 0),
sg.Text('End-user IPv6 prefix (User PD):',
pad=0, font=('Helvetica', 13, 'bold'))],
[sg.Sizer(h_pixels=0, v_pixels=8)],
[sg.Push(),
sg.Text('User PD', font=('Helvetica', 13, 'bold'), pad=((5, 0), (5, 5))),
sg.Input('', size=(24, 1), pad=((4, 5), (5, 5)),
font=('Helvetica', 13, 'bold'), justification='centered',
use_readonly_for_disable=True, disabled=True, key='-USER_PD-',
disabled_readonly_background_color='#fdfdfc'),
sg.Push(),
sg.Text('CE IP', font='Helvetica 13 bold', pad=((0, 0), (5, 5))),
sg.Input('', size=(16, 1), font=('Helvetica', 13, 'bold'),
pad=((5, 0), (5, 5)), justification='centered',
use_readonly_for_disable=True, disabled=True, key='-USER_IP4-',
disabled_readonly_background_color='#fdfdfc'),
sg.Push(),
sg.Text('Port', font=('Helvetica', 13, 'bold'), pad=((4, 0), (5, 5))),
sg.Input('', size=((9), 1), font=('Helvetica', 13, 'bold'),
justification='centered', use_readonly_for_disable=True,
disabled=True, key='-USER_PORT-',
disabled_readonly_background_color='#fdfdfc'),
sg.Push()],
[sg.Sizer(0, 5)],
[sg.Frame('User IPv6 Source Address and Port',
multiline2_layout, expand_x=True, border_width=1, relief='ridge',
font=('Helvetica', 13, 'bold'), pad=((0, 0), (0, 0)),
title_location=sg.TITLE_LOCATION_TOP,)],
[sg.Sizer(0, 7)],
[sg.Sizer(h_pixels=2, v_pixels=0),
sg.Button('Next User PD', font='Helvetica 11', key='-NXT_USER_PD-'),
sg.Push(),
sg.Text('IPv4 Host:', font=('Helvetica', 13, 'bold')),
sg.Slider(range=(0, 0), default_value=0, orientation='h',
disable_number_display=False, enable_events=True,
size=(42, 8), trough_color='white', font=('Helvetica', 14, 'bold'),
pad=((5, 10), (0, 0)), key='-V4HOST_SLIDER-'),
sg.Button(' + 1', font='Helvetica 11', key='-NEXT_HOST-'),
],
]
# Error messages
bin_display_col3 = [
[sg.Push(),
sg.Text('', text_color='red', font='None 14 bold',
pad=((5, 5), (0, 0)), key='-PD_MESSAGES-'),
sg.Push()],
[sg.Sizer(h_pixels=0, v_pixels=1)]
]
bin_display_layout = [
[sg.Column(bin_display_col1, element_justification='centered',
expand_x=True)],
[sg.Column(bin_display_col2,
expand_x=True)],
[sg.Column(bin_display_col3, expand_x=True)],
]
# DHCP Display (4th frame)
#-------------------------------------#
dhcp_layout = [
# [sg.Text('------- DHCPv6 Options for MAP-T CEs -------', font=(None, 12, 'bold'),
# expand_x=True, justification='center')],
[sg.Text('DMR:', font=(None, 13, 'bold')),
sg.Input('Ex. 2001:db8:ffff::/64', background_color='#fdfdfc',
border_width=2, disabled=True, key='DMR_INPUT'),
sg.Button('Enter', key='DMR_ENTER')],
[sg.Text('S46 MAP-T Container Option 95:', font=('Helvetica', 13, 'bold')),
sg.Checkbox('FMR String', font=('Helvetica', 13, 'bold'), tooltip=fmr_tooltip,
enable_events=True, key='FMR_FLAG'),
sg.Text('', text_color='red', font='None 14 bold', key='-DHCP_MESSAGES-')],
[sg.Sizer(4, 0),
sg.Input('', justification='centered', size=(85, 1), disabled=True,
use_readonly_for_disable=True, key='OPT_95')],
# [sg.Text(f'Option 95', font=('Helvetica', 13, 'bold')),
# sg.Input('', disabled=True, use_readonly_for_disable=True, key='OPT_95')], # OPTION_S46_CONT_MAPT (95)
[sg.Text(f'Option 89', font=('Helvetica', 13, 'bold')),
sg.Input('', size=(55, 1), disabled=True, use_readonly_for_disable=True, key='OPT_89')], # OPTION_S46_RULE (89)
[sg.Text('Option 93', font=('Helvetica', 13, 'bold')),
sg.Input('', disabled=True, use_readonly_for_disable=True, key='OPT_93')], # OPTION_S46_PORTPARAMS (93)
[sg.Text(f'Option 91', font=('Helvetica', 13, 'bold')),
sg.Input('', disabled=True, use_readonly_for_disable=True, key='OPT_91')], # OPTION_S46_DMR (91)
# [sg.Button('Generate', key='DHCP_GENERATE')]
]
# sg.Input('', font=('Courier', 15, 'bold'),
# # use_readonly... with disabled creates display field that can be
# # selected and copied with cursor, but not edited (review need for this)
# justification='centered', size=(60, 1), use_readonly_for_disable=True,
# disabled=True, pad=((0, 8), (0, 0)), background_color='#fdfdfc',
# key='-BMR_STRING_DSPLY-'),
# Binary Display (5th frame)
#-------------------------------------#
button_col1 = [
[sg.Button('Example', font='Helvetica 11', key='-EXAMPLE-'),
sg.Button('Clear', font='Helvetica 11', key='-CLEAR-'),
sg.Text('', text_color='red', font='None 14 bold', key='-BTN_MESSAGES-'),
sg.Push(),
# sg.Button('Save', font='Helvetica 11', key=('-SAVE_MAIN-')),
sg.Button('About', font=('Helvetica', 12)),
sg.Button(' Exit ', font=('Helvetica', 12, 'bold'))]
]
button_layout = [
[sg.Column(button_col1, expand_x=True)]
]
# Saved rule string frame (6th frame)
#-------------------------------------#
#MLINE_SAVED = '-MLINE_SAVED-'+sg.WRITE_ONLY_KEY
saved_section_layout = [
[sg.Sizer(h_pixels=3, v_pixels=0),
sg.Multiline(default_text='', size=(81, 8),
font=('Courier', 14, 'bold'), disabled=True, autoscroll=True,
expand_x=True, expand_y=True, pad=(0,0), horizontal_scroll=True,
background_color='#fdfdfc', key='-MLINE_SAVED-'),
sg.Push()]
]
# All Sections
#-------------------------------------#
sections_layout = [
[sg.Frame('', display_layout, expand_x=True, border_width=6,
relief='ridge', element_justification='centered')],
[sg.Frame('Enter or Edit BMR Parameters', editor_layout,
font=('Helvetica', 13, 'bold'), title_location=sg.TITLE_LOCATION_TOP,
expand_x=True, border_width=6, relief='ridge')],
[sg.Frame('', bin_display_layout, expand_x=True, border_width=6,
relief='ridge')],
[sg.Frame('DHCPv6 Options for MAP-T CEs', dhcp_layout,
font=('Helvetica', 13, 'bold'), title_location=sg.TITLE_LOCATION_TOP,
expand_x=True, border_width=6, relief='ridge')],
[sg.Frame('', button_layout, expand_x=True, border_width=6,
relief='ridge')],
[sg.Frame('Saved Rule Strings', saved_section_layout, expand_x=True,
font=('Helvetica', 13, 'bold'), title_location=sg.TITLE_LOCATION_TOP,
border_width=6, relief='ridge')]
]
# Final Layout
# Width 735 allows multiline widths of 83
#-----------------------------------------#
layout = [
[sg.Column(sections_layout, size=(735, None), expand_y=True,
scrollable=True, vertical_scroll_only = True,
sbar_background_color='#D6CFBF', sbar_arrow_color='#09348A',
sbar_relief='solid')]
]
#-------------------------------------------------------------------------#
# Create Main Window
#-------------------------------------------------------------------------#
# Window uses last screen location for next start
# Location is set upon Exit or WINDOW_CLOSE... event
# Width 780 allows Final Layout width of 735
window = sg.Window('IP MAP Calculator', layout, font=windowfont,
enable_close_attempted_event=True,
location=sg.user_settings_get_entry('-location-', (None, None)),
# keep_on_top=False, resizable=True, size=(780, 1150), finalize=True) #(755, 1070)
# keep_on_top=False, resizable=True, size=(780, 1260), finalize=True) #(755, 1070)
keep_on_top=False, resizable=True, size=(780, 1445), finalize=True) #(755, 1070)
# Prevent horizontal window resizing
# window.set_resizable(False, True) # Not available until PySimpleGUI v5
# Formatting for disabled input string fields - applied immediately
#-------------------------------------------------------------------#
window['-BMR_STRING_DSPLY-'].Widget.config(readonlybackground='#fdfdfc',
borderwidth=3, relief='ridge')
window['DMR_INPUT'].Widget.config(readonlybackground='#fdfdfc',
borderwidth=2)
window['OPT_95'].Widget.config(readonlybackground='#fdfdfc',
borderwidth=2)
window['OPT_89'].Widget.config(readonlybackground='#fdfdfc',
borderwidth=2)
window['OPT_93'].Widget.config(readonlybackground='#fdfdfc',
borderwidth=2)
window['OPT_91'].Widget.config(readonlybackground='#fdfdfc',
borderwidth=2)
# Enable "Return" key to trigger Enter event in Rule String field
# Bind events to text fields
window['-STRING_IN-'].bind('<Return>', '_Enter')
window['DMR_INPUT'].bind('<FocusIn>', '_FOCUS')
window['DMR_INPUT'].bind('<Return>', '_Enter')
'''
██████ ██ ██ ██ ███████ ██████ █████ ██ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ██ ██ █████ ██ ███████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██████ ███████ ███████ ██████ ██ ██ ███████ ██████
'''
#----------------------------------------------------------------------------#
# Calculate Results
#----------------------------------------------------------------------------#
# param_ls contains BMR parameters
# param_ls = [name, v6pfx, v6pfx len, v4pfx, v4pfx len, ealen, psid offset]
# user_pd_obj is "User delegated prefix" received from DHCP
def rule_calc(param_ls, user_pd_obj, v4host = None, portidx = None):
# print('\n********** START rule_calc **************')
'''The rule_calc function accepts a BMR paramater list, an IPv6
user PD (ipaddress object), and an optional port index integer from
a user input or editing option. It calculates the network address results,
formats them for the UI, and enters them into a dictionary for the
UI display function displays_update().
'''
# initial values
#----------------------------------#
psidlen = (param_ls[5] - (32 - param_ls[4])) # ea_len - host_len
bmrpfx_len = param_ls[2]
upd_len = user_pd_obj.prefixlen
# ppusr = 2^(16 - k) - 2^m (per rfc7597) (2^m is # of excluded ports)
# 16 is the number of IP port bits (constant)
# Used for d_dic and other calculations
if param_ls[6] > 0: # Because PSID Offset > 0 excludes some ports
ppusr = (2 ** (16 - psidlen)) - (2 ** (16 - psidlen - param_ls[6]))
else:
ppusr = (2 ** (16 - psidlen))
# If call is from the host slider, update the upd with host bits
# and update v4pfx value in param_ls
if v4host != None:
pdbin = f'{user_pd_obj.network_address:b}'
pdbin_l = pdbin[:param_ls[2]]
pdbin_r = pdbin[param_ls[2] + (32 - last_params[4]) : ]
v4hostbin = bin(v4host)[2:].zfill(32 - param_ls[4])
newpdbin = pdbin_l + v4hostbin + pdbin_r
newpdint = int(newpdbin, 2)
new_upd_add_str = \
ip.IPv6Address(newpdint).compressed + '/' + str(user_pd_obj.prefixlen)
user_pd_obj = ip.IPv6Network(new_upd_add_str)
v4pfxbin = f'{ip.IPv4Address(param_ls[3]):b}'[: int(param_ls[4])]
newv4bin = v4pfxbin + v4hostbin
newv4int = int(newv4bin, 2)
newv4add = ip.IPv4Address(newv4int)
v4str = newv4add.compressed
else:
v4pfxbin = f'{ip.IPv4Address(param_ls[3]):b}'
v4hostbin = f'{user_pd_obj.network_address:b}]'[bmrpfx_len : bmrpfx_len + (32 - param_ls[4])]
v4hostint = int(v4hostbin, 2)
newv4bin = v4pfxbin[:param_ls[4]] + v4hostbin
# window['-V4HOST_SLIDER-'].update(value=0)
newv4add = ip.IPv4Address(int(newv4bin, 2))
v4str = newv4add.compressed
window['-V4HOST_SLIDER-'].update(v4hostint)
#-------------------------------------------------------------------------#
# Binary display strings
#-------------------------------------------------------------------------#
# Sec 1, BMR PD, User PD, and EA Bits data
#--------------------------------------------------#
v6p_hex_exp = ip.IPv6Address(param_ls[1]).exploded # Hex style: 0000:0000:...
v6p_hex_seglst = v6p_hex_exp.split(':')[:4] # ['0000', '0000', ...]
upd_hex_exp = ip.IPv6Address(user_pd_obj.network_address).exploded # 0000:0000:...
upd_hex_seglst = upd_hex_exp.split(':')[:4] # ['0000', '0000', ...]
v6p_bin = f'{ip.IPv6Address(param_ls[1]):b}'[:64] # first 64 bits of pfx
v6p_bin_seglst = [v6p_bin[i:i+16] for i in range(0, 64, 16)]
v6p_bin_fmt = ':'.join(v6p_bin_seglst) + f'::/{bmrpfx_len}'
upd_bin = f'{ip.IPv6Address(user_pd_obj.network_address):b}'[:64]
upd_bin_seglst = [upd_bin[i:i+16] for i in range(0, 64, 16)]
upd_bin_fmt = ':'.join(upd_bin_seglst) + f'::/{upd_len}'
ea_bin_idxl = V6Indices(param_ls[2]) # V6Indices adds # of ":"separators
ea_bin_idxr = V6Indices(upd_len)
ea_bin_fmt = upd_bin_fmt[ea_bin_idxl : ea_bin_idxr]
# Sec 1, User PD (upd) string data
#-----------------------------------#
v4hostbin_len = 32 - param_ls[4]
psid_idxl = param_ls[2] + v4hostbin_len
psid_idxr = param_ls[2] + param_ls[5]
psid = upd_bin[psid_idxl : psid_idxr]
# Generate 16 bit string for Port number
# is PSID > 0?
if param_ls[6] > 0:
psid_ofst_bin = bin(1)[2:].zfill(param_ls[6]) # binary 1 with len = offset
else:
psid_ofst_bin = ''
portrpad_bin = '0' * (16 - param_ls[6] - psidlen) # MAY NEED TO ACCEPT EDITOR VALUES !!!
port_bin = psid_ofst_bin + \
psid + \
portrpad_bin
port_int = str(int(port_bin, 2))
pidx_bin = psid_ofst_bin + portrpad_bin # Port index number binary string
# Sec 1, IPv4 string data
#-----------------------------------#
# v4ip_str = v4str #param_ls[3]
v4_seglst = v4str.split('.')
v4mask = param_ls[4]
# v4_obj = ip.ip_network(v4str + '/' + str(v4mask), strict=False)
# v4_bin = f'{ip.IPv4Address(v4ip_str):b}'
# make segments equal length for display
v4_bin_seglst = [newv4bin[i:i+8] for i in range(0, 32, 8)]
v4_bin_fmt = '.'.join(v4_bin_seglst) + f'/{v4mask}'
for i, seg in enumerate(v4_seglst):
if len(seg) == 3:
pass
elif len(seg) == 2:
v4_seglst[i] = f' {seg}'
elif len(seg) == 1:
v4_seglst[i] = f' {seg} '
# Sec 1, Modify Port string if Port Index (portidx) has been changed
#--------------------------------------------------------------------#
if portidx:
# Initial value of port index
pidx_bin_base = int(pidx_bin, 2)
if portidx == 0:
pass
# Increment val can't exceed max value of idx binary - index initial value
elif portidx < (2 ** len(pidx_bin)) - pidx_bin_base:
pidx_int = int(pidx_bin, 2)
pidx_int = pidx_int + portidx
pidx_bin = bin(pidx_int)[2:].zfill(len(pidx_bin)) # for d_dic
psid_ofst_bin = pidx_bin[: len(psid_ofst_bin)]
portrpad_bin = pidx_bin[len(psid_ofst_bin) :]
port_bin = psid_ofst_bin + psid + portrpad_bin
port_int = int(port_bin, 2) # for d_dic
else:
window['-PD_MESSAGES-'].update('Port index maximum reached')
pidx_bin = "1" * len(pidx_bin)
pidx_int = int(pidx_bin, 2)
psid_ofst_bin = pidx_bin[: len(psid_ofst_bin)]
portrpad_bin = pidx_bin[len(psid_ofst_bin) :]
port_bin = psid_ofst_bin + psid + portrpad_bin
port_int = int(port_bin, 2) # for d_dic
# Binary Section 1
# BMR PD, User PD, and EA Bits dictionary entries
#--------------------------------------------------#
bin_str_dic = {
# 'params_str': (f' --- BMR PD Len /{param_ls[2]},'
# f' with User PD Len /{upd_len}'
# f' = EA Bits Len {param_ls[5]} ---')
'blank1': '',
'v6p_hexstr': (f" BMR PD:{' ' * 8}"
f"{' : '.join(v6p_hex_seglst)}"
f" ::/{bmrpfx_len}"),
'upd_hexstr': (f" User PD:{' ' * 7}"
f"{' : '.join(upd_hex_seglst)}"
f" ::/{upd_len}"),
'bmr_binstr': f' BMR PD: {v6p_bin_fmt}',
'upd_binstr': f' User PD: {upd_bin_fmt}',
'ea_binstr': f' EA Bits: {"." * V6Indices(param_ls[2])}{ea_bin_fmt}',
# IPv4 dictionary entries
#--------------------------------------------------#
'blank2': '',
'v4_intstr': (f" IPv4 Addr: "
f"{' . '.join(v4_seglst)}"
f" /{v4mask}"),
'v4_binstr': f" IPv4 Addr: {v4_bin_fmt}",
# Port and Port Index dictionary entries
#--------------------------------------------------#
'blank3': '',
'portidx': (f' The "PORT INDEX" is PSID-Offset/Right-Padding:'
f' {psid_ofst_bin}-{portrpad_bin}'),
'port_bin': (f' The PORT is PSID-Offset/PSID/Right-Padding: '
f'{psid_ofst_bin}-{psid}-{portrpad_bin}'
f' = Port {port_int}')
}
# Sec 2, User source IPv6 string data
#--------------------------------------------------#
pad1 = ' ' * 6
pad2 = '0' * 16
v6hex_pad = f'{pad1}0000{pad1}'
v4bin_segls = [newv4bin[i:i+16] for i in range(0, 32, 16)]
v4hex_segls = [int(str(x),2) for x in v4bin_segls]
v4hex_segls = [hex(x)[2:].zfill(4) for x in v4hex_segls]
v4hex_segs = f'{pad1}{v4hex_segls[0]}{pad1}:{pad1}{v4hex_segls[1]}{pad1}'
if psid != '':
psid_hex = int(psid,2)
else:
psid_hex = 0
psid_hex = pad1 + hex(psid_hex)[2:].zfill(4)
v6sip_hex_pfx = bin_str_dic['upd_hexstr'][10:78]
v4sip_bin = f'{v4bin_segls[0]}:{v4bin_segls[1]}'
psid_bin = psid.zfill(16)
v6sip_bin = upd_bin_fmt[:68]
# Binary Section 2
# Source IPv6 address dictionary entry
#--------------------------------------------------#
bin_ipstr_dic = {
# 'label_1': (' ' * 23) + '--- User IPv6 Source Address: ---'
'blank_line1': '',
'v6sip_hex_str1': f' [ {v6sip_hex_pfx}',
'v6sip_binstr1': f' [ {v6sip_bin}',
'blank_line2': '',
'v6sip_hex_str2': f' {v6hex_pad}:{v4hex_segs}:{psid_hex} ]:{port_int}',
'v6sip_binstr2': f' {pad2}:{v4sip_bin}:{psid_bin} ]:{port_int}'
}
#-------------------------------------------------------------------------#
# Binary display highlight indices
#-------------------------------------------------------------------------#
# Binary display 1 highlight index data
#---------------------------------------------#
# return index of first character after " BMR PD: "
bmr_pfx_l = next(i for (i, e) in enumerate(bin_str_dic["bmr_binstr"])
if e not in "BMR PD: ")
bmr_pfx_r = bmr_pfx_l + V6Indices(param_ls[2])
v6_pfxlen_l = 79
v6_pfxlen_r = 82
upd_pfx_l = bmr_pfx_r
upd_pfx_r = bmr_pfx_l + V6Indices(upd_len)
upd_binstr_sbnt_l = upd_pfx_r
upd_binstr_sbnt_r = bin_str_dic['upd_binstr'].index('::')
ea_binstr_l = next(i for (i, e) in enumerate(bin_str_dic["ea_binstr"])
if e not in "EA Bits:.")
ea_binstr_r = ea_binstr_l + len(ea_bin_fmt)
# ea_binstr_div is v4host_r and psid_l
ea_binstr_div = bmr_pfx_l + V6Indices(param_ls[2] + v4hostbin_len)
prtidx_ofst_l = 48
prtidx_ofst_r = prtidx_ofst_l + param_ls[6]
prtidx_pad_l = prtidx_ofst_r + 1
prtidx_pad_r = prtidx_pad_l + (16 - param_ls[6] - psidlen)
portbin_ofst_hl_l = 45
portbin_ofst_hl_r = portbin_ofst_hl_l + param_ls[6]
portbin_psid_hl_l = 45 + len(psid_ofst_bin) + 1
portbin_psid_hl_r = portbin_psid_hl_l + psidlen
portbin_pad_hl_l = portbin_psid_hl_r + 1
portbin_pad_hl_r = portbin_pad_hl_l + (16 - param_ls[6] - psidlen)
v4ip_hl_l = 13 + V4Indices(param_ls[4])
v4ip_hl_r = 13 + V4Indices(32)
# Binary display 1 highlight index dictionary
#---------------------------------------------#
# Prepend line number for each highlight index
hl_dic1 = {
'bmr_hl': [f'4.{bmr_pfx_l}', f'4.{bmr_pfx_r}'],
'bmr_len_hl': [f'4.{v6_pfxlen_l}', f'4.{v6_pfxlen_r}'],
'upd_hl': [f'5.{upd_pfx_l}', f'5.{upd_pfx_r}'],
'upd_len_hl': [f'5.{v6_pfxlen_l}', f'5.{v6_pfxlen_r}'],
'sbnt_hl': [f'5.{upd_binstr_sbnt_l}', f'5.{upd_binstr_sbnt_r}'],
'ea_v4_hl': [f'6.{ea_binstr_l}', f'6.{ea_binstr_div}'],
'ea_psid_hl': [f'6.{ea_binstr_div}', f'6.{ea_binstr_r}'],
'v4ip_hl': [f'8.{v4ip_hl_l}', f'8.{v4ip_hl_r}'],
'v4ipbin_hl': [f'9.{v4ip_hl_l}', f'9.{v4ip_hl_r}'],
'prtidx_ofst_hl': [f'11.{prtidx_ofst_l}', f'11.{prtidx_ofst_r}'],
'prtidx_pad_hl': [f'11.{prtidx_pad_l}', f'11.{prtidx_pad_r}'],
'portbin_ofst_hl': [f'12.{portbin_ofst_hl_l}', f'12.{portbin_ofst_hl_r}'],
'portbin_psid_hl': [f'12.{portbin_psid_hl_l}', f'12.{portbin_psid_hl_r}'],
'portbin_pad_hl': [f'12.{portbin_pad_hl_l}', f'12.{portbin_pad_hl_r}']
}
# Binary display 2 highlight index data
#---------------------------------------------#
if bmrpfx_len % 16 == 0: # Prevent highlighting a colon
v4if_l1_l = 5 + (V6Indices(bmrpfx_len)) + 1
else:
v4if_l1_l = 5 + (V6Indices(bmrpfx_len))
v4if_l1_r = 5 + (V6Indices(bmrpfx_len + v4hostbin_len))
psid_l1_l = v4if_l1_r
psid_l1_r = 5 + (V6Indices(bmrpfx_len + v4hostbin_len + psidlen))
v4if_l2_l = 5 + (V6Indices(16 + param_ls[4]))
v4if_l2_r = v4if_l2_l + (32 - param_ls[4])
psid_l2_l = 56 + (16 - psidlen)
psid_l2_r = 72
# Binary display 2 highlight index dictionary
#---------------------------------------------#
# Prepend line number for each highlight index
hl_dic2 = {
'v4if_hl1': [f'3.{v4if_l1_l}', f'3.{v4if_l1_r}'],
'psid_hl1': [f'3.{psid_l1_l}', f'3.{psid_l1_r}'],
'v4if_hl2': [f'6.{v4if_l2_l}', f'6.{v4if_l2_r}'],
'psid_hl2': [f'6.{psid_l2_l}', f'6.{psid_l2_r}']
}
#-------------------------------------------------------------------------#
# Results = Display values dictionary
#-------------------------------------------------------------------------#
d_dic = {
'paramlist': param_ls,
'v4ips': 2 ** v4hostbin_len, # 2^host_bits
'sratio': 2 ** psidlen, # num of unique psids per v4-host address
'users': (2 ** v4hostbin_len) * (2 ** psidlen),
'ppusr': ppusr,
'excl_ports': 65536 - ((2 ** psidlen) * (ppusr)),
'bmr_str': '|'.join([str(x) for x in param_ls]),
'upd_str': user_pd_obj.compressed, # upd_str = User Delegated Prefix (PD)
'ce_ip': v4str,
'port_int': port_int,
'pidx_bin': pidx_bin,
'bin_str_dic': bin_str_dic,
'bin_ipstr_dic': bin_ipstr_dic,
'hl_dic1': hl_dic1,
'hl_dic2': hl_dic2,
'num_excl_ports': 2 ** (16 - param_ls[6])
}
# If v4host is None, clear DHCPv6 fields. If v4host is 0, don't clear
if not v4host and v4host != 0:
clear_dhcp_fields()
displays_update(d_dic, user_pd_obj)
return
'''
██████ ██ ███████ ██████ ██ █████ ██ ██ ███████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ███████ ██████ ██ ███████ ████ ███████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ███████ ██ ███████ ██ ██ ██ ███████
'''
def displays_update(dic, pd_obj):
'''The displays_update function accepts a display values dictionary
and an IPv6 user PD (ipaddress object) from the rule_calc function.
It then updates all affected display fields.
'''
# Initial field updates
#----------------------------------#
# Output BMR results to display
window['-IPS_DSPLY-'].update(dic['v4ips'])
window['-RATIO_DSPLY-'].update(dic['sratio'])
window['-USERS_DSPLY-'].update(dic['users'])
window['-PORTS_DSPLY-'].update(dic['ppusr'])
window['-EXCL_PORTS_DSPLY-'].update(dic['excl_ports'])
window['-BMR_STRING_DSPLY-'].update(dic['bmr_str'])
# Output values to Edit String and Values fields
window['-STRING_IN-'].update(dic['bmr_str'])
for i, element in enumerate(pframe_ls):
window[element].update(dic['paramlist'][i])
# Output User PD, CE IP, and Port to binary string editor
window['-USER_PD-'].update(dic['upd_str'])
window['-USER_IP4-'].update(dic['ce_ip'])
window['-USER_PORT-'].update(dic['port_int'])
window['-SP_INT-'].update(dic['port_int'])
window['-SP_INDEX-'].update(dic['pidx_bin'])
# Binary displays update values and highlights
multiline1: sg.Multiline = window['MLINE_BIN_1']
multiline2: sg.Multiline = window['MLINE_BIN_2']
# Output binary strings to binary string editor
multiline1.update('') # Clear field
for num, bstr in enumerate(dic['bin_str_dic']):
# Don't append \n after last line
multiline1.update(dic['bin_str_dic'][bstr]
+ ('\n' if num < len(dic['bin_str_dic']) -1 else ''), append=True)
multiline2.update('') # Clear field
for num, bstr in enumerate(dic['bin_ipstr_dic']):
# Don't append \n after last line
multiline2.update(dic['bin_ipstr_dic'][bstr]
+ ('\n' if num < len(dic['bin_ipstr_dic']) -1 else ''), append=True)
# Apply highlighting
highlights(multiline1, dic)
highlights(multiline2, dic)
# Output values to binary editor sliders and input fields
window['-V6PFX_LEN_SLDR-'].update(disabled=False)
window['-EA_LEN_SLDR-'].update(disabled=False)
window['-V4PFX_LEN_SLDR-'].update(disabled=False)
window['-PSID_OFST_SLDR-'].update(disabled=False)
window['-V6PFX_LEN_SLDR-'].update(dic['paramlist'][2])
window['-EA_LEN_SLDR-'].update(dic['paramlist'][5])
window['-V4PFX_LEN_SLDR-'].update(dic['paramlist'][4])
window['-PSID_OFST_SLDR-'].update(dic['paramlist'][6])
window['-V4HOST_SLIDER-'].update(range=(0, dic['v4ips'] - 1))
# window['-PORT_SLIDER-'].update(range=(0, (dic['ppusr'] - 1) ))
# window['-PORT_INDEX-'].update('0') #### <<<<--- UPDATE WITH ACTUAL VALUE !!! !!!
return
'''
██████ ██ █████ ███████ ███████ ███████ ███████ ██
██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ███████ ███████ ███████ █████ ███████ ██
██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ███████ ██ ██ ███████ ███████ ███████ ███████ ██
███████ ██ ██ ███ ██ ██████ ████████ ██ ██████ ███ ██ ███████
██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ████ ██ ██
█████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██████ ██ ████ ██████ ██ ██ ██████ ██ ████ ███████
'''
#----------------------------------------------------------------------------#
# Classes & Functions
#----------------------------------------------------------------------------#
def highlights(display, dic):
""" Highlighting for binary displays. Called from displays_update() """
widget = display.Widget
# Highlight color option definitions
#-------------------------------------#
widget.tag_config('white', foreground='black', background='#FFFFFF')
widget.tag_config('yellow', foreground='black', background='#FFFF00')
widget.tag_config('new_yellow', foreground='black', background='#F8FF00')
widget.tag_config('burley', foreground='black', background='#FFD39B') # burlywood
widget.tag_config('burley3', foreground='black', background='#CDAA7D') # burlywood3
widget.tag_config('grey49', foreground='black', background='#7D7D7D')
widget.tag_config('sage', foreground='black', background='#C2C9A6')
widget.tag_config('peri', foreground='black', background='#B2CAFA') # periwinkle
widget.tag_config('pink', foreground='black', background='#EDABBF') # cherry blossom pink
widget.tag_config('new_teal', foreground='black', background='#7cdff3') # moonstone
widget.tag_config('lt_purple', foreground='black', background='#D7C1D5')
widget.tag_config('lt_blue1', foreground='black', background='#B3C3D1') # lt blue grey
widget.tag_config('lt_blue2', foreground='black', background='#CCD7E0') # ltr blue grey
widget.tag_config('lt_blue', foreground='black', background='#B2CAFA') # lt blue
widget.tag_config('tan', foreground='black', background='tan')
widget.tag_config('lt_tan', foreground='black', background='#dbcdbd')
widget.tag_config('lt_orange', foreground='black', background='#ffcc99')
widget.tag_config('new_lt_green', foreground='black', background='#c2ebc2')
if display.Key == 'MLINE_BIN_1':
# widget.tag_add('lt_orange', *dic['hl_dic1']['title_hl'])
widget.tag_add('new_lt_green', *dic['hl_dic1']['bmr_hl'])
widget.tag_add('new_lt_green', *dic['hl_dic1']['bmr_len_hl'])
widget.tag_add('new_lt_green', *dic['hl_dic1']['upd_hl'])
widget.tag_add('new_lt_green', *dic['hl_dic1']['upd_len_hl'])
widget.tag_add('grey49', *dic['hl_dic1']['sbnt_hl'])
widget.tag_add('pink', *dic['hl_dic1']['ea_v4_hl'])
widget.tag_add('new_teal', *dic['hl_dic1']['ea_psid_hl'])
widget.tag_add('burley', *dic['hl_dic1']['prtidx_ofst_hl'])
widget.tag_add('yellow', *dic['hl_dic1']['prtidx_pad_hl'])
widget.tag_add('burley', *dic['hl_dic1']['portbin_ofst_hl'])
widget.tag_add('new_teal', *dic['hl_dic1']['portbin_psid_hl'])
widget.tag_add('yellow', *dic['hl_dic1']['portbin_pad_hl'])
widget.tag_add('pink', *dic['hl_dic1']['v4ip_hl'])
widget.tag_add('pink', *dic['hl_dic1']['v4ipbin_hl'])
elif display.Key == 'MLINE_BIN_2':
# widget.tag_add('lt_orange', *dic['hl_dic2']['heading_hl'])
widget.tag_add('pink', *dic['hl_dic2']['v4if_hl1'])
widget.tag_add('new_teal', *dic['hl_dic2']['psid_hl1'])
widget.tag_add('pink', *dic['hl_dic2']['v4if_hl2'])
widget.tag_add('new_teal', *dic['hl_dic2']['psid_hl2'])
return
# Example BMR prefix lists and parameters
#-----------------------------------------#
# param_ls = [name, v6pfx, v6pfx mask, v4pfx, v4pfx mask, ealen, psid offset]
class ExampleParams:
'''Returns a list of example BMR parameters. The Python ipaddress module and
self.rv6blk is used to create an IPv6 class (.subnets) object. New IPv6 BMR
prefixes are pulled from this object until ExampleParams() is called and
last_plist (created in global space by a global function) has been changed.
At this time, a new class instance is created and saved.
'''
def __init__(self):
self.ex_cnt = None
self.last_plist = []
self.rv6plen = 46
self.rv4plen = 24
self.ealen = 14
self.psid_ofst = 6
self.rv6blk = '2001:db8::/32'
self.rv4blk = '192.168.0.0/16'
self.rv6pfx_obj = None
self.rv4pfx_obj = None
def new_params(self):
if self.last_plist:
plist = self.last_plist
plist[0] = 'example_' + str(next(self.ex_cnt) + 1)
plist[1] = next(self.rv6pfx_obj).network_address.compressed
plist[3] = next(self.rv4pfx_obj).network_address.compressed
return(plist)
else:
self.ex_cnt = (i for i in range(16384))
self.rv6pfx_obj = ip.ip_network(self.rv6blk).subnets(
new_prefix = self.rv6plen)
self.rv4pfx_obj = ip.ip_network(self.rv4blk).subnets(
new_prefix = self.rv4plen)
plist = ['example_' + str(next(self.ex_cnt) + 1),
next(self.rv6pfx_obj).network_address.compressed,
self.rv6plen,
next(self.rv4pfx_obj).network_address.compressed,
self.rv4plen,
self.ealen,
self.psid_ofst
]
self.last_plist = plist
return(plist)
class UserPd:
'''Return a User Delegated Prefix (PD|upd) object.
If BMR parameters (last_plist) remains the same, it returns
PD object from current object. If the parameters change,
a new PD object is created.
syntax: x = UserPd(param_ls)
print(x.new_pd())'''
def __init__(self, plist):
self.plist = plist
self.last_plist = []
self.pd_obj = None
self.lastpd = ''
def new_pd(self):
if self.plist == self.last_plist: # Next PD from current PD object
nextpd = next(self.pd_obj)
self.lastpd = nextpd
return nextpd
else: # New PD object & new PD
self.last_plist = self.plist
bmr_v6p = ip.ip_network('/'.join((self.plist[1], str(self.plist[2]))))
pd_len = int(self.plist[2]) + int(self.plist[5])
self.pd_obj = bmr_v6p.subnets(new_prefix = pd_len)
nextpd = next(self.pd_obj)
self.lastpd = nextpd
return nextpd
# "Pad right" when IPv6 prefix not divisible by 8
def find_next_divisible(num):
"""Finds the next higher number evenly divisible by the divisor."""
while True:
if num % 8 == 0:
return num
num += 1
#-------------------------------------------------------------------------#
# DHCPv6 Option String Calculations = Based on RFC7597
#-------------------------------------------------------------------------#
def dhcp_calc(dmr, fmr=False):
rulv6_obj = ip.ip_network(f"{param_ls[1]}/{param_ls[2]}")
rulv6_len = rulv6_obj.prefixlen
rulv4_obj = ip.ip_network(f"{param_ls[3]}/{param_ls[4]}")
rulv4_len = rulv4_obj.prefixlen
# rulv4_host_len = 32 - rulv4_len
ea_bits = int(param_ls[5])
ps_ofst = int(param_ls[6]) # "a bits" from RFC 7597
# psid_len = ea_bits - (32 - rulv4_len) # "k bits" from RFC 7597