#include "archinc.h"
#include "cli.h"
#include "strutil.h"
#include "zarlink.h"
#include "icmp.h"
#include "ip.h"

#include <stdio.h>
#include <stdlib.h>

#ifdef CONFIG_INCLUDE_ZARLINK

static unsigned char ipAddr[4] = {192, 168, 0, 10};

/*
 * The structure of an ethernet header is: 
 * | Source MAC (6 bytes)  | Dest MAC (6 bytes) | type (2 bytes) |
 *
 * A type of 0x0800 indicates an IP packet
 *
 * The ethernet trailer is a 32-bit CRC
 */


static int ipsetHandler(int argc, char * argv[], int flags)
{
  int retVal = 0;
  int sendPort;
  char * pOutBuf;
  int bufLen;

  pOutBuf = clicharGetOutputBuffer(&bufLen);

  if (argc == 2) {
    retVal = parseDottedQuadV4(argv[1], ipAddr);
    }
  
  if (retVal == 0) {
    sprintf(pOutBuf, "%s: IP address is %d.%d.%d.%d\r\n", argv[0], ipAddr[0], ipAddr[1], 
      ipAddr[2], ipAddr[3]);
    clicharSendOutputBuffer(pOutBuf, bufLen);
    }

  return retVal;
}

int ipAddrGet(char *ipv4Addr)
{
  int i;

  if (ipv4Addr == NULL)
    return -1;

  for (i=0;i<4;i++)
    ipv4Addr[i] = ipAddr[i];

  return 0;
}

int ipInit(void)
{
  cliRegisterCommand("ipset", ipsetHandler, "ipset [IP Addr] - sets IP v4 address");
  return 0;
}  

#endif /* CONFIG_INCLUDE_ZARLINK */

