CREATE TABLE IF NOT EXISTS connections (
  id SERIAL PRIMARY KEY,
  time_stamp TIMESTAMP DEFAULT NULL,
  ip VARCHAR(15) DEFAULT NULL,
  remote_port INTEGER DEFAULT NULL,
  local_host VARCHAR(15) DEFAULT NULL,
  local_port INTEGER DEFAULT NULL,
  messg INTEGER DEFAULT NULL,
  request INTEGER DEFAULT NULL,
  sensor SMALLINT DEFAULT NULL
);

CREATE INDEX IF NOT EXISTS time_idx ON connections (time_stamp);
CREATE INDEX IF NOT EXISTS ip_idx ON connections (ip);
CREATE INDEX IF NOT EXISTS ip2_idx ON connections (time_stamp, ip);

CREATE TABLE IF NOT EXISTS messages (
  id SERIAL PRIMARY KEY,
  val VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS requests (
  id SERIAL PRIMARY KEY,
  accept INTEGER DEFAULT NULL,
  allow INTEGER DEFAULT NULL,
  callid INTEGER DEFAULT NULL,
  contact INTEGER DEFAULT NULL,
  con_len INTEGER DEFAULT NULL,
  con_type INTEGER DEFAULT NULL,
  cseq INTEGER DEFAULT NULL,
  expires INTEGER DEFAULT NULL,
  frm INTEGER DEFAULT NULL,
  forwards INTEGER DEFAULT NULL,
  supportd INTEGER DEFAULT NULL,
  dest INTEGER DEFAULT NULL,
  agent INTEGER DEFAULT NULL,
  via INTEGER DEFAULT NULL,
  auth INTEGER DEFAULT NULL
);

CREATE TABLE IF NOT EXISTS accepts (
  id SERIAL PRIMARY KEY,
  val VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS allows (
  id SERIAL PRIMARY KEY,
  val VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS callids (
  id SERIAL PRIMARY KEY,
  val VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS contacts (
  id SERIAL PRIMARY KEY,
  val VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS con_lens (
  id SERIAL PRIMARY KEY,
  val VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS con_types (
  id SERIAL PRIMARY KEY,
  val VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS cseqs (
  id SERIAL PRIMARY KEY,
  val VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS expires (
  id SERIAL PRIMARY KEY,
  val VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS froms (
  id SERIAL PRIMARY KEY,
  val VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS forwards (
  id SERIAL PRIMARY KEY,
  val VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS supporteds (
  id SERIAL PRIMARY KEY,
  val VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS tos (
  id SERIAL PRIMARY KEY,
  val VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS agents (
  id SERIAL PRIMARY KEY,
  val VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS vias (
  id SERIAL PRIMARY KEY,
  val VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS authorizations (
  id SERIAL PRIMARY KEY,
  val VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS unknown (
  id SERIAL PRIMARY KEY,
  header VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS sensors (
  id SERIAL PRIMARY KEY,
  sname VARCHAR(255) DEFAULT NULL
);

CREATE UNIQUE INDEX name_idx ON sensors (sname);

CREATE TABLE IF NOT EXISTS geolocation (
  id SERIAL PRIMARY KEY,
  ip VARCHAR(15) DEFAULT NULL,
  country_name VARCHAR(45) DEFAULT '',
  country_iso_code VARCHAR(2) DEFAULT '',
  city_name VARCHAR(128) DEFAULT '',
  org VARCHAR(128) DEFAULT '',
  org_asn INTEGER DEFAULT NULL
);

CREATE UNIQUE INDEX ip3_idx ON geolocation (ip);
