#include <sys/types.h>
#include <sys/stat.h>
#include "ip.h"
#include "byte.h"
#include "stralloc.h"
#include "serverok.h"

static char fncurve[4 + IPFMT];
static char fnserver[4 + IPFMT];

/* -1 no curve server
    1 ommitt server */

int serverok(char ip[16])
{
  struct stat st;

  fncurve[0] = fnserver[0] = 'i'; 
  fncurve[1] = fnserver[1] = 'p';
  fncurve[2] = fnserver[2] = '/';
  fncurve[3] = '-';	
  fnserver[3] = '%';	

  if (byte_equal(ip,12,V4mappedprefix)) {
    fncurve[4 + ip4_fmt(fncurve + 4,ip+12)] = 0;
    fnserver[4 + ip4_fmt(fnserver + 4,ip+12)] = 0;
  } else {
    fncurve[4 + ip6_fmt(fncurve + 4,ip)] = 0;
    fnserver[4 + ip6_fmt(fnserver + 4,ip)] = 0;
  }

  if (stat(fncurve,&st) == 0) return -1;
  if (stat(fnserver,&st) == 0) return 1;

  return 0;
}
