📄 ppp.c
字号:
// this unit assumes the cpu is big endian (MS-Byte 1st) - for now
//
// we don't bother with Address/Control field compression - saving 2 bytes isn't worth the extra code
// we don't bother with Protocol field compression - saving 1 byte definately isn't worth the extra code
/*
* Copyright (C) 2003-2004 by Clive Moss. Email c.a.m@blueyonder.co.uk All rights reserved.
*
* Help & Contributions from D.J.Armstrong Email heli.pad@ntlworld.com
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY CLIVE MOSS 'AS IS' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.
* IN NO EVENT SHALL CLIVE MOSS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
//#ifdef CPU_eZ8
// #pragma stkck // enable stack checking
//#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "common.h"
#include "at.h"
#include "fcs.h"
#include "ppp.h"
#include "ip.h"
#ifdef IncludeTCP
#include "tcp.h"
#endif
#ifdef WindowsPPP
flash char PPP_Rom_Username[] = "avr";
flash char PPP_Rom_Password[] = "1234";
flash char PPP_ModeStr[] = "\nPPP Mode: Windows\n";
flash char DialInit1[] = "";
flash char DialInit2[] = "";
flash char DialStr[] = "";
flash char PPP_WindowsStr[] = "CLIENTCLIENT";
flash char OurIP[4] = {192, 168, 254, 9}; // fixed ip - change this to '0, 0, 0, 0' if you want the ppp server to assign you an ip - that goes for ALL the ip's listed here
flash char Dns1IP[4] = {0, 0, 0, 0}; // dynamic ip
flash char Dns2IP[4] = {0, 0, 0, 0}; // dynamic ip
#endif
#ifdef ATModemPPP
flash char PPP_Rom_Username[] = "";
flash char PPP_Rom_Password[] = "";
flash char PPP_ModeStr[] = "\nPPP Mode: AT Modem\n";
flash char DialInit1[] = "ATZ\n";
flash char DialInit2[] = "ATV1\n";
flash char DialInit3[] = "ATE0\n";
flash char DialStr[] = "ATD179\n";
flash char OurIP[4] = {0, 0, 0, 0}; // dynamic ip
flash char Dns1IP[4] = {0, 0, 0, 0}; // dynamic ip
flash char Dns2IP[4] = {0, 0, 0, 0}; // dynamic ip
#endif
#ifdef GPRS_Orange
flash char PPP_Rom_Username[] = "";
flash char PPP_Rom_Password[] = "";
flash char PPP_ModeStr[] = "\nPPP Mode: GPRS Orange\n";
flash char DialInit1[] = "ATV1\n";
flash char DialInit2[] = "ATE0\n";
flash char DialInit3[] = "AT+CGDCONT=1,\"IP\",\"orangeinternet\"\n"; // oranges Access Point Name
//flash char DialStr[] = "ATD*99#\n";
flash char DialStr[] = "ATD*99***1#\n";
flash char OurIP[4] = {0, 0, 0, 0}; // dynamic ip
flash char Dns1IP[4] = {193, 35, 131, 194}; // fixed ip
flash char Dns2IP[4] = {193, 35, 131, 195}; // fixed ip
#endif
#ifdef GPRS_VodaPhone
flash char PPP_Rom_Username[] = "web";
flash char PPP_Rom_Password[] = "web";
flash char PPP_ModeStr[] = "\nPPP Mode: GPRS VodaPhone\n";
flash char DialInit1[] = "ATV1\n";
flash char DialInit2[] = "ATE0\n";
flash char DialInit3[] = "AT+CGDCONT=1,\"IP\",\"pp.vodafone.co.uk\"\n"; // vodaphones Access Point Name
//flash char DialStr[] = "ATD*99#\n";
flash char DialStr[] = "ATD*99***1#\n";
flash char OurIP[4] = {0, 0, 0, 0}; // dynamic ip
flash char Dns1IP[4] = {0, 0, 0, 0}; // dynamic ip
flash char Dns2IP[4] = {0, 0, 0, 0}; // dynamic ip
#endif
#ifdef Debug
flash char ppp_str1[] = " accepted:";
flash char ppp_str2[] = " n-rejected:";
flash char ppp_str3[] = " rejected:";
flash char LoopBackStr[] = "loop-back mode - disconnecting\n";
flash char Str2[] = "TX: PPP IP REPLY\n";
flash char UnknownProtocolStr[] = " rejected unknown prot\n";
flash char PPPStageStr1[] = "\n*** PPP Stage: ";
flash char PPPStageStr2[] = "None\n";
flash char PPPStageStr3[] = "Starting\n";
flash char PPPStageStr4[] = "LCP Options\n";
flash char PPPStageStr5[] = "Auth'ing\n";
flash char PPPStageStr6[] = "Sorting IP's\n";
flash char PPPStageStr7[] = "IP\n";
flash char PPPStageStr8[] = "Disconnecting\n";
flash char PPPStageStr9[] = "Unknown\n";
flash char CodeStr[] = " Code:";
flash char CREQStr[] = "CREQ";
flash char CACKStr[] = "CACK";
flash char CREJStr[] = "CREJ";
flash char CNAKStr[] = "CNAK";
flash char TREQStr[] = "TREQ";
flash char TACKStr[] = "TACK";
flash char CodeREJStr[] = "CodeREJ";
flash char PREJStr[] = "PREJ";
flash char EREQStr[] = "EREQ";
flash char ERPYStr[] = "ERPY";
flash char UnknownCodeStr[] = "F00kKnows";
flash char LCPStr[] = "PPP_LCP";
flash char IPCPStr[] = "PPP_IPCP";
flash char PAPStr[] = "PPP_PAP";
flash char IPStr[] = "PPP_IP";
flash char AuthAcceptedStr[] = "\n They accepted our Auth'\n";
flash char AuthNoNeedStr[] = "\n No Auth' required it seems\n";
flash char AuthRejectedStr[] = "\n They rejected our Auth'\n";
flash char WeAreInStr[] = "\n *** WE'RE IN ***\n";
flash char UnknownCodeStr2[] = " rejected unknown code\n";
flash char PPPRetryFailStr[] = "*** PPP retry failure\n";
flash char TxBytesStr[] = "Tx-Bytes:";
flash char RxBytesStr[] = " Rx-Bytes:";
#endif
flash char OurIPStr[] = " Our IP: ";
flash char TheirIPStr[] = "\nTheir IP: ";
flash char DNS1IPStr[] = "\nDNS-1 IP: ";
flash char DNS2IPStr[] = "\nDNS-2 IP: ";
T_PPP PPP; // PPP link details
T_PPP_Header1 *PPPHeader = NULL;
T_CodeHeader *CodeHeader = NULL;
//****************************************************************************
bool PPP_DisplayIP(void)
{ // display our IP's
if (!SendConsoleRStr(OurIPStr)) return false;
IP_Str((char*)ScratchPad, PPP.OUR_IP.ip32);
if (!SendConsoleStr((char*)ScratchPad)) return false;
if (!SendConsoleRStr(TheirIPStr)) return false;
IP_Str((char*)ScratchPad, PPP.THEIR_IP.ip32);
if (!SendConsoleStr((char*)ScratchPad)) return false;
if (!SendConsoleRStr(DNS1IPStr)) return false;
IP_Str((char*)ScratchPad, PPP.DNS1_IP.ip32);
if (!SendConsoleStr((char*)ScratchPad)) return false;
if (!SendConsoleRStr(DNS2IPStr)) return false;
IP_Str((char*)ScratchPad, PPP.DNS2_IP.ip32);
if (!SendConsoleStr((char*)ScratchPad)) return false;
if (!SendConsoleByte('\r')) return false;
return SendConsoleByte('\n');
}
#ifdef Debug
bool PPP_WeAcceptedStr(u8 type)
{
if (!SendDebugRStr(ppp_str1)) return false;
sprintf((char*)ScratchPad, "%u\n", type);
return SendDebugStr((char*)ScratchPad);
}
bool PPP_WeNRejectedStr(u8 type)
{
if (!SendDebugRStr(ppp_str2)) return false;
sprintf((char*)ScratchPad, "%u\n", type);
return SendDebugStr((char*)ScratchPad);
}
bool PPP_WeRejectedStr(u8 type)
{
if (!SendDebugRStr(ppp_str3)) return false;
sprintf((char*)ScratchPad, "%u\n", type);
return SendDebugStr((char*)ScratchPad);
}
bool PPP_DisplayPacket(bool Tx, u16 Bytes)
{
u8 type, len;
u16 i, j, Protocol;
u32 dw;
T_CodeHeader *CodeHeader;
i = 0;
j = ntohs(*(u16*)(MainBuffer + i)); //
if (j == 0xff03) i += 2; // the address/control fields are present (no ACFC)
//
if (MainBuffer[i] & 0x01) //
Protocol = (u16)MainBuffer[i++]; // the protocol field has been compressed
else //
{ //
Protocol = ntohs(*(u16*)(MainBuffer + i)); // no PFC
i += 2; //
} //
//
if (Protocol == PPP_IP) return true; // let the IP routine display the packet
// ********************
// display the protocol
if (Tx)
strcpy((char*)ScratchPad, "\nT");
else
strcpy((char*)ScratchPad, "\nR");
strcat((char*)ScratchPad, "X: Protocol: ");
switch (Protocol)
{
case PPP_LCP : rstrcpy((char*)ScratchPad + strlen((char*)ScratchPad), LCPStr);
break;
case PPP_IPCP : rstrcpy((char*)ScratchPad + strlen((char*)ScratchPad), IPCPStr);
break;
case PPP_PAP : rstrcpy((char*)ScratchPad + strlen((char*)ScratchPad), PAPStr);
break;
default : sprintf((char*)ScratchPad + strlen((char*)ScratchPad), "%04x\n", Protocol);
return SendDebugStr((char*)ScratchPad);
}
sprintf((char*)ScratchPad + strlen((char*)ScratchPad), " [%04x]", Protocol);
if (!SendDebugStr((char*)ScratchPad)) return false;
// ********************
// display the code header
CodeHeader = (T_CodeHeader *)(MainBuffer + i);
i += sizeof(T_CodeHeader);
if (!SendDebugRStr(CodeStr)) return false;
switch (CodeHeader->Code)
{
case PPP_CREQ : rstrcpy((char*)ScratchPad, CREQStr);
break;
case PPP_CACK : rstrcpy((char*)ScratchPad, CACKStr);
break;
case PPP_CNAK : rstrcpy((char*)ScratchPad, CNAKStr);
break;
case PPP_CREJ : rstrcpy((char*)ScratchPad, CREJStr);
break;
case PPP_TREQ : rstrcpy((char*)ScratchPad, TREQStr);
break;
case PPP_TACK : rstrcpy((char*)ScratchPad, TACKStr);
break;
case PPP_CodeREJ: rstrcpy((char*)ScratchPad, CodeREJStr);
break;
case PPP_PREJ : rstrcpy((char*)ScratchPad, PREJStr);
break;
case PPP_EREQ : rstrcpy((char*)ScratchPad, EREQStr);
break;
case PPP_ERPY : rstrcpy((char*)ScratchPad, ERPYStr);
break;
default : rstrcpy((char*)ScratchPad, UnknownCodeStr);
break;
}
sprintf((char*)ScratchPad + strlen((char*)ScratchPad), " [%u] ID:%u Len:%u\n", CodeHeader->Code, CodeHeader->ID, ntohs(CodeHeader->Len));
if (!SendDebugStr((char*)ScratchPad)) return false;
// ********************
// display the options (if any)
if ((Protocol == PPP_LCP) || (Protocol == PPP_IPCP))
{
if ((CodeHeader->Code == PPP_CREQ) || (CodeHeader->Code == PPP_CACK) || (CodeHeader->Code == PPP_CNAK) || (CodeHeader->Code == PPP_CREJ))
{
while ((i + 2) <= Bytes)
{
type = MainBuffer[i + 0];
len = MainBuffer[i + 1];
if (len < 2) len = 2;
sprintf((char*)ScratchPad, " type:%u len:%u", type, len);
if (Protocol == PPP_IPCP)
{
if (type == 2)
{
if (len >= 2)
{
strcat((char*)ScratchPad, " comp-prot: ");
j = ntohs(*(u16*)(MainBuffer + i + 2));
sprintf((char*)ScratchPad + strlen((char*)ScratchPad), "%04X", j);
}
}
else
if ((type == 3) || (type == 129) || (type == 130) || (type == 131) || (type == 132))
{
if (len >= 4)
{
strcat((char*)ScratchPad, " ip: ");
dw = *(u32*)(MainBuffer + i + 2);
IP_Str((char*)ScratchPad + strlen((char*)ScratchPad), dw);
}
}
}
else
if (Protocol == PPP_LCP)
{
switch (type)
{
case LCP_MRU : strcat((char*)ScratchPad, " MRU: ");
j = ntohs(*(u16*)(MainBuffer + i + 2));
sprintf((char*)ScratchPad + strlen((char*)ScratchPad), "%u", j);
break;
case LCP_ACCM : strcat((char*)ScratchPad, " ACCM: ");
dw = ntohl(*(u32*)(MainBuffer + i + 2));
bin2bstr(ScratchPad + strlen((char*)ScratchPad), dw, 32);
break;
case LCP_AP : strcat((char*)ScratchPad, " AP: ");
j = ntohs(*(u16*)(MainBuffer + i + 2));
sprintf((char*)ScratchPad + strlen((char*)ScratchPad), "%02X", j);
break;
case LCP_MN : strcat((char*)ScratchPad, " MN: ");
dw = ntohl(*(u32*)(MainBuffer + i + 2));
sprintf((char*)ScratchPad + strlen((char*)ScratchPad), "%lu", dw);
break;
case LCP_PFC : strcat((char*)ScratchPad, " PFC");
break;
case LCP_ACFC : strcat((char*)ScratchPad, " ACFC");
break;
default : strcat((char*)ScratchPad, " .. ");
for (j = 2; j < len; j++) sprintf((char*)ScratchPad + strlen((char*)ScratchPad), "%02X ", (u16)((u8)MainBuffer[i + j]));
break;
}
}
strcat((char*)ScratchPad, "\n");
if (!SendDebugStr((char*)ScratchPad)) return false;
i += len;
}
}
}
// ********************
return true;
}
bool PPP_DisplayStage(void)
{
if(!SendDebugRStr(PPPStageStr1)) return false;
switch (PPP.Stage)
{
case PPPS_None : return SendDebugRStr(PPPStageStr2);
case PPPS_Start : return SendDebugRStr(PPPStageStr3);
case PPPS_LCP : return SendDebugRStr(PPPStageStr4);
case PPPS_LogOn : return SendDebugRStr(PPPStageStr5);
case PPPS_IP_Addr : return SendDebugRStr(PPPStageStr6);
case PPPS_IP : return SendDebugRStr(PPPStageStr7);
case PPPS_Disc : return SendDebugRStr(PPPStageStr8);
default : return SendDebugRStr(PPPStageStr9);
}
}
#endif
//****************************************************************************
void PPP_Stage(T_PPPStage Stage)
{
if (PPP.Stage == Stage) return; // no change
//
#ifdef Debug
PPP_DisplayStage(); // display current stage
#endif
//
PPP.Stage = Stage; // set the new PPP stage/state
PPP.Retries = 0; // reset the send retry counter
u16_Put(&PPP.SendTick, PPP_Timeout); // send next packet asap
PPP.NAK_REJ_Count = 0; // reset the rejection counter
#ifdef Debug
PPP_DisplayStage();
#endif
}
//****************************************************************************
// send the PPP buffer to the server.
//
// This is where we add the byte stuffing and also calculate the fcs on the fly.
//
// Byte stuffing is used to make sure the start/stop flag (a single byte) never
// appears in the packet data - so ensuring we can 100% reliably detect the packet boundries from
// one packet to the next.
bool PPP_SendByte(u8 c)
{
PPP.TxBytes++; // update byte counter
return SendModemByte(c); // send byte
}
bool PPP_TxByte(u8 c, bool MustByteStuff)
{ // send byte with byte stuffing
if (!WatchdogCounter) WatchdogCounter++; //
//
if (c == PPP_Flag) goto Stuff; // stuff the byte
if (c == PPP_EscapeFlag) goto Stuff; // stuff the byte
if (c > 31) goto NoStuff; // no need to stuff the byte
if (MustByteStuff) goto Stuff; // must use byte stuffing - for now
if (!(PPP.TxACCM & (1 << c))) goto NoStuff; // no need to stuff the byte
Stuff:
if (!PPP_SendByte(PPP_EscapeFlag)) return false; // send byte
c ^= PPP_Stuff; // modify the byte
NoStuff:
return PPP_SendByte(c); // send byte
}
bool PPP_SendPacket(bool MustByteStuff)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -