mosquitto/lib/mosquitto_internal.h

363 lines
9.2 KiB
C
Raw Normal View History

2014-05-07 22:27:00 +00:00
/*
Copyright (c) 2010-2020 Roger Light <roger@atchoo.org>
2014-05-07 22:27:00 +00:00
All rights reserved. This program and the accompanying materials
2020-11-25 17:34:21 +00:00
are made available under the terms of the Eclipse Public License 2.0
2014-05-07 22:27:00 +00:00
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
2020-11-25 17:34:21 +00:00
https://www.eclipse.org/legal/epl-2.0/
2014-05-07 22:27:00 +00:00
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
Contributors:
Roger Light - initial implementation and documentation.
Tatsuzo Osawa - Add epoll.
2014-05-07 22:27:00 +00:00
*/
#ifndef MOSQUITTO_INTERNAL_H
#define MOSQUITTO_INTERNAL_H
2014-05-07 22:27:00 +00:00
2015-04-29 20:37:47 +00:00
#include "config.h"
2014-05-07 22:27:00 +00:00
#ifdef WIN32
# include <winsock2.h>
#endif
#ifdef WITH_TLS
# include <openssl/ssl.h>
#else
# include <time.h>
2014-05-07 22:27:00 +00:00
#endif
#include <stdlib.h>
#if defined(WITH_THREADING) && !defined(WITH_BROKER)
# include <pthread.h>
#else
# include <dummypthread.h>
#endif
#ifdef WITH_SRV
# include <ares.h>
#endif
#ifdef WIN32
# if _MSC_VER < 1600
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
# else
# include <stdint.h>
# endif
#else
# include <stdint.h>
#endif
#include "mosquitto.h"
#include "time_mosq.h"
#ifdef WITH_BROKER
2017-02-06 22:39:39 +00:00
# ifdef __linux__
# include <netdb.h>
# endif
# include "uthash.h"
2014-05-07 22:27:00 +00:00
struct mosquitto_client_msg;
#endif
#ifdef WIN32
typedef SOCKET mosq_sock_t;
#else
typedef int mosq_sock_t;
#endif
2014-05-07 22:27:00 +00:00
enum mosquitto_msg_direction {
mosq_md_in = 0,
mosq_md_out = 1
};
enum mosquitto_msg_state {
mosq_ms_invalid = 0,
mosq_ms_publish_qos0 = 1,
mosq_ms_publish_qos1 = 2,
mosq_ms_wait_for_puback = 3,
mosq_ms_publish_qos2 = 4,
mosq_ms_wait_for_pubrec = 5,
mosq_ms_resend_pubrel = 6,
mosq_ms_wait_for_pubrel = 7,
mosq_ms_resend_pubcomp = 8,
mosq_ms_wait_for_pubcomp = 9,
mosq_ms_send_pubrec = 10,
mosq_ms_queued = 11
};
enum mosquitto_client_state {
mosq_cs_new = 0,
mosq_cs_connected = 1,
mosq_cs_disconnecting = 2,
mosq_cs_active = 3,
2014-05-07 22:27:00 +00:00
mosq_cs_connect_pending = 4,
2014-06-29 22:16:10 +00:00
mosq_cs_connect_srv = 5,
2014-07-08 22:16:34 +00:00
mosq_cs_disconnect_ws = 6,
mosq_cs_disconnected = 7,
mosq_cs_socks5_new = 8,
mosq_cs_socks5_start = 9,
mosq_cs_socks5_request = 10,
mosq_cs_socks5_reply = 11,
mosq_cs_socks5_auth_ok = 12,
mosq_cs_socks5_userpass_reply = 13,
mosq_cs_socks5_send_userpass = 14,
mosq_cs_expiring = 15,
mosq_cs_duplicate = 17, /* client that has been taken over by another with the same id */
2019-02-27 09:22:00 +00:00
mosq_cs_disconnect_with_will = 18,
2019-03-12 22:47:48 +00:00
mosq_cs_disused = 19, /* client that has been added to the disused list to be freed */
mosq_cs_authenticating = 20, /* Client has sent CONNECT but is still undergoing extended authentication */
mosq_cs_reauthenticating = 21, /* Client is undergoing reauthentication and shouldn't do anything else until complete */
2014-05-07 22:27:00 +00:00
};
enum mosquitto__protocol {
2014-05-07 22:27:00 +00:00
mosq_p_invalid = 0,
mosq_p_mqtt31 = 1,
mosq_p_mqtt311 = 2,
mosq_p_mqtts = 3,
mosq_p_mqtt5 = 5,
2014-05-07 22:27:00 +00:00
};
enum mosquitto__threaded_state {
mosq_ts_none, /* No threads in use */
mosq_ts_self, /* Threads started by libmosquitto */
mosq_ts_external /* Threads started by external code */
};
enum mosquitto__transport {
2014-05-07 22:27:00 +00:00
mosq_t_invalid = 0,
mosq_t_tcp = 1,
mosq_t_ws = 2,
mosq_t_sctp = 3
};
2019-01-18 21:30:34 +00:00
struct mosquitto__alias{
char *topic;
uint16_t alias;
};
struct session_expiry_list {
struct mosquitto *context;
struct session_expiry_list *prev;
struct session_expiry_list *next;
};
struct mosquitto__packet{
uint8_t *payload;
struct mosquitto__packet *next;
2014-05-07 22:27:00 +00:00
uint32_t remaining_mult;
uint32_t remaining_length;
uint32_t packet_length;
uint32_t to_process;
uint32_t pos;
uint16_t mid;
uint8_t command;
int8_t remaining_count;
2014-05-07 22:27:00 +00:00
};
struct mosquitto_message_all{
struct mosquitto_message_all *next;
struct mosquitto_message_all *prev;
mosquitto_property *properties;
2014-05-07 22:27:00 +00:00
time_t timestamp;
enum mosquitto_msg_state state;
bool dup;
struct mosquitto_message msg;
uint32_t expiry_interval;
2014-05-07 22:27:00 +00:00
};
#ifdef WITH_TLS
2019-02-26 17:11:29 +00:00
enum mosquitto__keyform {
mosq_k_pem = 0,
mosq_k_engine = 1,
};
#endif
struct will_delay_list {
struct mosquitto *context;
struct will_delay_list *prev;
struct will_delay_list *next;
};
struct mosquitto_msg_data{
#ifdef WITH_BROKER
struct mosquitto_client_msg *inflight;
struct mosquitto_client_msg *queued;
unsigned long msg_bytes;
unsigned long msg_bytes12;
int msg_count;
int msg_count12;
#else
struct mosquitto_message_all *inflight;
int queue_len;
# ifdef WITH_THREADING
pthread_mutex_t mutex;
# endif
#endif
int inflight_quota;
uint16_t inflight_maximum;
};
2014-05-07 22:27:00 +00:00
struct mosquitto {
#if defined(WITH_BROKER) && defined(WITH_EPOLL)
/* This *must* be the first element in the struct. */
int ident;
#endif
mosq_sock_t sock;
#ifndef WITH_BROKER
mosq_sock_t sockpairR, sockpairW;
2017-02-06 22:39:39 +00:00
#endif
#if defined(__GLIBC__) && defined(WITH_ADNS)
2017-02-06 22:39:39 +00:00
struct gaicb *adns; /* For getaddrinfo_a */
2014-05-07 22:27:00 +00:00
#endif
enum mosquitto__protocol protocol;
2014-05-07 22:27:00 +00:00
char *address;
char *id;
char *username;
char *password;
uint16_t keepalive;
2015-02-13 21:17:11 +00:00
uint16_t last_mid;
2014-05-07 22:27:00 +00:00
enum mosquitto_client_state state;
time_t last_msg_in;
time_t next_msg_out;
2014-05-07 22:27:00 +00:00
time_t ping_t;
struct mosquitto__packet in_packet;
struct mosquitto__packet *current_out_packet;
struct mosquitto__packet *out_packet;
2018-10-24 13:07:09 +00:00
struct mosquitto_message_all *will;
2019-01-18 21:30:34 +00:00
struct mosquitto__alias *aliases;
struct will_delay_list *will_delay_entry;
2019-01-18 21:30:34 +00:00
int alias_count;
uint32_t maximum_packet_size;
2019-02-25 22:28:51 +00:00
uint32_t will_delay_interval;
time_t will_delay_time;
2014-05-07 22:27:00 +00:00
#ifdef WITH_TLS
SSL *ssl;
SSL_CTX *ssl_ctx;
char *tls_cafile;
char *tls_capath;
char *tls_certfile;
char *tls_keyfile;
int (*tls_pw_callback)(char *buf, int size, int rwflag, void *userdata);
char *tls_version;
char *tls_ciphers;
char *tls_psk;
char *tls_psk_identity;
int tls_cert_reqs;
2014-05-07 22:27:00 +00:00
bool tls_insecure;
bool ssl_ctx_defaults;
bool tls_ocsp_required;
char *tls_engine;
2019-02-26 17:11:29 +00:00
char *tls_engine_kpass_sha1;
enum mosquitto__keyform tls_keyform;
char *tls_alpn;
2014-05-07 22:27:00 +00:00
#endif
bool want_write;
bool want_connect;
2014-05-07 22:27:00 +00:00
#if defined(WITH_THREADING) && !defined(WITH_BROKER)
pthread_mutex_t callback_mutex;
pthread_mutex_t log_callback_mutex;
pthread_mutex_t msgtime_mutex;
pthread_mutex_t out_packet_mutex;
pthread_mutex_t current_out_packet_mutex;
pthread_mutex_t state_mutex;
pthread_mutex_t mid_mutex;
2014-05-07 22:27:00 +00:00
pthread_t thread_id;
#endif
bool clean_start;
uint32_t session_expiry_interval;
time_t session_expiry_time;
2014-05-07 22:27:00 +00:00
#ifdef WITH_BROKER
bool removed_from_by_id; /* True if removed from by_id hash */
bool is_dropping;
2014-05-07 22:27:00 +00:00
bool is_bridge;
2015-05-16 18:03:12 +00:00
struct mosquitto__bridge *bridge;
struct mosquitto_msg_data msgs_in;
struct mosquitto_msg_data msgs_out;
struct mosquitto__acl_user *acl_list;
2015-05-16 18:03:12 +00:00
struct mosquitto__listener *listener;
struct mosquitto__packet *out_packet_last;
struct mosquitto__subhier **subs;
2019-03-13 16:51:06 +00:00
struct mosquitto__subshared_ref **shared_subs;
char *auth_method;
int sub_count;
2019-03-13 16:51:06 +00:00
int shared_sub_count;
int pollfd_index;
# ifdef WITH_WEBSOCKETS
2015-12-19 01:21:17 +00:00
# if defined(LWS_LIBRARY_VERSION_NUMBER)
struct lws *wsi;
# else
struct libwebsocket_context *ws_context;
struct libwebsocket *wsi;
2015-12-19 01:21:17 +00:00
# endif
# endif
bool ws_want_write;
bool assigned_id;
2014-05-07 22:27:00 +00:00
#else
# ifdef WITH_SOCKS
char *socks5_host;
2020-10-17 00:23:08 +00:00
uint16_t socks5_port;
char *socks5_username;
char *socks5_password;
# endif
2014-05-07 22:27:00 +00:00
void *userdata;
bool in_callback;
struct mosquitto_msg_data msgs_in;
struct mosquitto_msg_data msgs_out;
2014-05-07 22:27:00 +00:00
void (*on_connect)(struct mosquitto *, void *userdata, int rc);
void (*on_connect_with_flags)(struct mosquitto *, void *userdata, int rc, int flags);
2018-11-20 14:36:18 +00:00
void (*on_connect_v5)(struct mosquitto *, void *userdata, int rc, int flags, const mosquitto_property *props);
2014-05-07 22:27:00 +00:00
void (*on_disconnect)(struct mosquitto *, void *userdata, int rc);
2018-11-20 14:36:18 +00:00
void (*on_disconnect_v5)(struct mosquitto *, void *userdata, int rc, const mosquitto_property *props);
2014-05-07 22:27:00 +00:00
void (*on_publish)(struct mosquitto *, void *userdata, int mid);
void (*on_publish_v5)(struct mosquitto *, void *userdata, int mid, int reason_code, const mosquitto_property *props);
2014-05-07 22:27:00 +00:00
void (*on_message)(struct mosquitto *, void *userdata, const struct mosquitto_message *message);
2018-11-20 14:36:18 +00:00
void (*on_message_v5)(struct mosquitto *, void *userdata, const struct mosquitto_message *message, const mosquitto_property *props);
2014-05-07 22:27:00 +00:00
void (*on_subscribe)(struct mosquitto *, void *userdata, int mid, int qos_count, const int *granted_qos);
2018-11-20 14:36:18 +00:00
void (*on_subscribe_v5)(struct mosquitto *, void *userdata, int mid, int qos_count, const int *granted_qos, const mosquitto_property *props);
2014-05-07 22:27:00 +00:00
void (*on_unsubscribe)(struct mosquitto *, void *userdata, int mid);
2018-11-20 14:36:18 +00:00
void (*on_unsubscribe_v5)(struct mosquitto *, void *userdata, int mid, const mosquitto_property *props);
2014-05-07 22:27:00 +00:00
void (*on_log)(struct mosquitto *, void *userdata, int level, const char *str);
/*void (*on_error)();*/
2014-05-07 22:27:00 +00:00
char *host;
2020-10-17 00:23:08 +00:00
uint16_t port;
2014-05-07 22:27:00 +00:00
char *bind_address;
unsigned int reconnects;
2014-05-07 22:27:00 +00:00
unsigned int reconnect_delay;
unsigned int reconnect_delay_max;
bool reconnect_exponential_backoff;
char threaded;
struct mosquitto__packet *out_packet_last;
mosquitto_property *connect_properties;
2014-05-07 22:27:00 +00:00
# ifdef WITH_SRV
ares_channel achan;
# endif
#endif
uint8_t maximum_qos;
uint8_t retain_available;
bool tcp_nodelay;
#ifdef WITH_BROKER
UT_hash_handle hh_id;
UT_hash_handle hh_sock;
struct mosquitto *for_free_next;
struct session_expiry_list *expiry_list_item;
#endif
#ifdef WITH_EPOLL
uint32_t events;
#endif
2014-05-07 22:27:00 +00:00
};
#define STREMPTY(str) (str[0] == '\0')
void do_client_disconnect(struct mosquitto *mosq, int reason_code, const mosquitto_property *properties);
2014-05-07 22:27:00 +00:00
#endif