Commit 8742fac5 authored by Dmitry Frank's avatar Dmitry Frank Committed by Cesanta Bot

Publish mongoose src and tests

CL: Mongoose Web Server: Publish sources and tests

Resolves https://github.com/cesanta/mongoose/issues/745

PUBLISHED_FROM=7ecd7a3c518cfa614a6ba0838678dcb91b75a8c0
parent 0ab1c7ef
exclude_files=sha1\.c
This diff is collapsed.
/*
* Copyright (c) 2015 Cesanta Software Limited
* All rights reserved
* This software is dual-licensed: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. For the terms of this
* license, see <http://www.gnu.org/licenses/>.
*
* You are free to use this software under the terms of the GNU General
* Public License, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* Alternatively, you can license this software under a commercial
* license, as set out in <https://www.cesanta.com/license>.
*/
/*
* === CoAP API reference
*
* CoAP message format:
*
* ```
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
* |Ver| T | TKL | Code | Message ID | Token (if any, TKL bytes) ...
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
* | Options (if any) ... |1 1 1 1 1 1 1 1| Payload (if any) ...
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
* ```
*/
#ifndef CS_MONGOOSE_SRC_COAP_H_
#define CS_MONGOOSE_SRC_COAP_H_
#if MG_ENABLE_COAP
#define MG_COAP_MSG_TYPE_FIELD 0x2
#define MG_COAP_CODE_CLASS_FIELD 0x4
#define MG_COAP_CODE_DETAIL_FIELD 0x8
#define MG_COAP_MSG_ID_FIELD 0x10
#define MG_COAP_TOKEN_FIELD 0x20
#define MG_COAP_OPTIOMG_FIELD 0x40
#define MG_COAP_PAYLOAD_FIELD 0x80
#define MG_COAP_ERROR 0x10000
#define MG_COAP_FORMAT_ERROR (MG_COAP_ERROR | 0x20000)
#define MG_COAP_IGNORE (MG_COAP_ERROR | 0x40000)
#define MG_COAP_NOT_ENOUGH_DATA (MG_COAP_ERROR | 0x80000)
#define MG_COAP_NETWORK_ERROR (MG_COAP_ERROR | 0x100000)
#define MG_COAP_MSG_CON 0
#define MG_COAP_MSG_NOC 1
#define MG_COAP_MSG_ACK 2
#define MG_COAP_MSG_RST 3
#define MG_COAP_MSG_MAX 3
#define MG_COAP_CODECLASS_REQUEST 0
#define MG_COAP_CODECLASS_RESP_OK 2
#define MG_COAP_CODECLASS_CLIENT_ERR 4
#define MG_COAP_CODECLASS_SRV_ERR 5
#define MG_COAP_EVENT_BASE 300
#define MG_EV_COAP_CON (MG_COAP_EVENT_BASE + MG_COAP_MSG_CON)
#define MG_EV_COAP_NOC (MG_COAP_EVENT_BASE + MG_COAP_MSG_NOC)
#define MG_EV_COAP_ACK (MG_COAP_EVENT_BASE + MG_COAP_MSG_ACK)
#define MG_EV_COAP_RST (MG_COAP_EVENT_BASE + MG_COAP_MSG_RST)
/*
* CoAP options.
* Use mg_coap_add_option and mg_coap_free_options
* for creation and destruction.
*/
struct mg_coap_option {
struct mg_coap_option *next;
uint32_t number;
struct mg_str value;
};
/* CoAP message. See RFC 7252 for details. */
struct mg_coap_message {
uint32_t flags;
uint8_t msg_type;
uint8_t code_class;
uint8_t code_detail;
uint16_t msg_id;
struct mg_str token;
struct mg_coap_option *options;
struct mg_str payload;
struct mg_coap_option *optiomg_tail;
};
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/* Sets CoAP protocol handler - triggers CoAP specific events. */
int mg_set_protocol_coap(struct mg_connection *nc);
/*
* Adds a new option to mg_coap_message structure.
* Returns pointer to the newly created option.
* Note: options must be freed by using mg_coap_free_options
*/
struct mg_coap_option *mg_coap_add_option(struct mg_coap_message *cm,
uint32_t number, char *value,
size_t len);
/*
* Frees the memory allocated for options.
* If the cm parameter doesn't contain any option it does nothing.
*/
void mg_coap_free_options(struct mg_coap_message *cm);
/*
* Composes a CoAP message from `mg_coap_message`
* and sends it into `nc` connection.
* Returns 0 on success. On error, it is a bitmask:
*
* - `#define MG_COAP_ERROR 0x10000`
* - `#define MG_COAP_FORMAT_ERROR (MG_COAP_ERROR | 0x20000)`
* - `#define MG_COAP_IGNORE (MG_COAP_ERROR | 0x40000)`
* - `#define MG_COAP_NOT_ENOUGH_DATA (MG_COAP_ERROR | 0x80000)`
* - `#define MG_COAP_NETWORK_ERROR (MG_COAP_ERROR | 0x100000)`
*/
uint32_t mg_coap_send_message(struct mg_connection *nc,
struct mg_coap_message *cm);
/*
* Composes CoAP acknowledgement from `mg_coap_message`
* and sends it into `nc` connection.
* Return value: see `mg_coap_send_message()`
*/
uint32_t mg_coap_send_ack(struct mg_connection *nc, uint16_t msg_id);
/*
* Parses CoAP message and fills mg_coap_message and returns cm->flags.
* This is a helper function.
*
* NOTE: usually CoAP works over UDP, so lack of data means format error.
* But, in theory, it is possible to use CoAP over TCP (according to RFC)
*
* The caller has to check results and treat COAP_NOT_ENOUGH_DATA according to
* underlying protocol:
*
* - in case of UDP COAP_NOT_ENOUGH_DATA means COAP_FORMAT_ERROR,
* - in case of TCP client can try to receive more data
*
* Return value: see `mg_coap_send_message()`
*/
uint32_t mg_coap_parse(struct mbuf *io, struct mg_coap_message *cm);
/*
* Composes CoAP message from mg_coap_message structure.
* This is a helper function.
* Return value: see `mg_coap_send_message()`
*/
uint32_t mg_coap_compose(struct mg_coap_message *cm, struct mbuf *io);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* MG_ENABLE_COAP */
#endif /* CS_MONGOOSE_SRC_COAP_H_ */
/*
* Copyright (c) 2004-2013 Sergey Lyubka
* Copyright (c) 2013-2015 Cesanta Software Limited
* All rights reserved
*
* This software is dual-licensed: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. For the terms of this
* license, see <http://www.gnu.org/licenses/>.
*
* You are free to use this software under the terms of the GNU General
* Public License, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* Alternatively, you can license this software under a commercial
* license, as set out in <https://www.cesanta.com/license>.
*/
#ifndef CS_MONGOOSE_SRC_COMMON_H_
#define CS_MONGOOSE_SRC_COMMON_H_
#define MG_VERSION "6.10"
/* Local tweaks, applied before any of Mongoose's own headers. */
#ifdef MG_LOCALS
#include <mg_locals.h>
#endif
#endif /* CS_MONGOOSE_SRC_COMMON_H_ */
/*
* Copyright (c) 2014 Cesanta Software Limited
* All rights reserved
*/
#if MG_ENABLE_DNS
#include "mongoose/src/internal.h"
#include "mongoose/src/dns.h"
static int mg_dns_tid = 0xa0;
struct mg_dns_header {
uint16_t transaction_id;
uint16_t flags;
uint16_t num_questions;
uint16_t num_answers;
uint16_t num_authority_prs;
uint16_t num_other_prs;
};
struct mg_dns_resource_record *mg_dns_next_record(
struct mg_dns_message *msg, int query,
struct mg_dns_resource_record *prev) {
struct mg_dns_resource_record *rr;
for (rr = (prev == NULL ? msg->answers : prev + 1);
rr - msg->answers < msg->num_answers; rr++) {
if (rr->rtype == query) {
return rr;
}
}
return NULL;
}
int mg_dns_parse_record_data(struct mg_dns_message *msg,
struct mg_dns_resource_record *rr, void *data,
size_t data_len) {
switch (rr->rtype) {
case MG_DNS_A_RECORD:
if (data_len < sizeof(struct in_addr)) {
return -1;
}
if (rr->rdata.p + data_len > msg->pkt.p + msg->pkt.len) {
return -1;
}
memcpy(data, rr->rdata.p, data_len);
return 0;
#if MG_ENABLE_IPV6
case MG_DNS_AAAA_RECORD:
if (data_len < sizeof(struct in6_addr)) {
return -1; /* LCOV_EXCL_LINE */
}
memcpy(data, rr->rdata.p, data_len);
return 0;
#endif
case MG_DNS_CNAME_RECORD:
mg_dns_uncompress_name(msg, &rr->rdata, (char *) data, data_len);
return 0;
}
return -1;
}
int mg_dns_insert_header(struct mbuf *io, size_t pos,
struct mg_dns_message *msg) {
struct mg_dns_header header;
memset(&header, 0, sizeof(header));
header.transaction_id = msg->transaction_id;
header.flags = htons(msg->flags);
header.num_questions = htons(msg->num_questions);
header.num_answers = htons(msg->num_answers);
return mbuf_insert(io, pos, &header, sizeof(header));
}
int mg_dns_copy_questions(struct mbuf *io, struct mg_dns_message *msg) {
unsigned char *begin, *end;
struct mg_dns_resource_record *last_q;
if (msg->num_questions <= 0) return 0;
begin = (unsigned char *) msg->pkt.p + sizeof(struct mg_dns_header);
last_q = &msg->questions[msg->num_questions - 1];
end = (unsigned char *) last_q->name.p + last_q->name.len + 4;
return mbuf_append(io, begin, end - begin);
}
int mg_dns_encode_name(struct mbuf *io, const char *name, size_t len) {
const char *s;
unsigned char n;
size_t pos = io->len;
do {
if ((s = strchr(name, '.')) == NULL) {
s = name + len;
}
if (s - name > 127) {
return -1; /* TODO(mkm) cover */
}
n = s - name; /* chunk length */
mbuf_append(io, &n, 1); /* send length */
mbuf_append(io, name, n);
if (*s == '.') {
n++;
}
name += n;
len -= n;
} while (*s != '\0');
mbuf_append(io, "\0", 1); /* Mark end of host name */
return io->len - pos;
}
int mg_dns_encode_record(struct mbuf *io, struct mg_dns_resource_record *rr,
const char *name, size_t nlen, const void *rdata,
size_t rlen) {
size_t pos = io->len;
uint16_t u16;
uint32_t u32;
if (rr->kind == MG_DNS_INVALID_RECORD) {
return -1; /* LCOV_EXCL_LINE */
}
if (mg_dns_encode_name(io, name, nlen) == -1) {
return -1;
}
u16 = htons(rr->rtype);
mbuf_append(io, &u16, 2);
u16 = htons(rr->rclass);
mbuf_append(io, &u16, 2);
if (rr->kind == MG_DNS_ANSWER) {
u32 = htonl(rr->ttl);
mbuf_append(io, &u32, 4);
if (rr->rtype == MG_DNS_CNAME_RECORD) {
int clen;
/* fill size after encoding */
size_t off = io->len;
mbuf_append(io, &u16, 2);
if ((clen = mg_dns_encode_name(io, (const char *) rdata, rlen)) == -1) {
return -1;
}
u16 = clen;
io->buf[off] = u16 >> 8;
io->buf[off + 1] = u16 & 0xff;
} else {
u16 = htons((uint16_t) rlen);
mbuf_append(io, &u16, 2);
mbuf_append(io, rdata, rlen);
}
}
return io->len - pos;
}
void mg_send_dns_query(struct mg_connection *nc, const char *name,
int query_type) {
struct mg_dns_message *msg =
(struct mg_dns_message *) MG_CALLOC(1, sizeof(*msg));
struct mbuf pkt;
struct mg_dns_resource_record *rr = &msg->questions[0];
DBG(("%s %d", name, query_type));
mbuf_init(&pkt, 64 /* Start small, it'll grow as needed. */);
msg->transaction_id = ++mg_dns_tid;
msg->flags = 0x100;
msg->num_questions = 1;
mg_dns_insert_header(&pkt, 0, msg);
rr->rtype = query_type;
rr->rclass = 1; /* Class: inet */
rr->kind = MG_DNS_QUESTION;
if (mg_dns_encode_record(&pkt, rr, name, strlen(name), NULL, 0) == -1) {
/* TODO(mkm): return an error code */
goto cleanup; /* LCOV_EXCL_LINE */
}
/* TCP DNS requires messages to be prefixed with len */
if (!(nc->flags & MG_F_UDP)) {
uint16_t len = htons((uint16_t) pkt.len);
mbuf_insert(&pkt, 0, &len, 2);
}
mg_send(nc, pkt.buf, pkt.len);
mbuf_free(&pkt);
cleanup:
MG_FREE(msg);
}
static unsigned char *mg_parse_dns_resource_record(
unsigned char *data, unsigned char *end, struct mg_dns_resource_record *rr,
int reply) {
unsigned char *name = data;
int chunk_len, data_len;
while (data < end && (chunk_len = *data)) {
if (((unsigned char *) data)[0] & 0xc0) {
data += 1;
break;
}
data += chunk_len + 1;
}
if (data > end - 5) {
return NULL;
}
rr->name.p = (char *) name;
rr->name.len = data - name + 1;
data++;
rr->rtype = data[0] << 8 | data[1];
data += 2;
rr->rclass = data[0] << 8 | data[1];
data += 2;
rr->kind = reply ? MG_DNS_ANSWER : MG_DNS_QUESTION;
if (reply) {
if (data >= end - 6) {
return NULL;
}
rr->ttl = (uint32_t) data[0] << 24 | (uint32_t) data[1] << 16 |
data[2] << 8 | data[3];
data += 4;
data_len = *data << 8 | *(data + 1);
data += 2;
rr->rdata.p = (char *) data;
rr->rdata.len = data_len;
data += data_len;
}
return data;
}
int mg_parse_dns(const char *buf, int len, struct mg_dns_message *msg) {
struct mg_dns_header *header = (struct mg_dns_header *) buf;
unsigned char *data = (unsigned char *) buf + sizeof(*header);
unsigned char *end = (unsigned char *) buf + len;
int i;
memset(msg, 0, sizeof(*msg));
msg->pkt.p = buf;
msg->pkt.len = len;
if (len < (int) sizeof(*header)) return -1;
msg->transaction_id = header->transaction_id;
msg->flags = ntohs(header->flags);
msg->num_questions = ntohs(header->num_questions);
if (msg->num_questions > (int) ARRAY_SIZE(msg->questions)) {
msg->num_questions = (int) ARRAY_SIZE(msg->questions);
}
msg->num_answers = ntohs(header->num_answers);
if (msg->num_answers > (int) ARRAY_SIZE(msg->answers)) {
msg->num_answers = (int) ARRAY_SIZE(msg->answers);
}
for (i = 0; i < msg->num_questions; i++) {
data = mg_parse_dns_resource_record(data, end, &msg->questions[i], 0);
if (data == NULL) return -1;
}
for (i = 0; i < msg->num_answers; i++) {
data = mg_parse_dns_resource_record(data, end, &msg->answers[i], 1);
if (data == NULL) return -1;
}
return 0;
}
size_t mg_dns_uncompress_name(struct mg_dns_message *msg, struct mg_str *name,
char *dst, int dst_len) {
int chunk_len, num_ptrs = 0;
char *old_dst = dst;
const unsigned char *data = (unsigned char *) name->p;
const unsigned char *end = (unsigned char *) msg->pkt.p + msg->pkt.len;
if (data >= end) {
return 0;
}
while ((chunk_len = *data++)) {
int leeway = dst_len - (dst - old_dst);
if (data >= end) {
return 0;
}
if ((chunk_len & 0xc0) == 0xc0) {
uint16_t off = (data[-1] & (~0xc0)) << 8 | data[0];
if (off >= msg->pkt.len) {
return 0;
}
/* Basic circular loop avoidance: allow up to 16 pointer hops. */
if (++num_ptrs > 15) {
return 0;
}
data = (unsigned char *) msg->pkt.p + off;
continue;
}
if (chunk_len > 63) {
return 0;
}
if (chunk_len > leeway) {
chunk_len = leeway;
}
if (data + chunk_len >= end) {
return 0;
}
memcpy(dst, data, chunk_len);
data += chunk_len;
dst += chunk_len;
leeway -= chunk_len;
if (leeway == 0) {
return dst - old_dst;
}
*dst++ = '.';
}
if (dst != old_dst) {
*--dst = 0;
}
return dst - old_dst;
}
static void dns_handler(struct mg_connection *nc, int ev,
void *ev_data MG_UD_ARG(void *user_data)) {
struct mbuf *io = &nc->recv_mbuf;
struct mg_dns_message msg;
/* Pass low-level events to the user handler */
nc->handler(nc, ev, ev_data MG_UD_ARG(user_data));
switch (ev) {
case MG_EV_RECV:
if (!(nc->flags & MG_F_UDP)) {
mbuf_remove(&nc->recv_mbuf, 2);
}
if (mg_parse_dns(nc->recv_mbuf.buf, nc->recv_mbuf.len, &msg) == -1) {
/* reply + recursion allowed + format error */
memset(&msg, 0, sizeof(msg));
msg.flags = 0x8081;
mg_dns_insert_header(io, 0, &msg);
if (!(nc->flags & MG_F_UDP)) {
uint16_t len = htons((uint16_t) io->len);
mbuf_insert(io, 0, &len, 2);
}
mg_send(nc, io->buf, io->len);
} else {
/* Call user handler with parsed message */
nc->handler(nc, MG_DNS_MESSAGE, &msg MG_UD_ARG(user_data));
}
mbuf_remove(io, io->len);
break;
}
}
void mg_set_protocol_dns(struct mg_connection *nc) {
nc->proto_handler = dns_handler;
}
#endif /* MG_ENABLE_DNS */
/*
* Copyright (c) 2014 Cesanta Software Limited
* All rights reserved
*/
/*
* === DNS API reference
*/
#ifndef CS_MONGOOSE_SRC_DNS_H_
#define CS_MONGOOSE_SRC_DNS_H_
#include "mongoose/src/net.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#define MG_DNS_A_RECORD 0x01 /* Lookup IP address */
#define MG_DNS_CNAME_RECORD 0x05 /* Lookup CNAME */
#define MG_DNS_PTR_RECORD 0x0c /* Lookup PTR */
#define MG_DNS_TXT_RECORD 0x10 /* Lookup TXT */
#define MG_DNS_AAAA_RECORD 0x1c /* Lookup IPv6 address */
#define MG_DNS_SRV_RECORD 0x21 /* Lookup SRV */
#define MG_DNS_MX_RECORD 0x0f /* Lookup mail server for domain */
#define MG_DNS_ANY_RECORD 0xff
#define MG_DNS_NSEC_RECORD 0x2f
#define MG_MAX_DNS_QUESTIONS 32
#define MG_MAX_DNS_ANSWERS 32
#define MG_DNS_MESSAGE 100 /* High-level DNS message event */
enum mg_dns_resource_record_kind {
MG_DNS_INVALID_RECORD = 0,
MG_DNS_QUESTION,
MG_DNS_ANSWER
};
/* DNS resource record. */
struct mg_dns_resource_record {
struct mg_str name; /* buffer with compressed name */
int rtype;
int rclass;
int ttl;
enum mg_dns_resource_record_kind kind;
struct mg_str rdata; /* protocol data (can be a compressed name) */
};
/* DNS message (request and response). */
struct mg_dns_message {
struct mg_str pkt; /* packet body */
uint16_t flags;
uint16_t transaction_id;
int num_questions;
int num_answers;
struct mg_dns_resource_record questions[MG_MAX_DNS_QUESTIONS];
struct mg_dns_resource_record answers[MG_MAX_DNS_ANSWERS];
};
struct mg_dns_resource_record *mg_dns_next_record(
struct mg_dns_message *msg, int query, struct mg_dns_resource_record *prev);
/*
* Parses the record data from a DNS resource record.
*
* - A: struct in_addr *ina
* - AAAA: struct in6_addr *ina
* - CNAME: char buffer
*
* Returns -1 on error.
*
* TODO(mkm): MX
*/
int mg_dns_parse_record_data(struct mg_dns_message *msg,
struct mg_dns_resource_record *rr, void *data,
size_t data_len);
/*
* Sends a DNS query to the remote end.
*/
void mg_send_dns_query(struct mg_connection *nc, const char *name,
int query_type);
/*
* Inserts a DNS header to an IO buffer.
*
* Returns the number of bytes inserted.
*/
int mg_dns_insert_header(struct mbuf *io, size_t pos,
struct mg_dns_message *msg);
/*
* Appends already encoded questions from an existing message.
*
* This is useful when generating a DNS reply message which includes
* all question records.
*
* Returns the number of appended bytes.
*/
int mg_dns_copy_questions(struct mbuf *io, struct mg_dns_message *msg);
/*
* Encodes and appends a DNS resource record to an IO buffer.
*
* The record metadata is taken from the `rr` parameter, while the name and data
* are taken from the parameters, encoded in the appropriate format depending on
* record type and stored in the IO buffer. The encoded values might contain
* offsets within the IO buffer. It's thus important that the IO buffer doesn't
* get trimmed while a sequence of records are encoded while preparing a DNS
* reply.
*
* This function doesn't update the `name` and `rdata` pointers in the `rr`
* struct because they might be invalidated as soon as the IO buffer grows
* again.
*
* Returns the number of bytes appended or -1 in case of error.
*/
int mg_dns_encode_record(struct mbuf *io, struct mg_dns_resource_record *rr,
const char *name, size_t nlen, const void *rdata,
size_t rlen);
/*
* Encodes a DNS name.
*/
int mg_dns_encode_name(struct mbuf *io, const char *name, size_t len);
/* Low-level: parses a DNS response. */
int mg_parse_dns(const char *buf, int len, struct mg_dns_message *msg);
/*
* Uncompresses a DNS compressed name.
*
* The containing DNS message is required because of the compressed encoding
* and reference suffixes present elsewhere in the packet.
*
* If the name is less than `dst_len` characters long, the remainder
* of `dst` is terminated with `\0` characters. Otherwise, `dst` is not
* terminated.
*
* If `dst_len` is 0 `dst` can be NULL.
* Returns the uncompressed name length.
*/
size_t mg_dns_uncompress_name(struct mg_dns_message *msg, struct mg_str *name,
char *dst, int dst_len);
/*
* Attaches a built-in DNS event handler to the given listening connection.
*
* The DNS event handler parses the incoming UDP packets, treating them as DNS
* requests. If an incoming packet gets successfully parsed by the DNS event
* handler, a user event handler will receive an `MG_DNS_REQUEST` event, with
* `ev_data` pointing to the parsed `struct mg_dns_message`.
*
* See
* [captive_dns_server](https://github.com/cesanta/mongoose/tree/master/examples/captive_dns_server)
* example on how to handle DNS request and send DNS reply.
*/
void mg_set_protocol_dns(struct mg_connection *nc);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* CS_MONGOOSE_SRC_DNS_H_ */
/*
* Copyright (c) 2014 Cesanta Software Limited
* All rights reserved
*/
#if MG_ENABLE_DNS_SERVER
#include "mongoose/src/internal.h"
#include "mongoose/src/dns-server.h"
struct mg_dns_reply mg_dns_create_reply(struct mbuf *io,
struct mg_dns_message *msg) {
struct mg_dns_reply rep;
rep.msg = msg;
rep.io = io;
rep.start = io->len;
/* reply + recursion allowed */
msg->flags |= 0x8080;
mg_dns_copy_questions(io, msg);
msg->num_answers = 0;
return rep;
}
void mg_dns_send_reply(struct mg_connection *nc, struct mg_dns_reply *r) {
size_t sent = r->io->len - r->start;
mg_dns_insert_header(r->io, r->start, r->msg);
if (!(nc->flags & MG_F_UDP)) {
uint16_t len = htons((uint16_t) sent);
mbuf_insert(r->io, r->start, &len, 2);
}
if (&nc->send_mbuf != r->io) {
mg_send(nc, r->io->buf + r->start, r->io->len - r->start);
r->io->len = r->start;
}
}
int mg_dns_reply_record(struct mg_dns_reply *reply,
struct mg_dns_resource_record *question,
const char *name, int rtype, int ttl, const void *rdata,
size_t rdata_len) {
struct mg_dns_message *msg = (struct mg_dns_message *) reply->msg;
char rname[512];
struct mg_dns_resource_record *ans = &msg->answers[msg->num_answers];
if (msg->num_answers >= MG_MAX_DNS_ANSWERS) {
return -1; /* LCOV_EXCL_LINE */
}
if (name == NULL) {
name = rname;
rname[511] = 0;
mg_dns_uncompress_name(msg, &question->name, rname, sizeof(rname) - 1);
}
*ans = *question;
ans->kind = MG_DNS_ANSWER;
ans->rtype = rtype;
ans->ttl = ttl;
if (mg_dns_encode_record(reply->io, ans, name, strlen(name), rdata,
rdata_len) == -1) {
return -1; /* LCOV_EXCL_LINE */
};
msg->num_answers++;
return 0;
}
#endif /* MG_ENABLE_DNS_SERVER */
/*
* Copyright (c) 2014 Cesanta Software Limited
* All rights reserved
*/
/*
* === DNS server API reference
*
* Disabled by default; enable with `-DMG_ENABLE_DNS_SERVER`.
*/
#ifndef CS_MONGOOSE_SRC_DNS_SERVER_H_
#define CS_MONGOOSE_SRC_DNS_SERVER_H_
#if MG_ENABLE_DNS_SERVER
#include "mongoose/src/dns.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#define MG_DNS_SERVER_DEFAULT_TTL 3600
struct mg_dns_reply {
struct mg_dns_message *msg;
struct mbuf *io;
size_t start;
};
/*
* Creates a DNS reply.
*
* The reply will be based on an existing query message `msg`.
* The query body will be appended to the output buffer.
* "reply + recursion allowed" will be added to the message flags and the
* message's num_answers will be set to 0.
*
* Answer records can be appended with `mg_dns_send_reply` or by lower
* level function defined in the DNS API.
*
* In order to send a reply use `mg_dns_send_reply`.
* It's possible to use a connection's send buffer as reply buffer,
* and it will work for both UDP and TCP connections.
*
* Example:
*
* ```c
* reply = mg_dns_create_reply(&nc->send_mbuf, msg);
* for (i = 0; i < msg->num_questions; i++) {
* rr = &msg->questions[i];
* if (rr->rtype == MG_DNS_A_RECORD) {
* mg_dns_reply_record(&reply, rr, 3600, &dummy_ip_addr, 4);
* }
* }
* mg_dns_send_reply(nc, &reply);
* ```
*/
struct mg_dns_reply mg_dns_create_reply(struct mbuf *io,
struct mg_dns_message *msg);
/*
* Appends a DNS reply record to the IO buffer and to the DNS message.
*
* The message's num_answers field will be incremented. It's the caller's duty
* to ensure num_answers is properly initialised.
*
* Returns -1 on error.
*/
int mg_dns_reply_record(struct mg_dns_reply *reply,
struct mg_dns_resource_record *question,
const char *name, int rtype, int ttl, const void *rdata,
size_t rdata_len);
/*
* Sends a DNS reply through a connection.
*
* The DNS data is stored in an IO buffer pointed by reply structure in `r`.
* This function mutates the content of that buffer in order to ensure that
* the DNS header reflects the size and flags of the message, that might have
* been updated either with `mg_dns_reply_record` or by direct manipulation of
* `r->message`.
*
* Once sent, the IO buffer will be trimmed unless the reply IO buffer
* is the connection's send buffer and the connection is not in UDP mode.
*/
void mg_dns_send_reply(struct mg_connection *nc, struct mg_dns_reply *r);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* MG_ENABLE_DNS_SERVER */
#endif /* CS_MONGOOSE_SRC_DNS_SERVER_H_ */
/*
* Copyright (c) 2014-2016 Cesanta Software Limited
* All rights reserved
*/
#ifndef CS_MONGOOSE_SRC_FEATURES_H_
#define CS_MONGOOSE_SRC_FEATURES_H_
#ifndef MG_DISABLE_HTTP_DIGEST_AUTH
#define MG_DISABLE_HTTP_DIGEST_AUTH 0
#endif
#ifndef MG_DISABLE_HTTP_KEEP_ALIVE
#define MG_DISABLE_HTTP_KEEP_ALIVE 0
#endif
#ifndef MG_DISABLE_PFS
#define MG_DISABLE_PFS 0
#endif
#ifndef MG_DISABLE_WS_RANDOM_MASK
#define MG_DISABLE_WS_RANDOM_MASK 0
#endif
#ifndef MG_ENABLE_ASYNC_RESOLVER
#define MG_ENABLE_ASYNC_RESOLVER 1
#endif
#ifndef MG_ENABLE_BROADCAST
#define MG_ENABLE_BROADCAST 0
#endif
#ifndef MG_ENABLE_COAP
#define MG_ENABLE_COAP 0
#endif
#ifndef MG_ENABLE_DEBUG
#define MG_ENABLE_DEBUG 0
#endif
#ifndef MG_ENABLE_DIRECTORY_LISTING
#define MG_ENABLE_DIRECTORY_LISTING 0
#endif
#ifndef MG_ENABLE_DNS
#define MG_ENABLE_DNS 1
#endif
#ifndef MG_ENABLE_DNS_SERVER
#define MG_ENABLE_DNS_SERVER 0
#endif
#ifndef MG_ENABLE_FAKE_DAVLOCK
#define MG_ENABLE_FAKE_DAVLOCK 0
#endif
#ifndef MG_ENABLE_FILESYSTEM
#define MG_ENABLE_FILESYSTEM 0
#endif
#ifndef MG_ENABLE_GETADDRINFO
#define MG_ENABLE_GETADDRINFO 0
#endif
#ifndef MG_ENABLE_HEXDUMP
#define MG_ENABLE_HEXDUMP CS_ENABLE_STDIO
#endif
#ifndef MG_ENABLE_HTTP
#define MG_ENABLE_HTTP 1
#endif
#ifndef MG_ENABLE_HTTP_CGI
#define MG_ENABLE_HTTP_CGI 0
#endif
#ifndef MG_ENABLE_HTTP_SSI
#define MG_ENABLE_HTTP_SSI MG_ENABLE_FILESYSTEM
#endif
#ifndef MG_ENABLE_HTTP_SSI_EXEC
#define MG_ENABLE_HTTP_SSI_EXEC 0
#endif
#ifndef MG_ENABLE_HTTP_STREAMING_MULTIPART
#define MG_ENABLE_HTTP_STREAMING_MULTIPART 0
#endif
#ifndef MG_ENABLE_HTTP_WEBDAV
#define MG_ENABLE_HTTP_WEBDAV 0
#endif
#ifndef MG_ENABLE_HTTP_WEBSOCKET
#define MG_ENABLE_HTTP_WEBSOCKET MG_ENABLE_HTTP
#endif
#ifndef MG_ENABLE_IPV6
#define MG_ENABLE_IPV6 0
#endif
#ifndef MG_ENABLE_MQTT
#define MG_ENABLE_MQTT 1
#endif
#ifndef MG_ENABLE_SOCKS
#define MG_ENABLE_SOCKS 0
#endif
#ifndef MG_ENABLE_MQTT_BROKER
#define MG_ENABLE_MQTT_BROKER 0
#endif
#ifndef MG_ENABLE_SSL
#define MG_ENABLE_SSL 0
#endif
#ifndef MG_ENABLE_SYNC_RESOLVER
#define MG_ENABLE_SYNC_RESOLVER 0
#endif
#ifndef MG_ENABLE_STDIO
#define MG_ENABLE_STDIO CS_ENABLE_STDIO
#endif
#ifndef MG_NET_IF
#define MG_NET_IF MG_NET_IF_SOCKET
#endif
#ifndef MG_SSL_IF
#define MG_SSL_IF MG_SSL_IF_OPENSSL
#endif
#ifndef MG_ENABLE_THREADS /* ifdef-ok */
#ifdef _WIN32
#define MG_ENABLE_THREADS 1
#else
#define MG_ENABLE_THREADS 0
#endif
#endif
#if MG_ENABLE_DEBUG && !defined(CS_ENABLE_DEBUG)
#define CS_ENABLE_DEBUG 1
#endif
/* MQTT broker requires MQTT */
#if MG_ENABLE_MQTT_BROKER && !MG_ENABLE_MQTT
#undef MG_ENABLE_MQTT
#define MG_ENABLE_MQTT 1
#endif
#ifndef MG_ENABLE_HTTP_URL_REWRITES
#define MG_ENABLE_HTTP_URL_REWRITES \
(CS_PLATFORM == CS_P_WINDOWS || CS_PLATFORM == CS_P_UNIX)
#endif
#ifndef MG_ENABLE_TUN
#define MG_ENABLE_TUN MG_ENABLE_HTTP_WEBSOCKET
#endif
#ifndef MG_ENABLE_SNTP
#define MG_ENABLE_SNTP 0
#endif
#ifndef MG_ENABLE_EXTRA_ERRORS_DESC
#define MG_ENABLE_EXTRA_ERRORS_DESC 0
#endif
#ifndef MG_ENABLE_CALLBACK_USERDATA
#define MG_ENABLE_CALLBACK_USERDATA 0
#endif
#if MG_ENABLE_CALLBACK_USERDATA
#define MG_UD_ARG(ud) , ud
#define MG_CB(cb, ud) cb, ud
#else
#define MG_UD_ARG(ud)
#define MG_CB(cb, ud) cb
#endif
#endif /* CS_MONGOOSE_SRC_FEATURES_H_ */
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
/*
* === API reference
*/
#ifndef CS_MONGOOSE_SRC_MQTT_CLIENT_H_
#define CS_MONGOOSE_SRC_MQTT_CLIENT_H_
#endif /* CS_MONGOOSE_SRC_MQTT_CLIENT_H_ */
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
/*
* Copyright (c) 2014-2016 Cesanta Software Limited
* All rights reserved
*/
#ifndef CS_MONGOOSE_SRC_NET_IF_SOCKET_H_
#define CS_MONGOOSE_SRC_NET_IF_SOCKET_H_
#include "mongoose/src/net_if.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#ifndef MG_ENABLE_NET_IF_SOCKET
#define MG_ENABLE_NET_IF_SOCKET MG_NET_IF == MG_NET_IF_SOCKET
#endif
extern const struct mg_iface_vtable mg_socket_iface_vtable;
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* CS_MONGOOSE_SRC_NET_IF_SOCKET_H_ */
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
You may need to edit Makefile and t/Makefile to specify where
your openssl include files and libraries are located. To build
without openssl see the comments in Makefile.
To build: make
To test: make test (requires openssl)
To install:
copy libccgi.a to a lib directory such as /usr/local/lib
copy ccgi.h to an include directory such as /usr/local/include
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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