You are on page 1of 5

3COM0271 Computer Network Protocols & Architectures A

Week 4: Bit Stuffing and Un-stuffing Practical

You should be familiar with the idea of framing from Chapter 2 of Peterson and Davie and also from the lectures. Using a flag character is one way of doing framing. But to ensure transparency, it requires bit stuffing. You are expected to spend some time doing task A before moving on to task B. In task A, you will understand how framing with flags and bit stuffing works and in task B, you are to put together the fragments of C code that have been provided to make up a complete programs that performs the un-stuffing simulation. TASK A: Simulation of Bit Stuffing and Un-Stuffing In this section, you will be simulating a bit stuffing and un-stuffing example using UNIX pipes. The files you need are available from: Five programs are presented: encoder channel decoder This represents the data source. It takes hexadecimal in, and outputs it as binary. This represents the binary channel. It does not preserve the line boundaries in the input. This represents the receiver of the data. It takes binary input from the channel, and outputs it as hexadecimal. This takes lines of binary input, and outputs binary with a flag character between each line of input ands with a zero stuffed after any 5 consecutive 1 bits in the data. This takes binary input, and outputs it as binary, inserting a new line wherever a flag is encountered on the input, and removing stuffed bits.



Note: bitstuff and unstuff are only available as compiled programs -- you will need to write the source code yourself. 1. Compile the encoder and decoder programs, using: gcc -o encoder encoder.c gcc -o decoder decoder.c Each of these programs takes input from standard input, and sends output to standard output. In not connected to a pipe, standard input (and output) will be from (and to) the terminal. Use ^D (Control and D) to mark end of input from the terminal. Test the encoder and decoder programs; first separately, to confirm that you are familiar with hex numbers, and then use the two programs in a pipeline (as shown below) to verify that together they implement a null transformation (i.e. that one is the inverse of the other). encoder | decoder decoder | encoder hexadecimal input, hexadecimal output binary input, binary output (which, because of buffering, may not appear until input is ended with ^D)


Compile the channel program, and test it. See that if it is part of the pipeline, the boundaries between blocks of data from the encoder are lost. The channel has fixed size (16 bit) messages; it is easier to understand what is happening if your first message is at least 5 hex digits long. gcc -o channel channel.c encoder | channel | decoder


Understand the framing problem: you are going to design and implement a program that will separate the received data into separate messages. This would then be tested as follows: encoder | insert-markers | channel | remove-markers | decoder There are two folders, one called PC-Linux and one called Sparc-SunOS, containing executable versions of the programs. In these you can find the bitstuff and unstuff programs that implement the insert-markers and remove-markers functions. Download the bitstuff and unstuff programs from the PC-Linux folder. Try encoder|bitstuff. See how the flag characters 01111110 is inserted after each line of input and how input containing long sequences of 1 bit is expanded by the insertion of 0 bits. These 0 bits have to be removed at the receiving end. Then, try unstuff with input and output to the terminal, so that you can see what it is doing.


How much increase in length is caused by framing and bit stuffing? Assuming we count the flag at the beginning of a frame and the flag at the end of the frame, how many bits have to be in a frame for the overhead to drop to 30% of the total length?

TASK B: Un-Stuffing Simulation Programming In this section, you are to write a un-stuff program that will remove the markers in the bit stream as well as the stuffed 0 after five consecutive 1s. The bit stuffing program in C is provided at the end of this handout as a guide to help you get started. All the code fragments to do the un-stuff program are given; your task is to put them in the right order and test it as follows! encoder | insert-markers | channel | remove-markers | decoder Un-stuff C code fragments The C codes to do un-stuffing are broken into several fragments. By counting 1s and 0s and their combination, the unstuffing program is required to (i) read framed and stuffed binary from stdin replace the flag 01111110 by a new line replace the 0 after five consecutive 1s by 11111 output the original binary data to stdout You should include the following header library #include <stdio.h> (ii) You should define some variables static int ch, count0, count1; //state shared with utility function // ch: the channel input bit // count0: counter of 0s // count1: counter of 1s


Every C program will need a main function as the program entrance and will look something like this main () { ... // this is where your code fragments is placed }


There is a function to output the buffered bits to the stdout. The function is defined as: flushbuffer() // function to output the buffered bits { if (count0==1) printf("0"); count0=0; while (count1>0) { printf("1"); count1--; } }


The buffer is switched off and reset after the program starts. setbuf(stdout, NULL); // switch off output buffering count0 = count1 = 0; // empty buffer


This is used to read input from stdin while ((ch = getchar ()) != EOF) // start the while loop { ... // your code goes here }


This is used to flush the buffer after the while loop and before the program finishes flushbuffer();


This is the if-statement when the input digit is 1, add it to the buffer if (ch == '1') { count1++; // "1" add it to the buffer }


And you may need an if-statement when the input digit is 0. // If input is "0"... different cases to consider... if (count0==0 && count1==0) count0++; // empty buffer, buffer the new 0; else if (count0==1 && count1==0) { flushbuffer(); count0++ } else if (count1==5) // stuffed '0', empty buffer

{ flushbuffer(); } else if (count0==1 && count1==6) // flag { printf("NL\n"); count0=count1=0; } else if (count1>5) { printf("error\n"); count0=count1=0; } else { flushbuffer(); count0=1; } (x) And another if-statement if the input digit is neither 0or 1. if (ch != '0' && ch != '1') { // (debug) newlines and invalid characters printf ("Z"); }

Some Extra Questions to do 1. Assuming a framing protocol that uses bit stuffing, show the bit sequence transmitted over the link when the frame contains the following bit sequence: 110101111101011111101011111110 2. Suppose the following sequence of bits arrives over a link: 1101011111010111110010111110110 Show the resulting frame after any stuffed bits have been removed. Indicate any errors that might have been introduced into the frame


Suppose you want to send some data using the BISYNC framing protocol, and the last 2 bytes of your data are DLE and ETX. What sequence of bytes would be transmitted immediately prior to the CRC? For each of the following framing protocols, give an example of a byte/bit sequence that should never appear in a transmission? (a) BISYNC (b) HDLC


****************************** Bit-Stuffing C Program Example*****************************

#include <stdio.h> main () { int c; int count; //count the number of 1s setbuf(stdout, NULL); // switch off output buffering printf("\n01111110"); // simulate the flag

while ((c = getchar ()) != EOF) // read from stdin binary data { switch (c) { case '0': count = 0; printf ("%c",c); break; case '1': printf ("%c", c); count ++; if (count == 5) { printf("0"); count = 0; } break; case '\n': printf ("01111110"); count = 0; break; } } } // input is a '0' // reset counter // print to stdout the digit

// // // //

input is a '1' print to stdout the digit increase the counter by 1 after 5 consecutive 1s, insert one 0

// resets counter

// end of frame (EOF) // resets counter