Distance Vector Routing Protocol Program in C

Distance Vector Routing is a crucial routing algorithm in computer networks for identifying the optimal packet routing path. It involves each router keeping a table (vector) that records the shortest distance to every other router and regularly exchanges this data with neighboring routers. This algorithm, which derives from the Bellman-Ford algorithm, underpins protocols like the Routing Information Protocol (RIP).

Distance Vector Routing Protocol Program in C


Overview

Distance Vector Routing operates under the following principles:

  1. Routers Exchange Information: Periodically, each router sends its routing table to its immediate neighbors.
  2. Update Tables: Upon receiving routing information from a neighbor, a router updates its own table to reflect the shortest known path to each destination.
  3. Convergence: Over time, this exchange of information ensures that all routers have a consistent view of the network, eventually converging to the optimal routes.

Key Concepts

  • Routing Table: A table that holds the shortest distance from the router to each network node and the next hop to reach that node.
  • Bellman-Ford Algorithm: An algorithm that computes shortest paths from a single source vertex to all other vertices in a weighted graph.

Implementation in C

Below is a C program that implements the Distance Vector Routing Protocol.

#include <stdio.h>

#include <stdlib.h>

#include <limits.h>


#define MAX_NODES 10

#define INFINITY INT_MAX


typedef struct {

    int distance[MAX_NODES];

    int nextHop[MAX_NODES];

} RoutingTable;


void initializeRoutingTables(RoutingTable rt[], int numNodes) {

    for (int i = 0; i < numNodes; i++) {

        for (int j = 0; j < numNodes; j++) {

            if (i == j) {

                rt[i].distance[j] = 0;

                rt[i].nextHop[j] = i;

            } else {

                rt[i].distance[j] = INFINITY;

                rt[i].nextHop[j] = -1;

            }

        }

    }

}


void updateRoutingTables(RoutingTable rt[], int numNodes) {

    int updated;

    do {

        updated = 0;

        for (int i = 0; i < numNodes; i++) {

            for (int j = 0; j < numNodes; j++) {

                for (int k = 0; k < numNodes; k++) {

                    if (rt[i].distance[k] != INFINITY && rt[k].distance[j] != INFINITY) {

                        int newDist = rt[i].distance[k] + rt[k].distance[j];

                        if (newDist < rt[i].distance[j]) {

                            rt[i].distance[j] = newDist;

                            rt[i].nextHop[j] = k;

                            updated = 1;

                        }

                    }

                }

            }

        }

    } while (updated);

}


void printRoutingTables(RoutingTable rt[], int numNodes) {

    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 (rt[i].distance[j] == INFINITY) {

                printf("%d\t\t%s\t\t%s\n", j, "INF", "N/A");

            } else {

                printf("%d\t\t%d\t\t%d\n", j, rt[i].distance[j], rt[i].nextHop[j]);

            }

        }

        printf("\n");

    }

}


int main() {

    int numNodes;

    printf("Enter the number of nodes: ");

    scanf("%d", &numNodes);


    RoutingTable rt[MAX_NODES];

    initializeRoutingTables(rt, numNodes);


    int costMatrix[MAX_NODES][MAX_NODES];

    printf("Enter the cost matrix (enter %d for infinity):\n", INFINITY);

    for (int i = 0; i < numNodes; i++) {

        for (int j = 0; j < numNodes; j++) {

            scanf("%d", &costMatrix[i][j]);

            if (costMatrix[i][j] != INFINITY) {

                rt[i].distance[j] = costMatrix[i][j];

                rt[i].nextHop[j] = j;

            }

        }

    }


    updateRoutingTables(rt, numNodes);

    printRoutingTables(rt, numNodes);


    return 0;

}

Explanation

  1. Initialization:

    • The initializeRoutingTables function initializes each node's routing table with infinite distances (INFINITY) except for itself, which is set to 0.
  2. Cost Matrix Input:

    • The program takes the number of nodes and the cost matrix as input. The cost matrix represents the direct costs between nodes, with INFINITY indicating no direct link.
  3. Updating Routing Tables:

    • The updateRoutingTables function iteratively updates the routing tables based on the Bellman-Ford algorithm until no further updates are needed. It checks if a new path found through an intermediate node is shorter than the currently known path.
  4. Output:

    • The printRoutingTables function prints the final routing tables for all nodes, showing the shortest distance to each destination and the next hop to reach that destination.

Conclusion

The Distance Vector Routing Protocol is a crucial algorithm in the field of networking, designed to optimize routing by reducing the distance to each destination node. The accompanying C program offers a clear-cut implementation of this protocol, underscoring the significance of grasping and utilizing basic algorithms for enhanced network efficiency. Utilizing the Bellman-Ford algorithm, the program guarantees that all nodes acquire uniform and optimal routing details, thereby enabling effective and dependable data transfer throughout the network.

Comments