summaryrefslogtreecommitdiffstats
path: root/workspace/networkDiscovery/networkdiscovery.cpp
blob: f2cb2bbacb574f40a91cdbfadfdf8dababc2ec27 (plain) (blame)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "networkdiscovery.h"
#include <QNetworkInterface>

#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <paths.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "config.h"
#include "client.h"
#include "dhcpcd/dhcpcd.h"
#include "dhcp.h"
#include "interface.h"
#include "logger.h"
#include "socket.h"
#include "version.h"

NetworkDiscovery::NetworkDiscovery(QWidget *parent)
    : QWidget(parent)
{
	ui.setupUi(this);
	QList<QNetworkInterface> interfaces = getListOfNetworkInterfaces();

	/**
	 * test if i get some dhcp values for the dummy0 interface
	 */
	doDHCP(interfaces);
}

NetworkDiscovery::~NetworkDiscovery()
{

}

QList<QNetworkInterface> NetworkDiscovery::getListOfNetworkInterfaces() {
	QList<QNetworkInterface> nIList = QNetworkInterface::allInterfaces();
	QList<QNetworkInterface> result;

	foreach(QNetworkInterface nI, nIList) {
		if (((!(nI.flags() & QNetworkInterface::CanBroadcast)||
				nI.flags() & QNetworkInterface::IsLoopBack) ||
				nI.flags() & QNetworkInterface::IsPointToPoint))
		{
			continue;
		}
	   	qDebug() << nI.humanReadableName();
	   	result.append(nI);
	}
	return result;
}

int NetworkDiscovery::doDHCP(QList<QNetworkInterface> interfaces)
{

}

static int atoint (const char *s)
{
	char *t;
	long n;

	errno = 0;
	n = strtol (s, &t, 0);
	if ((errno != 0 && n == 0) || s == t ||
	    (errno == ERANGE && (n == LONG_MAX || n == LONG_MIN)))
	{
		logger (LOG_ERR, "`%s' out of range", s);
		return (-1);
	}

	return ((int) n);
}

static pid_t read_pid (const char *pidfile)
{
	FILE *fp;
	pid_t pid = 0;

	if ((fp = fopen (pidfile, "r")) == NULL) {
		errno = ENOENT;
		return 0;
	}

	fscanf (fp, "%d", &pid);
	fclose (fp);

	return (pid);
}

int NetworkDiscovery::doDhcp(QNetworkInterface interface)
{

	options_t *options;
	int userclasses = 0;
	int opt;
	int option_index = 0;
	char *prefix;
	pid_t pid;
	int debug = 0;
	int i;
	int pidfd = -1;
	int sig = 0;
	int retval = EXIT_FAILURE;

	/* Close any un-needed fd's */
	for (i = getdtablesize() - 1; i >= 3; --i)
		close(i);

	openlog(PACKAGE, LOG_PID, LOG_LOCAL0);

	options = xzalloc(sizeof(*options));
	options->script = (char *) DEFAULT_SCRIPT;
	snprintf(options->classid, CLASS_ID_MAX_LEN, "%s %s", PACKAGE, VERSION);

	options->doarp = true;
	options->dodns = true;
	options->domtu = true;
	options->donis = true;
	options->dontp = true;
	options->dogateway = true;
	options->daemonise = true;
	options->doinform = false;
	options->doipv4ll = true;
	options->doduid = true;
	options->timeout = DEFAULT_TIMEOUT;

	gethostname(options->hostname, sizeof(options->hostname));
	if (strcmp(options->hostname, "(none)") == 0 ||
		strcmp(options->hostname, "localhost") == 0)
		memset(options->hostname, 0, sizeof(options->hostname));


/*
 * kann vermutlich gelöscht werden.

#ifdef THERE_IS_NO_FORK
	dhcpcd_argv = argv;
	dhcpcd_argc = argc;
	if (! realpath (argv[0], dhcpcd)) {
		logger (LOG_ERR, "unable to resolve the path `%s': %s",
				argv[0], strerror (errno));
		goto abort;
	}
#endif
*/

	//TODO:: do something with the .toStdString()!! may cause errors cause char is expected

	if (interface.humanReadableName().length() > IF_NAMESIZE) {

		logger(LOG_ERR, "`%s' too long for an interface name (max=%d)",
				interface.humanReadableName().toStdString(), IF_NAMESIZE);
		goto abort;
	} else {
		strlcpy(options->interface, interface.humanReadableName().toStdString(), sizeof(options->interface));
	}

	if (strchr(options->hostname, '.')) {
		if (options->fqdn == FQDN_DISABLE)
			options->fqdn = FQDN_BOTH;
	} else
		options->fqdn = FQDN_DISABLE;

	if (options->request_address.s_addr == 0 && options->doinform) {
		if ((options->request_address.s_addr = get_address(options->interface))
				!= 0)
			options->keep_address = true;
	}

	if (IN_LINKLOCAL (ntohl (options->request_address.s_addr))) {
		logger(LOG_ERR, "you are not allowed to request a link local address");
		goto abort;
	}

	if (geteuid())
		logger(LOG_WARNING, PACKAGE " will not work correctly unless"
		" run as root");

	prefix = xmalloc(sizeof(char) * (IF_NAMESIZE + 3));
	snprintf(prefix, IF_NAMESIZE, "%s: ", options->interface);
	setlogprefix(prefix);
	snprintf(options->pidfile, sizeof(options->pidfile), PIDFILE,
			options->interface);
	free(prefix);

	chdir("/");
	umask(022);

	if (mkdir(INFODIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP
			| S_IROTH | S_IXOTH) && errno != EEXIST) {
		logger(LOG_ERR, "mkdir(\"%s\",0): %s\n", INFODIR, strerror(errno));
		goto abort;
	}

	if (mkdir(ETCDIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH
			| S_IXOTH) && errno != EEXIST) {
		logger(LOG_ERR, "mkdir(\"%s\",0): %s\n", ETCDIR, strerror(errno));
		goto abort;
	}

	if (options->test) {
		if (options->dorequest || options->doinform) {
			logger(LOG_ERR, "cannot test with --inform or --request");
			goto abort;
		}

		if (options->dolastlease) {
			logger(LOG_ERR, "cannot test with --lastlease");
			goto abort;
		}

		if (sig != 0) {
			logger(LOG_ERR, "cannot test with --release or --renew");
			goto abort;
		}
	}

	if (sig != 0) {
		int killed = -1;
		pid = read_pid(options->pidfile);
		if (pid != 0)
			logger(LOG_INFO, "sending signal %d to pid %d", sig, pid);

		if (!pid || (killed = kill(pid, sig)))
			logger(sig == SIGALRM ? LOG_INFO : LOG_ERR, ""PACKAGE" not running");

		if (pid != 0 && (sig != SIGALRM || killed != 0))
			unlink(options->pidfile);

		if (killed == 0) {
			retval = EXIT_SUCCESS;
			goto abort;
		}

		if (sig != SIGALRM)
			goto abort;
	}

	if (!options->test && !options->daemonised) {
		if ((pid = read_pid(options->pidfile)) > 0 && kill(pid, 0) == 0) {
			logger(LOG_ERR, ""PACKAGE
			" already running on pid %d (%s)", pid, options->pidfile);
			goto abort;
		}

		pidfd = open(options->pidfile, O_WRONLY | O_CREAT | O_NONBLOCK, 0664);
		if (pidfd == -1) {
			logger(LOG_ERR, "open `%s': %s", options->pidfile, strerror(errno));
			goto abort;
		}

		/* Lock the file so that only one instance of dhcpcd runs
		 * on an interface */
		if (flock(pidfd, LOCK_EX | LOCK_NB) == -1) {
			logger(LOG_ERR, "flock `%s': %s", options->pidfile, strerror(errno));
			goto abort;
		}

		/* dhcpcd.sh should not interhit this fd */
		if ((i = fcntl(pidfd, F_GETFD, 0)) == -1 || fcntl(pidfd, F_SETFD, i
				| FD_CLOEXEC) == -1)
			logger(LOG_ERR, "fcntl: %s", strerror(errno));

		writepid(pidfd, getpid());
		logger(LOG_INFO, PACKAGE " " VERSION " starting");
	}

	/* Seed random */
	srandomdev();

	/* Massage our filters per platform */
	setup_packet_filters();

	/**
	 * here the dhcp magic begins.
	 * the dhcp protocol is started here
	 */
	if (dhcp_run(options, &pidfd) == 0)
		retval = EXIT_SUCCESS;

	abort:
	/* If we didn't daemonise then we need to punt the pidfile now */
	if (pidfd > -1) {
		close(pidfd);
		unlink(options->pidfile);
	}

	free(options);

#ifdef THERE_IS_NO_FORK
	/* There may have been an error before the dhcp_run function
	 * clears this, so just do it here to be safe */
	free (dhcpcd_skiproutes);
#endif

	logger(LOG_INFO, "exiting");

	exit(retval);
	/* NOTREACHED */
}