/*
*
* versuch project
* AAA module
* $Id: Aaa.h,v 1.4 2006/08/24 06:44:48 warlock_kg Exp $
* warlock copyright since 2005 Nov
*
*/

#ifndef _AAA_H
#define _AAA_H

#include "Sockets.h"
#include "radlib.h"
#include "global.h"

#include <vector>
#include <string>
#include <map>
#include <algorithm>

#define CISCO_VENDOR_ID		9
#define CISCO_AVPAIR		1

#define H323_REMOTE_ADDRESS     23
#define H323_CONF_ID            24
#define H323_SETUP_TIME         25
#define H323_CALL_ORIGIN        26
#define H323_CALL_TYPE          27
#define H323_CONNECT_TIME       28
#define H323_DISCONNECT_TIME    29
#define H323_DISCONNECT_CAUSE   30
#define H323_GW_ID              33

using namespace std;

//
// Class for Auth and Autorize (Routing)
//

class TCiscoCommandCode
{
public:
 TCiscoCommandCode() {};
 ~TCiscoCommandCode() {};
 bool MediaProxy;
 string CallingNumber, CalledNumber, BCalledNumber, BCallingNumber;
};

class RuntimePeerCmp 
{
public:
 RuntimePeerCmp(const string Called ) { CalledNumber = Called ; };
 bool operator() (const TVoicePeer* pr1, const TVoicePeer* pr2) const 
 {
   return (pr1->Preference < pr2->Preference);
 };
private:
 string CalledNumber;
};

class TAuth
{
public:
 TAuth(TGlobal * menv);
 ~TAuth();
 void InitRadius(const char * Host, int Port, const char * Secret);
 bool FirstAuthorize(WTCPSocket::Address PeerAddri, const string CallingNumber, const string CalledNumber, 
 	const string ConferenceID);
 vector<string> Routes; // routes for call in gateway's names
 vector<string> KillList; // List of calls for shutting down
 map<string, TCiscoCommandCode*> Replies; // replies from Radius
private:
 void FetchPeers(const string & CallingNumber, const string & CalledNumber);
 struct rad_handle * RadHandle;
 vector<TVoicePeer*> PlainPeers;
 TGlobal * env;
};

//
// Accounting
//

struct TThreadCDR
{
 unsigned int CallRef, StartTime, StopTime, IncOctets, OutOctets;
 int DisconnectCause, InternalCause, FastStart, Tunneling, RouteRetries;
 char SetupTime[50], ConnectTime[50], DisconnectTime[50];
 char CallID[50], gwID[50], ConferenceID[50], Codec[50];
 char CalledStationId[30], CallingStationId[30];
 char BCalled[30], BCalling[30];
 char AcctSessionID[40], RemoteAddr[20], LocalAddr[20];
};

class TCDR
{
public:
 TCDR::TCDR() { DisconnectCause = 0; StartTime = 0; StopTime = 0; };
 unsigned int CallRef;
 unsigned int StartTime, StopTime, IncOctets, OutOctets, RouteRetries;
 int FastStart, Tunneling;
 string SetupTime, ConnectTime, DisconnectTime;
 string CallID, gwID, ConferenceID, Codec;
 string CalledStationId, CallingStationId, CiscoConnectTime, CiscoDisconnectTime;
 string AcctSessionID, RemoteAddr, LocalAddr;
 string CalledBill, CallingBill;
 int DisconnectCause, InternalCause;
 void ToThreadCDR(TThreadCDR & ThrCDR);
};

struct acct_thread_data
{
 TThreadCDR CDR;
 bool isStartReq;
 bool CallOrigin;
 struct rad_handle * rh;
 pthread_mutex_t * mtx;
 TGlobal * env;
};

struct log_thread_data
{
 TThreadCDR inCDR;
 TThreadCDR outCDR;
 char log_file[50];
 pthread_mutex_t * mtx;
};

struct rad_log_thread_data
{
 TThreadCDR inCDR;
 TThreadCDR outCDR;
 pthread_mutex_t * mtx;
 struct rad_handle * rh;
 bool isStopLeg;
 TGlobal * env;
};

struct newlog_thread_data
{
 unsigned int time_span; // minutes
 char log_file[50];
 char log_file_umask[6];
 pthread_mutex_t * mtx;
};

class TAcct
{
public:
 enum TCallOrigin 
 {
   Both      = 0,
   Origin    = 1,
   Answer    = 2
 };
 TAcct(TGlobal * menv);
 ~TAcct();
 void StartAcct(TCDR & CDR, bool CallOrigin);
 void StartAcctTransit(TCDR & inCDR, TCDR & outCDR);
 void StopAcct(TCDR & inCDR, TCDR & outCDR, TCallOrigin CallOrigin); // global mutex for logfile
 static string GetLocalTime();
private:
 struct rad_handle * RadHandle;
 pthread_mutex_t mtx;
 vector<pthread_t> threads;
 pthread_attr_t attr;
 TGlobal * env;
};

#endif // _AAA_H


syntax highlighted by Code2HTML, v. 0.9.1