/* This file is part of Lcrzo.
*/

/*-------------------------------------------------------------*/
int lcrzo_priv_etha_init_ipa_iphlpapi(const lcrzo_ipa ipa, lcrzo_etha etha)
/* cette fonction interroge Winsock */
#if defined LCRZODEF_SYSTEM_Unix
{ return(LCRZO_ERR_OKUNRESOLVED);
  { int i; 
    /* just for the compiler warning */
    if (ipa==NULL && etha==NULL) i=0;
  }
}
#elif defined LCRZODEF_SYSTEM_Windows
{ lcrzo_priv_iphlpapidll iphlpapidll;
  lcrzo_ipl ipl;
  lcrzo_uint32 bufsize;
  ULONG ethalocal[LCRZO_ETHA_MAXBYTES];
  lcrzo_etha ethalocal2;
  lcrzo_priv_globalvars *ptrglo;
  int ret;

  lcrzo_er(lcrzo_priv_globalvars_ptr_get(&ptrglo));
  switch(ptrglo->windows_systemversion)
  { case LCRZO_WINDOWS_SYSTEMVERSION_31:
    case LCRZO_WINDOWS_SYSTEMVERSION_95:
    case LCRZO_WINDOWS_SYSTEMVERSION_98:
    case LCRZO_WINDOWS_SYSTEMVERSION_me:
    case LCRZO_WINDOWS_SYSTEMVERSION_nt350:
    case LCRZO_WINDOWS_SYSTEMVERSION_nt351:
    case LCRZO_WINDOWS_SYSTEMVERSION_nt4:
      /* SendARP is unsupported */
      return(LCRZO_ERR_OKUNRESOLVED);
      break;
  }

  /* load the library */
  lcrzo_er(lcrzo_priv_iphlpapidll_init(&iphlpapidll));

  /* init wanted address */
  ret=lcrzo_ipl_init_ipa(ipa, &ipl);
  if ( ret!=LCRZO_ERR_OK )
  { lcrzo_priv_iphlpapidll_close(&iphlpapidll);
    return(ret);
  }

  /* send the request */
  bufsize=LCRZO_ETHA_MAXBYTES;
  ret=iphlpapidll.SendARP(lcrzo_htonl(ipl), 0, ethalocal, &bufsize);
  if ( ret!=NO_ERROR )
  { lcrzo_priv_iphlpapidll_close(&iphlpapidll);
    return(LCRZO_ERR_OKUNRESOLVED);
  }

  /* check the size */
  if ( bufsize!=LCRZO_ETHA_MAXBYTES )
  { lcrzo_priv_iphlpapidll_close(&iphlpapidll);
    return(LCRZO_ERR_OKUNRESOLVED);
  }

  /* set output */
  memcpy(ethalocal2, ethalocal, LCRZO_ETHA_MAXBYTES);
  ret=lcrzo_priv_conf_arp_add_system("", ethalocal2, ipl, LCRZO_FALSE);
  if (ret!=LCRZO_ERR_OK)
  { lcrzo_priv_iphlpapidll_close(&iphlpapidll);
    return(ret);
  }
  if ( etha!=NULL )
  { memcpy(etha, ethalocal2, LCRZO_ETHA_MAXBYTES);
  }

  /* unload the library */
  lcrzo_er(lcrzo_priv_iphlpapidll_close(&iphlpapidll));
  return(LCRZO_ERR_OK);
}
#else
 #error "Unknown value for LCRZODEF_SYSTEM"
#endif

