Global header file for all layers, containging all necessary libraries and defining global preprocessor directives such as the chunk size.
More...
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
Go to the source code of this file.
|
#define | MAX_CHUNK_SIZE 16 /* sets chunk for all layers as 16 bytes */ |
|
#define | LAYER 0 /* 1-5 for testing. 0 for normal use */ |
|
|
void | print_title (int num, FILE *mode, char *title) |
| Used for testing, prints a pretty formatted title a specified file. More...
|
|
int | my_pow (int x, int n) |
| Used fast exponentiation to calculate powers. More...
|
|
int | bin_exp (int num) |
| Lazy Binary number calculation. More...
|
|
void | itozstring (char *o_buff, int num, int len) |
| Converts a integer to a string. More...
|
|
int | stonum (char *i_buff, int len) |
| Converts 0 padded strings to an integer. More...
|
|
double | stodoub (char *i_buff) |
| Converts a string formatted as a double to an actual double. More...
|
|
int | layer1_read (char *b) |
| Sample layer1_read just calls read on stdin. More...
|
|
int | layer1_write (char b) |
| Sample layer1_write just calls write to stdout. More...
|
|
int | layer2_write (char *chunk, int len) |
| Sends a chunk that consists of the sequence of bytes starting at the address specified by the first parameter (chunk) with length len. More...
|
|
int | layer2_read (char *chunk, int max) |
| Reads a chunk and stores the incoming bytes in the buffer starting at the address specified by the first parameter (chunk). More...
|
|
int | layer3_write (char *msg, int len) |
| Sends a message. More...
|
|
int | layer3_read (char *msg, int max) |
| reads a message and stores it in memory starting at the address specified by msg More...
|
|
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...
|
|
int | layer5_write (student *stu) |
|
int | layer5_read (student *stu) |
| Dynamically allocate memory of the appropriate size for the firstname and lastname fields. More...
|
|
Global header file for all layers, containging all necessary libraries and defining global preprocessor directives such as the chunk size.
Additionally preprocessor directives are used to limit the scope of testing various layers. Set layer from 1-5 for testing and 0 for normal use.
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
#define MAX_CHUNK_SIZE 16 /* sets chunk for all layers as 16 bytes */ |
A chunk :: a sequence of bytes whose length is no greater than 16 bytes
- Each of the bytes in a chunk can have any value, including binary data and the '\0' character; only restriction is there are no more than 16 bytes.
- Note that it is also valid to send/receive a chunk of size 0.
Lazy Binary number calculation.
- Parameters
-
[in] | num | The number to calculate binary of, i.e. 4 = 2^3 = 8 |
- Returns
- 2^num
void itozstring |
( |
char * |
o_buff, |
|
|
int |
num, |
|
|
int |
len |
|
) |
| |
Converts a integer to a string.
- Parameters
-
[out] | o_buff | The correctly sized buffer to store the string in. |
[in] | num | The number to convert to a string. |
[in] | len | The size of the zero padded string to produce. |
- Returns
int layer1_read |
( |
char * |
b | ) |
|
Sample layer1_read just calls read on stdin.
- Parameters
-
- Returns
- The number of bytes read: 1 if everything goes well. -1 if there was an error.
int layer1_write |
( |
char |
b | ) |
|
Sample layer1_write just calls write to stdout.
- Parameters
-
- Returns
- The number of bytes written: 1 if everything goes well. -1 if there was an error.
A chunk :: a sequence of bytes whose length is no greater than 16 bytes
- Each of the bytes in a chunk can have any value, including binary data and the '\0' character; only restriction is there are no more than 16 bytes.
- Note that it is also valid to send/receive a chunk of size 0. TODO List
- Add check function that checks max length vs actual length (const)
- Parameters
-
- Returns
- The number of bytes written: 1 if everything goes well. -1 if there was an error.
int layer2_read |
( |
char * |
chunk, |
|
|
int |
max |
|
) |
| |
Reads a chunk and stores the incoming bytes in the buffer starting at the address specified by the first parameter (chunk).
- Warning
- Make sure that your layer2_read() does not allow the sender to overflow the buffer! And it's not enough to recognize when this has happened and return an error; you must not store anything beyond the max location of chunk.
- Parameters
-
[in] | chunk | A sequence of bytes whose length is no greater than 16 bytes as defined by MAX_CHUNK_SIZE which can contain any type of data. |
[in] | max | No more than max bytes will be put into chunk, so max limits the size of the chunk read. |
- Returns
- Upon successfully receiving a chunk, the size of the chunk in bytes is returned.
int layer2_write |
( |
char * |
chunk, |
|
|
int |
len |
|
) |
| |
Sends a chunk that consists of the sequence of bytes starting at the address specified by the first parameter (chunk) with length len.
- Warning
- all errors that can be detected here must be handled here including valid values of len and the return value of layer1_write()
- Parameters
-
[in] | chunk | The data to write using layer 1 protocols. |
[in] | len | The size of the chunk to write. |
- Returns
- len on success, -1 on error
int layer3_read |
( |
char * |
msg, |
|
|
int |
max |
|
) |
| |
reads a message and stores it in memory starting at the address specified by msg
- Parameters
-
[in] | msg | Where the message that is read is stored. |
[in] | max | No more than max bytes will be put into memory, so max must limit the size of the message read. |
- Returns
- The size of the message received or -1 on error.
If a message is received by layer3_read() that would require more than max bytes, layer3_read() must return -1 (indicating an error).
int layer3_write |
( |
char * |
msg, |
|
|
int |
len |
|
) |
| |
Sends a message.
- Parameters
-
[in] | msg | A message of any length to send. |
[in] | len | The size of the message to send. |
- Returns
- The number of bytes (of the message) sent on success (should be len) or -1 on error.
int layer4_read |
( |
char * |
msg, |
|
|
int |
max |
|
) |
| |
Reads a message into memory starting at the address specified by msg.
- Parameters
-
[in] | msg | No more than max bytes will be put into memory, so max must limit the size of the message read. |
[in] | max | The 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] | msg | The message to send. |
[in] | len | The length of the message. |
- Returns
- The number of Bytes sent.
Dynamically allocate memory of the appropriate size for the firstname and lastname fields.
- Warning
- It is the responsibility of the caller to free this memory.
- Parameters
-
[out] | stu | The student struct to read onto. |
- Returns
- The return value is 1 on success, or -1 on error.
int my_pow |
( |
int |
x, |
|
|
int |
n |
|
) |
| |
Used fast exponentiation to calculate powers.
- Parameters
-
[in] | x | The base. |
[in] | y | The power. |
- Returns
- x^y.
void print_title |
( |
int |
num, |
|
|
FILE * |
mode, |
|
|
char * |
title |
|
) |
| |
Used for testing, prints a pretty formatted title a specified file.
- Parameters
-
[in] | num | 0 for first header, 1 for any other header |
[in] | mode | The file to write to |
[in] | title | The string to print in the title |
- Returns
- void
double stodoub |
( |
char * |
i_buff | ) |
|
Converts a string formatted as a double to an actual double.
- Parameters
-
[in] | i_buff | The string to convert. |
- Returns
- The string result as a double.
int stonum |
( |
char * |
i_buff, |
|
|
int |
len |
|
) |
| |
Converts 0 padded strings to an integer.
- Parameters
-
[in] | i_buff | The input buffer containing the string to decode. |
[in] | size | The size of the input stirng. |
- Returns
- The number as an integer.