/*******************************************
*
* Quiz6 - copy the variables x and y into the buffer
*         that is pointed to by buf2.
*         complete Lines numbered 1-4
*          Compile :  'gcc quiz6-sol.c'
*               You will see some warnings....just ignore
* 
*  int bufSize = 8;
*  char *buf2 = malloc(bufSize);
*  uint32_t x = 0x01020304;
*  uint16_t y = 0x0506;
*  void *dstPtr = buf2;
*  uint32_t *tmpPtr1 = ?
*  uint16_t *tmpPtr2 = ?
*  printf("test: values of pointers:  tmpPtr1:%d, tmpPtr2:%d \n",(int)tmpPtr1,(int)tmpPtr2);
*  *tmpPtr1 = ?
*  *tmpPtr2 = ? 
*

* This quiz illustrates the use of a void * pointer.
* The dstPtr, a void ptr, is effectively a generic ptr.
*     
*      void *myPtr = NULL;
*      //This creates a generic ptr that can be used
*        without casting to specific types.
*      Allows void *dstPtr = buf2 
*         which is conventient as we do not need to
*            cast buf2 to void *. The following is fine but the cast (void *) of buf2 not necessary
*                  (void *)dstPtr = (void *)buf2
       Also allows a routine that operates on a buffer, a void *
*      parameter makes it a bit easier to code as it can reduce
*      the number of type casts that might be necessary.
*          
*      Note: most significant rule when using void * is you can not
*                1.Can not copy into a void * ptr
*                  ...can not do:    *dstPtr = htonl(x)
*                2.Can not do arithmetic operations on void * ptrs            
*                  ...can not do :    *dstPtr++ 
*
*   Running the test routine on a Little Endian machine (Intel CPU)
*     ./a.out
*     test: values of pointers:  tmpPtr1:14856208, tmpPtr2:14856212 
*     test: show Buf2:  
*      1  2  3  4  5  6  0  0 
*
*   Note: the output if we did not use the htonl and htons
*    test: show Buf2:  
*           4  3  2  1  6  5  0  0 
*
*
*     When looking at the byte order when shorts or ints
*     are laid out in consecutive memory locations....
*     Little endian stores the Least Significant byte in the
*     smallest address (or first if we assume memory grows sequentially)
*     Big endian stores the Most Signicant Byte in the smallest
*     address (or  first).
*
*     Below shows program output if we modify the showBuf routine 
*           printf("%d %d %u \n",loopCount, tmpPtr,*tmpPtr++);
*
*loopCount  memory location   value
*  test: show Buf2:  
*  1 39845905 1  MSB of x
*  2 39845906 2 
*  3 39845907 3 
*  4 39845908 4  LSB of x
*  5 39845909 5  MSB of y
*  6 39845910 6  LSB of y
*  7 39845911 0 
*  8 39845912 0 
*
*  Network byte order is Big Endian. Picture a network buffer
*  as a block of continguous memory that might be in the
*  payload of a frame that will be transmitted. 
*  If buf2 has been packed in network byte order,
*  the transmission will begin with the MSB as a frame
*  is viewed as a sequential block of memory. THe first
*  octet of the frame corresponds to the lowest memory address.
*
********************************************/

/*  You can copy and paste the following into a script file....
    this compiles and runs the solution. 
    The buf2 buffer is dumped into a file and we use
    the od tool to show each octet 
gcc quiz6-sol.c
rm *.dat
./a.out
FILE="buf2.dat"
echo " "
mySize=$(stat $FILE |   grep Size | awk '{print $2}')
echo " The size of $FILE is  $mySize"
#The following od command shows each octet in hex
od -t x1 $FILE
#The following od command shows each octet in decimal
#od -t u1 $FILE

*/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>  /*brings in C99 types of uint32_t uint64_t  ...*/
#include <limits.h>  /*brings in limits such as LONG_MIN LLONG_MAX ... */
#include <math.h>    /* floor, ... */

#include <string.h>     
#include <errno.h>


#include <sys/socket.h> /* for socket(), connect(), sendto(), and recvfrom() */
#include <netinet/in.h> /* for in_addr */
#include <arpa/inet.h> /* for inet_addr ... */

#include <unistd.h>     /* for close() */
#include <fcntl.h>
#include <netdb.h>

#include <signal.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <time.h>

#define FAILURE -1
#define SUCCESS  0

void test();
int dumpBufferToFile(void *bufferPtr, uint32_t bufSize, char *fileName);
void showBuf(void *bufPtr, uint32_t bufSize);

int  main (void)
{
  int rc  = 1;

  test();
  return rc;
}


/*******************************************
* routine:  test
*
*   This routine is an example of code that
*   mallocs a 'network buffer' and transfers
*   two variables into the network buffer
*   
********************************************/
void test ()
{
  int bufSize = 8;
  char *buf2 = malloc(bufSize);
  bzero(buf2,bufSize);
  uint32_t x = 0x01020304;
  uint16_t y = 0x0506;
  int rc  = FAILURE;

  void *dstPtr = buf2;

//Lines 1 and 2
  uint32_t *tmpPtr1 =  dstPtr;
  uint16_t *tmpPtr2 = (dstPtr+4);

//NOTE:  THIS WILL NOT WORK!!
//  uint32_t *tmpPtr1 =  dstPtr++
//  uint16_t *tmpPtr2 = dstPtr

  printf("test: values of pointers:  tmpPtr1:%d, tmpPtr2:%d \n",(int)tmpPtr1,(int)tmpPtr2);

//Lines 3 and 4
  *tmpPtr1 =  htonl(x); 
  *tmpPtr2 =  htons(y);

  rc = dumpBufferToFile(buf2,  bufSize, "buf2.dat");
  printf("test: rc from dump:%d,  showBuf:  \n",rc);
  showBuf( buf2, bufSize);
}



void showBuf(void *bufPtr, uint32_t bufSize)
{
  int loopCount = 0;
  unsigned char *tmpPtr = bufPtr;

  while(loopCount++<bufSize) 
  {
//    printf("%d %d %u \n",loopCount, tmpPtr,*tmpPtr++);
    printf(" %u ",*tmpPtr++);
  }
  printf("\n"); 
}


/***********************************************************
* Function: int dumpBufferToFile(void *bufferPtr, uint32_t bufSize, char *fileName)
*
* Explanation:  This dumps the buffer to a file in binary format
*               It is stored as a stream using a fwrite operation.
*
* inputs: 
*    var *bufferPtr : reference to caller's buffer
*    uint32_t bufSize size of buffer
*    char *fileName : file name
*
* outputs:
*    Returns a FAILURE or the number of bytes in the file 
*
* notes: 
*   To view the contents of the file, issue od -x filename
*
***********************************************************/
int dumpBufferToFile(void *bufferPtr, uint32_t bufSize, char *fileName)
{
  int size=FAILURE;
  int rc = FAILURE;
  int statRC=0;
  struct stat st;

  FILE *bufferFile = NULL;


  bufferFile = fopen(fileName, "w+");
  if (bufferFile == NULL)
    return rc;

  size = fwrite(bufferPtr,sizeof(char),bufSize,bufferFile);
  fclose(bufferFile);
 
  statRC = stat(fileName,&st);

  if ( (size <bufSize) || (statRC > 0)) {
    printf("dumpBufferToFile: ERROR on dump to file %s or stat : rc = %d   \n",fileName,rc);
    return rc;
  }

  //We are good if we get here

#ifdef TRACEME
  printf("dumpBufferToFile: Succeded to dump to file %s, paramSize:%d, sizeFwrite:%d, statSize:%d  \n",fileName,bufSize, size, (int)st.st_size);
#endif

  return size;
}

