/* This file is part of Lcrzo.
*/

/* This file is part of Lcrzo.
*/

#ifdef LCRZODEF_SYSTEM_Unix
  /* this function is not needed*/

#elif defined LCRZODEF_SYSTEM_Windows

/*-------------------------------------------------------------*/
lcrzo_bool lcrzo_priv_conf_devicenumbername_item_cmp(const void *pelem, void *pinfos);
lcrzo_bool lcrzo_priv_conf_devicenumbername_item_cmp(const void *pelem, void *pinfos)
{ const lcrzo_priv_conf_devicenumbername d1=*(const lcrzo_priv_conf_devicenumbername *)pelem;
  lcrzo_priv_conf_devicenumbername d2=*(lcrzo_priv_conf_devicenumbername *)pinfos;

  if ( d1.number==d2.number )
  { return(LCRZO_TRUE);
  }
  return(LCRZO_FALSE);
}

/*-------------------------------------------------------------*/
int lcrzo_priv_conf_devicenumbername_item_add(lcrzo_uint32 devicenumber,
                                         lcrzo_device devicename);
int lcrzo_priv_conf_devicenumbername_item_add(lcrzo_uint32 devicenumber,
                                         lcrzo_device devicename)
{ lcrzo_priv_conf_devicenumbername dnn;
  lcrzo_priv_confvars *ptrcv;
  lcrzo_int32 pos;
  int ret;
  
  lcrzo_er(lcrzo_priv_confvars_ptr_get(&ptrcv));

  /* set structure */
  dnn.number=devicenumber;
  lcrzo_er(lcrzo_device_init_noalias(devicename, dnn.name));
  
  /* search if devicenumber is already in the list */
  ret=lcrzo_list_search_all(&(ptrcv->list_devicenumbername), &lcrzo_priv_conf_devicenumbername_item_cmp, 
                            &dnn, &pos, NULL);
  if ( ret!=LCRZO_ERR_OKSEARCHNOTFOUND && ret!=LCRZO_ERR_OK )
    return(ret);
  if ( ret==LCRZO_ERR_OK )
  { /* we found it, so we replace it */
    lcrzo_er(lcrzo_list_replace_pos(&(ptrcv->list_devicenumbername), pos, &dnn));
  }
  else
  { /* we add it to the list */
    lcrzo_er(lcrzo_list_add_last(&(ptrcv->list_devicenumbername), &dnn));
  }

  return(LCRZO_ERR_OK);
}

/*-------------------------------------------------------------*/
int lcrzo_priv_conf_devicenumbername_item_get(lcrzo_uint32 devicenumber,
                                         lcrzo_device devicename);
int lcrzo_priv_conf_devicenumbername_item_get(lcrzo_uint32 devicenumber,
                                         lcrzo_device devicename)
{ lcrzo_priv_conf_devicenumbername dnn, dnn2;
  lcrzo_priv_confvars *ptrcv;
  lcrzo_int32 pos;
  int ret;
  
  lcrzo_er(lcrzo_priv_confvars_ptr_get(&ptrcv));

  /* set structure */
  dnn.number=devicenumber;
  lcrzo_er(lcrzo_device_initdefault(dnn.name));
  
  /* search if devicenumber is in the list */
  ret=lcrzo_list_search_all(&(ptrcv->list_devicenumbername), &lcrzo_priv_conf_devicenumbername_item_cmp, 
                            &dnn, &pos, &dnn2);
  if ( ret!=LCRZO_ERR_OKSEARCHNOTFOUND && ret!=LCRZO_ERR_OK )
    return(ret);
  if ( ret==LCRZO_ERR_OK )
  { /* we found it */
    lcrzo_er(lcrzo_device_init(dnn2.name, devicename));
  }
  else
  { /* we set it to number */
    lcrzo_string_init_int(devicenumber, "%d", LCRZO_DEVICE_MAXBYTES, devicename);
  }


  return(LCRZO_ERR_OK);
}

/*-------------------------------------------------------------*/
int lcrzo_priv_conf_devicenumbername_debug_fprint(LCRZOFILE *pf);
int lcrzo_priv_conf_devicenumbername_debug_fprint(LCRZOFILE *pf)
{ lcrzo_priv_confvars *ptrcv;
  lcrzo_priv_conf_devicenumbername dnn;
  lcrzo_int32 count, i;

  fprintf(pf, "\n\nDisplay each entry in list_devicenumbername\n");
 
  lcrzo_er(lcrzo_priv_confvars_ptr_get(&ptrcv));
  lcrzo_er(lcrzo_list_count(&ptrcv->list_devicenumbername, &count));
  for ( i=1 ; i<=count ; i++ )
  { lcrzo_er(lcrzo_list_value_pos(&ptrcv->list_devicenumbername, i, &dnn));
    fprintf(pf, "%lu\t", dnn.number);
    lcrzo_device_fprint(pf, "device=", dnn.name, "\n");
  }

  return(LCRZO_ERR_OK);
}

/*-------------------------------------------------------------*/
int lcrzo_priv_conf_devicenumbername_devices_add_system(lcrzo_uint32 devicenumber,
	  		                                           const lcrzo_etha etha,
			                                             lcrzo_ipl ipl,
			                                             lcrzo_ipl netmask,
			                                             lcrzo_uint32 mtu,
			                                             lcrzo_bool isup,
			                                             lcrzo_bool isanalias);
int lcrzo_priv_conf_devicenumbername_devices_add_system(lcrzo_uint32 devicenumber,
	  		                                           const lcrzo_etha etha,
			                                             lcrzo_ipl ipl,
			                                             lcrzo_ipl netmask,
			                                             lcrzo_uint32 mtu,
			                                             lcrzo_bool isup,
			                                             lcrzo_bool isanalias)
{ lcrzo_device device;
  char *pc;
  int ret;

  ret=lcrzo_priv_conf_device_init_ipl(ipl, device);
  if (ret==LCRZO_ERR_OK)
  { /* set device */
    lcrzo_priv_conf_devicenumbername_item_add(devicenumber, device);
    /* search if it's an alias (overwrite passed value) */
    pc=strchr(device, ':');
    if (pc==NULL)
    { isanalias=LCRZO_FALSE;
    }
    else
    { isanalias=LCRZO_TRUE;
    }
  }
  else
  { /* get device */
    lcrzo_priv_conf_devicenumbername_item_get(devicenumber, device);
    /* eventually add ":1" */
    if (isanalias)
    { lcrzo_er(lcrzo_string_append_text(":1", LCRZO_DEVICE_MAXBYTES, device));
    }
  }

  ret=lcrzo_priv_conf_devices_add_system(device, etha, ipl, netmask, mtu, isup, isanalias);

  return(ret);
}

/*-------------------------------------------------------------*/
int lcrzo_priv_conf_devicenumbername_arp_add_system(lcrzo_uint32 devicenumber,
                                               const lcrzo_etha etha,
		                                           lcrzo_ipl ipl,
		                                           lcrzo_bool ispermanent);
int lcrzo_priv_conf_devicenumbername_arp_add_system(lcrzo_uint32 devicenumber,
                                               const lcrzo_etha etha,
		                                           lcrzo_ipl ipl,
		                                           lcrzo_bool ispermanent)
{ lcrzo_device device;
  int ret;
  
  ret=lcrzo_priv_conf_devicenumbername_item_get(devicenumber, device);
  if (ret==LCRZO_ERR_OK)
  { ret=lcrzo_priv_conf_arp_add_system(device, etha, ipl, ispermanent);
  }

  return(ret);
}

/*-------------------------------------------------------------*/
int lcrzo_priv_conf_devicenumbername_routes_add_system(lcrzo_uint32 devicenumber,
 			                                            lcrzo_ipl dest,
			                                            lcrzo_ipl mask,
			                                            lcrzo_ipl ipsource,
			                                            lcrzo_ipl gateway,
                                                  lcrzo_uint32 metric,
			                                            lcrzo_bool isup);
int lcrzo_priv_conf_devicenumbername_routes_add_system(lcrzo_uint32 devicenumber,
 			                                            lcrzo_ipl dest,
			                                            lcrzo_ipl mask,
			                                            lcrzo_ipl ipsource,
			                                            lcrzo_ipl gateway,
                                                  lcrzo_uint32 metric,
			                                            lcrzo_bool isup)
{ lcrzo_device device;
  int ret;
  
  ret=lcrzo_priv_conf_devicenumbername_item_get(devicenumber, device);
  if (ret==LCRZO_ERR_OK)
  { ret=lcrzo_priv_conf_routes_add_system(device, dest, mask, ipsource, gateway, metric, isup);
  }

  return(ret);
}

#else
 #error "Unknown value for LCRZODEF_SYSTEM"
#endif
