Debian Codegraph @

runtime/tcpsrv.h

raw
1 /* Definitions for tcpsrv class.
2 *
3 * Copyright 2008-2026 Adiscon GmbH.
4 *
5 * This file is part of rsyslog.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 * -or-
13 * see COPYING.ASL20 in the source distribution
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21 #ifndef INCLUDED_TCPSRV_H
22 #define INCLUDED_TCPSRV_H
23
24 #if defined(ENABLE_IMTCP_EPOLL) && defined(HAVE_SYS_EPOLL_H)
25 #include <sys/epoll.h>
26 #endif
27
28 #include "obj.h"
29 #include "prop.h"
30 #include "net.h"
31 #include "tcps_sess.h"
32 #include "statsobj.h"
33 #include "regexp.h"
34
35 /* support for framing anomalies */
36 typedef enum ETCPsyslogFramingAnomaly {
37 frame_normal = 0,
38 frame_NetScreen = 1,
39 frame_CiscoIOS = 2
40 } eTCPsyslogFramingAnomaly;
41
42
43 /**
44 * Config parameters for TCP listeners.
45 *
46 * Ownership: the caller allocates and populates this struct, then hands it to
47 * tcpsrv via `configureTCPListen()`. tcpsrv becomes the owner (including any
48 * dynamically allocated fields) and will free it on error or when the listener
49 * is torn down.
50 */
51 struct tcpLstnParams_s {
52 const uchar *pszPort; /**< the ports the listener shall listen on */
53 const uchar *pszAddr; /**< the addrs the listener shall listen on */
54 sbool bSuppOctetFram; /**< do we support octect-counted framing? (if no->legay only!)*/
55 sbool bSPFramingFix; /**< support work-around for broken Cisco ASA framing? */
56 sbool bPreserveCase; /**< preserve case in fromhost */
57 const uchar *pszLstnPortFileName; /**< File in which the dynamic port is written */
58 char *pszNetworkNamespace; /**< network namespace to use */
59 uchar *pszStrmDrvrName; /**< stream driver to use */
60 uchar *pszInputName; /**< value to be used as input name */
61 prop_t *pInputName;
62 ruleset_t *pRuleset; /**< associated ruleset */
63 uchar dfltTZ[8]; /**< default TZ if none in timestamp; '\0' =No Default */
64 sbool bMultiLine; /**< support multi-line messages */
65 uchar *pszStartRegex; /**< regex that indicates start of frame */
66 uchar *pszRatelimitName; /**< name of rate limit configuration */
67 };
68
69 /* list of tcp listen ports */
70 struct tcpLstnPortList_s {
71 tcpLstnParams_t *cnf_params; /**< listener config parameters */
72 tcpsrv_t *pSrv; /**< pointer to higher-level server instance */
73 statsobj_t *stats; /**< associated stats object */
74 ratelimit_t *ratelimiter;
75 STATSCOUNTER_DEF(ctrSubmit, mutCtrSubmit)
76 #ifdef FEATURE_REGEXP
77 regex_t start_preg;
78 sbool bHasStartRegex;
79 #endif
80 tcpLstnPortList_t *pNext; /**< next port or NULL */
81 };
82
83
84 typedef struct tcpsrvWrkrData_s {
85 statsobj_t *stats;
86 STATSCOUNTER_DEF(ctrRuns, mutCtrRuns);
87 STATSCOUNTER_DEF(ctrRead, mutCtrRead);
88 STATSCOUNTER_DEF(ctrEmptyRead, mutCtrEmptyRead);
89 STATSCOUNTER_DEF(ctrStarvation, mutCtrStarvation);
90 STATSCOUNTER_DEF(ctrAccept, mutCtrAccept);
91 } tcpsrvWrkrData_t;
92
93 typedef struct workQueue_s {
94 tcpsrv_io_descr_t *head;
95 tcpsrv_io_descr_t *tail;
96 pthread_mutex_t mut;
97 pthread_cond_t workRdy;
98 unsigned numWrkr; /* how many workers to spawn */
99 pthread_t *wrkr_tids; /* array of thread IDs */
100 tcpsrvWrkrData_t *wrkr_data;
101 } workQueue_t;
102
103 /**
104 * The following structure is a descriptor for tcpsrv i/o. It is
105 * primarily used together with epoll at the moment.
106 */
107 struct tcpsrv_io_descr_s {
108 int id; /* index into listener or session table, depending on ptrType */
109 int sock; /* socket descriptor we need to "monitor" */
110 unsigned ioDirection;
111 enum { NSD_PTR_TYPE_LSTN, NSD_PTR_TYPE_SESS } ptrType;
112 union {
113 tcps_sess_t *pSess;
114 netstrm_t **ppLstn; /**< accept listener's netstream */
115 } ptr;
116 int isInError; /* boolean, if set, subsystem indicates we need to close because we had an
117 * unrecoverable error at the network layer. */
118 tcpsrv_t *pSrv; /* our server object */
119 tcpsrv_io_descr_t *next; /* for use in workQueue_t */
120 #if defined(ENABLE_IMTCP_EPOLL)
121 struct epoll_event event; /* to re-enable EPOLLONESHOT */
122 #endif
123 DEF_ATOMIC_HELPER_MUT(mut_isInError);
124 };
125
126 #define TCPSRV_NO_ADDTL_DELIMITER -1 /* specifies that no additional delimiter is to be used in TCP framing */
127
128 /* the tcpsrv object */
129 struct tcpsrv_s {
130 BEGINobjInstance
131 ; /**< Data to implement generic object - MUST be the first data element! */
132 int bUseKeepAlive; /**< use socket layer KEEPALIVE handling? */
133 int iKeepAliveIntvl; /**< socket layer KEEPALIVE interval */
134 int iKeepAliveProbes; /**< socket layer KEEPALIVE probes */
135 int iKeepAliveTime; /**< socket layer KEEPALIVE timeout */
136 netstrms_t *pNS; /**< pointer to network stream subsystem */
137 int iDrvrMode; /**< mode of the stream driver to use */
138 int DrvrChkExtendedKeyUsage; /**< if true, verify extended key usage in certs */
139 int DrvrPrioritizeSan; /**< if true, perform stricter checking of names in certs */
140 int DrvrTlsVerifyDepth; /**< Verify Depth for certificate chains */
141 int DrvrTlsRevocationCheck; /**< Enable TLS revocation checking (OCSP/CRL) */
142 uchar *gnutlsPriorityString; /**< priority string for gnutls */
143 uchar *pszLstnPortFileName; /**< File in which the dynamic port is written */
144 uchar *pszDrvrAuthMode; /**< auth mode of the stream driver to use */
145 uchar *pszDrvrPermitExpiredCerts; /**< current driver setting for handlign expired certs */
146 uchar *pszDrvrCAFile;
147 uchar *pszDrvrCRLFile;
148 uchar *pszDrvrKeyFile;
149 uchar *pszDrvrCertFile;
150 uchar *pszDrvrName; /**< name of stream driver to use */
151 uchar *pszInputName; /**< value to be used as input name */
152 uchar *pszOrigin; /**< module to be used as "origin" (e.g. for pstats) */
153 ruleset_t *pRuleset; /**< ruleset to bind to */
154 permittedPeers_t *pPermPeers; /**< driver's permitted peers */
155 sbool bEmitMsgOnClose; /**< emit an informational message when the remote peer closes connection */
156 sbool bEmitMsgOnOpen;
157 sbool bUseFlowControl; /**< use flow control (make light delayable) */
158 sbool bSPFramingFix; /**< support work-around for broken Cisco ASA framing? */
159 int iLstnCurr; /**< max nbr of listeners currently supported */
160 netstrm_t **ppLstn; /**< our netstream listeners */
161 /* We could use conditional compilation, but that causes more complexity and is (proofen causing errors) */
162 union {
163 struct {
164 int efd;
165 } epoll;
166 struct {
167 uint32_t maxfds;
168 uint32_t currfds;
169 struct pollfd *fds;
170 } poll;
171 } evtdata;
172 tcpLstnPortList_t **ppLstnPort; /**< pointer to relevant listen port description */
173 tcpsrv_io_descr_t **ppioDescrPtr; /**< pointer to i/o descriptor object */
174 int iLstnMax; /**< max number of listeners supported */
175 int iSessMax; /**< max number of sessions supported */
176 uchar dfltTZ[8]; /**< default TZ if none in timestamp; '\0' =No Default */
177 tcpLstnPortList_t *pLstnPorts; /**< head pointer for listen ports */
178
179 int addtlFrameDelim; /**< additional frame delimiter for plain TCP syslog
180 framing (e.g. to handle NetScreen) */
181 int maxFrameSize; /**< max frame size for octet counted*/
182 int bDisableLFDelim; /**< if 1, standard LF frame delimiter is disabled (*very dangerous*) */
183 int discardTruncatedMsg; /**< discard msg part that has been truncated*/
184 sbool bPreserveCase; /**< preserve case in fromhost */
185 int iSynBacklog;
186 unsigned int ratelimitInterval;
187 unsigned int ratelimitBurst;
188 tcps_sess_t **pSessions; /**< array of all of our sessions */
189 unsigned int starvationMaxReads;
190 void *pUsr; /**< a user-settable pointer (provides extensibility for "derived classes")*/
191 /* callbacks */
192 int (*pIsPermittedHost)(struct sockaddr *addr, char *fromHostFQDN, void *pUsrSrv, void *pUsrSess);
193 rsRetVal (*pRcvData)(tcps_sess_t *, char *, size_t, ssize_t *, int *, unsigned *);
194 rsRetVal (*OpenLstnSocks)(struct tcpsrv_s *);
195 rsRetVal (*pOnListenDeinit)(void *);
196 rsRetVal (*OnDestruct)(void *);
197 rsRetVal (*pOnRegularClose)(tcps_sess_t *pSess);
198 rsRetVal (*pOnErrClose)(tcps_sess_t *pSess);
199 /* session specific callbacks */
200 rsRetVal (*pOnSessAccept)(tcpsrv_t *, tcps_sess_t *, char *connInfo);
201 #define TCPSRV_CONNINFO_SIZE (2 * (INET_ADDRSTRLEN + 20))
202 rsRetVal (*OnSessConstructFinalize)(void *);
203 rsRetVal (*pOnSessDestruct)(void *);
204 rsRetVal (*OnMsgReceive)(tcps_sess_t *, uchar *pszMsg, int iLenMsg); /* submit message callback */
205 /* work queue */
206 workQueue_t workQueue;
207 int currWrkrs;
208 };
209
210
211 /**
212 */
213 struct tcpsrv_workset_s {
214 int idx; /**< index into session table (or -1 if listener) */
215 void *pUsr;
216 };
217
218
219 /* interfaces */
220 BEGINinterface(tcpsrv) /* name must also be changed in ENDinterface macro! */
221 INTERFACEObjDebugPrint(tcpsrv);
222 rsRetVal (*Construct)(tcpsrv_t **ppThis);
223 rsRetVal (*ConstructFinalize)(tcpsrv_t __attribute__((unused)) * pThis);
224 rsRetVal (*Destruct)(tcpsrv_t **ppThis);
225 /**
226 * Configure a TCP listener using the provided configuration parameters.
227 *
228 * Ownership of `cnf_params` is always transferred to tcpsrv. On success,
229 * tcpsrv retains the configuration for the listener. On failure (including
230 * invalid port), tcpsrv frees `cnf_params` and its owned fields.
231 */
232 rsRetVal (*ATTR_NONNULL(1, 2) configureTCPListen)(tcpsrv_t *, tcpLstnParams_t *const cnf_params);
233 rsRetVal (*create_tcp_socket)(tcpsrv_t *pThis);
234 rsRetVal (*Run)(tcpsrv_t *pThis);
235 /* set methods */
236 rsRetVal (*SetAddtlFrameDelim)(tcpsrv_t *, int);
237 rsRetVal (*SetMaxFrameSize)(tcpsrv_t *, int);
238 /**
239 * Set the input name for a listener configuration.
240 *
241 * Ownership: the name is duplicated into `cnf_params` and managed by
242 * tcpsrv along with the rest of `cnf_params` after ownership transfer.
243 */
244 rsRetVal (*SetInputName)(tcpsrv_t *const pThis, tcpLstnParams_t *const cnf_params, const uchar *const name);
245 rsRetVal (*SetUsrP)(tcpsrv_t *, void *);
246 rsRetVal (*SetCBIsPermittedHost)(tcpsrv_t *, int (*)(struct sockaddr *addr, char *, void *, void *));
247 rsRetVal (*SetCBOpenLstnSocks)(tcpsrv_t *, rsRetVal (*)(tcpsrv_t *));
248 rsRetVal (*SetCBRcvData)(tcpsrv_t *pThis,
249 rsRetVal (*pRcvData)(tcps_sess_t *, char *, size_t, ssize_t *, int *, unsigned *));
250 rsRetVal (*SetCBOnListenDeinit)(tcpsrv_t *, rsRetVal (*)(void *));
251 rsRetVal (*SetCBOnDestruct)(tcpsrv_t *, rsRetVal (*)(void *));
252 rsRetVal (*SetCBOnRegularClose)(tcpsrv_t *, rsRetVal (*)(tcps_sess_t *));
253 rsRetVal (*SetCBOnErrClose)(tcpsrv_t *, rsRetVal (*)(tcps_sess_t *));
254 rsRetVal (*SetDrvrMode)(tcpsrv_t *pThis, int iMode);
255 rsRetVal (*SetDrvrAuthMode)(tcpsrv_t *pThis, uchar *pszMode);
256 rsRetVal (*SetDrvrPermitExpiredCerts)(tcpsrv_t *pThis, uchar *pszMode);
257 rsRetVal (*SetDrvrPermPeers)(tcpsrv_t *pThis, permittedPeers_t *);
258 /* session specifics */
259 rsRetVal (*SetCBOnSessAccept)(tcpsrv_t *, rsRetVal (*)(tcpsrv_t *, tcps_sess_t *, char *));
260 rsRetVal (*SetCBOnSessDestruct)(tcpsrv_t *, rsRetVal (*)(void *));
261 rsRetVal (*SetCBOnSessConstructFinalize)(tcpsrv_t *, rsRetVal (*)(void *));
262 /* added v5 */
263 rsRetVal (*SetSessMax)(tcpsrv_t *pThis, int iMaxSess); /* 2009-04-09 */
264 /* added v6 */
265 rsRetVal (*SetOnMsgReceive)(tcpsrv_t *pThis,
266 rsRetVal (*OnMsgReceive)(tcps_sess_t *, uchar *, int)); /* 2009-05-24 */
267 rsRetVal (*SetRuleset)(tcpsrv_t *pThis, ruleset_t *); /* 2009-06-12 */
268 /* added v7 (accidentally named v8!) */
269 rsRetVal (*SetLstnMax)(tcpsrv_t *pThis, int iMaxLstn); /* 2009-08-17 */
270 rsRetVal (*SetNotificationOnRemoteClose)(tcpsrv_t *pThis, int bNewVal); /* 2009-10-01 */
271 rsRetVal (*SetNotificationOnRemoteOpen)(tcpsrv_t *pThis, int bNewVal); /* 2022-08-23 */
272 /* added v9 -- rgerhards, 2010-03-01 */
273 rsRetVal (*SetbDisableLFDelim)(tcpsrv_t *, int);
274 /* added v10 -- rgerhards, 2011-04-01 */
275 rsRetVal (*SetDiscardTruncatedMsg)(tcpsrv_t *, int);
276 rsRetVal (*SetUseFlowControl)(tcpsrv_t *, int);
277 /* added v11 -- rgerhards, 2011-05-09 */
278 rsRetVal (*SetKeepAlive)(tcpsrv_t *, int);
279 /* added v13 -- rgerhards, 2012-10-15 */
280 rsRetVal (*SetLinuxLikeRatelimiters)(tcpsrv_t *pThis, unsigned int interval, unsigned int burst);
281 /* added v14 -- rgerhards, 2013-07-28 */
282 rsRetVal (*SetDfltTZ)(tcpsrv_t *pThis, uchar *dfltTZ);
283 /* added v15 -- rgerhards, 2013-09-17 */
284 rsRetVal (*SetDrvrName)(tcpsrv_t *pThis, uchar *pszName);
285 /* added v16 -- rgerhards, 2014-09-08 */
286 rsRetVal (*SetOrigin)(tcpsrv_t *, uchar *);
287 /* added v17 */
288 rsRetVal (*SetKeepAliveIntvl)(tcpsrv_t *, int);
289 rsRetVal (*SetKeepAliveProbes)(tcpsrv_t *, int);
290 rsRetVal (*SetKeepAliveTime)(tcpsrv_t *, int);
291 /* added v18 */
292 rsRetVal (*SetbSPFramingFix)(tcpsrv_t *, sbool);
293 /* added v19 -- PascalWithopf, 2017-08-08 */
294 rsRetVal (*SetGnutlsPriorityString)(tcpsrv_t *, uchar *);
295 /* added v21 -- Preserve case in fromhost, 2018-08-16 */
296 rsRetVal (*SetPreserveCase)(tcpsrv_t *pThis, int bPreserveCase);
297 /* added v23 -- Options for stricter driver behavior, 2019-08-16 */
298 rsRetVal (*SetDrvrCheckExtendedKeyUsage)(tcpsrv_t *pThis, int ChkExtendedKeyUsage);
299 rsRetVal (*SetDrvrPrioritizeSAN)(tcpsrv_t *pThis, int prioritizeSan);
300 /* added v24 -- Options for TLS verify depth driver behavior, 2019-12-20 */
301 rsRetVal (*SetDrvrTlsVerifyDepth)(tcpsrv_t *pThis, int verifyDepth);
302 rsRetVal (*SetDrvrTlsRevocationCheck)(tcpsrv_t *pThis, int enabled);
303 /* added v25 -- Options for TLS certificates, 2021-07-19 */
304 rsRetVal (*SetDrvrCAFile)(tcpsrv_t *pThis, uchar *pszMode);
305 rsRetVal (*SetDrvrKeyFile)(tcpsrv_t *pThis, uchar *pszMode);
306 rsRetVal (*SetDrvrCertFile)(tcpsrv_t *pThis, uchar *pszMode);
307 /* added v26 -- Options for TLS CRL file */
308 rsRetVal (*SetDrvrCRLFile)(tcpsrv_t *pThis, uchar *pszMode);
309 /* added v27 -- sync backlog for listen() */
310 rsRetVal (*SetSynBacklog)(tcpsrv_t *pThis, int);
311 /* added v28 */
312 rsRetVal (*SetNumWrkr)(tcpsrv_t *pThis, int);
313 rsRetVal (*SetStarvationMaxReads)(tcpsrv_t *pThis, unsigned int);
314 /* added v29 */
315 /*
316 * @brief Set the Network Namespace into the listener parameters
317 * @param pThis The associated TCP Server instance
318 * @param cnf_params The listener parameters to configure
319 * @param networkNamespace The namespace parameter to set into the
320 * listener configuration parameters
321 * @return RS_RET_OK on success, otherwise a failure code.
322 * @details For platforms that do not support network namespaces,
323 * this function should fail for any non-null and non-empty
324 * namespace passed. Note that the empty string is treated
325 * the same as a NULL, i.e. you are not allowed to actually
326 * use a network namespace with the empty string. So both
327 * a NULL and an empty string "" both mean to use the
328 * original startup network namespace.
329 */
330 rsRetVal (*SetNetworkNamespace)(tcpsrv_t *pThis, tcpLstnParams_t *const cnf_params,
331 const char *const networkNamespace);
332
333 ENDinterface(tcpsrv)
334 #define tcpsrvCURR_IF_VERSION 29 /* increment whenever you change the interface structure! */
335 /* change for v4:
336 * - SetAddtlFrameDelim() added -- rgerhards, 2008-12-10
337 * - SetInputName() added -- rgerhards, 2008-12-10
338 * change for v5 and up: see above
339 * for v12: param bSuppOctetFram added to configureTCPListen
340 * for v20: add oserr to setCBRcvData signature -- rgerhards, 2017-09-04
341 */
342
343
344 /* prototypes */
345 PROTOTYPEObjFull(tcpsrv);
346 PROTOTYPEObjDebugPrint(tcpsrv);
347
348 /* the name of our library binary */
349 #define LM_TCPSRV_FILENAME "lmtcpsrv"
350
351 #endif /* #ifndef INCLUDED_TCPSRV_H */