How Ethernet works?

There would be many of use who are presently in colleges and stay in touch with their mates via Ethernet present in colleges .Most libraries today also use Ethernet to make the access of books easier for the readers.

Now Ethernet basically works on a LAN, and is used for communication between the various nodes that are linked to each other. I want to specify that by nodes I mean the PCs and Laptops that have an Ethernet port present in them. Now suppose you want to send a message to your fellow mate using another port. What will happen is your message will be broadcasted on the network. But the question is how does the Ethernet know to whom the message is intended to. I mean you don’t wanna just broadcast your private message right. So, this is accomplished by the use of IP addresses. That are unique for every system. Now the packet or in Ethernet language the frame sent by you would be prefixed with a header that would contain the info about the source address and the destination address, to whom the message is intended to. Now every node that is connected to the network will receive a broadcast frame, and will look upon the destination address. If it matches its IP Address then it will take it otherwise it will discard it.

A Daily Life Example

Now Suppose that there is small room in which some well mannered people are having a conversation. Now, as they are smart and well mannered, before anyone would be willing to share something they would confirm that no one else is talking at that moment. And then only would they speak. This is what happens in Ethernet. If a node wants to broadcast a frame then it waits till the network becomes silent or the network is currently idle. Now coming back to the room example, suppose the everyone has finished and more than one person wants to share something. This situation is termed as collision. When this kind of situation occurs in the Ethernet then the node wait for a random amount of time, so that when the nodes have finished waiting they don’t collide again.

The Ethernet range is usually small like about 500 meters only. But in huge campuses, this kind of thing is undesirable. So repeaters are used to increase the range. The repeaters are basically devices that transfer the frame from one Ethernet segment to another, thereby increasing the range. 

Suppose the small room now becomes a larger room where there are many groups talking to each other at the same time. Now if every one follows the same procedure of mannerism that is waiting for others to finish and then start saying something. That wont be viable. But what happens when someone in your group is talking to you, you are able to distinguish that person’s voice from all the voices that are being heard at the same time. So ,  The probability of collision increases many folds and this leads to delay in communication and slows it. To resolve this network traffic issue, Bridges come into play. Bridges mostly function same as repeaters like transferring the frame from one segment to other. The network is divided into segments. When the frame comes to bridge it checks for the destination address and transfers the frame to that segment accordingly rather than simply broadcasting it to every other segment, it encounters. Bridges are able to reduce traffic upto some level. 

Switches were introduced later on to improvise the concept of bridges and to enhance the transfer speed between the network. Switched Ethernet allows a system to have its own segment and switches allow more segments to connect. Switches replaced the sharing technology with the dedicated station segment for each node. Switches on receiving the frame sends it to the appropriate segment. But since every segment is associated with an individual node, it allows the frame to sent only to the recipient. This allows many conversation to occur simultaneously on the switched network. Switched networks allowed the Ethernet to be full duplex that is node can send as well as receive at the same time. Since the nodes do not directly communicate with each other they do it via the switch, This allows the communication to be collision free. With the use of fiber optic cables transfer speeds of 100Mb/s to 1000Mb/s can be achieved. Switches allow the device to use the whole bandwidth for itself.

Now there is a difference between how the switches and how the routers do. Switches are considered to operable at layer 2 (Data-Link) of OSI Model i.e they work over the MAC addresses, and NIC. while the routers operate on layer 3 that includes IP addresses. Now consider a 4 way road. Suppose vehicles are coming from all four directions intersecting at a common point. Instead of waiting for the road to be clear Switches makes an exit pass for the vehicle that allows them to cross the point without waiting for others to stop. Thats why switches allow the device to use the whole bandwidth for sending and receiving data. In a network the devices can communicate easily if they know destination addresses of each other. Suppose a new device is added to the network. Then how would the other devices know its address. So when a new device enters the network it broadcasts a message which allows all the devices to save its address in their memory, and communicate directly from that point. 

How are routers different from this? Suppose on 4 way road, on reaching the intersection point you have to give the destination address to the border security guard before you can cross the road. Now if you dont know the destination address, then you cant pass. So routers dont allow broadcasts to be sent without knowing the destination address. This is where switches are used. Routers are used to connect two different networks.

Here’s a step-by-step description of transparent bridging:

  • The switch is added to the network, and the various segments are plugged into the switch’s ports.
  • A computer (Node A) on the first segment (Segment A) sends data to a computer (Node B) on another segment (Segment C).
  • The switch gets the first packet of data from Node A. It reads the MAC address and saves it to the lookup table for Segment A. The switch now knows where to find Node A anytime a packet is addressed to it. This process is called learning.
  • Since the switch does not know where Node B is, it sends the packet to all of the segments except the one that it arrived on (Segment A). When a switch sends a packet out to all segments to find a specific node, it is called flooding.
  • Node B gets the packet and sends a packet back to Node A in acknowledgement.
  • The packet from Node B arrives at the switch. Now the switch can add the MAC address of Node B to the lookup table for Segment C. Since the switch already knows the address of Node A, it sends the packet directly to it. Because Node A is on a different segment than Node B, the switch must connect the two segments to send the packet. This is known as forwarding.
  • The next packet from Node A to Node B arrives at the switch. The switch now has the address of Node B, too, so it forwards the packet directly to Node B.
  • Node C sends information to the switch for Node A. The switch looks at the MAC address for Node C and adds it to the lookup table for Segment A. The switch already has the address for Node A and determines that both nodes are on the same segment, so it does not need to connect Segment A to another segment for the data to travel from Node C to Node A. Therefore, the switch will ignore packets traveling between nodes on the same segment. This is filtering.
  • Learning and flooding continue as the switch adds nodes to the lookup tables. Most switches have plenty of memory in a switch for maintaining the lookup tables; but to optimize the use of this memory, they still remove older information so that the switch doesn’t waste time searching through stale addresses. To do this, switches use a technique called aging. Basically, when an entry is added to the lookup table for a node, it is given a timestamp. Each time a packet is received from a node, the timestamp is updated. The switch has a user-configurable timer that erases the entry after a certain amount of time with no activity from that node. This frees up valuable memory resources for other entries. As you can see, transparent bridging is a great and essentially maintenance-free way to add and manage all the information a switch needs to do its job!

In our example, two nodes share segment A, while the switch creates independent segments for Node B and Node D. In an ideal LAN-switched network, every node would have its own segment. This would eliminate the possibility of collisions and also the need for filtering.

Abstract Class and Virtual Function in C++

Abstract Class

Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes are used to provide an Interface for its sub classes. Classes inheriting an Abstract Class must provide definition to the pure virtual function, otherwise they will also become abstract class.

 

Characteristics of Abstract Class

  1. Abstract class cannot be instantiated, but pointers and refrences of Abstract class type can be created.
  2. Abstract class can have normal functions and variables along with a pure virtual function.
  3. Abstract classes are mainly used for Upcasting, so that its derived classes can use its interface.
  4. Classes inheriting an Abstract Class must implement all pure virtual functions, or else they will become Abstract too.

Pure Virtual Functions

Pure virtual Functions are virtual functions with no definition. They start with virtual keyword and ends with= 0. Here is the syntax for a pure virtual function,

virtual void f() = 0;

Concept Of VTables

When working with virtual functions in C++, it’s the vtable that’s being used behind the scenes to help achieve polymorphism. And, although you can understand polymorphism without understanding vtables, many interviewers like to ask this question just to see if you really know your stuff.

Whenever a class itself contains virtual functions or overrides virtual functions from a parent class the compiler builds a vtable for that class. This means that not all classes have a vtable created for them by the compiler. The vtable contains function pointers that point to the virtual functions in that class. There can only be one vtable per class, and all objects of the same class will share the same vtable.

Associated with every vtable is what’s called a vpointer. The vpointer points to the vtable, and is used to access the functions inside the vtable. The vtable would be useless without a vpointer.

class Animal // base class
{

public:

int weight;

virtual int getWeight() {};
}


// Obviously, Tiger derives from the Animal class
class Tiger: public Animal {

public:

int weight;

int height; 

int getWeight() {return weight;};

int getHeight() {return height;};

int main()
{	
	Tiger t1;
	
	/* below, an Animal object pointer is set to point
	   to an object of the derived Tiger class  */
	
	Animal *a1 = &t1; 
	
	/*  below, how does this know to call the 
		definition of getWeight in the Tiger class, 
		and not the definition provided in the Animal 
		class  */
		
	a1 -> getWeight(); 
}			
}

For any class that contains a vtable, the compiler will also add “hidden” code to the constructor of that class to initialize the vpointers of its objects to the address of the corresponding vtable.

The vtable contains function pointers that point to the virtual functions in that class. It’s important to note that there can only be one vtable per class, and all objects of the same class will share the same vtable. This means that in the example above, the Animal and Tiger classes will each have their very own vtable, and any objects of the Animal or Tiger classes will use their respective class’s vtables.

The question that the code above raises is at runtime how does the call “a1 -> getWeight()” know to use the version of getWeight provided in the Tiger – and not the Animal class. The answer, as you probably guessed, is by using vtables.

Because the Tiger class contains a pointer to the vtable called vptr1, the call “a1 -> getWeight()” will actually be translated to “(*(a1 -> vptr1 -> getWeight())”.