CITS3002 Computer Networks  
prev
next CITS3002 help3002 CITS3002 schedule  

A complete stopandwait Data-Link Layer protocol

We now present a complete example of the stopandwait Data-Link Layer protocol. This implementation is based on Tanenbaum's 'protocol 4', 5th edition, p230. Some other textbook authors refer to the same protocol as the IRQ protocol.

This protocol employs only data and acknowledgement frames - piggybacking and negative acknowledgements are not supported.

We first define some global types, data structures, and variables for this protocol. It is important to understand that each of these is unique to each of the nodes in the simulation.

#include <cnet.h>
#include <stdlib.h>
#include <string.h>

typedef enum    { DL_DATA, DL_ACK }   FRAMEKIND;

typedef struct {
    char        data[MAX_MESSAGE_SIZE];
} MSG;

typedef struct {
    FRAMEKIND    kind;       // only ever DL_DATA or DL_ACK
    size_t       len;        // the length of the msg field only
    int          checksum;   // checksum of the whole frame
    int          seq;        // only ever 0 or 1
    MSG          msg;
} FRAME;

#define FRAME_HEADER_SIZE  (sizeof(FRAME) - sizeof(MSG))
#define FRAME_SIZE(f)      (FRAME_HEADER_SIZE + f.len)

static  int             ackexpected             = 0;
static  int             nextdatatosend          = 0;
static  int             dataexpected            = 0;

static  MSG             lastmsg;
static  size_t          lastlength              = 0;
static  CnetTimerID     lasttimer               = NULLTIMER;

Although each of the nodes will typically use the same source code file, each node has its own local copy of its variables. It is not possible for one node to modify the variables in another node. The only way for the nodes to communicate is via the Physical Layer.


CITS3002 Computer Networks, Lecture 3, Data Link Layer protocols, p17, 13th March 2024.