#define	LOGFILE	"/var/log/trusdb.log"

#include        <stdio.h>
#include        <stdlib.h>
#include        <malloc.h>
#include        <string.h>
#include        <errno.h>
#include        <signal.h>
#include        <fcntl.h>
#include	<unistd.h>
#include        <sys/types.h>
#include        <sys/socket.h>
#include        <netinet/in.h>
#include        <netdb.h>
#include        <sys/stat.h>
#include        <sys/param.h>
#include        <sys/time.h>
#include        <sgtty.h>
#include        <arpa/inet.h>
#include	<libpq-fe.h>
#include	<time.h>


/* reqType      */
#define         NoEntry         0x00
#define         ReqConnect      0x01
#define         Ack             0x02
#define         Nak             0x03
#define         NoRoute		0x04
#define		RepActive	0x80
#define         ReqEnd          0xff


struct  sockaddr_in     db_in;
struct	sockaddr_in	db_out;

int	TrustDB_in;
int	TrustDB_out;

in_port_t	Port;
in_port_t	dbInPort;
in_port_t	dbOutPort;
struct	in_addr	dbIP;
int	cmdmode;
int	cmdin;
char	TrustDbDomainName[128];
PGconn	*conDB;
char	repeaterCall[8];
int	AutoEntry;
int	RepAutoEntry;

struct  shm_form {
        unsigned char    type;          /* 0x00 not use         */
                                        /* 0x01 リクエスト　    */
                                        /* 0x02 リプライ        */
                                        /* 0x03 未登録          */
					/* 0x04 転送先無し	*/
                                        /* 0xff 削除            */
	char		protocol;
	union {
		struct {
        		char    dstRep[8];
        		char    Rep2Call[8];
        		char    Rep1Call[8];
        		char    dstCall[8];
        		char    srcCall[8];
		};
		struct {
			char	rep_name[8];
			char	trust_name[16];
			char	reserve[16];
		};
	};
        in_port_t       srcPort;
        struct  in_addr srcIP;
	unsigned char	PackerID[2];
};

struct  shm_form        send_msg;

struct packet{
        unsigned char   ID[4];                          /* DSVT */
        unsigned char   flags[2];
        unsigned char   reserve[2];
        struct
        {
                unsigned char   B_ID;                   /* 0x20 */
                unsigned char   DestID;                 /* 0x00 */
                unsigned char   SrcID;                  /* 0x01 */
                unsigned char   SrcTermID;              /* 0x02 */
                unsigned char   PacketID[2];
                unsigned char   seq;
        } B_header;
        union {
                        struct
                        {
                                unsigned char   flags[3];
                                char            Rpt2Call[8];
                                char            Rpt1Call[8];
                                char            YourCall[8];
                                char            MyCall[8];
                                char            Mycall2[4];
                                unsigned char   crc[2];
                        } header;
                        struct
                        {
                                char    voice[9];
                                char    slowdata[3];
                        } voice;
                        char    lastframe[15];
        };
} ;

struct
{
	union
	{
		struct
		{
			struct	in_addr	ip;
			in_port_t	port;
			struct	packet	pkt;
		} dv;
		struct	shm_form	send_msg;
	};
} RelayPacket;

time_t	now;
FILE	*logfile;

