#include <stdio.h>
/*******************************************************
*  Module :  A very simple program to demonstrate
*            an htonl an ntohl pair of routines.
*   
*    Remember:
*      little endian (like an intel-based machine) stores the
*          LSB first.
*      Big endian (like a sparc-based machine) stores the
*          MSB first.
* 
*********************************************************/

unsigned int htonlLocal(int bigEndianFlag,unsigned int address);
unsigned int ntohlLocal(int bigEndianFlag,unsigned int address);

int main(void)
{
  int             x, y, z;         // Integer values
  char           *ptr;             // Pointer

  int bigEndianFlag;
  int networkAddress;   //will hold the address in network byte order

  // Assign x and output it in byte order and as a value
  x = 0x12345678;
  ptr = (char *) &x;

  //Test if this machine is big or little endian.
  if (ptr[0] == 0x01) {
    bigEndianFlag= 0x01;
    printf("This machine appears to be big endian \n");
  } else if (ptr[0] == 0x78) {
    printf("This machine appears to be little endian \n");
    bigEndianFlag= 0x00;
  } else {
    printf("ERROR:  Why do I see %0xX ??\n",ptr[0]);
  }

  printf("(LSB)ptr[0] = %02X \n", ptr[0]);
  printf("ptr[1] = %02X \n", ptr[1]);
  printf("ptr[2] = %02X \n", ptr[2]);
  printf("(MSB)ptr[3] = %02X \n", ptr[3]);
  printf("x = %08X \n", x);

  // Convert x to network order (big indian) and output it
  y = htonl(x);
  z =  htonlLocal(bigEndianFlag,x);
  ptr = (char *) &y;
  printf("ptr[0] = %02X \n", ptr[0]);
  printf("ptr[1] = %02X \n", ptr[1]);
  printf("ptr[2] = %02X \n", ptr[2]);
  printf("ptr[3] = %02X \n", ptr[3]);
  printf("After htonl:  y = %08X,  z = %08X \n", y,z);

  // Convert back to host order and output it
  y = ntohl(y);
  z=  ntohlLocal(bigEndianFlag,z);
  ptr = (char *) &y;
  printf("ptr[0] = %02X \n", ptr[0]);
  printf("ptr[1] = %02X \n", ptr[1]);
  printf("ptr[2] = %02X \n", ptr[2]);
  printf("ptr[3] = %02X \n", ptr[3]);
  printf("After ntohl: y = %08X,  z = %08X \n", y,z);

  return(0);
}


/*******************************************************
*  Routine:  unsigned int ntohl(int bigEndianFlag,unsigned int address)
*      This routine is passed an address in bigEndian (network byte)
*       format. IT must return the int packed in host format.
*
*      Logic:  if the machine is bigEndian, we are done.
*              else, reverse the order.
*  
*  Inputs:
*     int bigEndianFlag : a value of 1 is true; else fals
*     unsigned int address : the 32 bit address
* 
*******************************************************/
unsigned int ntohlLocal(int bigEndianFlag,unsigned int address)
{
  unsigned int networkAddress = address;
  int i;

  printf("ntohl: entered with  %08X \n", address);
  if (bigEndianFlag !=  1) {
    //swap order
    unsigned char *ptr1=(unsigned char *)&networkAddress;
    unsigned char *ptr2=(unsigned char *)&address;
    for (i=0;i<4;i++) {
      ptr1[i]=ptr2[3-i];
    }
  }

  printf("ntohl: exit with  %08X \n", networkAddress);
  return networkAddress;
}

/*******************************************************
*  Routine:  unsigned int htonl(int bigEndianFlag,unsigned int address)
*      This routine is passed an address in host order. 
*      It must return the integer in network byte order.
*
*      Logic:  if the machine is bigEndian, we are done.
*              else, reverse the order.
*  
*  Inputs:
*     int bigEndianFlag : a value of 1 is true; else fals
*     unsigned int address : the 32 bit address
* 
*******************************************************/
unsigned int htonlLocal(int bigEndianFlag,unsigned int address)
{
  unsigned int networkAddress = address;
  int i;

  printf("htonl: entered with  %08X \n", address);
  if (bigEndianFlag !=  1) {
    //swap order
    unsigned char *ptr1=(unsigned char *)&networkAddress;
    unsigned char *ptr2=(unsigned char *)&address;
    for (i=0;i<4;i++) {
      ptr1[i]=ptr2[3-i];
    }
  }

  printf("ntohl: exit with  %08X \n", networkAddress);
  return networkAddress;
}



