aboutsummaryrefslogtreecommitdiff
path: root/daemon/server.c
blob: a3771cd025d7433b86c1f88a6a4a95f8cd6f9fe0 (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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/* ****************************************************************************
 * server.c -- gestion du serveur, réception des requêtes.
 * Copyright (C) 2018 Thomas Touhey <thomas@touhey.fr>
 *
 * This project is governed by the CeCILL license under French law and
 * abiding by the rules of distribution of free software. You can use,
 * modify and/or redistribute the software under the terms of the
 * CeCILL license as circulated by CEA, CNRS and INRIA at the
 * following URL: "http://www.cecill.info".
 *
 * As a counterpart to the access to the source code and rights to copy,
 * modify and redistribute granted by the license, users are provided only
 * with a limited warranty and the project's author, the holder of the
 * economic rights, and the successive licensors have only limited liability.
 *
 * In this respect, the user's attention is drawn to the risks associated
 * with loading, using, modifying and/or developing or reproducing the
 * software by the user in light of its specific status of free software,
 * that may mean that it is complicated to manipulate, and that also therefore
 * means that it is reserved for developers and experienced professionals
 * having in-depth computer knowledge. Users are therefore encouraged to load
 * and test the software's suitability as regards their requirements in
 * conditions enabling the security of their systems and/or data to be
 * ensured and, more generally, to use and operate it in the same conditions
 * as regards security.
 *
 * The fact that you are presently reading this means that you have had
 * knowledge of the CeCILL license and that you accept its terms.
 * ************************************************************************* */
#include "internals.h"
#include <signal.h>
#include <setjmp.h>
#include <weshd.h> /* définitions du protocole depuis `weshd.x` */
#include <rpc/pmap_clnt.h>

#define REQADDR inet_ntoa(svc_getcaller(req->rq_xprt)->sin_addr)

#define GETWES(ID, RET, RESP) { \
	int getwes_err; \
	switch ((getwes_err = get_in_list(list, &wes, ID))) { \
	case WROK: \
		break; \
	case WRNOTFOUND: \
		msg((wlerror, "no server with id %d found", ID)); \
		RET = WESPRET_NOW; \
		return (RESP); \
	default: \
		msg((wlerror, "generic error while getting resource: %s", \
			error_string(getwes_err))); \
		RET = WESPRET_INT; \
		return (RESP); \
	}}

/* Ici, on stocke les ressources « globales » de sorte à ce que les
 * appels de services (svc) puissent les utiliser. */

static weslist_t *list;

/* On stocke également ce qu'il faut afin que les signaux du type
 * « arrête-toi » puissent être attrapés et que l'on quitte gracieusement. */

static jmp_buf endjmp;

/* ---
 * Gestion des ressources existentes.
 * --- */

/* `register_new_1_svc()`: Création de ressource. */

wespret_with_id_t *register_new_1_svc(wespregopts_t *args,
	struct svc_req *req)
{
	static wespret_with_id_t resp;
	wescreatedata_t data;
	int (*addfunc)(weslist_t *, wes_t **, wescreatedata_t *);
	const unsigned char *ip;
	wesaddr_t *addr; int err;
	wes_t *wes;

	resp.ret = WESPRET_INT;

	/* On vérifie le type de ressource WES à créer. */

	switch (args->type) {
	case WESPREGTYPE_DUMMY:
		addfunc = &add_dummy_wes;
		break;
	case WESPREGTYPE_IP:
		addfunc = &add_ip_wes;
		break;
	default:
		resp.ret = WESPRET_VAL;
		return (&resp);
	}

	/* On prépare les données de création en fonction de ce qui est envoyé. */

	addr = &data.addr;
	switch (args->ip.type) {
	case WESPIPTYPE_4:
		ip = args->ip.ipv4;

		msg((wlnotice, "(%s) address is IPv4 %hhu.%hhu.%hhu.%hhu", \
			REQADDR, ip[0], ip[1], ip[2], ip[3]));

		addr->type = WINET;
		addr->data.inet.has = HASIPv4;
		memcpy(addr->data.inet.ipv4, ip, 4);

		break;

	case WESPIPTYPE_6:
		ip = args->ip.ipv6;

		msg((wlnotice, "(%s) address is IPv6 "
			"%02hhX%02hhX:%02hhX%02hhX:%02hhX%02hhX:%02hhX%02hhX:"
			"%02hhX%02hhX:%02hhX%02hhX:%02hhX%02hhX:%02hhX%02hhX",
			REQADDR, ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6], ip[7],
			ip[8], ip[9], ip[10], ip[11], ip[12], ip[13], ip[14], ip[15]));

		addr->type = WINET;
		addr->data.inet.has = HASIPv6;
		memcpy(addr->data.inet.ipv6, ip, 16);

		break;

	default:
		addr->type = WNONET;
		break;
	}

	/* TODO: identifiants */

	/* On ajoute la ressource en utilisant la fonction dédiée de l'interface
	 * choisie par le client. */

	if ((err = (*addfunc)(list, &wes, &data))) {
		/* TODO: vérification de l'erreur */
		resp.ret = WESPRET_INT;
		return (&resp);
	}

	/* On a tout, on peut donner un petit sourire au client :) */

	msg((wlinfo, "success in adding resource!"));

	resp.ret = WESPRET_OK;
	resp.id = wes->id;
	return (&resp);
}

/* `unregister_1_svc()`: Suppression de ressource. */

wespret_t *unregister_1_svc(wespid_t *id, struct svc_req *req)
{
	static wespret_t ret;

	msg((wlnotice, "(%s) delete server %d",
		REQADDR, *id));
	ret = WESPRET_INT;

	/* Suppression du WES en utilisant l'identifiant. */

	switch (remove_in_list_using_id(list, *id)) {
	case WROK:
		break;
	case WRNOTFOUND:
		msg((wlerror, "no server with id %d found", *id));
		ret = WESPRET_NOW;
		return (&ret);
	default:
		return (&ret);
	}

	/* Tout s'est bien passé ! */

	msg((wlerror, "deletion completed"));
	ret = WESPRET_OK;
	return (&ret);
}

/* ---
 * Gestion du nom d'hôte du serveur WES.
 * --- */

/* `get_host_name_1_svc()`: Récupération du nom d'hôte. */

wespret_with_hostname_t *get_host_name_1_svc(wespid_t *id, struct svc_req *req)
{
	static wespret_with_hostname_t resp;
	wes_t *wes;

	msg((wlnotice, "(%s) get hostname from server %d", REQADDR, *id));
	resp.ret = WESPRET_INT;

	GETWES(*id, resp.ret, &resp)

	resp.hostname = wes->name;
	resp.ret = WESPRET_OK;
	return (&resp);
}

/* `set_host_name_1_svc()`: Définition du nom d'hôte. */

wespret_t *set_host_name_1_svc(wespid_with_hostname_t *args,
	struct svc_req *req)
{
	static wespret_t ret;

	msg((wlnotice, "(%s) set hostname on server %d", REQADDR, args->id));

	/* TODO: validate LDH hostname (uppercase) with max. 16 chars? */
	/* TODO: dynamically allocate hostname */
	/* TODO: update on the WES server using the interface function */
	/* TODO: update in the wes_t structure */

	ret = WESPRET_IMP;
	return (&ret);
}

/* ---
 * Gestion des identifiants du serveur WES.
 * --- */

/* `set_http_ids_1_svc()`: Définition des identifiants HTTP. */

wespret_t *set_http_ids_1_svc(wespid_with_ids_t *args, struct svc_req *req)
{
	static wespret_t ret;

	msg((wlnotice, "(%s) set http ids on server %d", REQADDR, args->id));

	ret = WESPRET_IMP;
	return (&ret);
}

/* `set_ftp_ids_1_svc()`: Définition des identifiants FTP. */

wespret_t *set_ftp_ids_1_svc(wespid_with_ids_t *args, struct svc_req *req)
{
	static wespret_t ret;

	msg((wlnotice, "(%s) set ftp ids on server %d", REQADDR, args->id));

	ret = WESPRET_IMP;
	return (&ret);
}

/* ---
 * Gestion de l'heure du serveur WES.
 * --- */

/* `get_date_time_1_svc()`: Récupération de l'horodate. */

wespret_with_datetime_t *get_date_time_1_svc(wespid_t *id, struct svc_req *req)
{
	static wespret_with_datetime_t resp;
	wes_t *wes; westime_t *dt;

	msg((wlnotice, "(%s) get datetime from server %d", REQADDR, *id));

	resp.ret = WESPRET_INT;

	GETWES(*id, resp.ret, &resp)

	/* Extraction via l'interface. */

	if (!wes->iface->gettime) {
		resp.ret = WESPRET_IMP;
		return (&resp);
	}

	switch ((*wes->iface->gettime)(wes, &dt)) {
	case WROK:
		break;

	default:
		resp.ret = WESPRET_INT;
		return (&resp);
	}

	/* Conversion en les données du protocole puis libération des données.
	 * TODO */

	if (dt->free)
		(*dt->free)(dt);

	resp.ret = WESPRET_IMP;
	return (&resp);

	/* Tout est bien qui finit bien ! */

	msg((wlinfo, "datetime gathered successfully"));
	resp.ret = WESPRET_OK;
	return (&resp);
}

/* `set_date_time_1_svc()`: Définition de l'horodate. */

wespret_t *set_date_time_1_svc(wespid_with_datetime_t *args,
	struct svc_req *req)
{
	static wespret_t ret;

	msg((wlnotice, "(%s) set datetime on server %d", REQADDR, args->id));

	ret = WESPRET_IMP;
	return (&ret);
}

/* ---
 * Gestion de l'aspect réseau du serveur WES.
 * --- */

/* `get_network_1_svc()`: Récupération des paramètres réseau. */

wespret_with_net_t *get_network_1_svc(wespid_t *id, struct svc_req *req)
{
	static wespret_with_net_t resp; wesnet_t *net;
	wes_t *wes; int err;

	msg((wlnotice, "(%s) get network info from server %d", REQADDR, *id));
	resp.ret = WESPRET_INT;

	GETWES(*id, resp.ret, &resp)

	/* Extraction depuis l'interface. */

	if (!wes->iface->getnet) {
		resp.ret = WESPRET_IMP;
		return (&resp);
	}

	switch ((err = (*wes->iface->getnet)(wes, &net))) {
	case WROK:
		break;

	default:
		msg((wlerror, "if error occurred: %s", error_string(err)));
		resp.ret = WESPRET_INT;
		return (&resp);
	}

	/* On convertit ça dans la structure de données du protocole. */

	resp.cfg.enable_dhcp = net->cfg.dhcp_enabled;
	memcpy(resp.cfg.ip, net->cfg.ip, 4);
	memcpy(resp.cfg.mask, net->cfg.mask, 4);
	memcpy(resp.cfg.gw, net->cfg.gw, 4);
	memcpy(resp.cfg.dns1, net->cfg.ns[0], 4);
	memcpy(resp.cfg.dns2, net->cfg.ns[1], 4);
	memcpy(resp.data.mac, net->data.mac, 6);

	/* La réponse est bonne. */

	resp.ret = WESPRET_OK;
	set_obj_to_free(wes, net);
	return (&resp);
}

/* `set_network_1_svc()`: Définition des paramètres réseau. */

wespret_t *set_network_1_svc(wespid_with_net_t *args, struct svc_req *req)
{
	static wespret_t ret;

	msg((wlnotice, "(%s) set network config on server %d", REQADDR, args->id));

	ret = WESPRET_IMP;
	return (&ret);
}

/* ---
 * Gestion du TÉLÉINFO.
 * --- */

/* `get_tic_1_svc()`: Récupération de la configuration et des données
 * immédiates d'un compteur branché via TÉLÉINFO. */

wespret_with_tic_t *get_tic_1_svc(wespid_with_tic_id_t *args,
	struct svc_req *req)
{
	static wespret_with_tic_t resp;
	westic_t *tic; wes_t *wes;

	msg((wlnotice, "(%s) get teleinfo %d info from server %d", REQADDR,
		args->tic_id, args->id));
	resp.ret = WESPRET_INT;

	/* On vérifie si le numéro du compteur est bon. */

	GETWES(args->id, resp.ret, &resp)

	/* On récupère les données. */

	if (!wes->iface->gettic) {
		resp.ret = WESPRET_IMP;
		return (&resp);
	}

	switch ((*wes->iface->gettic)(wes, args->tic_id, &tic)) {
	case WROK:
		break;

	default:
		return (&resp);
	}

	/* TODO: faire la conversion. */
	set_obj_to_free(wes, tic);
	return (&resp);

	/* Tout va bien, on peut retourner comme quoi on a bien rempli la
	 * structure. */

	resp.ret = WESPRET_OK;
	set_obj_to_free(wes, tic);
	return (&resp);
}

/* `set_tic_1_svc()`: Définition de la configuration d'un compteur branché
 * via TÉLÉINFO. */

wespret_t *set_tic_1_svc(wespid_with_tic_t *args, struct svc_req *req)
{
	static wespret_t ret;

	msg((wlnotice, "(%s) set teleinfo %d config on server %d", REQADDR,
		args->tic_id, args->id));

	ret = WESPRET_IMP;
	return (&ret);
}

/* ---
 * Fonction de lancement du serveur, avec gestions de signaux.
 * --- */

/* `its_the_signal()`: fonction de réception du signal d'arrêt. */

static __attribute__((noreturn)) void its_the_signal(int sig)
{
	(void)sig;

	longjmp(endjmp, 1);
}

/* `run_server()`: lancer le serveur. */

int run_server(weslist_t *llist)
{
	SVCXPRT *tcp = NULL;

	/* On stocke les données des appels de service (svc) pour que
	 * ceux-ci puissent les utiliser. */

	list = llist;

	/* On libère le port sur le service portmap. */

	pmap_unset(WESHD_PROG, WESHD_VERS1);

	/* Création du transport via TCP, création de l'application sur
	 * ce transport. */

	if (!(tcp = svctcp_create(RPC_ANYSOCK, 0, 0))) {
		msg((wlfatal, "could not create the transport"));
		return (WRUNKNOWN);
	} else if (!svc_register(tcp, WESHD_PROG, WESHD_VERS1, weshd_prog_1,
	 IPPROTO_TCP)) {
		msg((wlfatal, "could not register the service"));
		svc_destroy(tcp);
		return (WRUNKNOWN);
	}

	/* Gestion du signal d'arrêt. */

	signal(SIGINT, its_the_signal);

	/* Lancement de l'application.
	 * Remarquez la subtilité : on annonce que le serveur tourne alors qu'en
	 * réalité, pas encore. Mais chut, il ne faut surtout pas que
	 * l'administrateur apprenne ça ;) */

	if (setjmp(endjmp) == 0) {
		msg((wlnotice, "The server is now running."));
		svc_run();
	}

	/* Libération de mémoire. */

	msg((wlnotice, "Stopping the server."));
	svc_destroy(tcp);
	return (0);
}