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

📄 gprs_ppp.cpp

📁 gprs协议实现
💻 CPP
📖 第 1 页 / 共 2 页
字号:
					OutBuffer[8] = 0x02;
					OutBuffer[9] = 0x06;
					OutBuffer[10] = 0xFF;
					OutBuffer[11] = 0xFF;
					OutBuffer[12] = 0xFF;
					OutBuffer[13] = 0xFF;
			}
		ProcPPPSend ((unsigned char *)OutBuffer, OutBuffer[7] + 6);
		break;

		//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		case ACK:
			//	 Server Acknowledge Async Control
			//	 We Ask PAP Authentication 
			if (InBuffer [8] == 0x02) {
				SendPAPPacket (REQ, InBuffer[5] + 1, (unsigned char *)User, (unsigned char *)Password);
			}
		break;

		//++++++++++++++++++++++++++++++++++++++++++++++++++++
		case NAK:
		break;

		//++++++++++++++++++++++++++++++++++++++++++++++++++++
		case REJ:
		break;

		//++++++++++++++++++++++++++++++++++++++++++++++++++++
		case TERMINATE_ACK:			// Terminate ACK!
			PPPStatus &= ~LinkOn;
		break;

	}
	return;
}



/***********************************************************************
Function : 	protected HandleIPCPOptions
Parameters : 	None
Desc : 		State Machine that implement IPCP packet negotiation
***********************************************************************/
static void HandleIPCPOptions (void) {
unsigned char *dest = (unsigned char *)&OutBuffer[8];
unsigned char *ptr = (unsigned char *)&InBuffer[8];
unsigned char FrameSize;
unsigned char Option;
unsigned char Size;
	switch (InBuffer [4] ) {		//typecode
		case REQ:
			if ((InBuffer [8] != 0x03) && (InBuffer [7] > 0x0A)) {
					OutBuffer [0] = 0xFF;		// Build a IPCP header
					OutBuffer [1] = 0x03;
					OutBuffer [2] = 0x80;		// Set IPCP protocol field
					OutBuffer [3] = 0x21;
					OutBuffer [4] = REJ;		// This will be REJ packet for now
					OutBuffer [5] = InBuffer [5];
					FrameSize = InBuffer[7] - 4;
					///////// Ignore all but option #3 ///////
					while (FrameSize > 0) {
						Option = *ptr;
						Size = *(ptr + 1);
						FrameSize -= Size;
						if (Option == 3) {
							ptr += Size;
							OutBuffer [7] = InBuffer [7] - Size;	//Set New Packet size
						}
						else {
							while (Size-- ) {
								*dest++ = *ptr++;
							}
						}
					}
			}
			else {
				// Acknowledge IP Address 
					memmove (OutBuffer, InBuffer, InBuffer[7]+6);
					OutBuffer [4] = ACK;
                                    	ProcPPPSend ((unsigned char *)OutBuffer, OutBuffer[7] + 6);
                                    	//// Now Request IP address to complete 3-way handshake
          				OutBuffer [4] = REQ;			// Request command
					OutBuffer [5] = OutBuffer [5] + 1;	// Packet ID = ID + 1
					OutBuffer [10] = 0;			// IP address is set to 0 so ISP server
					OutBuffer [11] = 0;			// can assing one to us
					OutBuffer [12] = 0;
					OutBuffer [13] = 0;
			}
			ProcPPPSend ((unsigned char *)OutBuffer, OutBuffer[7] + 6);
		break;

		case ACK:
			if (InBuffer [8] == 3) {			// Reply of the only IPCP Request we can send
/*				
				IPAddress [0] = InBuffer [10];		// ISP assigned IP address
				IPAddress [1] = InBuffer [11];
				IPAddress [2] = InBuffer [12];
				IPAddress [3] = InBuffer [13];
*/
//                for pc 检测
                                 int i;
                                     AnsiString MyHexStr;
                                     AnsiString TempString;
                                     MyHexStr="IP 地址: ";
                                     Form1->Edit4->Text=IntToStr(InBuffer [10])+".";
                                     Form1->Edit4->Text=Form1->Edit4->Text+IntToStr(InBuffer [11])+".";
                                     Form1->Edit4->Text=Form1->Edit4->Text+IntToStr(InBuffer [12])+".";
                                     Form1->Edit4->Text=Form1->Edit4->Text+IntToStr(InBuffer [13]);
                                     for (i=0;i<4;i++)
                                     {
                                        TempString=IntToHex(InBuffer [10+i],2);
                                        MyHexStr+=TempString.SubString(TempString.Length()-1,2);
                                        MyHexStr+=" ";
                                     }
                                        Form1->RichEdit1->Lines->Add(MyHexStr);

				locnode.ip=(*(unsigned long *)&InBuffer[10]);
				locnode.ip=swapl(locnode.ip);
       				locnode.port=StrToInt(Form1->Edit5->Text);     //for pc
//				locnode.port=MYPORT;     //locate  udp port,read from eeprom

				PPPStatus |= LinkOn;			// PPP Link is now up
			}
		break;

		case NAK:
			if ((InBuffer [8] == 0x03) && (InBuffer [7] <= 0x0A)) {
				//	 Request IP Address 
				memmove (OutBuffer, InBuffer, InBuffer[7]+6);
				OutBuffer [4] = 0x01;
				ProcPPPSend ((unsigned char *)OutBuffer, OutBuffer[7] + 6);
			}
		break;

		case REJ:
		break;
	}

}


/***********************************************************************
Function : 	public PPPSendPAPPacket

Parameters : 	Action: 	REQ, REJ, NAK
		ID:		Sequence number of PPP packet
		user:		User name for login
		password:	Password in plain text
Desc : 		Formats a PAP packet on Output Buffer. This function
		supports the type field for future implementation of
		the PPP module in server mode.
***********************************************************************/

void SendPAPPacket (unsigned char Action, unsigned char ID, unsigned char  * user, unsigned char * password) {
	OutBuffer [0] = 0xFF;
	OutBuffer [1] = 0x03;
	OutBuffer [2] = 0xC0;
	OutBuffer [3] = 0x23;
	OutBuffer [4] = Action;
	OutBuffer [5] = InBuffer [5] + 1;
	OutBuffer [6] = 0;
	OutBuffer [7] = strlen (user) + strlen (password) + 6;
	OutBuffer [8] = strlen (user);
	memmove (&OutBuffer [9],user, strlen (user));
	OutBuffer [9 + strlen (user)] = strlen (password);
	memmove (&OutBuffer [10 + strlen (user)],password,  strlen (password));
	ProcPPPSend ((unsigned char *)OutBuffer, OutBuffer[7] + 6);
}



/***********************************************************************
Function : 	protected RejectProtocol

Parameters : 	InBuffer -> A pointer to the buffer that has the PPP
		Packet to reject
Desc : 		Rejects the a PPP packet based on its Protocol field
		Stored on InBuffer

***********************************************************************/

static void RejectProtocol (unsigned char *InBuffer) {

	OutBuffer [0] = 0xFF;
	OutBuffer [1] = 0x03;
	OutBuffer [2] = 0xC0;
	OutBuffer [3] = 0x21;
	OutBuffer [4] = 0x08;
	OutBuffer [5] = 20;
	OutBuffer [6] = 0;
	OutBuffer [7] = InBuffer[7] + 6;
	memmove (&OutBuffer[8], &InBuffer[2], InBuffer [7] + 2);
	ProcPPPSend ((unsigned char *)OutBuffer, OutBuffer[7] + 6);
}
/***********************************************************************
Function : 	protected PPPSendVoidLcp
Parameters : 	None
Desc : 		Sends a void LCP packet with no options to the PPP Server.
		This will force the server to reply with his options to
		negotiate. Some ISPs require scripts to stablish a connection thus
		a void LCP packet will try to force the server to negotiate PPP.
***********************************************************************/
void PPPSendVoidLCP (void) {
	memmove ( OutBuffer,(unsigned char *)PPPData, PPPData[7] + 6);
	ProcPPPSend ((unsigned char *)OutBuffer, OutBuffer[7] + 6);
}
/***********************************************************************
Function : 	PPPTerminate
Parameters : 	None
Desc : 		Terminates a PPP link by sending a terminate LCP packet
***********************************************************************/
void PPPTerminate (void) {
   memmove ( OutBuffer,(unsigned char *)LCPTerminate, 10);
   ProcPPPSend (OutBuffer, 10);
}
/***********************************************************************
Function : 	protected PPPSendVoidIPCP
Parameters : 	None
Desc : 		Sends a void IPCP packet with no options to the PPP Server.
		This will force the server to reply with his options to
		negotiate. Some ISPs require scripts to stablish a connection thus
		a void IPCP packet will try to force the server to negotiate PPP.
***********************************************************************/
void PPPSendVoidIPCP (void) {
	memmove ( OutBuffer,(unsigned char *)IPCPData, IPCPData[7] + 6);
	ProcPPPSend ((unsigned char *)OutBuffer, OutBuffer[7] + 6);
}
/***********************************************************************
Function : 	protected IPNetSend
Parameters : 	None
Desc : 		Sends a void IP packet to the PPP Server.
***********************************************************************/

void IPNetSend() {
	unsigned short len;
	OutBuffer [0] = 0xff;					/* Frame PPP packet */
	OutBuffer [1] = 0x03;
	OutBuffer [2] = 0x00;					/* This is a IP datagram, set protocol type */
	OutBuffer [3] = 0x21;
	len=OutBuffer [6];
	len=(len<<8)+OutBuffer [7];
	ProcPPPSend (OutBuffer, len + 6);
}
/***********************************************************************
Function : 	PPPEntry
Parameters : 	None
Desc : 		PPP Module Entry, Applications should call PPPEntry
		frequently in the main loop or in portions of the app
		code.
***********************************************************************/
void PPPEntry (void) {
		unsigned short protocol;
		short rxlen;
		NODE node;
                ICMPKT *icmp;
   		rxlen=Uart_Receive_Count;
		Uart_Receive_Count=0;

		protocol=InBuffer[2];
		protocol=(protocol<<8)+InBuffer[3];		//
		switch (protocol) {
			case LCP_PACKET:
				HandleLCPOptions ();
				break;
			case PAP_PACKET:
				if (InBuffer [4] == 0x02) {	// Authentication OK
                                       PPPSendVoidIPCP();
                    		}
				break;
			case IPCP_PACKET:  			// IPCP Handler
				HandleIPCPOptions ();
				break;
			case IP_DATAGRAM:			// IP Data Handler
                                ip_in=(IPKT *) &InBuffer [4];
                                ip_out=(IPKT *) &OutBuffer [4];
                                if ((rxlen=is_ip(rxlen))==0) break;
                           	#ifdef  bigend
                                        swap_ip(ip_in);
                                #endif
                                getip_srce(&node);
                                if (ip_in->i.dip==locnode.ip) {
                                	switch (ip_in->i.pcol) {
						case PUDP:
							if ((rxlen=is_udp(rxlen))!=0)
				       				UDP_Handler ((unsigned short)max(rxlen, 0));
        							break;
        					case PICMP:
            						if ((rxlen=is_icmp(rxlen))!=0)  {      /* ICMP? */
                						icmp= (ICMPKT *)ip_in;
                						if (icmp->c.type == ICREQ) {         /* Echo request? */
                    							rxlen = (unsigned short)max(rxlen, 0);       /* Make response */
                    							make_icmp(&locnode, &node,ICREP,
                                                                                icmp->c.codetype,
                                                                                (unsigned char *)&icmp->c.ident,
                                                                                (unsigned short)rxlen);
									IPNetSend();
                						}
							}
                                                        break;
						case PTCP:
        						break;
						default:
	        					break;
					}
                                 }
                                 break;
			default:
				RejectProtocol (InBuffer);	// Cannot handle this type of packet
				break;
			}
}

⌨️ 快捷键说明

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