IP Datagrams gets routed dynamically in the sense, we cannot precisely predict which path every IP Datagram will take to reach the destination. Some times you may need to
fix the path[ a set of routers] through which the IP Datagram must travel to reach the destination. IP Source Routing helps you to accomplish this.
Source Routing is a technique, through which you set a list of Router's IP addresses [Pre Configured Path], so that the IP datagram which gets sent out follows the path
which you are providing and reaches the destination.
Loose Source Routing:
Here you provide a list of IP addresses, but the datagram may take an alternative path to reach the destination.
Strict Source Routing:
Here you provide a list of IP addresses and the IP Datagram MUST pass through the provided IP Address list.
1: \\Define a structure for LSR
2: typedef struct
4: unsigned char Code;
5: unsigned char Len;
6: unsigned char Offset;
7: unsigned long Addrs;
10: //Set the Loose Source Routing Option:
11: LSR SourceRoute;
14: SourceRoute.Code = 0x83; // Loose Source Routing.
15: SourceRoute.Len = 11;
16: SourceRoute.Offset = 4;
17: SourceRoute.Addrs  = inet_addr("a.b.c.d");
18: SourceRoute.Addrs  = inet_addr("x.y.z.q");
20: //Set the source routing on the socket handle.
21: int iErr = setsockopt(SocketHandle,IPPROTO_IP,IP_OPTIONS,(char*)&SourceRoute,SourceRoute.oLen);
The above code injects the two IP addresses "a.b.c.d" and "x.y.z.q" into every IP Datagrams IP options header which are sent out using the socket . Now the IP Datagram
has the following things:
a) Source IP address
b) Destination IP address
c) IP options field in the IP header containing "a.b.c.d" and "x.y.z.q"
Here is how it works:
1) The actual Destination Address is replaced with "a.b.c.d" and the IP Datagram is pushed out to "a.b.c.d"
2) "a.b.c.d" will receive the datagram and replaces the destination address again with "x.y.z.q" and pushes the IP datagram to "x.y.z.q"
3) "x.y.z.q" will receive the datagram and replaces the destination address with the actual destination address and pushes the IP datagram to the intended destination.