Skip to content

Commit 1d9a56f

Browse files
committed
Creada GUI para la Tabla del Switch
1 parent cb020a9 commit 1d9a56f

File tree

4 files changed

+71
-102
lines changed

4 files changed

+71
-102
lines changed

Config.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ maxlogs = 20
1919
[BOOLEANS]
2020
print-key-pressed = False
2121

22+
[SWITCH]
23+
routing-ttl = 10
24+
2225
[DEBUG]
2326
show-cable-rectangle = False
2427
packet-received = False

Interface2.glade

Lines changed: 12 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,93 +1247,31 @@ http://ddavo.me</property>
12471247
<object class="GtkApplicationWindow" id="window_switch-table">
12481248
<property name="can_focus">False</property>
12491249
<property name="window_position">center</property>
1250+
<property name="skip_taskbar_hint">True</property>
1251+
<property name="urgency_hint">True</property>
12501252
<child>
12511253
<object class="GtkBox" id="window_switch-table_box">
12521254
<property name="visible">True</property>
12531255
<property name="can_focus">False</property>
12541256
<property name="orientation">vertical</property>
1257+
<property name="baseline_position">top</property>
12551258
<child>
1256-
<object class="GtkBox" id="window_switch_hbox">
1259+
<object class="GtkTreeView" id="window_switch-table-TreeView">
12571260
<property name="visible">True</property>
1258-
<property name="can_focus">False</property>
1259-
<property name="margin_bottom">3</property>
1260-
<property name="homogeneous">True</property>
1261-
<child>
1262-
<object class="GtkLabel" id="window_switch_mac">
1263-
<property name="visible">True</property>
1264-
<property name="can_focus">False</property>
1265-
<property name="margin_left">5</property>
1266-
<property name="label" translatable="yes">MAC</property>
1267-
<attributes>
1268-
<attribute name="weight" value="ultraheavy"/>
1269-
</attributes>
1270-
</object>
1271-
<packing>
1272-
<property name="expand">False</property>
1273-
<property name="fill">True</property>
1274-
<property name="position">0</property>
1275-
</packing>
1276-
</child>
1277-
<child>
1278-
<object class="GtkLabel" id="window_switch_port">
1279-
<property name="visible">True</property>
1280-
<property name="can_focus">False</property>
1281-
<property name="margin_left">5</property>
1282-
<property name="margin_right">5</property>
1283-
<property name="label" translatable="yes">PORT</property>
1284-
<property name="justify">center</property>
1285-
<attributes>
1286-
<attribute name="weight" value="ultraheavy"/>
1287-
</attributes>
1288-
</object>
1289-
<packing>
1290-
<property name="expand">False</property>
1291-
<property name="fill">True</property>
1292-
<property name="position">1</property>
1293-
</packing>
1294-
</child>
1295-
<child>
1296-
<object class="GtkLabel" id="window_switch_ttl">
1297-
<property name="visible">True</property>
1298-
<property name="can_focus">False</property>
1299-
<property name="margin_right">5</property>
1300-
<property name="label" translatable="yes">TTL (s)</property>
1301-
<property name="justify">center</property>
1302-
<attributes>
1303-
<attribute name="weight" value="ultraheavy"/>
1304-
</attributes>
1305-
</object>
1306-
<packing>
1307-
<property name="expand">False</property>
1308-
<property name="fill">True</property>
1309-
<property name="position">2</property>
1310-
</packing>
1261+
<property name="can_focus">True</property>
1262+
<property name="reorderable">True</property>
1263+
<property name="rules_hint">True</property>
1264+
<property name="level_indentation">3</property>
1265+
<child internal-child="selection">
1266+
<object class="GtkTreeSelection"/>
13111267
</child>
13121268
</object>
13131269
<packing>
1314-
<property name="expand">False</property>
1270+
<property name="expand">True</property>
13151271
<property name="fill">True</property>
13161272
<property name="position">0</property>
13171273
</packing>
13181274
</child>
1319-
<child>
1320-
<object class="GtkGrid" id="window_switch-table_grid">
1321-
<property name="visible">True</property>
1322-
<property name="can_focus">False</property>
1323-
<property name="margin_left">5</property>
1324-
<property name="margin_right">5</property>
1325-
<property name="row_spacing">2</property>
1326-
<property name="column_homogeneous">True</property>
1327-
<child>
1328-
<placeholder/>
1329-
</child>
1330-
</object>
1331-
<packing>
1332-
<property name="expand">False</property>
1333-
<property name="fill">True</property>
1334-
<property name="position">1</property>
1335-
</packing>
1336-
</child>
13371275
<child>
13381276
<object class="GtkButton" id="window_switch-table_button">
13391277
<property name="label">gtk-close</property>
@@ -1345,7 +1283,7 @@ http://ddavo.me</property>
13451283
<packing>
13461284
<property name="expand">False</property>
13471285
<property name="fill">True</property>
1348-
<property name="position">2</property>
1286+
<property name="position">1</property>
13491287
</packing>
13501288
</child>
13511289
</object>

Main.py

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -962,42 +962,58 @@ class w_switch_table(Gtk.ApplicationWindow):
962962
def __init__(self, switch):
963963
self.link = switch
964964
builder = switch.builder
965-
builder.get_object("window_switch_hbox").override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(*hex_to_rgba("#FF9800")))
965+
#builder.get_object("window_switch_hbox").override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(*hex_to_rgba("#FF9800")))
966966
builder.get_object("window_switch-table_button").connect("clicked", self.hide)
967967
builder.get_object("window_switch-table").connect("delete-event", self.hide)
968-
self.labels = []
968+
self.store = Gtk.ListStore(str,int,int,int)
969+
970+
self.view = builder.get_object("window_switch-table-TreeView")
971+
self.view.set_model(self.store)
972+
for i, column_title in enumerate(["MAC", "Puerto", "TTL (s)"]):
973+
renderer = Gtk.CellRendererText()
974+
column = Gtk.TreeViewColumn(column_title, renderer, text=i)
975+
self.view.append_column(column)
976+
self.ticking = False
977+
builder.get_object("window_switch-table").set_keep_above(True)
969978

970979
def show(self, *a):
971-
self.link.builder.get_object("window_switch-table").show_all()
972980
self.ticking = True
973981
GObject.timeout_add(1001, self.tick)
982+
for row in self.store:
983+
row[2] = row[3] - time.time()
984+
self.link.builder.get_object("window_switch-table").show_all()
974985

975986
def hide(self, window, *event):
976987
self.link.builder.get_object("window_switch-table").hide()
977988
self.ticking = False
978989
return True
979990
def append(self, lst):
980-
grid = self.link.builder.get_object("window_switch-table_grid")
981-
grid.insert_row(0)
982-
for i in range(len(lst)):
983-
if i == 0:
984-
readmac = str(hex(lst[i])).upper().replace("0X", "")
985-
readmac = ":".join([readmac[i * 2:i * 2 + 2] for i,bl in enumerate(readmac[::2])])
986-
child = Gtk.Label.new(readmac)
987-
elif i == 2:
988-
child = Gtk.Label.new(str(int(lst[i]-time.time())))
989-
child.exp = lst[i]
990-
self.labels.append(child)
991-
else:
992-
child = Gtk.Label.new(str(lst[i]))
993-
grid.attach(child, i, 0, 1, 1)
994-
child.show()
995-
print("Appending", child)
991+
lst.append(lst[2])
992+
for row in self.store:
993+
row[2] = row[3] - time.time()
994+
row = self.store.append(lst)
995+
print(self.view.get_property("visible"))
996+
if self.view.get_property("visible") == True and self.ticking == False:
997+
self.ticking = True
998+
GObject.timeout_add(1001, self.tick)
999+
9961000
def tick(self):
997-
for label in self.labels:
998-
label.set_label(str(int(label.exp-time.time())))
1001+
for row in self.store:
1002+
row[2] = row[3] - time.time()
1003+
if row[2] <= 0:
1004+
try:
1005+
self.store.remove(row.iter)
1006+
self.link.table.remove(row)
1007+
except:
1008+
pass
1009+
if len(self.store) == 0:
1010+
self.ticking = False
9991011
return self.ticking
10001012
def remove(self, lst):
1013+
for row in self.store:
1014+
if row == lst:
1015+
self.store.remove(row.iter)
1016+
self.link.table
10011017
pass
10021018
#Get children, if children.get_label() == MAC, delete it.
10031019

@@ -1012,7 +1028,7 @@ def __init__(self, x, y, *args, name="Default", maxconnections=5, ip=None):
10121028
ObjetoBase.__init__(self, x, y, self.objectype, name=name, maxconnections=maxconnections)
10131029
self.x = x
10141030
self.y = y
1015-
self.timeout = 100 #Segundos
1031+
self.timeout = 20 #Segundos
10161032

10171033
for p in range(self.max_connections):
10181034
self.Port(self)
@@ -1052,16 +1068,26 @@ def packet_received(self, pck, port=None):
10521068
macs = "{0:0112b}".format(pck.frame)[6*8+1:6*16+1]
10531069

10541070
#LO PRIMERO: AÑADIRLO A LA TABLA
1071+
readmac = str(hex(int(macs,2))).upper().replace("0X", "")
1072+
readmac = ":".join([readmac[i * 2:i * 2 + 2] for i,bl in enumerate(readmac[::2])])
10551073

1056-
if int(macs,2) not in [x[0] for x in self.table]:
1057-
tmp = [int(macs,2), port, int(time.time()+self.timeout)]
1058-
self.table.append(tmp)
1059-
self.wtable.append(tmp)
10601074
for tab in self.table:
10611075
if tab[2] <= time.time():
10621076
print("Ha llegado tu hora")
10631077
self.table.remove(tab)
10641078
self.wtable.remove(tab)
1079+
if tab[0] == int(macd,2):
1080+
print("TAB[0] == mcd")
1081+
tab[2] = int(time.time()+self.timeout)
1082+
for row in self.wtable.store:
1083+
print(row[0], tab[0])
1084+
if int(row[0].replace(":",""),16) == tab[0]:
1085+
row[3] = int(time.time()+self.timeout)
1086+
if int(macs,2) not in [x[0] for x in self.table]:
1087+
tmp = [int(macs,2), port, int(time.time()+self.timeout)]
1088+
self.table.append(tmp)
1089+
tmp = [readmac, port, int(time.time()+self.timeout)]
1090+
self.wtable.append(tmp)
10651091

10661092
################################
10671093

@@ -1120,6 +1146,8 @@ def debug(self, *args):
11201146
for row in self.table:
11211147
if row[1] == None:
11221148
row[1] = "None"
1149+
if int(row[2]-time.time()) <= 0:
1150+
self.table.remove(row)
11231151
print(row_format.format(row[0], row[1], int(row[2]-int(time.time()))))
11241152

11251153
#¿Tengo permisos de escritura?, no se si tendré permisos
@@ -1624,7 +1652,7 @@ def __init__(self):
16241652
pass
16251653

16261654
def create(r, sourceip, desti_ip, *n, payload=int( 4.3*10**19 ) << 6 | 42, \
1627-
flags=0b010, ttl=10):
1655+
flags=0b010, ttl=32):
16281656
self = Ping()
16291657
if r == 0:
16301658
Type = 8

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Si instalas la versión más reciente de Ubuntu 16, no es necesaria la descarga
5050
- [x] Enrutamiento de paquetes
5151
- [x] Regenerar dirección MAC en las opciones
5252
- [x] Hacer que las MAC sean una clase
53-
- [ ] Tabla de enrutamiento para los Switch
53+
- [x] Tabla de enrutamiento para los Switch
5454
- [ ] Poder "seleccionar"
5555
- [ ] Máscaras de red para las ip.
5656
- [ ] ARP

0 commit comments

Comments
 (0)