summaryrefslogtreecommitdiff
path: root/example/luatest/luatest.cpp
blob: f3e75528629bddc6428b9dab3b1637d62e8e5b3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
/*************************************************************************
    > File Name: main.cpp
    > Author: pxz
    > Created Time: Sun 02 Aug 2020 11:41:22 AM CST
 ************************************************************************/
extern "C"
{
#include<stdio.h>
#include<pcap.h>
#include<stdint.h>
#include<string.h>
#include<arpa/inet.h>
#include<time.h>
#include<unistd.h>
#include<pthread.h>
#include<stdlib.h>
#include<dlfcn.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
}
#include"elua.h"


#define MAC_FRAME_HEADER_LEN    14
#define IP_HEADER_LEN   20
#define TCP_HEADER_LEN  20

#ifndef MAX_THREAD_NUM
#define MAX_THREAD_NUM 16
#endif

#ifndef MIN
#define MIN(a, b)   ((a) > (b) ?(b):(a))
#endif

#define elua_LOOP_TIMES 10000

int c_pcap_flag[MAX_THREAD_NUM];
int c_text_flag[MAX_THREAD_NUM];
int lua_pcap_flag[MAX_THREAD_NUM];
int lua_text_flag[MAX_THREAD_NUM];

typedef int (*c_handle_function)(const u_char *packet, uint32_t packet_len, const char *feature, char *result);
enum {
    text_type = 0x01,
    pcap_type = 0x02
};
typedef struct c_thread_info_s{
    size_t      thread_id;
    const char  *lib;
    const char  *input;
    const char  *feature;
    uint8_t     file_type;
}c_thread_info_t;

typedef struct c_pcap_info_s{
    c_handle_function   c_handle;
    size_t              thread_id;
    uint32_t            packet_num;
    uint64_t            total_time;
    const char          *feature;
    const char          *debuginfo;
}c_pcap_info_t;

typedef struct c_text_info_s{
    c_handle_function   c_handle;
    size_t              thread_id;
    uint64_t            total_time;
    const char          *feature;
    const char          *file_name;
    const char          *debuginfo;
}c_text_info_t;

typedef struct lua_thread_info_s{
    size_t      thread_id;
    const char  *script;
    const char  *input;
    const char  *feature;
    uint8_t     file_type;
}lua_thread_info_t;

typedef struct lua_pacp_info_s
{
    elua_vm *L;
    size_t      script_id;
    size_t      thread_id;
    uint32_t    packet_num;
    uint64_t    total_time;
    const char  *feature;
    char        *debuginfo;
}lua_pcap_info_t;

typedef struct lua_text_info_s
{
    elua_vm *L;
    size_t      script_id;
    size_t      thread_id;
    uint64_t    total_time;
    const char  *feature;
    const char  *file_name;
    char        *debuginfo;
}lua_text_info_t;

typedef struct conf_t
{
    uint32_t thread_num;
    char     feature[1024];
    char     input[128];
    char     script[128];
    char     lib[128];
    uint8_t  file_type;
    uint8_t  complete;
#define THREAD_NUM_COMPLETE 0x01
#define FEATURE_COMPLETE    0x02
#define INPUT_COMPLETE      0x04
#define SCRIPT_COMPLETE     0x08
#define LIB_COMPLETE        0x10
#define FILE_TYPE_COMPLETE  0x20
#define ALL_COMPLETE    (THREAD_NUM_COMPLETE | FEATURE_COMPLETE | INPUT_COMPLETE | SCRIPT_COMPLETE | LIB_COMPLETE | FILE_TYPE_COMPLETE)
}conf_t;

int read_file_list(const char *path, char file_name[][256])
{
    DIR *dir;
    struct dirent *ptr;
    int path_len = strlen(path);
    int file_num = 0;

    if ((dir=opendir(path)) == NULL)
    {
        perror("Open dir error...");
        exit(-1);
    }
    
    while (((ptr=readdir(dir)) != NULL) && (file_num < 100))
    {
        if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)    ///current dir OR parrent dir
            continue;
        else if((ptr->d_type == 8) || (ptr->d_type == 10))
        {
            memcpy(file_name[file_num], path, path_len);
            strcat(file_name[file_num], ptr->d_name);
        }
        else if(ptr->d_type == 4)    ///dir
        {
            continue;
        }
        file_num++;
    }
    closedir(dir);
    return 0;
}

static int init_conf(char *path, conf_t *conf)
{
    char buf[1024];
    int ret = 0;
    size_t buf_len = sizeof(buf);
    FILE *fp = fopen(path, "r");
    if (fp == NULL)
    {
        perror(path);
        return -1;
    }
    while(fgets(buf, buf_len, fp) != NULL)
    {
        switch(buf[0])
        {
            case '#':
            case '\n':
                break;
            case '/':
                if (buf[1] == '*')
                {
                    while(fgets(buf, buf_len, fp) != NULL)
                    {
                        if ((buf[0] == '*') && (buf[1] == '/') && ((buf[3] == '\0') || (buf[3] == '\n')))
                            break;
                    }
                }else
                {
                    printf("error:unknow conf\n");
                    return -1;
                }
                break;
            case 't':
                if (conf->complete & THREAD_NUM_COMPLETE)
                {   
                    break;
                }
                if ((sscanf(buf, "thread_num %u", &conf->thread_num) == EOF) || (conf->thread_num == 0))
                {
                    printf("error: conf_file: thread_num error\n");
                    return -1;
                }
                conf->complete |= THREAD_NUM_COMPLETE;
                break;
            case 'f':
                if (!(conf->complete & FILE_TYPE_COMPLETE))
                {
                    ret =  sscanf(buf, "file_type %hhu", &conf->file_type);
                    if ((ret !=0) && (ret != EOF))
                    {
                        conf->complete |= FILE_TYPE_COMPLETE;
                        break;
                    }
                }

                if (!(conf->complete & FEATURE_COMPLETE))
                {
                    ret = sscanf(buf, "feature %1023s", conf->feature); 
                    if ((ret !=0 ) && (ret != EOF) && (strlen(conf->feature) != 0))
                    {
                        conf->complete |= FEATURE_COMPLETE;
                        break;
                    }
                }
                break;
            case 'i':
                if (conf->complete & INPUT_COMPLETE)
                {
                    break;
                }
                if ((sscanf(buf, "input %127s", conf->input) == EOF) || strlen(conf->input) == 0)
                {
                    printf("error: conf_file: input error\n");
                    return -1;
                }
                conf->complete |= INPUT_COMPLETE;
                break;
            case 's':
                if (conf->complete & SCRIPT_COMPLETE)
                {
                    break;
                }
                if ((sscanf(buf, "script %127s", conf->script) == EOF) || (strlen(conf->script) == 0))
                {
                    printf("error: conf_file: script error\n");
                    return -1;
                }
                conf->complete |= SCRIPT_COMPLETE;
                break;
            case 'l':
                if (conf->complete & LIB_COMPLETE)
                {
                    break;
                }
                if ((sscanf(buf, "lib %127s", conf->lib) == EOF) || (strlen(conf->lib) == 0))
                {
                    printf("error: conf_file: lib error\n");
                    return -1;
                }
                conf->complete |= LIB_COMPLETE;
                break;
            default:
                printf("error:unknow conf\n");
                return -1;
        }
    }
    fclose(fp);
    if (conf->complete == ALL_COMPLETE )
    {
        return 0;
    }
    printf("configure not complete\n");
    return -1;
}

static uint64_t  calc_time(struct timespec start, struct timespec end)
{
    return (end.tv_sec * 1000 * 1000 * 1000 + end.tv_nsec - 
        (start.tv_sec * 1000 * 1000 * 1000 + start.tv_nsec));
}

int file_to_buffer(const char *file, char *buffer, size_t *len)
{
    FILE *fp = fopen(file, "r");
    int num = 0;
    *len = 0;
    if (fp == NULL)
    {
        printf("fopen file failed:%s\n", file);
        return -1;
    }
    do{
        num = fread(&buffer[*len], 1, 4096, fp);
        if (num < 0)
        {
            return -1;
        }
        *len += num;
    }while(num == 4096);
    fclose(fp);
    return 0;
}

void c_pcap_handle(u_char *userarg, const struct pcap_pkthdr *pkthdr, const u_char *packet)
{
    struct timespec time_start, time_end;
    uint32_t i = 0;
    c_pcap_info_t *pcap_info = (c_pcap_info_t *)userarg;
    uint64_t time = 0;
    char result[512];
    int debuginfo_len;
    const u_char *tcp_payload;
    int tcp_payload_len;

    pcap_info->packet_num++;
    if (pkthdr->caplen <= MAC_FRAME_HEADER_LEN + IP_HEADER_LEN + TCP_HEADER_LEN)
    {
        return ;
    }
    tcp_payload = packet + MAC_FRAME_HEADER_LEN + IP_HEADER_LEN + TCP_HEADER_LEN;
    tcp_payload_len = pkthdr->caplen - MAC_FRAME_HEADER_LEN - IP_HEADER_LEN - TCP_HEADER_LEN;  
    
    /* 首次运行重复三次再取时间 */
    while(c_pcap_flag[pcap_info->thread_id] != 3)
    {
        memset(result, 0, sizeof(result));
        for(i = 0; i < elua_LOOP_TIMES; i++)
        {
            pcap_info->c_handle(tcp_payload, tcp_payload_len, pcap_info->feature, result);
        }
        c_pcap_flag[pcap_info->thread_id]++;
    }

    memset(result, 0, sizeof(result));
    clock_gettime(CLOCK_MONOTONIC, &time_start);
    for(i = 0; i < elua_LOOP_TIMES; i++)
    {
        pcap_info->c_handle(tcp_payload, tcp_payload_len, pcap_info->feature, result);
    }
    clock_gettime(CLOCK_MONOTONIC, &time_end);
    time += calc_time(time_start, time_end);
    time /= elua_LOOP_TIMES;
    pcap_info->total_time += time;

    debuginfo_len = strlen(pcap_info->debuginfo);
    if (debuginfo_len < 9500)
    {
        sprintf((char *)&pcap_info->debuginfo[debuginfo_len], "%-10s%-10zu%-11u%-11d%-20s%-20s%-20lu%-20lu\n", 
                "C", pcap_info->thread_id, pcap_info->packet_num, pkthdr->caplen, pcap_info->feature, result, time, pcap_info->total_time);
    }
    return;
}

static void c_text_handle(c_text_info_t *text_info, char *debuginfo)
{
    char buffer[1024 * 1024];
    size_t buffer_len;
    char result[512];
    struct timespec time_start, time_end;
    uint64_t time;
    int i;
    const char *file_name;

    if ((text_info == NULL) || (debuginfo == NULL))
    {
        return ;
    }

    /* 普通文件处理 */
    if (file_to_buffer(text_info->file_name, buffer, &buffer_len))
    {
        return;
    }
    /* 首次运行重复三次再取时间 */
    while(c_text_flag[text_info->thread_id] != 3)
    {
        memset(result, 0, sizeof(result));
        for(i = 0; i < elua_LOOP_TIMES; i++)
        {
            text_info->c_handle((const u_char *)buffer, buffer_len, text_info->feature, result);
        }
        c_text_flag[text_info->thread_id]++;
    }

    clock_gettime(CLOCK_MONOTONIC, &time_start);
    for (i = 0; i < elua_LOOP_TIMES; i ++)
    {
        if (text_info->c_handle((const u_char *)buffer, buffer_len, text_info->feature, result))
        {
            return;
        }
    }
    clock_gettime(CLOCK_MONOTONIC, &time_end);
    time = calc_time(time_start, time_end);
    time /= elua_LOOP_TIMES;
    text_info->total_time += time;

    file_name = (const char *)memrchr(text_info->file_name, '/', strlen(text_info->file_name));
    if (file_name == NULL)
    {
        file_name = text_info->file_name;
    }else
    {
        file_name++;
    }

    sprintf(&debuginfo[strlen(debuginfo)], "%-10s%-10zu%-20s%-20s%-20s%-20lu%-20lu\n", 
            "C", text_info->thread_id, file_name, text_info->feature, result, time, text_info->total_time);
    return;
}

static void c_pcap_prefix_handle(c_thread_info_t *thread_info, char *debuginfo, c_handle_function c_handle )
{
    if ((thread_info == NULL) || (debuginfo == NULL) || (c_handle == NULL))
    {
        return ;
    }
    /* pcap 文件处理 */
    if (thread_info->file_type & pcap_type)
    {
        char ebuf[PCAP_ERRBUF_SIZE];
        pcap_t *pcap_handle = pcap_open_offline(thread_info->input, ebuf);
        if (pcap_handle == NULL)
        {
            printf("input is error\n");
            return ;
        }

        c_pcap_info_t pcap_info;
        pcap_info.feature = thread_info->feature;
        pcap_info.c_handle = c_handle;
        pcap_info.debuginfo = debuginfo;
        pcap_info.packet_num = 0;
        pcap_info.total_time = 0;
        pcap_info.thread_id = thread_info->thread_id;

        pcap_loop(pcap_handle, -1, c_pcap_handle, (u_char *)&pcap_info);
        pcap_close(pcap_handle);

    }
    return ;
}

static void *c_file_handle(void *ptr)
{
    c_thread_info_t *thread_info = (c_thread_info_t *)ptr;
    char *debuginfo;
    void *lib_handle = NULL;
    c_handle_function c_handle; 
    struct stat s_buf;
    char file_name[100][256];
    c_text_info_t text_info;

    debuginfo = (char *)malloc(10240);
    if (debuginfo == NULL)
    {
        perror(" ");
        pthread_exit(NULL);
    }
    memset(debuginfo, 0, 10240);

    lib_handle = dlopen(thread_info->lib, RTLD_LAZY);
    if (lib_handle == NULL)
    {
        fprintf (stderr, "%s\n", dlerror());
        pthread_exit(debuginfo);
    }

    c_handle = (c_handle_function)dlsym(lib_handle, "c_handle_function");
    if (c_handle == NULL)
    {
        fprintf (stderr, "%s\n", dlerror());
        dlclose(lib_handle);
        pthread_exit(debuginfo);
    }

    text_info.thread_id = thread_info->thread_id;
    text_info.total_time = 0;
    text_info.feature = thread_info->feature;
    text_info.debuginfo = debuginfo;
    text_info.c_handle = c_handle;

    stat(thread_info->input, &s_buf);
    /* 目录处理 */
    if (S_ISDIR(s_buf.st_mode))
    {
        memset(file_name, 0, 100 * 256);
        read_file_list(thread_info->input, file_name);

        if (thread_info->file_type & pcap_type)
        {
            for (uint32_t i = 0; i < 100; i++)
            {
                if(file_name[i][0] == '\0')
                    break;
                thread_info->input = file_name[i];
                c_pcap_prefix_handle(thread_info, debuginfo, c_handle); 
            }
        }else
        {
            for (uint32_t i = 0; i < 100; i++)
            {
                if(file_name[i][0] == '\0')
                    break;
                text_info.file_name = file_name[i];
                c_text_handle(&text_info, debuginfo);
            }
        }
    }else
    {
        /* 文件处理 */
        if (thread_info->file_type & pcap_type)
        {
            c_pcap_prefix_handle(thread_info, debuginfo, c_handle); 
        }else
        {
            text_info.file_name = thread_info->input;
            c_text_handle(&text_info, debuginfo);
        }
    }

    dlclose(lib_handle);
    pthread_exit(debuginfo);
}

void lua_pcap_handle(u_char *userarg, const struct pcap_pkthdr *pkthdr, const u_char *packet)
{
    char out[256] = {0};
    size_t out_len = 0;
    size_t out_type = 0;
    int ret = 0;
    int i;
    lua_pcap_info_t *pcap_info = (lua_pcap_info_t *)userarg;
    char result[512];
    int debuginfo_len;
    struct timespec time_start, time_end;
    uint64_t time = 0;

    if ((pcap_info == NULL) || (pkthdr == NULL) || (packet == NULL))
    {
        return ;
    }

    memset(result, 0, sizeof(result));
    pcap_info->packet_num++; 
    if (pkthdr->caplen <= MAC_FRAME_HEADER_LEN + IP_HEADER_LEN + TCP_HEADER_LEN)
    {
        return ;
    }
    const u_char *tcp_payload = packet + MAC_FRAME_HEADER_LEN + IP_HEADER_LEN + TCP_HEADER_LEN;
    int tcp_payload_len = pkthdr->caplen - MAC_FRAME_HEADER_LEN - IP_HEADER_LEN - TCP_HEADER_LEN;  

    while (lua_pcap_flag[pcap_info->thread_id] != 3)
    {
        for (i = 0; i < elua_LOOP_TIMES; i++)
        {
            ret = elua_execute_script(pcap_info->L, pcap_info->script_id, (const char *)tcp_payload, tcp_payload_len, out, &out_len, &out_type);
            if (ret < 0)
            {
                sprintf(result, "failed");
                pthread_exit(pcap_info->debuginfo);
            }
        }
        lua_pcap_flag[pcap_info->thread_id]++;
    }

    clock_gettime(CLOCK_MONOTONIC, &time_start);
    for (i = 0; i < elua_LOOP_TIMES; i++)
    {
        ret = elua_execute_script(pcap_info->L, pcap_info->script_id, (const char *)tcp_payload, tcp_payload_len, out, &out_len, &out_type);
        if (ret < 0)
        {
            sprintf(result, "failed");
            pthread_exit(pcap_info->debuginfo);
        }
    }
    clock_gettime(CLOCK_MONOTONIC, &time_end);
    time += calc_time(time_start, time_end);
    time /= elua_LOOP_TIMES;
    pcap_info->total_time += time;

    switch(out_type)
    {
        case STRING:
            snprintf(result, out_len + 1, "%s", out);
            break;
        case BOOLEAN:
            sprintf(result, "%s", out[0] ? "true":"false");
            break;
        case INTEGER:
            sprintf(result, "%lu", *(uint64_t *)out);
            break;
        default:
            break;
    }
    debuginfo_len = strlen(pcap_info->debuginfo);
    if (debuginfo_len < 9500)
    {
        sprintf(&pcap_info->debuginfo[debuginfo_len], "%-10s%-10zu%-11u%-11d%-20s%-20s%-20lu%-20lu\n", 
                "LUA", pcap_info->thread_id, pcap_info->packet_num, pkthdr->caplen, pcap_info->feature, result, time, pcap_info->total_time);
    }
    return ;
}
static void lua_pcap_prefix_handle(lua_thread_info_t *thread_info, char *debuginfo, elua_vm *L, size_t script_id)
{

    if ((thread_info == NULL) || (debuginfo == NULL) || (script_id < 1))
    {
        return ;
    }
    /* pcap 文件处理 */
    if (thread_info->file_type & pcap_type)
    {
        char ebuf[PCAP_ERRBUF_SIZE];
        pcap_t *pcap_handle = pcap_open_offline(thread_info->input, ebuf);
        if (pcap_handle == NULL)
        {
            printf("input is error\n");
            return ;
        }

        lua_pcap_info_t pcap_info;
        pcap_info.L = L;
        pcap_info.feature = thread_info->feature;
        pcap_info.script_id = script_id;
        pcap_info.debuginfo = debuginfo;
        pcap_info.packet_num = 0;
        pcap_info.total_time = 0;
        pcap_info.thread_id = thread_info->thread_id;

        pcap_loop(pcap_handle, -1, lua_pcap_handle, (u_char *)&pcap_info);
        pcap_close(pcap_handle);

    }
    return ;
}

static void lua_text_handle(lua_text_info_t *text_info, char *debuginfo, elua_vm *Lua)
{
    char buffer[1024 * 1024];
    size_t buffer_len;
    char result[512];
    struct timespec time_start, time_end;
    uint64_t time;
    int i;
    int ret;
    size_t out_len = 0;
    size_t out_type = 0;
    char out[256] = {0};
    const char *file_name;

    if ((text_info == NULL) || (debuginfo == NULL))
    {
        return ;
    }

    /* 普通文件处理 */
    if (file_to_buffer(text_info->file_name, buffer, &buffer_len))
    {
        return;
    }

    while (lua_text_flag[text_info->thread_id] != 3)
    {
        for (i = 0; i < elua_LOOP_TIMES; i++)
        {
            ret = elua_execute_script(lua, text_info->script_id, (const char *)buffer, buffer_len, out, &out_len, &out_type);
            if (ret < 0)
            {
                return;
            }
        }
        lua_text_flag[text_info->thread_id]++;
    }

    clock_gettime(CLOCK_MONOTONIC, &time_start);
    for (i = 0; i < elua_LOOP_TIMES; i ++)
    {
        ret = elua_execute_script(lua, text_info->script_id, (const char *)buffer, buffer_len, out, &out_len, &out_type);
        if (ret < 0)
        {
            return;
        }
    }
    clock_gettime(CLOCK_MONOTONIC, &time_end);
    time = calc_time(time_start, time_end);
    time /= elua_LOOP_TIMES;
    text_info->total_time += time;

    switch(out_type)
    {
        case STRING:
            snprintf(result, out_len + 1, "%s", out);
            break;
        case BOOLEAN:
            sprintf(result, "%s", out[0] ? "true":"false");
            break;
        case INTEGER:
            sprintf(result, "%lu", *(uint64_t *)out);
            break;
        default:
            break;
    }
    file_name = (const char *)memrchr(text_info->file_name, '/', strlen(text_info->file_name));
    if (file_name == NULL)
    {
        file_name = text_info->file_name;
    }else
    {
        file_name++;
    }

    sprintf(&debuginfo[strlen(debuginfo)], "%-10s%-10zu%-20s%-20s%-20s%-20lu%-20lu\n", 
            "lua", text_info->thread_id, file_name, text_info->feature, result, time, text_info->total_time);

    return ;
}

static void *lua_file_handle(void *ptr)
{
    size_t script_id;
    char script[4096];
    size_t script_len;
    elua_vm *Lua;
    char *debuginfo = NULL;
    lua_thread_info_t * thread_info = (lua_thread_info_t *)ptr;
    char result[512];
    lua_text_info_t text_info;
    struct stat s_buf;
    char file_name[100][256];

    debuginfo = (char *)malloc(10240);
    if (debuginfo == NULL)
    {
        perror(" ");
        pthread_exit(NULL);
    }
    memset(debuginfo, 0, 10240);
    memset(result, 0, sizeof(result));

    lua = elua_create_vm(NULL);
    if (lua == NULL)
    {
        pthread_exit(debuginfo);
    }
    if (file_to_buffer(thread_info->script, script, &script_len))
    {
        pthread_exit(debuginfo);
    }
    script_id = elua_cache_script(lua, script, script_len);
    if (script_id < 1)
    {
        pthread_exit(debuginfo);
    }

    text_info.L = lua;
    text_info.thread_id = thread_info->thread_id;
    text_info.script_id = script_id;
    text_info.total_time = 0;
    text_info.feature = thread_info->feature;
    text_info.debuginfo = debuginfo;

    stat(thread_info->input, &s_buf);
    /* 目录处理 */
    if (S_ISDIR(s_buf.st_mode))
    {
        memset(file_name, 0, 100 * 256);
        read_file_list(thread_info->input, file_name);

        if (thread_info->file_type & pcap_type)
        {
            for (uint32_t i = 0; i < 100; i++)
            {
                if(file_name[i][0] == '\0')
                    break;
                thread_info->input = file_name[i];
                lua_pcap_prefix_handle(thread_info, debuginfo, lua, script_id); 
            }
        }else
        {
            for (uint32_t i = 0; i < 100; i++)
            {
                if(file_name[i][0] == '\0')
                    break;
                text_info.file_name = file_name[i];
                lua_text_handle(&text_info, debuginfo, lua);
            }
        }
    }else
    {
        /* 文件处理 */
        if (thread_info->file_type & pcap_type)
        {
            lua_pcap_prefix_handle(thread_info, debuginfo, lua, script_id); 
        }else
        {
            text_info.file_name = thread_info->input;
            lua_text_handle(&text_info, debuginfo, lua);
        }
    }
    pthread_exit(debuginfo);
}

int main(int argc, char *argv[])
{
    conf_t conf;
    int ret;
    char conf_path[128];
    pthread_t thread[MAX_THREAD_NUM] = {0};
    pthread_attr_t attr[MAX_THREAD_NUM] = {0};
    uint32_t thread_num; 
    int ch;
    int buf_len;
    lua_thread_info_t lua_thread_info[MAX_THREAD_NUM];
    c_thread_info_t c_thread_info[MAX_THREAD_NUM];
    char *retval;
    cpu_set_t mask;
    FILE *log = NULL;
    char log_name[256];
    const char *log_prefix = "./log/";
    time_t timep;

    memset(&conf, 0, sizeof(conf_t));
    strcpy(conf_path, "./conf/default.conf");
    while((ch = getopt(argc, argv, "t:f:i:s:l:c:h")) != -1)
    {
        switch(ch)
        {
            case 't':
                conf.thread_num = atoi(optarg);
                if (conf.thread_num >= MAX_THREAD_NUM)
                {
                    printf("MAX_THREAD_NUM is %d\n",MAX_THREAD_NUM - 1);
                    conf.thread_num = MAX_THREAD_NUM;
                }
                conf.complete |= THREAD_NUM_COMPLETE;
                break; 
            case 'f':
                buf_len = MIN(1024,  strlen(optarg));
                strncpy((char *)conf.feature, optarg, buf_len);
                conf.complete |= FEATURE_COMPLETE;
                break;          
            case 'i':
                buf_len = MIN(127, strlen(optarg));
                strncpy(conf.input, optarg, buf_len);
                conf.input[buf_len] = '\0';
                conf.complete |= INPUT_COMPLETE;
                break;
            case 's':
                buf_len = MIN(127, strlen(optarg));
                strncpy(conf.script, optarg, buf_len);
                conf.script[buf_len] = '\0';
                conf.complete |= SCRIPT_COMPLETE;
                break;
            case 'l':
                buf_len = MIN(127, strlen(optarg));
                strncpy(conf.lib, optarg, buf_len);
                conf.lib[buf_len] = '\0';
                conf.complete |= LIB_COMPLETE;
                break;
            case 'c':
                buf_len = MIN(127, strlen(optarg));
                strncpy(conf_path, optarg, buf_len);
                conf_path[buf_len] = '\0';
                break;
            case 'h':
            default:
                printf("usage: multithread \n[-t set thread num] \n[-f set feature] \n"
                        "[-i set input] \n[-s set script] \n[-l set library] \n"
                        "[-c set conf_path] \n[-h show help info]\n");
                return -1;
                break;
        }
    }

    ret = init_conf(conf_path, &conf);
    if (ret != 0)
    {
        return -1;
    }

    strcpy(log_name, log_prefix);
    time(&timep);
    strftime(&log_name[strlen(log_prefix)], sizeof(log_name) - strlen(log_prefix),"%Y%m%d%H%M%S.log", localtime(&timep));
    log = fopen(log_name, "a+");
    if (log == NULL)
    {
        perror(log_name);
        return -1;
    }

    /* 识别文件类型 */

    printf("\n==============================================================================================================================\n");
    if (conf.file_type & pcap_type)
    {
        printf("%-10s%-10s%-11s%-11s%-20s%-20s%-20s%-20s\n", "C or LUA", "thread_id", "packet_num", "packet_len", "feature", "result", "time", "total_time");
    }else
    {
        printf("%-10s%-10s%-20s%-20s%-20s%-20s\n", "C or LUA", "thread_id", "file_name", "feature", "result", "time");
    }
#if 1
    /* c function */
    for ( thread_num = 0; thread_num < conf.thread_num; thread_num++ )
    {
        c_thread_info[thread_num].lib = conf.lib;
        c_thread_info[thread_num].input = conf.input;
        c_thread_info[thread_num].feature = conf.feature;
        c_thread_info[thread_num].thread_id = thread_num;
        c_thread_info[thread_num].file_type = conf.file_type;
        pthread_attr_init(&attr[thread_num]);
        if(pthread_create(&thread[thread_num], &attr[thread_num], c_file_handle, (void *)&c_thread_info[thread_num]))
        {
            perror(" ");
            fclose(log);
            return -1;
        }

        CPU_ZERO(&mask);
        CPU_SET(thread_num + 8, &mask);
        if (pthread_setaffinity_np(thread[thread_num], sizeof(mask), &mask) != 0)
        {
            printf("warning:could not set CPU affinity, continuing...\n");
        }
    }
    printf("\n");

    for (thread_num = 0; thread_num < conf.thread_num; thread_num++)
    {
        pthread_join(thread[thread_num], (void **)&retval);
        if (retval)
        {
            printf("%s", retval);
            fwrite(retval, strlen(retval), 1, log);
            free(retval);
        }
    }
#endif
    /* lua function */
    for (thread_num = 0; thread_num < conf.thread_num; thread_num++)
    {
        lua_thread_info[thread_num].script = conf.script;
        lua_thread_info[thread_num].input = conf.input;
        lua_thread_info[thread_num].feature = conf.feature;
        lua_thread_info[thread_num].thread_id = thread_num;
        lua_thread_info[thread_num].file_type = conf.file_type;
        pthread_attr_init(&attr[thread_num]);
        if(pthread_create(&thread[thread_num], &attr[thread_num], lua_file_handle, (void *)&lua_thread_info[thread_num]) != 0)
        {
            perror(" ");
            fclose(log);
            return -1;
        }

        CPU_ZERO(&mask);
        CPU_SET(thread_num + 8, &mask);
        if (pthread_setaffinity_np(thread[thread_num], sizeof(mask), &mask) != 0)
        {
            printf("warning:could not set CPU affinity, continuing...\n");
        }
    }
    printf("\n");

    for (thread_num = 0; thread_num < conf.thread_num; thread_num++)
    {
        pthread_join(thread[thread_num], (void **)&retval);
        if (retval)
        {
            printf("%s", retval);
            fwrite(retval, strlen(retval), 1, log);
            free(retval);
        }
    }

    fclose(log);

    return 0;
}