summaryrefslogtreecommitdiff
path: root/src/muniad.cc
blob: e2257608d682fb8268315a95eab45e8d843a0c65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set et sw=2 ts=2: */
/***************************************************************************
 *            muniad.cc
 *
 *  Thu Feb 16 15:03:28 CET 2012
 *  Copyright 2012 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of Munia.
 *
 *  Munia is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Munia is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Munia; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <sys/time.h>
#include <list>
#include <string>
#include <fstream>

#include <libwebsockets.h>

#include "http.h"
#include "munia_proto.h"
#include "hugin.hpp"
#include "nodemanager.h"

static struct lws_protocols protocols[] =
{
	// first protocol must always be HTTP handler
	{	"http-only", callback_http,	0	},
	{ "lws-node-protocol", callback_lws_node, sizeof(struct per_session_data__lws_node) },
	{	nullptr, nullptr, 0 }		// End of list
};

static struct option options[] = {
	{ "help", no_argument, nullptr, 'h' },
	{ "background", no_argument, nullptr, 'b' },
	{ "pidfile", required_argument, nullptr, 'P' },
	{ "port", required_argument, nullptr, 'p' },
	{ "file", required_argument, nullptr, 'f' },
	{ "ssl", no_argument, nullptr, 's' },
	{ "killmask", no_argument, nullptr, 'k' },
	{ "interface", required_argument, nullptr, 'i' },
//  { "dumpfile", required_argument, nullptr, 'd'},
	{ nullptr, 0, 0, 0 }
};

int main(int argc, char **argv)
{
	const char *db_filename = "/tmp/munia.xml";
	int n = 0;
	const char *cert_path =
		LOCAL_RESOURCE_PATH"/libwebsockets-test-server.pem";
	const char *key_path =
		LOCAL_RESOURCE_PATH"/libwebsockets-test-server.key.pem";

	std::string pidfile;
	bool background{false};
	int port = 7681;
	int use_ssl = 0;
	struct lws_context *context;
	int opts = 0;
	char interface_name[128] = "";
	const char * interface = nullptr;
//  std::string dumpfile;

	fprintf(stderr, "Munia test server\n"
	        "(C) Copyright 2012-2019 Bent Bisballe Nyeng (deva@aasimon.org)\n"
	        "                      & Jonas Suhr Christensen (jsc@umbraculum.org)\n"
	        "License GPLv3+: GNU GPL version 3 or later "
	        "<http://gnu.org/licenses/gpl.html>\n"
	        "\n"
	        "This is free software; you are free to change and redistribute it.\n"
	        "There is NO WARRANTY, to the extent permitted by law.\n");

	while(n >= 0)
	{
		n = getopt_long(argc, argv, "ci:khsp:f:P:b", options, nullptr);
		if(n < 0)
		{
			continue;
		}

		switch(n)
		{
		case 's':
			use_ssl = 1;
			break;
//		case 'd':
//			dumpfile = optarg;
		case 'k':
			//opts = LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK;
			break;
		case 'p':
			port = atoi(optarg);
			break;
		case 'P':
			pidfile = optarg;
			break;
		case 'b':
			background = true;
			break;
		case 'f':
			db_filename = strdup(optarg);
			break;
		case 'i':
			strncpy(interface_name, optarg, sizeof interface_name);
			interface_name[(sizeof interface_name) - 1] = '\0';
			interface = interface_name;
			break;
		case 'h':
			fprintf(stderr, "Usage: muniad [--port=<p>] [--ssl] [--file=<f>] [--background] [--pidfile=<f>]\n");
			exit(1);
		}
	}

	//_parse("+all");
	if(background)
	{
		// Fork, chdir to / and redirect stdout and stderr to /dev/null
		daemon(0, 0);
		// Create a new session, make the current process process group leader.
		setsid();
	}

	if(!pidfile.empty())
	{
		std::ofstream fs(pidfile);
		fs << getpid() << '\n';
	}

	node_manager.init(db_filename);

	if(!use_ssl)
	{
		cert_path = key_path = nullptr;
	}

	struct lws_context_creation_info info{};
	info.port = port;
	info.iface = interface;
	info.protocols = protocols;
	//info.extensions = lws_get_internal_extensions(); // DEPRECATED

	if(!use_ssl)
	{
		info.ssl_cert_filepath = cert_path;
		info.ssl_private_key_filepath = nullptr;
	}
	else
	{
		info.ssl_cert_filepath =
			LOCAL_RESOURCE_PATH"/libwebsockets-test-server.pem";
		info.ssl_private_key_filepath =
			LOCAL_RESOURCE_PATH"/libwebsockets-test-server.key.pem";
	}

	info.gid = -1;
	info.uid = -1;
	info.options = opts;

	context = lws_create_context(&info);
	if(context == nullptr)
	{
		fprintf(stderr, "libwebsocket init failed\n");
		return -1;
	}

	while(1)
	{
		lws_service(context, 50);
	}

	lws_context_destroy(context);

	return 0;
}