private void arpListener() { byte[] pkt_data = null; packet_headers pkt_hdr = null; isListeningArp = true; do { if (pcaparp.pcapnet_next_ex(out pkt_hdr, out pkt_data) == 0) { continue; } byte[] array = new byte[6]; Array.Copy(pkt_data, 6, array, 0, 6); if (tools.areValuesEqual(array, localMAC)) { continue; } byte b = pkt_data[21]; if (b.ToString().CompareTo("2") == 0) { byte[] array2 = new byte[4]; byte[] array3 = new byte[6]; Array.Copy(pkt_data, 22, array3, 0, 6); Array.Copy(pkt_data, 28, array2, 0, 4); PC pC = new PC(); pC.ip = new IPAddress(array2); pC.mac = new PhysicalAddress(array3); pC.capDown = 0; pC.capUp = 0; pC.isLocalPc = false; pC.name = ""; pC.nbPacketReceivedSinceLastReset = 0; pC.nbPacketSentSinceLastReset = 0; pC.redirect = true; DateTime now = DateTime.Now; pC.timeSinceLastRarp = now; pC.totalPacketReceived = 0; pC.totalPacketSent = 0; if (tools.areValuesEqual(array2, routerIP)) { routerMAC = array; pC.isGateway = true; } else { pC.isGateway = false; } pcList.addPcToList(pC); } }while (isListeningArp); arpListenerThreadTerminated.Set(); }
private async void arpListener() { byte[] numArray = null; packet_headers packetHeader = null; isListeningArp = true; do { if (pcaparp.pcapnet_next_ex(out packetHeader, out numArray) == 0) { continue; } byte[] numArray1 = new byte[6]; Array.Copy(numArray, 6, numArray1, 0, 6); if (tools.areValuesEqual(numArray1, localMAC) || numArray[21].ToString().CompareTo("2") != 0) { continue; } byte[] numArray2 = new byte[4]; byte[] numArray3 = new byte[6]; Array.Copy(numArray, 22, numArray3, 0, 6); Array.Copy(numArray, 28, numArray2, 0, 4); PC pC = new PC() { ip = new IPAddress(numArray2), mac = new PhysicalAddress(numArray3), CapDown = 0, CapUp = 0, isLocalPc = false, name = "", nbPacketReceivedSinceLastReset = 0, nbPacketSentSinceLastReset = 0, redirect = true, timeSinceLastRarp = DateTime.Now, totalPacketReceived = 0, totalPacketSent = 0 }; if (!tools.areValuesEqual(numArray2, routerIP)) { pC.isGateway = false; } else { routerMAC = numArray1; pC.isGateway = true; } pcList.addPcToList(pC); await Task.Delay(20); }while (isListeningArp); //arpListenerThreadTerminated.Set(); }
private void redirector() { byte[] pkt_data = null; packet_headers pkt_hdr = null; isRedirecting = true; byte[] array = new byte[6]; byte[] array2 = new byte[4]; byte[] array3 = new byte[4]; PC router = pcList.getRouter(); if (router != null) { routerMAC = router.mac.GetAddressBytes(); } if (routerMAC == null) { MessageBox.Show("no router found to redirect packet"); isRedirecting = false; return; } if (isRedirecting) { do { if (pcapredirect.pcapnet_next_ex(out pkt_hdr, out pkt_data) == 0) { continue; } Array.Copy(pkt_data, 6, array, 0, 6); if (tools.areValuesEqual(array, localMAC)) { Array.Copy(pkt_data, 26, array2, 0, 4); if (tools.areValuesEqual(array2, localIP)) { pcList.getLocalPC().nbPacketSentSinceLastReset += (int)pkt_hdr.caplen; } continue; } if (tools.areValuesEqual(array, routerMAC)) { Array.Copy(pkt_data, 30, array3, 0, 4); if (tools.areValuesEqual(array3, localIP)) { pcList.getLocalPC().nbPacketReceivedSinceLastReset += (int)pkt_hdr.caplen; continue; } PC pCFromIP = pcList.getPCFromIP(array3); if (pCFromIP != null) { int capDown = pCFromIP.capDown; if ((capDown == 0 || capDown > pCFromIP.nbPacketReceivedSinceLastReset) && pCFromIP.redirect) { Array.Copy(pCFromIP.mac.GetAddressBytes(), 0, pkt_data, 0, 6); Array.Copy(localMAC, 0, pkt_data, 6, 6); pcapredirect.pcapnet_sendpacket(pkt_data); pCFromIP.nbPacketReceivedSinceLastReset += (int)pkt_hdr.caplen; } } continue; } Array.Copy(pkt_data, 30, array3, 0, 4); if (tools.areValuesEqual(array3, localIP)) { continue; } PC pCFromMac = pcList.getPCFromMac(array); if (pCFromMac != null) { int capUp = pCFromMac.capUp; if ((capUp == 0 || capUp > pCFromMac.nbPacketSentSinceLastReset) && pCFromMac.redirect) { Array.Copy(routerMAC, 0, pkt_data, 0, 6); Array.Copy(localMAC, 0, pkt_data, 6, 6); pcapredirect.pcapnet_sendpacket(pkt_data); pCFromMac.nbPacketSentSinceLastReset += (int)pkt_hdr.caplen; } } }while (isRedirecting); } redirectorThreadTerminated.Set(); }
private void redirector() { byte[] numArray = null; packet_headers packetHeader = null; isRedirecting = true; byte[] numArray1 = new byte[6]; byte[] numArray2 = new byte[4]; byte[] numArray3 = new byte[4]; PC router = pcList.getRouter(); if (router != null) { routerMAC = router.mac.GetAddressBytes(); } if (routerMAC != null) { if (isRedirecting) { do { if (pcapredirect.pcapnet_next_ex(out packetHeader, out numArray) == 0) { continue; } Array.Copy(numArray, 6, numArray1, 0, 6); if (tools.areValuesEqual(numArray1, localMAC)) { Array.Copy(numArray, 26, numArray2, 0, 4); if (!tools.areValuesEqual(numArray2, localIP)) { continue; } pcList.getLocalPC().nbPacketSentSinceLastReset += (int)packetHeader.caplen; } else if (!tools.areValuesEqual(numArray1, routerMAC)) { Array.Copy(numArray, 30, numArray3, 0, 4); if (tools.areValuesEqual(numArray3, localIP)) { continue; } PC pCFromMac = pcList.getPCFromMac(numArray1); if (pCFromMac == null) { continue; } int num = pCFromMac.CapUp; if (num != 0 && num <= pCFromMac.nbPacketSentSinceLastReset || !pCFromMac.redirect) { continue; } Array.Copy(routerMAC, 0, numArray, 0, 6); Array.Copy(localMAC, 0, numArray, 6, 6); pcapredirect.pcapnet_sendpacket(numArray); pCFromMac.nbPacketSentSinceLastReset += (int)packetHeader.caplen; } else { Array.Copy(numArray, 30, numArray3, 0, 4); if (!tools.areValuesEqual(numArray3, localIP)) { PC pCFromIP = pcList.getPCFromIP(numArray3); if (pCFromIP == null) { continue; } int num1 = pCFromIP.CapDown; if (num1 != 0 && num1 <= pCFromIP.nbPacketReceivedSinceLastReset || !pCFromIP.redirect) { continue; } Array.Copy(pCFromIP.mac.GetAddressBytes(), 0, numArray, 0, 6); Array.Copy(localMAC, 0, numArray, 6, 6); pcapredirect.pcapnet_sendpacket(numArray); pCFromIP.nbPacketReceivedSinceLastReset += (int)packetHeader.caplen; } else { pcList.getLocalPC().nbPacketReceivedSinceLastReset += (int)packetHeader.caplen; } } }while (isRedirecting); } //redirectorThreadTerminated.Set(); } else { MessageBox.Show("no router found to redirect packet"); isRedirecting = false; } }