summaryrefslogtreecommitdiffstats
path: root/module-template.h
blob: 55b8f821090aee5244d3cf2648f6c63faa9e8ebf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* module-template.h
 * This header contains macros that can be used to implement the 
 * plumbing of modules. 
 *
 * File begun on 2007-07-25 by RGerhards
 *
 * Copyright 2007 Rainer Gerhards and Adiscon GmbH.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * A copy of the GPL can be found in the file "COPYING" in this distribution.
 */
#ifndef	MODULE_TEMPLATE_H_INCLUDED
#define	MODULE_TEMPLATE_H_INCLUDED 1

/* to following macros are used to generate function headers and standard
 * functionality. It works as follows (described on the sample case of
 * createInstance()):
 *
 * BEGINcreateInstance
 * ... custom variable definitions (on stack) ... (if any)
 * CODESTARTcreateInstance
 * ... custom code ... (if any)
 * ENDcreateInstance
 */

/* createInstance()
 */
#define BEGINcreateInstance \
static rsRetVal createInstance(instanceData **ppData)\
	{\
	rsRetVal iRet = RS_RET_OK; /* store error code here */\
	instanceData *pData; /* use this to point to data elements */

#define CODESTARTcreateInstance \
	if((pData = calloc(1, sizeof(instanceData))) == NULL) {\
		*ppData = NULL;\
		return RS_RET_OUT_OF_MEMORY;\
	}

#define ENDcreateInstance \
	*ppData = pData;\
	return iRet;\
}

/* freeInstance()
 */
#define BEGINfreeInstance \
static rsRetVal freeInstance(selector_t *f, void* pModData)\
{\
	rsRetVal iRet = RS_RET_OK;\
	instanceData *pData;

#define CODESTARTfreeInstance \
	assert(f != NULL);\
	pData = (instanceData*) pModData;

#define ENDfreeInstance \
	if(pData != NULL)\
		free(pData); /* we need to free this in any case */\
	return iRet;\
}

/* isCompatibleWithFeature()
 */
#define BEGINisCompatibleWithFeature \
static rsRetVal isCompatibleWithFeature(syslogFeature __attribute__((unused)) eFeat)\
{\
	rsRetVal iRet = RS_RET_INCOMPATIBLE;

#define CODESTARTisCompatibleWithFeature

#define ENDisCompatibleWithFeature \
	return iRet;\
}

/* doAction()
 */
#define BEGINdoAction \
static rsRetVal doAction(selector_t *f, instanceData __attribute__((unused)) *pData)\
{\
	rsRetVal iRet = RS_RET_OK;

#define CODESTARTdoAction \
	assert(f != NULL);

#define ENDdoAction \
	return iRet;\
}


/* dbgPrintInstInfo()
 * Extra comments:
 * Print debug information about this instance.
 */
#define BEGINdbgPrintInstInfo \
static rsRetVal dbgPrintInstInfo(selector_t *f, void *pModData)\
{\
	rsRetVal iRet = RS_RET_OK;\
	instanceData *pData = NULL;

#define CODESTARTdbgPrintInstInfo \
	assert(f != NULL);\
	pData = (instanceData*) pModData;

#define ENDdbgPrintInstInfo \
	return iRet;\
}


/* onSelectReadyWrite()
 * Extra comments:
 * This is called when select() returned with a writable file descriptor
 * for this module. The fd was most probably obtained by getWriteFDForSelect()
 * before.
 */
#define BEGINonSelectReadyWrite \
static rsRetVal onSelectReadyWrite(selector_t *f, void *pModData)\
{\
	rsRetVal iRet = RS_RET_NONE;\
	instanceData *pData = NULL;

#define CODESTARTonSelectReadyWrite \
	assert(f != NULL);\
	pData = (instanceData*) pModData;

#define ENDonSelectReadyWrite \
	return iRet;\
}


/* getWriteFDForSelect()
 * Extra comments:
 * Gets writefd for select call. Must only be returned when the selector must
 * be written to. If the module has no such fds, it must return RS_RET_NONE.
 * In this case, the default implementation is sufficient.
 * This interface will probably go away over time, but we need it now to
 * continue modularization.
 */
#define BEGINgetWriteFDForSelect \
static rsRetVal getWriteFDForSelect(selector_t *f, void *pModData, short *fd)\
{\
	rsRetVal iRet = RS_RET_NONE;\
	instanceData *pData = NULL;

#define CODESTARTgetWriteFDForSelect \
	assert(f != NULL);\
	assert(fd != NULL);\
	pData = (instanceData*) pModData;

#define ENDgetWriteFDForSelect \
	return iRet;\
}


/* parseSelectorAct()
 * Extra comments:
 * try to process a selector action line. Checks if the action
 * applies to this module and, if so, processed it. If not, it
 * is left untouched. The driver will then call another module
 */
#define BEGINparseSelectorAct \
static rsRetVal parseSelectorAct(uchar **pp, selector_t *f, void **ppModData)\
{\
	rsRetVal iRet = RS_RET_OK;\
	uchar *p;\
	instanceData *pData = NULL;

#define CODESTARTparseSelectorAct \
	assert(pp != NULL);\
	assert(ppModData != NULL);\
	assert(f != NULL);\
	p = *pp;

#define ENDparseSelectorAct \
	if(iRet == RS_RET_OK) {\
		*ppModData = pData;\
		*pp = p;\
	}\
	return iRet;\
}


/* queryEtryPt()
 */
#define BEGINqueryEtryPt \
static rsRetVal queryEtryPt(uchar *name, rsRetVal (**pEtryPoint)())\
{\
	rsRetVal iRet = RS_RET_OK;

#define CODESTARTqueryEtryPt \
	if((name == NULL) || (pEtryPoint == NULL))\
		return RS_RET_PARAM_ERROR;\
	*pEtryPoint = NULL;

#define ENDqueryEtryPt \
	if(iRet == RS_RET_OK)\
		iRet = (*pEtryPoint == NULL) ? RS_RET_NOT_FOUND : RS_RET_OK;\
	return iRet;\
}

/* the following defintion is the standard block for queryEtryPt for output
 * modules. This can be used if no specific handling (e.g. to cover version
 * differences) is needed.
 */
#define CODEqueryEtryPt_STD_OMOD_QUERIES\
	if(!strcmp((char*) name, "doAction")) {\
		*pEtryPoint = doAction;\
	} else if(!strcmp((char*) name, "parseSelectorAct")) {\
		*pEtryPoint = parseSelectorAct;\
	} else if(!strcmp((char*) name, "isCompatibleWithFeature")) {\
		*pEtryPoint = isCompatibleWithFeature;\
	} else if(!strcmp((char*) name, "dbgPrintInstInfo")) {\
		*pEtryPoint = dbgPrintInstInfo;\
	} else if(!strcmp((char*) name, "freeInstance")) {\
		*pEtryPoint = freeInstance;\
	} else if(!strcmp((char*) name, "getWriteFDForSelect")) {\
		*pEtryPoint = getWriteFDForSelect;\
	} else if(!strcmp((char*) name, "onSelectReadyWrite")) {\
		*pEtryPoint = onSelectReadyWrite;\
	}

/* modInit()
 * This has an extra parameter, which is the specific name of the modInit
 * function. That is needed for built-in modules, which must have unique
 * names in order to link statically.
 *
 * Extra Comments:
 * initialize the module
 *
 * Later, much more must be done. So far, we only return a pointer
 * to the queryEtryPt() function
 * TODO: do interface version checking & handshaking
 * iIfVersRequeted is the version of the interface specification that the
 * caller would like to see being used. ipIFVersProvided is what we
 * decide to provide.
 */
#define BEGINmodInit(uniqName) \
rsRetVal modInit##uniqName(int iIFVersRequested __attribute__((unused)), int *ipIFVersProvided, rsRetVal (**pQueryEtryPt)())\
{\
	rsRetVal iRet = RS_RET_OK;

#define CODESTARTmodInit \
	if((pQueryEtryPt == NULL) || (ipIFVersProvided == NULL))\
		return RS_RET_PARAM_ERROR;

#define ENDmodInit \
	*pQueryEtryPt = queryEtryPt;\
	return iRet;\
}

/*
 */
#define BEGIN \

#define CODESTART \

#define END \


#endif /* #ifndef MODULE_TEMPLATE_H_INCLUDED */
/*
 * vi:set ai:
 */