Distance Vector Routing Protocol Program in C

Pushpendra Sharma
4 min readJul 1, 2024

--

Distance Vector Routing Protocol Program in C

Introduction

The Distance Vector Routing Protocol (DVRP) is a fundamental protocol in computer networks that enables dynamic routing of data packets. It utilizes the Bellman-Ford algorithm to determine the shortest path to all possible destinations in a network. Routers employing DVRP periodically exchange routing tables with their immediate neighbors, which allows the network to dynamically adapt and sustain efficient routes.

This article offers an in-depth guide to implementing a Distance Vector Routing Protocol program in C, encompassing essential concepts, data structures, the algorithm, and a detailed step-by-step process.

Key Concepts

Before diving into the code, it’s essential to understand the core concepts behind Distance Vector Routing Protocol (DVRP):

  1. Distance Vector: Each router maintains a table (vector) that records the cost to reach every possible destination in the network.
  2. Periodic Updates: Routers periodically exchange their distance vectors with their immediate neighbour.
  3. Bellman-Ford Algorithm: The DVRP utilizes the Bellman-Ford algorithm to compute the shortest paths from a source node to all other nodes in the network.
  4. Convergence: The network is said to be in a state of convergence when all routers have consistent and accurate routing tables, indicating that no further changes are occurring.

Algorithm

The DVRP algorithm can be divided into several stages:

  1. Initialization: Initialize the distance vectors and next hops for all nodes based on the cost matrix.
  2. Update Distance Vectors: Periodically update the distance vectors based on information received from neighbors.
  3. Bellman-Ford Update: Apply the Bellman-Ford algorithm to update the routing tables.
  4. Convergence Check: Determine if the network has reached convergence by checking for changes in the distance vectors.

Implementation

Here is a detailed implementation of the Distance Vector Routing Protocol in C:

#include <stdio.h>
#include <stdlib.h>

#define MAX_NODES 10
#define INFINITY 9999

typedef struct {
int id;
int distance[MAX_NODES];
int nextHop[MAX_NODES];
} Node;

Node nodes[MAX_NODES];
int costMatrix[MAX_NODES][MAX_NODES];
int numNodes;

void initializeNodes() {
for (int i = 0; i < numNodes; i++) {
for (int j = 0; j < numNodes; j++) {
if (i == j) {
nodes[i].distance[j] = 0;
nodes[i].nextHop[j] = i;
} else if (costMatrix[i][j] != 0) {
nodes[i].distance[j] = costMatrix[i][j];
nodes[i].nextHop[j] = j;
} else {
nodes[i].distance[j] = INFINITY;
nodes[i].nextHop[j] = -1;
}
}
}
}

void updateDistanceVector(int node) {
for (int i = 0; i < numNodes; i++) {
if (i != node) {
for (int j = 0; j < numNodes; j++) {
if (nodes[node].distance[j] > costMatrix[node][i] + nodes[i].distance[j]) {
nodes[node].distance[j] = costMatrix[node][i] + nodes[i].distance[j];
nodes[node].nextHop[j] = i;
}
}
}
}
}

void printRoutingTable() {
for (int i = 0; i < numNodes; i++) {
printf(“Routing table for node %d:\n”, i);
printf(“Destination\tDistance\tNext Hop\n”);
for (int j = 0; j < numNodes; j++) {
if (nodes[i].distance[j] == INFINITY) {
printf(“%d\t\tINFINITY\t-\n”, j);
} else {
printf(“%d\t\t%d\t\t%d\n”, j, nodes[i].distance[j], nodes[i].nextHop[j]);
}
}
printf(“\n”);
}
}

int main() {
printf(“Enter the number of nodes: “);
scanf(“%d”, &numNodes);

printf(“Enter the cost matrix:\n”);
for (int i = 0; i < numNodes; i++) {
for (int j = 0; j < numNodes; j++) {
scanf(“%d”, &costMatrix[i][j]);
}
}

initializeNodes();

int converged = 0;
while (!converged) {
converged = 1;
for (int i = 0; i < numNodes; i++) {
int oldDistance[MAX_NODES];
for (int j = 0; j < numNodes; j++) {
oldDistance[j] = nodes[i].distance[j];
}
updateDistanceVector(i);
for (int j = 0; j < numNodes; j++) {
if (nodes[i].distance[j] != oldDistance[j]) {
converged = 0;
}
}
}
}

printRoutingTable();

return 0;
}

Explanation

  • Initialization:

The initializeNodes() function initializes the distance vectors and next hops for all nodes based on the provided cost matrix.

  • Update Distance Vectors:

The updateDistanceVector(int node) function updates the distance vector of the specified node using the Bellman-Ford algorithm. It checks if a shorter path exists via an immediate neighbor and updates the distance and next hop accordingly.

  • Convergence Check:

The main loop continues to update distance vectors until the network reaches convergence, which is when no further changes occur in any of the distance vectors.

  • Print Routing Table:

The printRoutingTable() function prints the routing tables for all nodes, showing the distance to each destination and the next hop.

Conclusion

Implementing the Distance Vector Routing Protocol in C offers a hands-on experience with network routing principles and the Bellman-Ford algorithm. Enhancements to this fundamental implementation could include features like managing link failures, accommodating larger networks, and improving performance. Delving into and experimenting with this code provides a deeper understanding of dynamic routing protocols and their role in sustaining efficient paths for data transmission within a network.

--

--

Pushpendra Sharma

I am a Digital Marketing Executive, currently working in JavaTpoint Noida.