/* RDT3.0 GBN实验 */
#include <stdio.h>
#include <string.h>
/* 可以调用的函数:
starttimer(),stoptimer():调用此函数启动和停止节点的计时器
tolayer3(calling_entity,packet):调用此函数,传入需要发送的packet,将数据包传递
至网络层。这之后的传输工作将由框架自动完成。
tolayer5(calling_entity,message):通过调用此函数,传入message,将消息传递至应用
层。这之后仿真框架将检查数据是否完好、是否按序到达。
*/
#define BIDIRECTIONAL 0 /* change to 1 if you're doing extra credit */
/* and write a routine called B_output */
/* msg 是从layer 5(老师的代码) 传给layer 4(你的代码) 的数据单元,
它包括需要从你的传输协议被发送到layer 5的数据。 */
struct msg
{
char data[20];
};
/* pkt是从layer 4 传给layer 3(老师的代码) 的数据单元. 注意预定义的packet结构,你的代码必须按此写。
*/
struct pkt
{
int seqnum;
int acknum;
int checksum;
char payload[20];
};
/********* 你要完成接下来的七个动作 *********/
/* A为发送端,B为接收端*/
/* 窗口中元素,用来缓存包和确认包是否已发送*/
struct send_window
{
struct pkt *pkt;
int recv_ack;
};
/* base 当前窗口头位置,base + win_size 窗口尾, nextseqnum 已经发送了的数据在窗口的位置*/
struct sender_info
{
int base;
int win_size;
int nextseqnum;
struct send_window sw[10];
};
int ComputeChecksum(struct pkt *packet)
{
int sum, i;
sum = packet->acknum + packet->seqnum;
for (i = 0; i < 20; i += 2) {
int t1 = (int) packet->payload[i];
int t2 = (int) packet->payload[i + 1];
sum += t1 + (t2 << 8);
}
while(sum>>16)
sum = (sum >> 16) + sum & 0xffff;
return ~sum;
}
int TestChecksum(struct pkt* packet) {
int sum = ComputeChecksum(packet);
return sum == packet->checksum;
};
#define A 0
#define B 1
/* 下面取消注释产生调试信息 */
// #define DEBUG
float interval = 30.0;
struct sender_info packetBufferA,packetBufferB;
/* 从layer 5 调用, 传数据给另一端*/
A_output(message) struct msg message;
{
if(packetBufferA.nextseqnum < packetBufferA.base + packetBufferA.win_size) {
#ifdef DEBUG
printf("A_output: send packet %d\n",packetBufferA.nextseqnum);
#endif
packetBufferA.sw[packetBufferA.nextseqnum % packetBufferA.win_size].recv_ack = 0;
struct pkt* packet = packetBufferA.sw[packetBufferA.nextseqnum % packetBufferA.win_size].pkt;
packet->seqnum = packetBufferA.nextseqnum;
strncpy(packet->payload,message.data,20);
packet->checksum = ComputeChecksum(packet);
tolayer3(A, *packet);
if(packetBufferA.base == packetBufferA.nextseqnum)
starttimer(A, interval);
packetBufferA.nextseqnum++;
}
}
B_output(message) /* need be completed only for extra credit */
struct msg message;
{
}
/* 当一个包到达layer 4, 从layer 3 调用*/
A_input(packet) struct pkt packet;
{
#ifdef DEBUG
printf("A_input: received a packet\n");
#endif
/* 如果包未出错则A窗口头右移*/
if(TestChecksum(&packet)) {
packetBufferA.base = packet.acknum + 1;
#ifdef DEBUG
printf("A_input: base move right\n");
#endif
// if(packetBufferA.base == packetBufferA.nextseqnum)
// stoptimer(A);
// else
// starttimer(A, interval);
stoptimer(A);
if(packetBufferA.base != packetBufferA.nextseqnum) {
starttimer(A, interval);
}
}
}
/* 超时 */
A_timerinterrupt()
{
/* 重传窗口内的全部数据包 */
#ifdef DEBUG
printf("Time out!\n");
#endif
starttimer(A, interval);
int i;
for(i = packetBufferA.base; i < packetBufferA.nextseqnum; i++) {
#ifdef DEBUG
printf("Time out: resend packet %d\n",i);
#endif
tolayer3(A, *packetBufferA.sw[i % packetBufferA.win_size].pkt);
packetBufferA.sw[i % packetBufferA.win_size].recv_ack = 0;
}
}
/* 初始化 */
A_init() {
packetBufferA.base = 0;
packetBufferA.win_size = 10;
packetBufferA.nextseqnum = 0;
int i;
for(i = 0; i < packetBufferA.win_size; i++) {
packetBufferA.sw[i].pkt = malloc(sizeof(struct pkt));
}
}
/* 不会有 B_output */
/* 当包到达layer 4的B时,从layer 3 调用*/
/* 接收端接收到包*/
B_input(packet) struct pkt packet;
{
#ifdef DEBUG
printf("b_input: received packet: %d expected:%d\n",packet.seqnum,packetBufferB.nextseqnum);
#endif
if(TestChecksum(&packet) && packet.seqnum == packetBufferB.nextseqnum) {
struct msg message;
strncpy(message.data, packet.payload, 20);
tolayer5(B, message);
packet.acknum = packet.seqnum;
packet.checksum = ComputeChecksum(&packet);
tolayer3(B, packet);
packetBufferB.nextseqnum ++;
}
}
/* called when B's timer goes off */
B_timerinterrupt()
{
}
/* the following rouytine will be called once (only) before any other */
/* entity B routines are called. You can use it to do any initialization */
B_init() {
packetBufferB.base = 0;
packetBufferB.win_size = 10;
packetBufferB.nextseqnum = 0;
packetBufferB.sw[0].pkt = malloc(sizeof(struct pkt));
}
/*****************************************************************
***************** NETWORK EMULATION CODE STARTS BELOW ***********
The code below emulates the layer 3 and below network environment:
- emulates the tranmission and delivery (possibly with bit-level corruption
and packet loss) of packets across the layer 3/4 interface
- handles the starting/stopping of a timer, and generates timer
interrupts (resulting in calling students timer handler).
- generates message to be sent (passed from later 5 to 4)
THERE IS NOT REASON THAT ANY STUDENT SHOULD HAVE TO READ OR UNDERSTAND
THE CODE BELOW. YOU SHOLD NOT TOUCH, OR REFERENCE (in your code) ANY
OF THE DATA STRUCTURES BELOW. If you're interested in how I designed
the emulator, you're welcome to look at the code - but again, you should have
to, and you defeinitely should not have to modify
******************************************************************/
struct event
{
float evtime; /* event time */
int evtype; /* event type code */
int eventity; /* entity where event occurs */
struct pkt *pktptr; /* ptr to packet (if any) assoc w/ this event */
struct event *prev;
struct event *next;
};
struct event *evlist = NULL; /* the event list */
/* possible events: */
#define TIMER_INTERRUPT 0
#define FROM_LAYER5 1
#define FROM_LAYER3 2
#define OFF 0
#define ON 1
#define A 0
#define B 1
int TRACE = 1; /* for my debugging */
int nsim = 0; /* number of messages from 5 to 4 so far */
int nsimmax = 0; /* number of msgs to generate, then stop */
float time = 0.000;
float lossprob; /* probability that a packet is dropped */
float corruptprob; /* probability that one bit is packet is flipped */
float lambda; /* arrival rate of messages from layer 5 */
int ntolayer3; /* number sent into layer 3 */
int nlost; /* number lost in media */
int ncorrupt; /* number corrupted by media*/
int recv_pkt_num = 0; /* 鎺ユ敹绔垚鍔熸敹鍒扮殑鍖呰鏁� */
float first_recv_time = 0.000; /* 鎺ユ敹绔渶寮€濮嬫垚鍔熸敹鍒板寘鐨勬椂闂� */
float last_recv_time = 0.000; /* 鎺ユ敹绔渶鍚庢垚鍔熸帴鏀跺埌鍖呯殑鏃堕棿 */
float throughput; /* 鎺ユ敹绔钩鍧囧悶鍚愮巼 */
main()
{
struct event *eventptr;
struct msg msg2give;
struct pkt pkt2give;
int i, j;
char c;
init();
A_init();
B_init();
while (1)
{
eventptr = evlist; /* get next event to simulate */
if (eventptr == NULL)
goto terminate;
evlist = evlist->next; /* remove this event from event list */
if (evlist != NULL)
evlist->prev = NULL;
// -------------------trace-----------------------
if (TRACE >= 2)
{
printf("\nEVENT time: %f,", eventptr->evtime);
printf(" type: %d", eventptr->evtype);
if (eventptr->evtype == 0)
printf(", timerinterrupt ");
else if (eventptr->evtype == 1)
printf(", fromlayer5 ");
else
printf(", fromlayer3 ");
printf(" entity: %d\n", eventptr->eventity);
}
// -------------------trace-----------------------
time = eventptr->evtime; /* update time to next event time */
if (nsim == nsimmax )
break; /* all done with simulation */
if (eventptr->evtype == FROM_LAYER5)
{
generate_next_arrival(); /* set up future arrival */
/* fill in msg to give with string of same letter */
j = nsim % 26;
for (i = 0; i < 20; i++) msg2give.data[i] = 97 + j;
// ----------------------------打印包中内容-----------------------------
if (TRACE > 2)
{
printf(" MAINLOOP: data given to student: ");
for (i = 0; i < 20; i++) printf("%c", msg2give.data[i]);
printf("\n");
}
// ----------------------------打印包中内容-----------------------------
nsim++;
if (eventptr->eventity == A)
A_output(msg2give);
else
B_output(msg2give);
}
else if (eventptr->evtype == FROM_LAYER3)
{
pkt2give.seqnum = eventptr->pktptr->seqnum;
pkt2give.acknum = eventptr->pktptr->acknum;
pkt2give.checksum = eventptr->pktptr->checksum;
for (i = 0; i < 20; i++)
pkt2give.payload[i] = eventptr->pktptr->payload[i];
if (eventptr->eventity == A) /* deliver packet by calling */
A_input(pkt2give); /* appropriate entity */
else
B_input(pkt2give);
free(eventptr->pktptr); /* free the memory for packet */
}
else if (eventptr->evtype == TIMER_INTERRUPT)
{
if (eventptr->eventity == A)
A_timerinterrupt();
else
B_timerinterrupt();
}
else
{
printf("INTERNAL PANIC: unknown event type \n");
}
free(eventptr);
}
terminate:
throughput = recv_pkt_num / time;
printf(" Simulator terminated at time %f\n after sending %d msgs from layer5\n B receives %d data and throughput is %f", time, nsim, recv_pkt_num, throughput);
while(1);
}
init() /* initialize the simulator */
{
int i;
float sum, avg;
float jimsrand();
printf("----- Stop and Wait Network Simulator Version 1.1 -------- \n\n");
printf("Enter the number of messages to simulate: ");
scanf("%d", &nsimmax);
printf("Enter packet loss probability [enter 0.0 for no loss]:");
scanf("%f", &lossprob);
printf("Enter packet corruption probability [0.0 for no corruption]:");
scanf("%f", &corruptprob);
printf("Enter average time between messages from sender's layer5 [ > 0.0]:");
scanf("%f", &lambda);
printf("Enter TRACE:");
scanf("%d", &TRACE);
srand(9999); /* init random number generator */
sum = 0.0; /* test random number generator for students */
for (i = 0; i < 1000; i++)
sum = sum + jimsrand(); /* jimsrand() should be uniform in [0,1] */
avg = sum / 1000.0;
if (avg < 0.25 || avg > 0.75)
{
printf("It is likely that random number generation on your machine\n");
printf("is different from what this emulator expects. Please take\n");
printf("a look at the routine jimsrand() in the emulator code. Sorry. \n");
exit(-1);
}
ntolayer3 = 0;
nlost = 0;
ncorrupt = 0;
time = 0.0; /* initialize time to 0.0 */
generate_next_arrival(); /* initialize event list */
}
/****************************************************************************/
/* jimsrand(): return a float in range [0,1]. The routine below is used to */
/* isolate all random number generation in one location. We assume that the*/
/* system-supplied rand() function return an int in therange [0,mmm] */
/****************************************************************************/
float jimsrand()
{
double mmm = 32767; /* largest int - MACHINE DEPENDENT!!!!!!!! */
float x; /* individual students may need to change mmm */
x = rand() / mmm; /* x should be uniform in [0,1] */
return (x);
}
/********************* EVENT HANDLINE ROUTINES *******/
/* The next set of routines handle the event list */
/*****************************************************/
generate_next_arrival()
{
double x, log(), ceil();
struct event *evptr;
char *malloc();
float ttime;
int tempint;
if (TRACE > 2)
printf(" GENERATE NEXT ARRIVAL: creating new arrival\n");
// x = lambda*jimsrand()*2; /* x is uniform on [0,2*lambda] */
/* having mean of lambda */
x = lambda;
evptr = (struct event *)malloc(sizeof(struct event));
evptr->evtime = time + x;
evptr->evtype = FROM_LAYER5;
if (BIDIRECTIONAL && (jimsrand() > 0.5))
evptr->eventity = B;
else
evptr->eventity = A;
insertevent(evptr);
}
insertevent(p) struct event *p;
{
struct event *q, *qold;
if (TRACE > 2)
{
printf(" INSERTEVENT: time is %lf\n", time);
printf(" INSERTEVENT: future time will be %lf\n", p->evtime);
}
q = evlist; /* q points to header of list in which p struct inserted */
if (q == NULL)
{ /* list is empty */
evlist = p;
p->next = NULL;
p->prev = NULL;
}
else
{
for (qold = q; q != NULL && p->evtime > q->evtime; q = q->next)
qold = q;
if (q == NULL)
{ /* end of list */
qold->next = p;
p->prev = qold;
p->next = NULL;
}
else if (q == evlist)
{ /* front of list */
p->next = evlist;
p->prev = NULL;
p->next->prev = p;
evlist = p;
}
else
{ /* middle of list */
p->next = q;
p->prev = q->prev;
q->prev->next = p;
q->prev = p;
}
}
}
printevlist()
{
struct event *q;
int i;
printf("--------------\nEvent List Follows:\n");
for (q = evlist; q != NULL; q = q->next)
{
printf("Event time: %f, type: %d entity: %d\n", q->evtime, q->evtype, q->eventity);
}
printf("--------------\n");
}
/********************** Student-callable ROUTINES ***********************/
/* called by students routine to cancel a previously-started timer */
stoptimer(AorB) int AorB; /* A or B is trying to stop timer */
{
struct event *q, *qold;
if (TRACE > 2)
printf(" STOP TIMER: stopping timer at %f\n", time);
/* for (q=evlist; q!=NULL && q->next!=NULL; q = q->next) */
for (q = evlist; q != NULL; q = q->next)
if ((q->evtype == TIMER_INTERRUPT && q->eventity == AorB))
{
/* remove this event */
if (q->next == NULL && q->prev == NULL)
evlist = NULL; /* remove first and only event on list */
else if (q->next == NULL) /* end of list - there is one in front */
q->prev->next = NULL;
else if (q == evlist)
{ /* front of list - there must be event after */
q->next->prev = NULL;
evlist = q->next;
}
else
{ /* middle of list */
q->next->prev = q->prev;
q->prev->next = q->next;
}
free(q);
return;
}
printf("Warning: unable to cancel your timer. It wasn't running.\n");
}
starttimer(AorB, increment) int AorB; /* A or B is trying to stop timer */
float increment;
{
struct event *q;
struct event *evptr;
char *malloc();
if (TRACE > 2)
printf(" START TIMER: starting timer at %f\n", time);
/* be nice: check to see if timer is already started, if so, then warn */
/* for (q=evlist; q!=NULL && q->next!=NULL; q = q->next) */
for (q = evlist; q != NULL; q = q->next)
if ((q->evtype == TIMER_INTERRUPT && q->eventity == AorB))
{
printf("Warning: attempt to start a timer that is already started\n");
return;
}
/* create future event for when timer goes off */
evptr = (struct event *)malloc(sizeof(struct event));
evptr->evtime = time + increment;
evptr->evtype = TIMER_INTERRUPT;
evptr->eventity = AorB;
insertevent(evptr);
}
/************************** TOLAYER3 ***************/
tolayer3(AorB, packet) int AorB; /* A or B is trying to stop timer */
struct pkt packet;
{
struct pkt *mypktptr;
struct event *evptr, *q;
char *malloc();
float lastime, x, jimsrand();
int i;
ntolayer3++;
/* simulate losses: */
if (jimsrand() < lossprob)
{
nlost++;
if (TRACE > 0)
printf(" TOLAYER3: packet being lost\n");
return;
}
/* make a copy of the packet student just gave me since he/she may decide */
/* to do something with the packet after we return back to him/her */
mypktptr = (struct pkt *)malloc(sizeof(struct pkt));
mypktptr->seqnum = packet.seqnum;
mypktptr->acknum = packet.acknum;
mypktptr->checksum = packet.checksum;
for (i = 0; i < 20; i++)
mypktptr->payload[i] = packet.payload[i];
if (TRACE > 2)
{
printf(" TOLAYER3: seq: %d, ack %d, check: %d ", mypktptr->seqnum,
mypktptr->acknum, mypktptr->checksum);
for (i = 0; i < 20; i++)
printf("%c", mypktptr->payload[i]);
printf("\n");
}
/* create future event for arrival of packet at the other side */
evptr = (struct event *)malloc(sizeof(struct event));
evptr->evtype = FROM_LAYER3; /* packet will pop out from layer3 */
evptr->eventity = (AorB + 1) % 2; /* event occurs at other entity */
evptr->pktptr = mypktptr; /* save ptr to my copy of packet */
/* finally, compute the arrival time of packet at the other end.
medium can not reorder, so make sure packet arrives between 1 and 10
time units after the latest arrival time of packets
currently in the medium on their way to the destination */
lastime = time;
/* for (q=evlist; q!=NULL && q->next!=NULL; q = q->next) */
for (q = evlist; q != NULL; q = q->next)
if ((q->evtype == FROM_LAYER3 && q->eventity == evptr->eventity))
lastime = q->evtime;
evptr->evtime = lastime + 1 + 9 * jimsrand();
// evptr->evtime = lastime + 5;
/* simulate corruption: */
if (jimsrand() < corruptprob)
{
ncorrupt++;
if ((x = jimsrand()) < .75)
mypktptr->payload[0] = 'Z'; /* corrupt payload */
else if (x < .875)
mypktptr->seqnum = 999999;
else
mypktptr->acknum = 999999;
if (TRACE > 0)
printf(" TOLAYER3: packet being corrupted\n");
}
if (TRACE > 2)
printf(" TOLAYER3: scheduling arrival on other side\n");
insertevent(evptr);
}
tolayer5(AorB, datasent) int AorB;
char datasent[20];
{
int i;
if (TRACE > 2)
{
printf(" TOLAYER5: data received: ");
for (i = 0; i < 20; i++)
printf("%c", datasent[i]);
printf("\n");
}
if (AorB == B)
{
recv_pkt_num++;
if (recv_pkt_num == 1)
{
first_recv_time = time;
}
last_recv_time = time;
}
}
计网 RDT3.0 GBN实验(带注释)
于 2023-04-19 13:19:38 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)