/*****************************************************************************/
/*
                                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"

#define PROXY_REWORK_COUNT 4

#pragma nomember_alignment

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

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

#pragma member_alignment __save
#pragma member_alignment

#define TUNNEL_COUNT_MAX 5

typedef struct ProxyAccountingStruct PROXY_ACCOUNTING_STRUCT;

struct ProxyAccountingStruct
{
   BOOL  ServingEnabled;

   int  ConnectIpv4Count,
        ConnectIpv6Count,
        /* ipv4/ipv6 -> ipv4/ipv6 */
        GatewayIpvCount[2][2],
        /* http/https -> http/https */
        GatewaySchemeCount[2][2],
        ReworkCount,
        ReworkNoType,
        ReworkReplaceCount,
        ReworkReplaceSearch,
        ReworkTooBig,
        Socks5Count,
        Socks5FailCount,
        Socks5SuccessCount,
        /* [service-in][proxy-out] */
        TunnelCount[TUNNEL_COUNT_MAX][TUNNEL_COUNT_MAX],
        TunnelCurrent,
        Verify200Count,
        Verify403Count,
        Verify404Count,
        VerifyCurrentCount,
        VerifyFindRecordCount,
        VerifyFullCount,
        VerifySetRecordCount;

   int64  /* count of TOTAL blocks received from the server */
          BlocksRawRx64,
          /* count of TOTAL blocks sent to the server */
          BlocksRawTx64,
          /* count of bytes in cache-serviced request header */
          BytesRawRx64,
          /* count of bytes in cache-serviced response */
          BytesRawTx64;
};

#pragma member_alignment __restore

/**************************************/
/* 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;
};

/*****************************************/
/* 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;
   int64  SinceTime64;

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

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

#pragma member_alignment __save
#pragma member_alignment

/* 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,
         ParseInUse,
         PersistentRequest,
         PersistentResponse,
	 ProxyAffinityInUse,
         ProxySslHttpRequest,
         RequestAuthorization,
         ResponseCacheControlMaxAgeZero,
         ResponseCacheControlMustReval,
         ResponseCacheControlNoCache,
         ResponseCacheControlNoStore,
         ResponseCacheControlNoTransform,
         ResponseCacheControlPrivate,
         ResponseCacheControlProxyReval,
         ResponseCacheControlPublic,
         ResponseContentEncodingGzip,
         ResponseContentEncodingUnknown,
         ResponseChunkedEnd,
         ResponseChunkedEol,
         ResponseChunkedEot,
         ResponseChunkedInit,
         ResponseConnectionClose,
         ResponseConnectionKeepAlive,
         ResponseKeepAlive,
         ResponseNegative,
         ResponseNoCache,
         ResponseReworkInit,
         ResponseTransferEncodingChunked,
         ResponseTransferEncodingUnsupported,
         ResponseUpgradeWebSocket,
         ResponseVaryUnsupported,
	 TunnelEstablished,
         TunnelRequestGenerated;

   int  ChainIpPort,
        ConnectPort,
        HttpMethod,
        ConnectionCount,
        ProxyLookupRetryCount,
        ProxyTunnel,
        QueuedBodyRead,
        RebuiltHeaderLength,
        RebuiltRequestLength,
        RequestHostPortLength,
        RequestHttpMethod,
        RequestHttpVersion,
        RequestPort,
        RequestScheme,
        ResponseBodyLength,
        ResponseBufferCount,
        ResponseBufferNetCount,
        ResponseBufferRemaining,
        ResponseBufferSize,
        ResponseBytes,
        ResponseCacheControlMaxAge,
        ResponseCacheControlSMaxAge,
        ResponseChunkedCount,
        ResponseChunkedSize,
        ResponseChunkedNewlineCount,
        ResponseConsecutiveNewLineCount,
        ResponseContentLength,
        ResponseHeaderLength,
        ResponseHttpVersion,
        ResponseReworkMax,
        ResponseStatusCode;

   int64  ResponseExpiresTime64,
          ResponseLastModifiedTime64;

   char  *ChainHostPortPtr,
         *ConnectHostPortPtr,
         *RebuiltHeaderPtr,
         *RebuiltRequestPtr,
         *ResponseBufferPtr,
         *ResponseBufferCurrentPtr,
         *ResponseBufferNetPtr,
         *ResponseContentTypePtr,
         *RequestHttpCookiePtr,
         *RequestHttpMethodNamePtr,
         *ResponseHeaderPtr,
         *RequestUriPtr,
         *RequestUriQueryStringPtr;

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

   int  ReworkLen1 [PROXY_REWORK_COUNT],
        ReworkLen2 [PROXY_REWORK_COUNT];

   char  *ReworkPtr1 [PROXY_REWORK_COUNT],
         *ReworkPtr2 [PROXY_REWORK_COUNT];

   IPADDRESS  BindIpAddress,
              ChainIpAddress,
              ConnectIpAddress,
              RequestHostIpAddress;

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

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

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

   /* network read and write buffer */
   char  *ProxyReadRawDataPtr,
         *ProxyWriteRawDataPtr;
   int  ProxyReadRawDataSize,
        ProxyWriteRawDataCount;

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

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

   /* I/O status blocks */
   IO_SB  ProxyConnectIOsb;

   SOCKADDRESS  ProxySocketName,
                ProxyBindSocketName;

   TCPIP_HOST_LOOKUP  HostLookup;

   VMS_ITEM_LIST2  ProxyBindSocketNameItem,
                   ProxySocketNameItem;
};

#pragma member_alignment __restore

#endif /* PROXYSTRUCT_H_LOADED */

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

