/*
 * simco_b_ext.c
 *
 * Copyright (C) 2005, Ranch Networks, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 *
 * Author : Srivatsa Chivukula <srivatsa@ranchnetworks.com>
 *
 */

#include "simco_core_ext.h"
#include "simco_b_ext.h"

/**
 * Builds a packet for extended simco firewall rule create request, sends the
 * packet over TCP/IP and parses response to fill corresponding fields in \a sep.
 * @param con a pointer to MCAgenCon structure.
 * @param sep a pointer to simcoExtPacket_t structure.
 * @return -1 if communication failed, 0 on success, or error code set by server.
 */
int b_mcAgentTCPFirewallRuleCreate(MCAgentCon *con, simcoExtPacket_t *sep)
{
  char request[SET_REQUEST_SIZE];
  char *response;
  int req_len = 0;
  int res_len = 0;

  req_len = makeFirewallRuleCreateRequest(request, sep);
  res_len = MAX_READ_SIZE;
  response = (char*)malloc(MAX_READ_SIZE);
  if (rnMCAgentComm(con, request, req_len, &response, &res_len) <= 0)
    {
      return -1;
    }
  parseSIMCOExtPdu(sep, response, res_len);
  free(response);
  return sep->status;
}

/**
 * Builds a packet for extended simco firewall rule delete request, sends the
 * packet over TCP/IP and parses response to fill corresponding fields in \a sep.
 * @param con a pointer to MCAgenCon structure.
 * @param sep a pointer to simcoExtPacket_t structure.
 * @return -1 if communication failed, 0 on success, or error code set by server.
 */
int b_mcAgentTCPFirewallRuleDelete(MCAgentCon *con, simcoExtPacket_t *sep)
{
  char request[SET_REQUEST_SIZE];
  char *response;
  int req_len = 0;
  int res_len = 0;

  req_len = makeFirewallRuleDeleteRequest(request, sep);
  res_len = MAX_READ_SIZE;
  response = (char*)malloc(MAX_READ_SIZE);
  if (rnMCAgentComm(con, request, req_len, &response, &res_len) <= 0)
    {
      return -1;
    }
  parseSIMCOExtPdu(sep, response, res_len);
  free(response);
  return sep->status;
}

/**
 * Builds a packet for extended simco firewall rule status request, sends the
 * packet over TCP/IP and parses response to fill corresponding fields in \a sep.
 * @param con a pointer to MCAgenCon structure.
 * @param sep a pointer to simcoPacket_t structure.
 * @return -1 if communication failed, 0 on success, or error code set by server.
 */
int b_mcAgentTCPFirewallRuleStatus(MCAgentCon *con, simcoExtPacket_t *sep)
{
  char request[SET_REQUEST_SIZE];
  char *response;
  int req_len = 0;
  int res_len = 0;

  req_len = makeFirewallRuleStatusRequest(request, sep);
  res_len = MAX_READ_SIZE;
  response = (char*)malloc(MAX_READ_SIZE);
  if (rnMCAgentComm(con, request, req_len, &response, &res_len) <= 0)
    {
      return -1;
    }
  parseSIMCOExtPdu(sep, response, res_len);
  free(response);
  return sep->status;
}

/**
 * Builds a packet for extended simco firewall rule list request, sends the
 * packet over TCP/IP and parses response to get an array of rules.
 * @param con a pointer to MCAgenCon structure.
 * @param rules a pointer to an integer array.
 * @param numrules contains the number of elements in the integer array \a rules.
 * @return -1 if communication failed, 0 on success, or error code set by server.
 */
int b_mcAgentTCPFirewallRuleList(MCAgentCon *con, int **rules, int *numrules)
{
  char request[SET_REQUEST_SIZE+2];
  char *response;
  int req_len = 0;
  int res_len = 0;
  int ret;

  req_len = makeFirewallRuleListRequest(request);
  res_len = MAX_READ_SIZE;
  response = (char*)malloc(MAX_READ_SIZE);
  if (rnMCAgentComm(con, request, req_len, &response, &res_len) <= 0)
    {
      return -1;
    }
  ret = parseSIMCORuleList(response, res_len, rules, numrules);
  free(response);
  return ret;
}

