⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ppp.c

📁 ppp拨号程序源代码采用atmel芯片c语言编
💻 C
📖 第 1 页 / 共 4 页
字号:
			goto ppp_end1;									//
		}													//

		// *******************************************************
		// protocol reject

		if (CodeHeader->Code == PPP_PREJ)					//
		{													//
			w = ntohs(*(u16*)&MainBuffer[MainBufferRd_Rx]);	// Rejected Protocol
															//
			#ifdef Debug
				sprintf((char*)ScratchPad, "     they rejected protocol:%04X\n", w);
				SendDebugStr((char*)ScratchPad);
			#endif

			// this prolly means things have gone wrong as we are only using the most basic of protocols

			PPP_End();										// terminate the link
  			goto ppp_end1;									//
		}

		// *******************************************************
		// Code reject

		if (CodeHeader->Code == PPP_CodeREJ)				//
		{													//
			w = (u16)MainBuffer[MainBufferRd_Rx++];			// Rejected code

			#ifdef Debug
				sprintf((char*)ScratchPad, "     they rejected code:%u\n", w);
				SendDebugStr((char*)ScratchPad);
			#endif

			// this will prolly mean things have gone wrong

			PPP_End();										// terminate the link
  			goto ppp_end1;									//
		}

		// *******************************************************
		// Echo request - we MUST send back an echo reply - it's the rules!

		if (CodeHeader->Code == PPP_EREQ)								//
		{																//
			dw = ntohl(*(u32*)(MainBuffer + MainBufferRd_Rx));		// magic number - MS-Byte 1st

			if ((dw) && (dw == PPP.TxMN))
			{	// seems we are in loopback mode - well, more than likely !
				#ifdef Debug
					SendDebugRStr(LoopBackStr);
				#endif
				Terminate = true;
				goto ppp_end1;
			}

			dw = 0;
			if (PPP.AcceptedOurLCPOptions) dw = PPP.TxMN;			// replace the Magic number with ours
			*(u32*)(MainBuffer + MainBufferRd_Rx) = htonl(dw);		// magic number
																	//
			CodeHeader->Code = PPP_ERPY;							// change the packet into a reply packet
			MainBufferWr_Tx = MainBufferWr_Rx;						// number of bytes to send back

  			goto ppp_end1;											// send it back
		}

		if (CodeHeader->Code == PPP_ERPY)
		{
			dw = ntohl(*(u32*)(MainBuffer + MainBufferRd_Rx));		// magic number - MS-Byte 1st

			if ((dw) && (dw == PPP.TxMN))
			{	// seems we are in loopback mode
				#ifdef Debug
					SendDebugRStr(LoopBackStr);
				#endif
				Terminate = true;
				goto ppp_end1;
			}

			if (dw == PPP.RxMN)
			{	// it's for us
				u32_Put(&PPP.LCP_Echo_Timer, 0);			// reset echo timer
				PPP.Retries = 0;							// reset the send retry counter
				u16_Put(&PPP.SendTick, PPP_Timeout);		// send next packet asap
			}

			goto ppp_end1;
		}

		// *******************************************************
		// don't go any further if we are not sorting/sorted things out or are disconnecting

		if ((PPP.Stage == PPPS_None) || (PPP.Stage == PPPS_Start) || (PPP.Stage == PPPS_Disc)) goto ppp_end1;

		// *******************************************************
		// They are requesting LCP options

		if (CodeHeader->Code == PPP_CREQ)
		{
			// *************************************
			// first do a NAK test - to see if we could accept any of the options but with different values

			i = MainBufferRd_Rx;
			while ((i + 2) <= MainBufferWr_Rx)
			{	// go thru each option
				type = MainBuffer[i++];
				len = MainBuffer[i++];
				if (len < 2) len = 2;
				switch (type)
				{
					case LCP_MN		:	// check to see if their magic number is the same as ours - if it is, then nak a suggested one back to them for them to use
										// it could be that we are in loop back mode - in which case, give up with the PPP link
										dw = ntohl(*(u32*)(MainBuffer + i));							// fetch the Magic number
										if ((dw) && (dw != PPP.TxMN)) break;							// their MN seems ok to us
																										//
										while (dw == PPP.TxMN) dw = Random32;							//
																										//
										if (MainBufferWr_Tx == 0) MainBufferWr_Tx = MainBufferRd_Rx;	// number bytes to send back - so far
										Len += PPP_AddMN(dw);											//

										#ifdef Debug
											PPP_WeNRejectedStr(type);
										#endif

										break;
					case LCP_MRU	:	// Maximum Receive Unit - limit it to our max rx MRU
										w = ntohs(*(u16*)(MainBuffer + i));								// fetch the MRU they want to use when sending to us
										if (w < 128) w = 128;											// set a minimum MRU to use
										else
										if (w > PPP.RxMRU) w = PPP.RxMRU;								// set a max MRU we can accept
										else
											break;														// it's within limits

										// copy the option back into the packet with a suggestion we can accept
										if (MainBufferWr_Tx == 0) MainBufferWr_Tx = MainBufferRd_Rx;	// number bytes to send back - so far
										Len += PPP_AddMRU(w);											//

										#ifdef Debug
											PPP_WeNRejectedStr(type);
										#endif
										break;
					case LCP_AP		:	// authentication protocol
										w = ntohs(*(u16*)(MainBuffer + i));								// authentication protocol they are requesting us to use
										if (w == PPP_PAP) break;										// they are requesting the one we can do

										// add the auth option we CAN accept
										if (MainBufferWr_Tx == 0) MainBufferWr_Tx = MainBufferRd_Rx;	// number bytes to send back - so far
										Len += PPP_AddPAP();											//

										#ifdef Debug
											sprintf((char*)ScratchPad, "     we n-rejected auth-prot:%04X .. asking for PAP instead\n", w);
											SendDebugStr((char*)ScratchPad);
										#endif
										break;
				}
				i += len - 2;
			}
			if (MainBufferWr_Tx > 0)
			{	// we have n-rejected some options - send them back
				CodeHeader->Code = PPP_CNAK;							// update the header we are sending back
				CodeHeader->Len = htons(Len);							// update the header we are sending back
				PPP.NAK_REJ_Count++;									// update counter
				goto ppp_end1;												// send the packet back
			}

			// *************************************
			// now see if their are any options we must reject

			i = MainBufferRd_Rx;
			while ((i + 2) <= MainBufferWr_Rx)
			{	// go thru each option
				type = MainBuffer[i + 0];
				len = MainBuffer[i + 1];
				if (len < 2) len = 2;
				switch (type)
				{
					case LCP_MRU	:
					case LCP_ACCM	:
					case LCP_MN		:	break;
					case LCP_AP		:	break;	//if ((PPP.Username[0]) || (PPP.Password[0])) break;				// we are going to try to log on
					default			:	// found an option we must reject as we don't use it
										if (MainBufferWr_Tx == 0) MainBufferWr_Tx = MainBufferRd_Rx;	// number bytes to send back - so far
										// copy the option back into the packet for sending back
										if (i != MainBufferWr_Tx) memmove(MainBuffer + MainBufferWr_Tx, MainBuffer + i, len);
										MainBufferWr_Tx += len;
										Len += len;
										#ifdef Debug
											PPP_WeRejectedStr(type);
										#endif
										break;
				}
				i += len;
			}
			if (MainBufferWr_Tx > 0)
			{	// we have rejected some options - send them back
				CodeHeader->Code = PPP_CREJ;							// update the header we are sending back
				CodeHeader->Len = htons(Len);							// update the header we are sending back
				PPP.NAK_REJ_Count++;									// update counter
				goto ppp_end1;												// send the packet back
			}

			// *************************************
			// Accept all of their options - we only get this far if we didunt reject any of their options

			i = MainBufferRd_Rx;										//
			while ((i + 2) <= MainBufferWr_Rx)							//
			{															// go thru each option
				type = MainBuffer[i++];									//
				len = MainBuffer[i++];									//
				if (len < 2) len = 2;									//
				switch (type)											//
				{														//
					case LCP_MRU	:	PPP.RxMRU = ntohs(*(u16*)(MainBuffer + i));
										break;
					case LCP_ACCM	:	PPP.RxACCM = ntohl(*(u32*)(MainBuffer + i));
										break;
					case LCP_MN		:	PPP.RxMN = ntohl(*(u32*)(MainBuffer + i));
										break;
					case LCP_AP		:	PPP.AuthProtocol = ntohs(*(u16*)(MainBuffer + i));
										break;
				}														//
				#ifdef Debug
					PPP_WeAcceptedStr(type);
				#endif
				i += len - 2;											//
			}															//
			CodeHeader->Code = PPP_CACK;								//
			MainBufferWr_Tx = MainBufferWr_Rx;							//
																		//
			PPP.AcceptedTheirLCPOptions = true;							// we have accepted their options
																		//
			u16_Put(&PPP.SendTick, PPP_Timeout);						// send next packet asap
  			goto ppp_end1;												// send ACK packet back
		}																//

		// *******************************************************
		// they have accepted our options

		if (CodeHeader->Code == PPP_CACK)								//
	    {																//
/*			i = MainBufferRd_Rx;										//
			while ((i + 2) <= MainBufferWr_Rx)							//
			{															// go thru each option
				type = MainBuffer[i++];									//
				len = MainBuffer[i++];									//
				if (len < 2) len = 2;									//
				switch (type)											//
				{														//
					case LCP_MRU	:
										break;							//
					case LCP_MN		:
										break;							//
					case LCP_ACCM	:
										break;							//
				}														//
				i += len - 2;											//
			}															//
*/
			PPP.AcceptedOurLCPOptions = true;							// they have accepted our options
																		//
			u16_Put(&PPP.SendTick, PPP_Timeout);						// send next packet asap
  			goto ppp_end1;													//
		}																//
		
		// *******************************************************
		// they have rejected at least one of our options

		if ((CodeHeader->Code == PPP_CNAK) || (CodeHeader->Code == PPP_CREJ))
		{
			i = MainBufferRd_Rx;										//
			while ((i + 2) <= MainBufferWr_Rx)							//
			{															// go thru each option
				type = MainBuffer[i++];									//
				len = MainBuffer[i++];									//
				if (len < 2) len = 2;									//
				switch (type)											//
				{														//
					case LCP_MRU	:	PPP.TxMRU = 0;																	// don't ask again - assum the default of 1500
										if (CodeHeader->Code == PPP_CNAK) PPP.TxMRU = ntohs(*(u16*)(MainBuffer + i));	// no wait - use their suggestion instead
										break;																			//
					case LCP_MN		:	PPP.TxMN = 0;																	// don't ask again
										if (CodeHeader->Code == PPP_CNAK) PPP.TxMN = ntohl(*(u32*)(MainBuffer + i));	// no wait - use their suggestion instead
										break;																			//
					case LCP_ACCM	:	PPP.TxACCM = 0xffffffff;														// wtf else can we do if they send a REJ
										if (CodeHeader->Code == PPP_CNAK) PPP.TxACCM = ntohl(*(u32*)(MainBuffer + i));	// no wait - use their suggestion instead
										break;																			//
				}														//
				i += len - 2;											//
			}															//
																		//
			PPP.AcceptedOurLCPOptions = false;							//
																		//
			u16_Put(&PPP.SendTick, PPP_Timeout);						// send next packet asap
			goto ppp_end1;													//
		}

		// *******************************************************
		// unknown lcp code - send the packet back as a code reject

		goto ppp_CodeReject;
	}

	// ****************
	// it's not a LCP packet

	if ((PPP.Stage == PPPS_None) || (PPP.Stage == PPPS_Start) || (PPP.Stage == PPPS_LCP) || (PPP.Stage == PPPS_Disc)) goto ppp_end1;

	if ((Protocol != PPP_PAP) && (Protocol != PPP_IPCP)) goto ppp_Unknown;	// pffft

	MustByteStuff = false;										// only force byte stuffing with LCP packets

	// ****************
	// Password Authenication Protocol (PAP)

	if (Protocol == PPP_PAP)
	{
		if (PPP.Stage != PPPS_LogOn) goto ppp_end1;

       #ifdef Debug
		ScratchPad[0] = 0;
		if (Len > sizeof(T_CodeHeader))
		{
			len = MainBuffer[MainBufferRd_Rx++];			// message length
			if (len > 0)
			{	// they have sent an ascii message back
				strcpy((char*)ScratchPad, " msg: ");
				memcpy(ScratchPad + strlen((char*)ScratchPad), &MainBuffer[MainBufferRd_Rx], len);
				ScratchPad[len++] = '\n';
				ScratchPad[len] = 0;
			}
		}
		#endif
       
		if (CodeHeader->Code == PPP_CACK)
		{	// Authenticate-Ack - w00t! ... we have logged on
			#ifdef Debug
				if (SendDebugRStr(AuthAcceptedStr))
				{
					if (ScratchPad[0] != 0) SendDebugStr((char*)ScratchPad);
				}
			#endif

			PPP_Stage(PPPS_IP_Addr);							// onto next stage
			goto ppp_end1;
		}

		if ((CodeHeader->Code == PPP_CREJ) || (CodeHeader->Code == PPP_CNAK))
		{	// hmmmmmmmm
			#ifdef Debug
				if (SendDebugRStr(AuthRejectedStr))
				{
					if (ScratchPad[0] != 0) SendDebugStr((char*)ScratchPad);
				}
			#endif

			PPP_End();										// terminate the link
			goto ppp_end1;
		}

		goto ppp_CodeReject;									// unknown code
	}

	// ****************
	// Internet Protocol Control Protocol (IPCP)

	if (Protocol == PPP_IPCP)
	{
		if ((PPP.Stage != PPPS_IP_Addr) && (PPP.Stage != PPPS_IP)) goto ppp_end1;

		// **********************************************************
		// CREQ

		if (CodeHeader->Code == PPP_CREQ)
		{
			// **********************************************************
			// first check to see if theirs any options we need to reject

			i = MainBufferRd_Rx;
			while ((i + 2) <= MainBufferWr_Rx)
			{
				type = MainBuffer[i + 0];
				len = MainBuffer[i + 1];
				if (len < 2) len = 2;
				if ((type != 3) && (type != 129) && (type != 131))
				{	// reject the option
					// copy the option back into the packet for sending back
					if (MainBufferWr_Tx == 0) MainBufferWr_Tx = MainBufferRd_Rx;		// number bytes to send back - so far
					if (i != MainBufferWr_Tx) memmove(&MainBuffer[MainBufferWr_Tx], &MainBuffer[i], len);
					MainBufferWr_Tx += len;
					Len += len;
					#ifdef Debug
						PPP_WeRejectedStr(type);
					#endif
				}
				i += len;
			}
			if (MainBufferWr_Tx > 0)
			{	// we have rejected some options - send them back
				CodeHeader->Code = PPP_CREJ;							// update the header we are sending back
				CodeHeader->Len = htons(Len);							// update the header we are sending back
				PPP.NAK_REJ_Count++;									// update counter
				goto ppp_end1;											// send the packet back
			}

			// **********************************************************
			// accept all the options - we only get this far if we didunt reject any

			i = MainBufferRd_Rx;
			while ((i + 2) <= MainBufferWr_Rx)
			{
				type = MainBuffer[i++];
				len = MainBuffer[i++];
				if (len < 2) len = 2;
				p = 0;
				if (type == 3) p = &PPP.THEIR_IP.ip32;		// Their ip address
				else
				if (type == 129) p = &PPP.DNS1_IP.ip32;		// primary dns ip address
				else
				if (type == 131) p = &PPP.DNS2_IP.ip32;		// secondary dns ip address
				if (p) memmove(p, MainBuffer + i, 4);		// save the ip address
				#ifdef Debug
					PPP_WeAcceptedStr(type);
				#endif
				i += len - 2;
			}
			CodeHeader->Code = PPP_CACK;
			MainBufferWr_Tx = MainBufferWr_Rx;
			goto ppp_end1;
		}

		// **********************************************************
		// CREJ

		if (CodeHeader->Code == PPP_CREJ)
		{
			i = MainBufferRd_Rx;
			while ((i + 2) <= MainBufferWr_Rx)
			{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -