Project 1 - Peer-to-peer and interface protocols using C  1.0
A half-duplex layered communication system.
 All Classes Files Functions Macros
Macros | Functions
layer4.c File Reference

Layer 4 adds simple error detection to layer 4 services. More...

#include "layer.h"

Macros

#define LAYER4_H
 
#define CHKFRSZ   4
 

Functions

int layer4_write (char *msg, int len)
 Computes a checksum of the message and sends it to layer4_read. More...
 
int layer4_read (char *msg, int max)
 Reads a message into memory starting at the address specified by msg. More...
 

Detailed Description

Layer 4 adds simple error detection to layer 4 services.

The errors to look for here involve transmission errors; we want to somehow make sure that the message received is the same as the message that was sent. The simplest approach to error detection is to use a checksum. The checksum is the sum % 65536. The resulting two-byte checksum should be sent along with the message data; the receiving end goes through the same steps to compute the checksum, then compares the received checksum to the computed checksum.

Peer-to-peer and Interface Protocols Using C

Author
Nicholas Guthrie guthr.nosp@m.n@rp.nosp@m.i.edu http//nickguthrie.com January 30, 2014

Compile with: gcc -c layer4.c -o ../build/layer4.o

Function Documentation

int layer4_read ( char *  msg,
int  max 
)

Reads a message into memory starting at the address specified by msg.

Parameters
[in]msgNo more than max bytes will be put into memory, so max must limit the size of the message read.
[in]maxThe maximum size of the message.
Returns
-1 if max size is incorrect or if there is a transmission error. The size of the message in bytes is returned otherwise.
int layer4_write ( char *  msg,
int  len 
)

Computes a checksum of the message and sends it to layer4_read.

Parameters
[in]msgThe message to send.
[in]lenThe length of the message.
Returns
The number of Bytes sent.