#include <stdio.h>
#include <ctype.h>

struct xcp_state;
typedef unsigned short uint16;
typedef unsigned char octet;

extern void *create_ppp_link(),destroy_ppp_link();
extern void generic_sender();

char *
ecp_decrypt(struct xcp_state *xcp, char *indata, int *inlen)
{
  printf("Would decrypt %d bytes here.\n",*inlen);
  return indata;
}

char *
ccp_uncompress(struct xcp_state *xcp, char *indata, int *inlen)
{
  printf("Would uncompress %d bytes here.\n",*inlen);
  return indata;
}

void
ccp_uncompressed(struct xcp_state *xcp, uint16 proto, char *indata, int inlen)
{
  printf("Would update decompressor dictionary here.\n");
}

static void
dump_buffer(octet *buf, int count)
{
  int i,j;

  for (i = 0; i < count; i += 16) {
    printf("%04X:  ",i);
    for (j = 0; j < 16 && j+i < count; j++)
      printf("%02X ",buf[j+i]&0xFF);
    printf("%*s",(16-j)*3+2,"");
    for (j = 0; j < 16 && j+i < count; j++)
      if (isprint(buf[j+i]))
	putchar(buf[j+i]);
      else
	putchar('.');
    putchar('\n');
  }
}

static int h1,h2;
static void *l1,*l2;
static octet testbuf[2048];

void
lcp_handler(struct xcp_state *xcp, octet *indata, int inlen)
{
  printf("LCP handler.\n");
  dump_buffer(indata,inlen);
}

void
ip_handler(struct xcp_state *xcp, octet *indata, int inlen)
{
  printf("IP handler; %d bytes.\n",inlen);
  dump_buffer(indata,inlen);
}

static void
link_output(void *handle, octet *outdata, int outlen)
{
  void *lptr;

  if (handle == (void *)&h1) {
    printf("Output for link 1");
    lptr = l1;
  } else if (handle == (void *)&h2) {
    printf("Output for link 2");
    lptr = l2;
  } else {
    printf("Error; bad handle %X\n",(unsigned)handle);
    return;
  }
  printf(", %d bytes:\n",outlen);
  dump_buffer(outdata,outlen);
  link_receive(lptr,outdata,outlen);
}

static void
fill_testbuf()
{
  int i;

  for (i = 0; i < sizeof(testbuf); i++)
    testbuf[i] = rand();
}

int
main(argc,argv)
int argc;
char **argv;
{
  void *mp1,*mp2,*ip;

  fill_testbuf();
  l1 = create_ppp_link(&h1,link_output);
  set_link_mtu(l1,250);
  ip = add_xcp(l1,0x21,ip_handler,generic_sender);
  l2 = create_ppp_link(&h2,link_output);
  set_link_mtu(l2,250);
  mp1 = find_bundle_head(l1);
  mp2 = find_bundle_head(l2);
  if (mp1 != mp2) {
    printf("Null peername/ed should bundle together.\n");
    return 1;
  }
  printf("Sending test buffer of 1500 bytes:\n");
  dump_buffer(testbuf+32,1500);
  generic_sender(ip,testbuf+32,150);
  generic_sender(ip,testbuf+32,300);
  generic_sender(ip,testbuf+32,1500);
  generic_sender(ip,testbuf+32,15);
  generic_sender(ip,testbuf+32,150);
  generic_sender(ip,testbuf+32,1500);
  generic_sender(ip,testbuf+32,300);
  destroy_ppp_link(l1);
  destroy_ppp_link(l2);
  return 0;
}
