Exemple #1
0
def network_alt(env):
    simpynet.PointToPoint.unique_number=0
    simpynet.Switch.unique_number=0
    h0 = simpynet.Host(env , simpynet.Mac("00.00.00.00.00") , simpynet.IP("192.0.0.0"))
    h1 = simpynet.Host(env , simpynet.Mac("00.00.00.00.01") , simpynet.IP("192.0.0.1"))

    link0 = simpynet.PointToPoint(env, lambda x,y:PROPAGATION_DELAY, lambda src,trg,l: TRANSMISSION_DELAY)
    link1 = simpynet.PointToPoint(env, lambda x,y:PROPAGATION_DELAY, lambda src,trg,l: TRANSMISSION_DELAY)

    switch = simpynet.Switch( env, lambda x, y : PROCESSING_DELAY )

    simpynet.physical.plug(link0,h0)
    simpynet.physical.plug(link0,switch)
    simpynet.physical.plug(link1,switch)
    simpynet.physical.plug(link1,h1)

    env.add_collector_functions( 'received_frames', switch.collector_receivedframes , TIME_INCREMENT )
    env.add_collector_functions( 'sent_frames', switch.collector_sentframes , TIME_INCREMENT )

    protocol0 = selective_repeat.Selective_Repeat( env, 10 )
    protocol0.timeout_time = 30
    protocol1 = selective_repeat.Selective_Repeat( env, 10 )
    protocol1.timeout_time = 30
    protocol0.add_handler( h0.add_transport_handler( protocol0.get_handler() ) )
    protocol1.add_handler( h1.add_transport_handler( protocol1.get_handler() ) )
    protocol0.send( NUMBER_OF_PACKETS , simpynet.IP("192.0.0.1") )
Exemple #2
0
def network(env):
    simpynet.PointToPoint.unique_number=0
    simpynet.Switch.unique_number=0
    h0 = simpynet.Host(env , simpynet.Mac("00.00.00.00.00") , simpynet.IP("192.0.0.0"))
    h1 = simpynet.Host(env , simpynet.Mac("00.00.00.00.01") , simpynet.IP("192.0.0.1"))

    link0 = simpynet.PointToPoint(env, lambda x,y:PROPAGATION_DELAY, lambda src,trg,l: TRANSMISSION_DELAY)
    link1 = simpynet.PointToPoint(env, lambda x,y:PROPAGATION_DELAY, lambda src,trg,l: TRANSMISSION_DELAY)

    switch = simpynet.Switch( env, lambda x, y : PROCESSING_DELAY )

    tc = TimeCollector(env)
    tc.reset()
    simpynet.physical.plug(link0,h0)
    tc.add_handlers( switch , link0 )
    tc.add_handlers( switch , link1 )
    simpynet.physical.plug(link1,h1)

    env.add_collector_functions( 'data', tc.collector , float('inf')  )

    protocol0 = selective_repeat.Selective_Repeat( env, 10 )
    protocol0.timeout_time = 30
    protocol1 = selective_repeat.Selective_Repeat( env, 10 )
    protocol1.timeout_time = 30
    protocol0.add_handler( h0.add_transport_handler( protocol0.get_handler() ) )
    protocol1.add_handler( h1.add_transport_handler( protocol1.get_handler() ) )
    protocol0.send( NUMBER_OF_PACKETS , simpynet.IP("192.0.0.1") )
Exemple #3
0
    def frw(datagram, mac):
        print mac
        if mac == spn.Mac('88:B2:2F:54:1A:0F'):

            print 'forwanding'
            return spn.Mac('5C:66:AB:90:75:B1')
        else:
            print 'no fornarding'
Exemple #4
0
def example1(env):
    """
    Example at the link level.

    h1----l1----| r1 |---l2----h2

    """

    h1 = spn.Host(env, spn.Mac("00.00.00.00.00"), spn.IP("192.0.0.0"))
    h2 = spn.Host(env, spn.Mac("00.00.00.00.01"), spn.IP("192.0.0.1"))

    l1 = spn.Link(env, lambda x, y: 3, lambda src, trg, l: 1)
    l2 = spn.Link(env, lambda x, y: 3, lambda src, trg, l: 1)

    def host_handler(data):
        print data

    def forwarding(datagram, src_mac):
        if datagram.dst_ip == spn.IP("192.0.0.0"):
            return spn.Mac("00.00.00.00.00")
        else:
            return spn.Mac("00.00.00.00.01")

    r1 = spn.Router(env, spn.Mac("00.00.00.00.02"), forwarding,
                    lambda x, y: 0.1)

    spn.physical.plug(l1, r1)
    spn.physical.plug(l2, r1)
    spn.physical.plug(l1, h1)
    spn.physical.plug(l2, h2)

    send1 = h1.add_transport_handler(host_handler)
    send2 = h2.add_transport_handler(host_handler)

    def funct():
        yield env.timeout(0)
        send1("Ciao inviato da host 1", spn.IP("192.0.0.1"))

    def funct2():
        yield env.timeout(1)
        yield env.timeout(10)
        send2("Ciao inviato da host 2", spn.IP("192.0.0.0"))

    r_r = r1.collector_receiveddatagrams
    r_s = r1.collector_sentdatagrams
    r_t = r1.collector_processingtime
    h2_r = h2.collector_receiveddatagrams
    h1_s = h1.collector_sentdatagrams

    env.add_collector_functions('router received', r_r, 1)
    env.add_collector_functions('router sent', r_s, 2)
    env.add_collector_functions('router proc. time', r_t, 1)
    env.add_collector_functions('h2 received', h2_r, 1)
    env.add_collector_functions('h1 sent', h1_s, 1)

    env.process(funct())
    env.process(funct2())
Exemple #5
0
 def forwarding(datagram, router):
     if datagram.dst_ip == simpynet.IP("192.0.0.0"):
         return simpynet.Mac("00.00.00.00.00")
     if datagram.dst_ip == simpynet.IP("192.0.0.1"):
         return simpynet.Mac("00.00.00.00.01")
     if datagram.dst_ip == simpynet.IP("192.0.0.50"):
         return simpynet.Mac("00.00.00.00.02")
     if datagram.dst_ip == simpynet.IP("192.0.0.80"):
         return simpynet.Mac("00.00.00.00.03")
     else:
         return None
Exemple #6
0
    def _forwarding(self, name, data, d_trm_delay):
        """
        Simulates the forwarding process.
        The data is processed and put to the appropriate output buffer.
        """
        with self.switch_resource.request() as req:
            yield req
            temp_time = float(self.env.now)
            yield self.env.timeout(self.prcss_delay(data, repr(self)))
            src_mac = repr(data.src_mac)
            for mac in self.arp_table:
                if self.arp_table[mac] == name:
                    del self.arp_table[mac]
                    break
            self.arp_table[repr(
                src_mac
            )] = name  # si aggiunge all'arp table il riferimento mac-interface

            self.queues[name][0] -= len(data)
            if ((data.dst_mac == self.mac or data.dst_mac == simpynet.Mac())
                    and data.datagram.ttl > 0):
                datagram = data.datagram
                datagram.ttl -= 1
                dst_mac = self.forwarding(datagram, repr(self))
                data = simpynet.Frame(self.mac, dst_mac, datagram)
                if self.arp_table.has_key(repr(dst_mac)):
                    self.env.process(
                        self._deliver(self.arp_table[repr(dst_mac)], data,
                                      d_trm_delay))
                else:
                    self.env.process(self._broadcast(name, data, d_trm_delay))
                self.processing_time += float(self.env.now) - temp_time
Exemple #7
0
def sr_network(env):
    h0 = simpynet.Host(env, simpynet.Mac("00.00.00.00.00"),
                       simpynet.IP("192.0.0.0"))
    h1 = simpynet.Host(env, simpynet.Mac("00.00.00.00.01"),
                       simpynet.IP("192.0.0.1"))

    link = simpynet.PointToPoint(env, lambda x, y: PROPAGATION_DELAY,
                                 lambda src, trg, l: PROPAGATION_DELAY,
                                 lambda src, trg, l: LOSS_PROBABILITY)
    simpynet.physical.plug(link, h0)
    simpynet.physical.plug(link, h1)
    protocol0 = Selective_Repeat(env, 10)
    protocol1 = Selective_Repeat(env, 10)
    protocol0.add_handler(h0.add_transport_handler(protocol0.get_handler()))
    protocol1.add_handler(h1.add_transport_handler(protocol1.get_handler()))
    protocol0.send(NUMBER_OF_PACKETS, simpynet.IP("192.0.0.1"))
Exemple #8
0
def example(env):
    """

    dhcp_client------l1------dhcp_server

    """
    # --------------------------------------------------------

    c_host = spn.Host(env, spn.Mac('aa.aa.aa.aa.aa.aa'), spn.IP('0.0.0.0'))
    s_host = spn.Host(env, spn.Mac('bb.bb.bb.bb.bb.bb'), spn.IP('192.168.1.1'))

    client = spn.DHCP_client(env, c_host)
    server = spn.DHCP_server(env, s_host)

    def choose_ip_server(lista_ip):
        if len(lista_ip) > 0:
            return lista_ip.pop(0)

        else:
            return None

    def choose_ip_for_client():
        return '123'

    server.add_function_choose(choose_ip_for_client)

    client.add_function_choose(choose_ip_server)

    l1 = spn.Link(env, lambda x, y: 1, lambda src, trg, l: 1)

    spn.physical.plug(l1, c_host)
    spn.physical.plug(l1, s_host)

    def funct():
        yield env.timeout(1)
        client.send_DHCP_DISCOVERED()

    env.process(funct())
Exemple #9
0
    def __init__(self, environment, mac_addr, ip_addr):

        simpynet.link.nic.NIC.__init__(self, environment, mac_addr)

        self.ip_addr = ip_addr
        self.host_handler = self._receive_host
        self.transport_handler = []

        self.discarded_datagrams = 0
        self.sent_datagrams = 0
        self.received_datagrams = 0
        self.corrupted_datagrams = 0

        self.default_gateway = simpynet.Mac()
Exemple #10
0
def example2(env):
    """"
    example figure 5.17 p. 492 (Computer Networking, 6th edition, Kurose-Ross).

    c----l1-----|
                |
    b----l2--- |so|---l4---ro
                |
    a----l3-----|
    """

    env = spn.Environment()

    host_c = spn.Host(env, spn.Mac('1A:23:F9:CD:06:9B'),
                      spn.IP('222.222.222.220'))
    host_b = spn.Host(env, spn.Mac('5C:66:AB:90:75:B1'),
                      spn.IP('222.222.222.221'))
    host_a = spn.Host(env, spn.Mac('49:BD:D2:C7:56:2A'),
                      spn.IP('222.222.222.222'))

    l1 = spn.Link(env,
                  trm_delay=lambda name, data: 1,
                  prg_delay=lambda s, r, l: 1)
    l2 = spn.Link(env,
                  trm_delay=lambda name, data: 1,
                  prg_delay=lambda s, r, l: 1)
    l3 = spn.Link(env,
                  trm_delay=lambda name, data: 1,
                  prg_delay=lambda s, r, l: 1)
    l4 = spn.Link(env,
                  trm_delay=lambda name, data: 1,
                  prg_delay=lambda s, r, l: 1)

    s0 = spn.Switch(env)

    def frw(datagram, mac):
        print mac
        if mac == spn.Mac('88:B2:2F:54:1A:0F'):

            print 'forwanding'
            return spn.Mac('5C:66:AB:90:75:B1')
        else:
            print 'no fornarding'

    r0 = spn.Router(env, spn.Mac('88:B2:2F:54:1A:0F'), frw)

    spn.physical.plug(host_c, l1)

    spn.physical.plug(l1, s0)
    spn.physical.plug(host_b, l2)
    spn.physical.plug(l2, s0)
    spn.physical.plug(host_a, l3)
    spn.physical.plug(l3, s0)
    spn.physical.plug(r0, l4)
    spn.physical.plug(l4, r0)

    def tcp_handler(segment):
        print 'attivato tcp'

    def h_receiver(name, data, trm_delay):  # receiver' s handler
        def _new_handler(name, data, trm_delay):
            yield env.timeout(trm_delay)

        return env.process(_new_handler(name, data, trm_delay))

    f_send = host_c.add_transport_handler(tcp_handler)
    host_b.add_handler('rcv', h_receiver)

    def rcv_r():
        pass

    host_c.default_gateway = spn.Mac('88:B2:2F:54:1A:0F')

    def sender():
        yield env.timeout(0)
        f_send('ciao da host 1', spn.IP('222.222.222.220'))

    env.process(sender())
Exemple #11
0
 def forwarding(datagram, src_mac):
     if datagram.dst_ip == spn.IP("192.0.0.0"):
         return spn.Mac("00.00.00.00.00")
     else:
         return spn.Mac("00.00.00.00.01")
Exemple #12
0
def network(env):

    domains = {
        "moogle.com": simpynet.IP("192.0.0.50"),
        "mybank.com": simpynet.IP("192.0.0.80")
    }

    def dns_function(name):
        if domains.has_key(name):
            return domains[name]
        else:
            return None

    def sender(env):
        client.send("Hello", "moogle.com")
        yield env.timeout(5)
        client.send("Hello", "mybank.com")
        yield env.timeout(5)
        client.send("Hello", "random.com")
        client.send("Hello again", "moogle.com")

    def forwarding(datagram, router):
        if datagram.dst_ip == simpynet.IP("192.0.0.0"):
            return simpynet.Mac("00.00.00.00.00")
        if datagram.dst_ip == simpynet.IP("192.0.0.1"):
            return simpynet.Mac("00.00.00.00.01")
        if datagram.dst_ip == simpynet.IP("192.0.0.50"):
            return simpynet.Mac("00.00.00.00.02")
        if datagram.dst_ip == simpynet.IP("192.0.0.80"):
            return simpynet.Mac("00.00.00.00.03")
        else:
            return None

    router = simpynet.Router(env, simpynet.Mac("00.00.00.00.10"), forwarding,
                             lambda x, y: PROCESSING_DELAY)
    host = simpynet.Host(env, simpynet.Mac("00.00.00.00.00"),
                         simpynet.IP("192.0.0.0"))
    dns_host = simpynet.Host(env, simpynet.Mac("00.00.00.00.01"),
                             simpynet.IP("192.0.0.1"))
    server_host0 = simpynet.Host(env, simpynet.Mac("00.00.00.00.02"),
                                 simpynet.IP("192.0.0.50"))
    server_host1 = simpynet.Host(env, simpynet.Mac("00.00.00.00.03"),
                                 simpynet.IP("192.0.0.80"))

    host.set_default_gateway(simpynet.Mac("00.00.00.00.10"))
    dns_host.set_default_gateway(simpynet.Mac("00.00.00.00.10"))
    server_host0.set_default_gateway(simpynet.Mac("00.00.00.00.10"))
    server_host1.set_default_gateway(simpynet.Mac("00.00.00.00.10"))

    client = Client(env, host, dns_host.ip_addr)
    dns = DNS_Server(env, dns_host, dns_function)
    moogle = Server(env, server_host0, "moogle.com")
    mybank = Server(env, server_host1, "mybank.com")

    link0 = simpynet.PointToPoint(env, lambda x, y: PROPAGATION_DELAY,
                                  lambda src, trg, l: PROPAGATION_DELAY)
    link1 = simpynet.PointToPoint(env, lambda x, y: PROPAGATION_DELAY,
                                  lambda src, trg, l: PROPAGATION_DELAY)
    link2 = simpynet.PointToPoint(env, lambda x, y: PROPAGATION_DELAY,
                                  lambda src, trg, l: PROPAGATION_DELAY)
    link3 = simpynet.PointToPoint(env, lambda x, y: PROPAGATION_DELAY,
                                  lambda src, trg, l: PROPAGATION_DELAY)

    simpynet.physical.plug(link0, host)
    simpynet.physical.plug(link0, router)
    simpynet.physical.plug(link1, dns_host)
    simpynet.physical.plug(link1, router)
    simpynet.physical.plug(link2, server_host0)
    simpynet.physical.plug(link2, router)
    simpynet.physical.plug(link3, server_host1)
    simpynet.physical.plug(link3, router)

    env.process(sender(env))