Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The data link layer will read bytes from each of the input files, and separate t

ID: 3576960 • Letter: T

Question

The data link layer will read bytes from each of the input files, and separate the bytes into messages. Each message is then given to the network layer.

The network layer will determine if this node is the destination of the message. If it is, it gives the message to the transport layer. If it is not, it gives the message to the link layer to be forwarded to the channel towards the destination (the destination may be multiple hops away)

The network layer will also perform routing.

The transport layer will do two things:

Send the string given in the argument to the appropriate destination (by breaking it up and giving it to the network layer)

Receive messages from the network layer: only those messages that are, of course, addressed to this node

Output to a file called "nodeXreceived" where X is the node id (0 .. 9). The contents of this file should look like this for node 2 in the example above:

From 0 received: this is a message from 0

From 1 received: this is a message from 1

That is, it should contain one line for every message received, it should say from which node the message originated and what was the contents of the message.

The datalink layer has two subroutines (the subroutines don't have to have exactly the same parameters, you can add more or change them if you want, they are just a guideline)

datalink_receive_from_network(char * msg, int len, char next_hop)  
This function will be called by the network layer to give a network layer message to the datalink layer. The network layer message is pointed to by char * msg, and the length of the message is integer len. If the network layer is sending a data message, then argument next_hop  is the id of the neighboring node that should receive this message. This routine will output to the output channel (text file) the message given by the network layer, with stuffing and parity, as described above.

How do I go about setting up the datalink, transport and network layers. Or help me set up the most important layer. Here is my code thus far.

Here is my code so far.

Explanation / Answer

Transport Layer (TCP Sever)

#include <stdio.h>

#include <sys/socket.h>

#include <sys/types.h>

#include <arpa/inet.h>

#include <string.h>

int main(void)

{

int s, len;

char buf[BUFSIZ];

struct sockaddr_in remote_addr;

memset(&remote_addr, 0, sizeof(remote_addr));

remote_addr.sin_family = AF_INET;

remote_addr.sin_addr.s_addr = inet_addr(“127.0.0.1”);

remote_addr.sin_port = htons(8000);

if( (s = socket(AF_INET, SOCK_STREAM, 0)) < 0)

{

printf(“Error ”);

return -1;

}

if( connect(s, (struct sockaddr*)&remote_addr, sizeof(struct sockaddr)) < 0 )

{

printf(“Error ”);

return -1;

}

printf(“Connection established ”);

len = recv(s, buf, BUFSIZ, 0);

buf[len] = ”;

printf(“%s ”, buf);

while(1)

{

printf(“Enter text: “);

scanf(“%s%”, buf);

if( !strcmp(buf, “quit”) )

break;

len = send(s, buf, strlen(buf), 0);

len = recv(s, buf, BUFSIZ, 0);

buf[len] = ”;

printf(“From server: %s ”, buf);

}

close(s);

return 0;

}

TCP Client:

#include <stdio.h>

#include <sys/socket.h>

#include <sys/types.h>

#include <arpa/inet.h>

#include <string.h>

int main(void)

{

        char buf[BUFSIZ], ubuf[BUFSIZ];

        int s, fd, len;

        int sin_size;

        struct sockaddr_in my_addr;

        struct sockaddr_in remote_addr;

        memset(&my_addr, 0, sizeof(my_addr));

        my_addr.sin_family = AF_INET;

        my_addr.sin_addr.s_addr = INADDR_ANY;

        my_addr.sin_port = htons(8000);

        if( (s = socket(AF_INET, SOCK_STREAM, 0)) < 0)

        {

               printf("Error ");

               return -1;

        }

        if( bind(s, (struct sockaddr*)&my_addr, sizeof(struct sockaddr)) < 0)

        {

               printf("Error ");

               return -1;

        }

        listen(s, 6);

        sin_size = sizeof(struct sockaddr_in);

        if( (fd = accept(s, (struct sockaddr*)&remote_addr, &sin_size)) < 0 )

        {

               buf[len] = '';

               buf[0] = toupper(buf[0]);

               send(fd, buf, len, 0);

        }

        close(fd);

        close(s);

        return 0;

}

Data Link Layer framing methods such as Bit Stuffing.

Security and Error detection are the most prominent features that are to be provided by any application which transfers data from one end to the other end. One of such a mechanism in tracking errors which may add up to the original data during transfer is known as Stuffing. It is of two types namely Bit Stuffing and the other Character Stuffing. Coming to the Bit Stuffing, 01111110 is appended within the original data while transfer of it. The following program describes how it is stuffed at the sender end and de-stuffed at the receiver end.

#include
main()
{
int a[15];
int i,j,k,n,c=0,pos=0;
clrscr();
printf(" Enter the number of bits");
scanf("%d",&n);
printf(" Enter the bits");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]==1)
{
c++;
if(c==5)
{
pos=i+1;
c=0;
for(j=n;j>=pos;j--)
{
k=j+1;
a[k]=a[j];
}
a[pos]=0;
n=n+1;
}
}
else
c=0;
}

printf(" DATA AFTER STUFFING ");
printf(" 01111110 ");

for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
printf(" 01111110 ");
getch();
}

Output:</n;i++)
</n;i++)
</n;i++)

Character Stuffing

Coming to the Character Stuffing, DLESTX and DLEETX are used to denote start and end of character data with some constraints imposed on repetition of charaters as shown in the program below clearly.

Program:

#include
#include
#include
void charc(void);
void main()
{
int choice;
while(1)
{
printf(" 1.character stuffing");
printf(" 2.exit");
printf(" enter choice");
scanf("%d",&choice);
printf("%d",choice);
if(choice>2)
printf(" invalid option....please renter");
switch(choice)
{
case 1:
charc();
break;
case 2:
exit(0);
}
}
}
void charc(void)
{
char c[50],d[50],t[50];
int i,m,j;
clrscr();
printf("enter the number of characters ");
scanf("%d",&m);
printf(" enter the characters ");
for(i=0;i<m+1;i++)
{
scanf("%c",&c[i]);
}
printf(" original data ");
for(i=0;i<m+1;i++)
printf("%c",c[i]);
d[0]='d';
d[1]='l';
d[2]='e';
d[3]='s';
d[4]='t';
d[5]='x';
for(i=0,j=6;i<m+1;i++,j++)
{
if((c[i]=='d'&&c[i+1]=='l'&& c[i+2]=='e'))
{
d[j]='d';
j++;
d[j]='l';
j++;
d[j]='e';
j++;
m=m+3;
}
d[j]=c[i];
}
m=m+6;
m++;
d[m]='d';
m++;
d[m]='l';
m++;
d[m]='e';
m++;
d[m]='e';
m++;
d[m]='t';
m++;
d[m]='x';
m++;
printf(" transmitted data: ");
for(i=0;i<m;i++)
{
printf("%c",d[i]);
}
for(i=6,j=0;i<m-6;i++,j++)
{
if(d[i]=='d'&&d[i+1]=='l'&&d[i+2]=='e'&&d[i+3]=='d'&&d[i+4]=='l'&&d[i+5]=='e')
i=i+3;
t[j]=d[i];
}
printf(" received data:");
for(i=0;i<j;i++)
{printf("%c",t[i]);
}
}</j;i++)
</m-6;i++,j++)
</m;i++)
</m+1;i++,j++)
</m+1;i++)
</m+1;i++)

Distance Vector Routing

#include<stdio.h>

struct node { unsigned dist[20];

unsigned from[20]; }rt[10];

int main()

{

int dmat[20][20];

int n,i,j,k,count=0;

printf(" Enter the number of nodes : ");
scanf("%d",&n);

printf("Enter the cost matrix : ");

for(i=0;i<n;i++) for(j=0;j<n;j++)

{

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

dmat[i][i]=0;

rt[i].dist[j]=dmat[i][j];

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

}

Do

{

count=0;

for(i=0;i<n;i++)

for(j=0;j<n;j++)

for(k=0;k<n;k++)

if(rt[i].dist[j]>dmat[i][k]+rt[k].dist[j])

{

rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j]; rt[i].from[j]=k; count++;

}

}

while(count!=0);

for(i=0;i<n;i++)

{

printf(" State value for router %d is ",i+1);

for(j=0;j<n;j++)

{

printf(" node %d via %d Distance%d",j+1,rt[i].from[j]+1,rt[i].dist[j]);

}
} printf(" "); }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote