2011-03-20 19:18:19 +01:00
|
|
|
/****************************************************************************
|
2012-09-13 14:36:32 +02:00
|
|
|
* examples/thttpd/thttpd_main.c
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2012-03-04 00:18:34 +01:00
|
|
|
* Copyright (C) 2009-2012 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
|
|
|
* 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 Gregory Nutt 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
2011-03-20 19:18:19 +01:00
|
|
|
#include <netinet/ether.h>
|
|
|
|
|
2014-05-30 20:13:06 +02:00
|
|
|
#include <nuttx/net/arp.h>
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/netlib.h"
|
|
|
|
#include "netutils/thttpd.h"
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2016-07-20 22:02:41 +02:00
|
|
|
#include <nuttx/drivers/ramdisk.h>
|
2012-10-24 22:19:44 +02:00
|
|
|
#include <nuttx/binfmt/binfmt.h>
|
2015-06-02 18:45:48 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_THTTPD_NXFLAT
|
|
|
|
# include <nuttx/binfmt/nxflat.h>
|
|
|
|
#endif
|
|
|
|
|
2015-06-02 22:26:08 +02:00
|
|
|
#ifdef CONFIG_THTTPD_BINFS
|
2015-06-06 20:39:57 +02:00
|
|
|
# include <nuttx/fs/unionfs.h>
|
2015-06-02 22:26:08 +02:00
|
|
|
# include <nuttx/binfmt/builtin.h>
|
|
|
|
#endif
|
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
#ifdef CONFIG_NET_SLIP
|
2012-03-04 00:18:34 +01:00
|
|
|
# include <nuttx/net/net.h>
|
2011-03-20 19:18:19 +01:00
|
|
|
#endif
|
|
|
|
|
2015-06-06 20:39:57 +02:00
|
|
|
#include "content/romfs.h"
|
|
|
|
|
2015-06-02 18:45:48 +02:00
|
|
|
#ifdef CONFIG_THTTPD_NXFLAT
|
|
|
|
# include "content/symtab.h"
|
|
|
|
#endif
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-02 22:06:11 +02:00
|
|
|
* Pre-processor Definitions
|
2011-03-20 19:18:19 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Check configuration. This is not all of the configuration settings that
|
|
|
|
* are required -- only the more obvious.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS < 1
|
|
|
|
# error "You must provide file descriptors via CONFIG_NFILE_DESCRIPTORS in your configuration file"
|
|
|
|
#endif
|
|
|
|
|
2015-06-02 18:45:48 +02:00
|
|
|
#ifdef CONFIG_BINFMT_DISABLE
|
|
|
|
# error "You must not disable loadable modules via CONFIG_BINFMT_DISABLE in your configuration file"
|
2011-03-20 19:18:19 +01:00
|
|
|
#endif
|
|
|
|
|
2015-06-02 18:45:48 +02:00
|
|
|
#ifdef CONFIG_THTTPD_NXFLAT
|
|
|
|
# ifndef CONFIG_NXFLAT
|
|
|
|
# error "You must select CONFIG_NXFLAT in your configuration file"
|
|
|
|
# endif
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2015-06-02 18:45:48 +02:00
|
|
|
# ifndef CONFIG_FS_ROMFS
|
|
|
|
# error "You must select CONFIG_FS_ROMFS in your configuration file"
|
|
|
|
# endif
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2015-06-02 18:45:48 +02:00
|
|
|
# ifdef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
# error "You must not disable mountpoints via CONFIG_DISABLE_MOUNTPOINT in your configuration file"
|
|
|
|
# endif
|
2011-03-20 19:18:19 +01:00
|
|
|
#endif
|
|
|
|
|
2015-06-02 18:45:48 +02:00
|
|
|
#ifdef CONFIG_THTTPD_BINFS
|
2015-06-06 20:39:57 +02:00
|
|
|
# ifndef CONFIG_BUILTIN
|
2015-06-02 18:45:48 +02:00
|
|
|
# error "You must select CONFIG_BUILTIN=y in your configuration file"
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifndef CONFIG_FS_BINFS
|
|
|
|
# error "You must select CONFIG_FS_BINFS=y in your configuration file"
|
|
|
|
# endif
|
2015-06-06 20:39:57 +02:00
|
|
|
|
|
|
|
# ifndef CONFIG_FS_UNIONFS
|
|
|
|
# error "CONFIG_FS_UNIONFS=y is required in this configuration"
|
|
|
|
# endif
|
2015-06-02 18:45:48 +02:00
|
|
|
#endif
|
2015-06-06 20:39:57 +02:00
|
|
|
|
2014-11-15 20:52:39 +01:00
|
|
|
/* Ethernet specific configuration */
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2014-11-15 20:52:39 +01:00
|
|
|
#ifdef CONFIG_NET_ETHERNET
|
2015-06-06 20:39:57 +02:00
|
|
|
/* Use the standard Ethernet device name */
|
2014-11-15 20:52:39 +01:00
|
|
|
|
|
|
|
# define NET_DEVNAME "eth0"
|
|
|
|
|
|
|
|
#else
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2014-11-15 20:52:39 +01:00
|
|
|
/* No Ethernet -> No MAC address operations */
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2012-10-04 19:36:07 +02:00
|
|
|
# undef CONFIG_EXAMPLES_THTTPD_NOMAC
|
2014-11-15 20:52:39 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* SLIP-specific configuration */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_SLIP
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* TTY device to use */
|
|
|
|
|
|
|
|
# ifndef CONFIG_NET_SLIPTTY
|
|
|
|
# define CONFIG_NET_SLIPTTY "/dev/ttyS1"
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# define SLIP_DEVNO 0
|
|
|
|
|
2014-11-15 20:52:39 +01:00
|
|
|
# ifndef NET_DEVNAME
|
|
|
|
# define NET_DEVNAME "sl0"
|
|
|
|
# endif
|
2011-03-20 19:18:19 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Describe the ROMFS file system */
|
|
|
|
|
2015-06-07 02:33:34 +02:00
|
|
|
#define SECTORSIZE 64
|
2011-03-20 19:18:19 +01:00
|
|
|
#define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE)
|
|
|
|
#define ROMFSDEV "/dev/ram0"
|
2015-06-06 20:39:57 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_THTTPD_BINFS
|
|
|
|
# define ROMFS_MOUNTPT "/mnt/tmp1"
|
|
|
|
# define ROMFS_PREFIX NULL
|
|
|
|
# define BINFS_MOUNTPT "/mnt/tmp2"
|
|
|
|
# define BINFS_PREFIX "cgi-bin"
|
|
|
|
# define UNIONFS_MOUNTPT CONFIG_THTTPD_PATH
|
|
|
|
#else
|
|
|
|
# define ROMFS_MOUNTPT CONFIG_THTTPD_PATH
|
|
|
|
#endif
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-06-02 18:45:48 +02:00
|
|
|
#ifdef CONFIG_THTTPD_NXFLAT
|
2011-03-20 19:18:19 +01:00
|
|
|
/* These values must be provided by the user before the THTTPD task daemon
|
|
|
|
* is started:
|
|
|
|
*
|
|
|
|
* g_thttpdsymtab: A symbol table describing all of the symbols exported
|
|
|
|
* from the base system. These symbols are used to bind address references
|
|
|
|
* in CGI programs to NuttX.
|
|
|
|
* g_nsymbols: The number of symbols in g_thttpdsymtab[].
|
|
|
|
*/
|
|
|
|
|
|
|
|
FAR const struct symtab_s *g_thttpdsymtab;
|
|
|
|
int g_thttpdnsymbols;
|
2015-06-02 18:45:48 +02:00
|
|
|
#endif
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-08-30 22:13:50 +02:00
|
|
|
* thttp_main
|
2011-03-20 19:18:19 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-06 17:19:15 +02:00
|
|
|
#ifdef CONFIG_BUILD_KERNEL
|
2014-09-06 17:23:23 +02:00
|
|
|
int main(int argc, FAR char *argv[])
|
2014-09-06 17:19:15 +02:00
|
|
|
#else
|
2012-08-30 22:13:50 +02:00
|
|
|
int thttp_main(int argc, char *argv[])
|
2014-09-06 17:19:15 +02:00
|
|
|
#endif
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
|
|
|
struct in_addr addr;
|
2012-10-04 19:36:07 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_THTTPD_NOMAC
|
2011-03-20 19:18:19 +01:00
|
|
|
uint8_t mac[IFHWADDRLEN];
|
|
|
|
#endif
|
|
|
|
char *thttpd_argv = "thttpd";
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Configure SLIP */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_SLIP
|
|
|
|
ret = slip_initialize(SLIP_DEVNO, CONFIG_NET_SLIPTTY);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("ERROR: SLIP initialization failed: %d\n", ret);
|
2011-03-20 19:18:19 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Many embedded network interfaces must have a software assigned MAC */
|
|
|
|
|
2012-10-04 19:36:07 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_THTTPD_NOMAC
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("Assigning MAC\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
mac[0] = 0x00;
|
|
|
|
mac[1] = 0xe0;
|
2012-07-20 00:32:19 +02:00
|
|
|
mac[2] = 0xde;
|
|
|
|
mac[3] = 0xad;
|
|
|
|
mac[4] = 0xbe;
|
|
|
|
mac[5] = 0xef;
|
2014-07-03 00:52:02 +02:00
|
|
|
netlib_setmacaddr(NET_DEVNAME, mac);
|
2011-03-20 19:18:19 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set up our host address */
|
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("Setup network addresses\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
addr.s_addr = HTONL(CONFIG_THTTPD_IPADDR);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4addr(NET_DEVNAME, &addr);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* Set up the default router address */
|
|
|
|
|
2012-10-04 19:36:07 +02:00
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_THTTPD_DRIPADDR);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_dripv4addr(NET_DEVNAME, &addr);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* Setup the subnet mask */
|
|
|
|
|
2012-10-04 19:36:07 +02:00
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_THTTPD_NETMASK);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4netmask(NET_DEVNAME, &addr);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2017-05-19 17:30:26 +02:00
|
|
|
/* New versions of netlib_set_ipvXaddr will not bring the network up,
|
2017-05-19 17:50:26 +02:00
|
|
|
* So ensure the network is really up at this point.
|
|
|
|
*/
|
2017-05-19 17:30:26 +02:00
|
|
|
|
|
|
|
netlib_ifup("eth0");
|
|
|
|
|
2015-06-02 18:45:48 +02:00
|
|
|
#ifdef CONFIG_THTTPD_NXFLAT
|
2011-03-20 19:18:19 +01:00
|
|
|
/* Initialize the NXFLAT binary loader */
|
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("Initializing the NXFLAT binary loader\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
ret = nxflat_initialize();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("ERROR: Initialization of the NXFLAT loader failed: %d\n", ret);
|
2011-03-20 19:18:19 +01:00
|
|
|
exit(2);
|
|
|
|
}
|
2015-06-06 20:39:57 +02:00
|
|
|
#endif
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* Create a ROM disk for the ROMFS filesystem */
|
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("Registering romdisk\n");
|
2015-06-02 22:26:08 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
ret = romdisk_register(0, (uint8_t*)romfs_img, NSECTORS(romfs_img_len), SECTORSIZE);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("ERROR: romdisk_register failed: %d\n", ret);
|
2015-06-06 20:39:57 +02:00
|
|
|
#ifdef CONFIG_THTTPD_NXFLAT
|
2011-03-20 19:18:19 +01:00
|
|
|
nxflat_uninitialize();
|
2015-06-06 20:39:57 +02:00
|
|
|
#endif
|
2011-03-20 19:18:19 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-06-02 19:55:45 +02:00
|
|
|
/* Mount the ROMFS file system */
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("Mounting ROMFS filesystem at target=%s with source=%s\n",
|
2015-06-06 20:39:57 +02:00
|
|
|
ROMFS_MOUNTPT, ROMFSDEV);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2015-06-06 20:39:57 +02:00
|
|
|
ret = mount(ROMFSDEV, ROMFS_MOUNTPT, "romfs", MS_RDONLY, NULL);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2015-06-02 19:55:45 +02:00
|
|
|
printf("ERROR: mount(%s,%s,romfs) failed: %d\n",
|
2015-06-06 20:39:57 +02:00
|
|
|
ROMFSDEV, ROMFS_MOUNTPT, errno);
|
|
|
|
#ifdef CONFIG_THTTPD_NXFLAT
|
2011-03-20 19:18:19 +01:00
|
|
|
nxflat_uninitialize();
|
2015-06-02 18:45:48 +02:00
|
|
|
#endif
|
2015-06-06 20:39:57 +02:00
|
|
|
}
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2015-06-02 19:55:45 +02:00
|
|
|
#ifdef CONFIG_THTTPD_BINFS
|
2015-06-06 20:39:57 +02:00
|
|
|
/* Initialize the BINFS binary loader */
|
2015-06-02 22:26:08 +02:00
|
|
|
|
|
|
|
printf("Initializing the Built-In binary loader\n");
|
|
|
|
|
|
|
|
ret = builtin_initialize();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
printf("ERROR: Initialization of the Built-In loader failed: %d\n", ret);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2015-06-02 19:55:45 +02:00
|
|
|
/* Mount the BINFS file system */
|
|
|
|
|
2015-06-06 20:39:57 +02:00
|
|
|
printf("Mounting BINFS filesystem at %s\n", BINFS_MOUNTPT);
|
|
|
|
|
|
|
|
ret = mount(NULL, BINFS_MOUNTPT, "binfs", MS_RDONLY, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
printf("ERROR: mount(NULL,%s,binfs) failed: %d\n", BINFS_MOUNTPT, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now create and mount the union file system */
|
|
|
|
|
|
|
|
printf("Creating UNIONFS filesystem at %s\n", UNIONFS_MOUNTPT);
|
2015-06-02 19:55:45 +02:00
|
|
|
|
2015-06-06 20:39:57 +02:00
|
|
|
ret = unionfs_mount(ROMFS_MOUNTPT, ROMFS_PREFIX, BINFS_MOUNTPT, BINFS_PREFIX,
|
|
|
|
UNIONFS_MOUNTPT);
|
2015-06-02 19:55:45 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2015-06-06 20:39:57 +02:00
|
|
|
printf("ERROR: Failed to create the union file system at %s: %d\n", UNIONFS_MOUNTPT, ret);
|
2015-06-02 19:55:45 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
/* Start THTTPD. At present, symbol table info is passed via global variables */
|
|
|
|
|
2015-06-02 18:45:48 +02:00
|
|
|
#ifdef CONFIG_THTTPD_NXFLAT
|
2011-03-20 19:18:19 +01:00
|
|
|
g_thttpdsymtab = exports;
|
|
|
|
g_thttpdnsymbols = NEXPORTS;
|
2015-06-02 18:45:48 +02:00
|
|
|
#endif
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("Starting THTTPD\n");
|
|
|
|
fflush(stdout);
|
2011-03-20 19:18:19 +01:00
|
|
|
thttpd_main(1, &thttpd_argv);
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("THTTPD terminated\n");
|
|
|
|
fflush(stdout);
|
2011-03-20 19:18:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|