<commit_before>#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/time.h>
#include <sys/types.h>

#include <jemalloc/jemalloc.h>

#include <json-c/json.h>
#include <json-c/json_object_private.h>

#include "metadata.h"
#include "common.h"
#include "ioc.h"
#include "ip_utils.h"
#include "je_utils.h"
#include "json.h"
#include "nn_queue.h"

int ss_metadata_prepare_eth(const char* source, const char* rule, nn_queue_t* nn_queue, json_object* jobject, ss_frame_t* fbuf) {
    char tmp[1024];
    json_object* item = NULL;
    
    item = json_object_new_int(fbuf->data.port_id);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "port_id", item);
    item = json_object_new_string(ss_direction_dump(fbuf->data.direction));
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "direction", item);
    item = json_object_new_int(fbuf->data.self);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "self", item);
    item = json_object_new_int(fbuf->data.length);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "length", item);
    item = json_object_new_int(fbuf->data.eth_type);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "eth_type", item);

    snprintf(tmp, sizeof(tmp), "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
        fbuf->data.smac[0], fbuf->data.smac[1], fbuf->data.smac[2],
        fbuf->data.smac[3], fbuf->data.smac[4], fbuf->data.smac[5]);
    item = json_object_new_string(tmp);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "smac", item);
    
    snprintf(tmp, sizeof(tmp), "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
        fbuf->data.dmac[0], fbuf->data.dmac[1], fbuf->data.dmac[2],
        fbuf->data.dmac[3], fbuf->data.dmac[4], fbuf->data.dmac[5]);
    item = json_object_new_string(tmp);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "dmac", item);
    
    item = NULL;
    
    return 0;
    
    error_out:
    fprintf(stderr, "could not serialize ethernet metadata\n");
    if (item)    { json_object_put(item);    item = NULL;    }
    if (jobject) { json_object_put(jobject); jobject = NULL; }
    
    return -1;
}

int ss_metadata_prepare_ip(const char* source, const char* rule, nn_queue_t* nn_queue, json_object* jobject, ss_frame_t* fbuf) {
    char tmp[1024];
    json_object* sip         = NULL;
    json_object* dip         = NULL;
    json_object* ip_protocol = NULL;
    json_object* ttl         = NULL;
    json_object* l4_length   = NULL;
    json_object* icmp_type   = NULL;
    json_object* icmp_code   = NULL;
    json_object* sport       = NULL;
    json_object* dport       = NULL;
    json_object* dns_name    = NULL;
    
    if      (fbuf->data.eth_type == ETHER_TYPE_IPV4) {
        snprintf(tmp, sizeof(tmp), "%hhu.%hhu.%hhu.%hhu",
            fbuf->data.sip[0], fbuf->data.sip[1], fbuf->data.sip[2], fbuf->data.sip[3]);
        sip = json_object_new_string(tmp);
        snprintf(tmp, sizeof(tmp), "%hhu.%hhu.%hhu.%hhu",
            fbuf->data.dip[0], fbuf->data.dip[1], fbuf->data.dip[2], fbuf->data.dip[3]);
        dip = json_object_new_string(tmp);
    }
    else if (fbuf->data.eth_type == ETHER_TYPE_IPV6) {
        snprintf(tmp, sizeof(tmp), "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
            *(uint16_t*) &fbuf->data.sip[0],  *(uint16_t*) &fbuf->data.sip[2],
            *(uint16_t*) &fbuf->data.sip[4],  *(uint16_t*) &fbuf->data.sip[6],
            *(uint16_t*) &fbuf->data.sip[8],  *(uint16_t*) &fbuf->data.sip[10],
            *(uint16_t*) &fbuf->data.sip[12], *(uint16_t*) &fbuf->data.sip[14]);
        sip = json_object_new_string(tmp);
        snprintf(tmp, sizeof(tmp), "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
            *(uint16_t*) &fbuf->data.sip[0],  *(uint16_t*) &fbuf->data.sip[2],
            *(uint16_t*) &fbuf->data.sip[4],  *(uint16_t*) &fbuf->data.sip[6],
            *(uint16_t*) &fbuf->data.sip[8],  *(uint16_t*) &fbuf->data.sip[10],
            *(uint16_t*) &fbuf->data.sip[12], *(uint16_t*) &fbuf->data.sip[14]);
        dip = json_object_new_string(tmp);
    }
    else {
        fprintf(stderr, "could not extract IP addresses\n");
    }
    
    ip_protocol = json_object_new_int(fbuf->data.ip_protocol);
    if (ip_protocol == NULL) goto error_out;<commit_msg>metadata.c: fix typo in rendering dip field<commit_after>#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/time.h>
#include <sys/types.h>

#include <jemalloc/jemalloc.h>

#include <json-c/json.h>
#include <json-c/json_object_private.h>

#include "metadata.h"
#include "common.h"
#include "ioc.h"
#include "ip_utils.h"
#include "je_utils.h"
#include "json.h"
#include "nn_queue.h"

int ss_metadata_prepare_eth(const char* source, const char* rule, nn_queue_t* nn_queue, json_object* jobject, ss_frame_t* fbuf) {
    char tmp[1024];
    json_object* item = NULL;
    
    item = json_object_new_int(fbuf->data.port_id);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "port_id", item);
    item = json_object_new_string(ss_direction_dump(fbuf->data.direction));
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "direction", item);
    item = json_object_new_int(fbuf->data.self);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "self", item);
    item = json_object_new_int(fbuf->data.length);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "length", item);
    item = json_object_new_int(fbuf->data.eth_type);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "eth_type", item);

    snprintf(tmp, sizeof(tmp), "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
        fbuf->data.smac[0], fbuf->data.smac[1], fbuf->data.smac[2],
        fbuf->data.smac[3], fbuf->data.smac[4], fbuf->data.smac[5]);
    item = json_object_new_string(tmp);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "smac", item);
    
    snprintf(tmp, sizeof(tmp), "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
        fbuf->data.dmac[0], fbuf->data.dmac[1], fbuf->data.dmac[2],
        fbuf->data.dmac[3], fbuf->data.dmac[4], fbuf->data.dmac[5]);
    item = json_object_new_string(tmp);
    if (item == NULL) goto error_out;
    json_object_object_add(jobject, "dmac", item);
    
    item = NULL;
    
    return 0;
    
    error_out:
    fprintf(stderr, "could not serialize ethernet metadata\n");
    if (item)    { json_object_put(item);    item = NULL;    }
    if (jobject) { json_object_put(jobject); jobject = NULL; }
    
    return -1;
}

int ss_metadata_prepare_ip(const char* source, const char* rule, nn_queue_t* nn_queue, json_object* jobject, ss_frame_t* fbuf) {
    char tmp[1024];
    json_object* sip         = NULL;
    json_object* dip         = NULL;
    json_object* ip_protocol = NULL;
    json_object* ttl         = NULL;
    json_object* l4_length   = NULL;
    json_object* icmp_type   = NULL;
    json_object* icmp_code   = NULL;
    json_object* sport       = NULL;
    json_object* dport       = NULL;
    json_object* dns_name    = NULL;
    
    if      (fbuf->data.eth_type == ETHER_TYPE_IPV4) {
        snprintf(tmp, sizeof(tmp), "%hhu.%hhu.%hhu.%hhu",
            fbuf->data.sip[0], fbuf->data.sip[1], fbuf->data.sip[2], fbuf->data.sip[3]);
        sip = json_object_new_string(tmp);
        snprintf(tmp, sizeof(tmp), "%hhu.%hhu.%hhu.%hhu",
            fbuf->data.dip[0], fbuf->data.dip[1], fbuf->data.dip[2], fbuf->data.dip[3]);
        dip = json_object_new_string(tmp);
    }
    else if (fbuf->data.eth_type == ETHER_TYPE_IPV6) {
        snprintf(tmp, sizeof(tmp), "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
            *(uint16_t*) &fbuf->data.sip[0],  *(uint16_t*) &fbuf->data.sip[2],
            *(uint16_t*) &fbuf->data.sip[4],  *(uint16_t*) &fbuf->data.sip[6],
            *(uint16_t*) &fbuf->data.sip[8],  *(uint16_t*) &fbuf->data.sip[10],
            *(uint16_t*) &fbuf->data.sip[12], *(uint16_t*) &fbuf->data.sip[14]);
        sip = json_object_new_string(tmp);
        snprintf(tmp, sizeof(tmp), "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
            *(uint16_t*) &fbuf->data.dip[0],  *(uint16_t*) &fbuf->data.dip[2],
            *(uint16_t*) &fbuf->data.dip[4],  *(uint16_t*) &fbuf->data.dip[6],
            *(uint16_t*) &fbuf->data.dip[8],  *(uint16_t*) &fbuf->data.dip[10],
            *(uint16_t*) &fbuf->data.dip[12], *(uint16_t*) &fbuf->data.dip[14]);
        dip = json_object_new_string(tmp);
    }
    else {
        fprintf(stderr, "could not extract IP addresses\n");
        goto error_out;
    }
    
    ip_protocol = json_object_new_int(fbuf->data.ip_protocol);
    if (ip_protocol == NULL) goto error_out;
{'fraction_non_alphanumeric': 0.1756543321299639, 'fraction_numerical': 0.025045126353790612, 'mean_word_length': 3.3929633300297324, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 113, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '1250062', 'n_tokens_mistral': 3632, 'n_tokens_neox': 3518, 'n_words': 589}