int status;
int bound = 0;
- int tcp_ka_idl = 1;
- int tcp_ka_int = 1;
- int tcp_ka_cnt = 3;
+ int tcp_ka_idl, tcp_ka_int, tcp_ka_cnt;
struct listener *l = NULL;
struct addrinfo *addrs = NULL, *p = NULL;
}
/* TCP keep-alive */
- if( setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(yes)) ||
- setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, &tcp_ka_idl, sizeof(tcp_ka_idl)) ||
- setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, &tcp_ka_int, sizeof(tcp_ka_int)) ||
- setsockopt(sock, SOL_TCP, TCP_KEEPCNT, &tcp_ka_cnt, sizeof(tcp_ka_cnt)) )
+ if( conf->tcp_keepalive > 0 )
{
- fprintf(stderr, "Notice: Unable to enable TCP keep-alive: %s\n",
- strerror(errno));
+ tcp_ka_idl = 1;
+ tcp_ka_cnt = 3;
+ tcp_ka_int = conf->tcp_keepalive;
+
+ if( setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(yes)) ||
+ setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, &tcp_ka_idl, sizeof(tcp_ka_idl)) ||
+ setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, &tcp_ka_int, sizeof(tcp_ka_int)) ||
+ setsockopt(sock, SOL_TCP, TCP_KEEPCNT, &tcp_ka_cnt, sizeof(tcp_ka_cnt)) )
+ {
+ fprintf(stderr, "Notice: Unable to enable TCP keep-alive: %s\n",
+ strerror(errno));
+ }
}
/* required to get parallel v4 + v6 working */
#endif
}
+#ifdef HAVE_TLS
+static inline uh_inittls(struct config *conf)
+{
+ /* library handle */
+ void *lib;
+
+ /* already loaded */
+ if( conf->tls != NULL )
+ return 0;
+
+ /* load TLS plugin */
+ if( ! (lib = dlopen("uhttpd_tls.so", RTLD_LAZY | RTLD_GLOBAL)) )
+ {
+ fprintf(stderr,
+ "Notice: Unable to load TLS plugin - disabling SSL support! "
+ "(Reason: %s)\n", dlerror()
+ );
+
+ return 1;
+ }
+ else
+ {
+ /* resolve functions */
+ if( !(conf->tls_init = dlsym(lib, "uh_tls_ctx_init")) ||
+ !(conf->tls_cert = dlsym(lib, "uh_tls_ctx_cert")) ||
+ !(conf->tls_key = dlsym(lib, "uh_tls_ctx_key")) ||
+ !(conf->tls_free = dlsym(lib, "uh_tls_ctx_free")) ||
+ !(conf->tls_accept = dlsym(lib, "uh_tls_client_accept")) ||
+ !(conf->tls_close = dlsym(lib, "uh_tls_client_close")) ||
+ !(conf->tls_recv = dlsym(lib, "uh_tls_client_recv")) ||
+ !(conf->tls_send = dlsym(lib, "uh_tls_client_send"))
+ ) {
+ fprintf(stderr,
+ "Error: Failed to lookup required symbols "
+ "in TLS plugin: %s\n", dlerror()
+ );
+ exit(1);
+ }
+
+ /* init SSL context */
+ if( ! (conf->tls = conf->tls_init()) )
+ {
+ fprintf(stderr, "Error: Failed to initalize SSL context\n");
+ exit(1);
+ }
+ }
+
+ return 0;
+}
+#endif
int main (int argc, char **argv)
{
/* master file descriptor list */
- fd_set used_fds, serv_fds, read_fds;
+ fd_set serv_fds;
/* working structs */
struct addrinfo hints;
char bind[128];
char *port = NULL;
-#if defined(HAVE_TLS) || defined(HAVE_LUA)
+#ifdef HAVE_LUA
/* library handle */
void *lib;
#endif
- /* clear the master and temp sets */
- FD_ZERO(&used_fds);
FD_ZERO(&serv_fds);
- FD_ZERO(&read_fds);
/* handle SIGPIPE, SIGINT, SIGTERM, SIGCHLD */
sa.sa_flags = 0;
memset(&conf, 0, sizeof(conf));
memset(bind, 0, sizeof(bind));
-#ifdef HAVE_TLS
- /* load TLS plugin */
- if( ! (lib = dlopen("uhttpd_tls.so", RTLD_LAZY | RTLD_GLOBAL)) )
- {
- fprintf(stderr,
- "Notice: Unable to load TLS plugin - disabling SSL support! "
- "(Reason: %s)\n", dlerror()
- );
- }
- else
- {
- /* resolve functions */
- if( !(conf.tls_init = dlsym(lib, "uh_tls_ctx_init")) ||
- !(conf.tls_cert = dlsym(lib, "uh_tls_ctx_cert")) ||
- !(conf.tls_key = dlsym(lib, "uh_tls_ctx_key")) ||
- !(conf.tls_free = dlsym(lib, "uh_tls_ctx_free")) ||
- !(conf.tls_accept = dlsym(lib, "uh_tls_client_accept")) ||
- !(conf.tls_close = dlsym(lib, "uh_tls_client_close")) ||
- !(conf.tls_recv = dlsym(lib, "uh_tls_client_recv")) ||
- !(conf.tls_send = dlsym(lib, "uh_tls_client_send"))
- ) {
- fprintf(stderr,
- "Error: Failed to lookup required symbols "
- "in TLS plugin: %s\n", dlerror()
- );
- exit(1);
- }
-
- /* init SSL context */
- if( ! (conf.tls = conf.tls_init()) )
- {
- fprintf(stderr, "Error: Failed to initalize SSL context\n");
- exit(1);
- }
- }
-#endif
while( (opt = getopt(argc, argv,
- "fSDRC:K:E:I:p:s:h:c:l:L:d:r:m:x:i:t:T:")) > 0
+ "fSDRC:K:E:I:p:s:h:c:l:L:d:r:m:x:i:t:T:A:")) > 0
) {
switch(opt)
{
#ifdef HAVE_TLS
if( opt == 's' )
{
- if( !conf.tls )
+ if( uh_inittls(&conf) )
{
fprintf(stderr,
"Notice: TLS support is disabled, "
#ifdef HAVE_TLS
/* certificate */
case 'C':
- if( conf.tls )
+ if( !uh_inittls(&conf) )
{
if( conf.tls_cert(conf.tls, optarg) < 1 )
{
/* key */
case 'K':
- if( conf.tls )
+ if( !uh_inittls(&conf) )
{
if( conf.tls_key(conf.tls, optarg) < 1 )
{
conf.network_timeout = atoi(optarg);
break;
+ /* tcp keep-alive */
+ case 'A':
+ conf.tcp_keepalive = atoi(optarg);
+ break;
+
/* no fork */
case 'f':
nofork = 1;
case 'd':
if( (port = malloc(strlen(optarg)+1)) != NULL )
{
+ /* "decode" plus to space to retain compat */
+ for (opt = 0; optarg[opt]; opt++)
+ if (optarg[opt] == '+')
+ optarg[opt] = ' ';
+
memset(port, 0, strlen(optarg)+1);
uh_urldecode(port, strlen(optarg), optarg, strlen(optarg));
+
printf("%s", port);
free(port);
exit(0);