Skip to content

Commit 063b395

Browse files
committed
in_syslog: Handle octent_counting
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent d3be337 commit 063b395

File tree

6 files changed

+84
-12
lines changed

6 files changed

+84
-12
lines changed

plugins/in_syslog/syslog.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,11 @@ static struct flb_config_map config_map[] = {
244244
0, FLB_TRUE, offsetof(struct flb_syslog, source_address_key),
245245
"Key where the source address will be injected"
246246
},
247-
247+
{
248+
FLB_CONFIG_MAP_STR, "frame", (char *) NULL,
249+
0, FLB_TRUE, offsetof(struct flb_syslog, frame_str),
250+
"TCP framing: newline (default) or octet_counting (RFC 6587)"
251+
},
248252

249253
/* EOF */
250254
{0}

plugins/in_syslog/syslog.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
/* 32KB chunk size */
3434
#define FLB_SYSLOG_CHUNK "32768"
3535

36+
/* TCP framing */
37+
#define FLB_SYSLOG_FRAME_NEWLINE 0
38+
#define FLB_SYSLOG_FRAME_OCTET_COUNTING 1
39+
3640
struct syslog_conn;
3741

3842
/* Context / Config*/
@@ -67,6 +71,10 @@ struct flb_syslog {
6771
flb_sds_t raw_message_key;
6872
flb_sds_t source_address_key;
6973

74+
/* TCP framing */
75+
flb_sds_t frame_str;
76+
int frame_type;
77+
7078
int dgram_mode_flag;
7179
int collector_id;
7280
struct mk_event *collector_event;

plugins/in_syslog/syslog_conf.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ struct flb_syslog *syslog_conf_create(struct flb_input_instance *ins,
107107
ctx->mode = FLB_SYSLOG_UNIX_UDP;
108108
}
109109

110+
/* TCP Frame type (only applies to stream modes; default newline) */
111+
ctx->frame_type = FLB_SYSLOG_FRAME_NEWLINE;
112+
if (ctx->frame_str != NULL) {
113+
if (strcasecmp(ctx->frame_str, "octet_counting") == 0 ||
114+
strcasecmp(ctx->frame_str, "octet") == 0) {
115+
ctx->frame_type = FLB_SYSLOG_FRAME_OCTET_COUNTING;
116+
}
117+
else if (strcasecmp(ctx->frame_str, "newline") != 0) {
118+
flb_plg_warn(ins, "[in_syslog] unknown frame '%s', using 'newline'",
119+
ctx->frame_str);
120+
}
121+
}
122+
110123
/* Check if TCP mode was requested */
111124
if (ctx->mode == FLB_SYSLOG_TCP || ctx->mode == FLB_SYSLOG_UDP) {
112125
/* Listen interface (if not set, defaults to 0.0.0.0:5140) */

plugins/in_syslog/syslog_conn.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ struct syslog_conn *syslog_conn_add(struct flb_connection *connection,
178178
conn->ins = ctx->ins;
179179
conn->buf_len = 0;
180180
conn->buf_parsed = 0;
181+
conn->frame_expected_len = 0;
182+
conn->frame_have_len = 0;
181183

182184
/* Allocate read buffer */
183185
conn->buf_data = flb_malloc(ctx->buffer_chunk_size);

plugins/in_syslog/syslog_conn.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ struct syslog_conn {
3535
size_t buf_size; /* Buffer size */
3636
size_t buf_len; /* Buffer length */
3737
size_t buf_parsed; /* Parsed buffer (offset) */
38+
/* Octet-counting framing state */
39+
size_t frame_expected_len; /* remaining message bytes needed */
40+
int frame_have_len; /* 0 = need length, 1 = have length */
3841
struct flb_input_instance *ins; /* Parent plugin instance */
3942
struct flb_syslog *ctx; /* Plugin configuration context */
4043
struct flb_connection *connection;

plugins/in_syslog/syslog_prot.c

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,21 +218,52 @@ int syslog_prot_process(struct syslog_conn *conn)
218218

219219
flb_log_event_encoder_reset(ctx->log_encoder);
220220

221-
/* Always parse while some remaining bytes exists */
221+
/* Always parse while some remaining bytes exist */
222222
while (eof < end) {
223-
/* Lookup the ending byte */
224-
eof = p = conn->buf_data + conn->buf_parsed;
225-
while (*eof != '\n' && *eof != '\0' && eof < end) {
226-
eof++;
223+
if (ctx->frame_type == FLB_SYSLOG_FRAME_NEWLINE) {
224+
/* newline framing (current behavior) */
225+
eof = p = conn->buf_data + conn->buf_parsed;
226+
while (*eof != '\n' && *eof != '\0' && eof < end) {
227+
eof++;
228+
}
229+
/* Incomplete message */
230+
if (eof == end || (*eof != '\n' && *eof != '\0')) {
231+
break;
232+
}
233+
len = (eof - p);
227234
}
228-
229-
/* Incomplete message */
230-
if (eof == end || (*eof != '\n' && *eof != '\0')) {
231-
break;
235+
else {
236+
/* RFC 6587 octet-counting framing: <len> SP <msg> */
237+
p = conn->buf_data + conn->buf_parsed;
238+
239+
if (!conn->frame_have_len) {
240+
char *sp = p;
241+
size_t n = 0;
242+
while (sp < end && *sp >= '0' && *sp <= '9') {
243+
if (n > SIZE_MAX / 10) { n = SIZE_MAX; break; }
244+
n = n * 10 + (size_t)(*sp - '0');
245+
sp++;
246+
}
247+
if (sp == end) {
248+
break;
249+
}
250+
if (*sp != ' ') {
251+
flb_plg_warn(ctx->ins, "invalid octet-counting length");
252+
return -1;
253+
}
254+
conn->buf_parsed += (sp - p) + 1;
255+
conn->frame_expected_len = n;
256+
conn->frame_have_len = 1;
257+
p = conn->buf_data + conn->buf_parsed;
258+
end = conn->buf_data + conn->buf_len;
259+
}
260+
if ((size_t)(end - p) < conn->frame_expected_len) {
261+
break;
262+
}
263+
len = (int)conn->frame_expected_len;
232264
}
233265

234266
/* No data ? */
235-
len = (eof - p);
236267
if (len == 0) {
237268
consume_bytes(conn->buf_data, 1, conn->buf_len);
238269
conn->buf_len--;
@@ -266,7 +297,18 @@ int syslog_prot_process(struct syslog_conn *conn)
266297
flb_plg_debug(ctx->ins, "unparsed log message: %.*s", len, p);
267298
}
268299

269-
conn->buf_parsed += len + 1;
300+
if (ctx->frame_type == FLB_SYSLOG_FRAME_NEWLINE) {
301+
conn->buf_parsed += len + 1;
302+
}
303+
else {
304+
conn->buf_parsed += len;
305+
conn->frame_expected_len = 0;
306+
conn->frame_have_len = 0;
307+
if (conn->buf_parsed < conn->buf_len &&
308+
conn->buf_data[conn->buf_parsed] == '\n') {
309+
conn->buf_parsed += 1;
310+
}
311+
}
270312
end = conn->buf_data + conn->buf_len;
271313
eof = conn->buf_data + conn->buf_parsed;
272314
}

0 commit comments

Comments
 (0)