mosquitto/src/websockets.c

549 lines
14 KiB
C
Raw Normal View History

/*
Copyright (c) 2014 Roger Light <roger@atchoo.org>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of mosquitto nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
2014-05-26 18:16:59 +00:00
#ifdef WITH_WEBSOCKETS
#include <libwebsockets.h>
#include "mosquitto_internal.h"
#include "mosquitto_broker.h"
#include "mqtt3_protocol.h"
#include "memory_mosq.h"
#include <stdlib.h>
2014-09-08 22:25:02 +00:00
#include <errno.h>
#include <sys/stat.h>
#ifdef WITH_SYS_TREE
extern uint64_t g_bytes_received;
extern uint64_t g_bytes_sent;
extern unsigned long g_msgs_received;
extern unsigned long g_msgs_sent;
extern unsigned long g_pub_msgs_received;
extern unsigned long g_pub_msgs_sent;
#endif
extern struct mosquitto_db int_db;
static int callback_mqtt(struct libwebsocket_context *context,
struct libwebsocket *wsi,
enum libwebsocket_callback_reasons reason,
void *user,
void *in,
size_t len);
static int callback_http(struct libwebsocket_context *context,
struct libwebsocket *wsi,
enum libwebsocket_callback_reasons reason,
void *user,
void *in,
size_t len);
enum mosq_ws_protocols {
PROTOCOL_HTTP = 0,
PROTOCOL_MQTT,
DEMO_PROTOCOL_COUNT
};
struct libws_http_data {
2014-09-08 22:25:02 +00:00
FILE *fptr;
};
static struct libwebsocket_protocols protocols[] = {
/* first protocol must always be HTTP handler */
{
"http-only",
callback_http,
sizeof (struct libws_http_data),
0,
0,
NULL,
0
},
{
"mqtt",
callback_mqtt,
sizeof(struct libws_mqtt_data),
0,
1,
NULL,
0
},
{
"mqttv3.1",
callback_mqtt,
sizeof(struct libws_mqtt_data),
0,
1,
NULL,
0
},
{ NULL, NULL, 0, 0, 0, NULL, 0}
};
2014-09-17 21:19:45 +00:00
static void easy_address(int sock, struct mosquitto *mosq)
{
char address[1024];
if(!_mosquitto_socket_get_address(sock, address, 1024)){
mosq->address = _mosquitto_strdup(address);
}
}
static int callback_mqtt(struct libwebsocket_context *context,
struct libwebsocket *wsi,
enum libwebsocket_callback_reasons reason,
void *user,
void *in,
size_t len)
{
struct mosquitto_db *db;
struct mosquitto *mosq = NULL;
struct _mosquitto_packet *packet;
int count;
struct libws_mqtt_data *u = (struct libws_mqtt_data *)user;
size_t pos;
uint8_t *buf;
int rc;
uint8_t byte;
db = &int_db;
switch (reason) {
case LWS_CALLBACK_ESTABLISHED:
2014-06-29 22:16:10 +00:00
mosq = mqtt3_context_init(db, WEBSOCKET_CLIENT);
if(mosq){
mosq->ws_context = context;
mosq->wsi = wsi;
u->mosq = mosq;
2014-07-03 20:55:25 +00:00
}else{
return -1;
}
2014-09-17 21:19:45 +00:00
easy_address(libwebsocket_get_socket_fd(wsi), mosq);
if(!mosq->address){
/* getpeername and inet_ntop failed and not a bridge */
_mosquitto_free(mosq);
u->mosq = NULL;
return -1;
}
break;
case LWS_CALLBACK_CLOSED:
2014-07-03 20:55:25 +00:00
if(!u){
return -1;
}
mosq = u->mosq;
2014-06-30 22:30:43 +00:00
if(mosq){
mosq->wsi = NULL;
2014-07-03 00:00:57 +00:00
do_disconnect(db, mosq);
}
break;
case LWS_CALLBACK_SERVER_WRITEABLE:
2014-07-03 20:55:25 +00:00
if(!u){
return -1;
}
mosq = u->mosq;
2014-06-30 22:30:43 +00:00
if(!mosq || mosq->state == mosq_cs_disconnect_ws || mosq->state == mosq_cs_disconnecting){
return -1;
}
if(mosq->out_packet && !mosq->current_out_packet){
mosq->current_out_packet = mosq->out_packet;
mosq->out_packet = mosq->out_packet->next;
if(!mosq->out_packet){
mosq->out_packet_last = NULL;
}
}
while(mosq->current_out_packet && !lws_send_pipe_choked(mosq->wsi)){
packet = mosq->current_out_packet;
if(packet->pos == 0 && packet->to_process == packet->packet_length){
/* First time this packet has been dealt with.
* libwebsockets requires that the payload has
* LWS_SEND_BUFFER_PRE_PADDING space available before the
* actual data and LWS_SEND_BUFFER_POST_PADDING afterwards.
* We've already made the payload big enough to allow this,
* but need to move it into position here. */
memmove(&packet->payload[LWS_SEND_BUFFER_PRE_PADDING], packet->payload, packet->packet_length);
packet->pos += LWS_SEND_BUFFER_PRE_PADDING;
}
count = libwebsocket_write(wsi, &packet->payload[packet->pos], packet->to_process, LWS_WRITE_BINARY);
if(count < 0){
return 0;
}
packet->to_process -= count;
packet->pos += count;
if(packet->to_process > 0){
break;
}
/* Free data and reset values */
mosq->current_out_packet = mosq->out_packet;
if(mosq->out_packet){
mosq->out_packet = mosq->out_packet->next;
if(!mosq->out_packet){
mosq->out_packet_last = NULL;
}
}
_mosquitto_packet_cleanup(packet);
_mosquitto_free(packet);
mosq->last_msg_out = mosquitto_time();
}
if(mosq->current_out_packet){
libwebsocket_callback_on_writable(mosq->ws_context, mosq->wsi);
}
break;
case LWS_CALLBACK_RECEIVE:
2014-09-22 23:35:51 +00:00
if(!u || !u->mosq){
2014-07-03 20:55:25 +00:00
return -1;
}
mosq = u->mosq;
pos = 0;
buf = (uint8_t *)in;
#ifdef WITH_SYS_TREE
g_bytes_received += len;
#endif
while(pos < len){
if(!mosq->in_packet.command){
mosq->in_packet.command = buf[pos];
pos++;
/* Clients must send CONNECT as their first command. */
if(mosq->state == mosq_cs_new && (mosq->in_packet.command&0xF0) != CONNECT){
return -1;
}
}
2015-02-13 21:17:11 +00:00
if(mosq->in_packet.remaining_count == 0){
do{
if(pos == len){
2014-05-18 21:45:09 +00:00
return 0;
}
byte = buf[pos];
pos++;
mosq->in_packet.remaining_count++;
/* Max 4 bytes length for remaining length as defined by protocol.
* Anything more likely means a broken/malicious client.
*/
if(mosq->in_packet.remaining_count > 4){
return -1;
}
mosq->in_packet.remaining_length += (byte & 127) * mosq->in_packet.remaining_mult;
mosq->in_packet.remaining_mult *= 128;
}while((byte & 128) != 0);
if(mosq->in_packet.remaining_length > 0){
mosq->in_packet.payload = _mosquitto_malloc(mosq->in_packet.remaining_length*sizeof(uint8_t));
2014-06-30 22:30:43 +00:00
if(!mosq->in_packet.payload){
return -1;
}
mosq->in_packet.to_process = mosq->in_packet.remaining_length;
}
}
while(mosq->in_packet.to_process>0){
if(len - pos >= mosq->in_packet.to_process){
memcpy(&mosq->in_packet.payload[mosq->in_packet.pos], &buf[pos], mosq->in_packet.to_process);
mosq->in_packet.pos += mosq->in_packet.to_process;
pos += mosq->in_packet.to_process;
mosq->in_packet.to_process = 0;
}else{
memcpy(&mosq->in_packet.payload[mosq->in_packet.pos], &buf[pos], len-pos);
mosq->in_packet.pos += len-pos;
mosq->in_packet.to_process -= len-pos;
break;
}
}
/* All data for this packet is read. */
mosq->in_packet.pos = 0;
#ifdef WITH_SYS_TREE
g_msgs_received++;
if(((mosq->in_packet.command)&0xF5) == PUBLISH){
g_pub_msgs_received++;
}
#endif
rc = mqtt3_packet_handle(db, mosq);
/* Free data and reset values */
_mosquitto_packet_cleanup(&mosq->in_packet);
mosq->last_msg_in = mosquitto_time();
if(rc){
2014-07-08 22:18:11 +00:00
do_disconnect(db, mosq);
return -1;
}
}
break;
default:
break;
}
return 0;
}
static int callback_http(struct libwebsocket_context *context,
struct libwebsocket *wsi,
enum libwebsocket_callback_reasons reason,
void *user,
void *in,
size_t len)
{
2014-09-08 22:25:02 +00:00
struct libws_http_data *u = (struct libws_http_data *)user;
2014-09-08 17:39:51 +00:00
struct libws_mqtt_hack *hack;
char *http_dir;
2014-09-08 22:25:02 +00:00
size_t buflen, slen, wlen;
char *filename, *filename_canonical;
unsigned char buf[4096];
struct stat filestat;
2014-09-08 17:39:51 +00:00
2014-06-03 01:10:55 +00:00
/* FIXME - ssl cert verification is done here. */
2014-09-08 17:39:51 +00:00
switch (reason) {
case LWS_CALLBACK_HTTP:
hack = (struct libws_mqtt_hack *)libwebsocket_context_user(context);
if(!hack){
return -1;
}
http_dir = hack->http_dir;
if(!http_dir){
/* http disabled */
return -1;
}
/* Forbid POST */
if(lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI)){
2014-09-08 22:25:02 +00:00
libwebsockets_return_http_status(context, wsi, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL);
return -1;
}
if(!strcmp((char *)in, "/")){
slen = strlen(http_dir) + strlen("/index.html") + 2;
}else{
slen = strlen(http_dir) + strlen((char *)in) + 2;
}
filename = _mosquitto_malloc(slen);
if(!filename){
libwebsockets_return_http_status(context, wsi, HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
return -1;
}
if(!strcmp((char *)in, "/")){
snprintf(filename, slen, "%s/index.html", http_dir);
}else{
snprintf(filename, slen, "%s%s", http_dir, (char *)in);
}
/* Get canonical path and check it is within our http_dir */
#ifdef WIN32
filename_canonical = _fullpath(NULL, filename, 0);
if(!filename_canonical){
_mosquitto_free(filename);
libwebsockets_return_http_status(context, wsi, HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
return -1;
}
2014-09-08 22:25:02 +00:00
#else
filename_canonical = realpath(filename, NULL);
if(!filename_canonical){
_mosquitto_free(filename);
if(errno == EACCES){
libwebsockets_return_http_status(context, wsi, HTTP_STATUS_FORBIDDEN, NULL);
}else if(errno == EINVAL || errno == EIO || errno == ELOOP){
libwebsockets_return_http_status(context, wsi, HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
}else if(errno == ENAMETOOLONG){
libwebsockets_return_http_status(context, wsi, HTTP_STATUS_REQ_URI_TOO_LONG, NULL);
}else if(errno == ENOENT || errno == ENOTDIR){
libwebsockets_return_http_status(context, wsi, HTTP_STATUS_NOT_FOUND, NULL);
}
return -1;
}
#endif
2014-09-08 22:25:02 +00:00
if(strncmp(http_dir, filename_canonical, strlen(http_dir))){
/* Requested file isn't within http_dir, deny access. */
free(filename_canonical);
_mosquitto_free(filename);
2014-09-08 17:39:51 +00:00
libwebsockets_return_http_status(context, wsi, HTTP_STATUS_FORBIDDEN, NULL);
return -1;
}
2014-09-08 22:25:02 +00:00
free(filename_canonical);
2014-09-08 17:39:51 +00:00
2014-09-08 22:25:02 +00:00
_mosquitto_log_printf(NULL, MOSQ_LOG_DEBUG, "http serving file \"%s\".", filename);
u->fptr = fopen(filename, "rb");
_mosquitto_free(filename);
if(!u->fptr){
libwebsockets_return_http_status(context, wsi, HTTP_STATUS_NOT_FOUND, NULL);
return -1;
}
if(fstat(fileno(u->fptr), &filestat) < 0){
libwebsockets_return_http_status(context, wsi, HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
fclose(u->fptr);
return -1;
}
2014-09-19 09:43:14 +00:00
#ifdef WIN32
if((filestat.st_mode & S_IFREG) != S_IFREG){
#else
2014-09-08 22:25:02 +00:00
if(!S_ISREG(filestat.st_mode)){
2014-09-19 09:43:14 +00:00
#endif
2014-09-08 22:25:02 +00:00
libwebsockets_return_http_status(context, wsi, HTTP_STATUS_FORBIDDEN, NULL);
return -1;
}
buflen = snprintf((char *)buf, 4096, "HTTP/1.0 200 OK\r\n"
"Server: mosquitto\r\n"
"Content-Length: %u\r\n\r\n",
(unsigned int)filestat.st_size);
if(libwebsocket_write(wsi, buf, buflen, LWS_WRITE_HTTP) < 0){
fclose(u->fptr);
return -1;
}
libwebsocket_callback_on_writable(context, wsi);
2014-09-08 17:39:51 +00:00
break;
case LWS_CALLBACK_HTTP_BODY:
/* For extra POST data? */
return -1;
case LWS_CALLBACK_HTTP_BODY_COMPLETION:
/* For end of extra POST data? */
return -1;
case LWS_CALLBACK_FILTER_HTTP_CONNECTION:
/* Access control here */
return 0;
case LWS_CALLBACK_HTTP_WRITEABLE:
/* Send our data here */
2014-09-08 22:25:02 +00:00
if(u->fptr){
do{
buflen = fread(buf, 1, sizeof(buf), u->fptr);
if(buflen < 1){
fclose(u->fptr);
return -1;
}
wlen = libwebsocket_write(wsi, buf, buflen, LWS_WRITE_HTTP);
if(wlen < buflen){
if(fseek(u->fptr, buflen-wlen, SEEK_CUR) < 0){
fclose(u->fptr);
u->fptr = NULL;
return -1;
}
2014-09-08 22:25:02 +00:00
}else{
if(buflen < sizeof(buf)){
fclose(u->fptr);
u->fptr = NULL;
}
}
}while(u->fptr && !lws_send_pipe_choked(wsi));
libwebsocket_callback_on_writable(context, wsi);
}else{
return -1;
2014-09-08 22:25:02 +00:00
}
2014-09-08 17:39:51 +00:00
default:
return 0;
}
return 0;
}
2014-06-03 01:10:55 +00:00
struct libwebsocket_context *mosq_websockets_init(struct _mqtt3_listener *listener)
{
struct lws_context_creation_info info;
struct libwebsocket_protocols *p;
int protocol_count;
int i;
2014-09-08 17:21:53 +00:00
struct libws_mqtt_hack *user;
/* Count valid protocols */
for(protocol_count=0; protocols[protocol_count].name; protocol_count++);
p = _mosquitto_calloc(protocol_count+1, sizeof(struct libwebsocket_protocols));
if(!p){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Out of memory.");
return NULL;
}
for(i=0; protocols[i].name; i++){
p[i].name = protocols[i].name;
p[i].callback = protocols[i].callback;
p[i].per_session_data_size = protocols[i].per_session_data_size;
p[i].rx_buffer_size = protocols[i].rx_buffer_size;
}
memset(&info, 0, sizeof(info));
2014-06-03 01:10:55 +00:00
info.port = listener->port;
info.protocols = p;
info.gid = -1;
info.uid = -1;
2014-06-03 01:10:55 +00:00
#ifdef WITH_TLS
info.ssl_ca_filepath = listener->cafile;
info.ssl_cert_filepath = listener->certfile;
info.ssl_private_key_filepath = listener->keyfile;
info.ssl_cipher_list = listener->ciphers;
if(listener->require_certificate){
info.options |= LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT;
}
#endif
2014-09-17 20:55:02 +00:00
info.options |= LWS_SERVER_OPTION_DISABLE_IPV6;
2014-09-08 17:21:53 +00:00
user = _mosquitto_calloc(1, sizeof(struct libws_mqtt_hack));
if(!user){
2014-09-17 20:50:08 +00:00
_mosquitto_free(p);
2014-06-30 22:30:43 +00:00
return NULL;
}
2014-09-08 22:25:02 +00:00
if(listener->http_dir){
2014-09-08 22:25:02 +00:00
#ifdef WIN32
user->http_dir = _fullpath(NULL, listener->http_dir, 0);
2014-09-08 22:25:02 +00:00
#else
user->http_dir = realpath(listener->http_dir, NULL);
#endif
if(!user->http_dir){
_mosquitto_free(user);
2014-09-17 20:50:08 +00:00
_mosquitto_free(p);
return NULL;
}
2014-09-08 22:25:02 +00:00
}
2014-09-08 17:21:53 +00:00
info.user = user;
2014-09-17 20:50:08 +00:00
listener->ws_protocol = p;
lws_set_log_level(0, NULL);
2014-07-03 10:32:29 +00:00
_mosquitto_log_printf(NULL, MOSQ_LOG_INFO, "Opening websockets listen socket on port %d.", listener->port);
return libwebsocket_create_context(&info);
}
#endif