-
Notifications
You must be signed in to change notification settings - Fork 7
/
lib_mysqludf_stomp.c
613 lines (507 loc) · 18.6 KB
/
lib_mysqludf_stomp.c
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*
lib_mysqludf_stomp - a library to send STOMP messages
Copyright 2005 LogicBlaze Inc.
Copyright (C) 2011 Dmitry Demianov aka barlone
this library use part of libstomp code
web of STOMP project: http://stomp.codehaus.org/
email: [email protected]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Compile with (adapt the include and lib path to your environment):
> gcc -Wall -O2 -I/usr/local/include/apr-1 -I/usr/local/include/mysql \
lib_mysqludf_stomp.c /usr/local/lib/libapr-1.so -shared \
-o lib_mysqludf_stomp.so
> strip ./lib_mysqludf_stomp.so
Add the functions to MySQL with:
mysql> CREATE FUNCTION stompsend RETURNS STRING SONAME "lib_mysqludf_stomp.so";
mysql> CREATE FUNCTION stompsend1 RETURNS STRING SONAME "lib_mysqludf_stomp.so";
mysql> CREATE FUNCTION stompsend2 RETURNS STRING SONAME "lib_mysqludf_stomp.so";
*/
#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(WIN32)
#define DLLEXP __declspec(dllexport)
#else
#define DLLEXP
#endif
#ifdef STANDARD
/* STANDARD is defined, don't use any mysql functions */
#include <stdlib.h>
#include <string.h>
#ifdef __WIN__
typedef unsigned __int64 ulonglong; /* Microsofts 64 bit types */
typedef __int64 longlong;
#else
typedef unsigned long long ulonglong;
typedef long long longlong;
#endif /*__WIN__*/
#else
#include <my_global.h>
#include <my_sys.h>
#include <m_string.h>
#endif
#include <mysql.h>
#include <ctype.h>
#include "apr.h"
#include "apr_strings.h"
#include "apr_general.h"
#include "apr_network_io.h"
#include "apr_hash.h"
#ifdef HAVE_DLOPEN
#define LIBVERSION "lib_mysqludf_stomp version 0.2.0"
/******************************************************************************
** function declarations
******************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
DLLEXP
my_bool stompsend_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
DLLEXP
void stompsend_deinit(UDF_INIT *initid);
DLLEXP
char *stompsend(UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *res_length, char *null_value, char *error);
DLLEXP
my_bool stompsend1_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
DLLEXP
void stompsend1_deinit(UDF_INIT *initid);
DLLEXP
char *stompsend1(UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *res_length, char *null_value, char *error);
DLLEXP
my_bool stompsend2_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
DLLEXP
void stompsend2_deinit(UDF_INIT *initid);
DLLEXP
char *stompsend2(UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *res_length, char *null_value, char *error);
typedef struct stomp_connection {
apr_socket_t *socket;
apr_sockaddr_t *local_sa;
char *local_ip;
apr_sockaddr_t *remote_sa;
char *remote_ip;
} stomp_connection;
typedef struct stomp_frame {
char *command;
apr_hash_t *headers;
char *body;
int body_length;
} stomp_frame;
static apr_pool_t *pool;
#ifdef __cplusplus
}
#endif
// ------------------------------------------------------------------------------
// STOMP functions
/******************************************************************************
*
* Used to establish a connection
*
********************************************************************************/
APR_DECLARE(apr_status_t) stomp_connect(stomp_connection **connection_ref, const char *hostname, int port, apr_pool_t *pool)
{
apr_status_t rc;
int socket_family;
stomp_connection *connection=NULL;
//
// Allocate the connection and a memory pool for the connection.
//
connection = apr_pcalloc(pool, sizeof(stomp_connection));
if( connection == NULL )
return APR_ENOMEM;
#define CHECK_SUCCESS if( rc!=APR_SUCCESS ) { return rc; }
// Look up the remote address
rc = apr_sockaddr_info_get(&connection->remote_sa, hostname, APR_UNSPEC, port, APR_IPV4_ADDR_OK, pool);
CHECK_SUCCESS;
// Create the socket.
socket_family = connection->remote_sa->sa.sin.sin_family;
rc = apr_socket_create(&connection->socket, socket_family, SOCK_STREAM, APR_PROTO_TCP, pool);
CHECK_SUCCESS;
// Set socket options.
rc = apr_socket_opt_set(connection->socket, APR_SO_NONBLOCK, 1);
CHECK_SUCCESS;
rc = apr_socket_timeout_set(connection->socket, 1 * APR_USEC_PER_SEC);
CHECK_SUCCESS;
// Try connect
rc = apr_socket_connect(connection->socket, connection->remote_sa);
CHECK_SUCCESS;
// Get the Socket Info
rc = apr_socket_addr_get(&connection->remote_sa, APR_REMOTE, connection->socket);
CHECK_SUCCESS;
rc = apr_sockaddr_ip_get(&connection->remote_ip, connection->remote_sa);
CHECK_SUCCESS;
rc = apr_socket_addr_get(&connection->local_sa, APR_LOCAL, connection->socket);
CHECK_SUCCESS;
rc = apr_sockaddr_ip_get(&connection->local_ip, connection->local_sa);
CHECK_SUCCESS;
#undef CHECK_SUCCESS
*connection_ref = connection;
return rc;
} // stomp_connect
APR_DECLARE(apr_status_t) stomp_disconnect(stomp_connection **connection_ref)
{
apr_status_t result, rc;
stomp_connection *connection = *connection_ref;
if( connection_ref == NULL || *connection_ref==NULL )
return APR_EGENERAL;
result = APR_SUCCESS;
rc = apr_socket_shutdown(connection->socket, APR_SHUTDOWN_WRITE);
if( result!=APR_SUCCESS )
result = rc;
if( connection->socket != NULL ) {
rc = apr_socket_close(connection->socket);
if( result!=APR_SUCCESS )
result = rc;
connection->socket=NULL;
}
*connection_ref=NULL;
return rc;
} // stomp_disconnect
/********************************************************************************
*
* Wrappers around the apr_socket_send and apr_socket_recv calls so that they
* read/write their buffers fully.
*
********************************************************************************/
APR_DECLARE(apr_status_t) stomp_write_buffer(stomp_connection *connection, const char *data, apr_size_t size)
{
apr_size_t remaining = size;
size=0;
while( remaining>0 ) {
apr_size_t length = remaining;
apr_status_t rc = apr_socket_send(connection->socket, data, &length);
data+=length;
remaining -= length;
// size += length;
if( rc != APR_SUCCESS )
return rc;
}
return APR_SUCCESS;
} // stomp_write_buffer
/********************************************************************************
*
* Handles reading and writing stomp_frames to and from the connection
*
********************************************************************************/
APR_DECLARE(apr_status_t) stomp_write(stomp_connection *connection, stomp_frame *frame, apr_pool_t* pool) {
apr_status_t rc;
#define CHECK_SUCCESS if( rc!=APR_SUCCESS ) { return rc; }
// Write the command.
rc = stomp_write_buffer(connection, frame->command, strlen(frame->command));
CHECK_SUCCESS;
rc = stomp_write_buffer(connection, "\n", 1);
CHECK_SUCCESS;
// Write the headers
if( frame->headers != NULL ) {
apr_hash_index_t *i;
const void *key;
void *value;
for (i = apr_hash_first(NULL, frame->headers); i; i = apr_hash_next(i)) {
apr_hash_this(i, &key, NULL, &value);
rc = stomp_write_buffer(connection, key, strlen(key));
CHECK_SUCCESS;
rc = stomp_write_buffer(connection, ":", 1);
CHECK_SUCCESS;
rc = stomp_write_buffer(connection, value, strlen(value));
CHECK_SUCCESS;
rc = stomp_write_buffer(connection, "\n", 1);
CHECK_SUCCESS;
}
if(frame->body_length >= 0) {
apr_pool_t *length_pool;
char *length_string;
apr_pool_create(&length_pool, pool);
rc = stomp_write_buffer(connection, "content-length:", 15);
CHECK_SUCCESS;
length_string = apr_itoa(length_pool, frame->body_length);
rc = stomp_write_buffer(connection, length_string, strlen(length_string));
CHECK_SUCCESS;
rc = stomp_write_buffer(connection, "\n", 1);
CHECK_SUCCESS;
apr_pool_destroy(length_pool);
}
}
rc = stomp_write_buffer(connection, "\n", 1);
CHECK_SUCCESS;
// Write the body.
if( frame->body != NULL ) {
int body_length = frame->body_length;
if(body_length < 0)
body_length = strlen(frame->body);
rc = stomp_write_buffer(connection, frame->body, body_length);
CHECK_SUCCESS;
}
rc = stomp_write_buffer(connection, "\0\n", 2);
CHECK_SUCCESS;
#undef CHECK_SUCCESS
return APR_SUCCESS;
} // stomp_write
my_bool stompsend_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
/* make sure user has provided exactly three string arguments */
if (args->arg_count != 3 || (args->arg_type[0] != STRING_RESULT)
|| (args->arg_type[1] != STRING_RESULT)
|| (args->arg_type[2] != STRING_RESULT)){
strcpy(message, "stompsend requires 3 string arguments");
return 1;
}
if ((args->lengths[0] == 0) || (args->lengths[1] == 0) || (args->lengths[2] == 0)){
strcpy(message, "stompsend arguments can not be empty");
return 1;
}
// init APR
if (apr_initialize() != APR_SUCCESS) {
strcpy(message, "stompsend could not initialize APR");
return 2;
}
if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
strcpy(message, "stompsend could not allocate APR pool");
return 3;
}
initid->maybe_null=0;
return 0;
}
/******************************************************************************
** purpose: deallocate memory allocated by str_translate_init(); this func
** is called once for each query which invokes str_translate(),
** it is called after all of the calls to str_translate() are done
** receives: pointer to UDF_INIT struct (the same which was used by
** str_translate_init() and str_translate())
** returns: nothing
******************************************************************************/
void stompsend_deinit(UDF_INIT *initid __attribute__((unused)))
{
apr_pool_destroy(pool);
}
/******************************************************************************/
char *stompsend(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *res_length,
char *null_value, char *error)
{
stomp_connection *connection;
stomp_frame frame;
char *host = args->args[0];
char *topic = args->args[1];
char *message = args->args[2];
if (stomp_connect( &connection, host, 61613, pool) != APR_SUCCESS) {
strcpy(error, "stompsend could not connect to broker");
*null_value = 1;
return NULL;
}
frame.command = "CONNECT";
frame.headers = apr_hash_make(pool);
frame.body = NULL;
frame.body_length = -1;
if (stomp_write(connection, &frame, pool) != APR_SUCCESS) {
strcpy(error, "stompsend could not send CONNECT frame");
*null_value = 1;
return NULL;
}
frame.command = "SEND";
frame.headers = apr_hash_make(pool);
apr_hash_set(frame.headers, "destination", APR_HASH_KEY_STRING, topic);
frame.body_length = -1;
frame.body = message;
if (stomp_write(connection, &frame, pool) != APR_SUCCESS) {
strcpy(error, "stompsend could not send SEND frame");
*null_value = 1;
return NULL;
}
frame.command = "DISCONNECT";
frame.headers = NULL;
frame.body_length = -1;
frame.body = NULL;
stomp_write(connection, &frame, pool); // ignore errors
stomp_disconnect(&connection);
*res_length = 2;
strcpy(result, "OK");
return result;
} // stompsend
my_bool stompsend1_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
/* make sure user has provided exactly three string arguments */
if (args->arg_count != 5 || (args->arg_type[0] != STRING_RESULT)
|| (args->arg_type[1] != STRING_RESULT)
|| (args->arg_type[2] != STRING_RESULT)
|| (args->arg_type[3] != STRING_RESULT)
|| (args->arg_type[4] != STRING_RESULT)){
strcpy(message, "stompsend1 requires 5 string arguments");
return 1;
}
if ((args->lengths[0] == 0) || (args->lengths[1] == 0) || (args->lengths[2] == 0) ||
(args->lengths[3] == 0) || (args->lengths[4] == 0)){
strcpy(message, "stompsend1 arguments can not be empty");
return 1;
}
// init APR
if (apr_initialize() != APR_SUCCESS) {
strcpy(message, "stompsend1 could not initialize APR");
return 2;
}
if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
strcpy(message, "stompsend1 could not allocate APR pool");
return 3;
}
initid->maybe_null=0;
return 0;
} // stompsend1_init
/******************************************************************************
** purpose: deallocate memory allocated by str_translate_init(); this func
** is called once for each query which invokes str_translate(),
** it is called after all of the calls to str_translate() are done
** receives: pointer to UDF_INIT struct (the same which was used by
** str_translate_init() and str_translate())
** returns: nothing
******************************************************************************/
void stompsend1_deinit(UDF_INIT *initid __attribute__((unused)))
{
apr_pool_destroy(pool);
} // stompsend1_deinit
/******************************************************************************/
char *stompsend1(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *res_length,
char *null_value, char *error)
{
stomp_connection *connection;
stomp_frame frame;
char *host = args->args[0];
char *topic = args->args[1];
char *message = args->args[2];
char *hdr1name = args->args[3];
char *hdr1val = args->args[4];
if (stomp_connect( &connection, host, 61613, pool) != APR_SUCCESS) {
strcpy(error, "stompsend1 could not connect to broker");
*null_value = 1;
return NULL;
}
frame.command = "CONNECT";
frame.headers = apr_hash_make(pool);
frame.body = NULL;
frame.body_length = -1;
if (stomp_write(connection, &frame, pool) != APR_SUCCESS) {
strcpy(error, "stompsend1 could not send CONNECT frame");
*null_value = 1;
return NULL;
}
frame.command = "SEND";
frame.headers = apr_hash_make(pool);
apr_hash_set(frame.headers, "destination", APR_HASH_KEY_STRING, topic);
apr_hash_set(frame.headers, hdr1name, APR_HASH_KEY_STRING, hdr1val);
frame.body_length = -1;
frame.body = message;
if (stomp_write(connection, &frame, pool) != APR_SUCCESS) {
strcpy(error, "stompsend1 could not send SEND frame");
*null_value = 1;
return NULL;
}
frame.command = "DISCONNECT";
frame.headers = NULL;
frame.body_length = -1;
frame.body = NULL;
stomp_write(connection, &frame, pool); // ignore errors
stomp_disconnect(&connection);
*res_length = 2;
strcpy(result, "OK");
return result;
} // stompsend1
my_bool stompsend2_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
/* make sure user has provided exactly three string arguments */
if (args->arg_count != 7 || (args->arg_type[0] != STRING_RESULT)
|| (args->arg_type[1] != STRING_RESULT)
|| (args->arg_type[2] != STRING_RESULT)
|| (args->arg_type[3] != STRING_RESULT)
|| (args->arg_type[4] != STRING_RESULT)
|| (args->arg_type[5] != STRING_RESULT)
|| (args->arg_type[6] != STRING_RESULT)){
strcpy(message, "stompsend2 requires 7 string arguments");
return 1;
}
if ((args->lengths[0] == 0) || (args->lengths[1] == 0) || (args->lengths[2] == 0) ||
(args->lengths[3] == 0) || (args->lengths[4] == 0) ||
(args->lengths[5] == 0) || (args->lengths[6] == 0)){
strcpy(message, "stompsend2 arguments can not be empty");
return 1;
}
// init APR
if (apr_initialize() != APR_SUCCESS) {
strcpy(message, "stompsend2 could not initialize APR");
return 2;
}
if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
strcpy(message, "stompsend2 could not allocate APR pool");
return 3;
}
initid->maybe_null=0;
return 0;
} // stompsend1_init
/******************************************************************************
** purpose: deallocate memory allocated by str_translate_init(); this func
** is called once for each query which invokes str_translate(),
** it is called after all of the calls to str_translate() are done
** receives: pointer to UDF_INIT struct (the same which was used by
** str_translate_init() and str_translate())
** returns: nothing
******************************************************************************/
void stompsend2_deinit(UDF_INIT *initid __attribute__((unused)))
{
apr_pool_destroy(pool);
} // stompsend2_deinit
/******************************************************************************/
char *stompsend2(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *res_length,
char *null_value, char *error)
{
stomp_connection *connection;
stomp_frame frame;
char *host = args->args[0];
char *topic = args->args[1];
char *message = args->args[2];
char *hdr1name = args->args[3];
char *hdr1val = args->args[4];
char *hdr2name = args->args[5];
char *hdr2val = args->args[6];
if (stomp_connect( &connection, host, 61613, pool) != APR_SUCCESS) {
strcpy(error, "stompsend2 could not connect to broker");
*null_value = 1;
return NULL;
}
frame.command = "CONNECT";
frame.headers = apr_hash_make(pool);
frame.body = NULL;
frame.body_length = -1;
if (stomp_write(connection, &frame, pool) != APR_SUCCESS) {
strcpy(error, "stompsend2 could not send CONNECT frame");
*null_value = 1;
return NULL;
}
frame.command = "SEND";
frame.headers = apr_hash_make(pool);
apr_hash_set(frame.headers, "destination", APR_HASH_KEY_STRING, topic);
apr_hash_set(frame.headers, hdr1name, APR_HASH_KEY_STRING, hdr1val);
apr_hash_set(frame.headers, hdr2name, APR_HASH_KEY_STRING, hdr2val);
frame.body_length = -1;
frame.body = message;
if (stomp_write(connection, &frame, pool) != APR_SUCCESS) {
strcpy(error, "stompsend2 could not send SEND frame");
*null_value = 1;
return NULL;
}
frame.command = "DISCONNECT";
frame.headers = NULL;
frame.body_length = -1;
frame.body = NULL;
stomp_write(connection, &frame, pool); // ignore errors
stomp_disconnect(&connection);
*res_length = 2;
strcpy(result, "OK");
return result;
} // stompsend2
#endif /* HAVE_DLOPEN */