1 /*! \file srAPI-lstn.c
2 * \brief Implementation of the listener part of the API
3 *
4 * \author Rainer Gerhards <rgerhards@adiscon.com>
5 * \date 2003-08-26
6 * begun coding on initial version.
7 *
8 * Copyright 2002-2014
9 * Rainer Gerhards and Adiscon GmbH. All Rights Reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are
13 * met:
14 *
15 * * Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 *
18 * * Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in
20 * the documentation and/or other materials provided with the
21 * distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
27 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #include <assert.h>
37 #include "settings.h"
38 #include "liblogging.h"
39 #include "beepsession.h"
40 #include "beepprofile.h"
41 #include "beeplisten.h"
42 #include "lstnprof-3195raw.h"
43 #include "lstnprof-3195cooked.h"
44 #include "srAPI.h"
45 #include "syslogmessage.h"
46 #include "namevaluetree.h"
47
48 /* ################################################################# *
49 * private members *
50 * ################################################################# */
51
52
53 /* ################################################################# *
54 * public members *
55 * ################################################################# */
56
srAPISetMsgRcvCallback(srAPIObj * pThis,void (* NewHandler)(srAPIObj *,srSLMGObj *))57 srRetVal srAPISetMsgRcvCallback(srAPIObj* pThis, void(*NewHandler)(srAPIObj*, srSLMGObj*))
58 {
59 if((pThis == NULL) || (pThis->OID != OIDsrAPI))
60 return SR_RET_INVALID_HANDLE;
61
62 pThis->OnSyslogMessageRcvd = NewHandler;
63
64 return SR_RET_OK;
65 }
66
67
srAPISetupListener(srAPIObj * pThis,void (* NewHandler)(srAPIObj *,srSLMGObj *))68 srRetVal srAPISetupListener(srAPIObj* pThis, void(*NewHandler)(srAPIObj*, srSLMGObj*))
69 {
70 srRetVal iRet;
71 sbProfObj *pProf;
72
73 if((pThis == NULL) || (pThis->OID != OIDsrAPI))
74 return SR_RET_INVALID_HANDLE;
75
76 if(pThis->pLstn != NULL)
77 return SR_RET_ALREADY_LISTENING;
78
79 if((pThis->pLstn = sbLstnConstruct()) == NULL)
80 return SR_RET_OUT_OF_MEMORY;
81
82 /* now tell the lib which listeners to start */
83
84 # if FEATURE_UDP == 1
85 /* UDP Listener */
86 pThis->pLstn->bLstnUDP = pThis->bListenUDP;
87 pThis->pLstn->uUDPLstnPort = pThis->iUDPListenPort;
88 # endif
89
90 # if FEATURE_UNIX_DOMAIN_SOCKETS == 1
91 /* Unix Domain Sockets listener */
92 pThis->pLstn->bLstnUXDOMSOCK = pThis->bListenUXDOMSOCK;
93 if(pThis->szNameUXDOMSOCK != NULL)
94 {
95 if((pThis->pLstn->pSockName = sbNVTEUtilStrDup(pThis->szNameUXDOMSOCK)) == NULL)
96 return SR_RET_OUT_OF_MEMORY;
97 }
98 # endif
99
100 /* please note: as of now, the BEEP listener will always be
101 * started.
102 */
103 pThis->pLstn->uListenPort = pThis->iBEEPListenPort;
104
105 /* now begin initializing the listeners */
106
107 if((iRet = sbLstnInit(pThis->pLstn)) != SR_RET_OK)
108 return iRet;
109
110 pThis->pLstn->pAPI = pThis;
111
112 if(pThis->bListenBEEP == TRUE)
113 {
114 /* set up the rfc 3195/raw listener */
115 if((iRet = sbProfConstruct(&pProf, "http://xml.resource.org/profiles/syslog/RAW")) != SR_RET_OK)
116 {
117 sbLstnDestroy(pThis->pLstn);
118 return iRet;
119 }
120
121 if((iRet = sbProfSetAPIObj(pProf, pThis)) != SR_RET_OK)
122 {
123 sbLstnDestroy(pThis->pLstn);
124 sbProfDestroy(pProf);
125 return iRet;
126 }
127
128 if((iRet = srAPISetMsgRcvCallback(pThis, NewHandler)) != SR_RET_OK)
129 {
130 sbLstnDestroy(pThis->pLstn);
131 sbProfDestroy(pProf);
132 return iRet;
133 }
134
135 if((iRet = sbProfSetEventHandler(pProf, sbPROFEVENT_ONCHANCREAT, (void*) psrrOnChanCreate)) != SR_RET_OK)
136 {
137 sbLstnDestroy(pThis->pLstn);
138 sbProfDestroy(pProf);
139 return iRet;
140 }
141
142 if((iRet = sbProfSetEventHandler(pProf, sbPROFEVENT_ONMESGRECV, (void*) psrrOnMesgRecv)) != SR_RET_OK)
143 {
144 sbLstnDestroy(pThis->pLstn);
145 sbProfDestroy(pProf);
146 return iRet;
147 }
148
149 if((iRet = sbLstnAddProfile(pThis->pLstn, pProf)) != SR_RET_OK)
150 {
151 sbLstnDestroy(pThis->pLstn);
152 sbProfDestroy(pProf);
153 return iRet;
154 }
155
156 /* set up the rfc 3195/COOKED listener */
157 if((iRet = sbProfConstruct(&pProf, "http://xml.resource.org/profiles/syslog/COOKED")) != SR_RET_OK)
158 {
159 sbLstnDestroy(pThis->pLstn);
160 return iRet;
161 }
162
163 if((iRet = sbProfSetAPIObj(pProf, pThis)) != SR_RET_OK)
164 {
165 sbLstnDestroy(pThis->pLstn);
166 sbProfDestroy(pProf);
167 return iRet;
168 }
169
170 if((iRet = srAPISetMsgRcvCallback(pThis, NewHandler)) != SR_RET_OK)
171 {
172 sbLstnDestroy(pThis->pLstn);
173 sbProfDestroy(pProf);
174 return iRet;
175 }
176
177 if((iRet = sbProfSetEventHandler(pProf, sbPROFEVENT_ONCHANCREAT, (void*) psrcOnChanCreate)) != SR_RET_OK)
178 {
179 sbLstnDestroy(pThis->pLstn);
180 sbProfDestroy(pProf);
181 return iRet;
182 }
183
184 if((iRet = sbProfSetEventHandler(pProf, sbPROFEVENT_ONMESGRECV, (void*) psrcOnMesgRecv)) != SR_RET_OK)
185 {
186 sbLstnDestroy(pThis->pLstn);
187 sbProfDestroy(pProf);
188 return iRet;
189 }
190
191 if((iRet = sbLstnAddProfile(pThis->pLstn, pProf)) != SR_RET_OK)
192 {
193 sbLstnDestroy(pThis->pLstn);
194 sbProfDestroy(pProf);
195 return iRet;
196 }
197 } /* End setup of BEEP listener (only executed if BEEP listener is enabled) */
198
199 return SR_RET_OK;
200 }
201
202
srAPIRunListener(srAPIObj * pThis)203 srRetVal srAPIRunListener(srAPIObj *pThis)
204 {
205 if((pThis == NULL) || (pThis->OID != OIDsrAPI))
206 return SR_RET_INVALID_HANDLE;
207
208 return sbLstnRun(pThis->pLstn);
209 }
210
211
srAPIShutdownListener(srAPIObj * pThis)212 srRetVal srAPIShutdownListener(srAPIObj *pThis)
213 {
214 if((pThis == NULL) || (pThis->OID != OIDsrAPI))
215 return SR_RET_INVALID_HANDLE;
216
217 pThis->pLstn->bRun = FALSE;
218
219 return SR_RET_OK;
220 }
221