/*en    Laurent Constantin's network library (lcrzo)
                 lcrzo_sysdep module

  Functions herein contains system dependant functions.
*/
/*fr    Bibliotheque reseau de Laurent Constantin (lcrzo)
                 Module lcrzo_sysdep

  Les fonctions presentes dans ce module contiennent les fonctions
dependantes du systeme.
*/

/*-------------------------------------------------------------*/
#ifdef LCRZODEF_SYSTEM_Linux
#elif defined LCRZODEF_SYSTEM_FreeBSD || defined LCRZODEF_SYSTEM_OpenBSD
 /* values stored in sysctl are padded in a 4 bytes boundary, so
    we use this define to acces items */
 #define FB_ROUNDUP(a) ((a)>0?(1+(((a)-1)|(sizeof(long)-1))):sizeof(long))
#elif defined LCRZODEF_SYSTEM_Solaris
#elif defined LCRZODEF_SYSTEM_Windows
#else
 #error "Unknown value for LCRZODEF_SYSTEM"
#endif

/*-------------------------------------------------------------*/
int lcrzo_priv_conf_init_dev(void);
int lcrzo_priv_conf_init_arpcache(void);
int lcrzo_priv_conf_init_rarpcache(void);
int lcrzo_priv_conf_init_routes(void);

/*-------------------------------------------------------------*/
int lcrzo_priv_etha_init_ipa_ioctlarp(lcrzo_constipa ipa, 
				      lcrzo_etha etha);
int lcrzo_priv_etha_init_ipa_etcethers(lcrzo_constipa ipa, 
				       lcrzo_etha etha);
int lcrzo_priv_etha_init_ipa_iphlpapi(lcrzo_constipa ipa, 
				      lcrzo_etha etha);

/*-------------------------------------------------------------*/
int lcrzo_priv_ipa_init_etha_ioctlrarp(lcrzo_constetha etha,
				       lcrzo_ipa ipa);
int lcrzo_priv_ipa_init_etha_etcethers(lcrzo_constetha etha,
				       lcrzo_ipa ipa);

/*-------------------------------------------------------------*/
#define LCRZO_PRIV_SPOOF_TYPE_NONE    0
#define LCRZO_PRIV_SPOOF_TYPE_ETH     1
#define LCRZO_PRIV_SPOOF_TYPE_IP      2

/*-------------------------------------------------------------*/
int lcrzo_priv_spoof_init_eth(lcrzo_spoof *pspoof,
			      lcrzo_constdevice device);
int lcrzo_priv_spoof_eth(lcrzo_spoof *pspoof,
			 lcrzo_constdevice device, 
			 lcrzo_constdata packet, 
			 lcrzo_int32 nboctpacket);
int lcrzo_priv_spoof_close_eth(lcrzo_spoof *pspoof);

/*-------------------------------------------------------------*/
int lcrzo_priv_spoof_init_ip(lcrzo_spoof *pspoof);
int lcrzo_priv_spoof_ip(lcrzo_spoof *pspoof,
                        lcrzo_constdata packet, 
                        lcrzo_int32 nboctpacket);
int lcrzo_priv_spoof_close_ip(lcrzo_spoof *pspoof);

/*-------------------------------------------------------------*/
int lcrzo_priv_conf_system_renew(lcrzo_priv_confvars *ptr);

/*-------------------------------------------------------------*/
#if defined LCRZODEF_SYSTEM_Unix
 #if defined LCRZODEF_SYS_Solaris_25_Sparc
  #define LCRZO_PRIV_MODERNFUNCTIONDEF 0
 #else
  #define LCRZO_PRIV_MODERNFUNCTIONDEF 1
 #endif
#elif defined LCRZODEF_SYSTEM_Windows
 #define LCRZO_PRIV_MODERNFUNCTIONDEF 0
#else
 #error "Unknown value for LCRZODEF_SYSTEM"
#endif

/*-------------------------------------------------------------*/
#if defined LCRZODEF_SYSTEM_Unix
 #define LAURENT_close(a) close(a)
 #define LAURENT_closesocket(a) close(a)
 #define LAURENT_fdopen(a,b) fdopen(a,b)
 #define LAURENT_fileno(a) fileno(a)
 #define LAURENT_lseek(a,b,c) lseek(a,b,c)
 #define LAURENT_open(a,b) open(a,b)
 #define LAURENT_open3(a,b,c) open(a,b,c)
 #define LAURENT_rmdir(a) rmdir(a)
 #define LAURENT_unlink(a) unlink(a)
 #define LAURENT_write(a,b,c) write(a,b,c)
#elif defined LCRZODEF_SYSTEM_Windows
 #define LAURENT_close(a) _close(a)
 #define LAURENT_closesocket(a) closesocket(a)
 #define LAURENT_fdopen(a,b) _fdopen(a,b)
 #define LAURENT_fileno(a) _fileno(a)
 #define LAURENT_lseek(a,b,c) _lseek(a,b,c)
 #define LAURENT_open(a,b) _open(a,b)
 #define LAURENT_open3(a,b,c) _open(a,b,c)
 #define LAURENT_rmdir(a) _rmdir(a)
 #define LAURENT_unlink(a) _unlink(a)
 #define LAURENT_write(a,b,c) _write(a,b,c)
#else
 #error "Unknown value for LCRZODEF_SYSTEM"
#endif

/*-------------------------------------------------------------*/
#if LCRZO_PRIV_MODERNFUNCTIONDEF==1
 #define LAURENT_size_t size_t
 #define LAURENT_socklen_t socklen_t
 #define LAURENT_recv(a,b,c,d) recv(a,(lcrzo_data)(b),c,d)
 #define LAURENT_recvfrom(a,b,c,d,e,f) recvfrom(a,(lcrzo_data)(b),c,d,e,f)
 #define LAURENT_send(a,b,c,d) send(a,(lcrzo_constdata)(b),c,d)
 #define LAURENT_sendto(a,b,c,d,e,f) sendto(a,(lcrzo_constdata)(b),c,d,e,f)
 #define LAURENT_setsockopt(a,b,c,d,e) setsockopt(a,b,c,(lcrzo_constdata)(d),e)
#elif LCRZO_PRIV_MODERNFUNCTIONDEF==0
 #define LAURENT_size_t int
 #define LAURENT_socklen_t int
 #define LAURENT_recv(a,b,c,d) recv(a,(lcrzo_string)(b),c,d)
 #define LAURENT_recvfrom(a,b,c,d,e,f) recvfrom(a,(lcrzo_string)(b),c,d,e,f)
 #define LAURENT_send(a,b,c,d) send(a,(lcrzo_conststring)(b),c,d)
 #define LAURENT_sendto(a,b,c,d,e,f) sendto(a,(lcrzo_conststring)(b),c,d,e,f)
 #define LAURENT_setsockopt(a,b,c,d,e) setsockopt(a,b,c,(lcrzo_conststring)(d),e)
#else
 #error "Unknown value for LCRZO_PRIV_MODERNFUNCTIONDEF"
#endif


/*-------------------------------------------------------------*/
int lcrzo_priv_filename_stat_get(lcrzo_conststring filename, 
				 struct stat *pst);
int lcrzo_priv_fd_stat_get(int fd, 
			   struct stat *pst);
int lcrzo_priv_stat_forbidschange(struct stat st1, 
				  struct stat st2);
int lcrzo_priv_stat_forbidslink(struct stat st);
int lcrzo_priv_stat_forbidsnotreg(struct stat st);
int lcrzo_priv_stat_type(struct stat st, 
			 lcrzo_int16 *ptype);

/*-------------------------------------------------------------*/
int lcrzo_priv_globalvars_init(void);
int lcrzo_priv_globalvars_close(void);
int lcrzo_priv_globalvars_ptr_get(lcrzo_priv_globalvars **pptr);

/*-------------------------------------------------------------*/
int lcrzo_priv_init_tlsalloc(void);
int lcrzo_priv_close_tlsalloc(void);

/*-------------------------------------------------------------*/
int lcrzo_priv_ipc_writetlv(lcrzo_ipc *pipc, lcrzo_uint16 type,
			    lcrzo_constdata donnees,
			    lcrzo_int32 nboctdonnees);

/*-------------------------------------------------------------*/
int lcrzo_priv_rand_seed(lcrzo_uint32 *pseed);

/*-------------------------------------------------------------*/
#define LCRZO_WAIT_NODELAY_VAL  0
#define LCRZO_WAIT_INFINITE_VAL 0xFFFFFFFFu
int lcrzo_priv_wait_file_wait(lcrzo_file *pfile, 
			      int waitaction,
                              lcrzo_time *pmaxwaitingtime, 
                              lcrzo_bool *pevent);
int lcrzo_priv_wait_file_read(lcrzo_file *pfile,
			      int waitaction);
int lcrzo_priv_wait_ipc_wait(lcrzo_ipc *pipc, 
                             lcrzo_time *pmaxwaitingtime, 
                             lcrzo_bool *pevent);
int lcrzo_priv_wait_ipc_read(lcrzo_ipc *pipc);
int lcrzo_priv_wait_process_wait(lcrzo_process *pprocess, 
                                 lcrzo_time *pmaxwaitingtime, 
                                 lcrzo_bool *pevent);
int lcrzo_priv_wait_process_read(lcrzo_process *pprocess);
int lcrzo_priv_wait_sniff_wait(lcrzo_sniff *psniff, 
                               lcrzo_time *pmaxwaitingtime, 
                               lcrzo_bool *pevent);
int lcrzo_priv_wait_sniff_read(lcrzo_sniff *psniff);
int lcrzo_priv_wait_sock_wait(lcrzo_sock *psock, 
                              lcrzo_time *pmaxwaitingtime, 
                              lcrzo_bool *pevent);
int lcrzo_priv_wait_sock_read(lcrzo_sock *psock);

/*-------------------------------------------------------------*/
int lcrzo_priv_childprocesses_init(void);
int lcrzo_priv_childprocesses_killall(void);

/*-------------------------------------------------------------*/
int lcrzo_priv_conf_dev_debug_fprint(LCRZOFILE *pf);
int lcrzo_priv_conf_arpcache_debug_fprint(LCRZOFILE *pf);
int lcrzo_priv_conf_rarpcache_debug_fprint(LCRZOFILE *pf);
int lcrzo_priv_conf_routes_debug_fprint(LCRZOFILE *pf);

/*-------------------------------------------------------------*/
int lcrzo_priv_conf_system_verify_routes(lcrzo_priv_confvars *ptr);



