Encoding and decoding of PPP data frames

Source: Internet
Author: User
Tags control characters

I. Summary

PPP is a commonly used link protocol in ATM Networks. pppoa based on PPP protocol is indispensable in XDSL modem. This article briefly describes the frame Encapsulation Format of the PPP protocol, and provides a simple PPP encoding and decoding algorithm, hoping to be helpful to the consumers.

II. Introduction to the PPP protocol

The format of the PPP data frame looks like the iso hdlc (High-Level Data Link Control) standard. Is PPP data
Frame format.

Each frame starts and ends with a symbolic character 0x7e. Followed by an address byte. The value is always 0xff, followed by
0x03 control bytes.

Next is the protocol field, which is similar to the type field function in Ethernet. When its value is 0x0021, it indicates information
The field is an IP datagram. If the value is 0xc021, it indicates that the information field is link control data. If the value is 0x8021, it indicates
The information field is the network control data.

The CRC Field (or FCS, frame check sequence) is a cyclic redundancy check code to detect errors in data frames.
Since the flag value is 0x7e, PPP needs to escape the character when it appears in the information field.
In the synchronization link, this process is completed through a hardware technology called bit stuffing.
1989]. In an asynchronous link, the special character 0x7d is used as an escape character. When it appears in a PPP data frame
The 6th bits of the character must be supplemented. The specific implementation process is as follows:

1) when the character 0x7e is encountered, two consecutive characters must be transferred: 0x7d and 0x5e to escape the character.
2) When the Escape Character 0x7d is encountered, two consecutive characters must be transferred: 0x7d and 0x5d to escape the escape character.
3) by default, if the character value is less than 0x20 (for example, an ASCII control character ),
. For example, if the character 0x01 is encountered, the character 0x7d and 0x21 must be transmitted consecutively (at this time, the 6th bits are converted
1, and the previous two cases change it to 0 ).

The reason for this is to prevent them from appearing in the serial interface driver or modem of both hosts, because
These control characters are interpreted as special meanings. Another possibility is to use the Link Control Protocol to specify whether or not
Escape some values of these 32 characters. By default, all 32 characters are escaped.

-- Excerpted from "TCP/IP explanation", Volume 1. Protocol (W. Richard Steven S)

Iii. encoding and decoding

1. Encoding

Encoding is to transform the characters to be escaped according to the preceding description. The following is a simple implementation code:

001 # define ppp_frame_flag 0x7e/* flag character */

002 # define ppp_frame_esc 0x7d/* escape character */

003 # define ppp_frame_enc 0x20/* encoded character */

004 # define buf_len 1500.

005/* return: bytes encoded */

006 int pppencode (unsigned char * Buf, int Len ){
007 unsigned char * Pi, * Po;
008 int I, olen;
009 unsigned char obuf [buf_len];

010 if (LEN> (buf_len> 1 )){
011 return-1;
012}

013 memset (obuf, 0, buf_len );
014 Pi = Buf;
015 po = obuf;
016 olen = Len;

017 for (I = 0; I <Len; I ++ ){

018/* byte needs encode, encode it */
019 if (* Pi = ppp_frame_flag
020 | * Pi = ppp_frame_esc
021 | * pI <0x20 ){
022 * Po = ppp_frame_esc;
023 Po ++;
024 olen ++;

025/* XOR the 6th bit */
026 * Po = * PI ^ ppp_frame_enc;
027}
028 else {
029 * Po = * PI;
030}
031 PI ++;
032 Po ++;
033}

034 memcpy (BUF, obuf, olen );

035 return olen;
036}

001 ~ 003: defines the flag characters, escape characters, and encoding characters.

010 ~ 012: check the length of the character to be encoded. In the worst case, a character is encoded into two characters, so the maximum length of the buffer zone can be half the length.

018 ~ 027: encoding is mainly implemented. When a flag character, escape character, or a control character smaller than 0x20 is encountered, it must be encoded. The method is to insert an escape character 0x7d in front of it, and then use the 6th-bit complement code.

028 ~ 030: other characters, not modified.

034 ~ 035: Modify the buffer and return the encoded character length.

2. Decoding

Decoding is actually an inverse operation of encoding. It removes the Escape Character and complements the 6th-bit character after the escape character.

001/* return: bytes decoded */

002 int pppdecode (unsigned char * Buf, int Len ){
003 unsigned char * Pi, * Po;
004 int I, olen;
005 unsigned char obuf [buf_len];

006 if (LEN> buf_len ){
007 return-1;
008}

009 memset (obuf, 0, buf_len );
010 Pi = Buf;
011 po = obuf;
012 olen = Len;

013 for (I = 0; I <Len; I ++ ){
014 if (* Pi = ppp_frame_esc ){

015/* skip the escape byte */
016 PI ++;
017 olen --;

018/* XOR the 6th bit */
019 * Po = * PI ^ ppp_frame_enc;
020}
021 else {
022 * Po = * PI;
023}
024 PI ++;
025 Po ++;
026}

027 memcpy (BUF, obuf, olen );

028 return olen;
029}

006 ~ 008: Check the length of characters to be decoded.

014 ~ 020: the main implementation of decoding. When an escape character is encountered, it is skipped and the 6th-bit complement of the character that follows it.

021 ~ 023: other characters, not processed.

027 ~ 028: Modify the buffer and return the decoded character length.

This codec code is mainly readable and can be optimized by the speaker.

---------------

For study and communication only, please indicate the author and source for reprinting. Thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.