This commit is contained in:
Gašper Dobrovoljc 2024-04-28 12:44:34 +02:00
commit 271f97e993
No known key found for this signature in database
GPG Key ID: 0E7E037018CFA5A5
14 changed files with 506 additions and 0 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

BIN
LDN5/promet.pcapng Normal file

Binary file not shown.

67
LDN5/tabele.txt Normal file
View File

@ -0,0 +1,67 @@
PC1
default via 192.168.4.1 dev eth0
192.168.4.0/24 dev eth0 scope link src 192.168.4.2
2001:db8:ef:4::/64 dev eth0 metric 256
fe80::/64 dev eth0 metric 256
default via 2001:db8:ef:4::1 dev eth0 metric 1024
ff00::/8 dev eth0 metric 256
--------------------------------------------------------------------------
PC2
default via 172.16.8.1 dev eth0
172.16.8.0/24 dev eth0 scope link src 172.16.8.2
2001:db8:8:ef::/64 dev eth0 metric 256
fe80::/64 dev eth0 metric 256
default via 2001:db8:8:ef::1 dev eth0 metric 1024
ff00::/8 dev eth0 metric 256
--------------------------------------------------------------------------
R1
10.0.0.0/30 dev eth1 proto kernel scope link src 10.0.0.1
10.1.0.0/30 dev eth0 proto kernel scope link src 10.1.0.1
127.0.0.0/8 dev lo proto kernel scope link src 127.0.0.1
172.16.8.0/24 via 10.0.0.2 dev eth1 proto zebra
192.168.4.0/24 via 10.1.0.2 dev eth0 proto zebra
2001:db8:0:f::/64 dev eth1 proto kernel metric 256
2001:db8:8:ef::/64 via 2001:db8:0:f::2 dev eth1 proto zebra metric 1024
2001:db8:e:1::/64 dev eth0 proto kernel metric 256
2001:db8:ef:4::/64 via 2001:db8:e:1::2 dev eth0 proto zebra metric 1024
fe80::/64 dev eth1 proto kernel metric 256
fe80::/64 dev eth0 proto kernel metric 256
--------------------------------------------------------------------------
R2
10.1.0.0/30 dev eth0 proto kernel scope link src 10.1.0.2
127.0.0.0/8 dev lo proto kernel scope link src 127.0.0.1
172.16.8.0/24 via 10.1.0.1 dev eth0 proto zebra
192.168.4.0/24 dev eth1 proto kernel scope link src 192.168.4.1
2001:db8:8:ef::/64 dev eth0 metric 256
fe80::/64 dev eth0 metric 256
default via 2001:db8:8:ef::1 dev eth0 metric 1024
ff00::/8 dev eth0 metric 256
--------------------------------------------------------------------------
R3
10.0.0.0/30 dev eth0 proto kernel scope link src 10.0.0.2
127.0.0.0/8 dev lo proto kernel scope link src 127.0.0.1
172.16.8.0/24 dev eth1 proto kernel scope link src 172.16.8.1
192.168.4.0/24 via 10.0.0.1 dev eth0 proto zebra
2001:db8:0:f::/64 dev eth0 proto kernel metric 256
2001:db8:8:ef::/64 dev eth1 proto kernel metric 256
2001:db8:ef:4::/64 via 2001:db8:0:f::1 dev eth0 proto zebra metric 1024
fe80::/64 dev eth1 proto kernel metric 256
fe80::/64 dev eth0 proto kernel metric 256

BIN
LDN5/topologija.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

152
LDN6/konfiguracija.txt Normal file
View File

@ -0,0 +1,152 @@
/ # ping 192.168.4.2
PING 192.168.4.2 (192.168.4.2): 56 data bytes
64 bytes from 192.168.4.2: seq=0 ttl=62 time=3.644 ms
64 bytes from 192.168.4.2: seq=1 ttl=62 time=7.362 ms
64 bytes from 192.168.4.2: seq=2 ttl=62 time=3.554 ms
64 bytes from 192.168.4.2: seq=3 ttl=62 time=3.654 ms
64 bytes from 192.168.4.2: seq=4 ttl=62 time=3.574 ms
--- 192.168.4.2 ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 3.554/4.357/7.362 ms
/ # traceroute 192.168.4.2
traceroute to 192.168.4.2 (192.168.4.2), 30 hops max, 46 byte packets
1 10.0.0.1 (10.0.0.1) 1.149 ms 1.144 ms 1.063 ms
2 1.1.1.2 (1.1.1.2) 3.049 ms 3.751 ms 1.721 ms
3 192.168.4.2 (192.168.4.2) 2.876 ms 3.201 ms 2.826 ms
/ # ping 192.168.4.2
PING 192.168.4.2 (192.168.4.2): 56 data bytes
64 bytes from 192.168.4.2: seq=0 ttl=61 time=9.979 ms
64 bytes from 192.168.4.2: seq=1 ttl=61 time=5.306 ms
64 bytes from 192.168.4.2: seq=2 ttl=61 time=8.230 ms
64 bytes from 192.168.4.2: seq=3 ttl=61 time=4.963 ms
64 bytes from 192.168.4.2: seq=4 ttl=61 time=7.786 ms
--- 192.168.4.2 ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 4.963/7.252/9.979 ms
/ # traceroute 192.168.4.2
traceroute to 192.168.4.2 (192.168.4.2), 30 hops max, 46 byte packets
1 10.0.0.1 (10.0.0.1) 1.079 ms 0.978 ms 0.875 ms
2 1.1.1.10 (1.1.1.10) 1.878 ms 2.529 ms 1.820 ms
3 1.1.1.5 (1.1.1.5) 4.696 ms 4.186 ms 3.769 ms
4 192.168.4.2 (192.168.4.2) 4.442 ms 9.486 ms 5.144 ms
R1
1.1.1.8/30 dev eth1 proto kernel scope link src 1.1.1.9
10.0.0.0/24 dev eth2 proto kernel scope link src 10.0.0.1
127.0.0.0/8 dev lo proto kernel scope link src 127.0.0.1
172.16.8.0/24 via 1.1.1.10 dev eth1 proto zebra metric 1
192.168.4.0/24 via 1.1.1.10 dev eth1 proto zebra
R2
1.1.1.4/30 dev eth1 proto kernel scope link src 1.1.1.5
10.0.0.0/24 via 1.1.1.6 dev eth1 proto zebra
127.0.0.0/8 dev lo proto kernel scope link src 127.0.0.1
172.16.8.0/24 via 1.1.1.6 dev eth1 proto zebra metric 1
192.168.4.0/24 dev eth2 proto kernel scope link src 192.168.4.1
R3
1.1.1.4/30 dev eth0 proto kernel scope link src 1.1.1.6
1.1.1.8/30 dev eth1 proto kernel scope link src 1.1.1.10
10.0.0.0/24 via 1.1.1.9 dev eth1 proto zebra metric 1
127.0.0.0/8 dev lo proto kernel scope link src 127.0.0.1
172.16.8.0/24 dev eth2 proto kernel scope link src 172.16.8.1
192.168.4.0/24 via 1.1.1.5 dev eth0 proto zebra metric 1
R1
configure
set interfaces ethernet eth0 address 1.1.1.1/30
set interfaces ethernet eth1 address 1.1.1.9/30
set interfaces ethernet eth3 address 10.0.0.1/30
set protocols bgp 65531 neighbor 1.1.1.2 remote-as '65532'
set protocols bgp 65531 neighbor 1.1.1.2 update-source 1.1.1.1
set protocols bgp 65531 parameters router-id 1.1.1.1
set protocols bgp 65531 neighbor 1.1.1.10 remote-as '65533'
set protocols bgp 65531 neighbor 1.1.1.10 update-source 1.1.1.9
set protocols bgp 65531 parameters router-id 1.1.1.10
set protocols bgp 65531 network 10.0.0.0/24
commit
save
exit
R2
configure
set interfaces ethernet eth0 address 1.1.1.2/30
set interfaces ethernet eth1 address 1.1.1.5/30
set interfaces ethernet eth2 address 192.168.4.1/24
set protocols bgp 65532 neighbor 1.1.1.1 remote-as '65531'
set protocols bgp 65532 neighbor 1.1.1.1 update-source 1.1.1.2
set protocols bgp 65532 parameters router-id 1.1.1.2
set protocols bgp 65532 neighbor 1.1.1.6 remote-as '65533'
set protocols bgp 65532 neighbor 1.1.1.6 update-source 1.1.1.5
set protocols bgp 65532 parameters router-id 1.1.1.6
set protocols bgp 65532 network 192.168.4.0/24
commit
save
exit
R3
configure
set interfaces ethernet eth0 address 1.1.1.6/30
set interfaces ethernet eth1 address 1.1.1.10/30
set interfaces ethernet eth3 address 172.16.8.1/24
set protocols bgp 65533 neighbor 1.1.1.9 remote-as '65531'
set protocols bgp 65533 neighbor 1.1.1.9 update-source 1.1.1.10
set protocols bgp 65533 parameters router-id 1.1.1.10
set protocols bgp 65533 neighbor 1.1.1.5 remote-as '65532'
set protocols bgp 65533 neighbor 1.1.1.5 update-source 1.1.1.6
set protocols bgp 65533 parameters router-id 1.1.1.6
set protocols bgp 65533 network 172.16.8.0/24
commit
save
exit
PC1
cat /etc/network/interfaces
auto eth0
iface eth0 inet static
address 10.0.0.2
netmask 255.255.255.0
gateway 10.0.0.1
PC2
cat /etc/network/interfaces
auto eth0
iface eth0 inet static
address 192.168.4.2
netmask 255.255.255.0
gateway 192.168.4.1
PC3
cat /etc/network/interfaces
auto eth0
iface eth0 inet static
address 172.16.8.2
netmask 255.255.255.0
gateway 172.16.8.1

BIN
LDN6/promet.pcapng Normal file

Binary file not shown.

BIN
LDN6/topologija.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 202 KiB

8
LDN7/.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

10
LDN7/.idea/LDN7.iml Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

7
LDN7/.idea/misc.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.12 (LDN7)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (LDN7)" project-jdk-type="Python SDK" />
</project>

8
LDN7/.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/LDN7.iml" filepath="$PROJECT_DIR$/.idea/LDN7.iml" />
</modules>
</component>
</project>

101
LDN7/client.py Normal file
View File

@ -0,0 +1,101 @@
import json
import socket
import struct
import sys
import threading
from datetime import datetime
PORT = 1234
HEADER_LENGTH = 2
def receive_fixed_length_msg(sock, msglen):
message = b''
while len(message) < msglen:
chunk = sock.recv(msglen - len(message)) # preberi nekaj bajtov
if chunk == b'':
raise RuntimeError("socket connection broken")
message = message + chunk # pripni prebrane bajte sporocilu
return message
def receive_json(sock):
# preberi glavo sporocila (v prvih 2 bytih je dolzina sporocila)
header = receive_fixed_length_msg(sock, HEADER_LENGTH)
message_length = struct.unpack("!H", header)[0] # pretvori dolzino sporocila v int
message = None
if message_length > 0: # ce je vse OK
message = receive_fixed_length_msg(sock, message_length) # preberi sporocilo
message = message.decode("utf-8")
message = json.loads(message)
return message
def send_json(sock, data):
data["time"] = datetime.now().strftime("%c")
# pretvori sporocilo v niz bajtov, uporabi UTF-8 kodno tabelo
encoded_data = json.dumps(data).encode("utf-8")
# ustvari glavo v prvih 2 bytih je dolzina sporocila (HEADER_LENGTH)
# metoda pack "!H" : !=network byte order, H=unsigned short
header = struct.pack("!H", len(encoded_data))
data = header + encoded_data # najprj posljemo dolzino sporocilo, sele nato sporocilo samo
sock.sendall(data)
def send_username(sock, username):
send_json(sock, {
"type": "username",
"data": username,
})
def send_message(sock, message):
send_json(sock, {
"type": "message",
"data": message,
})
# message_receiver funkcija tece v loceni niti
def message_receiver():
while True:
msg_received = receive_json(sock)
msg_type = msg_received["type"]
data = msg_received["data"]
time = msg_received["time"]
match msg_type:
case "message":
username = msg_received["username"]
print(f'[{time}] [{username}] : {data}')
case "error":
print(f'Error: {data}')
# povezi se na streznik
print("[system] connecting to chat server ...")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("localhost", PORT))
print("[system] connected!")
# zazeni message_receiver funkcijo v loceni niti
thread = threading.Thread(target=message_receiver)
thread.daemon = True
thread.start()
username = input("enter username: ")
send_username(sock, username)
# pocakaj da uporabnik nekaj natipka in poslji na streznik
while True:
try:
msg_send = input("")
send_message(sock, msg_send)
except KeyboardInterrupt:
sys.exit()

147
LDN7/server.py Normal file
View File

@ -0,0 +1,147 @@
import json
import signal
import socket
import struct
import threading
from datetime import datetime
signal.signal(signal.SIGINT, signal.SIG_DFL)
PORT = 1234
HEADER_LENGTH = 2
def receive_fixed_length_msg(sock, msglen):
message = b''
while len(message) < msglen:
chunk = sock.recv(msglen - len(message)) # preberi nekaj bajtov
if chunk == b'':
raise RuntimeError("socket connection broken")
message = message + chunk # pripni prebrane bajte sporocilu
return message
def receive_json(sock) -> dict:
# preberi glavo sporocila (v prvih 2 bytih je dolzina sporocila)
header = receive_fixed_length_msg(sock, HEADER_LENGTH)
message_length = struct.unpack("!H", header)[0] # pretvori dolzino sporocila v int
message = None
if message_length > 0: # ce je vse OK
message = receive_fixed_length_msg(sock, message_length) # preberi sporocilo
message = message.decode("utf-8")
message = json.loads(message)
return message
def send_json(sock, message):
message["time"] = datetime.now().strftime("%c")
encoded_message = json.dumps(message).encode("utf-8") # pretvori sporocilo v niz bajtov, uporabi UTF-8 kodno tabelo
# ustvari glavo v prvih 2 bytih je dolzina sporocila (HEADER_LENGTH)
# metoda pack "!H" : !=network byte order, H=unsigned short
header = struct.pack("!H", len(encoded_message))
message = header + encoded_message # najprj posljemo dolzino sporocilo, slee nato sporocilo samo
sock.sendall(message)
def send_error(sock, message):
send_json(sock, {
"type": "error",
"data": message,
})
# funkcija za komunikacijo z odjemalcem (tece v loceni niti za vsakega odjemalca)
def client_thread(client_sock, client_addr):
global clients
global usernames
print("[system] connected with " + client_addr[0] + ":" + str(client_addr[1]))
print("[system] we now have " + str(len(clients)) + " clients")
try:
while True: # neskoncna zanka
msg_received = receive_json(client_sock)
msg_type = str(msg_received["type"])
data = str(msg_received["data"])
time = str(msg_received["time"])
if not msg_received: # ce obstaja sporocilo
break
match msg_type:
case "username":
usernames[client_sock] = data
case "message":
username = usernames[client_sock]
if username is None:
username = client_addr[0] + ":" + str(client_addr[1])
msg_received["username"] = username
print(f'[{time}] [{username}] : {data}')
if data.startswith("@"):
[username, *data] = data.split()
username = str(username).replace("@", "")
cl = [c for c, u in usernames.items() if u == username]
if len(cl) == 0:
send_error(client_sock, "Client with this username does not exist.")
continue
client = cl[0]
msg_received["data"] = "".join(data)
send_json(client, msg_received)
continue
for client in clients:
if client == client_sock:
continue
send_json(client, msg_received)
except:
# tule bi lahko bolj elegantno reagirali, npr. na posamezne izjeme. Trenutno kar pozremo izjemo
pass
# prisli smo iz neskoncne zanke
with clients_lock:
clients.remove(client_sock)
if client_sock in usernames:
usernames.pop(client_sock)
print("[system] we now have " + str(len(clients)) + " clients")
client_sock.close()
# kreiraj socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("localhost", PORT))
server_socket.listen(1)
# cakaj na nove odjemalce
print("[system] listening ...")
clients = set()
usernames = dict()
clients_lock = threading.Lock()
while True:
try:
# pocakaj na novo povezavo - blokirajoc klic
client_sock, client_addr = server_socket.accept()
with clients_lock:
clients.add(client_sock)
thread = threading.Thread(target=client_thread, args=(client_sock, client_addr))
thread.daemon = True
thread.start()
except KeyboardInterrupt:
break
print("[system] closing server socket ...")
server_socket.close()