summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author贺文梁 <[email protected]>2018-11-30 17:43:01 +0800
committer贺文梁 <[email protected]>2018-11-30 17:43:01 +0800
commit031ae07542c53764dd178b1607e88c69fcc793ae (patch)
treea74a8fe79e4aee99eb532f5559513282428dfe7a
parentffc2bc9bc2f6e38f1cbfd030229ceb35e6478ead (diff)
Upload New File
-rw-r--r--src/tls_test.c255
1 files changed, 255 insertions, 0 deletions
diff --git a/src/tls_test.c b/src/tls_test.c
new file mode 100644
index 0000000..569d41e
--- /dev/null
+++ b/src/tls_test.c
@@ -0,0 +1,255 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <sys/time.h>
+#include <time.h>
+#include <errno.h>
+#include <MESA/MESA_htable.h>
+#include <MESA/MESA_handle_logger.h>
+#include "ssl.h"
+#include "stream.h"
+
+#define REUSE_PATH "/home/hewenliang/result/reuse.txt"
+#define MAPS_PATH "/home/hewenliang/result/maps.txt"
+#define RUNTIME_LOG "/home/hewenliang/result/runtime.log"
+#define LEN_PATH "/home/hewenliang/result/len.txt"
+//#define SET_ZERO
+#define THREAD_MAX 40
+#define STR_LENGTH 100
+#define TCPLEN_RANGE 30
+#define TCPLEN 500
+#define true 1
+#define false 0
+typedef int bool;
+
+typedef struct _st_Thrstatis
+{
+ uint id_reuse; //session id复用数
+ uint ticket_reuse; //session ticket复用数
+ uint datalen[TCPLEN_RANGE]; ////整个TCP流中在(i~i+1)*TCPLEN_RANGE内的个数
+ uint total; //总会话数
+}st_Thrstatis;
+
+typedef struct _st_StatisMap
+{
+ MESA_htable_handle versions; //SSL版本哈希表
+ MESA_htable_handle ciphers; //加密算法哈希表
+ MESA_htable_handle compresses; //压缩算法哈希表
+ MESA_htable_handle lens; //流长度哈希表
+ int total_num; //总数
+}st_StatisMap;
+
+static st_Thrstatis Thrd_stat[THREAD_MAX];
+static st_StatisMap statisMap;
+static FILE* reuse_fd = NULL, *maps_fd = NULL, *log_fd = NULL, *len_fd = NULL;
+
+void printf_maps(const uchar *key, uint size, void* data, void* arg){
+ int *value = (int*)data;
+ int i;
+ fprintf(maps_fd, "0x");
+ for(i = 0; i < size; i++){
+ fprintf(maps_fd, "%02x", key[i]);
+ }
+ fprintf(maps_fd, ":%d\t", *value);
+#ifdef SET_ZERO
+ *value = 0;
+#endif
+}
+
+static long hash_addone_cb(void* data, const uchar* key, uint size, void* arg){
+ if(data == NULL){
+ int *value = (int*)malloc(sizeof(int));
+ *value = 1;
+ MESA_htable_handle* p_handle = (MESA_htable_handle*)arg;
+ MESA_htable_add(*p_handle, key, size, (void*)value);
+ }else{
+ int* p = (int*)data;
+ ++(*p);
+ }
+ return 0;
+}
+
+// init thread_safe hashtables
+void htable_init(MESA_htable_handle *p_hd){
+ *p_hd = MESA_htable_born();
+
+ int opt_int = 1;
+ MESA_htable_set_opt(*p_hd, MHO_THREAD_SAFE, &opt_int, sizeof(opt_int));
+
+ int ret = MESA_htable_mature(*p_hd);
+ if(ret != 0){
+ MESA_handle_runtime_log(log_fd, RLOG_LV_FATAL, "HashTable Error", "Creat Htable error");
+ exit(-1);
+ }
+}
+
+void sig_handle(int signo)
+{
+ time_t now;
+ int i;
+ int id_sum=0,tkt_sum=0,tot_sum=0;
+ int tcplen[TCPLEN_RANGE+1] = {0};
+ time(&now);
+ char timeBuf[STR_LENGTH];
+ strftime(timeBuf, STR_LENGTH,"%m-%d %H:%M:%S\t", localtime(&now));
+ fprintf(reuse_fd,"%s",timeBuf);
+ fprintf(maps_fd,"%s\n",timeBuf);
+ for(i = 0; i < THREAD_MAX && Thrd_stat[i].total > 0; i++){
+ // fprintf(fd,"Thread %d:\tid_Num:%ld\ttkt_Num:%ld\ttotal:%ld \n", i, id_reuse[i], ticket_reuse[i], Total_Num[i]);
+ id_sum += Thrd_stat[i].id_reuse;
+ tkt_sum += Thrd_stat[i].ticket_reuse;
+ tot_sum += Thrd_stat[i].total;
+ int j;
+ for(j = 0; j < TCPLEN_RANGE; j++){
+ tcplen[j] += Thrd_stat[i].datalen[j];
+ }
+#ifdef SET_ZERO
+ Thrd_stat[i].id_reuse = 0;
+ Thrd_stat[i].ticket_reuse = 0;
+ Thrd_stat[i].total = 0;
+#endif
+ }
+ fprintf(reuse_fd,"%d\t%d\t%d\n", id_sum, tkt_sum, tot_sum);
+ for(i = 0; i < TCPLEN_RANGE; i++){
+ fprintf(len_fd, "\t%d",tcplen[i]);
+ }
+ fprintf(len_fd, "\n");
+ MESA_htable_iterate(statisMap.versions, printf_maps, NULL);
+ fprintf(maps_fd, "\n");
+ MESA_htable_iterate(statisMap.ciphers, printf_maps, NULL);
+ fprintf(maps_fd, "\n");
+ MESA_htable_iterate(statisMap.compresses, printf_maps, NULL);
+ fprintf(maps_fd, "\n%d\n",statisMap.total_num);
+ statisMap.total_num = 0;
+ fflush(reuse_fd);
+ fflush(maps_fd);
+ fflush(len_fd);
+}
+
+UCHAR TLS_TEST_ENTRY(stSessionInfo* session_info, void **param, int thread_seq, struct streaminfo *a_tcp, void *a_packet)
+{
+ //tls_test.inf set FUNC_FLAG=SSL_APPLICATION_DATA to get all "client hello" and "Cert" message
+ //只设置为SSL_CLIENT_HELLO时,无法得到证书信息(可能因为client hello逻辑完成后就没有继续解析证书了)
+ //只设置为SSL_CERTIFICATE时,如果会话复用没有证书信息,则无法统计
+ if(NULL == session_info)
+ {
+ //printf("session_info null return \n");
+ return PROT_STATE_DROPME;
+ }
+ if(!session_info->prot_flag)
+ {
+ return PROT_STATE_GIVEME;
+ }
+ ssl_stream *a_ssl_stream = (ssl_stream *)(session_info->app_info);
+ st_client_hello_t* client_hello = a_ssl_stream->stClientHello;
+ st_server_hello_t* server_hello = a_ssl_stream->stServerHello;
+
+ //处理server hello中的加密压缩算法信息
+ if(SSL_SERVER_HELLO == session_info->prot_flag && server_hello->ciphersuits.suite_len != 0){
+ char ssl_ver[2];
+ int ver_len = 2;
+ long return_cb;
+ ssl_ver[0] = server_hello->client_ver>>8;
+ ssl_ver[1] = server_hello->client_ver&7;
+ MESA_htable_search_cb(statisMap.versions, ssl_ver, ver_len, hash_addone_cb, (void*)&statisMap.versions, &return_cb);
+ MESA_htable_search_cb(statisMap.ciphers, server_hello->ciphersuits.suite_value, server_hello->ciphersuits.suite_len, hash_addone_cb, (void*)&statisMap.ciphers, &return_cb);
+ MESA_htable_search_cb(statisMap.compresses, server_hello->com_method.methods, (unsigned short)server_hello->com_method.methlen, hash_addone_cb, (void*)&statisMap.compresses, &return_cb);
+ ++statisMap.total_num;
+ return PROT_STATE_GIVEME;
+ }
+ if(client_hello == NULL)
+ {
+ // printf("No Client Hello\n");
+ return PROT_STATE_DROPME;
+ }
+ if(*param == NULL){
+
+ //printf("%d\t%d\n",a_ssl_stream->uiAllMsgLen,a_ssl_stream->uiMsgProcLen);
+ //printf("buflen:\t%d\tAllMsgLen:\t%d\n", session_info->buflen, a_ssl_stream->uiAllMsgLen);
+
+
+
+ *param = (char*)malloc(sizeof(char));
+
+ if(a_ssl_stream->stSSLCert == NULL)
+ {
+ int i;
+ bool se_ticket = false;
+ for(i=0; i<client_hello->ext_num; i++){
+ // printf("%d\n", client_hello->exts[i].type);
+ if(client_hello->exts[i].type == 35 && client_hello->exts[i].len>0){
+ se_ticket = true;
+ break;
+ }
+ }
+ if(se_ticket)
+ ++Thrd_stat[thread_seq].ticket_reuse;
+ else
+ ++Thrd_stat[thread_seq].id_reuse;
+ }
+ ++Thrd_stat[thread_seq].total;
+ // return PROT_STATE_DROPME;
+ }
+ if(session_info->session_state == OP_STATE_CLOSE){
+ int tcplen = (a_tcp->ptcpdetail->serverbytes + a_tcp->ptcpdetail->clientbytes) ;
+ tcplen = tcplen < TCPLEN_RANGE * TCPLEN ? tcplen/TCPLEN : TCPLEN_RANGE-1;
+ ++Thrd_stat[thread_seq].datalen[tcplen];
+ free(*param);
+ return PROT_STATE_DROPME;
+ }
+
+ return PROT_STATE_GIVEME;
+}
+
+int TLS_TEST_INIT(void)
+{
+ printf("TLS_TEST_INIT run \n");
+ signal(SIGALRM, sig_handle);
+ reuse_fd = fopen(REUSE_PATH, "w");
+ maps_fd = fopen(MAPS_PATH,"w");
+ log_fd = fopen(RUNTIME_LOG, "w");
+ len_fd = fopen(LEN_PATH, "w");
+
+ if(reuse_fd == NULL || maps_fd == NULL || log_fd == NULL || len_fd == NULL)
+ {
+ perror("Open file error! \n");
+ exit(-1);
+ }
+ MESA_handle_runtime_log(log_fd, RLOG_LV_FATAL, "OPEN FILE", "Open files success!");
+ //设置定时
+ printf("time\tsession_id\tsession_tkt\ttotal\n");
+ fprintf(reuse_fd, "time\tsession_id\tsession_tkt\ttotal\n");
+ fprintf(maps_fd, "1st:version\t2nd:ciphersuits\t3rd:compresses\t4th:total\n");
+
+ //init maps to statistic
+ htable_init(&statisMap.versions);
+ htable_init(&statisMap.ciphers);
+ htable_init(&statisMap.compresses);
+ htable_init(&statisMap.lens);
+ statisMap.total_num = 0;
+
+ //设置写文件间隔
+ struct itimerval new_value, old_value;
+ new_value.it_value.tv_sec = 0;
+ new_value.it_value.tv_usec = 1;
+ new_value.it_interval.tv_sec = 10;
+ new_value.it_interval.tv_usec = 0;
+ setitimer(ITIMER_REAL, &new_value, &old_value);
+ return 0;
+}
+
+void TLS_TEST_DESTROY(void)
+{
+ printf("TLS_TEST_DESTROY run \n");
+ MESA_htable_destroy(statisMap.versions, NULL);
+ MESA_htable_destroy(statisMap.ciphers, NULL);
+ MESA_htable_destroy(statisMap.compresses, NULL);
+ MESA_htable_destroy(statisMap.lens, NULL);
+ fclose(reuse_fd);
+ fclose(len_fd);
+ fclose(maps_fd);
+ fclose(log_fd);
+}