2011-03-20 19:18:19 +01:00
|
|
|
/****************************************************************************
|
2021-05-14 06:37:30 +02:00
|
|
|
* apps/examples/thttpd/thttpd_main.c
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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>
|
2015-06-02 18:45:48 +02:00
|
|
|
|
2021-05-14 06:37:30 +02:00
|
|
|
#ifdef CONFIG_THTTPD_NXFLAT
|
|
|
|
# include <sys/boardctl.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
|
|
|
#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-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.
|
|
|
|
*/
|
|
|
|
|
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
|
2021-05-20 01:42:28 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_BOARDCTL_ROMDISK
|
|
|
|
# error "CONFIG_BOARDCTL_ROMDISK should be enabled in the 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
|
2020-05-15 05:03:12 +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
|
|
|
|
2020-05-15 05:03:12 +02: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
|
|
|
|
2020-05-15 05:03:12 +02:00
|
|
|
/* TTY device to use */
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
# 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
|
|
|
|
2020-05-14 05:32:35 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Symbols from Auto-Generated Code
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
extern const unsigned char romfs_img[];
|
|
|
|
extern const unsigned int romfs_img_len;
|
|
|
|
|
2020-06-10 13:34:53 +02:00
|
|
|
#ifdef CONFIG_THTTPD_NXFLAT
|
2020-05-14 05:32:35 +02:00
|
|
|
extern const struct symtab_s g_thttpd_exports[];
|
|
|
|
extern const int g_thttpd_nexports;
|
|
|
|
#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:23:23 +02:00
|
|
|
int main(int argc, FAR char *argv[])
|
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;
|
2021-05-14 06:37:30 +02:00
|
|
|
#ifdef CONFIG_THTTPD_NXFLAT
|
|
|
|
struct boardioc_romdisk_s desc;
|
|
|
|
#endif
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
2020-05-15 05:03:12 +02:00
|
|
|
/* Many embedded network interfaces must have a software assigned MAC */
|
2011-03-20 19:18:19 +01:00
|
|
|
|
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");
|
|
|
|
|
2021-05-14 06:37:30 +02:00
|
|
|
#ifdef CONFIG_THTTPD_NXFLAT
|
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
|
|
|
|
2021-05-14 06:37:30 +02:00
|
|
|
desc.minor = 0; /* Minor device number of the ROM disk. */
|
|
|
|
desc.nsectors = NSECTORS(romfs_img_len); /* The number of sectors in the ROM disk */
|
|
|
|
desc.sectsize = SECTORSIZE; /* The size of one sector in bytes */
|
|
|
|
desc.image = (FAR uint8_t *)romfs_img; /* File system image */
|
|
|
|
|
|
|
|
ret = boardctl(BOARDIOC_ROMDISK, (uintptr_t)&desc);
|
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("ERROR: romdisk_register failed: %d\n", ret);
|
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);
|
|
|
|
}
|
2021-05-14 06:37:30 +02:00
|
|
|
#endif
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2015-06-02 19:55:45 +02:00
|
|
|
#ifdef CONFIG_THTTPD_BINFS
|
|
|
|
/* 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)
|
|
|
|
{
|
2020-05-15 05:03:12 +02:00
|
|
|
printf("ERROR: mount(NULL,%s,binfs) failed: %d\n",
|
|
|
|
BINFS_MOUNTPT, errno);
|
2015-06-06 20:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
2020-05-15 05:03:12 +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)
|
|
|
|
{
|
2020-05-15 05:03:12 +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
|
|
|
|
|
2020-05-15 05:03:12 +02:00
|
|
|
/* Start THTTPD. At present, symbol table info is passed via
|
|
|
|
* global variables.
|
|
|
|
*/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2015-06-02 18:45:48 +02:00
|
|
|
#ifdef CONFIG_THTTPD_NXFLAT
|
2018-07-15 00:36:25 +02:00
|
|
|
g_thttpdsymtab = g_thttpd_exports;
|
2020-05-14 05:32:35 +02:00
|
|
|
g_thttpdnsymbols = g_thttpd_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;
|
|
|
|
}
|