@@ -218,21 +218,55 @@ 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 ) {
244+ n = SIZE_MAX ;
245+ break ;
246+ }
247+ n = n * 10 + (size_t )(* sp - '0' );
248+ sp ++ ;
249+ }
250+ if (sp == end ) {
251+ break ;
252+ }
253+ if (* sp != ' ' ) {
254+ flb_plg_warn (ctx -> ins , "invalid octet-counting length" );
255+ return -1 ;
256+ }
257+ conn -> buf_parsed += (sp - p ) + 1 ;
258+ conn -> frame_expected_len = n ;
259+ conn -> frame_have_len = 1 ;
260+ p = conn -> buf_data + conn -> buf_parsed ;
261+ end = conn -> buf_data + conn -> buf_len ;
262+ }
263+ if ((size_t )(end - p ) < conn -> frame_expected_len ) {
264+ break ;
265+ }
266+ len = (int )conn -> frame_expected_len ;
232267 }
233268
234269 /* No data ? */
235- len = (eof - p );
236270 if (len == 0 ) {
237271 consume_bytes (conn -> buf_data , 1 , conn -> buf_len );
238272 conn -> buf_len -- ;
@@ -266,7 +300,18 @@ int syslog_prot_process(struct syslog_conn *conn)
266300 flb_plg_debug (ctx -> ins , "unparsed log message: %.*s" , len , p );
267301 }
268302
269- conn -> buf_parsed += len + 1 ;
303+ if (ctx -> frame_type == FLB_SYSLOG_FRAME_NEWLINE ) {
304+ conn -> buf_parsed += len + 1 ;
305+ }
306+ else {
307+ conn -> buf_parsed += len ;
308+ conn -> frame_expected_len = 0 ;
309+ conn -> frame_have_len = 0 ;
310+ if (conn -> buf_parsed < conn -> buf_len &&
311+ conn -> buf_data [conn -> buf_parsed ] == '\n' ) {
312+ conn -> buf_parsed += 1 ;
313+ }
314+ }
270315 end = conn -> buf_data + conn -> buf_len ;
271316 eof = conn -> buf_data + conn -> buf_parsed ;
272317 }
0 commit comments