iotivity 0.9.0
[platform/upstream/iotivity.git] / service / protocol-plugin / plugins / mqtt-light / lib / mosquitto_internal.h
1 /*
2 Copyright (c) 2010,2011,2013 Roger Light <roger@atchoo.org>
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice,
9    this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. Neither the name of mosquitto nor the names of its
14    contributors may be used to endorse or promote products derived from
15    this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef _MOSQUITTO_INTERNAL_H_
31 #define _MOSQUITTO_INTERNAL_H_
32
33 #include "config.h"
34
35 #ifdef WIN32
36 #  include <winsock2.h>
37 #endif
38
39 #ifdef WITH_TLS
40 #include <openssl/ssl.h>
41 #endif
42 #include <stdlib.h>
43
44 #if defined(WITH_THREADING) && !defined(WITH_BROKER)
45 #  include <pthread.h>
46 #else
47 #  include <dummypthread.h>
48 #endif
49
50 #ifdef WITH_SRV
51 #  include <ares.h>
52 #endif
53
54 #ifdef WIN32
55 #       if _MSC_VER < 1600
56                 typedef unsigned char uint8_t;
57                 typedef unsigned short uint16_t;
58                 typedef unsigned int uint32_t;
59                 typedef unsigned long long uint64_t;
60 #       else
61 #               include <stdint.h>
62 #       endif
63 #else
64 #       include <stdint.h>
65 #endif
66
67 #include "mosquitto.h"
68 #include "time_mosq.h"
69 #ifdef WITH_BROKER
70 struct mosquitto_client_msg;
71 #endif
72
73 enum mosquitto_msg_direction {
74         mosq_md_in = 0,
75         mosq_md_out = 1
76 };
77
78 enum mosquitto_msg_state {
79         mosq_ms_invalid = 0,
80         mosq_ms_publish_qos0 = 1,
81         mosq_ms_publish_qos1 = 2,
82         mosq_ms_wait_for_puback = 3,
83         mosq_ms_publish_qos2 = 4,
84         mosq_ms_wait_for_pubrec = 5,
85         mosq_ms_resend_pubrel = 6,
86         mosq_ms_wait_for_pubrel = 7,
87         mosq_ms_resend_pubcomp = 8,
88         mosq_ms_wait_for_pubcomp = 9,
89         mosq_ms_send_pubrec = 10,
90         mosq_ms_queued = 11
91 };
92
93 enum mosquitto_client_state {
94         mosq_cs_new = 0,
95         mosq_cs_connected = 1,
96         mosq_cs_disconnecting = 2,
97         mosq_cs_connect_async = 3,
98         mosq_cs_connect_pending = 4,
99         mosq_cs_connect_srv = 5
100 };
101
102 enum _mosquitto_protocol {
103         mosq_p_invalid = 0,
104         mosq_p_mqtt31 = 1,
105         mosq_p_mqtt311 = 2,
106         mosq_p_mqtts = 3
107 };
108
109 enum _mosquitto_transport {
110         mosq_t_invalid = 0,
111         mosq_t_tcp = 1,
112         mosq_t_ws = 2,
113         mosq_t_sctp = 3
114 };
115
116 struct _mosquitto_packet{
117         uint8_t command;
118         uint8_t have_remaining;
119         uint8_t remaining_count;
120         uint16_t mid;
121         uint32_t remaining_mult;
122         uint32_t remaining_length;
123         uint32_t packet_length;
124         uint32_t to_process;
125         uint32_t pos;
126         uint8_t *payload;
127         struct _mosquitto_packet *next;
128 };
129
130 struct mosquitto_message_all{
131         struct mosquitto_message_all *next;
132         time_t timestamp;
133         //enum mosquitto_msg_direction direction;
134         enum mosquitto_msg_state state;
135         bool dup;
136         struct mosquitto_message msg;
137 };
138
139 struct mosquitto {
140 #ifndef WIN32
141         int sock;
142 #  ifndef WITH_BROKER
143         int sockpairR, sockpairW;
144 #  endif
145 #else
146         SOCKET sock;
147 #  ifndef WITH_BROKER
148         SOCKET sockpairR, sockpairW;
149 #  endif
150 #endif
151         enum _mosquitto_protocol protocol;
152         char *address;
153         char *id;
154         char *username;
155         char *password;
156         uint16_t keepalive;
157         bool clean_session;
158         enum mosquitto_client_state state;
159         time_t last_msg_in;
160         time_t last_msg_out;
161         time_t ping_t;
162         uint16_t last_mid;
163         struct _mosquitto_packet in_packet;
164         struct _mosquitto_packet *current_out_packet;
165         struct _mosquitto_packet *out_packet;
166         struct mosquitto_message *will;
167 #ifdef WITH_TLS
168         SSL *ssl;
169         SSL_CTX *ssl_ctx;
170         char *tls_cafile;
171         char *tls_capath;
172         char *tls_certfile;
173         char *tls_keyfile;
174         int (*tls_pw_callback)(char *buf, int size, int rwflag, void *userdata);
175         int tls_cert_reqs;
176         char *tls_version;
177         char *tls_ciphers;
178         char *tls_psk;
179         char *tls_psk_identity;
180         bool tls_insecure;
181 #endif
182         bool want_write;
183 #if defined(WITH_THREADING) && !defined(WITH_BROKER)
184         pthread_mutex_t callback_mutex;
185         pthread_mutex_t log_callback_mutex;
186         pthread_mutex_t msgtime_mutex;
187         pthread_mutex_t out_packet_mutex;
188         pthread_mutex_t current_out_packet_mutex;
189         pthread_mutex_t state_mutex;
190         pthread_mutex_t in_message_mutex;
191         pthread_mutex_t out_message_mutex;
192         pthread_t thread_id;
193 #endif
194 #ifdef WITH_BROKER
195         bool is_bridge;
196         struct _mqtt3_bridge *bridge;
197         struct mosquitto_client_msg *msgs;
198         struct mosquitto_client_msg *last_msg;
199         int msg_count;
200         int msg_count12;
201         struct _mosquitto_acl_user *acl_list;
202         struct _mqtt3_listener *listener;
203         time_t disconnect_t;
204         int pollfd_index;
205         int db_index;
206         struct _mosquitto_packet *out_packet_last;
207         bool is_dropping;
208 #else
209         void *userdata;
210         bool in_callback;
211         unsigned int message_retry;
212         time_t last_retry_check;
213         struct mosquitto_message_all *in_messages;
214         struct mosquitto_message_all *in_messages_last;
215         struct mosquitto_message_all *out_messages;
216         struct mosquitto_message_all *out_messages_last;
217         void (*on_connect)(struct mosquitto *, void *userdata, int rc);
218         void (*on_disconnect)(struct mosquitto *, void *userdata, int rc);
219         void (*on_publish)(struct mosquitto *, void *userdata, int mid);
220         void (*on_message)(struct mosquitto *, void *userdata, const struct mosquitto_message *message);
221         void (*on_subscribe)(struct mosquitto *, void *userdata, int mid, int qos_count, const int *granted_qos);
222         void (*on_unsubscribe)(struct mosquitto *, void *userdata, int mid);
223         void (*on_log)(struct mosquitto *, void *userdata, int level, const char *str);
224         //void (*on_error)();
225         char *host;
226         int port;
227         int in_queue_len;
228         int out_queue_len;
229         char *bind_address;
230         unsigned int reconnect_delay;
231         unsigned int reconnect_delay_max;
232         bool reconnect_exponential_backoff;
233         bool threaded;
234         struct _mosquitto_packet *out_packet_last;
235         int inflight_messages;
236         int max_inflight_messages;
237 #  ifdef WITH_SRV
238         ares_channel achan;
239 #  endif
240 #endif
241 };
242
243 #endif