99#include "fd-util.h"
1010#include "io-util.h"
1111#include "netlog-manager.h"
12+ #include "netlog-network.h"
13+ #include "netlog-protocol.h"
1214
1315#define RFC_5424_NILVALUE "-"
1416#define RFC_5424_PROTOCOL 1
@@ -46,7 +48,7 @@ static int sendmsg_loop(Manager *m, struct msghdr *mh) {
4648 return 0 ;
4749}
4850
49- static int network_send (Manager * m , struct iovec * iovec , unsigned n_iovec ) {
51+ int network_send (Manager * m , struct iovec * iovec , unsigned n_iovec ) {
5052 struct msghdr mh = {
5153 .msg_iov = iovec ,
5254 .msg_iovlen = n_iovec ,
@@ -67,226 +69,6 @@ static int network_send(Manager *m, struct iovec *iovec, unsigned n_iovec) {
6769
6870 return sendmsg_loop (m , & mh );
6971}
70-
71- static int protocol_send (Manager * m , struct iovec * iovec , unsigned n_iovec ) {
72- int r ;
73-
74- switch (m -> protocol ) {
75- case SYSLOG_TRANSMISSION_PROTOCOL_DTLS :
76- r = dtls_datagram_writev (m -> dtls , iovec , n_iovec );
77- if (r < 0 && r != - EAGAIN ) {
78- manager_connect (m );
79- return r ;
80- }
81- break ;
82- case SYSLOG_TRANSMISSION_PROTOCOL_TLS :
83- r = tls_stream_writev (m -> tls , iovec , n_iovec );
84- if (r < 0 && r != - EAGAIN ) {
85- manager_connect (m );
86- return r ;
87- }
88- break ;
89- default :
90- r = network_send (m , iovec , n_iovec );
91- if (r < 0 && r != - EAGAIN ) {
92- manager_connect (m );
93- return r ;
94- }
95- break ;
96- }
97-
98- return 0 ;
99- }
100-
101- /* rfc3339 timestamp format: yyyy-mm-ddthh:mm:ss[.frac]<+/->zz:zz */
102- static void format_rfc3339_timestamp (const struct timeval * tv , char * header_time , size_t header_size ) {
103- char gm_buf [sizeof ("+0530" ) + 1 ];
104- struct tm tm ;
105- time_t t ;
106- size_t written ;
107- int r ;
108-
109- assert (header_time );
110-
111- t = tv ? tv -> tv_sec : ((time_t ) (now (CLOCK_REALTIME ) / USEC_PER_SEC ));
112- localtime_r (& t , & tm );
113-
114- written = strftime (header_time , header_size , "%Y-%m-%dT%T" , & tm );
115- assert (written != 0 );
116- header_time += written ;
117- header_size -= written ;
118-
119- /* add fractional part */
120- if (tv ) {
121- r = snprintf (header_time , header_size , ".%06ld" , tv -> tv_usec );
122- assert (r > 0 && (size_t )r < header_size );
123- header_time += r ;
124- header_size -= r ;
125- }
126-
127- /* format the timezone according to RFC */
128- xstrftime (gm_buf , "%z" , & tm );
129- r = snprintf (header_time , header_size , "%.3s:%.2s " , gm_buf , gm_buf + 3 );
130- assert (r > 0 && (size_t )r < header_size );
131- }
132-
133- /* The Syslog Protocol RFC5424 format :
134- * <pri>version sp timestamp sp hostname sp app-name sp procid sp msgid sp [sd-id]s sp msg
135- */
136- static int format_rfc5424 (Manager * m ,
137- int severity ,
138- int facility ,
139- const char * identifier ,
140- const char * message ,
141- const char * hostname ,
142- const char * pid ,
143- const struct timeval * tv ,
144- const char * syslog_structured_data ,
145- const char * syslog_msgid ) {
146-
147- char header_time [FORMAT_TIMESTAMP_MAX ];
148- char header_priority [sizeof ("< >1 " )];
149- struct iovec iov [14 ];
150- uint8_t makepri ;
151- int n = 0 , r ;
152-
153- assert (m );
154- assert (message );
155-
156- makepri = (facility << 3 ) + severity ;
157-
158- /* First: priority field Second: Version '<pri>version' */
159- r = snprintf (header_priority , sizeof (header_priority ), "<%i>%i " , makepri , RFC_5424_PROTOCOL );
160- assert (r > 0 && (size_t )r < sizeof (header_priority ));
161- IOVEC_SET_STRING (iov [n ++ ], header_priority );
162-
163- /* Third: timestamp */
164- format_rfc3339_timestamp (tv , header_time , sizeof (header_time ));
165- IOVEC_SET_STRING (iov [n ++ ], header_time );
166-
167- /* Fourth: hostname */
168- if (hostname )
169- IOVEC_SET_STRING (iov [n ++ ], hostname );
170- else
171- IOVEC_SET_STRING (iov [n ++ ], RFC_5424_NILVALUE );
172-
173- IOVEC_SET_STRING (iov [n ++ ], " " );
174-
175- /* Fifth: identifier */
176- if (identifier )
177- IOVEC_SET_STRING (iov [n ++ ], identifier );
178- else
179- IOVEC_SET_STRING (iov [n ++ ], RFC_5424_NILVALUE );
180-
181- IOVEC_SET_STRING (iov [n ++ ], " " );
182-
183- /* Sixth: procid */
184- if (pid )
185- IOVEC_SET_STRING (iov [n ++ ], pid );
186- else
187- IOVEC_SET_STRING (iov [n ++ ], RFC_5424_NILVALUE );
188-
189- IOVEC_SET_STRING (iov [n ++ ], " " );
190-
191- /* Seventh: msgid */
192- if (syslog_msgid )
193- IOVEC_SET_STRING (iov [n ++ ], syslog_msgid );
194- else
195- IOVEC_SET_STRING (iov [n ++ ], RFC_5424_NILVALUE );
196-
197- IOVEC_SET_STRING (iov [n ++ ], " " );
198-
199- /* Eighth: [structured-data] */
200- if (m -> structured_data )
201- IOVEC_SET_STRING (iov [n ++ ], m -> structured_data );
202- else if (syslog_structured_data )
203- IOVEC_SET_STRING (iov [n ++ ], syslog_structured_data );
204- else
205- IOVEC_SET_STRING (iov [n ++ ], RFC_5424_NILVALUE );
206-
207- IOVEC_SET_STRING (iov [n ++ ], " " );
208-
209- /* Ninth: message */
210- IOVEC_SET_STRING (iov [n ++ ], message );
211-
212- /* Last Optional newline message separator, if not implicitly terminated by end of UDP frame
213- * De facto standard: separate messages by a newline
214- */
215- if (m -> protocol == SYSLOG_TRANSMISSION_PROTOCOL_TCP )
216- IOVEC_SET_STRING (iov [n ++ ], "\n" );
217-
218- return protocol_send (m , iov , n );
219- }
220-
221- static int format_rfc3339 (Manager * m ,
222- int severity ,
223- int facility ,
224- const char * identifier ,
225- const char * message ,
226- const char * hostname ,
227- const char * pid ,
228- const struct timeval * tv ) {
229-
230- char header_priority [sizeof ("< >1 " )];
231- char header_time [FORMAT_TIMESTAMP_MAX ];
232- struct iovec iov [14 ];
233- uint8_t makepri ;
234- int n = 0 , r ;
235-
236- assert (m );
237- assert (message );
238-
239- makepri = (facility << 3 ) + severity ;
240-
241- /* rfc3339
242- * <35>Oct 12 22:14:15 client_machine su: 'su root' failed for joe on /dev/pts/2
243- */
244-
245- /* First: priority field '<pri>' */
246- r = snprintf (header_priority , sizeof (header_priority ), "<%i>" , makepri );
247- assert (r > 0 && (size_t )r < sizeof (header_priority ));
248- IOVEC_SET_STRING (iov [n ++ ], header_priority );
249-
250- /* Third: timestamp */
251- format_rfc3339_timestamp (tv , header_time , sizeof (header_time ));
252- IOVEC_SET_STRING (iov [n ++ ], header_time );
253-
254- /* Fourth: hostname */
255- if (hostname )
256- IOVEC_SET_STRING (iov [n ++ ], hostname );
257- else
258- IOVEC_SET_STRING (iov [n ++ ], RFC_5424_NILVALUE );
259-
260- IOVEC_SET_STRING (iov [n ++ ], " " );
261-
262- /* Fifth: identifier */
263- if (identifier )
264- IOVEC_SET_STRING (iov [n ++ ], identifier );
265- else
266- IOVEC_SET_STRING (iov [n ++ ], RFC_5424_NILVALUE );
267-
268- IOVEC_SET_STRING (iov [n ++ ], "[" );
269-
270- /* Sixth: procid */
271- if (pid )
272- IOVEC_SET_STRING (iov [n ++ ], pid );
273- else
274- IOVEC_SET_STRING (iov [n ++ ], RFC_5424_NILVALUE );
275-
276- IOVEC_SET_STRING (iov [n ++ ], "]: " );
277-
278- /* Ninth: message */
279- IOVEC_SET_STRING (iov [n ++ ], message );
280-
281- /* Last Optional newline message separator, if not implicitly terminated by end of UDP frame
282- * De facto standard: separate messages by a newline
283- */
284- if (m -> protocol == SYSLOG_TRANSMISSION_PROTOCOL_TCP )
285- IOVEC_SET_STRING (iov [n ++ ], "\n" );
286-
287- return protocol_send (m , iov , n );
288- }
289-
29072int manager_push_to_network (Manager * m ,
29173 int severity ,
29274 int facility ,
0 commit comments