Commit aa6ee266 authored by Pedro Oliveira's avatar Pedro Oliveira

change topology -> one more client connected to 2 routers

parent d618be3f
import socket
import struct
import sys
import netifaces
def chooseInterface():
interfaces = netifaces.interfaces()
def printInterfaces():
print('Indique a interface de captura:')
for i in range(len(interfaces)):
print (i+1, '-', interfaces[i])
if len(interfaces) == 1: #user has just 1 interface and any
return interfaces[0]
else:
printInterfaces()
inputValue = input('Numero da interface: ')
if int(inputValue)-1 not in range(len(interfaces)):
raise Exception('numero de interface invalida')
inputValue = interfaces[int(inputValue)-1]
return inputValue
if not hasattr(socket, 'SO_BINDTODEVICE'):
socket.SO_BINDTODEVICE = 25
multicast_group = '224.12.12.12'
server_address = ('', 10000)
# Create the socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind to the server address
sock.bind(server_address)
#interface_name = input("interface name: ")
interface_name = chooseInterface()
ip_interface = netifaces.ifaddresses(interface_name)[netifaces.AF_INET][0]['addr']
# Tell the operating system to add the socket to the multicast group
# on all interfaces.
group = socket.inet_aton(multicast_group)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP,
socket.inet_aton(multicast_group) + socket.inet_aton(ip_interface))
#sock.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, str(interface_name + "\0").encode('utf-8'))
# Receive/respond loop
while True:
#print >>sys.stderr, '\nwaiting to receive message'
data, address = sock.recvfrom(10240)
print(data.decode("utf-8"))
#print >>sys.stderr, 'received %s bytes from %s' % (len(data), address)
#print >>sys.stderr, data
#print >>sys.stderr, 'sending acknowledgement to', address
#sock.sendto('ack', address)
import socket
import struct
import sys
#import trac
import netifaces
import traceback
def chooseInterface():
interfaces = netifaces.interfaces()
def printInterfaces():
print('Indique a interface de captura:')
for i in range(len(interfaces)):
print (i+1, '-', interfaces[i])
if len(interfaces) == 1: #user has just 1 interface and any
return interfaces[0]
else:
printInterfaces()
inputValue = input('Numero da interface: ')
if int(inputValue)-1 not in range(len(interfaces)):
raise Exception('numero de interface invalida')
inputValue = interfaces[int(inputValue)-1]
return inputValue
#message = 'very important data'
multicast_group = ('224.12.12.12', 10000)
# Create the datagram socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Set a timeout so the socket does not block indefinitely when trying
# to receive data.
#sock.settimeout(0.2)
# Set the time-to-live for messages to 1 so they do not go past the
# local network segment.
ttl = struct.pack('b', 12)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
try:
# Send data to the multicast group
#print >>sys.stderr, 'sending "%s"' % message
#sent = sock.sendto(message, multicast_group)
interface_name = chooseInterface()
ip_interface = netifaces.ifaddresses(interface_name)[netifaces.AF_INET][0]['addr']
sock.bind((ip_interface, 10000))
try:
# Look for responses from all recipients
while True:
#print >>sys.stderr, 'waiting to receive'
input_msg = input('msg --> ')
try:
#message = struct.pack("s", input_msg.encode('utf-8'))
msg = bytes(input_msg, "utf-8")
sock.sendto(msg, multicast_group)
sock.sendto(input_msg.encode("utf-8"), multicast_group)
except:
traceback.print_exc()
......
ip addr add dev eth0 10.0.2.10/24
ip link set dev eth0 up
#default route para obter conectividade internet
ip route add 0.0.0.0/0 via 10.0.2.3
# install python
apt-get update && apt-get --assume-yes install python3 python3-pip
nameserver 8.8.8.8
nameserver 8.8.4.4
import socket
import struct
import sys
import netifaces
def chooseInterface():
interfaces = netifaces.interfaces()
def printInterfaces():
print('Indique a interface de captura:')
for i in range(len(interfaces)):
print (i+1, '-', interfaces[i])
if len(interfaces) == 1: #user has just 1 interface and any
return interfaces[0]
else:
printInterfaces()
inputValue = input('Numero da interface: ')
if int(inputValue)-1 not in range(len(interfaces)):
raise Exception('numero de interface invalida')
inputValue = interfaces[int(inputValue)-1]
return inputValue
if not hasattr(socket, 'SO_BINDTODEVICE'):
socket.SO_BINDTODEVICE = 25
multicast_group = '224.12.12.12'
server_address = ('', 10000)
# Create the socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind to the server address
sock.bind(server_address)
#interface_name = input("interface name: ")
interface_name = chooseInterface()
ip_interface = netifaces.ifaddresses(interface_name)[netifaces.AF_INET][0]['addr']
# Tell the operating system to add the socket to the multicast group
# on all interfaces.
group = socket.inet_aton(multicast_group)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP,
socket.inet_aton(multicast_group) + socket.inet_aton(ip_interface))
#sock.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, str(interface_name + "\0").encode('utf-8'))
# Receive/respond loop
while True:
#print >>sys.stderr, '\nwaiting to receive message'
data, address = sock.recvfrom(10240)
print(data.decode("utf-8"))
#print >>sys.stderr, 'received %s bytes from %s' % (len(data), address)
#print >>sys.stderr, data
#print >>sys.stderr, 'sending acknowledgement to', address
#sock.sendto('ack', address)
import socket
import struct
import sys
import netifaces
import traceback
message = 'very important data'
multicast_group = ('224.3.29.71', 10000)
def chooseInterface():
interfaces = netifaces.interfaces()
def printInterfaces():
print('Indique a interface de captura:')
for i in range(len(interfaces)):
print (i+1, '-', interfaces[i])
if len(interfaces) == 1: #user has just 1 interface and any
return interfaces[0]
else:
printInterfaces()
inputValue = input('Numero da interface: ')
if int(inputValue)-1 not in range(len(interfaces)):
raise Exception('numero de interface invalida')
inputValue = interfaces[int(inputValue)-1]
return inputValue
#message = 'very important data'
multicast_group = ('224.12.12.12', 10000)
# Create the datagram socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Set a timeout so the socket does not block indefinitely when trying
# to receive data.
sock.settimeout(10)
# Set the time-to-live for messages to 1 so they do not go past the
# local network segment.
ttl = struct.pack('b', 12)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
try:
# Send data to the multicast group
#print >>sys.stderr, 'sending "%s"' % message
print('sending "%s"' % message)
sent = sock.sendto(str.encode(message), multicast_group)
interface_name = chooseInterface()
ip_interface = netifaces.ifaddresses(interface_name)[netifaces.AF_INET][0]['addr']
sock.bind((ip_interface, 10000))
try:
# Look for responses from all recipients
while True:
#print >>sys.stderr, 'waiting to receive'
print('waiting to receive')
input_msg = input('msg --> ')
try:
data, server = sock.recvfrom(16)
except socket.timeout:
#print >>sys.stderr, 'timed out, no more responses'
print('timed out, no more responses')
break
else:
sock.sendto(input_msg.encode("utf-8"), multicast_group)
except:
traceback.print_exc()
continue
#print >>sys.stderr, 'received "%s" from %s' % (data, server)
print('received "%s" from %s' % (data, server))
finally:
#print >>sys.stderr, 'closing socket'
print('closing socket')
sock.close()
......@@ -8,16 +8,27 @@ router1[mem]=256
router2[0]=A
router2[1]=C
router2[2]=D
router2[2]=F
router2[mem]=256
router3[0]=B
router3[1]=C
router3[1]=D
router3[mem]=256
#########################################
# Switch setup
#########################################
switch1[0]=C
switch1[1]=D
switch1[2]=E
#########################################
# Client setup
#########################################
client1[0]=D
client1[0]=F
client1[mem]=256
client2[0]=E
client2[mem]=256
router2: router1
router2: router1 switch1
router3: router1
client1: router2
client2: switch1 router2 router3
ip link set dev eth0 up
ip link set dev eth1 up
ip link set dev eth2 up
brctl addbr br0
brctl addif br0 eth0
brctl addif br0 eth1
brctl addif br0 eth2
ip link set dev br0 up
# identify ports with routers (they need to receive all multicast traffic!)
echo "2" > /sys/devices/virtual/net/br0/brif/eth0/multicast_router
echo "2" > /sys/devices/virtual/net/br0/brif/eth1/multicast_router
nameserver 8.8.8.8
nameserver 8.8.4.4
topology.png

27.3 KB | W: | H:

topology.png

31.7 KB | W: | H:

topology.png
topology.png
topology.png
topology.png
  • 2-up
  • Swipe
  • Onion skin
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment