-
Notifications
You must be signed in to change notification settings - Fork 1
/
enricher.c
158 lines (120 loc) · 4.23 KB
/
enricher.c
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
//
// Created by tyy on 2017/1/13.
//
#define _BSD_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "wrapper.h"
#include "extractor.h"
#include "http.h"
struct enrichee enrichees[MAX_ENRICHEE];
bulk_t bulk;
int init() {
for (int i = 0; i < MAX_ENRICHEE; i++) {
enrichees[i].enriched_value = (char *) malloc(MAX_ENRICHED_VALUE_LEN);
}
return 0;
}
int type_enricher(struct enrichee *enrichee__) {
strncpy(bulk.type, enrichee__->orig_value, enrichee__->orig_value_len);
bulk.type[enrichee__->orig_value_len] = '\0';
enrichee__->orig_value_len = 0;
enrichee__->enriched_value_len = 0;
return 0;
}
int guid_enricher(struct enrichee *enrichee__) {
strncpy(bulk.guid, enrichee__->orig_value, enrichee__->orig_value_len);
bulk.guid[enrichee__->orig_value_len] = '\0';
enrichee__->orig_value_len = 0;
enrichee__->enriched_value_len = 0;
return 0;
}
int ip_enricher(struct enrichee *enrichee__) {
char value[MAX_ORIG_VAL_LEN] = {0,};
strncpy(value, enrichee__->orig_value, enrichee__->orig_value_len);
char *output = ip2JsonStr(value);
if (!output) {
enrichee__->orig_value_len = 0;
enrichee__->enriched_value_len = 0;
return 0;
}
int len = strlen(output);
memset(enrichee__->enriched_value, 0, MAX_ENRICHED_VALUE_LEN);
strncpy(enrichee__->enriched_value, output, len);
enrichee__->enriched_value_len = len;
return 0;
}
int ua_enricher(struct enrichee *enrichee__) {
char value[MAX_ORIG_VAL_LEN] = {0,};
// remove "
strncpy(value, enrichee__->orig_value + 1, enrichee__->orig_value_len - 2);
char *output = ua2JsonStr(value, enrichee__->orig_value_len - 2);
if (!output) {
enrichee__->orig_value_len = 0;
enrichee__->enriched_value_len = 0;
return 0;
}
int len = strlen(output);
memset(enrichee__->enriched_value, 0, MAX_ENRICHED_VALUE_LEN);
strncpy(enrichee__->enriched_value, output, len);
enrichee__->enriched_value_len = len;
return 0;
}
int time_enricher(struct enrichee *enrichee__) {
int offset = 0;
char *value = "\"@timestamp\":\"";
memset(enrichee__->enriched_value, 0, MAX_ENRICHED_VALUE_LEN);
strncpy(enrichee__->enriched_value, value, strlen(value));
offset += strlen(value);
time_t t = atoi(enrichee__->orig_value);
t = (t == 0) ? time(NULL) : t;
strftime(enrichee__->enriched_value + offset, MAX_ENRICHED_VALUE_LEN - offset,
"%Y-%m-%dT%H:%M:%SZ", gmtime(&t));
strcat(enrichee__->enriched_value, "\",");
enrichee__->enriched_value_len = strlen(enrichee__->enriched_value);
return 0;
}
// @return : enriched value length
int combine_enrichee(const char *buf, char *result) {
int i;
int offset_buf = 1;
int offset_result = 0;
const char *next_clean_ptr;
int next_clean_len;
// scan and add item to json header
for (i = 0; i < MAX_ENRICHEE; i++) {
if (enrichees[i].use == 1 && enrichees[i].mode == ENR_ADD) {
strncpy(result + offset_result, enrichees[i].enriched_value,
enrichees[i].enriched_value_len);
offset_result += enrichees[i].enriched_value_len;
enrichees[i].use = 0;
}
}
// update or delete item
for (i = 0; i < MAX_ENRICHEE; i++) {
if (enrichees[i].use == 0) {
continue;
}
enrichees[i].use = 0;
// copy before i clean buf
next_clean_ptr = buf + offset_buf;
next_clean_len = enrichees[i].orig_value - (buf + offset_buf);
strncpy(result + offset_result, next_clean_ptr, next_clean_len);
offset_buf += next_clean_len + enrichees[i].orig_value_len;
offset_result += next_clean_len;
// copy i fix
if (enrichees[i].enriched_value_len) {
strncpy(result + offset_result, enrichees[i].enriched_value, enrichees[i].enriched_value_len);
offset_result += enrichees[i].enriched_value_len;
}
}
next_clean_ptr = buf + offset_buf;
if (offset_buf < strlen(buf)) {
next_clean_len = strlen(buf) - offset_buf;
strncpy(result + offset_result, next_clean_ptr, next_clean_len);
offset_result += next_clean_len;
}
return offset_result;
}