<commit_before>/*
Copyright (c) 2014, <NAME>, Ltd.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this
  list of conditions and the following disclaimer in the documentation and/or
  other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "jdksavdecc-logger.h"
#include <stdint.h>
#include <time.h>
#include "us_allocator.h"
#include "us_rawnet_multi.h"
#include "us_logger_stdio.h"
#include "us_getopt.h"

bool option_help;
bool option_help_default=false;
bool option_dump;
bool option_dump_default=false;
int16_t option_adp;
int16_t option_adp_default=true;
int16_t option_acmp;
int16_t option_acmp_default=true;
int16_t option_aecp;
int16_t option_aecp_default=true;
int16_t option_jdkslog;
int16_t option_jdkslog_default=true;
uint64_t option_entity;
struct jdksavdecc_eui64 option_entity_eui64;
uint64_t option_entity_default=0xffffffffffffffffULL;

us_malloc_allocator_t allocator;
us_getopt_t options;
us_getopt_option_t option[] = {
    {"dump","Dump settings to stdout", US_GETOPT_FLAG, &option_dump_default, &option_dump },
    {"help","Show help", US_GETOPT_FLAG, &option_help_default, &option_help },
    {"adp","Enable logging ADP", US_GETOPT_INT16, &option_adp_default, &option_adp },
    {"acmp","Enable logging ACMP", US_GETOPT_INT16, &option_acmp_default, &option_acmp },
    {"aecp","Enable logging AECP", US_GETOPT_INT16, &option_aecp_default, &option_aecp },
    {"jdkslog","Enable logging of JDKS Logging messages", US_GETOPT_INT16, &option_jdkslog_default, &option_jdkslog },
    {"entity","Limit to only receive messages involving this entity", US_GETOPT_HEX64, &option_entity_default, &option_entity },
    {0,0,0,0}
};

us_rawnet_multi_t multi_rawnet;





void incoming_packet_handler( us_rawnet_multi_t *self, int ethernet_port, void *context, uint8_t *buf, uint16_t len ) {
    char text[4096]="";
    struct jdksavdecc_printer print;
    jdksavdecc_printer_init(&print, text, sizeof(text));

    if( option_acmp==1 && buf[JDKSAVDECC_FRAME_HEADER_LEN+0]==0x80+JDKSAVDECC_SUBTYPE_ACMP ) {
        struct jdksavdecc_acmpdu acmp;
        if( jdksavdecc_acmpdu_read(&acmp, buf, JDKSAVDECC_FRAME_HEADER_LEN, len )>0 ) {
            bool allow=true;
            // filter out non-interesting entity id's if we are asked to
            if( jdksavdecc_eui64_is_set(option_entity_eui64) ) {
                allow=false;
                if( jdksavdecc_eui64_compare(&acmp.controller_entity_id, &option_entity_eui64 )==0) {
                    allow=true;
                } else if( jdksavdecc_eui64_compare(&acmp.talker_entity_id, &option_entity_eui64 )==0) {
                    allow=true;
                } else if( jdksavdecc_eui64_compare(&acmp.listener_entity_id, &option_entity_eui64 )==0) {
                    allow=true;
                }
            }

            if( allow ) {
                jdksavdecc_printer_print(&print,"ACMP:");
                jdksavdecc_printer_print_eol(&print);
                jdksavdecc_acmpdu_print(&print, &acmp);
                jdksavdecc_printer_print_eol(&print);
            }
        }
    }
    else if( option_adp==1 && buf[JDKSAVDECC_FRAME_HEADER_LEN+0]==0x80+JDKSAVDECC_SUBTYPE_ADP ) {
        struct jdksavdecc_adpdu adp;
        if( jdksavdecc_adpdu_read(&adp, buf, JDKSAVDECC_FRAME_HEADER_LEN, len )>0 ) {
            bool allow=true;
            // filter out non-interesting entity id's if we are asked to
            if( jdksavdecc_eui64_is_set(option_entity_eui64) ) {
                allow=false;
                if( jdksavdecc_eui64_compare(&adp.header.entity_id, &option_entity_eui64 )==0) {
                    allow=true;
                }
            }
            if( allow ) {
                jdksavdecc_printer_print(&print,"ADP:");
                jdksavdecc_printer_print_eol(&print);
                jdksavdecc_adpdu_print(&print, &adp);
                jdksavdecc_printer_print_eol(&print);
            }
        }
    } else if( option_aecp==1 && buf[JDKSAVDECC_FRAME_HEADER_LEN+0]==0x80+JDKSAVDECC_SUBTYPE_AECP ) {
        struct jdksavdecc_aecpdu_common aecpdu;
        if( jdksavdecc_aecpdu_common_read(&aecpdu, buf, JDKSAVDECC_FRAME_HEADER_LEN, len )>0 ) {
            bool allow=true;

            // filter out non-interesting entity id's if we are asked to
            if( jdksavdecc_eui64_is_set(option_entity_eui64) ) {
                allow=false;
                if( jdksavdecc_eui64_compare(&aecpdu.controller_entity_id, &option_entity_eui64 )==0) {
                    allow=true;
                } else if( jdksavdecc_eui64_compare(&aecpdu.header.target_entity_id, &option_entity_eui64 )==0) {
                    allow=true;
                }
            }
            if( allow ) {
                jdksavdecc_printer_print(&print,"AECP:");
                jdksavdecc_printer_print_eol(&print);
                jdksavdecc_aecp_print(&print, &aecpdu, buf, JDKSAVDECC_FRAME_HEADER_LEN, len );
            }
        }
    } else if( option_jdkslog==1 && buf[JDKSAVDECC_FRAME_HEADER_LEN+0]==0x80+JDKSAVDECC_SUBTYPE_AECP
        && memcmp( &buf[JDKSAVDECC_FRAME_HEADER_DA_OFFSET], jdksavdecc_jdks_multicast_log.value, 6 )==0 ) {
        struct jdksavdecc_aecpdu_aem aem;
        if( jdksavdecc_aecpdu_aem_read(&aem, buf, JDKSAVDECC_FRAME_HEADER_LEN, len )>0 ) {
            if( aem.aecpdu_header.header.message_type == JDKSAVDECC_AECP_MESSAGE_TYPE_ADDRESS_ACCESS_RESPONSE
                && aem.command_type == 0x8000 + JDKSAVDECC_AEM_COMMAND_SET_CONTROL ) {
                bool allow=true;

                // filter out non-interesting entity id's if we are asked to
                if( jdksavdecc_eui64_is_set(option_entity_eui64) ) {
                    allow=false;
                    if( jdksavdecc_eui64_compare(&aem.aecpdu_header.controller_entity_id, &option_entity_eui64 )==0) {
                        allow=true;
                    } else if( jdksavdecc_eui64_compare(&aem.aecpdu_header.header.target_entity_id, &option_entity_eui64 )==0) {
                        allow=true;
                    }
                }
                if( allow ) {
                    struct jdksavdecc_jdks_log_control log_msg;
                    if( jdksavdecc_jdks_log_control_read(&log_msg,buf,JDKSAVDECC_FRAME_HEADER_LEN,len)>0 ) {
                        fprintf(stdout,"%" PRIx64 ":%" PRIx16 ":%" PRIx16 ":%" PRIx16 ":%" PRIx8 ":%s\n",
                            jdksavdecc_eui64_convert_to_uint64( &log_msg.cmd.aem_header.aecpdu_header.header.target_entity_id ),
                            log_msg.source_descriptor_type,
                            log_msg.source_descriptor_index,
                            log_msg.log_sequence_id,
                            log_msg.log_detail,
                            log_msg.text );
                    }
                }
            }
        }
    }
    if( *text!=0 ) {
        fprintf( stdout, "%s", text );
    }
}

int main(int argc, const char **argv ) {
    us_platform_init_sockets();
    us_malloc_allocator_init(&allocator);<commit_msg>logger receiver checks controller id must be jdksavdecc_jdks_notifications_controller_entity_id<commit_after>/*
Copyright (c) 2014, <NAME>, Ltd.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this
  list of conditions and the following disclaimer in the documentation and/or
  other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "jdksavdecc-logger.h"
#include <stdint.h>
#include <time.h>
#include "us_allocator.h"
#include "us_rawnet_multi.h"
#include "us_logger_stdio.h"
#include "us_getopt.h"

bool option_help;
bool option_help_default=false;
bool option_dump;
bool option_dump_default=false;
int16_t option_adp;
int16_t option_adp_default=true;
int16_t option_acmp;
int16_t option_acmp_default=true;
int16_t option_aecp;
int16_t option_aecp_default=true;
int16_t option_jdkslog;
int16_t option_jdkslog_default=true;
uint64_t option_entity;
struct jdksavdecc_eui64 option_entity_eui64;
uint64_t option_entity_default=0xffffffffffffffffULL;

us_malloc_allocator_t allocator;
us_getopt_t options;
us_getopt_option_t option[] = {
    {"dump","Dump settings to stdout", US_GETOPT_FLAG, &option_dump_default, &option_dump },
    {"help","Show help", US_GETOPT_FLAG, &option_help_default, &option_help },
    {"adp","Enable logging ADP", US_GETOPT_INT16, &option_adp_default, &option_adp },
    {"acmp","Enable logging ACMP", US_GETOPT_INT16, &option_acmp_default, &option_acmp },
    {"aecp","Enable logging AECP", US_GETOPT_INT16, &option_aecp_default, &option_aecp },
    {"jdkslog","Enable logging of JDKS Logging messages", US_GETOPT_INT16, &option_jdkslog_default, &option_jdkslog },
    {"entity","Limit to only receive messages involving this entity", US_GETOPT_HEX64, &option_entity_default, &option_entity },
    {0,0,0,0}
};

us_rawnet_multi_t multi_rawnet;





void incoming_packet_handler( us_rawnet_multi_t *self, int ethernet_port, void *context, uint8_t *buf, uint16_t len ) {
    char text[4096]="";
    struct jdksavdecc_printer print;
    jdksavdecc_printer_init(&print, text, sizeof(text));

    if( option_acmp==1 && buf[JDKSAVDECC_FRAME_HEADER_LEN+0]==0x80+JDKSAVDECC_SUBTYPE_ACMP ) {
        struct jdksavdecc_acmpdu acmp;
        if( jdksavdecc_acmpdu_read(&acmp, buf, JDKSAVDECC_FRAME_HEADER_LEN, len )>0 ) {
            bool allow=true;
            // filter out non-interesting entity id's if we are asked to
            if( jdksavdecc_eui64_is_set(option_entity_eui64) ) {
                allow=false;
                if( jdksavdecc_eui64_compare(&acmp.controller_entity_id, &option_entity_eui64 )==0) {
                    allow=true;
                } else if( jdksavdecc_eui64_compare(&acmp.talker_entity_id, &option_entity_eui64 )==0) {
                    allow=true;
                } else if( jdksavdecc_eui64_compare(&acmp.listener_entity_id, &option_entity_eui64 )==0) {
                    allow=true;
                }
            }

            if( allow ) {
                jdksavdecc_printer_print(&print,"ACMP:");
                jdksavdecc_printer_print_eol(&print);
                jdksavdecc_acmpdu_print(&print, &acmp);
                jdksavdecc_printer_print_eol(&print);
            }
        }
    }
    else if( option_adp==1 && buf[JDKSAVDECC_FRAME_HEADER_LEN+0]==0x80+JDKSAVDECC_SUBTYPE_ADP ) {
        struct jdksavdecc_adpdu adp;
        if( jdksavdecc_adpdu_read(&adp, buf, JDKSAVDECC_FRAME_HEADER_LEN, len )>0 ) {
            bool allow=true;
            // filter out non-interesting entity id's if we are asked to
            if( jdksavdecc_eui64_is_set(option_entity_eui64) ) {
                allow=false;
                if( jdksavdecc_eui64_compare(&adp.header.entity_id, &option_entity_eui64 )==0) {
                    allow=true;
                }
            }
            if( allow ) {
                jdksavdecc_printer_print(&print,"ADP:");
                jdksavdecc_printer_print_eol(&print);
                jdksavdecc_adpdu_print(&print, &adp);
                jdksavdecc_printer_print_eol(&print);
            }
        }
    } else if( option_aecp==1 && buf[JDKSAVDECC_FRAME_HEADER_LEN+0]==0x80+JDKSAVDECC_SUBTYPE_AECP ) {
        struct jdksavdecc_aecpdu_common aecpdu;
        if( jdksavdecc_aecpdu_common_read(&aecpdu, buf, JDKSAVDECC_FRAME_HEADER_LEN, len )>0 ) {
            bool allow=true;

            // filter out non-interesting entity id's if we are asked to
            if( jdksavdecc_eui64_is_set(option_entity_eui64) ) {
                allow=false;
                if( jdksavdecc_eui64_compare(&aecpdu.controller_entity_id, &option_entity_eui64 )==0) {
                    allow=true;
                } else if( jdksavdecc_eui64_compare(&aecpdu.header.target_entity_id, &option_entity_eui64 )==0) {
                    allow=true;
                }
            }
            if( allow ) {
                jdksavdecc_printer_print(&print,"AECP:");
                jdksavdecc_printer_print_eol(&print);
                jdksavdecc_aecp_print(&print, &aecpdu, buf, JDKSAVDECC_FRAME_HEADER_LEN, len );
            }
        }
    } else if( option_jdkslog==1 && buf[JDKSAVDECC_FRAME_HEADER_LEN+0]==0x80+JDKSAVDECC_SUBTYPE_AECP
        && memcmp( &buf[JDKSAVDECC_FRAME_HEADER_DA_OFFSET], jdksavdecc_jdks_multicast_log.value, 6 )==0 ) {
        struct jdksavdecc_aecpdu_aem aem;
        if( jdksavdecc_aecpdu_aem_read(&aem, buf, JDKSAVDECC_FRAME_HEADER_LEN, len )>0 ) {
            if( aem.aecpdu_header.header.message_type == JDKSAVDECC_AECP_MESSAGE_TYPE_ADDRESS_ACCESS_RESPONSE
                && aem.command_type == 0x8000 + JDKSAVDECC_AEM_COMMAND_SET_CONTROL ) {
                bool allow=true;

                // filter out non-interesting entity id's if we are asked to
                if( jdksavdecc_eui64_is_set(option_entity_eui64) ) {
                    allow=false;
                    if( jdksavdecc_eui64_compare(&aem.aecpdu_header.header.target_entity_id, &option_entity_eui64 )==0) {
                        allow=true;
                    }
                }
                if( jdksavdecc_eui64_compare(&aem.aecpdu_header.controller_entity_id, &jdksavdecc_jdks_notifications_controller_entity_id )==0) {
                    allow=true;
                }
                if( allow ) {
                    struct jdksavdecc_jdks_log_control log_msg;
                    if( jdksavdecc_jdks_log_control_read(&log_msg,buf,JDKSAVDECC_FRAME_HEADER_LEN,len)>0 ) {
                        fprintf(stdout,"%" PRIx64 ":%" PRIx16 ":%" PRIx16 ":%" PRIx16 ":%" PRIx8 ":%s\n",
                            jdksavdecc_eui64_convert_to_uint64( &log_msg.cmd.aem_header.aecpdu_header.header.target_entity_id ),
                            log_msg.source_descriptor_type,
                            log_msg.source_descriptor_index,
                            log_msg.log_sequence_id,
                            log_msg.log_detail,
                            log_msg.text );
                    }
                }
            }
        }
    }
    if( *text!=0 ) {
        fprintf( stdout, "%s", text );
    }
}

int main(int argc, const char **argv ) {
    us_platform_init_sockets();
    us_malloc_allocator_init(&allocator);
{'fraction_non_alphanumeric': 0.08651368266044913, 'fraction_numerical': 0.018529880966989813, 'mean_word_length': 2.7790401112914447, 'pattern_counts': {'":': 10, '<': 9, '<?xml version=': 0, '>': 19, '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': '467458', 'n_tokens_mistral': 5884, 'n_tokens_neox': 5487, 'n_words': 1085}