mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-07 16:01:30 +00:00
Make tests work on public repo as well
PUBLISHED_FROM=78acb18d597b6f47f31da13087fa6685f21ce445
This commit is contained in:
1 parent
3eb4eb8074
commit
7bf5144e62
145 files changed
+30660
-16
No files matched your search
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
* Copyright (c) 2014-2016 Cesanta Software Limited
|
||||
* All rights reserved
|
||||
*/
|
||||
|
||||
#ifndef CS_COMMON_PLATFORMS_SIMPLELINK_CS_SIMPLELINK_H_
|
||||
#define CS_COMMON_PLATFORMS_SIMPLELINK_CS_SIMPLELINK_H_
|
||||
|
||||
#if defined(MG_NET_IF) && MG_NET_IF == MG_NET_IF_SIMPLELINK
|
||||
|
||||
/* If simplelink.h is already included, all bets are off. */
|
||||
#if !defined(__SIMPLELINK_H__)
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifndef __TI_COMPILER_VERSION__
|
||||
#undef __CONCAT
|
||||
#undef FD_CLR
|
||||
#undef FD_ISSET
|
||||
#undef FD_SET
|
||||
#undef FD_SETSIZE
|
||||
#undef FD_ZERO
|
||||
#undef fd_set
|
||||
#endif
|
||||
|
||||
#if CS_PLATFORM == CS_P_CC3220
|
||||
#include <ti/drivers/net/wifi/porting/user.h>
|
||||
#include <ti/drivers/net/wifi/simplelink.h>
|
||||
#include <ti/drivers/net/wifi/sl_socket.h>
|
||||
#include <ti/drivers/net/wifi/netapp.h>
|
||||
#else
|
||||
/* We want to disable SL_INC_STD_BSD_API_NAMING, so we include user.h ourselves
|
||||
* and undef it. */
|
||||
#define PROVISIONING_API_H_
|
||||
#include <simplelink/user.h>
|
||||
#undef PROVISIONING_API_H_
|
||||
#undef SL_INC_STD_BSD_API_NAMING
|
||||
|
||||
#include <simplelink/include/simplelink.h>
|
||||
#include <simplelink/include/netapp.h>
|
||||
#endif /* CS_PLATFORM == CS_P_CC3220 */
|
||||
|
||||
/* Now define only the subset of the BSD API that we use.
|
||||
* Notably, close(), read() and write() are not defined. */
|
||||
#define AF_INET SL_AF_INET
|
||||
|
||||
#define socklen_t SlSocklen_t
|
||||
#define sockaddr SlSockAddr_t
|
||||
#define sockaddr_in SlSockAddrIn_t
|
||||
#define in_addr SlInAddr_t
|
||||
|
||||
#define SOCK_STREAM SL_SOCK_STREAM
|
||||
#define SOCK_DGRAM SL_SOCK_DGRAM
|
||||
|
||||
#define htonl sl_Htonl
|
||||
#define ntohl sl_Ntohl
|
||||
#define htons sl_Htons
|
||||
#define ntohs sl_Ntohs
|
||||
|
||||
#ifndef EACCES
|
||||
#define EACCES SL_EACCES
|
||||
#endif
|
||||
#ifndef EAFNOSUPPORT
|
||||
#define EAFNOSUPPORT SL_EAFNOSUPPORT
|
||||
#endif
|
||||
#ifndef EAGAIN
|
||||
#define EAGAIN SL_EAGAIN
|
||||
#endif
|
||||
#ifndef EBADF
|
||||
#define EBADF SL_EBADF
|
||||
#endif
|
||||
#ifndef EINVAL
|
||||
#define EINVAL SL_EINVAL
|
||||
#endif
|
||||
#ifndef ENOMEM
|
||||
#define ENOMEM SL_ENOMEM
|
||||
#endif
|
||||
#ifndef EWOULDBLOCK
|
||||
#define EWOULDBLOCK SL_EWOULDBLOCK
|
||||
#endif
|
||||
|
||||
#define SOMAXCONN 8
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
|
||||
char *inet_ntoa(struct in_addr in);
|
||||
int inet_pton(int af, const char *src, void *dst);
|
||||
|
||||
struct mg_mgr;
|
||||
struct mg_connection;
|
||||
|
||||
typedef void (*mg_init_cb)(struct mg_mgr *mgr);
|
||||
bool mg_start_task(int priority, int stack_size, mg_init_cb mg_init);
|
||||
|
||||
void mg_run_in_task(void (*cb)(struct mg_mgr *mgr, void *arg), void *cb_arg);
|
||||
|
||||
int sl_fs_init(void);
|
||||
|
||||
void sl_restart_cb(struct mg_mgr *mgr);
|
||||
|
||||
int sl_set_ssl_opts(int sock, struct mg_connection *nc);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* !defined(__SIMPLELINK_H__) */
|
||||
|
||||
/* Compatibility with older versions of SimpleLink */
|
||||
#if SL_MAJOR_VERSION_NUM < 2
|
||||
|
||||
#define SL_ERROR_BSD_EAGAIN SL_EAGAIN
|
||||
#define SL_ERROR_BSD_EALREADY SL_EALREADY
|
||||
#define SL_ERROR_BSD_ENOPROTOOPT SL_ENOPROTOOPT
|
||||
#define SL_ERROR_BSD_ESECDATEERROR SL_ESECDATEERROR
|
||||
#define SL_ERROR_BSD_ESECSNOVERIFY SL_ESECSNOVERIFY
|
||||
#define SL_ERROR_FS_FAILED_TO_ALLOCATE_MEM SL_FS_ERR_FAILED_TO_ALLOCATE_MEM
|
||||
#define SL_ERROR_FS_FILE_HAS_NOT_BEEN_CLOSE_CORRECTLY \
|
||||
SL_FS_FILE_HAS_NOT_BEEN_CLOSE_CORRECTLY
|
||||
#define SL_ERROR_FS_FILE_NAME_EXIST SL_FS_FILE_NAME_EXIST
|
||||
#define SL_ERROR_FS_FILE_NOT_EXISTS SL_FS_ERR_FILE_NOT_EXISTS
|
||||
#define SL_ERROR_FS_NO_AVAILABLE_NV_INDEX SL_FS_ERR_NO_AVAILABLE_NV_INDEX
|
||||
#define SL_ERROR_FS_NOT_ENOUGH_STORAGE_SPACE SL_FS_ERR_NO_AVAILABLE_BLOCKS
|
||||
#define SL_ERROR_FS_NOT_SUPPORTED SL_FS_ERR_NOT_SUPPORTED
|
||||
#define SL_ERROR_FS_WRONG_FILE_NAME SL_FS_WRONG_FILE_NAME
|
||||
#define SL_ERROR_FS_INVALID_HANDLE SL_FS_ERR_INVALID_HANDLE
|
||||
#define SL_NETCFG_MAC_ADDRESS_GET SL_MAC_ADDRESS_GET
|
||||
#define SL_SOCKET_FD_ZERO SL_FD_ZERO
|
||||
#define SL_SOCKET_FD_SET SL_FD_SET
|
||||
#define SL_SOCKET_FD_ISSET SL_FD_ISSET
|
||||
#define SL_SO_SECURE_DOMAIN_NAME_VERIFICATION SO_SECURE_DOMAIN_NAME_VERIFICATION
|
||||
|
||||
#define SL_FS_READ FS_MODE_OPEN_READ
|
||||
#define SL_FS_WRITE FS_MODE_OPEN_WRITE
|
||||
|
||||
#define SL_FI_FILE_SIZE(fi) ((fi).FileLen)
|
||||
#define SL_FI_FILE_MAX_SIZE(fi) ((fi).AllocatedLen)
|
||||
|
||||
#define SlDeviceVersion_t SlVersionFull
|
||||
#define sl_DeviceGet sl_DevGet
|
||||
#define SL_DEVICE_GENERAL SL_DEVICE_GENERAL_CONFIGURATION
|
||||
#define SL_LEN_TYPE _u8
|
||||
#define SL_OPT_TYPE _u8
|
||||
|
||||
#else /* SL_MAJOR_VERSION_NUM >= 2 */
|
||||
|
||||
#define FS_MODE_OPEN_CREATE(max_size, flag) \
|
||||
(SL_FS_CREATE | SL_FS_CREATE_MAX_SIZE(max_size))
|
||||
#define SL_FI_FILE_SIZE(fi) ((fi).Len)
|
||||
#define SL_FI_FILE_MAX_SIZE(fi) ((fi).MaxSize)
|
||||
|
||||
#define SL_LEN_TYPE _u16
|
||||
#define SL_OPT_TYPE _u16
|
||||
|
||||
#endif /* SL_MAJOR_VERSION_NUM < 2 */
|
||||
|
||||
int slfs_open(const unsigned char *fname, uint32_t flags);
|
||||
|
||||
#endif /* MG_NET_IF == MG_NET_IF_SIMPLELINK */
|
||||
|
||||
#endif /* CS_COMMON_PLATFORMS_SIMPLELINK_CS_SIMPLELINK_H_ */
|
||||
@@ -0,0 +1,411 @@
|
||||
/*
|
||||
* Copyright (c) 2014-2016 Cesanta Software Limited
|
||||
* All rights reserved
|
||||
*/
|
||||
|
||||
#if MG_NET_IF == MG_NET_IF_SIMPLELINK && \
|
||||
(defined(MG_FS_SLFS) || defined(MG_FS_SPIFFS))
|
||||
|
||||
int set_errno(int e) {
|
||||
errno = e;
|
||||
return (e == 0 ? 0 : -1);
|
||||
}
|
||||
|
||||
const char *drop_dir(const char *fname, bool *is_slfs) {
|
||||
if (is_slfs != NULL) {
|
||||
*is_slfs = (strncmp(fname, "SL:", 3) == 0);
|
||||
if (*is_slfs) fname += 3;
|
||||
}
|
||||
/* Drop "./", if any */
|
||||
if (fname[0] == '.' && fname[1] == '/') {
|
||||
fname += 2;
|
||||
}
|
||||
/*
|
||||
* Drop / if it is the only one in the path.
|
||||
* This allows use of /pretend/directories but serves /file.txt as normal.
|
||||
*/
|
||||
if (fname[0] == '/' && strchr(fname + 1, '/') == NULL) {
|
||||
fname++;
|
||||
}
|
||||
return fname;
|
||||
}
|
||||
|
||||
#if !defined(MG_FS_NO_VFS)
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#ifdef __TI_COMPILER_VERSION__
|
||||
#include <file.h>
|
||||
#endif
|
||||
|
||||
#include "common/cs_dbg.h"
|
||||
#include "common/platform.h"
|
||||
|
||||
#ifdef CC3200_FS_SPIFFS
|
||||
#include "cc3200_fs_spiffs.h"
|
||||
#endif
|
||||
|
||||
#ifdef MG_FS_SLFS
|
||||
#include "sl_fs_slfs.h"
|
||||
#endif
|
||||
|
||||
#define NUM_SYS_FDS 3
|
||||
#define SPIFFS_FD_BASE 10
|
||||
#define SLFS_FD_BASE 100
|
||||
|
||||
#if !defined(MG_UART_CHAR_PUT) && !defined(MG_UART_WRITE)
|
||||
#if CS_PLATFORM == CS_P_CC3200
|
||||
#include <inc/hw_types.h>
|
||||
#include <inc/hw_memmap.h>
|
||||
#include <driverlib/rom.h>
|
||||
#include <driverlib/rom_map.h>
|
||||
#include <driverlib/uart.h>
|
||||
#define MG_UART_CHAR_PUT(fd, c) MAP_UARTCharPut(UARTA0_BASE, c);
|
||||
#else
|
||||
#define MG_UART_WRITE(fd, buf, len)
|
||||
#endif /* CS_PLATFORM == CS_P_CC3200 */
|
||||
#endif /* !MG_UART_CHAR_PUT */
|
||||
|
||||
enum fd_type {
|
||||
FD_INVALID,
|
||||
FD_SYS,
|
||||
#ifdef CC3200_FS_SPIFFS
|
||||
FD_SPIFFS,
|
||||
#endif
|
||||
#ifdef MG_FS_SLFS
|
||||
FD_SLFS
|
||||
#endif
|
||||
};
|
||||
static int fd_type(int fd) {
|
||||
if (fd >= 0 && fd < NUM_SYS_FDS) return FD_SYS;
|
||||
#ifdef CC3200_FS_SPIFFS
|
||||
if (fd >= SPIFFS_FD_BASE && fd < SPIFFS_FD_BASE + MAX_OPEN_SPIFFS_FILES) {
|
||||
return FD_SPIFFS;
|
||||
}
|
||||
#endif
|
||||
#ifdef MG_FS_SLFS
|
||||
if (fd >= SLFS_FD_BASE && fd < SLFS_FD_BASE + MAX_OPEN_SLFS_FILES) {
|
||||
return FD_SLFS;
|
||||
}
|
||||
#endif
|
||||
return FD_INVALID;
|
||||
}
|
||||
|
||||
#if MG_TI_NO_HOST_INTERFACE
|
||||
int open(const char *pathname, unsigned flags, int mode) {
|
||||
#else
|
||||
int _open(const char *pathname, int flags, mode_t mode) {
|
||||
#endif
|
||||
int fd = -1;
|
||||
bool is_sl;
|
||||
const char *fname = drop_dir(pathname, &is_sl);
|
||||
if (is_sl) {
|
||||
#ifdef MG_FS_SLFS
|
||||
fd = fs_slfs_open(fname, flags, mode);
|
||||
if (fd >= 0) fd += SLFS_FD_BASE;
|
||||
#endif
|
||||
} else {
|
||||
#ifdef CC3200_FS_SPIFFS
|
||||
fd = fs_spiffs_open(fname, flags, mode);
|
||||
if (fd >= 0) fd += SPIFFS_FD_BASE;
|
||||
#endif
|
||||
}
|
||||
LOG(LL_DEBUG,
|
||||
("open(%s, 0x%x) = %d, fname = %s", pathname, flags, fd, fname));
|
||||
return fd;
|
||||
}
|
||||
|
||||
int _stat(const char *pathname, struct stat *st) {
|
||||
int res = -1;
|
||||
bool is_sl;
|
||||
const char *fname = drop_dir(pathname, &is_sl);
|
||||
memset(st, 0, sizeof(*st));
|
||||
/* Simulate statting the root directory. */
|
||||
if (fname[0] == '\0' || strcmp(fname, ".") == 0) {
|
||||
st->st_ino = 0;
|
||||
st->st_mode = S_IFDIR | 0777;
|
||||
st->st_nlink = 1;
|
||||
st->st_size = 0;
|
||||
return 0;
|
||||
}
|
||||
if (is_sl) {
|
||||
#ifdef MG_FS_SLFS
|
||||
res = fs_slfs_stat(fname, st);
|
||||
#endif
|
||||
} else {
|
||||
#ifdef CC3200_FS_SPIFFS
|
||||
res = fs_spiffs_stat(fname, st);
|
||||
#endif
|
||||
}
|
||||
LOG(LL_DEBUG, ("stat(%s) = %d; fname = %s", pathname, res, fname));
|
||||
return res;
|
||||
}
|
||||
|
||||
#if MG_TI_NO_HOST_INTERFACE
|
||||
int close(int fd) {
|
||||
#else
|
||||
int _close(int fd) {
|
||||
#endif
|
||||
int r = -1;
|
||||
switch (fd_type(fd)) {
|
||||
case FD_INVALID:
|
||||
r = set_errno(EBADF);
|
||||
break;
|
||||
case FD_SYS:
|
||||
r = set_errno(EACCES);
|
||||
break;
|
||||
#ifdef CC3200_FS_SPIFFS
|
||||
case FD_SPIFFS:
|
||||
r = fs_spiffs_close(fd - SPIFFS_FD_BASE);
|
||||
break;
|
||||
#endif
|
||||
#ifdef MG_FS_SLFS
|
||||
case FD_SLFS:
|
||||
r = fs_slfs_close(fd - SLFS_FD_BASE);
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
DBG(("close(%d) = %d", fd, r));
|
||||
return r;
|
||||
}
|
||||
|
||||
#if MG_TI_NO_HOST_INTERFACE
|
||||
off_t lseek(int fd, off_t offset, int whence) {
|
||||
#else
|
||||
off_t _lseek(int fd, off_t offset, int whence) {
|
||||
#endif
|
||||
int r = -1;
|
||||
switch (fd_type(fd)) {
|
||||
case FD_INVALID:
|
||||
r = set_errno(EBADF);
|
||||
break;
|
||||
case FD_SYS:
|
||||
r = set_errno(ESPIPE);
|
||||
break;
|
||||
#ifdef CC3200_FS_SPIFFS
|
||||
case FD_SPIFFS:
|
||||
r = fs_spiffs_lseek(fd - SPIFFS_FD_BASE, offset, whence);
|
||||
break;
|
||||
#endif
|
||||
#ifdef MG_FS_SLFS
|
||||
case FD_SLFS:
|
||||
r = fs_slfs_lseek(fd - SLFS_FD_BASE, offset, whence);
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
DBG(("lseek(%d, %d, %d) = %d", fd, (int) offset, whence, r));
|
||||
return r;
|
||||
}
|
||||
|
||||
int _fstat(int fd, struct stat *s) {
|
||||
int r = -1;
|
||||
memset(s, 0, sizeof(*s));
|
||||
switch (fd_type(fd)) {
|
||||
case FD_INVALID:
|
||||
r = set_errno(EBADF);
|
||||
break;
|
||||
case FD_SYS: {
|
||||
/* Create barely passable stats for STD{IN,OUT,ERR}. */
|
||||
memset(s, 0, sizeof(*s));
|
||||
s->st_ino = fd;
|
||||
s->st_mode = S_IFCHR | 0666;
|
||||
r = 0;
|
||||
break;
|
||||
}
|
||||
#ifdef CC3200_FS_SPIFFS
|
||||
case FD_SPIFFS:
|
||||
r = fs_spiffs_fstat(fd - SPIFFS_FD_BASE, s);
|
||||
break;
|
||||
#endif
|
||||
#ifdef MG_FS_SLFS
|
||||
case FD_SLFS:
|
||||
r = fs_slfs_fstat(fd - SLFS_FD_BASE, s);
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
DBG(("fstat(%d) = %d", fd, r));
|
||||
return r;
|
||||
}
|
||||
|
||||
#if MG_TI_NO_HOST_INTERFACE
|
||||
int read(int fd, char *buf, unsigned count) {
|
||||
#else
|
||||
ssize_t _read(int fd, void *buf, size_t count) {
|
||||
#endif
|
||||
int r = -1;
|
||||
switch (fd_type(fd)) {
|
||||
case FD_INVALID:
|
||||
r = set_errno(EBADF);
|
||||
break;
|
||||
case FD_SYS: {
|
||||
if (fd != 0) {
|
||||
r = set_errno(EACCES);
|
||||
break;
|
||||
}
|
||||
/* Should we allow reading from stdin = uart? */
|
||||
r = set_errno(ENOTSUP);
|
||||
break;
|
||||
}
|
||||
#ifdef CC3200_FS_SPIFFS
|
||||
case FD_SPIFFS:
|
||||
r = fs_spiffs_read(fd - SPIFFS_FD_BASE, buf, count);
|
||||
break;
|
||||
#endif
|
||||
#ifdef MG_FS_SLFS
|
||||
case FD_SLFS:
|
||||
r = fs_slfs_read(fd - SLFS_FD_BASE, buf, count);
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
DBG(("read(%d, %u) = %d", fd, count, r));
|
||||
return r;
|
||||
}
|
||||
|
||||
#if MG_TI_NO_HOST_INTERFACE
|
||||
int write(int fd, const char *buf, unsigned count) {
|
||||
#else
|
||||
ssize_t _write(int fd, const void *buf, size_t count) {
|
||||
#endif
|
||||
int r = -1;
|
||||
switch (fd_type(fd)) {
|
||||
case FD_INVALID:
|
||||
r = set_errno(EBADF);
|
||||
break;
|
||||
case FD_SYS: {
|
||||
if (fd == 0) {
|
||||
r = set_errno(EACCES);
|
||||
break;
|
||||
}
|
||||
#ifdef MG_UART_WRITE
|
||||
MG_UART_WRITE(fd, buf, count);
|
||||
#elif defined(MG_UART_CHAR_PUT)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < count; i++) {
|
||||
const char c = ((const char *) buf)[i];
|
||||
if (c == '\n') MG_UART_CHAR_PUT(fd, '\r');
|
||||
MG_UART_CHAR_PUT(fd, c);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
r = count;
|
||||
break;
|
||||
}
|
||||
#ifdef CC3200_FS_SPIFFS
|
||||
case FD_SPIFFS:
|
||||
r = fs_spiffs_write(fd - SPIFFS_FD_BASE, buf, count);
|
||||
break;
|
||||
#endif
|
||||
#ifdef MG_FS_SLFS
|
||||
case FD_SLFS:
|
||||
r = fs_slfs_write(fd - SLFS_FD_BASE, buf, count);
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
/*
|
||||
* On Newlib we override rename directly too, because the default
|
||||
* implementation using _link and _unlink doesn't work for us.
|
||||
*/
|
||||
#if MG_TI_NO_HOST_INTERFACE || defined(_NEWLIB_VERSION)
|
||||
int rename(const char *frompath, const char *topath) {
|
||||
int r = -1;
|
||||
bool is_sl_from, is_sl_to;
|
||||
const char *from = drop_dir(frompath, &is_sl_from);
|
||||
const char *to = drop_dir(topath, &is_sl_to);
|
||||
if (is_sl_from || is_sl_to) {
|
||||
set_errno(ENOTSUP);
|
||||
} else {
|
||||
#ifdef CC3200_FS_SPIFFS
|
||||
r = fs_spiffs_rename(from, to);
|
||||
#endif
|
||||
}
|
||||
DBG(("rename(%s, %s) = %d", from, to, r));
|
||||
return r;
|
||||
}
|
||||
#endif /* MG_TI_NO_HOST_INTERFACE || defined(_NEWLIB_VERSION) */
|
||||
|
||||
#if MG_TI_NO_HOST_INTERFACE
|
||||
int unlink(const char *pathname) {
|
||||
#else
|
||||
int _unlink(const char *pathname) {
|
||||
#endif
|
||||
int r = -1;
|
||||
bool is_sl;
|
||||
const char *fname = drop_dir(pathname, &is_sl);
|
||||
if (is_sl) {
|
||||
#ifdef MG_FS_SLFS
|
||||
r = fs_slfs_unlink(fname);
|
||||
#endif
|
||||
} else {
|
||||
#ifdef CC3200_FS_SPIFFS
|
||||
r = fs_spiffs_unlink(fname);
|
||||
#endif
|
||||
}
|
||||
DBG(("unlink(%s) = %d, fname = %s", pathname, r, fname));
|
||||
return r;
|
||||
}
|
||||
|
||||
#ifdef CC3200_FS_SPIFFS /* FailFS does not support listing files. */
|
||||
DIR *opendir(const char *dir_name) {
|
||||
DIR *r = NULL;
|
||||
bool is_sl;
|
||||
drop_dir(dir_name, &is_sl);
|
||||
if (is_sl) {
|
||||
r = NULL;
|
||||
set_errno(ENOTSUP);
|
||||
} else {
|
||||
r = fs_spiffs_opendir(dir_name);
|
||||
}
|
||||
DBG(("opendir(%s) = %p", dir_name, r));
|
||||
return r;
|
||||
}
|
||||
|
||||
struct dirent *readdir(DIR *dir) {
|
||||
struct dirent *res = fs_spiffs_readdir(dir);
|
||||
DBG(("readdir(%p) = %p", dir, res));
|
||||
return res;
|
||||
}
|
||||
|
||||
int closedir(DIR *dir) {
|
||||
int res = fs_spiffs_closedir(dir);
|
||||
DBG(("closedir(%p) = %d", dir, res));
|
||||
return res;
|
||||
}
|
||||
|
||||
int rmdir(const char *path) {
|
||||
return fs_spiffs_rmdir(path);
|
||||
}
|
||||
|
||||
int mkdir(const char *path, mode_t mode) {
|
||||
(void) path;
|
||||
(void) mode;
|
||||
/* for spiffs supports only root dir, which comes from mongoose as '.' */
|
||||
return (strlen(path) == 1 && *path == '.') ? 0 : ENOTDIR;
|
||||
}
|
||||
#endif
|
||||
|
||||
int sl_fs_init(void) {
|
||||
int ret = 1;
|
||||
#ifdef __TI_COMPILER_VERSION__
|
||||
#ifdef MG_FS_SLFS
|
||||
#pragma diag_push
|
||||
#pragma diag_suppress 169 /* Nothing we can do about the prototype mismatch. \
|
||||
*/
|
||||
ret = (add_device("SL", _MSA, fs_slfs_open, fs_slfs_close, fs_slfs_read,
|
||||
fs_slfs_write, fs_slfs_lseek, fs_slfs_unlink,
|
||||
fs_slfs_rename) == 0);
|
||||
#pragma diag_pop
|
||||
#endif
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* !defined(MG_FS_NO_VFS) */
|
||||
#endif /* MG_NET_IF == MG_NET_IF_SIMPLELINK && (defined(MG_FS_SLFS) || \
|
||||
defined(MG_FS_SPIFFS)) */
|
||||
@@ -0,0 +1,246 @@
|
||||
/*
|
||||
* Copyright (c) 2014-2016 Cesanta Software Limited
|
||||
* All rights reserved
|
||||
*/
|
||||
|
||||
/* Standard libc interface to TI SimpleLink FS. */
|
||||
|
||||
#if defined(MG_FS_SLFS) || defined(CC3200_FS_SLFS)
|
||||
|
||||
#include "common/platforms/simplelink/sl_fs_slfs.h"
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#if CS_PLATFORM == CS_P_CC3200
|
||||
#include <inc/hw_types.h>
|
||||
#endif
|
||||
|
||||
#include "common/cs_dbg.h"
|
||||
#include "common/mg_mem.h"
|
||||
|
||||
#if SL_MAJOR_VERSION_NUM < 2
|
||||
int slfs_open(const unsigned char *fname, uint32_t flags) {
|
||||
_i32 fh;
|
||||
_i32 r = sl_FsOpen(fname, flags, NULL /* token */, &fh);
|
||||
return (r < 0 ? r : fh);
|
||||
}
|
||||
#else /* SL_MAJOR_VERSION_NUM >= 2 */
|
||||
int slfs_open(const unsigned char *fname, uint32_t flags) {
|
||||
return sl_FsOpen(fname, flags, NULL /* token */);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* From sl_fs.c */
|
||||
int set_errno(int e);
|
||||
const char *drop_dir(const char *fname, bool *is_slfs);
|
||||
|
||||
/*
|
||||
* With SLFS, you have to pre-declare max file size. Yes. Really.
|
||||
* 64K should be enough for everyone. Right?
|
||||
*/
|
||||
#ifndef FS_SLFS_MAX_FILE_SIZE
|
||||
#define FS_SLFS_MAX_FILE_SIZE (64 * 1024)
|
||||
#endif
|
||||
|
||||
struct sl_file_size_hint {
|
||||
char *name;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
struct sl_fd_info {
|
||||
_i32 fh;
|
||||
_off_t pos;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
static struct sl_fd_info s_sl_fds[MAX_OPEN_SLFS_FILES];
|
||||
static struct sl_file_size_hint s_sl_file_size_hints[MAX_OPEN_SLFS_FILES];
|
||||
|
||||
static int sl_fs_to_errno(_i32 r) {
|
||||
DBG(("SL error: %d", (int) r));
|
||||
switch (r) {
|
||||
case SL_FS_OK:
|
||||
return 0;
|
||||
case SL_ERROR_FS_FILE_NAME_EXIST:
|
||||
return EEXIST;
|
||||
case SL_ERROR_FS_WRONG_FILE_NAME:
|
||||
return EINVAL;
|
||||
case SL_ERROR_FS_NO_AVAILABLE_NV_INDEX:
|
||||
case SL_ERROR_FS_NOT_ENOUGH_STORAGE_SPACE:
|
||||
return ENOSPC;
|
||||
case SL_ERROR_FS_FAILED_TO_ALLOCATE_MEM:
|
||||
return ENOMEM;
|
||||
case SL_ERROR_FS_FILE_NOT_EXISTS:
|
||||
return ENOENT;
|
||||
case SL_ERROR_FS_NOT_SUPPORTED:
|
||||
return ENOTSUP;
|
||||
}
|
||||
return ENXIO;
|
||||
}
|
||||
|
||||
int fs_slfs_open(const char *pathname, int flags, mode_t mode) {
|
||||
int fd;
|
||||
for (fd = 0; fd < MAX_OPEN_SLFS_FILES; fd++) {
|
||||
if (s_sl_fds[fd].fh <= 0) break;
|
||||
}
|
||||
if (fd >= MAX_OPEN_SLFS_FILES) return set_errno(ENOMEM);
|
||||
struct sl_fd_info *fi = &s_sl_fds[fd];
|
||||
|
||||
/*
|
||||
* Apply path manipulations again, in case we got here directly
|
||||
* (via TI libc's "add_device").
|
||||
*/
|
||||
pathname = drop_dir(pathname, NULL);
|
||||
|
||||
_u32 am = 0;
|
||||
fi->size = (size_t) -1;
|
||||
int rw = (flags & 3);
|
||||
size_t new_size = FS_SLFS_MAX_FILE_SIZE;
|
||||
if (rw == O_RDONLY) {
|
||||
SlFsFileInfo_t sl_fi;
|
||||
_i32 r = sl_FsGetInfo((const _u8 *) pathname, 0, &sl_fi);
|
||||
if (r == SL_FS_OK) {
|
||||
fi->size = SL_FI_FILE_SIZE(sl_fi);
|
||||
}
|
||||
am = SL_FS_READ;
|
||||
} else {
|
||||
if (!(flags & O_TRUNC) || (flags & O_APPEND)) {
|
||||
// FailFS files cannot be opened for append and will be truncated
|
||||
// when opened for write.
|
||||
return set_errno(ENOTSUP);
|
||||
}
|
||||
if (flags & O_CREAT) {
|
||||
size_t i;
|
||||
for (i = 0; i < MAX_OPEN_SLFS_FILES; i++) {
|
||||
if (s_sl_file_size_hints[i].name != NULL &&
|
||||
strcmp(s_sl_file_size_hints[i].name, pathname) == 0) {
|
||||
new_size = s_sl_file_size_hints[i].size;
|
||||
MG_FREE(s_sl_file_size_hints[i].name);
|
||||
s_sl_file_size_hints[i].name = NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
am = FS_MODE_OPEN_CREATE(new_size, 0);
|
||||
} else {
|
||||
am = SL_FS_WRITE;
|
||||
}
|
||||
}
|
||||
fi->fh = slfs_open((_u8 *) pathname, am);
|
||||
LOG(LL_DEBUG, ("sl_FsOpen(%s, 0x%x) sz %u = %d", pathname, (int) am,
|
||||
(unsigned int) new_size, (int) fi->fh));
|
||||
int r;
|
||||
if (fi->fh >= 0) {
|
||||
fi->pos = 0;
|
||||
r = fd;
|
||||
} else {
|
||||
r = set_errno(sl_fs_to_errno(fi->fh));
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
int fs_slfs_close(int fd) {
|
||||
struct sl_fd_info *fi = &s_sl_fds[fd];
|
||||
if (fi->fh <= 0) return set_errno(EBADF);
|
||||
_i32 r = sl_FsClose(fi->fh, NULL, NULL, 0);
|
||||
LOG(LL_DEBUG, ("sl_FsClose(%d) = %d", (int) fi->fh, (int) r));
|
||||
s_sl_fds[fd].fh = -1;
|
||||
return set_errno(sl_fs_to_errno(r));
|
||||
}
|
||||
|
||||
ssize_t fs_slfs_read(int fd, void *buf, size_t count) {
|
||||
struct sl_fd_info *fi = &s_sl_fds[fd];
|
||||
if (fi->fh <= 0) return set_errno(EBADF);
|
||||
/* Simulate EOF. sl_FsRead @ file_size return SL_FS_ERR_OFFSET_OUT_OF_RANGE.
|
||||
*/
|
||||
if (fi->pos == fi->size) return 0;
|
||||
_i32 r = sl_FsRead(fi->fh, fi->pos, buf, count);
|
||||
DBG(("sl_FsRead(%d, %d, %d) = %d", (int) fi->fh, (int) fi->pos, (int) count,
|
||||
(int) r));
|
||||
if (r >= 0) {
|
||||
fi->pos += r;
|
||||
return r;
|
||||
}
|
||||
return set_errno(sl_fs_to_errno(r));
|
||||
}
|
||||
|
||||
ssize_t fs_slfs_write(int fd, const void *buf, size_t count) {
|
||||
struct sl_fd_info *fi = &s_sl_fds[fd];
|
||||
if (fi->fh <= 0) return set_errno(EBADF);
|
||||
_i32 r = sl_FsWrite(fi->fh, fi->pos, (_u8 *) buf, count);
|
||||
DBG(("sl_FsWrite(%d, %d, %d) = %d", (int) fi->fh, (int) fi->pos, (int) count,
|
||||
(int) r));
|
||||
if (r >= 0) {
|
||||
fi->pos += r;
|
||||
return r;
|
||||
}
|
||||
return set_errno(sl_fs_to_errno(r));
|
||||
}
|
||||
|
||||
int fs_slfs_stat(const char *pathname, struct stat *s) {
|
||||
SlFsFileInfo_t sl_fi;
|
||||
/*
|
||||
* Apply path manipulations again, in case we got here directly
|
||||
* (via TI libc's "add_device").
|
||||
*/
|
||||
pathname = drop_dir(pathname, NULL);
|
||||
_i32 r = sl_FsGetInfo((const _u8 *) pathname, 0, &sl_fi);
|
||||
if (r == SL_FS_OK) {
|
||||
s->st_mode = S_IFREG | 0666;
|
||||
s->st_nlink = 1;
|
||||
s->st_size = SL_FI_FILE_SIZE(sl_fi);
|
||||
return 0;
|
||||
}
|
||||
return set_errno(sl_fs_to_errno(r));
|
||||
}
|
||||
|
||||
int fs_slfs_fstat(int fd, struct stat *s) {
|
||||
struct sl_fd_info *fi = &s_sl_fds[fd];
|
||||
if (fi->fh <= 0) return set_errno(EBADF);
|
||||
s->st_mode = 0666;
|
||||
s->st_mode = S_IFREG | 0666;
|
||||
s->st_nlink = 1;
|
||||
s->st_size = fi->size;
|
||||
return 0;
|
||||
}
|
||||
|
||||
off_t fs_slfs_lseek(int fd, off_t offset, int whence) {
|
||||
if (s_sl_fds[fd].fh <= 0) return set_errno(EBADF);
|
||||
switch (whence) {
|
||||
case SEEK_SET:
|
||||
s_sl_fds[fd].pos = offset;
|
||||
break;
|
||||
case SEEK_CUR:
|
||||
s_sl_fds[fd].pos += offset;
|
||||
break;
|
||||
case SEEK_END:
|
||||
return set_errno(ENOTSUP);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fs_slfs_unlink(const char *pathname) {
|
||||
/*
|
||||
* Apply path manipulations again, in case we got here directly
|
||||
* (via TI libc's "add_device").
|
||||
*/
|
||||
pathname = drop_dir(pathname, NULL);
|
||||
return set_errno(sl_fs_to_errno(sl_FsDel((const _u8 *) pathname, 0)));
|
||||
}
|
||||
|
||||
int fs_slfs_rename(const char *from, const char *to) {
|
||||
return set_errno(ENOTSUP);
|
||||
}
|
||||
|
||||
void fs_slfs_set_new_file_size(const char *name, size_t size) {
|
||||
int i;
|
||||
for (i = 0; i < MAX_OPEN_SLFS_FILES; i++) {
|
||||
if (s_sl_file_size_hints[i].name == NULL) {
|
||||
DBG(("File size hint: %s %d", name, (int) size));
|
||||
s_sl_file_size_hints[i].name = strdup(name);
|
||||
s_sl_file_size_hints[i].size = size;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* defined(MG_FS_SLFS) || defined(CC3200_FS_SLFS) */
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (c) 2014-2016 Cesanta Software Limited
|
||||
* All rights reserved
|
||||
*/
|
||||
|
||||
#ifndef CS_COMMON_PLATFORMS_SIMPLELINK_SL_FS_SLFS_H_
|
||||
#define CS_COMMON_PLATFORMS_SIMPLELINK_SL_FS_SLFS_H_
|
||||
|
||||
#if defined(MG_FS_SLFS)
|
||||
|
||||
#include <stdio.h>
|
||||
#ifndef __TI_COMPILER_VERSION__
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#endif
|
||||
|
||||
#define MAX_OPEN_SLFS_FILES 8
|
||||
|
||||
/* Indirect libc interface - same functions, different names. */
|
||||
int fs_slfs_open(const char *pathname, int flags, mode_t mode);
|
||||
int fs_slfs_close(int fd);
|
||||
ssize_t fs_slfs_read(int fd, void *buf, size_t count);
|
||||
ssize_t fs_slfs_write(int fd, const void *buf, size_t count);
|
||||
int fs_slfs_stat(const char *pathname, struct stat *s);
|
||||
int fs_slfs_fstat(int fd, struct stat *s);
|
||||
off_t fs_slfs_lseek(int fd, off_t offset, int whence);
|
||||
int fs_slfs_unlink(const char *filename);
|
||||
int fs_slfs_rename(const char *from, const char *to);
|
||||
|
||||
void fs_slfs_set_new_file_size(const char *name, size_t size);
|
||||
|
||||
#endif /* defined(MG_FS_SLFS) */
|
||||
|
||||
#endif /* CS_COMMON_PLATFORMS_SIMPLELINK_SL_FS_SLFS_H_ */
|
||||
@@ -0,0 +1,52 @@
|
||||
#if MG_NET_IF == MG_NET_IF_SIMPLELINK && !defined(MG_SIMPLELINK_NO_OSI)
|
||||
|
||||
#include "mg_task.h"
|
||||
|
||||
#include <oslib/osi.h>
|
||||
|
||||
enum mg_q_msg_type {
|
||||
MG_Q_MSG_CB,
|
||||
};
|
||||
struct mg_q_msg {
|
||||
enum mg_q_msg_type type;
|
||||
void (*cb)(struct mg_mgr *mgr, void *arg);
|
||||
void *arg;
|
||||
};
|
||||
static OsiMsgQ_t s_mg_q;
|
||||
static void mg_task(void *arg);
|
||||
|
||||
bool mg_start_task(int priority, int stack_size, mg_init_cb mg_init) {
|
||||
if (osi_MsgQCreate(&s_mg_q, "MG", sizeof(struct mg_q_msg), 16) != OSI_OK) {
|
||||
return false;
|
||||
}
|
||||
if (osi_TaskCreate(mg_task, (const signed char *) "MG", stack_size,
|
||||
(void *) mg_init, priority, NULL) != OSI_OK) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void mg_task(void *arg) {
|
||||
struct mg_mgr mgr;
|
||||
mg_init_cb mg_init = (mg_init_cb) arg;
|
||||
mg_mgr_init(&mgr, NULL);
|
||||
mg_init(&mgr);
|
||||
while (1) {
|
||||
struct mg_q_msg msg;
|
||||
mg_mgr_poll(&mgr, 1);
|
||||
if (osi_MsgQRead(&s_mg_q, &msg, 1) != OSI_OK) continue;
|
||||
switch (msg.type) {
|
||||
case MG_Q_MSG_CB: {
|
||||
msg.cb(&mgr, msg.arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void mg_run_in_task(void (*cb)(struct mg_mgr *mgr, void *arg), void *cb_arg) {
|
||||
struct mg_q_msg msg = {MG_Q_MSG_CB, cb, cb_arg};
|
||||
osi_MsgQWrite(&s_mg_q, &msg, OSI_NO_WAIT);
|
||||
}
|
||||
|
||||
#endif /* MG_NET_IF == MG_NET_IF_SIMPLELINK && !defined(MG_SIMPLELINK_NO_OSI) \
|
||||
*/
|
||||
@@ -0,0 +1,480 @@
|
||||
/*
|
||||
* Copyright (c) 2014-2016 Cesanta Software Limited
|
||||
* All rights reserved
|
||||
*/
|
||||
|
||||
#include "common/platforms/simplelink/sl_net_if.h"
|
||||
|
||||
#if MG_ENABLE_NET_IF_SIMPLELINK
|
||||
|
||||
#include "mongoose/src/internal.h"
|
||||
#include "mongoose/src/util.h"
|
||||
|
||||
#define MG_TCP_RECV_BUFFER_SIZE 1024
|
||||
#define MG_UDP_RECV_BUFFER_SIZE 1500
|
||||
|
||||
static sock_t mg_open_listening_socket(struct mg_connection *nc,
|
||||
union socket_address *sa, int type,
|
||||
int proto);
|
||||
|
||||
void mg_set_non_blocking_mode(sock_t sock) {
|
||||
SlSockNonblocking_t opt;
|
||||
#if SL_MAJOR_VERSION_NUM < 2
|
||||
opt.NonblockingEnabled = 1;
|
||||
#else
|
||||
opt.NonBlockingEnabled = 1;
|
||||
#endif
|
||||
sl_SetSockOpt(sock, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &opt, sizeof(opt));
|
||||
}
|
||||
|
||||
static int mg_is_error(int n) {
|
||||
return (n < 0 && n != SL_ERROR_BSD_EALREADY && n != SL_ERROR_BSD_EAGAIN);
|
||||
}
|
||||
|
||||
void mg_sl_if_connect_tcp(struct mg_connection *nc,
|
||||
const union socket_address *sa) {
|
||||
int proto = 0;
|
||||
if (nc->flags & MG_F_SSL) proto = SL_SEC_SOCKET;
|
||||
sock_t sock = sl_Socket(AF_INET, SOCK_STREAM, proto);
|
||||
if (sock < 0) {
|
||||
nc->err = sock;
|
||||
goto out;
|
||||
}
|
||||
mg_sock_set(nc, sock);
|
||||
#if MG_ENABLE_SSL
|
||||
nc->err = sl_set_ssl_opts(sock, nc);
|
||||
if (nc->err != 0) goto out;
|
||||
#endif
|
||||
nc->err = sl_Connect(sock, &sa->sa, sizeof(sa->sin));
|
||||
out:
|
||||
DBG(("%p to %s:%d sock %d %d err %d", nc, inet_ntoa(sa->sin.sin_addr),
|
||||
ntohs(sa->sin.sin_port), nc->sock, proto, nc->err));
|
||||
}
|
||||
|
||||
void mg_sl_if_connect_udp(struct mg_connection *nc) {
|
||||
sock_t sock = sl_Socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sock < 0) {
|
||||
nc->err = sock;
|
||||
return;
|
||||
}
|
||||
mg_sock_set(nc, sock);
|
||||
nc->err = 0;
|
||||
}
|
||||
|
||||
int mg_sl_if_listen_tcp(struct mg_connection *nc, union socket_address *sa) {
|
||||
int proto = 0;
|
||||
if (nc->flags & MG_F_SSL) proto = SL_SEC_SOCKET;
|
||||
sock_t sock = mg_open_listening_socket(nc, sa, SOCK_STREAM, proto);
|
||||
if (sock < 0) return sock;
|
||||
mg_sock_set(nc, sock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mg_sl_if_listen_udp(struct mg_connection *nc, union socket_address *sa) {
|
||||
sock_t sock = mg_open_listening_socket(nc, sa, SOCK_DGRAM, 0);
|
||||
if (sock == INVALID_SOCKET) return (errno ? errno : 1);
|
||||
mg_sock_set(nc, sock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void mg_sl_if_tcp_send(struct mg_connection *nc, const void *buf, size_t len) {
|
||||
mbuf_append(&nc->send_mbuf, buf, len);
|
||||
}
|
||||
|
||||
void mg_sl_if_udp_send(struct mg_connection *nc, const void *buf, size_t len) {
|
||||
mbuf_append(&nc->send_mbuf, buf, len);
|
||||
}
|
||||
|
||||
void mg_sl_if_recved(struct mg_connection *nc, size_t len) {
|
||||
(void) nc;
|
||||
(void) len;
|
||||
}
|
||||
|
||||
int mg_sl_if_create_conn(struct mg_connection *nc) {
|
||||
(void) nc;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void mg_sl_if_destroy_conn(struct mg_connection *nc) {
|
||||
if (nc->sock == INVALID_SOCKET) return;
|
||||
/* For UDP, only close outgoing sockets or listeners. */
|
||||
if (!(nc->flags & MG_F_UDP) || nc->listener == NULL) {
|
||||
sl_Close(nc->sock);
|
||||
}
|
||||
nc->sock = INVALID_SOCKET;
|
||||
}
|
||||
|
||||
static int mg_accept_conn(struct mg_connection *lc) {
|
||||
struct mg_connection *nc;
|
||||
union socket_address sa;
|
||||
socklen_t sa_len = sizeof(sa);
|
||||
sock_t sock = sl_Accept(lc->sock, &sa.sa, &sa_len);
|
||||
if (sock < 0) {
|
||||
DBG(("%p: failed to accept: %d", lc, sock));
|
||||
return 0;
|
||||
}
|
||||
nc = mg_if_accept_new_conn(lc);
|
||||
if (nc == NULL) {
|
||||
sl_Close(sock);
|
||||
return 0;
|
||||
}
|
||||
DBG(("%p conn from %s:%d", nc, inet_ntoa(sa.sin.sin_addr),
|
||||
ntohs(sa.sin.sin_port)));
|
||||
mg_sock_set(nc, sock);
|
||||
if (nc->flags & MG_F_SSL) nc->flags |= MG_F_SSL_HANDSHAKE_DONE;
|
||||
mg_if_accept_tcp_cb(nc, &sa, sa_len);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* 'sa' must be an initialized address to bind to */
|
||||
static sock_t mg_open_listening_socket(struct mg_connection *nc,
|
||||
union socket_address *sa, int type,
|
||||
int proto) {
|
||||
int r;
|
||||
socklen_t sa_len =
|
||||
(sa->sa.sa_family == AF_INET) ? sizeof(sa->sin) : sizeof(sa->sin6);
|
||||
sock_t sock = sl_Socket(sa->sa.sa_family, type, proto);
|
||||
if (sock < 0) return sock;
|
||||
if ((r = sl_Bind(sock, &sa->sa, sa_len)) < 0) goto clean;
|
||||
if (type != SOCK_DGRAM) {
|
||||
#if MG_ENABLE_SSL
|
||||
if ((r = sl_set_ssl_opts(sock, nc)) < 0) goto clean;
|
||||
#endif
|
||||
if ((r = sl_Listen(sock, SOMAXCONN)) < 0) goto clean;
|
||||
}
|
||||
mg_set_non_blocking_mode(sock);
|
||||
clean:
|
||||
if (r < 0) {
|
||||
sl_Close(sock);
|
||||
sock = r;
|
||||
}
|
||||
return sock;
|
||||
}
|
||||
|
||||
static void mg_write_to_socket(struct mg_connection *nc) {
|
||||
struct mbuf *io = &nc->send_mbuf;
|
||||
int n = 0;
|
||||
|
||||
if (nc->flags & MG_F_UDP) {
|
||||
n = sl_SendTo(nc->sock, io->buf, io->len, 0, &nc->sa.sa,
|
||||
sizeof(nc->sa.sin));
|
||||
DBG(("%p %d %d %d %s:%hu", nc, nc->sock, n, errno,
|
||||
inet_ntoa(nc->sa.sin.sin_addr), ntohs(nc->sa.sin.sin_port)));
|
||||
} else {
|
||||
n = (int) sl_Send(nc->sock, io->buf, io->len, 0);
|
||||
DBG(("%p %d bytes -> %d", nc, n, nc->sock));
|
||||
}
|
||||
|
||||
if (n > 0) {
|
||||
mg_if_sent_cb(nc, n);
|
||||
} else if (n < 0 && mg_is_error(n)) {
|
||||
/* Something went wrong, drop the connection. */
|
||||
nc->flags |= MG_F_CLOSE_IMMEDIATELY;
|
||||
}
|
||||
}
|
||||
|
||||
MG_INTERNAL size_t recv_avail_size(struct mg_connection *conn, size_t max) {
|
||||
size_t avail;
|
||||
if (conn->recv_mbuf_limit < conn->recv_mbuf.len) return 0;
|
||||
avail = conn->recv_mbuf_limit - conn->recv_mbuf.len;
|
||||
return avail > max ? max : avail;
|
||||
}
|
||||
|
||||
static void mg_handle_tcp_read(struct mg_connection *conn) {
|
||||
int n = 0;
|
||||
char *buf = (char *) MG_MALLOC(MG_TCP_RECV_BUFFER_SIZE);
|
||||
|
||||
if (buf == NULL) {
|
||||
DBG(("OOM"));
|
||||
return;
|
||||
}
|
||||
|
||||
n = (int) sl_Recv(conn->sock, buf,
|
||||
recv_avail_size(conn, MG_TCP_RECV_BUFFER_SIZE), 0);
|
||||
DBG(("%p %d bytes <- %d", conn, n, conn->sock));
|
||||
if (n > 0) {
|
||||
mg_if_recv_tcp_cb(conn, buf, n, 1 /* own */);
|
||||
} else {
|
||||
MG_FREE(buf);
|
||||
}
|
||||
if (n == 0) {
|
||||
/* Orderly shutdown of the socket, try flushing output. */
|
||||
conn->flags |= MG_F_SEND_AND_CLOSE;
|
||||
} else if (mg_is_error(n)) {
|
||||
conn->flags |= MG_F_CLOSE_IMMEDIATELY;
|
||||
}
|
||||
}
|
||||
|
||||
static void mg_handle_udp_read(struct mg_connection *nc) {
|
||||
char *buf = (char *) MG_MALLOC(MG_UDP_RECV_BUFFER_SIZE);
|
||||
if (buf == NULL) return;
|
||||
union socket_address sa;
|
||||
socklen_t sa_len = sizeof(sa);
|
||||
int n = sl_RecvFrom(nc->sock, buf, MG_UDP_RECV_BUFFER_SIZE, 0,
|
||||
(SlSockAddr_t *) &sa, &sa_len);
|
||||
DBG(("%p %d bytes from %s:%d", nc, n, inet_ntoa(nc->sa.sin.sin_addr),
|
||||
ntohs(nc->sa.sin.sin_port)));
|
||||
if (n > 0) {
|
||||
mg_if_recv_udp_cb(nc, buf, n, &sa, sa_len);
|
||||
} else {
|
||||
MG_FREE(buf);
|
||||
}
|
||||
}
|
||||
|
||||
#define _MG_F_FD_CAN_READ 1
|
||||
#define _MG_F_FD_CAN_WRITE 1 << 1
|
||||
#define _MG_F_FD_ERROR 1 << 2
|
||||
|
||||
void mg_mgr_handle_conn(struct mg_connection *nc, int fd_flags, double now) {
|
||||
DBG(("%p fd=%d fd_flags=%d nc_flags=%lu rmbl=%d smbl=%d", nc, nc->sock,
|
||||
fd_flags, nc->flags, (int) nc->recv_mbuf.len, (int) nc->send_mbuf.len));
|
||||
|
||||
if (nc->flags & MG_F_CONNECTING) {
|
||||
if (nc->flags & MG_F_UDP || nc->err != SL_ERROR_BSD_EALREADY) {
|
||||
mg_if_connect_cb(nc, nc->err);
|
||||
} else {
|
||||
/* In SimpleLink, to get status of non-blocking connect() we need to wait
|
||||
* until socket is writable and repeat the call to sl_Connect again,
|
||||
* which will now return the real status. */
|
||||
if (fd_flags & _MG_F_FD_CAN_WRITE) {
|
||||
nc->err = sl_Connect(nc->sock, &nc->sa.sa, sizeof(nc->sa.sin));
|
||||
DBG(("%p conn res=%d", nc, nc->err));
|
||||
if (nc->err == SL_ERROR_BSD_ESECSNOVERIFY ||
|
||||
/* TODO(rojer): Provide API to set the date for verification. */
|
||||
nc->err == SL_ERROR_BSD_ESECDATEERROR
|
||||
#if SL_MAJOR_VERSION_NUM >= 2
|
||||
/* Per SWRU455, this error does not mean verification failed,
|
||||
* it only means that the cert used is not present in the trusted
|
||||
* root CA catalog. Which is perfectly fine. */
|
||||
||
|
||||
nc->err == SL_ERROR_BSD_ESECUNKNOWNROOTCA
|
||||
#endif
|
||||
) {
|
||||
nc->err = 0;
|
||||
}
|
||||
if (nc->flags & MG_F_SSL && nc->err == 0) {
|
||||
nc->flags |= MG_F_SSL_HANDSHAKE_DONE;
|
||||
}
|
||||
mg_if_connect_cb(nc, nc->err);
|
||||
}
|
||||
}
|
||||
/* Ignore read/write in further processing, we've handled it. */
|
||||
fd_flags &= ~(_MG_F_FD_CAN_READ | _MG_F_FD_CAN_WRITE);
|
||||
}
|
||||
|
||||
if (fd_flags & _MG_F_FD_CAN_READ) {
|
||||
if (nc->flags & MG_F_UDP) {
|
||||
mg_handle_udp_read(nc);
|
||||
} else {
|
||||
if (nc->flags & MG_F_LISTENING) {
|
||||
mg_accept_conn(nc);
|
||||
} else {
|
||||
mg_handle_tcp_read(nc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!(nc->flags & MG_F_CLOSE_IMMEDIATELY)) {
|
||||
if ((fd_flags & _MG_F_FD_CAN_WRITE) && nc->send_mbuf.len > 0) {
|
||||
mg_write_to_socket(nc);
|
||||
}
|
||||
|
||||
if (!(fd_flags & (_MG_F_FD_CAN_READ | _MG_F_FD_CAN_WRITE))) {
|
||||
mg_if_poll(nc, now);
|
||||
}
|
||||
mg_if_timer(nc, now);
|
||||
}
|
||||
|
||||
DBG(("%p after fd=%d nc_flags=%lu rmbl=%d smbl=%d", nc, nc->sock, nc->flags,
|
||||
(int) nc->recv_mbuf.len, (int) nc->send_mbuf.len));
|
||||
}
|
||||
|
||||
/* Associate a socket to a connection. */
|
||||
void mg_sl_if_sock_set(struct mg_connection *nc, sock_t sock) {
|
||||
mg_set_non_blocking_mode(sock);
|
||||
nc->sock = sock;
|
||||
DBG(("%p %d", nc, sock));
|
||||
}
|
||||
|
||||
void mg_sl_if_init(struct mg_iface *iface) {
|
||||
(void) iface;
|
||||
DBG(("%p using sl_Select()", iface->mgr));
|
||||
}
|
||||
|
||||
void mg_sl_if_free(struct mg_iface *iface) {
|
||||
(void) iface;
|
||||
}
|
||||
|
||||
void mg_sl_if_add_conn(struct mg_connection *nc) {
|
||||
(void) nc;
|
||||
}
|
||||
|
||||
void mg_sl_if_remove_conn(struct mg_connection *nc) {
|
||||
(void) nc;
|
||||
}
|
||||
|
||||
time_t mg_sl_if_poll(struct mg_iface *iface, int timeout_ms) {
|
||||
struct mg_mgr *mgr = iface->mgr;
|
||||
double now = mg_time();
|
||||
double min_timer;
|
||||
struct mg_connection *nc, *tmp;
|
||||
struct SlTimeval_t tv;
|
||||
SlFdSet_t read_set, write_set, err_set;
|
||||
sock_t max_fd = INVALID_SOCKET;
|
||||
int num_fds, num_ev = 0, num_timers = 0;
|
||||
|
||||
SL_SOCKET_FD_ZERO(&read_set);
|
||||
SL_SOCKET_FD_ZERO(&write_set);
|
||||
SL_SOCKET_FD_ZERO(&err_set);
|
||||
|
||||
/*
|
||||
* Note: it is ok to have connections with sock == INVALID_SOCKET in the list,
|
||||
* e.g. timer-only "connections".
|
||||
*/
|
||||
min_timer = 0;
|
||||
for (nc = mgr->active_connections, num_fds = 0; nc != NULL; nc = tmp) {
|
||||
tmp = nc->next;
|
||||
|
||||
if (nc->sock != INVALID_SOCKET) {
|
||||
num_fds++;
|
||||
|
||||
if (!(nc->flags & MG_F_WANT_WRITE) &&
|
||||
nc->recv_mbuf.len < nc->recv_mbuf_limit &&
|
||||
(!(nc->flags & MG_F_UDP) || nc->listener == NULL)) {
|
||||
SL_SOCKET_FD_SET(nc->sock, &read_set);
|
||||
if (max_fd == INVALID_SOCKET || nc->sock > max_fd) max_fd = nc->sock;
|
||||
}
|
||||
|
||||
if (((nc->flags & MG_F_CONNECTING) && !(nc->flags & MG_F_WANT_READ)) ||
|
||||
(nc->send_mbuf.len > 0 && !(nc->flags & MG_F_CONNECTING))) {
|
||||
SL_SOCKET_FD_SET(nc->sock, &write_set);
|
||||
SL_SOCKET_FD_SET(nc->sock, &err_set);
|
||||
if (max_fd == INVALID_SOCKET || nc->sock > max_fd) max_fd = nc->sock;
|
||||
}
|
||||
}
|
||||
|
||||
if (nc->ev_timer_time > 0) {
|
||||
if (num_timers == 0 || nc->ev_timer_time < min_timer) {
|
||||
min_timer = nc->ev_timer_time;
|
||||
}
|
||||
num_timers++;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* If there is a timer to be fired earlier than the requested timeout,
|
||||
* adjust the timeout.
|
||||
*/
|
||||
if (num_timers > 0) {
|
||||
double timer_timeout_ms = (min_timer - mg_time()) * 1000 + 1 /* rounding */;
|
||||
if (timer_timeout_ms < timeout_ms) {
|
||||
timeout_ms = timer_timeout_ms;
|
||||
}
|
||||
}
|
||||
if (timeout_ms < 0) timeout_ms = 0;
|
||||
|
||||
tv.tv_sec = timeout_ms / 1000;
|
||||
tv.tv_usec = (timeout_ms % 1000) * 1000;
|
||||
|
||||
if (num_fds > 0) {
|
||||
num_ev = sl_Select((int) max_fd + 1, &read_set, &write_set, &err_set, &tv);
|
||||
}
|
||||
|
||||
now = mg_time();
|
||||
DBG(("sl_Select @ %ld num_ev=%d of %d, timeout=%d", (long) now, num_ev,
|
||||
num_fds, timeout_ms));
|
||||
|
||||
for (nc = mgr->active_connections; nc != NULL; nc = tmp) {
|
||||
int fd_flags = 0;
|
||||
if (nc->sock != INVALID_SOCKET) {
|
||||
if (num_ev > 0) {
|
||||
fd_flags =
|
||||
(SL_SOCKET_FD_ISSET(nc->sock, &read_set) &&
|
||||
(!(nc->flags & MG_F_UDP) || nc->listener == NULL)
|
||||
? _MG_F_FD_CAN_READ
|
||||
: 0) |
|
||||
(SL_SOCKET_FD_ISSET(nc->sock, &write_set) ? _MG_F_FD_CAN_WRITE
|
||||
: 0) |
|
||||
(SL_SOCKET_FD_ISSET(nc->sock, &err_set) ? _MG_F_FD_ERROR : 0);
|
||||
}
|
||||
/* SimpleLink does not report UDP sockets as writable. */
|
||||
if (nc->flags & MG_F_UDP && nc->send_mbuf.len > 0) {
|
||||
fd_flags |= _MG_F_FD_CAN_WRITE;
|
||||
}
|
||||
}
|
||||
tmp = nc->next;
|
||||
mg_mgr_handle_conn(nc, fd_flags, now);
|
||||
}
|
||||
|
||||
for (nc = mgr->active_connections; nc != NULL; nc = tmp) {
|
||||
tmp = nc->next;
|
||||
if ((nc->flags & MG_F_CLOSE_IMMEDIATELY) ||
|
||||
(nc->send_mbuf.len == 0 && (nc->flags & MG_F_SEND_AND_CLOSE))) {
|
||||
mg_close_conn(nc);
|
||||
}
|
||||
}
|
||||
|
||||
return now;
|
||||
}
|
||||
|
||||
void mg_sl_if_get_conn_addr(struct mg_connection *nc, int remote,
|
||||
union socket_address *sa) {
|
||||
/* SimpleLink does not provide a way to get socket's peer address after
|
||||
* accept or connect. Address should have been preserved in the connection,
|
||||
* so we do our best here by using it. */
|
||||
if (remote) memcpy(sa, &nc->sa, sizeof(*sa));
|
||||
}
|
||||
|
||||
void sl_restart_cb(struct mg_mgr *mgr) {
|
||||
/*
|
||||
* SimpleLink has been restarted, meaning all sockets have been invalidated.
|
||||
* We try our best - we'll restart the listeners, but for outgoing
|
||||
* connections we have no option but to terminate.
|
||||
*/
|
||||
struct mg_connection *nc;
|
||||
for (nc = mg_next(mgr, NULL); nc != NULL; nc = mg_next(mgr, nc)) {
|
||||
if (nc->sock == INVALID_SOCKET) continue; /* Could be a timer */
|
||||
if (nc->flags & MG_F_LISTENING) {
|
||||
DBG(("restarting %p %s:%d", nc, inet_ntoa(nc->sa.sin.sin_addr),
|
||||
ntohs(nc->sa.sin.sin_port)));
|
||||
int res = (nc->flags & MG_F_UDP ? mg_sl_if_listen_udp(nc, &nc->sa)
|
||||
: mg_sl_if_listen_tcp(nc, &nc->sa));
|
||||
if (res == 0) continue;
|
||||
/* Well, we tried and failed. Fall through to closing. */
|
||||
}
|
||||
nc->sock = INVALID_SOCKET;
|
||||
DBG(("terminating %p %s:%d", nc, inet_ntoa(nc->sa.sin.sin_addr),
|
||||
ntohs(nc->sa.sin.sin_port)));
|
||||
/* TODO(rojer): Outgoing UDP? */
|
||||
nc->flags |= MG_F_CLOSE_IMMEDIATELY;
|
||||
}
|
||||
}
|
||||
|
||||
/* clang-format off */
|
||||
#define MG_SL_IFACE_VTABLE \
|
||||
{ \
|
||||
mg_sl_if_init, \
|
||||
mg_sl_if_free, \
|
||||
mg_sl_if_add_conn, \
|
||||
mg_sl_if_remove_conn, \
|
||||
mg_sl_if_poll, \
|
||||
mg_sl_if_listen_tcp, \
|
||||
mg_sl_if_listen_udp, \
|
||||
mg_sl_if_connect_tcp, \
|
||||
mg_sl_if_connect_udp, \
|
||||
mg_sl_if_tcp_send, \
|
||||
mg_sl_if_udp_send, \
|
||||
mg_sl_if_recved, \
|
||||
mg_sl_if_create_conn, \
|
||||
mg_sl_if_destroy_conn, \
|
||||
mg_sl_if_sock_set, \
|
||||
mg_sl_if_get_conn_addr, \
|
||||
}
|
||||
/* clang-format on */
|
||||
|
||||
const struct mg_iface_vtable mg_simplelink_iface_vtable = MG_SL_IFACE_VTABLE;
|
||||
#if MG_NET_IF == MG_NET_IF_SIMPLELINK
|
||||
const struct mg_iface_vtable mg_default_iface_vtable = MG_SL_IFACE_VTABLE;
|
||||
#endif
|
||||
|
||||
#endif /* MG_ENABLE_NET_IF_SIMPLELINK */
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright (c) 2014-2016 Cesanta Software Limited
|
||||
* All rights reserved
|
||||
*/
|
||||
|
||||
#ifndef CS_COMMON_PLATFORMS_SIMPLELINK_SL_NET_IF_H_
|
||||
#define CS_COMMON_PLATFORMS_SIMPLELINK_SL_NET_IF_H_
|
||||
|
||||
#include "mongoose/src/net_if.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#ifndef MG_ENABLE_NET_IF_SIMPLELINK
|
||||
#define MG_ENABLE_NET_IF_SIMPLELINK MG_NET_IF == MG_NET_IF_SIMPLELINK
|
||||
#endif
|
||||
|
||||
extern const struct mg_iface_vtable mg_simplelink_iface_vtable;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* CS_COMMON_PLATFORMS_SIMPLELINK_SL_NET_IF_H_ */
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (c) 2014-2016 Cesanta Software Limited
|
||||
* All rights reserved
|
||||
*/
|
||||
|
||||
#if MG_NET_IF == MG_NET_IF_SIMPLELINK
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "common/platform.h"
|
||||
|
||||
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size) {
|
||||
int res;
|
||||
struct in_addr *in = (struct in_addr *) src;
|
||||
if (af != AF_INET) {
|
||||
errno = ENOTSUP;
|
||||
return NULL;
|
||||
}
|
||||
res = snprintf(dst, size, "%lu.%lu.%lu.%lu", SL_IPV4_BYTE(in->s_addr, 0),
|
||||
SL_IPV4_BYTE(in->s_addr, 1), SL_IPV4_BYTE(in->s_addr, 2),
|
||||
SL_IPV4_BYTE(in->s_addr, 3));
|
||||
return res > 0 ? dst : NULL;
|
||||
}
|
||||
|
||||
char *inet_ntoa(struct in_addr n) {
|
||||
static char a[16];
|
||||
return (char *) inet_ntop(AF_INET, &n, a, sizeof(a));
|
||||
}
|
||||
|
||||
int inet_pton(int af, const char *src, void *dst) {
|
||||
uint32_t a0, a1, a2, a3;
|
||||
uint8_t *db = (uint8_t *) dst;
|
||||
if (af != AF_INET) {
|
||||
errno = ENOTSUP;
|
||||
return 0;
|
||||
}
|
||||
if (sscanf(src, "%lu.%lu.%lu.%lu", &a0, &a1, &a2, &a3) != 4) {
|
||||
return 0;
|
||||
}
|
||||
*db = a3;
|
||||
*(db + 1) = a2;
|
||||
*(db + 2) = a1;
|
||||
*(db + 3) = a0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
#endif /* MG_NET_IF == MG_NET_IF_SIMPLELINK */
|
||||
@@ -0,0 +1,211 @@
|
||||
/*
|
||||
* Copyright (c) 2014-2016 Cesanta Software Limited
|
||||
* All rights reserved
|
||||
*/
|
||||
|
||||
#if MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_SIMPLELINK
|
||||
|
||||
#include "common/mg_mem.h"
|
||||
|
||||
#ifndef MG_SSL_IF_SIMPLELINK_SLFS_PREFIX
|
||||
#define MG_SSL_IF_SIMPLELINK_SLFS_PREFIX "SL:"
|
||||
#endif
|
||||
|
||||
#define MG_SSL_IF_SIMPLELINK_SLFS_PREFIX_LEN \
|
||||
(sizeof(MG_SSL_IF_SIMPLELINK_SLFS_PREFIX) - 1)
|
||||
|
||||
struct mg_ssl_if_ctx {
|
||||
char *ssl_cert;
|
||||
char *ssl_key;
|
||||
char *ssl_ca_cert;
|
||||
char *ssl_server_name;
|
||||
};
|
||||
|
||||
void mg_ssl_if_init() {
|
||||
}
|
||||
|
||||
enum mg_ssl_if_result mg_ssl_if_conn_init(
|
||||
struct mg_connection *nc, const struct mg_ssl_if_conn_params *params,
|
||||
const char **err_msg) {
|
||||
struct mg_ssl_if_ctx *ctx =
|
||||
(struct mg_ssl_if_ctx *) MG_CALLOC(1, sizeof(*ctx));
|
||||
if (ctx == NULL) {
|
||||
MG_SET_PTRPTR(err_msg, "Out of memory");
|
||||
return MG_SSL_ERROR;
|
||||
}
|
||||
nc->ssl_if_data = ctx;
|
||||
|
||||
if (params->cert != NULL || params->key != NULL) {
|
||||
if (params->cert != NULL && params->key != NULL) {
|
||||
ctx->ssl_cert = strdup(params->cert);
|
||||
ctx->ssl_key = strdup(params->key);
|
||||
} else {
|
||||
MG_SET_PTRPTR(err_msg, "Both cert and key are required.");
|
||||
return MG_SSL_ERROR;
|
||||
}
|
||||
}
|
||||
if (params->ca_cert != NULL && strcmp(params->ca_cert, "*") != 0) {
|
||||
ctx->ssl_ca_cert = strdup(params->ca_cert);
|
||||
}
|
||||
/* TODO(rojer): cipher_suites. */
|
||||
if (params->server_name != NULL) {
|
||||
ctx->ssl_server_name = strdup(params->server_name);
|
||||
}
|
||||
return MG_SSL_OK;
|
||||
}
|
||||
|
||||
void mg_ssl_if_conn_close_notify(struct mg_connection *nc) {
|
||||
/* Nothing to do */
|
||||
(void) nc;
|
||||
}
|
||||
|
||||
void mg_ssl_if_conn_free(struct mg_connection *nc) {
|
||||
struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data;
|
||||
if (ctx == NULL) return;
|
||||
nc->ssl_if_data = NULL;
|
||||
MG_FREE(ctx->ssl_cert);
|
||||
MG_FREE(ctx->ssl_key);
|
||||
MG_FREE(ctx->ssl_ca_cert);
|
||||
MG_FREE(ctx->ssl_server_name);
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
MG_FREE(ctx);
|
||||
}
|
||||
|
||||
bool pem_to_der(const char *pem_file, const char *der_file) {
|
||||
bool ret = false;
|
||||
FILE *pf = NULL, *df = NULL;
|
||||
bool writing = false;
|
||||
pf = fopen(pem_file, "r");
|
||||
if (pf == NULL) goto clean;
|
||||
remove(der_file);
|
||||
fs_slfs_set_new_file_size(der_file + MG_SSL_IF_SIMPLELINK_SLFS_PREFIX_LEN,
|
||||
2048);
|
||||
df = fopen(der_file, "w");
|
||||
if (df == NULL) goto clean;
|
||||
while (1) {
|
||||
char pem_buf[70];
|
||||
char der_buf[48];
|
||||
if (!fgets(pem_buf, sizeof(pem_buf), pf)) break;
|
||||
if (writing) {
|
||||
if (strstr(pem_buf, "-----END ") != NULL) {
|
||||
ret = true;
|
||||
break;
|
||||
}
|
||||
int l = 0;
|
||||
while (!isspace((unsigned int) pem_buf[l])) l++;
|
||||
int der_len = 0;
|
||||
cs_base64_decode((const unsigned char *) pem_buf, sizeof(pem_buf),
|
||||
der_buf, &der_len);
|
||||
if (der_len <= 0) break;
|
||||
if (fwrite(der_buf, 1, der_len, df) != der_len) break;
|
||||
} else if (strstr(pem_buf, "-----BEGIN ") != NULL) {
|
||||
writing = true;
|
||||
}
|
||||
}
|
||||
|
||||
clean:
|
||||
if (pf != NULL) fclose(pf);
|
||||
if (df != NULL) {
|
||||
fclose(df);
|
||||
if (!ret) remove(der_file);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if MG_ENABLE_FILESYSTEM && defined(MG_FS_SLFS)
|
||||
/* If the file's extension is .pem, convert it to DER format and put on SLFS. */
|
||||
static char *sl_pem2der(const char *pem_file) {
|
||||
const char *pem_ext = strstr(pem_file, ".pem");
|
||||
if (pem_ext == NULL || *(pem_ext + 4) != '\0') {
|
||||
return strdup(pem_file);
|
||||
}
|
||||
char *der_file = NULL;
|
||||
/* DER file must be located on SLFS, add prefix. */
|
||||
int l = mg_asprintf(&der_file, 0, MG_SSL_IF_SIMPLELINK_SLFS_PREFIX "%.*s.der",
|
||||
(int) (pem_ext - pem_file), pem_file);
|
||||
if (der_file == NULL) return NULL;
|
||||
bool result = false;
|
||||
cs_stat_t st;
|
||||
if (mg_stat(der_file, &st) != 0) {
|
||||
result = pem_to_der(pem_file, der_file);
|
||||
LOG(LL_DEBUG, ("%s -> %s = %d", pem_file, der_file, result));
|
||||
} else {
|
||||
/* File exists, assume it's already been converted. */
|
||||
result = true;
|
||||
}
|
||||
if (result) {
|
||||
/* Strip the SL: prefix we added since NWP does not expect it. */
|
||||
memmove(der_file, der_file + MG_SSL_IF_SIMPLELINK_SLFS_PREFIX_LEN,
|
||||
l - 2 /* including \0 */);
|
||||
} else {
|
||||
MG_FREE(der_file);
|
||||
der_file = NULL;
|
||||
}
|
||||
return der_file;
|
||||
}
|
||||
#else
|
||||
static char *sl_pem2der(const char *pem_file) {
|
||||
return strdup(pem_file);
|
||||
}
|
||||
#endif
|
||||
|
||||
int sl_set_ssl_opts(int sock, struct mg_connection *nc) {
|
||||
int err;
|
||||
const struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data;
|
||||
DBG(("%p ssl ctx: %p", nc, ctx));
|
||||
|
||||
if (ctx != NULL) {
|
||||
DBG(("%p %s,%s,%s,%s", nc, (ctx->ssl_cert ? ctx->ssl_cert : "-"),
|
||||
(ctx->ssl_key ? ctx->ssl_cert : "-"),
|
||||
(ctx->ssl_ca_cert ? ctx->ssl_ca_cert : "-"),
|
||||
(ctx->ssl_server_name ? ctx->ssl_server_name : "-")));
|
||||
if (ctx->ssl_cert != NULL && ctx->ssl_key != NULL) {
|
||||
char *ssl_cert = sl_pem2der(ctx->ssl_cert);
|
||||
char *ssl_key = sl_pem2der(ctx->ssl_key);
|
||||
if (ssl_cert != NULL && ssl_key != NULL) {
|
||||
err = sl_SetSockOpt(sock, SL_SOL_SOCKET,
|
||||
SL_SO_SECURE_FILES_CERTIFICATE_FILE_NAME, ssl_cert,
|
||||
strlen(ssl_cert));
|
||||
LOG(LL_INFO, ("CERTIFICATE_FILE_NAME %s -> %d", ssl_cert, err));
|
||||
err = sl_SetSockOpt(sock, SL_SOL_SOCKET,
|
||||
SL_SO_SECURE_FILES_PRIVATE_KEY_FILE_NAME, ssl_key,
|
||||
strlen(ssl_key));
|
||||
LOG(LL_INFO, ("PRIVATE_KEY_FILE_NAME %s -> %d", ssl_key, err));
|
||||
} else {
|
||||
err = -1;
|
||||
}
|
||||
MG_FREE(ssl_cert);
|
||||
MG_FREE(ssl_key);
|
||||
if (err != 0) return err;
|
||||
}
|
||||
if (ctx->ssl_ca_cert != NULL) {
|
||||
if (ctx->ssl_ca_cert[0] != '\0') {
|
||||
char *ssl_ca_cert = sl_pem2der(ctx->ssl_ca_cert);
|
||||
if (ssl_ca_cert != NULL) {
|
||||
err = sl_SetSockOpt(sock, SL_SOL_SOCKET,
|
||||
SL_SO_SECURE_FILES_CA_FILE_NAME, ssl_ca_cert,
|
||||
strlen(ssl_ca_cert));
|
||||
LOG(LL_INFO, ("CA_FILE_NAME %s -> %d", ssl_ca_cert, err));
|
||||
} else {
|
||||
err = -1;
|
||||
}
|
||||
MG_FREE(ssl_ca_cert);
|
||||
if (err != 0) return err;
|
||||
}
|
||||
}
|
||||
if (ctx->ssl_server_name != NULL) {
|
||||
err = sl_SetSockOpt(sock, SL_SOL_SOCKET,
|
||||
SL_SO_SECURE_DOMAIN_NAME_VERIFICATION,
|
||||
ctx->ssl_server_name, strlen(ctx->ssl_server_name));
|
||||
DBG(("DOMAIN_NAME_VERIFICATION %s -> %d", ctx->ssl_server_name, err));
|
||||
/* Domain name verificationw as added in a NWP service pack, older
|
||||
* versions return SL_ERROR_BSD_ENOPROTOOPT. There isn't much we can do
|
||||
* about it,
|
||||
* so we ignore the error. */
|
||||
if (err != 0 && err != SL_ERROR_BSD_ENOPROTOOPT) return err;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_SIMPLELINK */
|
||||
Reference in new issue
Block a user