def test_l2_recvfrom (self):
   #addr = ''
   #frame_queue = Queue.Queue()
   #external_address_queue = Queue.Queue()
   
   # To use receive from, we go like this:
   # frame, external_address = LinkLayer.l2
   node = Node.Node(1, 'localhost', 5555)
   node.SetMTU(1500)
   node.AddLink((2, 'localhost', 1))
   node.AddLink((3, 'localhost', 1))
   client_address, client_socket = LinkLayer.InitializeSocket(node)
   #print(client_address, client_socket.getsockname())
   some_frame = LinkLayer.Frame()
   some_frame.SetPayload('This is a payload.')
   #node.PrintContents()
   #some_frame.PrintContents()
   LinkLayer.l2_sendto(client_socket, 'localhost', some_frame, node)
   
   #frame_queue.put(result)
   #external_address_queue.put(addr)
   #thread_id = thread.start_new_thread(LinkLayer.l2_recvfrom, (client_socket, node))
   #frame = frame_queue.get()
   #external_address = external_address_queue.get()
   frame, external_address = LinkLayer.l2_recvfrom(client_socket, node)
   frame.PrintContents()
   #LinkLayer.l2_sendto(client_socket, 'localhost', some_frame, node)
   client_socket.close()
   pass
 def test_l2_sendto (self):
   node = Node.Node(1, 'localhost', 5556)
   node.SetMTU(1500)
   node.AddLink((2, 'localhost', 1))
   node.AddLink((3, 'localhost', 1))
   client_address, client_socket = LinkLayer.InitializeSocket(node)
   some_frame = LinkLayer.Frame()
   some_frame.SetPayload('This is a payload.')
   LinkLayer.l2_sendto(client_socket, 'localhost', some_frame, node)
   client_socket.close()
 def test_l2_sendto (self):
   # To use l2_sendto, we go like this:
   # <will add this tomorrow, April 02, 2010>
   node = Node.Node(1, 'localhost', 5556)
   node.SetMTU(1500)
   node.AddLink((2, 'localhost', 1))
   node.AddLink((3, 'localhost', 1))
   client_address, client_socket = LinkLayer.InitializeSocket(node)
   some_frame = LinkLayer.Frame()
   some_frame.SetPayload('This is a payload.')
   LinkLayer.l2_sendto(client_socket, 'localhost', some_frame, node)
   client_socket.close()
def l3_sendto(client_socket, destination_nid, destination_port, DVRP=None, segment=None, node=None):
    """
  This function will be used in Layer 4, the Transport layer. Nowhere in this Layer 3
  is this function used--rather, this layer purely uses l2_sendto from 
  the LinkLayer module.
  """
    # next_hop = DVRP.GetRoutingTable()[destination_nid]
    mtu = node.GetMTU()
    sequence_number = 1
    total_sequence_numbers = 1
    dest_hostname = ResolveNID(destination_nid, node)
    # dest_hostname = socket.gethostbyname(dest_hostname)

    while len(segment) > mtu:
        temp_segment = segment[: mtu - 1]
        datagram = Datagram(
            sequence_number,
            total_sequence_numbers,
            mtu,
            10,
            node.GetNID(),
            node.GetPort(),
            destination_nid,
            destination_port,
            temp_segment,
        )

        what_was_sent = LinkLayer.l2_sendto(client_socket, dest_hostname, datagram, node)
        segment = segment[mtu:]
        sequence_number += 1
        total_sequence_numbers += 1

    if len(segment) > 0:
        datagram = Datagram(
            sequence_number,
            total_sequence_numbers,
            mtu,
            10,
            node.GetNID(),
            node.GetPort(),
            destination_nid,
            destination_port,
            len(segment),
            segment,
        )

        what_was_sent = LinkLayer.l2_sendto(client_socket, dest_hostname, datagram, node)

    return datagram
  def test_communications (self):
    #node = Node.Node(1, 'localhost', 5555)
    #node.SetMTU(1500)
    #node.AddLink((2, 'localhost', 1))
    #node.AddLink((3, 'localhost', 1))
    node = Node.ConfigInitialNetworkTopology(sys.argv[3], int(sys.argv[1]))
    client_address, client_socket = LinkLayer.InitializeSocket(node)
    datagram = NetworkLayer.Datagram()
    datagram.SetMTU(node.GetMTU())

    itc_script = open('itc_test.txt')
    a_list = itc_script.readlines()
    itc_script.close()

    for entry in a_list:
      temp = entry.split(' ')
      if sys.argv[1] == temp[0]:
        datagram.SetSourcePort(int(temp[2]))
      if sys.argv[2] in temp[0]:
        datagram.SetDestPort(int(temp[2]))
    # YOU MUST PUT THE \r\n IN HERE OR IT WILL NOT WORK.
    #datagram.SetPayload('This is a payload.\r\n')
    inputs = [client_socket, sys.stdin]
    #print(what_was_sent.PrintContents())
    go=1
    while(go is 1):
      #sys.stdout.write(str(node.GetNID()) + '> ')
      inputready,outputready,exceptready = select.select(inputs,[],[])
      for s in inputready:
        if s == client_socket:
          length_of_buffer, received_frame, datagram_to_pass, external_address, received_segment = LinkLayer.l2_recvfrom(client_socket, node) # added node as a parameter 04-06-2010.
          print(datagram_to_pass.GetPayload())
        #  received_segment.PrintContents()
        elif s == sys.stdin:
          payload = list(sys.stdin.readline())
          payload.remove('\n')
          payload.append('\r')
          payload.append('\n')
          payload = ''.join(payload)

          if 'exit' in payload:
            print('exiting...')
            go=0

          datagram.SetPayload(payload)
          hn = NetworkLayer.ResolveNID(int(sys.argv[2]), node)
          what_was_sent = LinkLayer.l2_sendto(client_socket, hn, datagram, node)
      #print(length_of_buffer)
      #received_frame.PrintContents()
      #if received_frame.GetLength() > 0:
      #  pass # We need to deal with Layer 3 send here.
      #datagram_to_pass.PrintContents()
      #print(external_address)
    client_socket.close()
 def test_communications (self):
   #node = Node.Node(1, 'localhost', 5555)
   node = Node.Node(1, 'localhost', 5555, [(2, 'localhost', 1), (3, 'u-03.ecc.unr.edu', 1)], 1500, 0)
   routing_table = RoutingProtocol.DVRP(node)
   # YOU NEED THE \r\n IN HERE.
   segment = 'This is a payload.\r\n'
   
   #node.SetMTU(1500)
   #node.AddLink((1, 'localhost', 1))  # A link to itself...
   #node.AddLink((2, 'localhost', 1))
   #node.AddLink((3, 'localhost', 1))
   client_address, client_socket = LinkLayer.InitializeSocket(node)
   what_was_sent = TransportLayer.l4_sendto(client_socket, 2, 5555, routing_table, segment, node)
   #print(what_was_sent)
   # Receive it.
   length_of_buffer, received_frame, datagram_to_pass, external_address, received_segment = \
   LinkLayer.l2_recvfrom(client_socket, node)
   print(received_segment)
 def test_communications (self):
   node = Node.Node(1, 'localhost', 5555)
   routing_table = RoutingProtocol.DVRP(node)
   # YOU NEED THE \r\n IN HERE.
   segment = 'This is a payload.\r\n'
   
   node.SetMTU(1500)
   node.AddLink((1, 'localhost', 1))  # A link to itself...
   node.AddLink((2, 'localhost', 1))
   node.AddLink((3, 'localhost', 1))
   client_address, client_socket = LinkLayer.InitializeSocket(node)
   what_was_sent = NetworkLayer.l3_sendto(client_socket, 2, 5555, routing_table, segment, node)
   #print(what_was_sent)
   # Receive it.
   length_of_buffer, received_frame, datagram_to_pass, external_address, received_segment = \
   LinkLayer.l2_recvfrom(client_socket, node)
   
   print(received_segment)
   received_segment.PrintContents()
Exemple #8
0
            if forwarding_ip != WAN_ip:
                dst_ip = forwarding_ip
                
            else:
                dst_ip = network.dst_ip
            if dst_ip != forwarding_ip:
                print('forwaring to', forwarding_table[network.dst_ip])
            dst_mac = util.ip2mac(dst_ip)
            linklayer.sendto(dst_mac, frame)
        elif (network.dst_ip, transport.dst_port) in NAT_in :
            network.dst_ip, transport.dst_port = NAT_in[(network.dst_ip, transport.dst_port)]
            frame = pack(network, transport)
            IP.push(frame)
        else:
            ICMP.host_unreachable(WAN_ip, network.src_ip)

linklayer = LinkLayer(callback)


# def is_reachable_host(dst_ip):
#     '''
#     Private method for check if the current dst_ip is legal
#     :param dst_ip: dst_ip
#     :return: boolean
#     '''
#     ip_list = [util.get_local_ipv4_address()]
#     for ele in NAT_in.keys():
#         ip_list.append(ele[0])
#     if dst_ip not in forwarding_table.values() or ip_list:
#         return False
#     return True