
/* sqlflex client/server control */

#include "sqlflexd.h"

#ifndef _SFCSNET_H
#define _SFCSNET_H

/* en(dis)able debug
#define _SFCS_DEBUG
*/

/* Platform Identifiers */

#ifdef MSDOS3
#   define _SP_MSWIN32
#   define _SP_SYS_NAME  "mswin32"
#   include <windows.h>
#   include <stdio.h>
#   include <string.h>
#   include <memory.h>
#   include <errno.h>
#   include <winsock.h>
#endif

#ifdef SCOUNX 
#   define _SP_SCO3
#   define _SP_UNIX
#   define _SP_SYS_NAME  "sco"
#   include <stdio.h>
#   include <stdlib.h>
#   include <string.h>
#   include <signal.h>
#   include <errno.h>
#   include <sys/types.h> 
#   include <sys/socket.h>
#   include <netinet/in.h>
#   include <netdb.h> 
#   include <sys/select.h> 
#endif

#ifdef LINUX 
#   define _SP_LINUX
#   define _SP_UNIX
#   define _SP_SYS_NAME  "linux"
#   include <stdio.h>
#   include <stdlib.h>
#   include <string.h>
#   include <signal.h>
#   include <errno.h>
#   include <arpa/nameser.h>
#   include <netinet/in.h>
#   include <sys/socket.h>
#   include <sys/types.h>
#   include <linux/posix_types.h>
#   include <netdb.h>
#   include <resolv.h>
#   include <sys/time.h>
#   include <unistd.h>
#endif

#ifdef INTERIX
#   define _SP_LINUX
#   define _SP_UNIX
#   define _SP_SYS_NAME  "interix"
#   include <stdio.h>
#   include <stdlib.h>
#   include <string.h>
#   include <signal.h>
#   include <errno.h>
#   include <netinet/in.h>
#   include <sys/socket.h>
#   include <sys/types.h>
#   include <netdb.h>
#   include <sys/time.h>
#   include <unistd.h>
#endif

#ifdef CYGWIN 
#   define _SP_LINUX
#   define _SP_UNIX
#   define _SP_SYS_NAME  "cygwin"
#   include <stdio.h>
#   include <stdlib.h>
#   include <string.h>
#   include <signal.h>
#   include <errno.h>
#   include <arpa/nameser.h>
#   include <netinet/in.h>
#   include <sys/socket.h>
#   include <sys/types.h>
#   include <netdb.h>
#   include <resolv.h>
#   include <sys/time.h>
#   include <unistd.h>
#endif

#ifdef FREEBSD 
#   define _SP_SUN
#   define _SP_UNIX
#   define _SP_SYS_NAME  "sun"
#   include <stdio.h>
#   include <stdlib.h>
#   include <string.h>
#   include <signal.h>
#   include <errno.h>

#   include <sys/types.h>
#   include <sys/socket.h>
#   include <netinet/in.h>
/*#   include <arpa/nameser.h> */
#   include <arpa/inet.h> 
#   include <netdb.h>
/* #   include <resolv.h> */
#   include <sys/time.h>
#   include <unistd.h>
#endif

#ifdef IBMAIX 
#   define _SP_AIX3
#   define _SP_UNIX
#   define _SP_SYS_NAME  "aix3"
#   include <stdio.h>
#   include <stdlib.h>
#   include <string.h>
#   include <signal.h>
#   include <errno.h>
#   include <sys/types.h> 
#   include <sys/socket.h>
#   include <netinet/in.h>
#   include <netdb.h> 
#   include <sys/select.h> 
#endif
#ifdef  SGI 
#   define _SP_SGI
#   define _SP_UNIX
#   define _SP_SYS_NAME  "irix"
#   include <stdio.h>
#   include <stdlib.h>
#   include <string.h>
#   include <signal.h>
#   include <errno.h>
#   include <sys/socket.h>
#   include <netinet/in.h>
#   include <netdb.h> 
#   include <sys/types.h>
#   include <sys/time.h>
#   include <sys/select.h> 
#   include <unistd.h>
#endif
#ifdef HP9800 
#   define _SP_HPUX
#   define _SP_UNIX
#   define _SP_SYS_NAME  "hpux"
#   include <stdio.h>
#   include <stdlib.h>
#   include <string.h>
#   include <signal.h>
#   include <errno.h>
#   include <sys/types.h> 
#   include <sys/socket.h>
#   include <netinet/in.h>
#   include <netdb.h> 
#   include <sys/time.h>
#   include <unistd.h>
#endif
#ifdef SUNOS 
#   define _SP_SUN
#   define _SP_UNIX
#   define _SP_SYS_NAME  "sun"
#   include <stdio.h>
#   include <stdlib.h>
#   include <string.h>
#   include <signal.h>
#   include <errno.h>
#   include <sys/types.h> 
#   include <sys/socket.h>
#   include <netinet/in.h>
#   include <arpa/inet.h>
#   include <netdb.h> 
#   include <sys/time.h>
#   include <unistd.h>
#endif
#ifdef SUNSOLARIS
#   define _SP_SOLARIS
#   define _SP_UNIX
#   define _SP_SYS_NAME  "sun"
#   include <stdio.h>
#   include <stdlib.h>
#   include <string.h>
#   include <signal.h>
#   include <errno.h>
#   include <sys/types.h> 
#   include <sys/socket.h>
#   include <netinet/in.h>
#   include <arpa/inet.h>
#   include <netdb.h> 
#   include <sys/time.h>
#   include <unistd.h>
#endif

#ifndef C_NULL
#define C_NULL  '\0'
#endif  /* C_NULL */

#ifndef S_NULL
#define S_NULL  "\0"
#endif  /* S_NULL */

/* booleans */
#ifndef FALSE
#define FALSE   0
#endif

#ifndef TRUE
#define TRUE    1
#endif

#ifndef UNDEF
#define UNDEF   -1
#endif

#define CC_SPACE    ' '
#define CAV_SPACE   32


/* ---------------------------------------------------------------- */
#ifdef  _SP_MSWIN32

/* platform name */
#ifndef PATHSLASH
#   define PATHSLASH "\\"
#endif

/* path separator */
#define C_PATHSPT       '\\'

/* global error var */
#define MErrNo \
    WSAGetLastError()

/* set the "errno" text string -- assumes "ScErrNoText" declared */
#define MErrNoTextSet( MTag ) \
    memset( ScErrNoText, C_NULL, sizeof( ScErrNoText ) ); \
    sprintf( ScErrNoText, "%s/%s: [%d] ", FNCNM, MTag, MErrNo ); \
    strncat( ScErrNoText, CSUtlErrNo2Text( MErrNo ), \
            sizeof( ScErrNoText ) - strlen( ScErrNoText ) - 1 )

/* debugs */
#ifdef _SFCS_DEBUG
#define MDbg0( MPrm1 ) \
    fprintf( stdout, "%s: %s", FNCNM, MPrm1 ); \
    fflush( stdout )
#else
#define MDbg0( MPrm1 )
#endif

#ifdef _SFCS_DEBUG
#define MDbg1( MTag, MPrm1 ) \
    fprintf( stdout, "%s: ", FNCNM ); \
    fprintf( stdout, MTag, MPrm1 ); \
    fflush( stdout )
#else
#define MDbg1( MTag, MPrm1 )
#endif

#ifdef _SFCS_DEBUG
#define MDbg2( MTag, MPrm1, MPrm2 ) \
    fprintf( stdout, "%s: ", FNCNM ); \
    fprintf( stdout, MTag, MPrm1, MPrm2 ); \
    fflush( stdout )
#else
#define MDbg2( MTag, MPrm1, MPrm2 )
#endif

/* if non-threading */
#ifdef _MT
/* Thread Local Storage (TLS) macro */
#define M_TLS  __declspec(thread)
#else
#define M_TLS
#endif

#endif  /* _SP_MSWIN32 */

/* ---------------------------------------------------------------- */
#ifdef  _SP_UNIX

#ifdef _SP_SOLARIS
#define Mbcopy( MDest, MSrc, MLen ) memmove( MSrc, MDest, MLen );
#else
#define Mbcopy( MDest, MSrc, MLen ) bcopy( MSrc, MDest, MLen );
#endif

#   define PATHSLASH "/"
#   define FAR

#define C_PATHSPT           '/'

#define MErrNo errno

/* set the "errno" text string -- assumes "ScErrNoText" declared */
#define MErrNoTextSet( MTag ) \
    memset( ScErrNoText, C_NULL, sizeof( ScErrNoText ) ); \
    sprintf( ScErrNoText, "%s/%s: [%d] ", FNCNM, MTag, MErrNo ); \
    strncat( ScErrNoText, SFCSUtlErrNo2Text( MErrNo ), \
            sizeof( ScErrNoText ) - strlen( ScErrNoText ) - 1 )

/* debugs */
#ifdef _SFCS_DEBUG
#define MDbg0( MPrm1 ) \
    fprintf( stderr, "%s: %s", FNCNM, MPrm1 )
#else
#define MDbg0( MPrm1 )
#endif

#ifdef _SFCS_DEBUG
#define MDbg1( MTag, MPrm1 ) \
    fprintf( stderr, "%s: ", FNCNM ); \
    fprintf( stderr, MTag, MPrm1 )
#else
#define MDbg1( MTag, MPrm1 )
#endif

#ifdef _SFCS_DEBUG
#define MDbg2( MTag, MPrm1, MPrm2 ) \
    fprintf( stderr, "%s: ", FNCNM ); \
    fprintf( stderr, MTag, MPrm1, MPrm2 )
#else
#define MDbg2( MTag, MPrm1, MPrm2 )
#endif

#endif /* _SP_UNIX */

/* ---------------------------------------------------------------- */
/* common definitions */

#define SZ_ERRNOTEXT    128

/* print errno test -- pass function pointer */
#define MErrNoTextPrint( Mpf ) \
    { \
       char *_pcErrNoText = NULL; \
       _pcErrNoText = Mpf; \
       if ( _pcErrNoText != NULL ) \
        printf( "%s\n", _pcErrNoText ); \
    }

/* env variables */
#define ENV_VAR_SQLDIR      "SQLDIR"
#define ENV_VAR_SQLFLEXSVC  "SQLFLEXSVC"

/* general variables */
#define G_VAR_SQLHOSTS      "sqlhosts"

/* status codes */
#define SFCS_SC_OK              0
#define CS_ERR_SEL_SVC         -901
#define CS_ERR_HOSTNAME        -902
#define CS_ERR_PROTOCOL        -903
#define CS_ERR_SERVICE         -904
#define CS_ERR_NO_SVC_ENV      -905
#define CS_ERR_NO_DIR_ENV      -906
#define CS_ERR_NO_SHF          -907
#define CS_ERR_NO_SQL_SVC      -908
#define CS_ERR_BIND            -909
#define CS_ERR_CONNECT         -910
#define CS_ERR_CONN_TYPE       -911
#define CS_ERR_READ_NULLPTR    -912
#define CS_ERR_CRT_SOCKET      -913
#define CS_ERR_NO_SOCKET       -914
#define CS_ERR_RECEIVE         -915
#define CS_ERR_SEND            -916
#define CS_ERR_MID_OP          -917
#define CS_ERR_NOT_OPEN        -918
#define CS_ERR_NO_CONNECT      -919
#define CS_ERR_LOST_CONNECT    -920
#define CS_ERR_NOT_CLIENT      -921
#define CS_ERR_NOT_SERVER      -922
#define CS_ERR_NULL_FUNC       -923

/* select services operations */
#define SFCS_SEL_OP_READ    -1
#define SFCS_SEL_OP_WRITE   -2

#ifndef MAXPTHSIZ
#define MAXPTHSIZ  1024
#endif


#ifdef  _SP_MSWIN32
int CSUtlEnvInit( char *SQLSvcName );
int CSUtlSelCk( int Op, int Fd, int To );
int CSSHFindBySQLSvcName( char *SQLSvcName, char *PrtclName, char *HostName, char *SvcName );
char *CSUtlSC2Text( int SC, int *sqlerr );
char *CSUtlErrNo2Text( int ErrNo );
char *_CSUtlSFDirPath();
char *_CSUtlSQLSvcName();
char *_CSUtlErrNoText();
#endif /* _SP_MSWIN32 */

#ifdef _SP_UNIX
/* declarations */
#define CSUtlSC2Text      SFCSUtlSC2Text
#define _CSUtlSQLSvcName  _SFCSUtlSQLSvcName
int SFCSUtlEnvInit( char *SQLSvcName );
int SFCSUtlSelCk( int Op, int Fd, int To );
int SFCSSHFindBySQLSvcName( char *SQLSvcName, char *PrtclName, char *HostName, char *SvcName );
char *SFCSUtlSC2Text( int SC, int *sqlerr );
char *SFCSUtlErrNo2Text( int ErrNo );
char *_SFCSUtlSFDirPath();
char *_SFCSUtlSQLSvcName();
char *_SFCSUtlErrNoText();
#endif /* _SP_UNIX */

char *loadmsg();

/* ---------------------------------------------------------------- */
#ifdef  _SP_MSWIN32

#define WSA_PREF_VER        0x0101      // Preferred version
#define SFCS_NO_SOCKET      INVALID_SOCKET

/* Timer(s) */
#define CWSA_NET_TIMER1_ID  13
#define CWSA_NET_TIMER1_TO  6

/* socket close */
#define MSktClose( MSkt ) shutdown( MSkt, 2 )

/* Byte copy */
#define Mbcopy( MDest, MSrc, MLen ) memcpy( MDest, MSrc, MLen )

#endif  /* _SP_MSWIN32 */
/* ---------------------------------------------------------------- */

/* ---------------------------------------------------------------- */
#ifdef  _SP_UNIX

/* socket close */
#define MSktClose( MSkt ) close( MSkt )

#define SOCKET int

#define INVALID_SOCKET      -1
#define SFCS_NO_SOCKET      INVALID_SOCKET
#endif  /* _SP_UNIX */

/* I/O operations timeout */
#define NET_READ_TO     10
#define NET_WRITE_TO    10

/* connection types */
#define SFCS_CT_UNDEF       -1
#define SFCS_CT_SERVER      -1000
#define SFCS_CT_CLIENT      -1001

/* socket types (see appropriate include files) */
#define SOCK_UNDEF          -1

/* Protocols */
#define SFCS_NET_PRTCL_TCP  "tcp"
#define SFCS_NET_PRTCL_UDP  "udp"

/* state codes */
#define SFCS_STC_INIT       -200
#define SFCS_STC_STARTED    -201
#define SFCS_STC_CONN       -202

#ifdef  _SP_MSWIN32
/* declarations */
void CSNetBInit( void );
int CSNetBOpen( char *a_pcHostName, char *a_pcPrtclName, char *a_pSvcName );
void CSNetBClose( void );
int CSNetBConnect( int ConnType );
SOCKET _CSNetBSktID();
int _CSNetBStateC();
int _CSNetBConnType();
char *_CSNetBErrNoText();
int CSNetOpen( char *SQLSvcName, int ConnType );
void CSNetClose();
int CSNetConnect( int ConnType );
SOCKET _CSNetSktID();
void CSNetSktID_( SOCKET SktID );
char *_CSNetErrNoText();
int CSNetClientExec( int (*pfProc)( void ) );
char *_CSNetCErrNoText();
/* maximum server connections -- this is the value to change for
   allowed client connections */
int CSNetServerExec( void (*AppProc)( void * ) );
char *_CSNetSErrNoText();
int  CSNetSMsgDisConnCk( void );
int CSNetRead( SOCKET SsSktID, char *a_pcData, int *a_piDataLen );
int CSNetWrite( SOCKET SsSktID, char *a_pcData, int *a_piDataLen );
int CSNetERead( char *a_pcData, int *a_piDataLen );
int CSNetEWrite( char *a_pcData, int *a_piDataLen );
char *_CSNetEErrNoText();
SOCKET _CSNetESktID();
void CSNetESktID_( SOCKET a_sSktID );
#endif  /* _SP_MSWIN32 */

#ifdef _SP_UNIX
/* declarations */
#define CSNetBClose SFCSNetBClose
void SFCSNetBInit( void );
int SFCSNetBOpen( char *a_pcHostName, char *a_pcPrtclName, char *a_pSvcName );
void SFCSNetBClose( void );
int SFCSNetBConnect( int ConnType );
SOCKET _SFCSNetBSktID();
int _SFCSNetBStateC();
int _SFCSNetBConnType();
char *_SFCSNetBErrNoText();
#define CSNetOpen   SFCSNetOpen
#define CSNetRead   SFCSNetRead
#define CSNetWrite  SFCSNetWrite
/* declarations */
int SFCSNetOpen( char *SQLSvcName, int ConnType );
void SFCSNetClose();
int SFCSNetConnect( int ConnType );
int SFCSNetRead( char *Data, int *DataLen );
int SFCSNetWrite( char *Data, int *DataLen );
SOCKET _SFCSNetSktID();
void SFCSNetSktID_( SOCKET SktID );
char *_SFCSNetErrNoText();

#define CSNetERead   SFCSNetERead
#define CSNetEWrite  SFCSNetEWrite
/* declarations */
int SFCSNetERead( char *Bfr, int *DataLen );
int SFCSNetEWrite( char *Bfr, int *DataLen );
#endif /* _SP_UNIX */

/* SQL Flex network messages */

/* message types */
#define MT_CMD              "MCD"
#define MT_ACK              "MAK"
#define MT_INFO             "MIF"
#define MT_DATA             "MDT"

/* command messages */
#define MSG_CMD_LOGIN       "LIN"
#define MSG_CMD_LOGOUT      "LOT"
#define MSG_CMD_SHUTDOWN    "SDN"
#define MSG_CMD_NONE        "NOP"

/* acknowledgement messages */
#define MSG_ACK_OK          "AOK"
#define MSG_ACK_NOT_OK      "NOK"

/* info messages */
#define MSG_INFO_USERS      "USR"

/* endpoints */
#define EP_ADM_CLIENT       "ACT"
#define EP_DB_CLIENT        "DCT"
#define EP_SERVER           "SVR"
#define EP_SUBPROC          "SBP"

/* maximum server connections -- this is the value to change for
   allowed client connections */
#define SERVER_MAX_CONN     16

/* server connect timeout */
#define SERVER_CONN_TO      10

#ifdef  _SP_MSWIN32
int CSNetMgtMsgAsm( char *Bfr, int *BfrSize, char *MsgType, char *Msg );
int CSNetMgtMsgDisAsm( char *Bfr, int *BfrSize );
char *_CSNetMgtMsgMsgType();
char *_CSNetMgtMsgMsg();
char *_CSNetMgtMsgSndr();
char *_CSNetMgtMsgRcvr();
char *_CSNetMgtMsgSndrSys();
char *_CSNetMgtMsgSndrUser();
char *_CSNetMgtMsgData();
void CSNetMgtMsgSndrDflt_( char *a_pcSndrDflt );
struct  s_sSubProcList
{
    ULONG   ulTId;              /* Thread ID */
    BOOL    bThreadA;           /* Thread Active */
    SOCKET  sSktID;
    char    cClientSys[ 16 ];   /* Sender Platform Name */
    char    cClientIPAddr[ 16 ];
    char    cClientUser[ 16 ];  /* Sender user name */
    int     iReadC;             /* read count */
    int     iWriteC;            /* write count */
}; /* struct    s_sSubProcList */

/* declarations */
int CSNetSPLInit( void );
struct s_sSubProcList *CSNetSPLFindPtrByIx( int ix );
int _CSNetSPLIx();
int CSNetSPLAdd( SOCKET a_sSktID,
                   char *a_pcClientSys,
                   char *a_pcClientIPAddr,
                   char *a_pcClientUser );
int CSNetSPLFindIxBySktID( SOCKET a_sSktID );
int CSNetSPLDel( int a_ix );
void CSNetSPLReadCI( int a_ix );
void CSNetSPLWriteCI( int a_ix );
#endif  /* _SP_MSWIN32 */

#ifdef _SP_UNIX
int SFCSNetMgtMsgAsm( char *Bfr, int *BfrSize, char *MsgType, char *Msg );
int SFCSNetMgtMsgDisAsm( char *Bfr, int *BfrSize );
char *_SFCSNetMgtMsgMsgType();
char *_SFCSNetMgtMsgMsg();
char *_SFCSNetMgtMsgSndr();
char *_SFCSNetMgtMsgRcvr();
char *_SFCSNetMgtMsgSndrSys();
char *_SFCSNetMgtMsgSndrUser();
char *_SFCSNetMgtMsgData();
void SFCSNetMgtMsgSndrDflt_();
int SFCSNetServerExec( char *SubProcExecName );
char *_SFCSNetSErrNoText();
int  SFCSNetSMsgDisConnCk( void );
#define CSNetClientExec  SFCSNetClientExec
int SFCSNetClientExec( int (*pfProc)( void ) );
char *_SFCSNetCErrNoText();
struct  s_sSubProcList
{
  int       iPid;
  SOCKET    sSktID;
  char      cClientSys[ 16 ];    /* Sender Platform Name */
  char      cClientIPAddr[ 16 ];
  char      cClientUser[ 16 ];   /* Sender user name */
}; /* struct    s_sSubProcList */
struct s_sSubProcList *SFCSNetSPLFindPtrByIx( int ix );
int _SFCSNetSPLIx();
#endif /* _SP_UNIX */

#endif  /* _SFCSNET_H */
