
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "plugins/plugin_api.h"

#include "pdu.h"

#define P_MAX_LEN  20
#define I_MAX_LEN 160

void log(const char *fmt, ...) {
  va_list argptr;
  FILE *pf;

  pf = fopen("D:\\log.txt", "at");
  if (!pf) return;

  va_start(argptr, fmt);
  vfprintf(pf, fmt, argptr);
  va_end(argptr);
  fprintf(pf, "\n");

  fclose(pf);
}

/*--- pdu_init_internal -----------------------------------------------------*/
static void pdu_init_internal(pdu_data *pd) {
  pd->signature = PDU_SIG;
  pd->pi = NULL;
  pd->fn = 0;
  pd->pbeg = pd->pbuf;
  pd->ibeg = pd->iobeg = pd->ibuf;
  pd->ptoolong = FALSE;
  pd->itoolong = FALSE;
  pd->pbuf[0] = '\0';
  pd->ibuf[0] = '\0';
}

/*--- pdu_init --------------------------------------------------------------*/
void pdu_init(pdu_data *pd) {
  /*log("pdu_init(pd = %p)",pd);*/
  pdu_init_internal(pd);
}

/*--- pdu_next --------------------------------------------------------------*/
void pdu_next(packet_info *pi, pdu_data *pd) {
  /*log("pdu_next(pi = %p, pd = %p) pd->pi = %p, pi->private_data = %p, pi->fd->num = %d, ",pi,pd,pd->pi,pi->private_data,pi->fd->num);*/
  if (!pd->pi || (pd->pi != pi) || (pd->fn != pi->fd->num)) {  /* 1st PDU in packet */
    /*log("1st PDU");*/
    /*if (pd->pi && (pd->pi->private_data == pd))
      pd->pi->private_data = NULL;*/
    pdu_init_internal(pd);
    pi->private_data = pd;
    pd->pi = pi;
    pd->fn = pi->fd->num;
    if (check_col(pi->cinfo, COL_INFO))
      col_clear(pi->cinfo, COL_INFO);
  } else {  /* next PDU in packet */
    /*log("next PDU");*/
    strcat(pd->pbuf, "|");
    pd->pbeg = STREND(pd->pbuf);
    strcpy(pd->iobeg, " | ");
    pd->ibeg = pd->iobeg = STREND(pd->ibuf);
  }
}

/*--- pdu_set_proto ---------------------------------------------------------*/
void pdu_set_proto(packet_info *pi, gchar *str) {
  pdu_data *pd;

  pd = (pdu_data*)pi->private_data;
  if (((int)pd > 1) && (pd->signature == PDU_SIG)) {
    if (pd->ptoolong) return;
    if ((strlen(pd->pbuf) + strlen(str)) > P_MAX_LEN) {
      strcpy(pd->pbeg, "...");
      pd->ptoolong = TRUE;
    } else {
      strcpy(pd->pbeg, str);
    }
    if (check_col(pi->cinfo, COL_PROTOCOL))
      col_add_str(pi->cinfo, COL_PROTOCOL, pd->pbuf);
  } else {
    if (check_col(pi->cinfo, COL_PROTOCOL))
      col_set_str(pi->cinfo, COL_PROTOCOL, str);
  }
}

/*--- pdu_set_info ----------------------------------------------------------*/
void pdu_set_info(packet_info *pi, gchar *str, gchar *ostr) {
  pdu_data *pd;

  pd = (pdu_data*)pi->private_data;
  if (((int)pd > 1) && (pd->signature == PDU_SIG)) {
    if (pd->itoolong) return;
    if ((strlen(pd->ibuf) + strlen(str)) > I_MAX_LEN) {
      strcpy(pd->ibeg, "...");
      pd->itoolong = TRUE;
    } else {
      strcpy(pd->ibeg, str);
      pd->iobeg = STREND(pd->ibeg);
      if (ostr && (pd->ibuf == pd->ibeg))
        strcpy(pd->iobeg, ostr);
    }
    if (check_col(pi->cinfo, COL_INFO))
      col_add_str(pi->cinfo, COL_INFO, pd->ibuf);
  } else {
    if (check_col(pi->cinfo, COL_INFO)) {
      col_add_str(pi->cinfo, COL_INFO, str);
      if (ostr) col_append_str(pi->cinfo, COL_INFO, ostr);
    }
  }
}

