feat: adapt to stellar-2.0
This commit is contained in:
@@ -3,101 +3,135 @@
|
||||
* create time:2021-8-21
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "cJSON.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
#include "quic.h"
|
||||
#include "MESA_prof_load.h"
|
||||
#include <MESA/stream.h>
|
||||
#include <stellar/stellar.h>
|
||||
#include <stellar/session.h>
|
||||
#include <stellar/session_mq.h>
|
||||
#include <stellar/session_exdata.h>
|
||||
|
||||
extern "C" int commit_test_result_json(cJSON *node, const char *name);
|
||||
extern "C" int quic_version_int2string(unsigned int version, char *buff, int buff_len);
|
||||
|
||||
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)
|
||||
#if 0
|
||||
#define DEBUG_PRINT(fmt, args...) fprintf(stderr, fmt, ##args)
|
||||
#else
|
||||
#define DEBUG_PRINT(fmt, args...)
|
||||
#endif
|
||||
|
||||
struct quic_gtest_context
|
||||
{
|
||||
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;
|
||||
if(quic_info->client_hello==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if(quic_info->client_hello->sni!=NULL)
|
||||
{
|
||||
cJSON_AddStringToObject(ctx, "SNI", (char *)(quic_info->client_hello->sni));
|
||||
}
|
||||
if(quic_info->client_hello->user_agent!=NULL)
|
||||
{
|
||||
cJSON_AddStringToObject(ctx, "UA", (char *)(quic_info->client_hello->user_agent));
|
||||
}
|
||||
|
||||
quic_version_int2string((unsigned int)(quic_info->quic_version), version_str, sizeof(version_str));
|
||||
cJSON_AddStringToObject(ctx, "VERSION", version_str);
|
||||
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;
|
||||
cJSON *json_root;
|
||||
};
|
||||
|
||||
static void cJSON_Add_QStringToObject(cJSON * object, const char *name, const struct qstring * qstring)
|
||||
{
|
||||
char *tmp = (char *)calloc(1, qstring->str_len + 1);
|
||||
memcpy(tmp, qstring->str, qstring->str_len);
|
||||
cJSON_AddStringToObject(object, name, tmp);
|
||||
}
|
||||
|
||||
extern "C" int QUIC_TEST_PLUG_INIT()
|
||||
extern "C" void QUIC_TEST_PLUG_ENTRY(struct session *sess, int topic_id, const void *msg, void *per_session_ctx, void *plugin_env)
|
||||
{
|
||||
return 0;
|
||||
struct quic_gtest_context *qctx = (struct quic_gtest_context *)per_session_ctx;
|
||||
struct quic_message *qmsg = (struct quic_message *)msg;
|
||||
enum quic_message_type mtype = quic_message_type_get(qmsg);
|
||||
|
||||
DEBUG_PRINT("### QUIC_TEST_PLUG_ENTRY: mtype=%d\n", (int)mtype);
|
||||
|
||||
switch(mtype){
|
||||
case QUIC_NEW:
|
||||
qctx->json_root = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(qctx->json_root, "Tuple4", session_get0_readable_addr(sess));
|
||||
break;
|
||||
|
||||
case QUIC_VERSION:
|
||||
{
|
||||
unsigned int version_int = 0;
|
||||
char version_str[128]={0};
|
||||
quic_message_get_version(qmsg, &version_int);
|
||||
assert(version_int != 0);
|
||||
quic_version_int2string(version_int, version_str, sizeof(version_str));
|
||||
cJSON_AddStringToObject(qctx->json_root, "VERSION", version_str);
|
||||
DEBUG_PRINT("### QUIC_TEST_PLUG_ENTRY: version=%x, str_version=%s\n", version_int, version_str);
|
||||
}
|
||||
break;
|
||||
|
||||
case QUIC_SNI:
|
||||
{
|
||||
struct qstring result = {};
|
||||
quic_message_get_sni(qmsg, &result);
|
||||
cJSON_Add_QStringToObject(qctx->json_root, "SNI", &result);
|
||||
DEBUG_PRINT("### QUIC_TEST_PLUG_ENTRY: len=%d, SNI=%p, %.*s\n", (int)result.str_len, result.str, (int)result.str_len, result.str);
|
||||
}
|
||||
break;
|
||||
|
||||
case QUIC_USER_AGENT:
|
||||
{
|
||||
struct qstring result = {};
|
||||
quic_message_get_user_agent(qmsg, &result);
|
||||
cJSON_Add_QStringToObject(qctx->json_root, "UA", &result);
|
||||
DEBUG_PRINT("### QUIC_TEST_PLUG_ENTRY: len=%d, UA=%p, %.*s\n", (int)result.str_len, result.str, (int)result.str_len, result.str);
|
||||
}
|
||||
break;
|
||||
|
||||
case QUIC_FREE:
|
||||
{
|
||||
char result_name[16]="";
|
||||
sprintf(result_name,"QUIC_RESULT_%d", g_result_count);
|
||||
commit_test_result_json(qctx->json_root, result_name);
|
||||
g_result_count+=1;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
extern "C" void QUIC_TEST_PLUG_DESTROY(void)
|
||||
extern "C" void *quic_gtest_plug_session_ctx_new_cb(struct session *sess, void *plugin_env)
|
||||
{
|
||||
struct quic_gtest_context *ctx = (struct quic_gtest_context *)calloc(1, sizeof(struct quic_gtest_context));
|
||||
return ctx;
|
||||
}
|
||||
|
||||
extern "C" void quic_gtest_session_ctx_free_cb(struct session *sess, void *session_ctx, void *plugin_env)
|
||||
{
|
||||
free(session_ctx);
|
||||
}
|
||||
|
||||
extern "C" void *QUIC_TEST_PLUG_INIT(struct stellar *st)
|
||||
{
|
||||
void *fake_quic_gtest_plugin_env = (void *)"_fake_plugin_env_";
|
||||
int quic_gtest_plug_id = stellar_session_plugin_register(st, quic_gtest_plug_session_ctx_new_cb, quic_gtest_session_ctx_free_cb, fake_quic_gtest_plugin_env);
|
||||
int quic_topic_id = stellar_session_mq_get_topic_id(st, QUIC_DECODER_TOPIC);
|
||||
assert(quic_topic_id >= 0);
|
||||
stellar_session_mq_subscribe(st, quic_topic_id, QUIC_TEST_PLUG_ENTRY, quic_gtest_plug_id);
|
||||
|
||||
return fake_quic_gtest_plugin_env;
|
||||
}
|
||||
|
||||
extern "C" void QUIC_TEST_PLUG_DESTROY(void *plugin_env)
|
||||
{
|
||||
return ;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user