public void Send(Socketx s) { while (true) { //1. challenge var data = MakeChallenge(); Log("pppoe: send challenge request", data.ToArray(), data.Count); s.SendTo(data.ToArray(), target); byte[] buff = new byte[1024]; int recvlen = s.Receive(buff, ref target); Log("pppoe: received challenge response", buff.ToArray(), recvlen); count++; count %= 0xFF; //2. heartbeat byte[] seed = new byte[] { buff[8], buff[9], buff[10], buff[11] }; byte[] sip = new byte[] { buff[12], buff[13], buff[14], buff[15] }; if (count != 2 && count != 1) { data = MakeHeartbeat(sip, seed); } else { data = MakeHeartbeat(sip, seed, true); } Log("pppoe: send heartbeat request", data.ToArray(), data.Count); s.SendTo(data.ToArray(), target); try { recvlen = s.Receive(buff, ref target); Log("pppoe: received heartbeat response", buff.ToArray(), recvlen); break; } catch (Exception e) { Console.WriteLine("pppoe: heartbeat response failed, retry"); Console.WriteLine("pppoe: reset idx to 0x01"); count = 1; exit = onException(e); if (exit) { return; } continue; } count++; count %= 0xFF; } }
public static void KeepAlive2(Socketx s, PPPOEHeartbeat pppoe) { byte[] tail; List <byte> packet; string svr = server; int ran = random.Next(0, 0xFFFF); int svr_num = 0; byte[] data = new byte[1024]; ran += random.Next(1, 10); packet = KeepAlivePackageBuilder((byte)svr_num, ran, new byte[] { 0x00, 0x00, 0x00, 0x00 }, 1, true); int datalen = 0; while (true) { Log("[keep-alive2] send1", packet.ToArray(), packet.Count); s.SendTo(packet.ToArray(), target); datalen = s.ReceiveFrom(data, ref target); if (data[0] == 0x07 && data[2] == 0x28) { break; } else if (data[0] == 0x07 && data[2] == 0x10) { Console.WriteLine("[keep-alive2] recv file, resending..."); svr_num++; packet = KeepAlivePackageBuilder((byte)svr_num, ran, new byte[] { 0x00, 0x00, 0x00, 0x00 }, (byte)svr_num, false); } else { Log("[keep-alive2] recv1/unexpected", data.ToArray(), datalen); } } Log("[keep-alive2] recv1", data.ToArray(), datalen); ran += random.Next(1, 10); packet = KeepAlivePackageBuilder((byte)svr_num, ran, new byte[] { 0x00, 0x00, 0x00, 0x00 }, 1, false); Log("[keep-alive2] send2", packet.ToArray(), packet.Count); s.SendTo(packet.ToArray(), target); while (true) { datalen = s.ReceiveFrom(data, ref target); if (data[0] == 0x07) { svr_num++; break; } else { Log("[keep-alive2] recv2/unexpected", data.ToArray(), datalen); } } Log("[keep-alive2] recv2", data.ToArray(), datalen); tail = new byte[] { data[16], data[17], data[18], data[19] }; ran += random.Next(1, 10); packet = KeepAlivePackageBuilder((byte)svr_num, ran, tail, 3, false); Log("[keep-alive2] send3", packet.ToArray(), packet.Count); s.SendTo(packet.ToArray(), target); while (true) { datalen = s.ReceiveFrom(data, ref target); if (data[0] == 0x07) { svr_num++; break; } else { Log("[keep-alive2] recv3/unexpected", data.ToArray(), datalen); } } Log("[keep-alive2] recv3", data.ToArray(), datalen); tail = new byte[] { data[16], data[17], data[18], data[19] }; Console.WriteLine("[keep-alive2] keep-alive2 loop was in daemon."); int i = svr_num; while (true) { try { ran += random.Next(1, 10); packet = KeepAlivePackageBuilder((byte)i, ran, tail, 1, false); Log($"[keep-alive2] send{i}", packet.ToArray(), packet.Count); s.SendTo(packet.ToArray(), target); datalen = s.ReceiveFrom(data, ref target); Log($"[keep-alive2] recv{i}", data.ToArray(), datalen); tail = new byte[] { data[16], data[17], data[18], data[19] }; ran += random.Next(1, 10); packet = KeepAlivePackageBuilder((byte)(i + 1), ran, tail, 3, false); s.SendTo(packet.ToArray(), target); Log($"[keep-alive2] send{i + 1}", packet.ToArray(), packet.Count); datalen = s.ReceiveFrom(data, ref target); Log($"[keep-alive2] recv{i + 1}", data.ToArray(), datalen); tail = new byte[] { data[16], data[17], data[18], data[19] }; i = (i + 2) % 0xFF; Thread.Sleep(10 * 1000); pppoe.Send(s); } catch (Exception e) { Console.WriteLine($"[ka2] exception {e.Message}"); exit = onException(e); if (exit) { return; } } } }