/*
 *  author:yangwei
 *  create time:2021-8-21
 *
 */



#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <assert.h>

#include "cJSON.h"
#include "gquic.h"
#include "MESA_prof_load.h"
#include <MESA/stream.h>

extern "C" int commit_test_result_json(cJSON *node, const char *name);

static int g_result_count = 1;

extern "C" unsigned char QUIC_TEST_PLUG_ENTRY(stSessionInfo *session_info, void **pme,
                             int thread_seq, struct streaminfo *a_tcp, void *a_packet)
{
    assert(NULL != session_info || pme != NULL);

    cJSON *ctx = (cJSON *)*pme;
    struct _quic_info *quic_info=NULL;
    char version_str[128]={0};
    unsigned int version = 0;

    if (session_info->session_state & SESSION_STATE_PENDING)
    {
        if (*pme == NULL)
        {
            ctx = cJSON_CreateObject();
            *pme = (void *)ctx;
            cJSON_AddStringToObject(ctx, "Tuple4", printaddr(&a_tcp->addr, a_tcp->threadnum));
        }
    }

    switch (session_info->prot_flag)
    {
    case QUIC_CLIENT_HELLO:
        if (session_info == NULL || session_info->app_info == NULL)
        {
            break;
        }
        quic_info = (struct _quic_info *)session_info->app_info;
        cJSON_AddStringToObject(ctx, "SNI", (char *)(quic_info->client_hello->ext_tags[quic_info->client_hello->sni_idx].value));
		cJSON_AddStringToObject(ctx, "UA", (char *)(quic_info->client_hello->ext_tags[quic_info->client_hello->ua_idx].value));
        break;
    case QUIC_USEING_VERSION:
        version = *(unsigned int *)(session_info->buf);
        quic_version_int2string(version, version_str, sizeof(version_str));
        cJSON_AddStringToObject(ctx, "VERSION", version_str);
        break;
    default:
        break;
    }

    if(session_info->session_state&SESSION_STATE_CLOSE)
    {
            if(ctx)
            {
                char result_name[16]="";
                sprintf(result_name,"QUIC_RESULT_%d", g_result_count);
                commit_test_result_json(ctx, result_name);
                g_result_count+=1;
            }
            *pme = NULL;
            return PROT_STATE_DROPME;

    }

    return PROT_STATE_GIVEME;

}

extern "C" int QUIC_TEST_PLUG_INIT()
{
    return 0;
}

extern "C" void QUIC_TEST_PLUG_DESTROY(void)
{
    return ;
}/*CHAR_DESTRORY*/



