Imported Upstream version 0.9.1
[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 {
75     mosq_md_in = 0,
76     mosq_md_out = 1
77 };
78
79 enum mosquitto_msg_state
80 {
81     mosq_ms_invalid = 0,
82     mosq_ms_publish_qos0 = 1,
83     mosq_ms_publish_qos1 = 2,
84     mosq_ms_wait_for_puback = 3,
85     mosq_ms_publish_qos2 = 4,
86     mosq_ms_wait_for_pubrec = 5,
87     mosq_ms_resend_pubrel = 6,
88     mosq_ms_wait_for_pubrel = 7,
89     mosq_ms_resend_pubcomp = 8,
90     mosq_ms_wait_for_pubcomp = 9,
91     mosq_ms_send_pubrec = 10,
92     mosq_ms_queued = 11
93 };
94
95 enum mosquitto_client_state
96 {
97     mosq_cs_new = 0,
98     mosq_cs_connected = 1,
99     mosq_cs_disconnecting = 2,
100     mosq_cs_connect_async = 3,
101     mosq_cs_connect_pending = 4,
102     mosq_cs_connect_srv = 5
103 };
104
105 enum _mosquitto_protocol
106 {
107     mosq_p_invalid = 0,
108     mosq_p_mqtt31 = 1,
109     mosq_p_mqtt311 = 2,
110     mosq_p_mqtts = 3
111 };
112
113 enum _mosquitto_transport
114 {
115     mosq_t_invalid = 0,
116     mosq_t_tcp = 1,
117     mosq_t_ws = 2,
118     mosq_t_sctp = 3
119 };
120
121 struct _mosquitto_packet
122 {
123     uint8_t command;
124     uint8_t have_remaining;
125     uint8_t remaining_count;
126     uint16_t mid;
127     uint32_t remaining_mult;
128     uint32_t remaining_length;
129     uint32_t packet_length;
130     uint32_t to_process;
131     uint32_t pos;
132     uint8_t *payload;
133     struct _mosquitto_packet *next;
134 };
135
136 struct mosquitto_message_all
137 {
138     struct mosquitto_message_all *next;
139     time_t timestamp;
140     //enum mosquitto_msg_direction direction;
141     enum mosquitto_msg_state state;
142     bool dup;
143     struct mosquitto_message msg;
144 };
145
146 struct mosquitto
147 {
148 #ifndef WIN32
149     int sock;
150 #  ifndef WITH_BROKER
151     int sockpairR, sockpairW;
152 #  endif
153 #else
154     SOCKET sock;
155 #  ifndef WITH_BROKER
156     SOCKET sockpairR, sockpairW;
157 #  endif
158 #endif
159     enum _mosquitto_protocol protocol;
160     char *address;
161     char *id;
162     char *username;
163     char *password;
164     uint16_t keepalive;
165     bool clean_session;
166     enum mosquitto_client_state state;
167     time_t last_msg_in;
168     time_t last_msg_out;
169     time_t ping_t;
170     uint16_t last_mid;
171     struct _mosquitto_packet in_packet;
172     struct _mosquitto_packet *current_out_packet;
173     struct _mosquitto_packet *out_packet;
174     struct mosquitto_message *will;
175 #ifdef WITH_TLS
176     SSL *ssl;
177     SSL_CTX *ssl_ctx;
178     char *tls_cafile;
179     char *tls_capath;
180     char *tls_certfile;
181     char *tls_keyfile;
182     int (*tls_pw_callback)(char *buf, int size, int rwflag, void *userdata);
183     int tls_cert_reqs;
184     char *tls_version;
185     char *tls_ciphers;
186     char *tls_psk;
187     char *tls_psk_identity;
188     bool tls_insecure;
189 #endif
190     bool want_write;
191 #if defined(WITH_THREADING) && !defined(WITH_BROKER)
192     pthread_mutex_t callback_mutex;
193     pthread_mutex_t log_callback_mutex;
194     pthread_mutex_t msgtime_mutex;
195     pthread_mutex_t out_packet_mutex;
196     pthread_mutex_t current_out_packet_mutex;
197     pthread_mutex_t state_mutex;
198     pthread_mutex_t in_message_mutex;
199     pthread_mutex_t out_message_mutex;
200     pthread_t thread_id;
201 #endif
202 #ifdef WITH_BROKER
203     bool is_bridge;
204     struct _mqtt3_bridge *bridge;
205     struct mosquitto_client_msg *msgs;
206     struct mosquitto_client_msg *last_msg;
207     int msg_count;
208     int msg_count12;
209     struct _mosquitto_acl_user *acl_list;
210     struct _mqtt3_listener *listener;
211     time_t disconnect_t;
212     int pollfd_index;
213     int db_index;
214     struct _mosquitto_packet *out_packet_last;
215     bool is_dropping;
216 #else
217     void *userdata;
218     bool in_callback;
219     unsigned int message_retry;
220     time_t last_retry_check;
221     struct mosquitto_message_all *in_messages;
222     struct mosquitto_message_all *in_messages_last;
223     struct mosquitto_message_all *out_messages;
224     struct mosquitto_message_all *out_messages_last;
225     void (*on_connect)(struct mosquitto *, void *userdata, int rc);
226     void (*on_disconnect)(struct mosquitto *, void *userdata, int rc);
227     void (*on_publish)(struct mosquitto *, void *userdata, int mid);
228     void (*on_message)(struct mosquitto *, void *userdata, const struct mosquitto_message *message);
229     void (*on_subscribe)(struct mosquitto *, void *userdata, int mid, int qos_count,
230                          const int *granted_qos);
231     void (*on_unsubscribe)(struct mosquitto *, void *userdata, int mid);
232     void (*on_log)(struct mosquitto *, void *userdata, int level, const char *str);
233     //void (*on_error)();
234     char *host;
235     int port;
236     int in_queue_len;
237     int out_queue_len;
238     char *bind_address;
239     unsigned int reconnect_delay;
240     unsigned int reconnect_delay_max;
241     bool reconnect_exponential_backoff;
242     bool threaded;
243     struct _mosquitto_packet *out_packet_last;
244     int inflight_messages;
245     int max_inflight_messages;
246 #  ifdef WITH_SRV
247     ares_channel achan;
248 #  endif
249 #endif
250 };
251
252 #endif