prog2.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "prog2.h"
  4. /* ******************************************************************
  5. ALTERNATING BIT AND GO-BACK-N NETWORK EMULATOR: VERSION 1.1 J.F.Kurose
  6. This code should be used for PA2, unidirectional or bidirectional
  7. data transfer protocols (from A to B. Bidirectional transfer of data
  8. is for extra credit and is not required). Network properties:
  9. - one way network delay averages five time units (longer if there
  10. are other messages in the channel for GBN), but can be larger
  11. - packets can be corrupted (either the header or the data portion)
  12. or lost, according to user-defined probabilities
  13. - packets will be delivered in the order in which they were sent
  14. (although some can be lost).
  15. **********************************************************************/
  16. /*****************************************************************
  17. ***************** NETWORK EMULATION CODE STARTS BELOW ***********
  18. The code below emulates the layer 3 and below network environment:
  19. - emulates the tranmission and delivery (possibly with bit-level corruption
  20. and packet loss) of packets across the layer 3/4 interface
  21. - handles the starting/stopping of a timer, and generates timer
  22. interrupts (resulting in calling students timer handler).
  23. - generates message to be sent (passed from later 5 to 4)
  24. THERE IS NOT REASON THAT ANY STUDENT SHOULD HAVE TO READ OR UNDERSTAND
  25. THE CODE BELOW. YOU SHOLD NOT TOUCH, OR REFERENCE (in your code) ANY
  26. OF THE DATA STRUCTURES BELOW. If you're interested in how I designed
  27. the emulator, you're welcome to look at the code - but again, you should have
  28. to, and you defeinitely should not have to modify
  29. ******************************************************************/
  30. event_t *evlist = NULL; /* the event list */
  31. int TRACE = 1; /* for my debugging */
  32. int nsim = 0; /* number of messages from 5 to 4 so far */
  33. int nsimmax = 0; /* number of msgs to generate, then stop */
  34. float time = 0.000;
  35. float lossprob; /* probability that a packet is dropped */
  36. float corruptprob; /* probability that one bit is packet is flipped */
  37. float lambda; /* arrival rate of messages from layer 5 */
  38. int ntolayer3; /* number sent into layer 3 */
  39. int nlost; /* number lost in media */
  40. int ncorrupt; /* number corrupted by media*/
  41. int main() {
  42. event_t *eventptr;
  43. msg_t msg2give;
  44. pkt_t pkt2give;
  45. int i,j;
  46. init();
  47. A_init();
  48. B_init();
  49. while (1) {
  50. /* get next event to simulate */
  51. eventptr = evlist;
  52. if ( eventptr == NULL )
  53. goto terminate;
  54. /* remove this event from event list */
  55. evlist = evlist->next;
  56. if (evlist != NULL)
  57. evlist->prev=NULL;
  58. if ( TRACE >= 2 ) {
  59. printf("\nEVENT time: %f,",eventptr->evtime);
  60. printf(" type: %d",eventptr->evtype);
  61. if ( eventptr->evtype == 0 )
  62. printf(", timerinterrupt ");
  63. else if ( eventptr->evtype == 1 )
  64. printf(", fromlayer5 ");
  65. else
  66. printf(", fromlayer3 ");
  67. printf(" entity: %d\n",eventptr->eventity);
  68. }
  69. /* update time to next event time */
  70. time = eventptr->evtime;
  71. if ( nsim == nsimmax )
  72. break; /* all done with simulation */
  73. if ( eventptr->evtype == FROM_LAYER5 ) {
  74. /* set up future arrival */
  75. generate_next_arrival();
  76. /* fill in msg to give with string of same letter */
  77. j = nsim % 26;
  78. for ( i = 0; i < 20; i++ )
  79. msg2give.data[i] = 97 + j;
  80. if ( TRACE > 2 ) {
  81. printf(" MAINLOOP: data given to student: ");
  82. for ( i = 0; i < 20; i++ )
  83. printf("%c", msg2give.data[i]);
  84. printf("\n");
  85. }
  86. nsim++;
  87. if ( eventptr->eventity == A )
  88. A_output(msg2give);
  89. else
  90. B_output(msg2give);
  91. } else if ( eventptr->evtype == FROM_LAYER3 ) {
  92. pkt2give.seqnum = eventptr->pktptr->seqnum;
  93. pkt2give.acknum = eventptr->pktptr->acknum;
  94. pkt2give.checksum = eventptr->pktptr->checksum;
  95. for (i=0; i<20; i++)
  96. pkt2give.payload[i] = eventptr->pktptr->payload[i];
  97. if ( eventptr->eventity == A ) /* deliver packet by calling */
  98. A_input(pkt2give); /* appropriate entity */
  99. else
  100. B_input(pkt2give);
  101. free( eventptr->pktptr ); /* free the memory for packet */
  102. } else if ( eventptr->evtype == TIMER_INTERRUPT ) {
  103. if ( eventptr->eventity == A )
  104. A_timerinterrupt();
  105. else
  106. B_timerinterrupt();
  107. } else
  108. printf("INTERNAL PANIC: unknown event type \n");
  109. free(eventptr);
  110. }
  111. terminate:
  112. printf(" Simulator terminated at time %f\n after sending %d msgs from layer5\n",time,nsim);
  113. return EXIT_SUCCESS;
  114. }
  115. /* initialize the simulator */
  116. void init() {
  117. int i;
  118. float sum, avg;
  119. float jimsrand();
  120. printf("----- Stop and Wait Network Simulator Version 1.1 -------- \n\n");
  121. printf("Enter the number of messages to simulate: ");
  122. scanf("%d",&nsimmax);
  123. printf("Enter packet loss probability [enter 0.0 for no loss]:");
  124. scanf("%f",&lossprob);
  125. printf("Enter packet corruption probability [0.0 for no corruption]:");
  126. scanf("%f",&corruptprob);
  127. printf("Enter average time between messages from sender's layer5 [ > 0.0]:");
  128. scanf("%f",&lambda);
  129. printf("Enter TRACE:");
  130. scanf("%d",&TRACE);
  131. /* init random number generator */
  132. srand(9999);
  133. /* test random number generator for students */
  134. sum = 0.0;
  135. /* jimsrand() should be uniform in [0,1] */
  136. for ( i = 0; i < 1000; i++ )
  137. sum = sum + jimsrand();
  138. avg = sum / 1000.0;
  139. if ( avg < 0.25 || avg > 0.75 ) {
  140. printf("It is likely that random number generation on your machine\n" );
  141. printf("is different from what this emulator expects. Please take\n");
  142. printf("a look at the routine jimsrand() in the emulator code. Sorry. \n");
  143. exit(1);
  144. }
  145. ntolayer3 = 0;
  146. nlost = 0;
  147. ncorrupt = 0;
  148. /* initialize time to 0.0 */
  149. time=0.0;
  150. /* initialize event list */
  151. generate_next_arrival();
  152. }
  153. /****************************************************************************/
  154. /* jimsrand(): return a float in range [0,1]. The routine below is used to */
  155. /* isolate all random number generation in one location. We assume that the*/
  156. /* system-supplied rand() function return an int in therange [0,mmm] */
  157. /****************************************************************************/
  158. float jimsrand() {
  159. /* largest int - MACHINE DEPENDENT!!!!!!!! */
  160. double mmm = 2147483647;
  161. /* individual students may need to change mmm */
  162. float x;
  163. /* x should be uniform in [0,1] */
  164. x = rand() / mmm;
  165. return(x);
  166. }
  167. /********************* EVENT HANDLINE ROUTINES *******/
  168. /* The next set of routines handle the event list */
  169. /*****************************************************/
  170. void generate_next_arrival()
  171. {
  172. double x, log(), ceil();
  173. event_t *evptr;
  174. if ( TRACE > 2 )
  175. printf(" GENERATE NEXT ARRIVAL: creating new arrival\n");
  176. /* x is uniform on [0,2*lambda] */
  177. x = lambda * jimsrand()*2;
  178. /* having mean of lambda */
  179. evptr = (event_t *)malloc(sizeof(event_t));
  180. evptr->evtime = time + x;
  181. evptr->evtype = FROM_LAYER5;
  182. if (BIDIRECTIONAL && (jimsrand()>0.5) )
  183. evptr->eventity = B;
  184. else
  185. evptr->eventity = A;
  186. insertevent(evptr);
  187. }
  188. void insertevent( event_t *p ) {
  189. event_t *q,*qold;
  190. if ( TRACE > 2 ) {
  191. printf(" INSERTEVENT: time is %f\n",time);
  192. printf(" INSERTEVENT: future time will be %f\n",p->evtime);
  193. }
  194. /* q points to header of list in which p struct inserted */
  195. q = evlist;
  196. if ( q == NULL ) {
  197. /* list is empty */
  198. evlist=p;
  199. p->next=NULL;
  200. p->prev=NULL;
  201. } else {
  202. for ( qold = q; q != NULL && p->evtime > q->evtime; q = q->next )
  203. qold=q;
  204. if ( q == NULL ) {
  205. /* end of list */
  206. qold->next = p;
  207. p->prev = qold;
  208. p->next = NULL;
  209. } else if ( q == evlist ) {
  210. /* front of list */
  211. p->next=evlist;
  212. p->prev=NULL;
  213. p->next->prev=p;
  214. evlist = p;
  215. } else {
  216. /* middle of list */
  217. p->next=q;
  218. p->prev=q->prev;
  219. q->prev->next=p;
  220. q->prev=p;
  221. }
  222. }
  223. }
  224. void printevlist()
  225. {
  226. event_t *q;
  227. printf("--------------\nEvent List Follows:\n");
  228. for(q = evlist; q!=NULL; q=q->next)
  229. printf("Event time: %f, type: %d entity: %d\n",q->evtime,q->evtype,q->eventity);
  230. printf("--------------\n");
  231. }
  232. /********************** Student-callable ROUTINES ***********************/
  233. /* called by students routine to cancel a previously-started timer */
  234. void stoptimer(int AorB) {
  235. event_t *q;
  236. if ( TRACE > 2 )
  237. printf(" STOP TIMER: stopping timer at %f\n",time);
  238. /* for (q=evlist; q!=NULL && q->next!=NULL; q = q->next) */
  239. for ( q = evlist; q != NULL; q = q->next )
  240. if ( ( q->evtype == TIMER_INTERRUPT && q->eventity == AorB ) ) {
  241. /* remove this event */
  242. if (q->next==NULL && q->prev==NULL) {
  243. /* remove first and only event on list */
  244. evlist = NULL;
  245. } else if ( q->next == NULL ) {
  246. /* end of list - there is one in front */
  247. q->prev->next = NULL;
  248. } else if ( q== evlist ) {
  249. /* front of list - there must be event after */
  250. q->next->prev = NULL;
  251. evlist = q->next;
  252. } else {
  253. /* middle of list */
  254. q->next->prev = q->prev;
  255. q->prev->next = q->next;
  256. }
  257. free(q);
  258. return;
  259. }
  260. printf("Warning: unable to cancel your timer. It wasn't running.\n");
  261. }
  262. void starttimer( int AorB, float increment ) {
  263. event_t *q;
  264. event_t *evptr;
  265. if ( TRACE > 2 )
  266. printf(" START TIMER: starting timer at %f\n",time);
  267. /* be nice: check to see if timer is already started, if so, then warn */
  268. /* for (q=evlist; q!=NULL && q->next!=NULL; q = q->next) */
  269. for ( q = evlist; q != NULL; q = q->next )
  270. if ( ( q->evtype == TIMER_INTERRUPT && q->eventity == AorB ) ) {
  271. printf("Warning: attempt to start a timer that is already started\n");
  272. return;
  273. }
  274. /* create future event for when timer goes off */
  275. evptr = (event_t *) malloc( sizeof( event_t ) );
  276. evptr->evtime = time + increment;
  277. evptr->evtype = TIMER_INTERRUPT;
  278. evptr->eventity = AorB;
  279. insertevent(evptr);
  280. }
  281. /************************** TOLAYER3 ***************/
  282. /* A or B is trying to stop timer*/
  283. void tolayer3( int AorB, pkt_t packet ) {
  284. pkt_t *mypktptr;
  285. event_t *evptr,*q;
  286. float lastime, x, jimsrand();
  287. int i;
  288. ntolayer3++;
  289. /* simulate losses: */
  290. if (jimsrand() < lossprob) {
  291. nlost++;
  292. if ( TRACE > 0 )
  293. printf(" TOLAYER3: packet being lost\n");
  294. return;
  295. }
  296. /* make a copy of the packet student just gave me since he/she may decide */
  297. /* to do something with the packet after we return back to him/her */
  298. mypktptr = (pkt_t *)malloc(sizeof(pkt_t));
  299. mypktptr->seqnum = packet.seqnum;
  300. mypktptr->acknum = packet.acknum;
  301. mypktptr->checksum = packet.checksum;
  302. for ( i = 0; i < 20; i++ )
  303. mypktptr->payload[i] = packet.payload[i];
  304. if ( TRACE > 2 ) {
  305. printf(" TOLAYER3: seq: %d, ack %d, check: %d ", mypktptr->seqnum,
  306. mypktptr->acknum, mypktptr->checksum);
  307. for ( i = 0; i < 20; i++ )
  308. printf( "%c", mypktptr->payload[i] );
  309. printf("\n");
  310. }
  311. /* create future event for arrival of packet at the other side */
  312. evptr = (event_t *)malloc(sizeof(event_t));
  313. evptr->evtype = FROM_LAYER3; /* packet will pop out from layer3 */
  314. evptr->eventity = (AorB+1) % 2; /* event occurs at other entity */
  315. evptr->pktptr = mypktptr; /* save ptr to my copy of packet */
  316. /* finally, compute the arrival time of packet at the other end.
  317. medium can not reorder, so make sure packet arrives between 1 and 10
  318. time units after the latest arrival time of packets
  319. currently in the medium on their way to the destination */
  320. lastime = time;
  321. /* for (q=evlist; q!=NULL && q->next!=NULL; q = q->next) */
  322. for ( q = evlist; q != NULL; q = q->next )
  323. if ( ( q->evtype == FROM_LAYER3 && q->eventity == evptr->eventity ) )
  324. lastime = q->evtime;
  325. evptr->evtime = lastime + 1 + 9*jimsrand();
  326. /* simulate corruption: */
  327. if (jimsrand() < corruptprob) {
  328. ncorrupt++;
  329. if ( (x = jimsrand()) < .75) /* corrupt payload */
  330. mypktptr->payload[0]='Z';
  331. else if (x < .875)
  332. mypktptr->seqnum = 999999;
  333. else
  334. mypktptr->acknum = 999999;
  335. if ( TRACE > 0 )
  336. printf(" TOLAYER3: packet being corrupted\n");
  337. }
  338. if ( TRACE > 2 )
  339. printf(" TOLAYER3: scheduling arrival on other side\n");
  340. insertevent(evptr);
  341. }
  342. void tolayer5(int AorB, char datasent[20]) {
  343. int i;
  344. if (TRACE>2) {
  345. printf(" TOLAYER5: data received: ");
  346. for ( i = 0; i < 20; i++ )
  347. printf("%c",datasent[i]);
  348. printf("\n");
  349. }
  350. }