Question: This assignment will simulate the layered structure of the computer networks. We will implement a simplified network that consists of two hosts with each host
This assignment will simulate the layered structure of the computer networks. We will implement a simplified network that consists of two hosts with each host having just two layers, the data link layer and the network layer. The two hosts are connected by a physical wire.
You will need to develop three programs: physical_wire.c, data_link_layer.c, and network_layer.c. You are given partial codes for the three programs. You are also given a header file (structs.h) that defines the frame and packet structures. Following are the algorithms for the three programs.


C. Usage
physical_wire.exe wire_port
wire_port is the port the physical wire will listen to. It will accept two connection requests from data link layers through this port.
data_link_layer.exe wire_addr wire_port data_port
wire_addr is the IP address (or name) of the machine on which physical_wire.exe is running,
wire_port is the port number that physical_wire listens to,
data_prot is the port number that data link layer will listen to. It will accept one connection request from network layer through this port.
network_layer.exe data_addr data_port nickname
data_addr is the IP address of the machine on which data link layer (of this host) is running.
data_port is the port number that the data link layer (of this host) listens to.
Nickname is the name we assign to this host.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
datalink layer.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "structs.h"
void error(const char *msg)
{
perror(msg);
exit(1);
}
/*global variables to be used in the threads*/
int network_layersockfd;//the socket through which the network layer is connected.
int wiresockfd; //the socket through which the wire is connected.
/*the thread function that receives frames from the wiresocket and sends packets to the network_layer */
void * rcvfromwiresend2network_layer ( char *argv[] )
{
/*add codes to declare locals*/
...
while (1)
{
/*add codes receive a frame from wire*/
...
/*add codes to send the included packet to the network layer*/
...
}
}
int main(int argc, char *argv[])
{
/*add codes to declear local variables*/
...
/*check numeber of arguments*/
if (argc
fprintf(stderr,"Usage: %s wire__IP wire_port data_port ",argv[0] );
exit(1);
}
/* add codes to connect to the wire. Assign value to gobal varialbe wiresockfd */
...
/*generate a new thread to receive frames from the wire and pass packets to the network layer */
pthread_t wirepth; // this is our thread identifier
pthread_create(&wirepth,NULL,rcvfromwiresend2network_layer, NULL);
/*add codes to create and listen to a socket that the network_layer will connect to. Assign value to global variable network_layersockfd*/
...
/*the main function will receive packets from the network layer and pass frames to wire*/
while (1)
{
/*add codes to receive a packet from the network layer*/
...
/* add codes to wrap the packet into a frame*/
...
/*add codes to send the frame to the wire*/
...
/*if the message is "EXIT" */
if (strcmp (incoming_packet.message, "EXIT ")==0)
{
pthread_cancel(wirepth); //kill the child thread
close(wiresockfd);
close (network_layersockfd); //close sockets
return 0; //terminate the main function
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
network layer.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "structs.h"
void error(const char *msg)
{
perror(msg);
exit(0);
}
/*the thread function to receive packets from the data link layer and display the messages*/
void * rcvmsg (int threadsockfd)
{
/*add codes for local varialbes*/
while (1)
{
/*add codes to read a packet from threadsockfd and display it to the screen*/
...
}
return NULL;
}
int main(int argc, char *argv[])
{
/*add codes for local variables*/
...
/*check number of aruguments*/
if (argc
fprintf(stderr,"usage %s data_add data_port nickname ", argv[0]);
exit(0);
}
/*add codes to connect to the data link layer*/
...
/*creat a thread to receive packets from the data link layer*/
pthread_t pth; // this is our thread identifier
pthread_create(&pth,NULL,rcvmsg,sockfd);
/* the main function will receive messages from keyboard and send packets to the data link layer*/
while (1)
{
/*add codes to receive a message from keyboard, wrap it into a packet and send it to the data link layer*/
...
/*if the meassge is "EXIT"*/
if (strcmp (buffer, "EXIT ")==0)
{
pthread_cancel(pth); //kill the child thread
close(sockfd); // close socket
return 0; //terminate
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
physical wire.c
#include
#include
#include
#include
#include
#include
#include
#include
#include "structs.h"
void error(const char *msg)
{
perror(msg);
exit(1);
}
/*Global variables that will be accessed in the thread function */
int clientlist[2]; /*the socket numbers through with the 2 clients (e.g. data link layer) are connected to this wire*/
/*the thread function that will receive frames from socket (i.e. data_link layer) and send the received frames to another socket*/
void * onesocket ( int threadsockfd)
{
/*add codes to declear local variables*/
...
while (1)
{
/*add codes to receive a frame from threadsocketfd*/
...
/*if the message in the frame in EXIT close the socket and terminate this thread using
close(threadsockfd);
return NULL;
other wise send the frame to the other socket (which is stored in clientlist)
*/
...
}
}
int main(int argc, char *argv[])
{
/*add codes to declear local variables*/
...
/*check the number of arguments*/
if (argc
fprintf(stderr,"ERROR, no port provided ");
exit(1);
}
/*add codes to create a socket (sockfd), bind its address to it and listen to it*/
...
for (i=0;i
{
/*accept a request from the data link layer*/
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
..
/* store the new socket into clientlist*/
clientlist[i]=newsockfd;
...
/*creat a thread to take care of the new connection*/
pthread_t pth; /* this is the thread identifier*/
pthread_create(&pth,NULL,onesocket,clientlist[i]);
threadlist[i]=pth; /*save the thread identifier into an array*/
}
close(sockfd); /*so that wire will not accept further connection request*/
pthread_join(threadlist[0],NULL);
pthread_join(threadlist[1],NULL); /* the main function will not terminated untill both threads finished*/
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
structs.h
typedef struct packet_type {
char nickname[10];
char message[256];
}packet;
typedef struct frame_type {
int seq_num;
int type; // 1 for acknowledgement; 0 for data
packet my_packet;
}frame;
physical_wire.c Create a socket, bind its address to the socket, and listen to it. Accept two connection requests. Generate a thread for each connection. (The thread will reads frames from a socket and send them to the other socket. If the frame contains "EXIT" then the socket is closed and the thread is terminated.) The main function terminates after the two threads terminate data_link layer.c Create a socket and use it to connect to the phvsical wire Create a socket, bind its address to it, and listen to it. Accept one connection request from the network layer. physical_wire.c Create a socket, bind its address to the socket, and listen to it. Accept two connection requests. Generate a thread for each connection. (The thread will reads frames from a socket and send them to the other socket. If the frame contains "EXIT" then the socket is closed and the thread is terminated.) The main function terminates after the two threads terminate data_link layer.c Create a socket and use it to connect to the phvsical wire Create a socket, bind its address to it, and listen to it. Accept one connection request from the network layer
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
