/*----------------------------------------------------------------------------*/
/*                                                                            */
/*  Copyright (c) 1996 -  2001 by                                             */
/*  blaxxun interactive Inc, San Francisco, California, USA                   */
/*  All rights reserved                                                       */
/*                                                                            */
/*  This software is furnished under a license  and may be  used and  copied  */
/*  only in  accordance of the  terms of such license and with the inclusion  */
/*  of the above copyright notice.  This software or any other copies therof  */
/*  may not be provided or otherwise made available to any other person.  No  */
/*  title to and ownership of the software is hereby transferred.             */
/*                                                                            */
/*  The information in this software is subject to change without notice and  */
/*  should not be construed as a commitment by blaxxun interactive Inc.       */
/*                                                                            */
/*----------------------------------------------------------------------------*/

/*
--------------------------------------------------------------------------------

  MODULE NAME:  $RCSfile: raextern.h,v $
  REVISION:     $Revision: 1.13 $
  DATE:         $Date: 2001/02/15 16:38:43 $
  AUTHOR:       $Author: peter $
  STATE:        $State: Exp $

  CREATION DATE:       Jan 23, 1998

  =DocStart= Module raextern.h
  File raextern.h

  PROJECT:            VWP         - blaxxun virtual worlds platform
  SYSTEM:             server      - server processes
  COMPONENT:          apserver    - Community Server API server

  DESCRIPTION:
      external include file of ra server

  =DocEnd=

--------------------------------------------------------------------------------
*/

#ifndef _RAEXTERN_H_
#define _RAEXTERN_H_

/*
 * make it an extern C declararation for CPLUSPLUS
 */
#ifdef __cplusplus
extern "C" {
#endif

static char * _RAEXTERN_H_rcsid = "$Id: raextern.h,v 1.13 2001/02/15 16:38:43 peter Exp $";

/*
 * the following function is needed to suppress compiler warnings, don't call it
 */
static void _RAEXTERN_H_rcsid_fkt()
{
    if( ! *_RAEXTERN_H_rcsid )
        *_RAEXTERN_H_rcsid = 0;
    if( 0 )
        _RAEXTERN_H_rcsid_fkt();
}

/*----------------------------------------------------------------------------*/
/*             GLOBAL INCLUDE FILES                                           */
/*----------------------------------------------------------------------------*/
#include "apextern.h"

/*----------------------------------------------------------------------------*/
/*             MACROS/CONSTANTS                                               */
/*----------------------------------------------------------------------------*/
/*
 * =DocStart= Defines raextern
 */
/*
 * defaults for timeout and retries
 */
#define RA_DEFAULT_RETRIES          5
#define RA_DEFAULT_TIMEOUT          3

/*
 * maximum number of arguments for call
 */
#define RA_MAX_N_ARGUMENTS       1500
#define RA_MAX_SIZE_ARGUMENTS    32000

/*
 * in the http request case we allow the following request size
 */
#define RA_MAX_HTTP_REQUEST_SIZE ( 256 * 1024 )

/*
 * Error codes for the ra client calls
 */
#define RA_ERR_HOSTNAME	-1	/* error during hostname lookup               */
#define RA_ERR_SOCKET	-2	/* error during socket creation               */
#define RA_ERR_KILLED	-3	/* sigterm received                           */
#define RA_ERR_CONNECT	-4	/* error during connect to the raserver       */
#define RA_ERR_ARGSIZE	-5	/* length of arguments exceeds limit          */
#define RA_ERR_INARGC	-6	/* Parameter inargc exceeds maximum value     */
#define RA_ERR_TIMEOUT	-7	/* The raserver did not answer                */
#define RA_ERR_MEMORY	-8	/* Client is out of memory                    */
#define RA_ERR_CLIENT	-9	/* Invalid client pointer                     */
#define RA_ERR_DENIED	-10	/* Server denied request                      */
#define RA_ERR_DELETED	-11	/* a udp client delete call has been made     */
#define RA_ERR_NORESULT	-12	/* the server didn't give a result            */
#define RA_ERR_WRONGRESULT	-13	/* the server didn't give correct result  */


/* =DocEnd= */

/*----------------------------------------------------------------------------*/
/*             TYPE DECLARATIONS                                              */
/*----------------------------------------------------------------------------*/
/*
 * =DocStart= Types raextern
 */
typedef unsigned long  ra_ip_t;
typedef unsigned short ra_port_t;
typedef unsigned long  ra_4byte_t;

/*
 * A pointer to the Remote Acess UDP Request handler function
 */
typedef void (* raHandlerReplyPtr_t)( char * request, void * userdata, int rc );
typedef void (* raHandlerUdpFuncPtr_t)( char * client, int argc, char *argv[] );
typedef void (* raHandlerHttpFuncPtr_t)( char * client );
typedef void (* raHandlerShutdownPtr_t)( int exitcode );
typedef void (* raHandlerPeriodicPtr_t)( struct timeval * now );

/*
 * The following type is used internally by the RA server
 */
typedef struct raserver_s
{
    time_t            now;              /* the current time set by id_reply   */
    time_t            lastlog;          /* last time log was printed          */
    ra_port_t         port;             /* port number used by RA server      */
    ra_ip_t           ip;               /* ip address used by server          */
    int               socket;           /* udp socket used by RA server       */
    int               standalone;       /* flag: this is real raserver        */
    struct timeval    period;           /* period RA server works with        */
    struct timeval    iperiod;          /* internal period of RA server       */
    struct timeval    nextperiod;       /* next time periodic event is called */
    volatile int      statusprint;      /* flag for status print, set by      */
                                        /* id_sigusr1_handler                 */
                                        /* number of packets received of type */
    ra_4byte_t        n0packets;        /* length 0                           */
    ra_4byte_t        nunknown;         /* unknown packets                    */
    ra_4byte_t        nudprequest;      /* UDP client requests                */
    ra_4byte_t        nreplays;         /* UDP client requests replays        */


    char             *sessions;         /* pointer to sessions hash table     */
    ra_4byte_t        nsessions;        /* number of active udp sessions      */
    ra_4byte_t        tsessions;        /* total number of udp sessions ever  */

    void            * sessionhead;      /* head and tail of list of udp       */
    void            * sessiontail;      /* sessions                           */

    void            * connhead;         /* head and tail of list of tcp       */
    void            * conntail;         /* connections                        */
    ra_4byte_t        nconnections;     /* number of active tcp connections   */
    ra_4byte_t        tconnections;     /* total number of tcp connections    */


    void           * clienthead;        /* head and tail of list of clients   */
    void           * clienttail;

                                 /* the handlers for remote access requests   */
    raHandlerUdpFuncPtr_t  ra_handler;
    raHandlerHttpFuncPtr_t ra_httphandler;
    raHandlerShutdownPtr_t ra_shutdown;
    raHandlerPeriodicPtr_t ra_periodic;

    int               tcpsocket;        /* tcp listen socket used             */
    ra_port_t         tcpport;          /* port number used by RA server      */
    char            * tcpHostname;      /* on multihomed firewall machines    */
    ra_ip_t           tcpip;            /* the tcp and the udp connections    */
                                        /* can happen on different IP addrs   */
                                        /* In this case the tcp hostname has  */
                                        /* to be specified through the        */
                                        /* commandline, the udp address is    */
                                        /* taken from the server configuration*/
} raserver_t;

/* =DocEnd= */

/*----------------------------------------------------------------------------*/
/*             VARIABLE DECLARATIONS                                          */
/*----------------------------------------------------------------------------*/
/*
 * =DocStart= Globals raextern
 */

extern char * raerrstr;
extern int    raerrno;

/* =DocEnd= */

/*----------------------------------------------------------------------------*/
/*             FUNCTION PROTOTYPES                                            */
/*----------------------------------------------------------------------------*/
extern char * raclient_udp_create(
char  *  hostname,              /* IN: name of host RA server is running on  */
int      port,                  /* IN: port of RA server, in host byte order */
time_t   timeout,               /* IN: seconds between retries               */
int      nretries               /* IN: number of retries to attempt          */
);

extern void raclient_udp_delete(
char           * client         /* IN: pointer to client to delete           */
);

extern char * raclient_udp_error(
char           * client         /* IN: pointer to client                     */
);

extern char * raclient_udp_result(
char           * client,        /* IN: pointer to client                     */
int              index          /* IN: index of result to retrieve           */
);

extern int raclient_udp_request(
char           * client,        /* IN: pointer to client to use              */
int              inargc,        /* IN: number of arguments that follow       */
char           * inargv[]       /* IN: pointers array of arguments           */
);

extern int raclient_udp_multiple(
char           * pclient,       /* IN: pointer to client                     */
FILE           * infile,        /* IN: stream to read aruments from          */
FILE           * outfile        /* IN: stream to print results to            */
);

extern int raEventStartup(
int        argc,                        /* number of parameters             */
char      *argv[]                       /* comamnd line parameters          */
);

extern void raEventShutdown(
int exitcode                            /* return code for exit call        */
);

extern void raEventPeriodic(
struct timeval * now                    /* time when function was called    */
);

extern void raEventUdpRequest(
char            *client,                /* client that sent the request     */
int              inargc,                /* number of arguments sent         */
char            *inargv[]               /* pointerarray of arguments sent   */
);

extern ra_ip_t raGetServerIp();
extern ra_port_t raGetServerPort();

extern int raSetPeriod(
struct timeval period           /* r: period to be used for the session     */
);

extern char *  raGetArg(
char           * psession,      /* IN: pointer to client                     */
int              index          /* IN: index of argument to retrieve         */
);

extern ra_ip_t raGetClientIp(
char           * psession       /* IN: pointer to client                     */
);

extern ra_port_t raGetClientPort(
char           * psession       /* IN: pointer to client                     */
);

extern int  raSetResult(
char           * psession,      /* IN: pointer to client                     */
char           * result         /* IN: result to set                         */
);

extern int  raClearResult(
char           * psession       /* IN: pointer to client                     */
);

extern char * raNet2Mem(
char           * from,          /* IN:  pointer to string to convert         */
size_t         * tolen          /* OUT: length of output data                */
);

extern char * raMem2Net(
char           * from,          /* IN: pointer to data to convert            */
size_t           len            /* IN: length of that data                   */
);
extern void raEventHttpRequest(
char  *connection               /* r: connection that sent the request  */
);

extern unsigned long raSetHttpResult(
char           * pconnection,   /* IN: pointer to connection                 */
char           * result         /* IN: result to set                         */
);

extern int  raClearHttpResult(
char           * pconnection    /* IN: pointer to connection                 */
);

extern ra_ip_t raGetHttpClientIp(
char           * pconnection   /* IN: pointer to connection                 */
);

extern ra_port_t raGetHttpClientPort(
char           * pconnection   /* IN: pointer to connection                 */
);

extern char * raGetHttpHeader(
char           * pconnection,  /* IN: pointer to connection                 */
size_t         * len           /* OUT: length of header                     */
);

extern char * raGetHttpContent(
char           * pconnection,  /* IN: pointer to connection                 */
size_t         * len           /* OUT: length of content                    */
);

extern char * raGetHttpUrl(
char           * pconnection,  /* IN: pointer to connection                 */
size_t         * len           /* OUT: length of url                        */
);

char * raIp2Str(
ra_ip_t      ip                    /* r: ip address to convert         */
);

#ifdef __cplusplus
}
#endif

#endif
