📄 p16_ppp.c
字号:
/* PPP functions for ChipWeb - Copyright (c) Iosoft Ltd 2001
**
** This source code is only licensed for distribution in the Iosoft ChipWeb
** package, and the purchaser of that package is granted the non-exclusive
** right to use the software for personal experimentation only, provided
** that this copyright notice is retained. All other rights are retained by
** Iosoft Ltd.
**
** Redistribution of this source code is not permitted. Binary images derived
** from the source code may only be redistributed if a commercial license is
** obtained; see www.iosoft.co.uk or email license@iosoft.co.uk
**
** The software is supplied 'as-is' for development purposes, without warranty
** of any kind, either expressed or implied, including, but not limited to,
** the implied warranties of merchantability and fitness for purpose.
** In no event will Iosoft Ltd. be liable for damages, including any general,
** special, incidental or consequential damages arising out of the use or
** inability to use the software, including, but not limited to, loss or
** corruption of data, or losses sustained by the developer or third parties,
** or failure of the software to operate with any other software or systems.
** This license shall be governed by the laws of England. */
#define DEBUG_IPCP 0 // Set non-zero to emit IPCP state codes
#define INCLUDE_CLIENT_AUTH 1 // Set non-zero to enable client (outgoing) PAP
#define INCLUDE_SERVER_AUTH 0 // Set non-zero to enable server (incoming) PAP
#ifndef NET_TXBUFFERS
#define NET_TXBUFFERS 1 // Number of network transmit buffers
#endif
DEFBIT_2(PORTE, NIC_RESET) // NIC I/O Definitions
DEFBIT_1(PORTE, NIC_IOW_)
DEFBIT_0(PORTE, NIC_IOR_)
DEFBIT_2(PORTC, SER_RTS_) // RTS I/P
DEFBIT_5(PORTC, SER_CTS_) // CTS O/P
#define TRISC_VAL 0xdf // CTS is only O/P on port C
// PPP async encapsulation escape codes
#define PPP_END 0x7e
#define PPP_ESC 0x7d
// HDLC marker for start of frame
#define PPP_START 0xff
// PPP protocols
#define PPP_LCP 0xc021 // Link control
#define PPP_IPCP 0x8021 // IP control
#define PPP_IP_DATA 0x0021 // IP data
#define PPP_CCP 0x80fd // Compression control
#define PPP_PAP 0xc023 // Password authentication
// Values for PPP negotiation code field
#define PPP_CFG_REQ 1
#define PPP_CFG_ACK 2
#define PPP_CFG_NAK 3
#define PPP_CFG_REJ 4
#define PPP_TERM_REQ 5
#define PPP_TERM_ACK 6
#define PPP_CODE_REJ 7
#define PPP_PCOL_REJ 8
#define PPP_ECHO_REQ 9
#define PPP_ECHO_REP 0xa
#define ACTION_MASK 0xfff0
#define STATE_MASK 0x000f
// PPP states (one state machine per protocol) from RFC 1661
#define PPP_INITIAL 0
#define PPP_STARTING 1
#define PPP_CLOSED 2
#define PPP_STOPPED 3
#define PPP_CLOSING 4
#define PPP_STOPPING 5
#define PPP_REQ_SENT 6
#define PPP_ACK_RCVD 7
#define PPP_ACK_SENT 8
#define PPP_OPENED 9
// PPP events (from RFC 1661)
#define EVENT_UP 0
#define EVENT_DOWN 1
#define EVENT_OPEN 2
#define EVENT_CLOSE 3
#define EVENT_TO_OK 4
#define EVENT_TO_ERR 5
#define EVENT_RCR_OK 6
#define EVENT_RCR_ERR 7
#define EVENT_RCA 8
#define EVENT_RCN 9
#define EVENT_RTR 10
#define EVENT_RTA 11
#define EVENT_RUC 12
#define EVENT_RXJ_OK 13
#define EVENT_RXJ_ERR 14
#define EVENT_RXR 15
// Event flag for modem Rx (lo byte is char)
#define EVENT_RXCHAR 0x100
// PPP options
#define LCP_OPT_AUTH 0x3
#define IPCP_OPT_ADDR 0x3
// Data sizes
#define MAXLCP_OPTLEN 256
#define PPP_MRU 1500
#define MAXPPP 1510
// Timeout and retries
#define PPP_TIMEOUT (2*SECTICKS)
#define MAX_REQS 15
#define MAX_TERMS 2
#define MACLEN 6 /* Dummy MAC address */
BYTE myeth[MACLEN];
#define PPP_HEADLEN 4
//#define PPP_HEADLEN 6
#define RXBUFFLEN 128 // Size of Rx buffer
BANK2 WORD net_rxin; // Length of current frame
BANK2 WORD mdm_rxin; // Length of current modem command
BANK2 WORD rxbuffin; // Incoming Rx buffer pointer
BANK2 BOOL rx_checkoff; // Flag to disable Rx checksumming
BANK2 WORD rxout; // Outgoing Rx buffer pointer
BANK2 BOOL atend;
BANK1 BYTE rxbuff[RXBUFFLEN]; // Rx buffer
LOCATE(rxbuff, 0x110)
BOOL inframe; // Flag set if PPP frame is being received
WORD pppticks;
BYTE lcp_reqs, lcp_terms;
BYTE lcp_rest;
BYTE lcp_state, lcp_action;
BYTE ipcp_reqs, ipcp_terms;
BYTE ipcp_rest;
BYTE ipcp_state, ipcp_action;
WORD ppp_pcol;
BYTE ppp_code;
BYTE ppp_rxid, ppp_txid;
BOOL client_auth, client_auth_ok;
BOOL server_auth, server_auth_ok;
char clientname[12]="client";
char clientpass[12]="secret";
char serverid[] = "\7client2\7secret2";
#define TXBUFFLEN 128 // Size of Tx buffer
BANK3 WORD net_txlen; // Max length of Tx data sent to NIC
BANK3 WORD txin; // Current I/P pointer for Tx data
BANK3 BYTE txbuff[TXBUFFLEN]; // Tx buffer
LOCATE(txbuff, 0x190)
WORD txcrc; // Tx CRC value
#if NET_TXBUFFERS > 1
BYTE txbuffnum;
BANK3 BYTE txbuff2[TXBUFFLEN]; // 2nd Tx buffer
WORD txbuff_lens[NET_TXBUFFERS];// Length of last buffer transmission
#endif
#define MAXNET_DLEN (RXBUFFLEN-6)
// PPP action codes for state table
#define XXX 0x0f
#define TLU 0x10
#define TLD 0x20
#define TLS 0x40
#define TLF 0x80
// PPP action codes for action table
#define IRC 0x01
#define ZRC 0x02
#define SCR 0x04
#define SCA 0x08
#define SCN 0x10
#define STR 0x20
#define STA 0x40
#define SCJ 0x80
#define PPP_EVENTS 16
#define PPP_STATES 10
#define NUM_LCP_CODES 0x10
#define AUTH_FAIL_MSG "Unknown peer-ID or password"
// Modem states
#define MDM_IDLE 0
#define MDM_INITIALISING 1
#define MDM_INITIALISED 2
#define MDM_DIALLING 3
#define MDM_ANSWERING 4
#define MDM_CONNECTED 5
#define MDM_DISCONNECTED 6
#define MDM_FAIL 7
// PPP LCP and IPCP state table, giving action(s) and new state for each state & event
// Taken from RFC1661, with last row (echo request) removed
const BYTE lcp_states[PPP_EVENTS-1][PPP_STATES] =
{
// 0 Initial 1 Starting 2 Closed 3 Stopped 4 Closing 5 Stopping 6 ReqSent 7 AckRcvd 8 AckSent 9 Opened
{2, 6, XXX, XXX, XXX, XXX, XXX, XXX, XXX, XXX }, // Up
{XXX, XXX, 0, TLS|1, 0, 1, 1, 1, 1, TLD|1 }, // Down
{TLS|1, XXX, 6, 3, 5, 5, 6, 7, 8, 9 }, // Open
{0, TLF|0, 2, 2, 4, 4, 4, 4, 4, TLD|4 }, // Close
{XXX, XXX, XXX, XXX, 4, 5, 6, 6, 8, XXX }, // TO+
{XXX, XXX, XXX, XXX, TLF|2, TLF|3, TLF|3, TLF|3, TLF|3, XXX }, // TO-
{XXX, XXX, 2, 8, 4, 5, 8, TLU|9, 8, TLD|8 }, // RCR+
{XXX, XXX, 2, 6, 4, 5, 6, 7, 6, TLD|8 }, // RCR-
{XXX, XXX, 2, 3, 4, 5, 7, 6, TLU|9, TLD|6 }, // RCA
{XXX, XXX, 2, 3, 4, 5, 6, 6, 8, TLD|6 }, // RCN
{XXX, XXX, 2, 3, 4, 5, 6, 6, 6, TLD|5 }, // RTR
{XXX, XXX, 2, 3, TLF|2, TLF|3, 6, 6, 8, TLD|6 }, // RTA
{XXX, XXX, 2, 3, 4, 5, 6, 7, 8, 9 }, // RUC
{XXX, XXX, 2, 3, 4, 5, 6, 6, 8, 9 }, // RXJ+
{XXX, XXX, TLF|2, TLF|3, TLF|2, TLF|3, TLF|3, TLF|3, TLF|3, TLD|5 } // RXJ-
};
const BYTE lcp_actions[PPP_EVENTS-1][PPP_STATES] =
{
// 0 Initial 1 Starting 2 Closed 3 Stopped 4 Closing 5 Stopping 6 ReqSent 7 AckRcvd 8 AckSent 9 Opened
{0, IRC|SCR, 0, 0, 0, 0, 0, 0, 0, 0 }, // Up
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // Down
{0, 0, IRC|SCR, 0, 0, 0, 0, 0, 0, 0 }, // Open
{0, 0, 0, 0, 0, 0, IRC|STR, IRC|STR, IRC|STR, IRC|STR }, // Close
{0, 0, 0, 0, STR, STR, SCR, SCR, SCR, 0 }, // TO+
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // TO-
{0, 0, STA, IRC|SCR|SCA, 0, 0, SCA, SCA, SCA, SCR|SCA }, // RCR+
{0, 0, STA, IRC|SCR|SCN, 0, 0, SCN, SCN, SCN, SCR|SCN }, // RCR-
{0, 0, STA, STA, 0, 0, IRC, SCR, IRC, SCR }, // RCA
{0, 0, STA, STA, 0, 0, IRC|SCR, SCR, IRC|SCR, SCR }, // RCN
{0, 0, STA, STA, STA, STA, STA, STA, STA, ZRC|STA }, // RTR
{0, 0, 0, 0, 0, 0, 0, 0, 0, SCR }, // RTA
{0, 0, SCJ, SCJ, SCJ, SCJ, SCJ, SCJ, SCJ, SCJ }, // RUC
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // RXJ+
{0, 0, 0, 0, 0, 0, 0, 0, 0, IRC|STR } // RXJ-
};
void save_txbuff(void);
void check_byte(BYTE b);
void poll_net(void);
void lcp_rx_handler(void);
void lcp_event_handler(BYTE event);
void do_lcp_actions(void);
void ipcp_rx_handler(void);
void ipcp_event_handler(BYTE event);
void pap_rx_handler(void);
void pap_event_handler(BYTE event);
void do_ipcp_actions(void);
void send_ppp(BYTE code, BYTE id, BYTE withdata);
void send_ppp_byte(BYTE b);
void transmit(void);
WORD ppp_crc16(WORD crc, BYTE b);
/* Initialise PPP, return 0 if error */
BOOL init_net(void)
{
SER_CTS_ = 0;
lcp_event_handler(EVENT_OPEN);
do_lcp_actions();
#if !INCLUDE_DIAL
lcp_event_handler(EVENT_UP);
do_lcp_actions();
#endif
ipcp_event_handler(EVENT_OPEN);
return(1);
}
/* Initialise the receive buffer */
void init_rxbuff(void)
{
atend = rxout = 0;
rx_checkoff = 0;
}
/* Initialise the transmit buffer */
void init_txbuff(BYTE buffnum)
{
#if NET_TXBUFFERS > 1
txbuffnum = buffnum;
#endif
txin = net_txlen = 0;
checkflag = checkhi = checklo = 0;
}
/* Move the Rx O/P pointer to the given location, return 0 if beyond data */
BOOL setpos_rxout(WORD newpos)
{
if (newpos > net_rxin)
return(0);
rxout = newpos;
return(1);
}
/* Truncate the remaining Rx data to the given length */
void truncate_rxout(WORD len)
{
WORD end;
if ((end = rxout+len) < net_rxin)
net_rxin = end;
}
/* Move the Rx O/P pointer to the given location, return 0 if beyond data */
BOOL setpos_txin(WORD newpos)
{
if (newpos > TXBUFFLEN)
return(0);
save_txbuff();
txin = newpos;
return(1);
}
/* Save the contents of the Tx buffer into the NIC */
void save_txbuff(void)
{
if (txin > net_txlen)
net_txlen = txin;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -