/* File: message.c */ /* ********************************************************************* ALTERNATING BIT AND GO-BACK-N NETWORK EMULATOR Revised version of code by J.F. Kurose From the companion website for _Computer Networking: A Top-Down Approach Featuring the Internet_ by J.F. Kurose and K. W. Ross (c) 2001 by Addison Wesley Longman Based on document at (as of 02 July 2002) ********************************************************************* */ #include #include #include "payload.h" #include "message.h" /* struct msg a "msg" is the data unit passed from layer 5 (teacher's code) to layer 4 (student's code). It contains the data (characters) to be delivered to layer 5 via the student's transport level protocol entities. */ struct msg { char data[PAYLOAD_SZ]; }; /* create a message */ message_t new_msg(void) { message_t M; if (NULL != (M = malloc(sizeof(struct msg)))) { init_msg(M); } return M; } /* new_msg() */ /* destroy a message */ void dispose_msg(message_t M) { free(M); } /* dispose_msg() */ /* initialize value of message data field (make it empty) */ void init_msg(message_t M) { M->data[0] = '\0'; } /* init_msg() */ /* set value of message data field */ void set_msg_data (message_t M, char *data) { (void)strncpy(M->data, data, PAYLOAD_SZ - 1); } /* set_msg_data() */ /* append data to message's data field */ void append_msg_data (message_t M, char datum) { char str[2] = {'\0','\0'}; str[0] = datum; (void)strncat(M->data, str, PAYLOAD_SZ - strlen(M->data) - 1); } /* append_msg_data() */ /* get copy of message's data field */ char * msg_data (message_t M) { return M->data; } /* msg_data() */ /* display copy of message's data field */ int print_msg (FILE * where, char * before, message_t M, char * after) { return fprintf(where, "%s%s%s", before, M->data, after); } /* print_msg() */ /* EOF (message.c) */