/// protocol list: /// PING /// ARP /// etc... /* * //ARP * public void requestARP(Packet arpReq){ * Debug.Log(id + ": Creating an ARP request!"); * if(ports[0].isConnected()){ * ports[0].send(arpReq); * } * } * private void replyARP(Packet arpRep){ * Debug.Log("Replying to ARP!"); * * if (ports[0].isConnected ()) { * ports[0].send (arpRep); * } * } * */ /// <summary> /// handles the data link layer, /// assigning mac addresses and /// forwarding through port /// </summary> /// /// <param name="packet"></param> /// /// <returns> true or false depending on success or failure of packet sending</returns> public bool sendPacket(Packet packet) { //check if the port is connected if (ports[0].isConnected()) { Debug.Log(id + ": port is connected!"); packet.netAccess.setMAC(MAC, "src"); //check if the network mask is valid if (GetComponent <Subnet>().validMask) { Debug.Log(id + ": Valid Network mask"); //check if dest network is local if (GetComponent <Subnet>().CheckNetwork(packet.internet.getIP("dest"))) { Debug.Log(id + ": destination is on local network!"); if (ports[0].isListed(packet.internet.getIP("dest"))) { Debug.Log(id + ": MAC ADDRESS IS LISTED"); packet.netAccess.setMAC(ports[0].getDestMAC(packet), "dest"); //set destination mac address return(ports[0].send(packet)); } else { Debug.LogAssertion(id + ": ARP TABLE OUT OF DATE, REQUESTING>>>"); //send ARP Request! ports[0].send(arp.Request(IP, packet.internet.getIP("dest"))); return(false); } } else if (packet.GetComponent <DHCP>()) { //TODO //if the packet has a dhcp component, forward return(ports[0].send(packet)); } else if (subnet.defaultGateway != "") { Debug.LogAssertion(id + ": Not on same subnet!forwarding to default gateway"); packet.internet.setIP(subnet.defaultGateway, "dest"); return(sendPacket(packet)); } else { Debug.LogAssertion("ERROR FOUND IN PC !!! no gateway assigned"); return(false); } } else if (packet.GetComponent <DHCP>()) { //if the packet contains a dhcp component, forward it on return(ports[0].send(packet)); } else { Debug.LogAssertion(id + ": Not a valid Network!"); return(false); } } else { Debug.LogAssertion(id + ": NOT Connected"); return(false); } }
/********************************** * 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()); * } * }*/ }