/*****************************************************************************/
/*
                                ProxyStruct.h

08-AUG-1998  MGD  initial
*/
/*****************************************************************************/

#ifndef PROXYSTRUCT_H_LOADED
#define PROXYSTRUCT_H_LOADED 1

#include <atrdef.h>
#include <fibdef.h>

#include "enamel.h"
#include "auth.h"
#include "config.h"
#include "net.h"
#include "odsstruct.h"
#include "proxytunnel.h"

#ifndef __VAX
#   pragma nomember_alignment
#endif

/* forward references */
typedef struct RequestStruct REQUEST_STRUCT;
typedef struct ServiceStruct SERVICE_STRUCT;

/********************/
/* proxy accounting */
/********************/

#ifndef __VAX
#   pragma member_alignment __save
#   pragma member_alignment
#endif

#define PROXY_ACCOUNTING_STATUS_STRING_SIZE 128

#define TUNNEL_COUNT_MAX 5

typedef struct ProxyAccountingStruct PROXY_ACCOUNTING_STRUCT;

struct ProxyAccountingStruct
{
   BOOL  CacheEnabled,
         FreeSpaceAvailable,
         ServingEnabled;

   int  CacheReadCount,
        CacheRead304Count,
        CacheWriteCount,
        ConnectIpv4Count,
        ConnectIpv6Count,
        ConnectPersistCount,
        ConnectPersistFull,
        ConnectPersistMax,
        ConnectPersistPeak,
        FtpCount,
        FtpDeleCount,
        FtpDosCount,
        FtpListCount,
        FtpLoginFailCount,
        FtpRetrCount,
        FtpStorCount,
        FtpUnixCount,
        FtpUnknownCount,
        FtpVmsCount,
        /* ipv4/ipv6 -> ipv4/ipv6 */
        GatewayIpvCount[2][2],
        /* http/https -> http/https/ftp */
        GatewaySchemeCount[2][3],
        MethodConnectCount,
        MethodDeleteCount,
        MethodExtensionCount,
        MethodGetCount,
        MethodHeadCount,
        MethodOptionsCount,
        MethodPostCount,
        MethodPutCount,
        MethodSshCount,
        MethodTraceCount,
        MethodWebDavCopyCount,
        MethodWebDavLockCount,
        MethodWebDavMkColCount,
        MethodWebDavMoveCount,
        MethodWebDavPropFindCount,
        MethodWebDavPropPatchCount,
        MethodWebDavUnLockCount,
        PrevFileCount,
        RequestCannotCacheCount,
        ResponseCannotCacheCount,
        /* [service-in][proxy-out] */
        TunnelCount[TUNNEL_COUNT_MAX][TUNNEL_COUNT_MAX],
        TunnelCurrent,
        Verify200Count,
        Verify403Count,
        Verify404Count,
        VerifyCurrentCount,
        VerifyFindRecordCount,
        VerifyFullCount,
        VerifySetRecordCount;

   ulong  /* count of TOTAL blocks received from the server */
          BlocksRawRx [QUAD2],
          /* count of TOTAL blocks sent to the server */
          BlocksRawTx [QUAD2],
          /* count of bytes received from proxied server */
          BytesCacheRx [QUAD2],
          /* count of bytes sent to proxied server */
          BytesCacheTx [QUAD2],
          /* bytes received from proxied server - not cacheable */
          BytesCannotCacheRx [QUAD2],
          /* bytes sent to proxied server - not cacheable */
          BytesCannotCacheTx [QUAD2],
          /* count of bytes in cache-serviced request header */
          BytesRawRx [QUAD2],
          /* count of bytes in cache-serviced response */
          BytesRawTx [QUAD2],
          /* HTTP status status code counts from cache */
          CacheStatusCodeCount [6],
          /* HTTP status status code counts from network */
          NetStatusCodeCount [6];

   char  StatusString [PROXY_ACCOUNTING_STATUS_STRING_SIZE];
};

#ifndef __VAX
#   pragma member_alignment __restore
#endif

/**************************************/
/* proxy->server connection structure */
/**************************************/

typedef struct ProxyConnectStruct PROXY_CONNECT;

struct ProxyConnectStruct
{
   LIST_ENTRY  ListEntry;

   int  /* how many times the TCP/IP connection has been (re)used */
        ConnectionCount,
        /* server tick-second when the connection was placed on the list */
        EntryTickSecond,
	/* is this connection for a SSL client enabled service */
	SSLclientEnabled;

   ushort  /* connect port at remote server */
           ConnectPort;

   IPADDRESS  /* address of remote server */
              ConnectIpAddress;

   /* pointer to proxy network I/O structure */
   NETIO_STRUCT  *NetIoPtr;
};

/**************************/
/* proxy cache structures */
/**************************/

/* i.e. originated and works with any after v9.0.0 (previously v6.0.0) */
#define PROXY_CACHE_FILE_VERSION 0x90000

typedef struct ProxyCacheDescrStruct PROXY_CACHE_DESCR;

struct ProxyCacheDescrStruct
{
   int  CacheVersion,
        UrlLength,
        HeaderLength;
};

typedef struct ProxyCacheFileQioStruct PROXY_CACHE_FILE_QIO;

struct ProxyCacheFileQioStruct
{
   BOOL  EndOfFile;
   int  AdjustBuffer,
        AllocatedVbn,
        BlockNumber,
        BufferCount,
        BufferSize,
        EndOfFileVbn,
        FirstFreeByte,
        SizeInBytes;
   ushort  AcpChannel,
           QioChannel;
   ulong  BdtTime64 [QUAD2],
          CdtTime64 [QUAD2],
          EdtTime64 [QUAD2],
          RdtTime64 [QUAD2];
   char  *BufferPtr;
   char  FileName [40];
   ACPQIO_ASC_DATES  AscDates;
   ACPQIO_RECORD_ATTRIBUTES  RecAttr;
   ATRDEF  FileAtr [7];
   struct dsc$descriptor  FibDsc,
                          FileNameDsc;
   IO_SB  IOsb;

#ifdef ODS_EXTENDED
   /* only use the local ENAMEL.H FIB definition when necessary */
#  ifdef ENAMEL_FIBDEF
      struct enamel_fibdef  Fib;
#  else
      FIBDEF  Fib;
#  endif
#else /* ODS_EXTENDED */
   FIBDEF  Fib;
#endif /* ODS_EXTENDED */
};

/*****************************************/
/* reverse proxy verification structures */
/*****************************************/

#define PROXY_VERIFY_MAX_AUTH_LENGTH 96

typedef struct ProxyVerifyRecord PROXYVERIFY_RECORD;

struct ProxyVerifyRecord
{
   int  AuthorizationStringLength,
        VerifyAttemptCount;
   ulong  SourceRealm;
   char  AuthorizationString [PROXY_VERIFY_MAX_AUTH_LENGTH+1],
         HttpdPrcNam [15+1],
         RealmName [AUTH_MAX_REALM_GROUP_LENGTH+1],
         RemoteUser [AUTH_MAX_USERNAME_LENGTH+1];
};

typedef struct ProxyVerifyGblSecStruct PROXYVERIFY_GBLSEC;

struct ProxyVerifyGblSecStruct
{
   ulong  GblSecVersion,
          GblSecLength;

   int  RecordCount;
   ulong  SinceTime64 [QUAD2];

    /* verify records are stored from this point onwards */
   char  RecordPool [];
};

/************************/
/* proxy task structure */
/************************/

#ifndef __VAX
#   pragma member_alignment __save
#   pragma member_alignment
#endif

/* include to get the GZIP_COMPRESS structure */
#define GZIP_COMPRESS_ONLY
#include "gzip.h"
#undef GZIP_COMPRESS_ONLY

typedef struct ProxyTaskStruct PROXY_TASK;

struct ProxyTaskStruct
{
   BOOL  WatchItem,
         CannotCache,
         FtpDirList,
         FtpHasVersion,
         FtpListAlt,
         FtpListHide,
         FtpListRaw,
         FtpStorDone,
         FtpTypeDone,
         ParseInUse,
         PersistentRequest,
         PersistentResponse,
	 ProxyAffinityInUse,
         ProxyCacheBeyondHeader,
         ProxyCacheFileExisted,
         ProxyCacheSuitable,
         ProxySslHttpRequest,
         ResponseHeaderSent,
         RequestAuthorization,
         RequestNotFromCache,
         ResponseCacheControlMaxAgeZero,
         ResponseCacheControlMustReval,
         ResponseCacheControlNoCache,
         ResponseCacheControlNoStore,
         ResponseCacheControlNoTransform,
         ResponseCacheControlPrivate,
         ResponseCacheControlProxyReval,
         ResponseCacheControlPublic,
         ResponseContentEncodingGzip,
         ResponseContentEncodingUnknown,
         ResponseContentTypeMultipart,
         ResponseContentTypeText,
         ResponseChunkedEnd,
         ResponseChunkedEol,
         ResponseChunkedEot,
         ResponseChunkedInit,
         ResponseConnectionClose,
         ResponseConnectionKeepAlive,
         ResponseKeepAlive,
         ResponseNegative,
         ResponseNoCache,
         ResponseTransferEncodingChunked,
         ResponseTransferEncodingUnsupported,
         ResponseUpgradeWebSocket,
         ResponseVaryUnsupported,
	 TunnelEstablished,
         TunnelRequestGenerated;

   int  ChainIpPort,
        ConnectPort,
        Ftp230Length,
        FtpCommandCount,
        FtpCommandSize,
        FtpFilePathLength,
        FtpFileSystem,
        FtpResponseClass,
        FtpResponseCode,
        FtpResponseCount,
        FtpResponseLineCount,
        FtpResponseRemaining,
        FtpResponseSize,
        FtpSpecific,
        FtpState,
        HttpMethod,
        ConnectionCount,
        ProxyCacheFileNameLength,
        ProxyCacheFileSizeInBytes,
        ProxyCacheLastModifiedHours,
        ProxyCacheReadBytes,
        ProxyLookupRetryCount,
        ProxyTunnel,
        QueuedBodyRead,
        RebuiltRequestLength,
        RebuiltResponseLength,
        RequestHostPortLength,
        RequestHttpMethod,
        RequestHttpVersion,
        RequestPort,
        RequestScheme,
        ResponseBodyLength,
        ResponseBufferCount,
        ResponseBufferCacheCount,
        ResponseBufferNetCount,
        ResponseBufferRemaining,
        ResponseBufferSize,
        ResponseBytes,
        ResponseCacheControlMaxAge,
        ResponseCacheControlSMaxAge,
        ResponseChunkedCount,
        ResponseChunkedSize,
        ResponseChunkedNewlineCount,
        ResponseConsecutiveNewLineCount,
        ResponseContentLength,
        ResponseHeaderLength,
        ResponseHttpVersion,
        ResponseStatusCode;

   ushort  FtpDataIpPort;

   ulong  ProxyCacheFileCdt [QUAD2],
          ResponseExpiresTime64 [QUAD2],
          ResponseLastModifiedTime64 [QUAD2];

   char  *ChainHostPortPtr,
         *ConnectHostPortPtr,
         *Ftp230Ptr,
         *FtpCommandPtr,
         *FtpContentTypePtr,
         *FtpFileSystemPtr,
         *FtpFilePathPtr,
         *FtpNextResponsePtr,
         *FtpResponsePtr,
         *FtpResponseCurrentPtr,
         *FtpWildPtr,
         *RebuiltRequestPtr,
         *RebuiltResponsePtr,
         *ResponseBufferPtr,
         *ResponseBufferCachePtr,
         *ResponseBufferCurrentPtr,
         *ResponseBufferNetPtr,
         *RequestHttpCookiePtr,
         *RequestHttpMethodNamePtr,
         *ResponseHeaderPtr,
         *RequestUriPtr,
         *RequestUriQueryStringPtr;

   char  AffinityDomain [32],
         ChainHostPort [TCPIP_HOSTNAME_MAX+1+8],
         ProxyCacheDirectoryName [64],
         ProxyCacheMd5HexDigest [33],
         RequestHostName [TCPIP_HOSTNAME_MAX+1],
         RequestHostPort [TCPIP_HOSTNAME_MAX+1+8],
         RequestPortString [16],
         RequestSchemeName [16],
         ResponseChunkedString [32],
         ResponseExpires [32],
         ResponseHttpProtocol [16],
         ResponseLastModified [48],
         UrlPassword [32],
         UrlUserName [32];

   /* the cache and FTP storage are not required at the same time */
   variant_union {
      char  ExpandedFileName [256]; 
      char  FtpCWD [256]; 
   } Overlay1;
   variant_union {
      char  ProxyCacheFileName [128];
      char  FtpSYST [128];
   } Overlay2;

   IPADDRESS  BindIpAddress,
              ChainIpAddress,
              ConnectIpAddress,
              FtpDataBindIpAddress,
              FtpDataIpAddress,
              RequestHostIpAddress;

   /* pointer to service struct */
   SERVICE_STRUCT  *ServicePtr;

   /* pointer to associated request (if any) */
   REQUEST_STRUCT  *RequestPtr;

   /* structure is used to get/set file characteristics */
   PROXY_CACHE_FILE_QIO  CacheFileQio;

   /* pointer to structure holding reverse-proxy verification data */
   PROXYVERIFY_RECORD  *VerifyRecordPtr;

   /* RMS structures */
   struct FAB  LoadFab;
   struct RAB  LoadRab;
   struct FAB  ParseFab;
   struct NAM  ParseNam;
   struct XABDAT  LoadXabDat;
   struct XABPRO  LoadXabPro;

   ushort  FtpDataChannel;

   /* network read and write buffer */
   char  *FtpDataReadRawDataPtr,
         *FtpDataWriteRawDataPtr,
         *ProxyReadRawDataPtr,
         *ProxyWriteRawDataPtr;
   int  FtpDataReadRawDataSize,
        FtpDataWriteRawDataCount,
        ProxyReadRawDataSize,
        ProxyWriteRawDataCount;

   /* pointer to proxy network I/O structure */
   NETIO_STRUCT  *NetIoPtr;

   /* ZLIB (compressed) stream to/from remote server */
   GZIP_COMPRESS  GzipCompress;

   /* AST function pointers */
   PROXY_AST  FtpAstFunction;
   REQUEST_AST  FtpDataReadRawAstFunction;
   REQUEST_AST  FtpDataWriteRawAstFunction;

   /* I/O status blocks */
   IO_SB  FtpDataConnectIOsb,
          FtpDataReadIOsb,
          FtpDataWriteIOsb,
          ProxyConnectIOsb;

   SOCKADDRESS  FtpDataSocketName,
                FtpDataBindSocketName,
                ProxySocketName,
                ProxyBindSocketName;

   TCPIP_HOST_LOOKUP  HostLookup;

   VMS_ITEM_LIST2  FtpDataBindSocketNameItem,
                   FtpDataSocketNameItem, 
                   ProxyBindSocketNameItem,
                   ProxySocketNameItem;
};

#ifndef __VAX
#   pragma member_alignment __restore
#endif

#endif /* PROXYSTRUCT_H_LOADED */

/*****************************************************************************/

