/*<ATGCOPYRIGHT>
 * Copyright (C) 1997 Art Technology Group, Inc.
 * All Rights Reserved.  No use, copying or distribution of this
 * work may be made except in accordance with a valid license
 * agreement from Art Technology Group.  This notice must be
 * included on all copies, modifications and derivatives of this
 * work.
 *
 * Art Technology Group (ATG) MAKES NO REPRESENTATIONS OR WARRANTIES 
 * ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. ATG SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, 
 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 * "Dynamo" is a trademark of Art Technology Group, Inc.
 </ATGCOPYRIGHT>*/


/*
  DRPTypes.h - typedefs and constant #defines
 */


#ifndef _DRP_TYPES_H_
#define _DRP_TYPES_H_


#ifdef WIN32
/* NT */
#include <windows.h>
#include <winsock.h>
#else
/* UNIXen */
#include <sys/types.h>
#include <errno.h>
#endif


/* DRP Packet identifiers:  identify the type of DRP packet
   being sent by the CM or DRP server */
enum {
  kUnknownType           = 0,      /* placeholder - not used */
  kBeginRequest,       /*  1 */    /* CM sends to start response cycle */
  kReply,              /*  2 */    /* server sends to supply response headers,
			              response data, etc */
  kGetParameters,      /*  3 */    /* server sends to get add'l req params */
  kReadDataStream,     /*  4 */    /* server sends to read agent data (post) */
  kBeginLogRequest,    /*  5 */    /* server sends a log request */

  kPacketTypePlaceholder           /* placeholder - not used */
};


/* Typed-Literal data types: identify the type of object contained in a
   DRP2 TypedLiteral */
enum {
  kNull             = 0,      /* null object (not placeholder) */
  kInteger,        /* 1 */    /* 4-byte integer */
  kAscii,          /* 2 */
  kUnicode,        /* 3 */
  kDefined,        /* 4 */    /* pre-defined string placeholder (integral) */

  kTLiteralTypePlaceholder    /* placeholder - not used */
};


/* defined string identifiers: pre-defined string placeholders */
enum {
  kContentLength           = 1,
  kContentType,          /*  2 */
  kParameter,            /*  3 */
  kParameters,           /*  4 */
  kProtocol,             /*  5 */
  kRemoteAddr,           /*  6 */
  kRemoteHost,           /*  7 */
  kServerName,           /*  8 */
  kServerPort,           /*  9 */
  kAuthType,             /* 10 */
  kHeaderName,           /* 11 */
  kHeaderVal,            /* 12 */
  kHeaders,              /* 13 */
  kMethod,               /* 14 */
  kPathInfo,             /* 15 */
  kPathTranslated,       /* 16 */
  kQueryString,          /* 17 */
  kRemoteUser,           /* 18 */
  kRequestURI,           /* 19 */
  kServletPath,          /* 20 */
  kScheme,               /* 21 */
  kUserAgent,            /* 22 */

  kDefinedPlaceholder
};


/* socket_t: abstracted socket type for WIN32 & UNIXen */
#ifdef WIN32
typedef SOCKET          socket_t;
#define kInvalidSocket  INVALID_SOCKET
#else
typedef int             socket_t;
#define kInvalidSocket  -1
#ifndef INADDR_NONE
#define INADDR_NONE     -1
#endif
#endif

#define ATG_INVALID_DYNSOCKET NULL

#ifdef WIN32
#  define ATG_INVALID_SOCKET  INVALID_SOCKET
#  define ATG_SOCKET_ERROR    SOCKET_ERROR
#  define ATG_INADDR_NONE     INADDR_NONE
#  define net_error() WSAGetLastError()
#else   
#  define ATG_INVALID_SOCKET  -1
#  define ATG_SOCKET_ERROR    -1
#  define ATG_INADDR_NONE     -1
#  define net_error() errno
#endif



/* DRP2 protocol's atomic types */
typedef char drpByte;
typedef long drpInt;
typedef short drpChar;

/* DRP2 protocol's complex types */
typedef struct _drpTLiteral drpTLiteral;
typedef struct _drpIdentifier drpIdentifier;
typedef struct _growBuffer growBuffer;


/* TypedLiteral object type: represents a single value (e.g. a header name) */
struct _drpTLiteral {
  drpByte type;
  size_t numChars;  /* number of *characters*, not necessarily *bytes*;
		       relevant to kAscii and kUnicode TLiteral types */
  union {
    drpInt  integer;
    drpByte defined;
    drpByte * ascii;
    drpChar * unicode;
  } value;
};


/* Identifier object type: associates a TypedLiteral object with an optional
   (TypedLiteral) index value */
struct _drpIdentifier {
  drpByte hasIndex;
  drpTLiteral name;
  drpTLiteral index;
};


/* GrowBuffer: growable (e.g. via realloc()) buffer object */
struct _growBuffer {
  int allocated;   /* true if growBuffer structure was allocated by
		    * createGrowBuffer(). */
  char * buffer;
  size_t avail;    /* # bytes total allocated for buffer field */
  size_t used;     /* # bytes of actual data in buffer */
};


/* character constants */
#define kChar_Null        '\0'
#define kChar_Ampersand   '&'
#define kChar_Equals      '='
#define kChar_Colon       ':'
#define kChar_Semicolon   ';'
#define kChar_Comma       ','
#define kChar_Slash       '/'
#define kChar_QMark       '?'
#define kChar_Hash        '#'
#define kChar_Backslash   '\\'
#define kChar_Newline     '\n'
#define kChar_Dot         '.'
#define kChar_Space       ' '

/* string constants */
#define kString_HeaderNVDelim     ": "
#define kString_Newline           "\n"
#define kString_QMark             "?"
#define kString_Space             " "
#define kString_Star              "*"
#define kString_Slash             "/"
#define kString_Backslash         "\\"
#define kString_Colon             ":"
#define kString_HTTP_             "HTTP_"
#define kString_http              "http"
#define kString_https             "https"

#define kEmptyString              ""


#ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif
#ifndef MAX
#define MAX(a,b) ((a)>(b)?(a):(b))
#endif


/* Server-API-independent log messages */
enum {
  kLogMessage_DRPServerUnavailable = 1,
  kLogMessage_NoDRPServerAvailable,
  kLogMessage_CannotCreateSocket,
  kLogMessage_CannotConnect,
  kLogMessage_InternalProtoError,
  kLogMessage_BadSIDCharacter,
  kLogMessage_CannotCreateRWLock,
  kLogMessage_CannotStartCMLDRPThread,
  kLogMessage_BadCMLDRPResponseType,
  kLogMessage_NoCMLDRPResponsePeriod,
  kLogMessage_NoCMLDRPResponseFirst,
  kLogMessage_OneString,
  kLogMessage_CantResolveCMLDRPHost,
  kLogMessage_NoGoodCMLDRPServers,

  kLogMessage_Placeholder    /* placeholder used as starting point for
				API-dependent log messages */
};


#ifdef WIN32
#define net_error() WSAGetLastError()
#else
#define net_error() errno
#endif

#ifndef SOCKET_ERROR
#define SOCKET_ERROR     -1
#endif


#endif
