aboutsummaryrefslogtreecommitdiffstats
path: root/daemon/udp_listener.c
blob: f9c7c1a9fa377f263713aa8d7e620e00c26e1d9b (plain) (blame)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * udp_listener.c - syslogd implementation for windows, UDP listener
 *
 * Created by Alexander Yaworsky
 *
 * THIS SOFTWARE IS NOT COPYRIGHTED
 *
 * This source code is offered for use in the public domain. You may
 * use, modify or distribute it freely.
 *
 * This code is distributed in the hope that it will be useful but
 * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
 * DISCLAIMED. This includes but is not limited to warranties of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

#include <process.h>
#include <stdio.h>
#include <winsock2.h>

#include <glib.h>

#include <syslog.h>
#include <syslogd.h>

/* udp listener data */
static SOCKET *udp_socket_array = NULL;
static int udp_socket_count = 0;

static struct source **udp_source_references = NULL;

static HANDLE *udp_event_array = NULL;
static int udp_event_count = 0;
static HANDLE udp_listener_stop_event = NULL;

static unsigned max_datagram_size = 1024;
static gchar *datagram_buffer = NULL;

static HANDLE udp_listener_thread_handle = NULL;

struct fifo *udp_message_queue = NULL;
HANDLE udp_queue_event = NULL;
CRITICAL_SECTION udp_queue_cs;

/******************************************************************************
 * udp_listener_thread
 */
static unsigned __stdcall udp_listener_thread( void* arg )
{
    TRACE_ENTER( "\n" );

    SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL );

    for(;;)
    {
        DWORD w;
        int r;
        int addrlen;
        struct raw_message message;
        struct raw_message *msg;

        w = WaitForMultipleObjects( udp_event_count, udp_event_array, FALSE, INFINITE );
        if( WAIT_FAILED == w )
        {
            ERR( "Wait error %lu\n", GetLastError() );
            goto done;
        }
        if( w >= udp_event_count )
        {
            ERR( "Unknown wait error\n" );
            goto done;
        }
        if( w == udp_event_count - 1 )
        {
            TRACE_2( "shutdown\n" );
            goto done;
        }
        /* got UDP message, read it */
        addrlen = sizeof(message.sender_addr);
        r = recvfrom( udp_socket_array[ w ], datagram_buffer, max_datagram_size,
                      0, (struct sockaddr*) &message.sender_addr, &addrlen );
        if( r < 0 )
        {
            ERR( "recvfrom() error %lu\n", WSAGetLastError() );
            goto done;
        }
        if( !r )
        {
            TRACE_2( "empty datagram\n" );
            continue;
        }
        message.msg = g_strndup( datagram_buffer, r );
        message.source = udp_source_references[ w ];

        TRACE_2( "got message from %d.%d.%d.%d; source name %s; %s\n",
                 message.sender_addr.sin_addr.S_un.S_un_b.s_b1,
                 message.sender_addr.sin_addr.S_un.S_un_b.s_b2,
                 message.sender_addr.sin_addr.S_un.S_un_b.s_b3,
                 message.sender_addr.sin_addr.S_un.S_un_b.s_b4,
                 message.source->name, message.msg );

        /* allocate raw message and add it to the queue */
        msg = g_malloc( sizeof(struct raw_message) );
        memcpy( msg, &message, sizeof(struct raw_message) );
        EnterCriticalSection( &udp_queue_cs );
        if( fifo_push( udp_message_queue, msg ) )
            SetEvent( udp_queue_event );
        LeaveCriticalSection( &udp_queue_cs );
    }

done:
    TRACE_LEAVE( "done\n" );
    return 0;
}

/******************************************************************************
 * shutdown_udp_listener
 *
 * stop listening thread and dispose all objects
 * except message queue and event
 */
void shutdown_udp_listener()
{
    TRACE_ENTER( "\n" );

    if( udp_listener_thread_handle )
    {
        SetEvent( udp_listener_stop_event );
        TRACE_2( "wait for shutdown of udp listener thread\n" );
        WaitForSingleObject( udp_listener_thread_handle, INFINITE );
        CloseHandle( udp_listener_thread_handle );
        udp_listener_thread_handle = NULL;
    }

    if( udp_socket_array )
    {
        int i;

        for( i = 0; i < udp_socket_count; i++ )
            closesocket( udp_socket_array[ i ] );
        g_free( udp_socket_array );
        udp_socket_array = NULL;
        udp_socket_count = 0;
    }

    if( udp_source_references )
    {
        g_free( udp_source_references );
        udp_source_references = NULL;
    }

    if( udp_event_array )
    {
        int i;

        for( i = 0; i < udp_event_count; i++ )
            CloseHandle( udp_event_array[ i ] );
        g_free( udp_event_array );
        udp_event_array = NULL;
        udp_event_count = 0;
        udp_listener_stop_event = NULL;
    }

    if( datagram_buffer )
    {
        g_free( datagram_buffer );
        datagram_buffer = NULL;
    }
    max_datagram_size = 1024;

    TRACE_LEAVE( "done\n" );
}

/******************************************************************************
 * fini_udp_listener
 */
void fini_udp_listener()
{
    TRACE_ENTER( "\n" );

    shutdown_udp_listener();

    if( udp_message_queue )
    {
        fifo_destroy( udp_message_queue );
        udp_message_queue = NULL;
    }

    if( udp_queue_event )
    {
        DeleteCriticalSection( &udp_queue_cs );
        CloseHandle( udp_queue_event );
        udp_queue_event = NULL;
    }

    TRACE_LEAVE( "done\n" );
}

/******************************************************************************
 * init_udp_listener
 *
 * create sockets, queue and thread
 */
gboolean init_udp_listener()
{
    gboolean ret = FALSE;
    unsigned n;
    GList *item;
    int i;

    TRACE_ENTER( "\n" );

    /* create message queue and event;
     * we should do this first because of possible early returns
     * from this function when no sources are defined
     */
    udp_message_queue = fifo_create();
    udp_queue_event = CreateEvent( NULL, TRUE, FALSE, NULL );
    if( !udp_queue_event )
    {
        ERR( "Cannot create event; error %lu\n", GetLastError() );
        goto done;
    }
    InitializeCriticalSection( &udp_queue_cs );

    n = number_of_sources( ST_UDP );
    if( 0 == n )
    {
        ret = TRUE;
        goto done;
    }

    /* allocate memory for sockets, source references and events;
     * the number of sockets is not greater than number of sources
     */
    udp_socket_array = g_malloc( n * sizeof(SOCKET) );
    udp_source_references = g_malloc( n * sizeof(struct source*) );

    /* number of events is greater by one because of
     * included udp_listener_stop_event
     */
    udp_event_array = g_malloc( (n + 1) * sizeof(HANDLE) );

    /* create sockets */
    for( item = sources; item; item = item->next )
    {
        struct source *src = item->data;
        SOCKET sock;
        unsigned dgram_size;
        int size;

        if( src->type != ST_UDP )
            continue;

        sock = socket( AF_INET, SOCK_DGRAM, 0 );
        if( INVALID_SOCKET == sock )
        {
            ERR( "Cannot create source %s: socket() error %lu\n", src->name, WSAGetLastError() );
            continue;
        }

        if( bind( sock, (struct sockaddr*) &src->udp, sizeof(src->udp) ) )
        {
            ERR( "Cannot create source %s: bind() error %lu\n", src->name, WSAGetLastError() );
            closesocket( sock );
            continue;
        }

        size = sizeof(dgram_size);
        if( getsockopt( sock, SOL_SOCKET, SO_MAX_MSG_SIZE, (char*) &dgram_size, &size ) )
        {
            ERR( "Cannot create source %s: getsockopt( SO_MAX_MSG_SIZE ) error %lu\n",
                 src->name, WSAGetLastError() );
            closesocket( sock );
            continue;
        }
        TRACE( "datagram size for %d.%d.%d.%d:%d is %u\n",
               src->udp.sin_addr.S_un.S_un_b.s_b1, src->udp.sin_addr.S_un.S_un_b.s_b2,
               src->udp.sin_addr.S_un.S_un_b.s_b3, src->udp.sin_addr.S_un.S_un_b.s_b4,
               ntohs( src->udp.sin_port ), dgram_size );
        if( dgram_size > max_datagram_size )
            max_datagram_size = dgram_size;

        udp_source_references[ udp_socket_count ] = src;
        udp_socket_array[ udp_socket_count++ ] = sock;
    }

    if( 0 == udp_socket_count )
    {
        ret = TRUE;
        goto done;
    }

    /* create events */
    while( udp_event_count < udp_socket_count )
    {
        HANDLE evt = CreateEvent( NULL, FALSE, FALSE, NULL );
        if( !evt )
        {
            ERR( "Cannot initialize source %s: CreateEvent error %lu\n",
                 udp_source_references[ udp_event_count ]->name, GetLastError() );
            goto done;
        }
        udp_event_array[ udp_event_count++ ] = evt;
    }
    udp_listener_stop_event = CreateEvent( NULL, TRUE, FALSE, NULL );
    if( !udp_listener_stop_event )
    {
        ERR( "Cannot create event; error %lu\n", GetLastError() );
        goto done;
    }
    udp_event_array[ udp_event_count++ ] = udp_listener_stop_event;

    /* bind events to sockets */
    for( i = 0; i < udp_socket_count; i++ )
    {
        if( WSAEventSelect( udp_socket_array[ i ], udp_event_array[ i ], FD_READ ) )
        {
            ERR( "Cannot initialize source %s: WSAEventSelect() error %lu\n",
                 udp_source_references[ i ]->name, WSAGetLastError() );
            goto done;
        }
    }

    /* allocate datagram buffer */
    datagram_buffer = g_malloc( max_datagram_size );

    /* create thread */
    udp_listener_thread_handle = (HANDLE) _beginthreadex( NULL, 0, udp_listener_thread, NULL, 0, &n );
    if( !udp_listener_thread_handle )
    {
        ERR( "Cannot create thread; error %lu\n", GetLastError() );
        goto done;
    }

    ret = TRUE;

done:
    if( !ret )
        fini_udp_listener();

    TRACE_LEAVE( "done; udp_socket_count=%d, udp_event_count=%d, max_datagram_size=%d, ret=%d\n",
                 udp_socket_count, udp_event_count, max_datagram_size, (int) ret );
    return ret;
}