Commit 57f826ec authored by Dmitry Frank's avatar Dmitry Frank Committed by Cesanta Bot

Implement miot cloud group add, list, delete

PUBLISHED_FROM=4b6f7b6fb4559e085c93f5f8827b8fae1701de70
parent 729bdebc
PROG = nc PROG = nc
MODULE_CFLAGS=-DMG_ENABLE_THREADS MODULE_CFLAGS=-DMG_ENABLE_THREADS -DMG_DISABLE_HTTP
SSL_LIB=openssl SSL_LIB=openssl
include ../examples.mk include ../examples.mk
// Copyright (c) 2014-2016 Cesanta Software Limited // Copyright (c) 2014 Cesanta Software Limited
// All rights reserved // All rights reserved
// //
// This software is dual-licensed: you can redistribute it and/or modify // This software is dual-licensed: you can redistribute it and/or modify
...@@ -13,13 +13,14 @@ ...@@ -13,13 +13,14 @@
// //
// Alternatively, you can license this software under a commercial // Alternatively, you can license this software under a commercial
// license, as set out in <https://www.cesanta.com/license>. // license, as set out in <https://www.cesanta.com/license>.
//
// $Date: 2014-09-28 05:04:41 UTC $
// This file implements "netcat" utility with SSL and traffic hexdump. // This file implements "netcat" utility with SSL and traffic hexdump.
#include "mongoose.h" #include "mongoose.h"
static sig_atomic_t s_received_signal = 0; static sig_atomic_t s_received_signal = 0;
static int s_is_websocket;
static void signal_handler(int sig_num) { static void signal_handler(int sig_num) {
signal(sig_num, signal_handler); signal(sig_num, signal_handler);
...@@ -27,12 +28,15 @@ static void signal_handler(int sig_num) { ...@@ -27,12 +28,15 @@ static void signal_handler(int sig_num) {
} }
static void show_usage_and_exit(const char *prog_name) { static void show_usage_and_exit(const char *prog_name) {
fprintf(stderr, "%s\n", "Copyright (c) Cesanta. Built on: " __DATE__); fprintf(stderr, "%s\n", "Copyright (c) 2014 CESANTA SOFTWARE");
fprintf(stderr, "Usage: %s [OPTIONS] [IP:]PORT\n", prog_name); fprintf(stderr, "%s\n", "Usage:");
fprintf(stderr, "%s\n", "Options:"); fprintf(stderr,
fprintf(stderr, "%s\n", " -l\t\tOpen a listening socket"); " %s\n [-d debug_file] [-l] [tcp|ssl]://[ip:]port[:cert][:ca_cert]",
fprintf(stderr, "%s\n", " -d file\tHexdump traffic into a file"); prog_name);
fprintf(stderr, "%s\n", " -ws\t\tUse WebSocket protocol"); fprintf(stderr, "%s\n", "Examples:");
fprintf(stderr, " %s\n -d hexdump.txt ssl://google.com:443", prog_name);
fprintf(stderr, " %s\n -l ssl://443:ssl_cert.pem", prog_name);
fprintf(stderr, " %s\n -l tcp://8080", prog_name);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
...@@ -50,11 +54,7 @@ static void on_stdin_read(struct mg_connection *nc, int ev, void *p) { ...@@ -50,11 +54,7 @@ static void on_stdin_read(struct mg_connection *nc, int ev, void *p) {
} else { } else {
// A character is received from stdin. Send it to the connection. // A character is received from stdin. Send it to the connection.
unsigned char c = (unsigned char) ch; unsigned char c = (unsigned char) ch;
if (s_is_websocket) { mg_send(nc, &c, 1);
mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, &c, 1);
} else {
mg_send(nc, &c, 1);
}
} }
} }
...@@ -72,27 +72,21 @@ static void *stdio_thread_func(void *param) { ...@@ -72,27 +72,21 @@ static void *stdio_thread_func(void *param) {
} }
static void ev_handler(struct mg_connection *nc, int ev, void *p) { static void ev_handler(struct mg_connection *nc, int ev, void *p) {
(void) p;
switch (ev) { switch (ev) {
case MG_EV_ACCEPT: case MG_EV_ACCEPT:
case MG_EV_CONNECT: case MG_EV_CONNECT:
mg_start_thread(stdio_thread_func, nc->mgr); mg_start_thread(stdio_thread_func, nc->mgr);
break; break;
case MG_EV_WEBSOCKET_FRAME: {
struct websocket_message *wm = (struct websocket_message *) p;
fwrite(wm->data, 1, wm->size, stdout);
break;
}
case MG_EV_CLOSE: case MG_EV_CLOSE:
s_received_signal = 1; s_received_signal = 1;
break; break;
case MG_EV_RECV: case MG_EV_RECV:
if (!s_is_websocket) { fwrite(nc->recv_mbuf.buf, 1, nc->recv_mbuf.len, stdout);
fwrite(nc->recv_mbuf.buf, 1, nc->recv_mbuf.len, stdout); mbuf_remove(&nc->recv_mbuf, nc->recv_mbuf.len);
mbuf_remove(&nc->recv_mbuf, nc->recv_mbuf.len);
}
break; break;
default: default:
...@@ -104,8 +98,6 @@ int main(int argc, char *argv[]) { ...@@ -104,8 +98,6 @@ int main(int argc, char *argv[]) {
struct mg_mgr mgr; struct mg_mgr mgr;
int i, is_listening = 0; int i, is_listening = 0;
const char *address = NULL; const char *address = NULL;
struct mg_connection *c;
// struct mg_bind_opts = {};
mg_mgr_init(&mgr, NULL); mg_mgr_init(&mgr, NULL);
...@@ -115,8 +107,6 @@ int main(int argc, char *argv[]) { ...@@ -115,8 +107,6 @@ int main(int argc, char *argv[]) {
is_listening = 1; is_listening = 1;
} else if (strcmp(argv[i], "-d") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "-d") == 0 && i + 1 < argc) {
mgr.hexdump_file = argv[++i]; mgr.hexdump_file = argv[++i];
} else if (strcmp(argv[i], "-ws") == 0 && i + 1 < argc) {
s_is_websocket = 1;
} else { } else {
show_usage_and_exit(argv[0]); show_usage_and_exit(argv[0]);
} }
...@@ -133,20 +123,14 @@ int main(int argc, char *argv[]) { ...@@ -133,20 +123,14 @@ int main(int argc, char *argv[]) {
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
if (is_listening) { if (is_listening) {
if ((c = mg_bind(&mgr, address, ev_handler)) == NULL) { if (mg_bind(&mgr, address, ev_handler) == NULL) {
fprintf(stderr, "mg_bind(%s) failed\n", address); fprintf(stderr, "mg_bind(%s) failed\n", address);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} else if ((c = mg_connect(&mgr, address, ev_handler)) == NULL) { } else if (mg_connect(&mgr, address, ev_handler) == NULL) {
fprintf(stderr, "mg_connect(%s) failed\n", address); fprintf(stderr, "mg_connect(%s) failed\n", address);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (s_is_websocket) {
mg_set_protocol_http_websocket(c);
if (!is_listening) {
mg_send_websocket_handshake2(c, "/", address, NULL, NULL);
}
}
while (s_received_signal == 0) { while (s_received_signal == 0) {
mg_mgr_poll(&mgr, 1000); mg_mgr_poll(&mgr, 1000);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment