forked from mgdm/Mosquitto-PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mosquitto.c
1507 lines (1255 loc) · 43.5 KB
/
mosquitto.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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "zend_variables.h"
#include "zend_exceptions.h"
#include "zend_API.h"
#include "ext/standard/php_filestat.h"
#include "ext/standard/info.h"
#include "php_mosquitto.h"
zend_class_entry *mosquitto_ce_client;
zend_class_entry *mosquitto_ce_exception;
zend_object_handlers mosquitto_std_object_handlers;
ZEND_DECLARE_MODULE_GLOBALS(mosquitto)
#ifdef ZEND_ENGINE_3
# ifndef Z_BVAL
# define Z_BVAL(zv) (Z_TYPE(zv) == IS_TRUE)
# define Z_BVAL_P(pzv) Z_BVAL(*pzv)
# endif
# define ZO_HANDLE_DC
typedef size_t mosquitto_strlen_type;
#else /* ZEND_ENGINE_2 */
# ifndef Z_OBJ_P
# define Z_OBJ_P(pzv) ((zend_object*)zend_object_store_get_object(pzv TSRMLS_CC))
# endif
# define ZO_HANDLE_DC , zend_object_handle handle
typedef int mosquitto_strlen_type;
typedef long zend_long;
#endif
static inline mosquitto_client_object *mosquitto_client_object_get(zval *zobj TSRMLS_DC) {
// TODO: ZEND_ASSERT()s
mosquitto_client_object *obj = mosquitto_client_object_from_zend_object(Z_OBJ_P(zobj));
if (!obj->client) {
php_error(E_ERROR, "Internal surface object missing in %s wrapper, "
"you must call parent::__construct in extended classes", Z_OBJCE_P(zobj)->name);
}
return obj;
}
static inline void mosquitto_callback_addref(zend_fcall_info *finfo) {
#ifdef ZEND_ENGINE_3
zval tmp;
Z_TRY_ADDREF(finfo->function_name);
if (finfo->object) {
ZVAL_OBJ(&tmp, finfo->object);
Z_TRY_ADDREF(tmp);
}
#else
Z_ADDREF_P(finfo->function_name);
if (finfo->object_ptr) {
Z_ADDREF_P(finfo->object_ptr);
}
#endif
}
static inline const char *mosquitto_finfo_name(zend_fcall_info *info) {
#ifdef ZEND_ENGINE_3
return Z_STRVAL(info->function_name);
#else
return Z_STRVAL_P(info->function_name);
#endif
}
static int php_mosquitto_pw_callback(char *buf, int size, int rwflag, void *userdata);
/* {{{ Arginfo */
ZEND_BEGIN_ARG_INFO(Mosquitto_Client___construct_args, ZEND_SEND_BY_VAL)
ZEND_ARG_INFO(0, id)
ZEND_ARG_INFO(0, cleanSession)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(Mosquitto_Client_callback_args, ZEND_SEND_BY_VAL)
#if PHP_VERSION_ID > 50400
ZEND_ARG_TYPE_INFO(0, onConnect, IS_CALLABLE, 0)
#else
ZEND_ARG_INFO(0, onConnect)
#endif
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(Mosquitto_Client_setCredentials_args, ZEND_SEND_BY_VAL)
ZEND_ARG_INFO(0, username)
ZEND_ARG_INFO(0, password)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(Mosquitto_Client_setWill_args, ZEND_SEND_BY_VAL)
ZEND_ARG_INFO(0, topic)
ZEND_ARG_INFO(0, payload)
ZEND_ARG_INFO(0, qos)
ZEND_ARG_INFO(0, retain)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(Mosquitto_Client_setReconnectDelay_args, ZEND_SEND_BY_VAL)
ZEND_ARG_INFO(0, reconnectDelay)
ZEND_ARG_INFO(0, maxReconnectDelay)
ZEND_ARG_INFO(0, exponentialBackoff)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(Mosquitto_Client_setMessageRetry_args, ZEND_SEND_BY_VAL)
ZEND_ARG_INFO(0, messageRetry)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(Mosquitto_Client_connect_args, ZEND_SEND_BY_VAL)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, port)
ZEND_ARG_INFO(0, keepalive)
ZEND_ARG_INFO(0, interface)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(Mosquitto_Client_setMaxInFlightMessages_args, ZEND_SEND_BY_VAL)
ZEND_ARG_INFO(0, max)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(Mosquitto_Client_publish_args, ZEND_SEND_BY_VAL)
ZEND_ARG_INFO(0, topic)
ZEND_ARG_INFO(0, payload)
ZEND_ARG_INFO(0, qos)
ZEND_ARG_INFO(0, retain)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(Mosquitto_Client_subscribe_args, ZEND_SEND_BY_VAL)
ZEND_ARG_INFO(0, topic)
ZEND_ARG_INFO(0, qos)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(Mosquitto_Client_unsubscribe_args, ZEND_SEND_BY_VAL)
ZEND_ARG_INFO(0, topic)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(Mosquitto_Client_loop_args, ZEND_SEND_BY_VAL)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, maxPackets)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(Mosquitto_Client_loopForever_args, ZEND_SEND_BY_VAL)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, maxPackets)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ Mosquitto\Client::__construct() */
PHP_METHOD(Mosquitto_Client, __construct)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
char *id = NULL;
mosquitto_strlen_type id_len = 0;
zend_bool clean_session = 1;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!b", &id, &id_len, &clean_session) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
object->client = mosquitto_new(id, clean_session, object);
if (!object->client) {
char *message = php_mosquitto_strerror_wrapper(errno);
zend_throw_exception(mosquitto_ce_exception, message, 1 TSRMLS_CC);
if (message) {
efree(message);
}
}
}
/* }}} */
/* {{{ Mosquitto\Client::setTlsCertificates() */
PHP_METHOD(Mosquitto_Client, setTlsCertificates)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
char *ca_path = NULL, *cert_path = NULL, *key_path = NULL, *key_pw = NULL;
mosquitto_strlen_type ca_path_len = 0, cert_path_len = 0, key_path_len = 0, key_pw_len;
int retval = 0;
zval stat;
zend_bool is_dir = 0;
int (*pw_callback)(char *, int, int, void *) = NULL;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!|s!s!s!",
&ca_path, &ca_path_len,
&cert_path, &cert_path_len,
&key_path, &key_path_len,
&key_pw, &key_pw_len) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
if ((php_check_open_basedir(ca_path TSRMLS_CC) < 0) ||
(php_check_open_basedir(cert_path TSRMLS_CC) < 0) ||
(php_check_open_basedir(key_path TSRMLS_CC) < 0))
{
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
php_stat(ca_path, ca_path_len, FS_IS_DIR, &stat TSRMLS_CC);
is_dir = Z_BVAL(stat);
if (key_pw != NULL) {
pw_callback = php_mosquitto_pw_callback;
MQTTG(client_key) = estrdup(key_pw);
MQTTG(client_key_len) = key_pw_len;
}
if (is_dir) {
retval = mosquitto_tls_set(object->client, NULL, ca_path, cert_path, key_path, pw_callback);
} else {
retval = mosquitto_tls_set(object->client, ca_path, NULL, cert_path, key_path, pw_callback);
}
php_mosquitto_handle_errno(retval, errno TSRMLS_CC);
RETURN_LONG(retval);
}
/* }}} */
/* {{{ Mosquitto\Client::setTlsInsecure() */
PHP_METHOD(Mosquitto_Client, setTlsInsecure)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
zend_bool value = 0;
int retval = 0;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &value) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
retval = mosquitto_tls_insecure_set(object->client, value);
php_mosquitto_handle_errno(retval, errno TSRMLS_CC);
}
/* }}} */
/* {{{ Mosquitto\Client::setTlsOptions() */
PHP_METHOD(Mosquitto_Client, setTlsOptions)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
char *tls_version = NULL, *ciphers = NULL;
mosquitto_strlen_type tls_version_len = 0, ciphers_len = 0;
int retval = 0, verify_peer = 0;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|s!s!",
&verify_peer,
&tls_version, &tls_version_len,
&ciphers, &ciphers_len
) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
retval = mosquitto_tls_opts_set(object->client, verify_peer, tls_version, ciphers);
php_mosquitto_handle_errno(retval, errno TSRMLS_CC);
RETURN_LONG(retval);
}
/* }}} */
/* {{{ Mosquitto\Client::setTlsPSK() */
PHP_METHOD(Mosquitto_Client, setTlsPSK)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
char *psk = NULL, *identity = NULL, *ciphers = NULL;
mosquitto_strlen_type psk_len = 0, identity_len = 0, ciphers_len = 0;
int retval = 0;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!s!|s!",
&psk, &psk_len, &identity, &identity_len, &ciphers, &ciphers_len
) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
retval = mosquitto_tls_psk_set(object->client, psk, identity, ciphers);
php_mosquitto_handle_errno(retval, errno TSRMLS_CC);
RETURN_LONG(retval);
}
/* }}} */
/* {{{ Mosquitto\Client::setCredentials() */
PHP_METHOD(Mosquitto_Client, setCredentials)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
char *username = NULL, *password = NULL;
mosquitto_strlen_type username_len, password_len;
int retval;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &username, &username_len, &password, &password_len) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
retval = mosquitto_username_pw_set(object->client, username, password);
php_mosquitto_handle_errno(retval, errno TSRMLS_CC);
}
/* }}} */
/* {{{ Mosquitto\Client::setWill() */
PHP_METHOD(Mosquitto_Client, setWill)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
mosquitto_strlen_type topic_len, payload_len;
int retval;
zend_long qos;
zend_bool retain;
char *topic, *payload;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sslb",
&topic, &topic_len, &payload, &payload_len, &qos, &retain) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
retval = mosquitto_will_set(object->client, topic, payload_len, (void *) payload, qos, retain);
php_mosquitto_handle_errno(retval, errno TSRMLS_CC);
}
/* }}} */
/* {{{ Mosquitto\Client::clearWill() */
PHP_METHOD(Mosquitto_Client, clearWill)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
int retval;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters_none() == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
retval = mosquitto_will_clear(object->client);
php_mosquitto_handle_errno(retval, errno TSRMLS_CC);
}
/* }}} */
/* {{{ Mosquitto\Client::setReconnectDelay() */
PHP_METHOD(Mosquitto_Client, setReconnectDelay)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
int retval;
zend_long reconnect_delay = 0, reconnect_delay_max = 0;
zend_bool exponential_backoff = 0;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|lb",
&reconnect_delay, &reconnect_delay_max, &exponential_backoff) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
retval = mosquitto_reconnect_delay_set(object->client, reconnect_delay, reconnect_delay_max, exponential_backoff);
php_mosquitto_handle_errno(retval, errno TSRMLS_CC);
}
/* }}} */
/* {{{ Mosquitto\Client::connect() */
PHP_METHOD(Mosquitto_Client, connect)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
char *host = NULL, *interface = NULL;
mosquitto_strlen_type host_len, interface_len;
int retval;
zend_long port = 1883;
zend_long keepalive = 60;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lls!",
&host, &host_len, &port, &keepalive,
&interface, &interface_len) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
if (interface == NULL) {
retval = mosquitto_connect(object->client, host, port, keepalive);
} else {
retval = mosquitto_connect_bind(object->client, host, port, keepalive, interface);
}
php_mosquitto_handle_errno(retval, errno TSRMLS_CC);
RETURN_LONG(retval);
}
/* }}} */
/* {{{ Mosquitto\Client::disconnect() */
PHP_METHOD(Mosquitto_Client, disconnect)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
int retval;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters_none() == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
retval = mosquitto_disconnect(object->client);
php_mosquitto_exit_loop(object);
php_mosquitto_handle_errno(retval, errno TSRMLS_CC);
}
/* }}} */
/* {{{ Mosquitto\Client::onConnect() */
PHP_METHOD(Mosquitto_Client, onConnect)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
zend_fcall_info connect_callback = empty_fcall_info;
zend_fcall_info_cache connect_callback_cache = empty_fcall_info_cache;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f!",
&connect_callback, &connect_callback_cache) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
if (!ZEND_FCI_INITIALIZED(connect_callback)) {
zend_throw_exception(mosquitto_ce_exception, "Need a valid callback", 0 TSRMLS_CC);
}
PHP_MOSQUITTO_FREE_CALLBACK(object, connect);
object->connect_callback = connect_callback;
object->connect_callback_cache = connect_callback_cache;
mosquitto_callback_addref(&(object->connect_callback));
mosquitto_connect_callback_set(object->client, php_mosquitto_connect_callback);
}
/* }}} */
/* {{{ Mosquitto\Client::onDisconnect() */
PHP_METHOD(Mosquitto_Client, onDisconnect)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
zend_fcall_info disconnect_callback = empty_fcall_info;
zend_fcall_info_cache disconnect_callback_cache = empty_fcall_info_cache;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f!",
&disconnect_callback, &disconnect_callback_cache) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
if (!ZEND_FCI_INITIALIZED(disconnect_callback)) {
zend_throw_exception(mosquitto_ce_exception, "Need a valid callback", 0 TSRMLS_CC);
}
PHP_MOSQUITTO_FREE_CALLBACK(object, disconnect);
object->disconnect_callback = disconnect_callback;
object->disconnect_callback_cache = disconnect_callback_cache;
mosquitto_callback_addref(&(object->disconnect_callback));
mosquitto_disconnect_callback_set(object->client, php_mosquitto_disconnect_callback);
}
/* }}} */
/* {{{ Mosquitto\Client::onLog() */
PHP_METHOD(Mosquitto_Client, onLog)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
zend_fcall_info log_callback = empty_fcall_info;
zend_fcall_info_cache log_callback_cache = empty_fcall_info_cache;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f!",
&log_callback, &log_callback_cache) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
if (!ZEND_FCI_INITIALIZED(log_callback)) {
zend_throw_exception(mosquitto_ce_exception, "Need a valid callback", 0 TSRMLS_CC);
}
PHP_MOSQUITTO_FREE_CALLBACK(object, log);
object->log_callback = log_callback;
object->log_callback_cache = log_callback_cache;
mosquitto_callback_addref(&(object->log_callback));
mosquitto_log_callback_set(object->client, php_mosquitto_log_callback);
}
/* }}} */
/* {{{ Mosquitto\Client::onSubscribe() */
PHP_METHOD(Mosquitto_Client, onSubscribe)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
zend_fcall_info subscribe_callback = empty_fcall_info;
zend_fcall_info_cache subscribe_callback_cache = empty_fcall_info_cache;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f!",
&subscribe_callback, &subscribe_callback_cache) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
if (!ZEND_FCI_INITIALIZED(subscribe_callback)) {
zend_throw_exception(mosquitto_ce_exception, "Need a valid callback", 0 TSRMLS_CC);
}
PHP_MOSQUITTO_FREE_CALLBACK(object, subscribe);
object->subscribe_callback = subscribe_callback;
object->subscribe_callback_cache = subscribe_callback_cache;
mosquitto_callback_addref(&(object->subscribe_callback));
mosquitto_subscribe_callback_set(object->client, php_mosquitto_subscribe_callback);
}
/* }}} */
/* {{{ Mosquitto\Client::onUnsubscribe() */
PHP_METHOD(Mosquitto_Client, onUnsubscribe)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
zend_fcall_info unsubscribe_callback = empty_fcall_info;
zend_fcall_info_cache unsubscribe_callback_cache = empty_fcall_info_cache;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f!",
&unsubscribe_callback, &unsubscribe_callback_cache) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
if (!ZEND_FCI_INITIALIZED(unsubscribe_callback)) {
zend_throw_exception(mosquitto_ce_exception, "Need a valid callback", 0 TSRMLS_CC);
}
PHP_MOSQUITTO_FREE_CALLBACK(object, unsubscribe);
object->unsubscribe_callback = unsubscribe_callback;
object->unsubscribe_callback_cache = unsubscribe_callback_cache;
mosquitto_callback_addref(&(object->unsubscribe_callback));
mosquitto_unsubscribe_callback_set(object->client, php_mosquitto_unsubscribe_callback);
}
/* }}} */
/* {{{ Mosquitto\Client::onMessage() */
PHP_METHOD(Mosquitto_Client, onMessage)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
zend_fcall_info message_callback = empty_fcall_info;
zend_fcall_info_cache message_callback_cache = empty_fcall_info_cache;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f!",
&message_callback, &message_callback_cache) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
if (!ZEND_FCI_INITIALIZED(message_callback)) {
zend_throw_exception(mosquitto_ce_exception, "Need a valid callback", 0 TSRMLS_CC);
}
PHP_MOSQUITTO_FREE_CALLBACK(object, message);
object->message_callback = message_callback;
object->message_callback_cache = message_callback_cache;
mosquitto_callback_addref(&(object->message_callback));
mosquitto_message_callback_set(object->client, php_mosquitto_message_callback);
}
/* }}} */
/* {{{ Mosquitto\Client::onPublish() */
PHP_METHOD(Mosquitto_Client, onPublish)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
zend_fcall_info publish_callback = empty_fcall_info;
zend_fcall_info_cache publish_callback_cache = empty_fcall_info_cache;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f!",
&publish_callback, &publish_callback_cache) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
if (!ZEND_FCI_INITIALIZED(publish_callback)) {
zend_throw_exception(mosquitto_ce_exception, "Need a valid callback", 0 TSRMLS_CC);
}
PHP_MOSQUITTO_FREE_CALLBACK(object, publish);
object->publish_callback = publish_callback;
object->publish_callback_cache = publish_callback_cache;
mosquitto_callback_addref(&(object->publish_callback));
mosquitto_publish_callback_set(object->client, php_mosquitto_publish_callback);
}
/* }}} */
/* {{{ Mosquitto\Client::getSocket() */
PHP_METHOD(Mosquitto_Client, getSocket)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters_none() == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
RETURN_LONG(mosquitto_socket(object->client));
}
/* }}} */
/* {{{ Mosquitto\Client::setMaxInFlightMessages() */
PHP_METHOD(Mosquitto_Client, setMaxInFlightMessages)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
int retval;
zend_long max = 0;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &max) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
retval = mosquitto_max_inflight_messages_set(object->client, max);
php_mosquitto_handle_errno(retval, errno TSRMLS_CC);
}
/* }}} */
/* {{{ Mosquitto\Client::setMessageRetry() */
PHP_METHOD(Mosquitto_Client, setMessageRetry)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
zend_long retry = 0;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &retry) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
mosquitto_message_retry_set(object->client, retry);
}
/* }}} */
/* {{{ Mosquitto\Client::publish() */
PHP_METHOD(Mosquitto_Client, publish)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
int mid, retval;
mosquitto_strlen_type topic_len, payload_len;
zend_long qos = 0;
zend_bool retain = 0;
char *topic, *payload;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|lb",
&topic, &topic_len, &payload, &payload_len, &qos, &retain) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
retval = mosquitto_publish(object->client, &mid, topic, payload_len, (void *) payload, qos, retain);
php_mosquitto_handle_errno(retval, errno TSRMLS_CC);
RETURN_LONG(mid);
}
/* }}} */
/* {{{ Mosquitto\Client::subscribe() */
PHP_METHOD(Mosquitto_Client, subscribe)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
char *sub;
mosquitto_strlen_type sub_len;
int retval, mid;
zend_long qos;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl",
&sub, &sub_len, &qos) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
retval = mosquitto_subscribe(object->client, &mid, sub, qos);
php_mosquitto_handle_errno(retval, errno TSRMLS_CC);
RETURN_LONG(mid);
}
/* }}} */
/* {{{ Mosquitto\Client::unsubscribe() */
PHP_METHOD(Mosquitto_Client, unsubscribe)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
char *sub;
mosquitto_strlen_type sub_len;
int retval, mid;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
&sub, &sub_len) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
retval = mosquitto_unsubscribe(object->client, &mid, sub);
php_mosquitto_handle_errno(retval, errno TSRMLS_CC);
RETURN_LONG(mid);
}
/* }}} */
/* {{{ Mosquitto\Client::loop() */
PHP_METHOD(Mosquitto_Client, loop)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
zend_long timeout = 1000, max_packets = 1;
long retval = 0;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll",
&timeout, &max_packets) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
retval = mosquitto_loop(object->client, timeout, max_packets);
php_mosquitto_handle_errno(retval, errno TSRMLS_CC);
}
/* }}} */
/* {{{ Mosquitto\Client::loopForever() */
PHP_METHOD(Mosquitto_Client, loopForever)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
zend_long timeout = 1000, max_packets = 1;
long retval = 0;
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll",
&timeout, &max_packets) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
object->looping = 1;
while (object->looping) {
retval = mosquitto_loop(object->client, timeout, max_packets);
php_mosquitto_handle_errno(retval, errno TSRMLS_CC);
if (EG(exception)) {
break;
}
}
}
/* }}} */
/* {{{ Mosquitto\Client::exitLoop() */
PHP_METHOD(Mosquitto_Client, exitLoop)
{
mosquitto_client_object *object = mosquitto_client_object_from_zend_object(Z_OBJ_P(getThis()));
PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters_none() == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}
PHP_MOSQUITTO_RESTORE_ERRORS();
php_mosquitto_exit_loop(object);
}
/* }}} */
/* Internal functions */
#if defined(PHP_WIN32)
static int strerror_r(int errnum, char *buf, size_t buf_len)
{
return strerror_s(buf, buf_len, errnum);
}
#endif
PHP_MOSQUITTO_API char *php_mosquitto_strerror_wrapper(int err)
{
char *buf = ecalloc(256, sizeof(char));
strerror_r(err, buf, 256);
if (!buf[0]) {
efree(buf);
return NULL;
}
return buf;
}
PHP_MOSQUITTO_API void php_mosquitto_exit_loop(mosquitto_client_object *object)
{
object->looping = 0;
}
static void mosquitto_client_object_destroy(zend_object *object ZO_HANDLE_DC TSRMLS_DC)
{
mosquitto_client_object *client = mosquitto_client_object_from_zend_object(object);
/* Disconnect cleanly, but disregard an error if it wasn't connected */
/* We must loop here so that the disconnect packet is sent and acknowledged */
mosquitto_disconnect(client->client);
mosquitto_loop(client->client, 100, 1);
mosquitto_destroy(client->client);
client->client = NULL;
if (MQTTG(client_key_len) > 0) {
efree(MQTTG(client_key));
MQTTG(client_key) = NULL;
}
PHP_MOSQUITTO_FREE_CALLBACK(client, connect);
PHP_MOSQUITTO_FREE_CALLBACK(client, subscribe);
PHP_MOSQUITTO_FREE_CALLBACK(client, unsubscribe);
PHP_MOSQUITTO_FREE_CALLBACK(client, publish);
PHP_MOSQUITTO_FREE_CALLBACK(client, message);
PHP_MOSQUITTO_FREE_CALLBACK(client, disconnect);
PHP_MOSQUITTO_FREE_CALLBACK(client, log);
}
static void mosquitto_client_object_free(zend_object *object TSRMLS_DC) {
mosquitto_client_object *client = mosquitto_client_object_from_zend_object(object);
#ifdef ZEND_ENGINE_3
zend_object_std_dtor(object);
#else
if (object->properties) {
zend_hash_destroy(object->properties);
FREE_HASHTABLE(object->properties);
}
efree(object);
#endif
}
#ifdef ZEND_ENGINE_3
static zend_object *mosquitto_client_object_new(zend_class_entry *ce) {
mosquitto_client_object *client = ecalloc(1, sizeof(mosquitto_client_object) + zend_object_properties_size(ce));
zend_object *ret = mosquitto_client_object_to_zend_object(client);
#ifdef MOSQUITTO_NEED_TSRMLS
client->TSRMLS_C = TSRMLS_C;
#endif
zend_object_std_init(ret, ce);
ret->handlers = &mosquitto_std_object_handlers;
return ret;
}
#else
static zend_object_value mosquitto_client_object_new(zend_class_entry *ce TSRMLS_DC) {
zend_object_value retval;
mosquitto_client_object *client;
#if PHP_VERSION_ID < 50399
zval *temp;
#endif
client = ecalloc(1, sizeof(mosquitto_client_object));
client->std.ce = ce;
client->client = NULL;
#ifdef MOSQUITTO_NEED_TSRMLS
client->TSRMLS_C = TSRMLS_C;
#endif
ALLOC_HASHTABLE(client->std.properties);
zend_hash_init(client->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
#if PHP_VERSION_ID < 50399
zend_hash_copy(client->std.properties, &ce->default_properties, (copy_ctor_func_t) zval_add_ref,(void *) &temp, sizeof(zval *));
#else
object_properties_init(&client->std, ce);
#endif
retval.handle = zend_objects_store_put(client,
(zend_objects_store_dtor_t)mosquitto_client_object_destroy,
(zend_objects_free_object_storage_t)mosquitto_client_object_free, NULL TSRMLS_CC);
retval.handlers = &mosquitto_std_object_handlers;
return retval;
}
#endif
void php_mosquitto_handle_errno(int retval, int err TSRMLS_DC) {
if (retval == MOSQ_ERR_ERRNO) {
char *message = php_mosquitto_strerror_wrapper(errno);
if (message) {
zend_throw_exception(mosquitto_ce_exception, message, 0 TSRMLS_CC);
efree(message);
}
} else if (retval != MOSQ_ERR_SUCCESS) {
const char *message = mosquitto_strerror(retval);
if (message && *message) {
zend_throw_exception(mosquitto_ce_exception, message, 0 TSRMLS_CC);
}
}
}
PHP_MOSQUITTO_API void php_mosquitto_connect_callback(struct mosquitto *mosq, void *obj, int rc)
{
mosquitto_client_object *object = (mosquitto_client_object*)obj;
#ifdef ZEND_ENGINE_3
zval params[2], retval;
#else
zval *retval_ptr = NULL, *rc_zval = NULL, *message_zval = NULL;
zval **params[2];
#endif
const char *message;
#ifdef MOSQUITTO_NEED_TSRMLS
TSRMLS_D = object->TSRMLS_C;
#endif
if (!ZEND_FCI_INITIALIZED(object->connect_callback)) {
return;
}
message = mosquitto_connack_string(rc);
#ifdef ZEND_ENGINE_3
ZVAL_LONG(¶ms[0], rc);
ZVAL_STRING(¶ms[1], message);
ZVAL_UNDEF(&retval);
object->connect_callback.retval = &retval;
#else
MAKE_STD_ZVAL(rc_zval);
ZVAL_LONG(rc_zval, rc);
params[0] = &rc_zval;
MAKE_STD_ZVAL(message_zval);
ZVAL_STRING(message_zval, message, 1);
params[1] = &message_zval;
object->connect_callback.retval_ptr_ptr = &retval_ptr;
#endif
object->connect_callback.params = params;
object->connect_callback.param_count = 2;
if (zend_call_function(&object->connect_callback, &object->connect_callback_cache TSRMLS_CC) == FAILURE) {
if (!EG(exception)) {
zend_throw_exception_ex(mosquitto_ce_exception, 0 TSRMLS_CC, "Failed to invoke connect callback %s()", mosquitto_finfo_name(&object->connect_callback));
}
}
#ifdef ZEND_ENGINE_3
zval_ptr_dtor(¶ms[0]);
zval_ptr_dtor(¶ms[1]);
zval_ptr_dtor(&retval);