<commit_before>/*
 *
 * (C) 2014-2020 - ntop.org
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lessed General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 *
 */

/* ********************************* */

#include "pfring.h"
#include "pfring_hw_timestamp.h"

#include <linux/ip.h>

static int32_t thiszone = 0;
static int debug_ts = 0;

/* ********************************* */

void pfring_enable_hw_timestamp_debug() {
  debug_ts = 1;
}

/* ********************************* */

int pfring_read_metawatch_hw_timestamp(u_char *buffer, u_int32_t buffer_len, struct timespec *ts) {
  double sub_ns = 0.;
  struct metawatch_trailer *trailer = (struct metawatch_trailer *) &buffer[buffer_len - METAWATCH_TRAILER_LEN];
  u_int32_t tlv;

  //if (unlikely(thiszone == 0))
  //  thiszone = gmt_to_local(0);

  tlv = ntohl(trailer->tlv);

  if ((trailer->flags & METAWATCH_FLAG_TLV_PRESENT) == METAWATCH_FLAG_TLV_PRESENT)
    sub_ns = (tlv >> 8) / METAWATCH_SUB_NS_MULTIPLIER;

  ts->tv_sec = ntohl(trailer->ts_sec);
  //ts->tv_sec -= thiszone;
  ts->tv_nsec = ntohl(trailer->ts_nsec);

  if(unlikely(debug_ts))
    fprintf(stderr, "Flags are %d -> %lu.%lu(%.9f) - DevID:%d ; PortID:%d\tTLV:%d\n",
            trailer->flags, ts->tv_sec, ts->tv_nsec, sub_ns,
            ntohs(trailer->device_id), trailer->port_id, tlv);

  return METAWATCH_TRAILER_LEN;
}

/* ********************************* */

int pfring_read_metawatch_device_info(u_char *buffer, u_int32_t buffer_len, u_int16_t *device_id, u_int8_t *port_id) {
  struct metawatch_trailer *trailer = (struct metawatch_trailer *) &buffer[buffer_len - METAWATCH_TRAILER_LEN];

  *device_id = trailer->device_id;
  *port_id = trailer->port_id;

  return METAWATCH_TRAILER_LEN;
}

/* ********************************* */

int pfring_handle_metawatch_hw_timestamp(u_char* buffer, struct pfring_pkthdr *hdr) {
  struct timespec ts;

  if (unlikely(hdr->caplen != hdr->len))
    return -1; /* full packet only */

  /* Read timestamp */
  pfring_read_metawatch_hw_timestamp(buffer, hdr->len, &ts);
  hdr->ts.tv_sec = ts.tv_sec, hdr->ts.tv_usec = ts.tv_nsec/1000;
  hdr->extended_hdr.timestamp_ns = (((u_int64_t) ts.tv_sec) * 1000000000) + ts.tv_nsec;
<commit_msg>Debug cleanup. Convert dev id to HBO.<commit_after>/*
 *
 * (C) 2014-2020 - ntop.org
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lessed General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 *
 */

/* ********************************* */

#include "pfring.h"
#include "pfring_hw_timestamp.h"

#include <linux/ip.h>

static int32_t thiszone = 0;
static int debug_ts = 0;

/* ********************************* */

void pfring_enable_hw_timestamp_debug() {
  debug_ts = 1;
}

/* ********************************* */

int pfring_read_metawatch_hw_timestamp(u_char *buffer, u_int32_t buffer_len, struct timespec *ts) {
  double sub_ns = 0.;
  struct metawatch_trailer *trailer = (struct metawatch_trailer *) &buffer[buffer_len - METAWATCH_TRAILER_LEN];
  u_int32_t tlv;

  //if (unlikely(thiszone == 0))
  //  thiszone = gmt_to_local(0);

  tlv = ntohl(trailer->tlv);

  if ((trailer->flags & METAWATCH_FLAG_TLV_PRESENT) == METAWATCH_FLAG_TLV_PRESENT)
    sub_ns = (tlv >> 8) / METAWATCH_SUB_NS_MULTIPLIER;

  ts->tv_sec = ntohl(trailer->ts_sec);
  //ts->tv_sec -= thiszone;
  ts->tv_nsec = ntohl(trailer->ts_nsec);

  if (unlikely(debug_ts))
    fprintf(stderr, "[METAWATCH] Flags: %d Timestamp: %lu.%lu(%.9f) DeviceID: %d PortID: %d TLV: %d\n",
            trailer->flags, ts->tv_sec, ts->tv_nsec, sub_ns,
            ntohs(trailer->device_id), trailer->port_id, tlv);

  return METAWATCH_TRAILER_LEN;
}

/* ********************************* */

int pfring_read_metawatch_device_info(u_char *buffer, u_int32_t buffer_len, u_int16_t *device_id, u_int8_t *port_id) {
  struct metawatch_trailer *trailer = (struct metawatch_trailer *) &buffer[buffer_len - METAWATCH_TRAILER_LEN];

  *device_id = ntohs(trailer->device_id);
  *port_id = trailer->port_id;

  return METAWATCH_TRAILER_LEN;
}

/* ********************************* */

int pfring_handle_metawatch_hw_timestamp(u_char* buffer, struct pfring_pkthdr *hdr) {
  struct timespec ts;

  if (unlikely(hdr->caplen != hdr->len))
    return -1; /* full packet only */

  /* Read timestamp */
  pfring_read_metawatch_hw_timestamp(buffer, hdr->len, &ts);
  hdr->ts.tv_sec = ts.tv_sec, hdr->ts.tv_usec = ts.tv_nsec/1000;
  hdr->extended_hdr.timestamp_ns = (((u_int64_t) ts.tv_sec) * 1000000000) + ts.tv_nsec;

{'fraction_non_alphanumeric': 0.19427017984107067, 'fraction_numerical': 0.019238812212463405, 'mean_word_length': 4.45381984036488, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7039843', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1800, 'n_words': 362}