Exemplo n.º 1
0
    public void handlePacket(Packet packet)
    {
        Debug.LogAssertion(id + ": Packet Received! -> " + packet.type);

        //if it is a ping
        if (packet.type.Equals("PING"))
        {
            //if the packet is an ECHO ICMP
            if (packet.GetComponent <ICMP>().type.Equals("ECHO") && packet.GetComponent <ICMP>().ip.Equals(IP))
            {
                if (!sendPacket(ping.Reply(packet.internet.getIP("src"))))
                {
                    //if packet doesn't send...
                    Debug.LogAssertion(id + ": PACKET FAILED TO SEND");
                }
            }
            //if the packet is a REPLY ICMP
            else if (packet.GetComponent <ICMP>().type.Equals("REPLY"))
            {
                //notify the Ping Application of successful reply
                ping.success++;
            }
        }
        //if it is an arp packet
        if (packet.type.Equals("ARP"))
        {
            //if its a request ARP
            if (packet.GetComponent <ARP>().type.Equals("REQUEST"))
            {
                //if the request is addressed to me
                if (packet.internet.getIP("dest").Equals(IP))
                {
                    //check if i have that computer in my arp, if not, update and reply, else just reply
                    if (!ports[0].isListed(packet.internet.getIP("src")))
                    {
                        ports[0].updateARPTable(packet.internet.getIP("src"), packet.netAccess.getMAC("src"));
                    }
                    ports[0].send(arp.Reply(IP, packet.internet.getIP("src"), packet.netAccess.getMAC("src")));
                }
            }
            //else if its a REPLY ARP
            else if (packet.GetComponent <ARP>().type.Equals("REPLY"))
            {
                //update table
                Debug.Log(id + ": Processing ARP reply..");
                ports[0].updateARPTable(packet.internet.getIP("src"), packet.netAccess.getMAC("src"));
            }
            else
            {
                //just drop the packet
                Debug.Log(id + ": dropping ARP request , not my ip!");
            }
        }

        if (packet.type.Contains("DHCP"))
        {
            //if this device is a DHCP server/client..
            if (GetComponent <DHCPServer>())
            {
                GetComponent <DHCPServer>().handle(packet);
            }
            else if (GetComponent <DHCPClient>())
            {
                GetComponent <DHCPClient>().handle(packet);
            }
        }
    }
Exemplo n.º 2
0
    /**********************************
     * Receives and Handles incoming packets
     *
     */
    public void handlePacket(Packet packet, Port incomingPort)
    {
        Debug.Log("ROUTER: RECEIVED PACKET");

        /*
         * //add incoming device's MAC to port
         * if(!incomingPort.isListed(packet.internet.getIP("src")))
         * {
         *  incomingPort.updateARPTable(packet.internet.getIP("src"), packet.netAccess.getMAC("src"));
         * }*/

        //for dhcp
        if (packet.type.Equals("DHCP"))
        {
            for (int i = 0; i < servers.Length; i++)
            {
                if (servers[i].port.Equals(incomingPort.GetComponent <Port>()))
                {
                    servers[i].handle(packet);
                }
            }
        }

        //for ping
        if (packet.type.Equals("PING"))
        {
            //get destination ip    for ICMP currently
            string destIp = packet.GetComponent <ICMP>().ip;
            //get network of dest ip
            string destNetwork = incomingPort.GetComponent <Subnet>().GetNetworkFromIP(destIp);
            //fetches outgoing port based on route returned from routing table
            Port outPort = GetRoutePort(destNetwork);
            //if the port does not have the mac address, request it then send it after
            if (!outPort.isListed(destIp))
            {
                requestARP(arp.Request(outPort.GetComponent <Subnet>().defaultGateway, destIp), outPort);
            }
            if (outPort != null)
            {
                //set the new destination ip
                packet.internet.setIP(destIp, "dest");
                outPort.send(packet);
            }
            else
            {
                Debug.LogAssertion(id + ": Dropping Ping, no routes found!");
            }
        }

        //for arp
        //if incoming packet is an ARP request addressed to this IP , return a reply
        if (packet.type.Equals("ARP") && packet.internet.getIP("dest").Equals(incomingPort.GetComponent <Subnet>().defaultGateway))
        {
            //if this is a reply to this ip, add it to the incoming port's list
            if (packet.GetComponent <ARP>().type.Equals("REPLY"))
            {
                incomingPort.updateARPTable(packet.internet.getIP("src"), packet.netAccess.getMAC("src"));
            }
            else if (!replyARP(arp.Reply(incomingPort.GetComponent <Subnet>().defaultGateway, packet.internet.getIP("src"), packet.netAccess.getMAC("src")), incomingPort))
            {
                if (!incomingPort.isConnected())
                {
                    Debug.Log(id + ": Port is not connected, cant reply to ARP");
                }
                else
                {
                    requestARP(arp.Request(incomingPort.GetComponent <Subnet>().defaultGateway, packet.internet.getIP("src")), incomingPort);
                }
            }
        }

        /*
         * for(int i = 0; i < routingTable.Count; i++)
         * {
         *
         *  if(packet.type.Equals("ARP"))
         *  {
         *      Debug.Log("ROUTER: Dropping ARP request");
         *      break;
         *  }
         *  //if dest IP is on routing table, forward to the switch
         *  //TODO foreign networks , different ip ranges ,
         *  //this should only be for local area network.
         *
         *  else if (packet.internet.getIP("dest").Equals(routingTable[i]))
         *  {
         *      push(packet, incomingPort.getType());
         *  }
         * }*/
    }