first uses of libtalermhd

This commit is contained in:
Christian Grothoff 2019-11-23 17:53:42 +01:00
parent c693ec4a3e
commit 062bbbb2d7
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC
2 changed files with 16 additions and 258 deletions

View File

@ -63,6 +63,7 @@ taler_exchange_httpd_SOURCES = \
taler_exchange_httpd_LDADD = \
$(LIBGCRYPT_LIBS) \
$(top_builddir)/src/wire/libtalerwire.la \
$(top_builddir)/src/mhd/libtalermhd.la \
$(top_builddir)/src/json/libtalerjson.la \
$(top_builddir)/src/exchangedb/libtalerexchangedb.la \
$(top_builddir)/src/util/libtalerutil.la \

View File

@ -27,6 +27,7 @@
#include <microhttpd.h>
#include <pthread.h>
#include <sys/resource.h>
#include "taler_mhd_lib.h"
#include "taler-exchange-httpd_parsing.h"
#include "taler-exchange-httpd_mhd.h"
#include "taler-exchange-httpd_deposit.h"
@ -486,122 +487,6 @@ handle_mhd_request (void *cls,
}
/**
* Parse the configuration to determine on which port
* or UNIX domain path we should run an HTTP service.
*
* @param section section of the configuration to parse (usually "exchange")
* @param[out] rport set to the port number, or 0 for none
* @param[out] unix_path set to the UNIX path, or NULL for none
* @param[out] unix_mode set to the mode to be used for @a unix_path
* @return #GNUNET_OK on success
*/
static int
parse_port_config (const char *section,
uint16_t *rport,
char **unix_path,
mode_t *unix_mode)
{
const char *choices[] = {"tcp", "unix"};
const char *serve_type;
unsigned long long port;
if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_choice (cfg,
section,
"serve",
choices,
&serve_type))
{
GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
section,
"serve",
"serve type required");
return GNUNET_SYSERR;
}
if (0 == strcasecmp (serve_type, "tcp"))
{
if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_number (cfg,
section,
"port",
&port))
{
GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
section,
"port",
"port number required");
return GNUNET_SYSERR;
}
if ( (0 == port) ||
(port > UINT16_MAX) )
{
fprintf (stderr,
"Invalid configuration (value out of range): %llu is not a valid port\n",
port);
return GNUNET_SYSERR;
}
*rport = (uint16_t) port;
*unix_path = NULL;
return GNUNET_OK;
}
if (0 == strcmp (serve_type, "unix"))
{
struct sockaddr_un s_un;
char *modestring;
if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_filename (cfg,
section,
"unixpath",
unix_path))
{
GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
section,
"unixpath",
"unixpath required");
return GNUNET_SYSERR;
}
if (strlen (*unix_path) >= sizeof (s_un.sun_path))
{
fprintf (stderr,
"Invalid configuration: unix path too long\n");
return GNUNET_SYSERR;
}
if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_string (cfg,
section,
"UNIXPATH_MODE",
&modestring))
{
GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
section,
"UNIXPATH_MODE");
return GNUNET_SYSERR;
}
errno = 0;
*unix_mode = (mode_t) strtoul (modestring, NULL, 8);
if (0 != errno)
{
GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
section,
"UNIXPATH_MODE",
"must be octal number");
GNUNET_free (modestring);
return GNUNET_SYSERR;
}
GNUNET_free (modestring);
return GNUNET_OK;
}
/* not reached */
GNUNET_assert (0);
return GNUNET_SYSERR;
}
/**
* Load configuration parameters for the exchange
* server into the corresponding global variables.
@ -717,7 +602,8 @@ exchange_serve_process_config ()
}
if (GNUNET_OK !=
parse_port_config ("exchange",
TALER_MHD_parse_config (cfg,
"exchange",
&serve_port,
&serve_unixpath,
&unixpath_mode))
@ -843,141 +729,6 @@ connection_done (void *cls,
/* end of HAVE_DEVELOPER */
#endif
/**
* Function called for logging by MHD.
*
* @param cls closure, NULL
* @param fm format string (`printf()`-style)
* @param ap arguments to @a fm
*/
static void
handle_mhd_logs (void *cls,
const char *fm,
va_list ap)
{
static int cache;
char buf[2048];
(void) cls;
if (-1 == cache)
return;
if (0 == cache)
{
if (0 ==
GNUNET_get_log_call_status (GNUNET_ERROR_TYPE_INFO,
"libmicrohttpd",
__FILE__,
__FUNCTION__,
__LINE__))
{
cache = -1;
return;
}
}
cache = 1;
vsnprintf (buf,
sizeof (buf),
fm,
ap);
GNUNET_log_from_nocheck (GNUNET_ERROR_TYPE_INFO,
"libmicrohttpd",
"%s",
buf);
}
/**
* Open UNIX domain socket for listining at @a unix_path with
* permissions @a unix_mode.
*
* @param unix_path where to listen
* @param unix_mode access permissions to set
* @return -1 on error, otherwise the listen socket
*/
static int
open_unix_path (const char *unix_path,
mode_t unix_mode)
{
struct GNUNET_NETWORK_Handle *nh;
struct sockaddr_un *un;
int fd;
if (sizeof (un->sun_path) <= strlen (unix_path))
{
fprintf (stderr,
"unixpath `%s' too long\n",
unix_path);
return -1;
}
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Creating listen socket '%s' with mode %o\n",
unix_path,
unix_mode);
if (GNUNET_OK !=
GNUNET_DISK_directory_create_for_file (unix_path))
{
GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
"mkdir",
unix_path);
}
un = GNUNET_new (struct sockaddr_un);
un->sun_family = AF_UNIX;
strncpy (un->sun_path,
unix_path,
sizeof (un->sun_path) - 1);
GNUNET_NETWORK_unix_precheck (un);
if (NULL == (nh = GNUNET_NETWORK_socket_create (AF_UNIX,
SOCK_STREAM,
0)))
{
fprintf (stderr,
"create failed for AF_UNIX\n");
GNUNET_free (un);
return -1;
}
if (GNUNET_OK !=
GNUNET_NETWORK_socket_bind (nh,
(void *) un,
sizeof (struct sockaddr_un)))
{
fprintf (stderr,
"bind failed for AF_UNIX\n");
GNUNET_free (un);
GNUNET_NETWORK_socket_close (nh);
return -1;
}
GNUNET_free (un);
if (GNUNET_OK !=
GNUNET_NETWORK_socket_listen (nh,
UNIX_BACKLOG))
{
fprintf (stderr,
"listen failed for AF_UNIX\n");
GNUNET_NETWORK_socket_close (nh);
return -1;
}
if (0 != chmod (unix_path,
unix_mode))
{
fprintf (stderr,
"chmod failed: %s\n",
strerror (errno));
GNUNET_NETWORK_socket_close (nh);
return -1;
}
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"set socket '%s' to mode %o\n",
unix_path,
unix_mode);
fd = GNUNET_NETWORK_get_fd (nh);
GNUNET_NETWORK_socket_free_memory_only_ (nh);
return fd;
}
/**
* Called when the main thread exits, writes out performance
@ -1074,12 +825,17 @@ main (int argc,
const char *listen_pid;
const char *listen_fds;
int fh = -1;
enum TALER_MHD_GlobalOptions go;
if (0 >=
GNUNET_GETOPT_run ("taler-exchange-httpd",
options,
argc, argv))
return 1;
go = TALER_MHD_GO_NONE;
if (TEH_exchange_connection_close)
go |= TALER_MHD_GO_FORCE_CONNECTION_CLOSE;
TALER_MHD_setup (go);
GNUNET_assert (GNUNET_OK ==
GNUNET_log_setup ("taler-exchange-httpd",
(NULL == loglev) ? "INFO" : loglev,
@ -1138,7 +894,7 @@ main (int argc,
if ( (-1 == fh) &&
(NULL != serve_unixpath) )
{
fh = open_unix_path (serve_unixpath,
fh = TALER_MHD_open_unix_path (serve_unixpath,
unixpath_mode);
if (-1 == fh)
return 1;
@ -1155,7 +911,8 @@ main (int argc,
MHD_OPTION_THREAD_POOL_SIZE, (unsigned int) 32,
MHD_OPTION_LISTEN_BACKLOG_SIZE, (unsigned int) 1024,
MHD_OPTION_LISTEN_SOCKET, fh,
MHD_OPTION_EXTERNAL_LOGGER, &handle_mhd_logs, NULL,
MHD_OPTION_EXTERNAL_LOGGER, &TALER_MHD_handle_logs,
NULL,
MHD_OPTION_NOTIFY_COMPLETED,
&handle_mhd_completion_callback, NULL,
MHD_OPTION_CONNECTION_TIMEOUT, connection_timeout,