void pppReset() { pppStage = PPPStage.PPPS_None; pppLCP = new pppPocket(); pppLCP.Protocol = 0xc021; pppLCP.ID = 1; pppLCP.Code = 0x01; pppLCP.AddCommand(0x02, 0x00, 0x00, 0x00, 0x00); Random x = new Random(); Union_UInt32 x32 = new Union_UInt32(); x32.ofs_32 = (uint)x.Next(); pppLCP.AddCommand(0x05, x32.ofs_h.ofs_h, x32.ofs_h.ofs_l, x32.ofs_l.ofs_h, x32.ofs_l.ofs_l); //pppLCP.AddCommand(0x07); //pppLCP.AddCommand(0x08); pppPAP = new pppPocket(); pppPAP.Protocol = 0xc023; pppPAP.Code = 0x01; pppPAP.ID = 1; byte[] Login = new byte[10] { 0x04, 0x6E, 0x6F, 0x6E, 0x65, 0x04, 0x6E, 0x6F, 0x6E, 0x65 }; pppPAP.Datas.AddRange(Login); pppIPCP = new pppPocket(); pppIPCP.Protocol = 0x8021; pppIPCP.Code = 0x01; pppIPCP.ID = 1; pppIPCP.AddCommand(0x03, 0x00, 0x00, 0x00, 0x00); //dIP = new byte[4]; ourPort = (ushort)x.Next(4097, 4106); TCPSeqNum = (uint)x.Next(1000, 5000); IPID = 0;//(ushort)x.Next(100, 500); pppIPCP.AddCommand(0x81, 0x00, 0x00, 0x00, 0x00); pppIPCP.AddCommand(0x83, 0x00, 0x00, 0x00, 0x00); DNS1 = new byte[4]; DNS2 = new byte[4]; theirIP = new byte[4]; pppAcceptedTheirLCPOptions = false; pppAcceptedOurLCPOptions = false; pppAcceptedIPOptions = false; pppAcceptedIPOptions1 = false; pppLogin = false; bLCP = false; bDataTransfer = false; bPPP = false; }
public GsmModem(SerialPort _Com) { AutoRxList = new List <byte>(); Com = _Com; Com.ReadTimeout = 10000; Com.RtsEnable = true; Com.ReceivedBytesThreshold = 1; //Com.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(COM_DataReceived); pppLCP = new pppPocket(); pppLCP.Protocol = 0xc021; pppLCP.Code = 0x01; pppLCP.AddCommand(0x02, 0x00, 0x00, 0x00, 0x00); Random x = new Random(); Union_UInt32 x32 = new Union_UInt32(); x32.ofs_32 = (uint)x.Next(); pppLCP.AddCommand(0x05, x32.ofs_h.ofs_h, x32.ofs_h.ofs_l, x32.ofs_l.ofs_h, x32.ofs_l.ofs_l); //pppLCP.AddCommand(0x07); //pppLCP.AddCommand(0x08); pppPAP = new pppPocket(); pppPAP.Protocol = 0xc023; pppPAP.Code = 0x01; byte[] Login = new byte[10] { 0x04, 0x6E, 0x6F, 0x6E, 0x65, 0x04, 0x6E, 0x6F, 0x6E, 0x65 }; pppPAP.Datas.AddRange(Login); pppIPCP = new pppPocket(); pppIPCP.Protocol = 0x8021; pppIPCP.Code = 0x01; pppIPCP.AddCommand(0x03, 0x00, 0x00, 0x00, 0x00); dIP = new byte[4]; ourPort = (ushort)x.Next(4097, 4106); TCPSeqNum = (uint)x.Next(1000, 5000); IPID = 0;//(ushort)x.Next(100, 500); pppIPCP.AddCommand(0x81, 0x00, 0x00, 0x00, 0x00); pppIPCP.AddCommand(0x83, 0x00, 0x00, 0x00, 0x00); DNS1 = new byte[4]; DNS2 = new byte[4]; theirIP = new byte[4]; }
bool LCP(pppPocket pppUp) { pppStage = PPPStage.PPPS_LCP; if (pppUp == null) { return(false); } pppPocket pppDown; switch (pppUp.Code) { case 5: //Terminate-Request pppUp.Code = 0x06; pppSender(pppUp); pppReset(); return(true); case 6: //Terminate-Ack pppReset(); return(true); case 8: //Protocol-Reject pppEnd(); pppReset(); return(true); case 7: //Code-Reject pppEnd(); pppReset(); return(true); case 9: //Echo-Request { Union_UInt32 x32 = new Union_UInt32(); x32.ofs_h.ofs_h = pppUp.Datas[0]; x32.ofs_h.ofs_l = pppUp.Datas[1]; x32.ofs_l.ofs_h = pppUp.Datas[2]; x32.ofs_l.ofs_l = pppUp.Datas[3]; if (x32.ofs_32 != 0) { byte[] mn = (byte[])(pppLCP.Commands[(byte)0x05]); if (pppUp.Datas[0] != mn[0] || pppUp.Datas[1] != mn[1] || pppUp.Datas[2] != mn[2] || pppUp.Datas[3] != mn[3]) { if (pppAcceptedOurLCPOptions) { pppUp.Datas[0] = mn[0]; pppUp.Datas[1] = mn[1]; pppUp.Datas[2] = mn[2]; pppUp.Datas[3] = mn[3]; } else { pppUp.Datas[0] = 0; pppUp.Datas[1] = 0; pppUp.Datas[2] = 0; pppUp.Datas[3] = 0; } pppUp.Code = 0x10; pppSender(pppUp); return(true); } else { pppReset(); return(true); } } else { return(true); } } case 10: //Echo-Reply { Union_UInt32 x32 = new Union_UInt32(); x32.ofs_h.ofs_h = pppUp.Datas[0]; x32.ofs_h.ofs_l = pppUp.Datas[1]; x32.ofs_l.ofs_h = pppUp.Datas[2]; x32.ofs_l.ofs_l = pppUp.Datas[3]; if (x32.ofs_32 != 0) { byte[] mn = (byte[])(pppLCP.Commands[(byte)0x05]); if (pppUp.Datas[0] != mn[0] || pppUp.Datas[1] != mn[1] || pppUp.Datas[2] != mn[2] || pppUp.Datas[3] != mn[3]) { //是否应该有一些操作? return(true); } else { pppReset(); return(true); } } else { return(true); } } } if (pppStage == PPPStage.PPPS_None || pppStage == PPPStage.PPPS_Start || pppStage == PPPStage.PPPS_Disconnect) { return(true); } switch (pppUp.Code) { case 0x01: // Configure-Request(匹配请求) //首先找到我们不同意的请求,并给出建议值 bool bSend = false; pppDown = new pppPocket(); pppDown.Protocol = pppUp.Protocol; pppDown.Code = 0x03; pppDown.ID = pppUp.ID; foreach (DictionaryEntry comm in pppUp.Commands) { switch ((byte)comm.Key) { case 5: //Magic-Number { Union_UInt32 x32 = new Union_UInt32(); byte[] mn = (byte[])(pppUp.Commands[(byte)0x05]); x32.ofs_h.ofs_h = mn[0]; x32.ofs_h.ofs_l = mn[1]; x32.ofs_l.ofs_h = mn[2]; x32.ofs_l.ofs_l = mn[3]; if (x32.ofs_32 != 0) { byte[] mn1 = (byte[])(pppLCP.Commands[(byte)0x05]); if (mn[0] != mn1[0] || mn[1] != mn1[1] || mn[2] != mn1[2] || mn[3] != mn1[3]) { break; } else { Random x = new Random(); Union_UInt32 x321 = new Union_UInt32(); x32.ofs_32 = (uint)x.Next(); while (x321.ofs_32 == x32.ofs_32) { x32.ofs_32 = (uint)x.Next(); } pppDown.AddCommand(0x05, x321.ofs_h.ofs_h, x321.ofs_h.ofs_l, x321.ofs_l.ofs_h, x321.ofs_l.ofs_l); bSend = true; break; } } else { break; } } case 1: //Maximum-Receive-Unit(最大-接收-单元) byte[] mru = (byte[])(pppUp.Commands[(byte)0x01]); ushort w = BytesOP.MakeShort(mru[0], mru[1]); if (w < 128) { w = 128; } else if (w > 1500) { w = 1500; } else { break; } pppDown.AddCommand(0x01, BytesOP.GetHighByte(w), BytesOP.GetLowByte(w)); bSend = true; break; case 3: //Authentication-Protocol(鉴定-协议) byte[] ap = (byte[])(pppUp.Commands[(byte)0x03]); ushort AP = BytesOP.MakeShort(ap[0], ap[1]); if (AP == 0xc023) { break; } pppDown.AddCommand(0x03, 0xc0, 0x23); bSend = true; break; } } if (bSend) { pppSender(pppDown); return(true); } //去掉我们所不能接受的配置 pppDown.Code = 0x04; foreach (DictionaryEntry comm in pppUp.Commands) { switch ((byte)comm.Key) { case 5: //Magic-Number break; case 1: //Maximum-Receive-Unit(最大-接收-单元) break; case 2: //Async-Control-Character-Map(异步-控制-字符-映射) break; case 3: //Authentication-Protocol(鉴定-协议) break; default: bSend = true; byte[] bs = (byte[])(pppUp.Commands[(byte)comm.Key]); pppDown.AddCommand((byte)comm.Key, bs); break; } } if (bSend) { pppSender(pppDown); pppLCP.ID++; return(true); } //到此接受所有的服务器配置 pppAcceptedTheirLCPOptions = true; pppUp.Code = 0x02; pppSender(pppUp); return(true); case 2: // Configure-Ack pppAcceptedOurLCPOptions = true; return(true); case 3: //Configure-Nak foreach (DictionaryEntry comm in pppUp.Commands) { switch ((byte)comm.Key) { case 1: //Maximum-Receive-Unit(最大-接收-单元) pppLCP.RemoveCommand(0x01); byte[] mru = (byte[])(pppUp.Commands[(byte)0x01]); pppLCP.AddCommand(0x01, mru[0], mru[1]); break; case 5: //Magic-Number pppLCP.RemoveCommand(0x05); byte[] mn = (byte[])(pppUp.Commands[(byte)0x05]); pppLCP.AddCommand(0x01, mn[0], mn[1], mn[2], mn[3]); break; case 2: //Async-Control-Character-Map(异步-控制-字符-映射) pppLCP.RemoveCommand(0x02); byte[] accm = (byte[])(pppUp.Commands[(byte)0x02]); pppLCP.AddCommand(0x01, accm[0], accm[1], accm[2], accm[3]); break; } } pppAcceptedOurLCPOptions = false; return(true); case 4: //Configure-Reject foreach (DictionaryEntry comm in pppUp.Commands) { switch ((byte)comm.Key) { case 1: //Maximum-Receive-Unit(最大-接收-单元) pppLCP.RemoveCommand(0x01); break; case 5: //Magic-Number pppLCP.RemoveCommand(0x05); break; case 2: //Async-Control-Character-Map(异步-控制-字符-映射) pppLCP.RemoveCommand(0x02); break; } } pppAcceptedOurLCPOptions = false; return(true); default: pppCodeReject(pppUp); return(true); } }
bool IPCP(pppPocket pppUp) { if (pppUp == null) { return(false); } if (pppStage != PPPStage.PPPS_IPCP) { pppUnkown(pppUp); return(true); } pppPocket pppDown; bool bSend = false; Union_UInt32 x32; switch (pppUp.Code) { case 1: //配置请求 pppDown = new pppPocket(); pppDown.Protocol = 0x8021; pppDown.Code = 0x04; pppDown.ID = pppUp.ID; foreach (DictionaryEntry comm in pppUp.Commands) { byte type = (byte)comm.Key; if ((type != 3) && (type != 129) && (type != 131)) { byte[] bs = (byte[])(pppUp.Commands[type]); pppDown.AddCommand(type, bs); bSend = true; } } if (bSend) { pppSender(pppDown); return(true); } /************************************************************************/ /* 是否需要记录对方IP? */ /************************************************************************/ foreach (DictionaryEntry comm in pppUp.Commands) { byte type = (byte)comm.Key; byte[] ip = (byte[])(pppUp.Commands[type]); switch (type) { case 3: for (int i = 0; i < 4; i++) { theirIP[i] = ip[i]; } break; case 129: for (int i = 0; i < 4; i++) { DNS1[i] = ip[i]; } break; case 131: for (int i = 0; i < 4; i++) { DNS2[i] = ip[i]; } break; } } pppUp.Code = 0x02; if (pppAcceptedIPOptions) { pppSender(pppUp); pppAcceptedIPOptions1 = true; } return(true); case 4: //拒绝请求 pppIPCP.ID++; foreach (DictionaryEntry comm in pppUp.Commands) { byte type = (byte)comm.Key; byte[] ip = (byte[])(pppUp.Commands[type]); x32 = new Union_UInt32(); x32.ofs_h.ofs_h = ip[0]; x32.ofs_h.ofs_l = ip[1]; x32.ofs_l.ofs_h = ip[2]; x32.ofs_l.ofs_l = ip[3]; switch (type) { case 3: if (x32.ofs_32 != 0 && x32.ofs_32 != 0xffffffff) { pppIPCP.RemoveCommand(0x03); pppIPCP.AddCommand(0x03, 0x00, 0x00, 0x00, 0x00); } else { pppEnd(); pppReset(); } break; case 0x81: if (x32.ofs_32 != 0 && x32.ofs_32 != 0xffffffff) { pppIPCP.RemoveCommand(0x81); pppIPCP.AddCommand(0x81, 0x00, 0x00, 0x00, 0x00); } else { pppIPCP.RemoveCommand(0x81); } break; case 0x83: if (x32.ofs_32 != 0 && x32.ofs_32 != 0xffffffff) { pppIPCP.RemoveCommand(0x83); pppIPCP.AddCommand(0x83, 0x00, 0x00, 0x00, 0x00); } else { pppIPCP.RemoveCommand(0x83); } break; } } return(true); case 3: //服务给分配ip pppIPCP.ID++; foreach (DictionaryEntry comm in pppUp.Commands) { byte type = (byte)comm.Key; byte[] ip = (byte[])(pppUp.Commands[type]); switch (type) { case 3: ourIP = new byte[4]; for (int i = 0; i < 4; i++) { ourIP[i] = ip[i]; } break; case 129: for (int i = 0; i < 4; i++) { DNS1[i] = ip[i]; } break; case 131: for (int i = 0; i < 4; i++) { DNS2[i] = ip[i]; } break; } if (pppIPCP.Commands.ContainsKey(type)) { pppIPCP.RemoveCommand(type); pppIPCP.AddCommand(type, ip); } } return(true); case 2: //服务器同意当前IP配置 foreach (DictionaryEntry comm in pppUp.Commands) { byte type = (byte)comm.Key; byte[] ip = (byte[])(pppUp.Commands[type]); switch (type) { case 3: for (int i = 0; i < 4; i++) { ourIP[i] = ip[i]; } break; case 129: for (int i = 0; i < 4; i++) { DNS1[i] = ip[i]; } break; case 131: for (int i = 0; i < 4; i++) { DNS2[i] = ip[i]; } break; } } pppAcceptedIPOptions = true; return(true); default: pppCodeReject(pppUp); return(true); } }