@@ -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