
/*
 * sender - send network data with error detection.
 *
 * Written by James Warhol  CSc 645  Spring 1995
 *
 * Compile with: cc -o sender sender.c chksum.c
 *
 */

#include <errno.h>			/* defines file open error codes */
#include <fcntl.h>			/* defines open flags */
#include <stdio.h>			/* standard file i/o definitions */
#include <string.h>			/* string processing definitions */
#include "BUFDEF.h"			/* defines buffer parameters */

/* define variables */

message msgbuf;				/* message communication buffer */

int i;					/* scratch variable */
int ifn_id;				/* input file descriptor */
int errno;				/* error number for i/o errors */

/* define function prototypes */

void crash(char line[]);		/* crash on error with message */
void initialize();			/* initialize sender program */

/* main program - sender */

void main()
{

   int wc;				/* word count for read */

   initialize();			/* initialize sender program */

   /* loop repeatedly reading DATASIZE bytes from the input file 
      and writing them as formatted messages to standard output */

   do { 

      /* read up to DATASIZE bytes into message buffer; pad buffer with
         spaces as needed; set checksum field in message header; write it */

      wc = read(ifn_id, msgbuf.data, DATASIZE);
      if (wc > 0) {
         msgbuf.header.len = wc;	/* set message length */
         for (i = wc; i < DATASIZE; i++) msgbuf.data[i] = ' '; /* blank fill */

         msgbuf.header.cksum = 0;       /* clear checksum */
         msgbuf.header.cksum = 
            (short) in_cksum(&msgbuf,sizeof(msgbuf)); /* set checksum */

         i = write(1, &msgbuf, BUFSIZE); /* write packet to standard output */
         if (i < 0) crash("Unexpected write error in main function.\n");
      }
      else if (wc < 0) 
         crash("Unexpected read error in main function.\n");

   } while(wc == DATASIZE);

   /* close the input file and terminate sender */

   if (close(ifn_id) != 0) crash("An error occurred while closing infile.\n");
   exit(0);
}

/*
 * void crash(char line[]);
 *
 * crash sends the specified error message to standard error and 
 * then terminates execution.
 *
 */

void crash(char line[])
{
   i = write(2, line, strlen(line)); /* write error text to standard error */
   if (i < 0) crash("Unexpected write error in crash function.\n");
   exit(1);
}

/*
 * void initialize();
 *
 * initialize initializes processing for the sender program.
 * Specifically, this means the input file is opened and the author's
 * initials (jrw) are placed in the message header.
 *
 */

void initialize()
{

   /* open input file and check return status */

   ifn_id = open("infile", O_RDONLY, 0);
   if (ifn_id < 0) {
      if (errno == ENOENT) 
         crash("The input file does not exist.\n");
      else
         crash("An unexpected error occurred when opening the input file.\n"); 
   }

   /* set initials in message header */

   msgbuf.header.initials[0] = 'j';
   msgbuf.header.initials[1] = 'r';
   msgbuf.header.initials[2] = 'w';

   return;

}
