2. Program for character stuffing 3- Connect the computers in Local 4.
n Local        4.To configure DHCP and DNS server for a given network in packet tracer simulator tool.
#include<stdio.h>                            Area Network.                            DHCP (Dynamic Host Configuration Protocol) is a protocol that lets network administrators manage centrally and automate
#include<conio.h>                            On the Host Computer (Sharing            the assignment of Internet Protocol (IP) addresses in an organization's network. If a machine uses Internet's set of protocol
                                             Internet):                               (TCP/IP), each machine that can connect to the Internet needs a unique IP address. When an organization sets up its
#include<string.h>
                                             Log in as Administrator or Owner.        computer users with a connection to the Internet, an IP address must be assigned to each machine. Without DHCP, the IP
#include<process.h>                                                                   address must be entered manually at each computer and, if computers move to another location in another part of the
void main()                                  Go to: Start > Control Panel > Network
                                             and Internet Connections > Network       network, a new IP address must be entered. DHCP lets a network administrator supervise and distribute IP addresses from
{                                            Connections.                             a central point and automatically sends a new IP address when a computer is plugged into a different place in the network.
int i=0,j=0,n,pos;                           Right-click the Internet connection      IP Address Allocation Mechanism DHCP supports three mechanisms for IP address allocation. •
char a[20],b[50],ch;                         (e.g., dial-up, Ethernet).                Automatic allocation: DHCP assigns a permanent IP address to a host. •
clrscr();                                    Click Properties.                         Dynamic allocation: DHCP assigns an IP address to a host for a limited period of time (or until the host explicitly
                                             Go to the Advanced tab.                  relinquishes the address). •
printf("enter string\n");
                                             Check: "Allow other network users to      Manual allocation: Host's IP address is assigned by the network administrator, and DHCP is used simply to convey the
scanf("%s",&a);                                                                       assigned address to the host. A particular network will use one or more of these mechanisms, depending on the policies of
                                             connect through this computer's
n=strlen(a);                                                                          the network administrator.
                                             Internet connection".
printf("enter position\n");                  (If using dial-up) Check:               Messages Used by DHCP:
scanf("%d",&pos);                             "Establish a dial-up connection          • DHCP Discover - Client broadcast to locate available servers. It is assumed atleast one of the servers will have
if(pos>n)                                    whenever a computer on my network        resources to fulfill the request.( may include additional pointers to specific services required eg. particular subnet, minimum
{                                            attempts to access the Internet"         time limit etc ).
                                             (optional).                              • DHCP Offer - Server to client in response to DHCP Discover with offer of configration parameters.
printf("invalid position, Enter again :");
                                             Click OK.                                • DHCP Request - Client broadcast to servers requesting offered parameters from one server and implicitly declining offers
scanf("%d",&pos);                                                                     from all others.( also important in case of lease renewal if the alloted time is about to expire ).
                                             When prompted:
}                                                                                      • DHCP Decline - Client to server indicating configration parameters invalid.
                                              "Your LAN adapter will be set to IP
printf("enter the character\n");             192.168.0.1. Other PCs may lose           • DHCP Release - Client to server relinquishing network address and cancelling current lease.( in case of a graceful shut
ch=getche();                                 connectivity. Continue?"                down DHCP server is sent a DHCP Release by the host machine).
b[0]='d';                                     Click Yes.                              • DHCP Ack - Server to client with configration parameters, including committed Network address.
                                             Now, Internet is shared. The host LAN     • DHCP Nack - Server to client refusing request for configuration parameters (eg. requested network address already
b[1]='l';
                                             adapter is set to:                       allocated).
b[2]='e';                                                                             Timers Used Note:
b[3]='s';                                    IP Address: 192.168.0.1
                                                                                       that lease time is the time specified by the server for which the services have been provided to the client.
b[4]='t';                                    Subnet Mask: 255.255.255.0                • Lease Renewal Timer - When this timer expires machine will ask the server for more time sending a DHCP Request.
b[5]='x';                                                                              • Lease Rebinding Timer - Whenever this timer expires, we have not been receiving any response from the server and so
j=6;
                                             On the Client Computer                   we can assume the server is down. Thus send a DHCP Request to all the servers using IP Broadcast facility. This is only
while(i<n)                                   (Receiving Internet):                    point of difference between Lease renewal and rebinding.
                                             Log in as Administrator or Owner.        • Lease Expiry Timer - Whenever this timer expires, the system will have to start crashing as the host does not have a
{
                                             Go to: Start > Control Panel > Network   valid IP address in the network.
if(i==pos-1)                                 and Internet Connections > Network       Timer Configuration Policy
{                                            Connections.                             The timers have this usual setting which can be configured depending upon the usage pattern of the network.
b[j]='d';                                    Right-click Local Area Connection →      An example setting has been discussed below.
b[j+1]='l';                                  Properties.                              Lease Renewal = 50 % Lease time
b[j+2]='e';                                  On General tab, select Internet          Lease Rebinding = 87.5 % Lease time
                                             Protocol (TCP/IP) → Click Properties.    Lease Expiry = 100 % Lease time Practical:
b[j+3]=ch;
                                             Select:
b[j+4]='d';                                  Obtain an IP address automatically       DHCP ON PACKET TRACER STEPS ARE AS FOLLOWS:
b[j+5]='l';                                  Assign manually:                          1. Let us make a topology with one router on which we will apply DHCP and several client PCs. More like this one,
b[j+6]='e';                                  IP Address: 192.168.0.x (e.g.,           2. Now, we will apply DHCP on the router.The commands in sequence are as follows.
j=j+7;                                       192.168.0.2)                             Router>en
}                                            Subnet Mask: 255.255.255.0                Router#conf t
if(a[i]=='d' && a[i+1]=='l' &&               Default Gateway: 192.168.0.1             Router(config)# int fa 0/0
                                             Click OK to close all dialogs.           Router(config-if)# ip address 192.168.11 255.255.255.0
a[i+2]=='e')
                                             Exit Control Panel.                       Router(config-if)# no shut % LINK-5-CHANGED:
{                                                                                     % LINK-5-CHANGED:Interface FastEthernet 0/0, change state to up %LINEPROTO-5-UPDOWN:
b[j]='d';                                    _____________________________
                                                                                      Line protocol on Interface FastEthernet 0/0, change state to up
b[j+1]='l';                                                                           router (config-if)# ip dhcp pool cisco
                                             10. To configure dynamic routing
b[j+2]='e';                                                                           router (dhp-config)# network 192.168.1.0 255.255.255.0
j=j+3;
}
                                             🔧
                                             protocols in packet race tool.
                                                Steps:
                                             Step 1: Set up the Network Topology:
                                                                                      router(dhcp-config)#default-router 192.168.1.1
                                                                                       router(dhcp-config)# exit
                                                                                      router(config)# ip dhcp excluded-address 192.168.1.4 192.168.1.7
b[j]=a[i];
                                                                                      router(config)#exit
i++;                                         Create a simple topology by adding       (i)command “ip dhcp pool cisco”, we are creating a pool for DHCP called cisco.
j++;                                         devices like routers, switches, and      (ii)command “default-router “ we are telling the DHCP about the default route to follow.
}                                            PCs.                                     (iii)“ip dhcp excluded-addresses x-x”, where x is the starting and ending IP address respectively.
b[j]='d';                                    Connect them using appropriate            We are basically reserving some IPs for our use. It can be used to attach printers, or assign it to some specific users for
                                             cables (copper straight-through or       security purposes.
b[j+1]='l';                                  crossover, depending on device
b[j+2]='e';                                                                           3.Now, open the PC. Click on IP Configuration. Select from Static to DHCP
                                             types).                                  4. And after the DHCP request is completed you will see the following screen.
b[j+3]='e';
b[j+4]='t';                                  Step 2: Assign IP Addresses:
b[j+5]='x';                                  Assign IP addresses to all the             7. To configure NAT (Network Address Translation) for a given           Ro(config-if)# no shutdown
b[j+6]='\0';                                 interfaces of routers and PCs.             network in the Packet Tracer simulator tool.
                                             Ensure each device has a unique IP                                                                                 Ro(config-if)# exit
printf("\nframe after stuffing:\n");                                                    Code / Steps:
                                             address within the correct subnet.                                                                                Ro(config)# ip route 50.0.0.0
Computer Networks Lab LAB                                                               Configuration on Router R1:
MANUAL                                                                                                                                                          255.0.0.0 20.0.0.2
                                             Example:                                   Router> enable
17                                           Router1 Fa0/0: 192.168.1.1/24                                                                                      Explanation:
printf("%s",b);                                                                         Router# configure terminal                                              Since there is no direct route for 10.0.0.2,
                                             Router2 Fa0/0: 192.168.2.1/24
getch();                                     Step 3: Enable Routing on Routers          Router(config)# hostname R1                                             external networks like 30.0.0.0 will only
Program for bitstuffing                      Click on the router.                      R1(config)# interface FastEthernet 0/0                                  recognize it as the web server IP 50.0.0.1
#include <stdio.h>                                                                      R1(config-if)# ip address 10.0.0.1 255.0.0.0
                                             Go to the CLI tab at the bottom of the                                                                             (which is translated using NAT).
#include <string.h>
void bitStuffing(int N, int arr[]) {         Packet Tracer window.                     R1(config-if)# no shutdown                                              To test the setup:
    int brr[30];                                                                        R1(config-if)# exit                                                     Open any computer from the external
    int i = 0, j = 0, k;                     Step 4: Enter Privileged EXEC Mode                                                                                 network (connected to 30.0.0.0)
                                                                                        R1(config)# interface Serial 0/0/0
    while (i < N) {
                                             Step 5: Configure OSPF Routing             R1(config-if)# ip address 20.0.0.2 255.0.0.0                            Go to Command Prompt
       if (arr[i] == 1) {
           int count = 1;                    Protocol                                                                                                           Type:
                                                                                        R1(config-if)# no shutdown
           brr[j] = arr[i];                  bash                                                                                                               ping 50.0.0.1
           for (k = i + 1; arr[k] == 1 &&    CopyEdit                                   R1(config-if)# exit
                                                                                                                                                                Expected Output:
 k < N && count < 5; k++) {                  configure terminal                         R1(config)# ip route 30.0.0.0 255.0.0.0 20.0.0.1
                                                                                                                                                                Pinging 50.0.0.1 with 32 bytes of
              j++;                           router ospf <process-id>                   R1(config)# ip nat inside source static 10.0.0.2
              brr[j] = arr[k];               network <network-address>                                                                                          data:
              count++;                                                                  50.0.0.1
                                             <wildcard-mask> area <area-id>                                                                                     Reply from 50.0.0.1: bytes=32
              if (count == 5) {                                                         R1(config)# interface FastEthernet 0/0
                  j++;                                                                                                                                          time<1ms TTL=64
                                             Replace:                                   R1(config-if)# ip nat inside
                  brr[j] = 0;                                                                                                                                   Ping statistics for 50.0.0.1:
              }                              <process-id> with any unique               R1(config-if)# exit
                                             number (e.g., 1)                                                                                                         Packets: Sent = 4, Received =
              i = k;                                                                    R1(config)# interface Serial 0/0/0
           }                                 <network-address> and                                                                                              4, Lost = 0 (0% loss)
       } else {                              <wildcard-mask> with your actual           R1(config-if)# ip nat outside
                                                                                                                                                                Now ping from inside network:
           brr[j] = arr[i];                  network info                              R1(config-if)# exit                                                     ping 10.0.0.2
       }                                     <area-id> with the OSPF area               Configuration on Router Ro:
       i++;                                                                                                                                                     Expected Output:
    }
       j++;                                  ✅
                                             (usually 0 for backbone area)
                                                Example:
                                                                                        Router> enable
                                                                                        Router# configure terminal
                                                                                                                                                                Ping statistics for 10.0.0.2:
                                                                                                                                                                      Packets: Sent = 4, Received =
                                             router ospf 1
                                             network 192.168.1.0 0.0.0.255              Router(config)# hostname Ro                                             4, Lost = 0 (0% loss)
   for (i = 0; i < j; i++)
      printf("%d", brr[i]);                  area 0                                     Ro(config)# interface FastEthernet 0/0                                  Conclusion:
}                                            network 192.168.2.0 0.0.0.255              Ro(config-if)# ip address 30.0.0.1 255.0.0.0                            This demonstration shows how companies use
int main() {                                 area 0                                     Ro(config-if)# no shutdown                                              NAT (Network Address Translation) to hide
   int N = 6;                                Final Step:
   int arr[] = {1, 1, 1, 1, 1, 1};                                                      Ro(config-if)# exit                                                     internal IP addresses from the outside world,
                                             Save the configuration:
   bitStuffing(N, arr);                      write memory                               Ro(config)# interface Serial 0/0/0                                      while still allowing external access to services
   return 0;                                 copy running-config                                                                                                through a public IP address.
}                                                                                       Ro(config-if)# ip address 20.0.0.1 255.0.0.0
                                             startup-config
8- Write a program to implement TCP & UDP Sockets.                                                                                                9. C Program to Transmit a Character, String, and File
                                                                                                                                                  Data using Static Array (Simulated in Packet Tracer
Theory/concept:-                                                               6: Write a CAD code to get the MAC (physical) address of the       Style)
                                                                               system using ARP and to simulate it in a given network
Below find an example of a very simple client-server program in C. Basically   according to requirements in the Packet Tracer simulator.          #include <stdio.h>
the client connects to the server, the                                                                                                            #include <fcntl.h>
server sends the message “Hello World”, and the client prints the received :                                                                      #include <unistd.h>
                                                                               ARP:
                                                                                                                                                  #include <string.h>
                                                                               The Address Resolution Protocol (ARP) is a network protocol
Server Code
                                                                               used to map IP addresses (logical addresses) to MAC
                                                                                                                                                  int main() {
                                                                               addresses (physical addresses) in a Local Area Network (LAN).
#include <stdio.h>                                                                                                                                   int fd = open("/dev/ttyS0", O_WRONLY);
#include <sys/socket.h>                                                                                                                              if (fd == -1) {
#include <netinet/in.h>                                                        Working:                                                                  perror("Error Opening Port");
#include <string.h>                                                                                                                                      return 1;
                                                                                                                                                     }
int main(){                                                                    When a device (Host A) wants to communicate with another
   int welcomeSocket, newSocket;                                               device (Host B) in the same network, it needs to know the MAC          char ch = 'A';
   char buffer[1024];                                                          address of Host B.                                                     char str[] = "Hello RS-232";
   struct sockaddr_in serverAddr;                                                                                                                     char filedata[1024] = "Sample file content\n";
   struct sockaddr_storage serverStorage;
   socklen_t addr_size;                                                        Host A checks its ARP cache.                                           write(fd, &ch, 1);
                                                                                                                                                      write(fd, str, strlen(str));
    welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);                                                                                                  write(fd, filedata, strlen(filedata));
                                                                               If the MAC address is not found, Host A sends a broadcast ARP
                                                                               request to all devices on the network.
    serverAddr.sin_family = AF_INET;                                                                                                                  close(fd);
    serverAddr.sin_port = htons(7891);                                                                                                                return 0;
    serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");                       The device with the matching IP address (Host B) replies with an   }
    memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);             ARP reply containing its MAC address.
                                                                                                                                                  Receiver Code
   bind(welcomeSocket, (struct sockaddr *) &serverAddr,                        Host A stores this address in its ARP cache for future use.
sizeof(serverAddr));                                                           How to See ARP Table:                                             #include <stdio.h>
                                                                                Type arp -a in the command prompt and press Enter. The            #include <fcntl.h>
    if(listen(welcomeSocket,5)==0)                                             resulting ARP table will be displayed containing the MAC           #include <unistd.h>
        printf("Listening\n");                                                 addresses.                                                         #include <string.h>
    else
        printf("Error\n");                                                                                                                        int main() {
                                                                               ___________________________________-_______                           int fd = open("/dev/ttyS0", O_RDONLY);
  addr_size = sizeof serverStorage;                                                                                                                  char ch;
  newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage,                                                                              char str[50], filedata[1024];
&addr_size);                                                                   7. To configure a router using the command line interface
                                                                               (CLI) and also observe the data gram formats in the Packet             read(fd, &ch, 1);
    strcpy(buffer,"Hello World\n");                                            Tracer simulator tool.                                                 read(fd, str, sizeof(str));
    send(newSocket,buffer,13,0);                                                                                                                      read(fd, filedata, sizeof(filedata));
    return 0;                                                                  Theory:                                                                printf("Received Character: %c\n", ch);
}                                                                                                                                                     printf("Received String: %s\n", str);
                                                                                                                                                      printf("Received File Data: %s\n", filedata);
                                                                               A router is a device that transfers data packets between two
/****************** CLIENT CODE ****************/                              different networks. A router joins two or more distinct networks       close(fd);
                                                                               by acting as a mediator device. Its function is called routing.        return 0;
#include <stdio.h>                                                                                                                                }
#include <sys/socket.h>
#include <netinet/in.h>                                                        Basic Router Configuration with Commands:                          Router R1 Configuration
#include <string.h>
                                                                               enable                                                            enable
int main(){                                                                    (Entering privileged EXEC mode)                                   configure terminal
   int clientSocket;                                                                                                                              hostname R1
   char buffer[1024];                                                          configure terminal                                                interface fastEthernet 0/0
   struct sockaddr_in serverAddr;                                              (Entering global configuration mode)                              ip address 192.168.1.1 255.255.255.0
   socklen_t addr_size;                                                                                                                           no shutdown
                                                                               hostname RouterA                                                  interface serial 0/0/0
    clientSocket = socket(PF_INET, SOCK_STREAM, 0);                            (Setting the hostname)                                            ip address 10.0.0.1 255.255.255.0
                                                                                                                                                  no shutdown
    serverAddr.sin_family = AF_INET;                                           interface FastEthernet 0/0                                        ip route 192.168.2.0 255.255.255.0 10.0.0.2
    serverAddr.sin_port = htons(7891);                                         (Entering interface configuration mode)
    serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");                                                                                          Router R2 Configuration
    memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);             ip address 192.168.1.1 255.255.255.0
                                                                               (Assigning IP address to the interface)                           enable
    addr_size = sizeof serverAddr;                                                                                                                configure terminal
    connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);         no shutdown                                                       hostname R2
                                                                               (Activating the interface)                                        interface fastEthernet 0/0
    recv(clientSocket, buffer, 1024, 0);                                                                                                          ip address 192.168.2.1 255.255.255.0
    printf("Data received: %s", buffer);                                                                                                          no shutdown
                                                                               exit
                                                                                                                                                  interface serial 0/0/0
                                                                               (Exiting interface configuration mode)
    return 0;                                                                                                                                     ip address 10.0.0.2 255.255.255.0
}                                                                                                                                                 no shutdown
                                                                               ip route 0.0.0.0 0.0.0.0 192.168.1.254                            ip route 192.168.1.0 255.255.255.0 10.0.0.1
                                                                               (Setting the default gateway route)
                                                                               write memory
                                                                               (Saving the configuration)
                                                                               PC Configuration:
                                                                               PC1 IP: 192.168.1.2/24
                                                                               Gateway: 192.168.1.1
                                                                               PC2 IP: 192.168.1.3/24
                                                                               Gateway: 192.168.1.1