📄 pop3.c
字号:
#include "Page1.h"
#include "protocol.h"
#define POP3_MSG_OPEN 0
#define POP3_MSG_USER 1
#define POP3_MSG_PASS 2
#define POP3_MSG_STAT 3
#define POP3_MSG_DELE 4
#define POP3_MSG_QUIT 5
#define POP3_MSG_LIST 6
#define POP3_MSG_LISTALL 7
#define POP3_MSG_RETR 8
XCHAR Pop3_pServerDomain[DNS_DOMAIN_SIZE];
XCHAR Pop3_pServerIP[IP_ALEN];
XCHAR Pop3_pServerMac[HW_ALEN];
XCHAR Pop3_pUser[POP3_USER_SIZE];
XCHAR Pop3_pPass[POP3_PASS_SIZE];
// Tcp connection data
static XCHAR _pTcbData[TCB_LENGTH];
static XCHAR _iCurMsgPos;
static PXCHAR _pSendData;
static XLONG _iReceiveLength;
static XLONG _iReceiveCur;
static XCHAR _iMsgNum;
static XCHAR _iMsgSent;
static BOOLEAN _bDataAcked;
void SendUser()
{
_pSendData[0] = 'U';
_pSendData[1] = 'S';
_pSendData[2] = 'E';
_pSendData[3] = 'R';
_pSendData[4] = 0x20;
_iCurMsgPos = 0;
while(Pop3_pUser[_iCurMsgPos] != 0)
{
_pSendData[_iCurMsgPos+5] = Pop3_pUser[_iCurMsgPos];
_iCurMsgPos ++;
}
_iCurMsgPos += 5;
_pSendData[_iCurMsgPos] = 0x0d;
_pSendData[_iCurMsgPos+1] = 0x0a;
_iCurMsgPos += 2;
_iMsgSent = POP3_MSG_USER;
}
void _SendPassword()
{
_pSendData[0] = 'P';
_pSendData[1] = 'A';
_pSendData[2] = 'S';
_pSendData[3] = 'S';
_pSendData[4] = 0x20;
_iCurMsgPos = 0;
while(Pop3_pPass[_iCurMsgPos] != 0)
{
_pSendData[_iCurMsgPos+5] = Pop3_pPass[_iCurMsgPos];
_iCurMsgPos ++;
}
_iCurMsgPos += 5;
_pSendData[_iCurMsgPos] = 0x0d;
_pSendData[_iCurMsgPos+1] = 0x0a;
_iCurMsgPos += 2;
_iMsgSent = POP3_MSG_PASS;
}
void _SendStat()
{
_pSendData[0] = 'S';
_pSendData[1] = 'T';
_pSendData[2] = 'A';
_pSendData[3] = 'T';
_pSendData[4] = 0x0d;
_pSendData[5] = 0x0a;
_iCurMsgPos = 6;
_iMsgSent = POP3_MSG_STAT;
}
void _SendListall()
{
_pSendData[0] = 'L';
_pSendData[1] = 'I';
_pSendData[2] = 'S';
_pSendData[3] = 'T';
_pSendData[4] = 0x0d;
_pSendData[5] = 0x0a;
_iCurMsgPos = 6;
_iMsgSent = POP3_MSG_LISTALL;
}
void _SendList(UCHAR iNum)
{
XCHAR pBuf[4];
_pSendData[0] = 'L';
_pSendData[1] = 'I';
_pSendData[2] = 'S';
_pSendData[3] = 'T';
p_itoa(iNum, pBuf, 10);
_iCurMsgPos = 0;
while(pBuf[_iCurMsgPos] != 0)
{
_pSendData[_iCurMsgPos+5] = Pop3_pPass[_iCurMsgPos];
_iCurMsgPos ++;
}
_pSendData[_iCurMsgPos] = 0x0d;
_pSendData[_iCurMsgPos+1] = 0x0a;
_iCurMsgPos += 2;
_iMsgSent = POP3_MSG_LIST;
}
void _SendRetr()
{
XCHAR pBuf[4];
_pSendData[0] = 'R';
_pSendData[1] = 'E';
_pSendData[2] = 'T';
_pSendData[3] = 'R';
_pSendData[4] = 0x20;
p_itoa(_iMsgNum, pBuf, 10);
_iCurMsgPos = 0;
while(pBuf[_iCurMsgPos] != 0)
{
_pSendData[_iCurMsgPos+5] = Pop3_pPass[_iCurMsgPos];
_iCurMsgPos ++;
}
_pSendData[_iCurMsgPos] = 0x0d;
_pSendData[_iCurMsgPos+1] = 0x0a;
_iCurMsgPos += 2;
_iMsgSent = POP3_MSG_RETR;
}
void _SendDele()
{
XCHAR pBuf[4];
_pSendData[0] = 'D';
_pSendData[1] = 'E';
_pSendData[2] = 'L';
_pSendData[3] = 'E';
_pSendData[4] = 0x20;
p_itoa(_iMsgNum, pBuf, 10);
_iCurMsgPos = 0;
while(pBuf[_iCurMsgPos] != 0)
{
_pSendData[_iCurMsgPos+5] = Pop3_pPass[_iCurMsgPos];
_iCurMsgPos ++;
}
_pSendData[_iCurMsgPos] = 0x0d;
_pSendData[_iCurMsgPos+1] = 0x0a;
_iCurMsgPos += 2;
_iMsgSent = POP3_MSG_DELE;
}
void _SendQuit()
{
_pSendData[0] = 'Q';
_pSendData[1] = 'U';
_pSendData[2] = 'I';
_pSendData[3] = 'T';
_pSendData[4] = 0x0d;
_pSendData[5] = 0x0a;
_iCurMsgPos = 6;
_iMsgSent = POP3_MSG_QUIT;
}
void _RunPop3(USHORT iLength, PXCHAR pData)
{
}
void Pop3Initialize()
{
// Tcp init
_bDataAcked = TcbInit(_pTcbData, TCP_LOCAL_PORT_HIGH, POP3_LOCAL_PORT_LOW);
// protocol init
_iCurMsgPos = 0;
_iReceiveLength = 0;
_iReceiveCur = 0;
// buffers init
_pSendData = (PXCHAR)(Sdram_pDataCache2+EP_DATA+IP_DATA+TCP_DATA);
}
void Pop3Start()
{
SdramRead2(POP3_SDRAM_LO);
TcpOpen(_pTcbData, POP3_SERVER_PORT_HIGH, POP3_SERVER_PORT_LOW);
TcpPrepareData(Sdram_pDataCache2);
_bDataAcked = TcpSend(Sdram_pDataCache2, 0);
SdramWrite2(POP3_SDRAM_LO);
_iMsgSent = POP3_MSG_OPEN;
}
void Pop3HandleTimer()
{
if (_bDataAcked) return;
SdramRead2(POP3_SDRAM_LO);
if (TcbHandleTimer(_pTcbData, Sdram_pDataCache2))
{
return;
}
// Handle time out failure
DebugString("Pop3 timer failure restart");
// do error handle here
}
BOOLEAN Pop3Run()
{
if (!TcbCheckData(_pTcbData))
return FALSE;
SdramRead2(POP3_SDRAM_LO);
TcbRun(_pTcbData);
if (Tcp_bAcceptEvent)
{ // actually, no one will call in this pop3 tcp connection
// if called, this must be an error
Tcp_bAcceptEvent = FALSE;
return FALSE;
}
if (Tcp_bConnectEvent)
{ // handle connect event here
Tcp_bConnectEvent = FALSE;
}
if (Tcp_bDataEvent)
{ // Handle pop3 messages here
_RunPop3((UCHAR)(Adapter_iReceiveLen - Tcp_iReceiveHeaderLen), (PXCHAR)(Adapter_pReceivePacket + Tcp_iReceiveHeaderLen));
Tcp_bDataEvent = FALSE;
}
if (Tcp_bCloseEvent)
{ // handle close event here
TcpClose(_pTcbData); // Just call TcpClose to ensure, it is ok to call it repeatly
Tcp_bCloseEvent = FALSE;
}
_bDataAcked = TcpSend(Sdram_pDataCache2, _iCurMsgPos);
_iCurMsgPos = 0;
SdramWrite2(POP3_SDRAM_LO);
if (Tcp_bFreeEvent)
{
// handle free event here, now the tcp connection has shut down already
Pop3Initialize(); // you may re-initialize it here
Tcp_bFreeEvent = FALSE;
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -