aboutsummaryrefslogtreecommitdiff
path: root/daemon/ip/if.c
blob: bfaeae83e9783a6981868b605794147a283b8375 (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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
/* ****************************************************************************
 * ip/base.c -- définition de l'interface.
 * 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"
#define msgcgi(VAR, DESC) \
	msg((wlerror, "did CGI " VAR " (" DESC ") definition change?"));
#define GETCGI(NUM, BLK) { \
	int getcgi_err; \
	if ((getcgi_err = get_cgi(wes, (NUM), (BLK)))) { \
		msg((wlerror, "cgi error: %s", error_string(getcgi_err))); \
		return (getcgi_err); \
	}}

static wesif_t ipif;

/* ---
 * Création et ajout d'une ressource, et suppression de cette même ressource.
 * --- */

/* `free_ip_wes()`: Supprimer une ressource de type WES. */

static int free_ip_wes(wes_t *wes)
{
	if (wes->http_handle) {
		curl_easy_cleanup(wes->http_handle);
		wes->http_handle = NULL;
	}
	if (wes->ftp_handle) {
		curl_easy_cleanup(wes->ftp_handle);
		wes->ftp_handle = NULL;
	}

	deinit_m2m(wes);

	clear_config(wes);
	/* FIXME: clear_data(wes); */

/* FIXME: peut-être faire en sorte que ceci n'utilise pas l'alloc. dyn. ?
	set_http_ident(wes, NULL, NULL);
	set_ftp_ident(wes, NULL, NULL);
*/

	free(wes->http_data);
	free(wes->ftp_data);

	deinit_wes_base(wes);
	free(wes);
	return (WROK);
}

/* `make_ip_wes()`: Création bête et méchante d'une ressource WES de la
 * classe courante. Ce n'est pas cette fonction qui sera publique */

static int make_ip_wes(wes_t **wesp, wescreatedata_t *data)
{
	int err;
	wes_t *wes;

	/* Allocation dynamique de la ressource. */

	wes = malloc(sizeof(wes_t));
	if (!wes)
		return (WRALLOC);

	/* Initialisation de la base. */

	if ((err = init_wes_base(wes, &ipif))) {
		free(wes);
		return (err);
	}

	memcpy(&wes->addr, &data->addr, sizeof(wes->addr));

	wes->sections = NULL;
	wes->data = NULL;
	wes->http_data = NULL;
	wes->ftp_data = NULL;
	wes->http_handle = NULL;
	wes->ftp_handle = NULL;

	/* TODO: use `data` here */

	init_m2m(wes);

	if ((err = set_http_ident(wes, NULL, NULL))
	 || (err =  set_ftp_ident(wes, NULL, NULL))) {
		free_ip_wes(wes);
		return (err);
	}

	*wesp = wes;
	return (WROK);
}

/* `add_ip_wes()`: créer une ressource de type serveur WES via IP (ou
 * rafraîchir une existante).
 *
 * Historiquement, cette fonction s'appelait `register_from_dhcpv4()`
 * et a d'abord été pensée comme un hook appelé indirectement par le
 * serveur DHCP qui détecterait une nouvelle machine sur un réseau
 * réservé à des serveurs WES. Ensuite, le paramètre correspondant à
 * l'adresse MAC a été retiré et la fonction ne servait plus que de helper
 * pour le code du serveur (puisque l'interface WES via IP était l'unique
 * à une époque, et le code n'était pas encore pensé en interfaces).
 *
 * Depuis, la façon dont est pensée cette fonction est devenue le
 * standard sur la manière de créer/d'ajouter un nouveau serveur WES
 * pour toute interface. */

int add_ip_wes(weslist_t *list, wes_t **wesp, wescreatedata_t *data)
{
	wesiter_t iter;
	wes_t *wes;
	wesaddr_t *addr;
	int err;

	/* On vérifie si les paramètres sont OK. */

	addr = &data->addr;
	if (addr->type != WINET || ~addr->data.inet.has & HASIPv4
	 || addr->data.inet.has & HASIPv6)
		return (WROP);
	/* TODO: check if IPv4 adress is valid, not broadcast, etc? */

	/* On vérifie si une entrée correspondante n'existe pas déjà. */

	iterate_on_list(&iter, list);
	while (!(err = get_next_iteration(&iter, &wes))) {
		/* On regarde s'il s'agit bien d'un WES de la même interface. */

		if (wes->base.iface != &ipif)
			continue;

		/* On regarde si l'adresse IP correspond.
		 * TODO: si on gère plus de familles d'adresses réseau un jour,
		 * il faudra contrôler que tout va bien. */

		if (memcmp(addr->data.inet.ipv4, wes->addr.data.inet.ipv4, 4))
			continue;

		/* L'adresse correspond, mince, ça veut dire qu'on a failli
		 * créer un duplicata ! Ne le créons pas, et renvoyons plutôt
		 * celui-ci avec le fait qu'il existe. */

		*wesp = wes;
		return (WREXISTS);
	}

	/* Aucune entrée n'existe avec cette IP, créons-la ! */

	if ((err = make_ip_wes(&wes, data)))
		return (err);

	/* Et ajoutons l'entrée à la liste. */

	if ((err = add_to_list(list, wes))) {
		free_ip_wes(wes);
		return (err);
	}

	*wesp = wes;
	return (WROK);
}

/* ---
 * Petits utilitaires (TODO: à déplacer, quelque chose ?)
 * --- */

#define DEFAULT_HTTP_NAME "admin"
#define DEFAULT_HTTP_PASS "wes"
#define  DEFAULT_FTP_NAME "adminftp"
#define  DEFAULT_FTP_PASS "wesftp"

/* `set_http_ident()`: Définir les identifiants HTTP du serveur WES. */

int set_http_ident(wes_t *wes, const char *name, const char *pass)
{
	size_t namesz, passsz;

	if (wes->http_data)
		free(wes->http_data);

	wes->http_data = NULL;
	wes->http_name = NULL;
	wes->http_pass = NULL;

	if (!name)
		name = DEFAULT_HTTP_NAME;
	if (!pass)
		pass = DEFAULT_HTTP_PASS;

	namesz = strlen(name) + 1;
	passsz = strlen(pass) + 1;
	if (!(wes->http_data = malloc(namesz + passsz + 2)))
		return (WRALLOC);

	memcpy(wes->http_data, name, namesz);
	memcpy(&wes->http_data[namesz], pass, passsz);
	wes->http_name = wes->http_data;
	wes->http_pass = &wes->http_data[namesz];

	return (WROK);
}

/* `set_ftp_ident()`: Définir les identifiants FTP du serveur WES. */

int set_ftp_ident(wes_t *wes, const char *name, const char *pass)
{
	size_t namesz, passsz;

	if (wes->ftp_data)
		free(wes->ftp_data);

	wes->ftp_data = NULL;
	wes->ftp_name = NULL;
	wes->ftp_pass = NULL;

	if (!name)
		name = DEFAULT_FTP_NAME;
	if (!pass)
		pass = DEFAULT_FTP_PASS;

	namesz = strlen(name) + 1;
	passsz = strlen(pass) + 1;
	if (!(wes->ftp_data = malloc(namesz + passsz + 2)))
		return (WRALLOC);

	memcpy(wes->ftp_data, name, namesz);
	memcpy(&wes->ftp_data[namesz], pass, passsz);
	wes->ftp_name = wes->ftp_data;
	wes->ftp_pass = &wes->ftp_data[namesz];

	return (WROK);
}

/* `open_http_handle()`: Ouverture d'un canal HTTP pour un chemin HTTP.
 * Ce canal n'a point besoin d'être fermé. */

int open_http_handle(wes_t *wes, CURL **handlep, const char *path)
{
	CURL *handle;
	const unsigned char *ip;
	char url[50 + 400 + 1];

	/* On prépare l'URL complète, avec protocole et interface du WES. */

	if (wes->addr.data.inet.has & HASIPv4) {
		ip = wes->addr.data.inet.ipv4;
		sprintf(url, "http://%d.%d.%d.%d%.400s", ip[0], ip[1], ip[2], ip[3],
			path);
	} else if (wes->addr.data.inet.has & HASIPv6) {
		ip = wes->addr.data.inet.ipv4;
		sprintf(url, "http://[%02X%02X:%02X%02X:%02X%02X:%02X%02X"
			":%02X%02X:%02X%02X:%02X%02X:%02X%02X]%.400s",
			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],
			path);
	} else
		return (WRUNKNOWN);

	/* On ouvre le handle si nécessaire. */

	if (wes->http_handle)
		handle = wes->http_handle;
	else {
		handle = curl_easy_init();
		if (!handle)
			return (WRUNKNOWN);

		wes->http_handle = handle;
	}

	/* On prépare le handle à ce qui l'attend. */

	curl_easy_setopt(handle, CURLOPT_URL, url);
	curl_easy_setopt(handle, CURLOPT_USERNAME, wes->http_name);
	curl_easy_setopt(handle, CURLOPT_PASSWORD, wes->http_pass);
	curl_easy_setopt(handle, CURLOPT_UPLOAD, 0L);
	curl_easy_setopt(handle, CURLOPT_POST, 0L);
	curl_easy_setopt(handle, CURLOPT_VERBOSE, CURLHTTPVERBOSE);
	curl_easy_setopt(handle, CURLOPT_QUOTE, NULL);
	curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, NULL);
	curl_easy_setopt(handle, CURLOPT_WRITEDATA, stdout);
	curl_easy_setopt(handle, CURLOPT_READFUNCTION, NULL);
	curl_easy_setopt(handle, CURLOPT_READDATA, stdin);
	curl_easy_setopt(handle, CURLOPT_USERAGENT, "weshd/" WESHD_VERSION);

	*handlep = handle;
	return (WROK);
}

/* `open_ftp_handle()`: Ouverture d'un canal FTP pour un chemin FTP.
 * Ce canal n'a point besoin d'être fermé. */

int open_ftp_handle(wes_t *wes, CURL **handlep, const char *path)
{
	CURL *handle;
	const unsigned char *ip;
	char url[50 + 200 + 1];

	/* On prépare l'URL complète, avec protocole et interface du WES. */

	if (wes->addr.data.inet.has & HASIPv4) {
		ip = wes->addr.data.inet.ipv4;
		sprintf(url, "ftp://%d.%d.%d.%d%.200s", ip[0], ip[1], ip[2], ip[3],
			path);
	} else if (wes->addr.data.inet.has & HASIPv6) {
		ip = wes->addr.data.inet.ipv4;
		sprintf(url, "ftp://[%02X%02X:%02X%02X:%02X%02X:%02X%02X"
			":%02X%02X:%02X%02X:%02X%02X:%02X%02X]%.200s",
			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],
			path);
	} else
		return (WRUNKNOWN);

	/* On ouvre le handle si nécessaire. */

	if (wes->ftp_handle)
		handle = wes->ftp_handle;
	else {
		handle = curl_easy_init();
		if (!handle)
			return (WRUNKNOWN);

		wes->ftp_handle = handle;
	}

	/* On prépare le handle à ce qui l'attend. */

	curl_easy_setopt(handle, CURLOPT_URL, url);
	curl_easy_setopt(handle, CURLOPT_USERNAME, wes->ftp_name);
	curl_easy_setopt(handle, CURLOPT_PASSWORD, wes->ftp_pass);
	curl_easy_setopt(handle, CURLOPT_UPLOAD, 0L);
	curl_easy_setopt(handle, CURLOPT_VERBOSE, CURLFTPVERBOSE);
	curl_easy_setopt(handle, CURLOPT_QUOTE, NULL);
	curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, stdout);
	curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL);
	curl_easy_setopt(handle, CURLOPT_READFUNCTION, stdin);
	curl_easy_setopt(handle, CURLOPT_READDATA, NULL);

	*handlep = handle;
	return (WROK);
}

/* ---
 * Configuration des identifiants.
 * --- */

static int setwesids(wes_t *wes, wessrvtype_t service,
	const char *name, const char *pass)
{
	if (service != WESSRVTYPE_HTTP && service != WESSRVTYPE_FTP
	 && service != WESSRVTYPE_BDPV)
		return (WROP);

	/* TODO: définir les identifiants distants */
	(void)wes;
	(void)name;
	(void)pass;

	return (WRIMPL);
}

/* ---
 * Configuration de l'horodate.
 * --- */

/* `getwestime()`: récupération de l'horodate actuelle d'un serveur WES */

static int getwestime(wes_t *wes, westime_t **dtp)
{
	static westime_t dt;
	char datebuf[20], timebuf[20], gmtbuf[10], sumbuf[10],
		sembuf1[5], sembuf2[5], sembuf3[5], sembuf4[5],
		sembuf5[5], sembuf6[5];

	wescgi_t blocks[] = {
		/* La date du jour, sous format numérique `D M Y`. */
		WESCGI("h d", "%d %d %d", datebuf, 20),

		/* L'heure courante, sous format numérique `H M S`. */
		WESCGI("h h", "%d %d %d", timebuf, 20),

		/* Le fuseau horaire (en nombre entier d'heures). */
		WESCGI("h G", "%d", gmtbuf, 10),

		/* Selon si l'on est en heure d'été (activée) ou en
		 * heure d'hiver (désactivée). */
		WESCGI("h e", "%.1s", sumbuf, 10),

		/* Le jour de la semaine qu'on est (où `g [1-7]` est "selected" si
		 * c'est le jour de la semaine actuel, et rien sinon.
		 * TODO: peut-être que `g j` fait ça mieux… ? */
		WESCGI("g 1", "%.1s", sembuf1, 5),
		WESCGI("g 2", "%.1s", sembuf2, 5),
		WESCGI("g 3", "%.1s", sembuf3, 5),
		WESCGI("g 4", "%.1s", sembuf4, 5),
		WESCGI("g 5", "%.1s", sembuf5, 5),
		WESCGI("g 6", "%.1s", sembuf6, 5)
	};

	GETCGI(10, blocks)

	if (blocks[0].lines != 1
	 || sscanf(datebuf, "%d%d%d", &dt.cfg.dom, &dt.cfg.mon,
		&dt.cfg.year) < 3) {
		msgcgi("h d", "date");
		return (WRUNKNOWN);
	}
	if (blocks[1].lines != 1
	 || sscanf(timebuf, "%d%d%d", &dt.cfg.hour, &dt.cfg.min,
		&dt.cfg.sec) < 3) {
		msgcgi("h h", "hour");
		return (WRUNKNOWN);
	}

	if (blocks[2].lines != 1
	 || sscanf(gmtbuf, "%d", &dt.cfg.tzhour) < 1) {
		msgcgi("h G", "gmt");
		return (WRUNKNOWN);
	}
	dt.cfg.tzmin = 0;

	if (blocks[3].lines != 1) {
		msgcgi("h e", "dst");
		return (WRUNKNOWN);
	}
	dt.cfg.sum = sumbuf[0] ? 1 : 0;

	if (blocks[4].lines != 1 || blocks[5].lines != 1
	 || blocks[6].lines != 1 || blocks[7].lines != 1
	 || blocks[8].lines != 1 || blocks[9].lines != 1) {
		msgcgi("g [1-7]", "day of week");
		return (WRUNKNOWN);
	}
	dt.cfg.dow = sembuf1[0] ? 0 : sembuf2[0] ? 1 : sembuf3[0] ? 2 :
		sembuf4[0] ? 3 : sembuf5[0] ? 4 : sembuf6[0] ? 5 : 6;

	dt.free = NULL;
	*dtp = &dt;
	return (WROK);
}

/* ---
 * Configuration réseau.
 * --- */

/* `getwesnet()`: récupération de serveurs WES. */

static int getwesnet(wes_t *wes, wesnet_t **netp)
{
	static wesnet_t net;
	static wesnetcfg_t *cfg;
	static wesnetdata_t *data;
	char dhcpbuf[5], ipbuf[20], msbuf[20], gwbuf[20], dpbuf[20], dsbuf[20],
		macbuf[30];

	net.free = NULL;
	cfg = &net.cfg;
	data = &net.data;

	wescgi_t blocks[] = {
		/* Selon si le DHCP est activé (checked) ou désactivé. */
		WESCGI("r d", "%.1s", dhcpbuf, 5),

		/* L'adresse IP actuelle. */
		WESCGI("r i", "%d %d %d %d", ipbuf, 20),

		/* Le masque de sous-réseau actuel. */
		WESCGI("r m", "%d %d %d %d", msbuf, 20),

		/* La passerelle par défaut actuelle. */
		WESCGI("r g", "%d %d %d %d", gwbuf, 20),

		/* Les serveurs DNS primaires (p) et secondaires (s). */
		WESCGI("r p", "%d %d %d %d", dpbuf, 20),
		WESCGI("r s", "%d %d %d %d", dsbuf, 20),

		/* L'adresse MAC du serveur. */
		WESCGI("r a", "%d %d %d %d %d %d", macbuf, 30)
	};

	/* Extraction des données via CGI, puis décodage. */

	GETCGI(7, blocks)

	if (blocks[0].lines != 1) {
		msgcgi("r d", "dhcp enabled");
		return (WRUNKNOWN);
	}
	cfg->dhcp_enabled = dhcpbuf[0] ? 1 : 0;

	if (blocks[1].lines != 1
	 || sscanf(ipbuf, "%hhu%hhu%hhu%hhu", &cfg->ip[0], &cfg->ip[1],
		&cfg->ip[2], &cfg->ip[3]) < 4) {
		msgcgi("r i", "current ip");
		return (WRUNKNOWN);
	}
	if (blocks[2].lines != 1
	 || sscanf(msbuf, "%hhu%hhu%hhu%hhu", &cfg->mask[0],
		&cfg->mask[1], &cfg->mask[2], &cfg->mask[3]) < 4) {
		msgcgi("r m", "netmask");
		return (WRUNKNOWN);
	}
	if (blocks[3].lines != 1
	 || sscanf(gwbuf, "%hhu%hhu%hhu%hhu", &cfg->gw[0], &cfg->gw[1],
		&cfg->gw[2], &cfg->gw[3]) < 4) {
		msgcgi("r g", "gateway");
		return (WRUNKNOWN);
	}
	if (blocks[4].lines != 1
	 || sscanf(dpbuf, "%hhu%hhu%hhu%hhu", &cfg->ns[0][0],
		&cfg->ns[0][1], &cfg->ns[0][2], &cfg->ns[0][3]) < 4) {
		msgcgi("r p", "primary dns");
		return (WRUNKNOWN);
	}
	if (blocks[5].lines != 1
	 || sscanf(dsbuf, "%hhu%hhu%hhu%hhu", &cfg->ns[1][0],
		&cfg->ns[1][1], &cfg->ns[1][2], &cfg->ns[1][3]) < 4) {
		msgcgi("r s", "secon. dns");
		return (WRUNKNOWN);
	}
	if (blocks[6].lines != 1
	 || sscanf(macbuf, "%hhu%hhu%hhu%hhu%hhu%hhu", &data->mac[0],
		&data->mac[1], &data->mac[2], &data->mac[3], &data->mac[4],
		&data->mac[5]) < 6) {
		msgcgi("r a", "mac addr.");
		return (WRUNKNOWN);
	}

	*netp = &net;
	return (WROK);
}

/* ---
 * Compteurs via TÉLÉINFO.
 * --- */

/* `getwestic()`: récupération des données concernant le compteur. */

static int getwestic(wes_t *wes, int tic_id, westic_t **ticp)
{
	static westic_t tic;
	static westiccfg_t *cfg;
	static westicdata_t *data;
	static char name[20];

	tic.free = NULL;
	cfg = &tic.cfg;
	data = &tic.data;

	if (tic_id != 0 && tic_id != 1)
		return (WROP);

	{
		char isrd[5], abo[10], pror[5],
			tarif0[10], tarif1[10], tarif2[10], tarif3[10],
			tarif4[10], tarif5[10], prod2[5],
			bdpve[5], bdpvh[10], bdpvm[10],
			adco[20], abotype[10], monotri[5], ptec[20],
			isousc[10], pa[10], ii0[10], ii1[10], ii2[10],
			im0[10], im1[10], im2[10], idx0[15], idx1[15],
			idx2[15], idx3[15], idx4[15], idx5[15];

		wescgi_t blocks[] = {
			/* Selon si le compteur est lu (checked) ou non.
			 * `G t`: TIC1, `G T`: TIC2 */
			WESCGI(tic_id ? "G T" : "G t", "%.1s", isrd, 5),

			/* Nom donné via l'interface au compteur.
			 * `e n`: TIC1, `e N`: TIC2 */
			WESCGI(tic_id ? "e N" : "e n", "%s", name, 20),

			/* Coût fixe en euros du compteur. */
			WESCGI(tic_id ? "C A" : "C a", "%0.2f", abo, 10),

			/* Prorata abonnement au prix du kWh (checked) ou non ? */
			WESCGI(tic_id ? "C p2" : "C p1", "%.1s", pror, 5),

			/* Tarifs pour les différents abonnements. */
			WESCGI(tic_id ? "C T0" : "C t0", "%0.5f", tarif0, 10),
			WESCGI(tic_id ? "C T1" : "C t1", "%0.5f", tarif1, 10),
			WESCGI(tic_id ? "C T2" : "C t2", "%0.5f", tarif2, 10),
			WESCGI(tic_id ? "C T3" : "C t3", "%0.5f", tarif3, 10),
			WESCGI(tic_id ? "C T4" : "C t4", "%0.5f", tarif4, 10),
			WESCGI(tic_id ? "C T5" : "C t5", "%0.5f", tarif5, 10),

			/* Selon si l'on est en mode consommation ou production (pour
			 * le compteur 2 uniquement).
			 * `G C`: mode consommation (checked).
			 * `G p`: mode production (checked). */
			WESCGI("G p", "%.1s", prod2, 5),

			/* Si mode production, paramètres BDPV (pour le compteur 2
			 * uniquement).
			 * `H a`: activé (checked).
			 * `H i`: identifiant.
			 * `H p`: mot de passe.
			 * `H h`: heure d'envoi.
			 * `H m`: minute d'envoi. */
			WESCGI("H a", "%.1s", bdpve, 5),
			WESCGI("H h", "%d", bdpvh, 10),
			WESCGI("H m", "%d", bdpvm, 10),

			/* Numéro ADCO du compteur. */
			WESCGI(tic_id ? "e A" : "e a", "%s", adco, 20),

			/* Type d'abonnement.
			 * - 0: BASE
			 * - 1: Heure Creuse
			 * - 2: EJP
			 * - 3: TEMPO */
			WESCGI(tic_id ? "e D" : "e d", "%d", abotype, 10),

			/* Mode de l'entrée (mono ou triphasé).
			 * - 0: inconnu
			 * - 1: monophasé
			 * - 3: triphasé */
			WESCGI(tic_id ? "I M" : "i M", "%d", monotri, 10),

			/* Période tarifaire en cours (TODO: format?). */
			WESCGI(tic_id ? "T P" : "T p", "%s", ptec, 20),

			/* Intensité souscrite (en A),
			 * puissance apparente (en VA). */
			WESCGI(tic_id ? "e C" : "e c", "%d", isousc, 10),
			WESCGI(tic_id ? "I p" : "i p", "%d", pa, 10),

			/* Intensité instantanée pour chaque phase (en A). */
			WESCGI(tic_id ? "Ii0" : "ii0", "%d", ii0, 10),
			WESCGI(tic_id ? "Ii1" : "ii1", "%d", ii1, 10),
			WESCGI(tic_id ? "Ii2" : "ii2", "%d", ii2, 10),

			/* Intensité maximale pour chaque phase (en A). */
			WESCGI(tic_id ? "Im0" : "im0", "%d", im0, 10),
			WESCGI(tic_id ? "Im1" : "im1", "%d", im1, 10),
			WESCGI(tic_id ? "Im2" : "im2", "%d", im2, 10),

			/* Index 1 à 6 pour les compteurs. */
			WESCGI(tic_id ? "TI1" : "Ti1", "%s", idx0, 15),
			WESCGI(tic_id ? "TI2" : "Ti2", "%s", idx1, 15),
			WESCGI(tic_id ? "TI3" : "Ti3", "%s", idx2, 15),
			WESCGI(tic_id ? "TI4" : "Ti4", "%s", idx3, 15),
			WESCGI(tic_id ? "TI5" : "Ti5", "%s", idx4, 15),
			WESCGI(tic_id ? "TI6" : "Ti6", "%s", idx5, 15)
		};

		GETCGI(34, blocks)

		/* Options de base du compteur TELEINFO. */

		if (blocks[0].lines != 1) {
			msgcgi("G [tT]", "teleinfo is read");
			return (WRUNKNOWN);
		}
		cfg->is_read = isrd[0] ? 1 : 0;

		if (blocks[1].lines != 1) {
			msgcgi("e [nN]", "teleinfo name");
			return (WRUNKNOWN);
		}

		/* TODO: store name */
		/* cfg->name = name; */

		/* Tarifs. */

		if (blocks[2].lines != 1
		 || sscanf(abo, "%lf", &cfg->abo) < 1) {
			msgcgi("C [aA]", "fixed costs");
			return (WRUNKNOWN);
		}

		if (blocks[3].lines != 1) {
			msgcgi("C p[12]", "prorata");
			return (WRUNKNOWN);
		}
		cfg->prorata = pror[0] ? 1 : 0;

		{
			double tf0, tf1, tf2, tf3, tf4, tf5;

			if (blocks[4].lines != 1
			 || sscanf(tarif0, "%lf", &tf0) < 1) {
				msgcgi("C [tT]0", "tarif 0");
				return (WRUNKNOWN);
			}
			if (blocks[5].lines != 1
			 || sscanf(tarif1, "%lf", &tf1) < 1) {
				msgcgi("C [tT]1", "tarif 1");
				return (WRUNKNOWN);
			}
			if (blocks[6].lines != 1
			 || sscanf(tarif2, "%lf", &tf2) < 1) {
				msgcgi("C [tT]2", "tarif 2");
				return (WRUNKNOWN);
			}
			if (blocks[7].lines != 1
			 || sscanf(tarif3, "%lf", &tf3) < 1) {
				msgcgi("C [tT]3", "tarif 3");
				return (WRUNKNOWN);
			}
			if (blocks[8].lines != 1
			 || sscanf(tarif4, "%lf", &tf4) < 1) {
				msgcgi("C [tT]4", "tarif 4");
				return (WRUNKNOWN);
			}
			if (blocks[9].lines != 1
			 || sscanf(tarif5, "%lf", &tf5) < 1) {
				msgcgi("C [tT]5", "tarif 5");
				return (WRUNKNOWN);
			}

			cfg->tarif_base = tf0;
			cfg->tarif_hchp_hc = tf0;
			cfg->tarif_hchp_hp = tf1;
			cfg->tarif_tempo_hcjb = tf0;
			cfg->tarif_tempo_hpjb = tf1;
			cfg->tarif_tempo_hcjw = tf2;
			cfg->tarif_tempo_hpjw = tf3;
			cfg->tarif_tempo_hcjr = tf4;
			cfg->tarif_tempo_hpjr = tf5;
			cfg->tarif_ejp_hn = tf0;
			cfg->tarif_ejp_pm = tf1;
		}

		/* Mode consommation/production (si compteur 2), connexion
		 * avec le site BDPV. */

		if (tic_id == 0) {
			cfg->bdpv_hour = 0;
			cfg->bdpv_min = 0; 
		} else {
			if (blocks[10].lines != 1) {
				msgcgi("G p", "prod cpt. 2");
				return (WRUNKNOWN);
			}
			if (blocks[11].lines != 1) {
				msgcgi("H a", "bdpv enabled");
				return (WRUNKNOWN);
			}

			if (blocks[12].lines != 1
			 || sscanf(bdpvh, "%d", &cfg->bdpv_hour)) {
				msgcgi("H h", "bdpv send hour");
				return (WRUNKNOWN);
			}
			if (blocks[13].lines != 1
			 || sscanf(bdpvm, "%d", &cfg->bdpv_min)) {
				msgcgi("H m", "bdpv send min.");
				return (WRUNKNOWN);
			}

			cfg->mode = prod2[0] ? WESTICMODE_PROD : WESTICMODE_CONSO;
			if (cfg->mode == WESTICMODE_CONSO)
				cfg->bdpv_enabled = 0;
			else
				cfg->bdpv_enabled = bdpve[0] ? 1 : 0;
		}

		/* En-têtes TELEINFO du compteur. */

		data->is_plugged_in = 1;
		if (blocks[14].lines != 1
		 || (strcmp(adco, "Pas Dispo") && strlen(adco) != 12)) {
			msgcgi("e [aA]", "ADCO");
			return (WRUNKNOWN);
		}

		if (adco[0] == 'P')
			data->is_plugged_in = 0;
		else {
			data->is_plugged_in = 1;
			memcpy(data->adco, adco, 12);
		}

		{
			int at;

			if (blocks[15].lines != 1
			 || sscanf(abotype, "%d", &at) < 1
			 || at < 0 || at > 3) {
				msgcgi("e [dD]", "sub. type");
				return (WRUNKNOWN);
			}

			switch (at) {
			case 0:
				data->tarif = WESTICTARIF_BASE;
				break;
			case 1:
				data->tarif = WESTICTARIF_HCHP;
				break;
			case 2:
				data->tarif = WESTICTARIF_EJP;
				break;
			case 3:
				data->tarif = WESTICTARIF_TEMPO;
				break;
			default:
				msgcgi("e [dD]", "sub. type");
				return (WRUNKNOWN);
			}
		}

		{
			int mt;

			if (blocks[16].lines != 1
			 || sscanf(monotri, "%d", &mt) < 1) {
				msgcgi("[iI] M", "phase mode");
				return (WRUNKNOWN);
			}

			switch (mt) {
			case 0:
				data->phases = 0;
				break;
			case 1:
				data->phases = 1;
				break;
			case 3:
				data->phases = 3;
				break;
			default:
				msgcgi("[iI] M", "phase mode");
				return (WRUNKNOWN);
			}
		}

		/* TODO: période tarifaire en cours */
		data->period = WESTICPERIOD_HN;

		/* Intensité souscrite, puissance apparente. */

		if (blocks[18].lines != 1
		 || sscanf(isousc, "%u", &data->isousc) < 1) {
			msgcgi("e [cC]", "intens. souscrite");
			return (WRUNKNOWN);
		}
		if (blocks[19].lines != 1
		 || sscanf(pa, "%u", &data->pa) < 1) {
			msgcgi("[iI] p", "puis. app.");
			return (WRUNKNOWN);
		}

		/* Intensité instantanée pour chaque phase. */

		if (blocks[20].lines != 1
		 || sscanf(ii0, "%u", &data->iinst[0]) < 1) {
			msgcgi("[iI]i0", "i. inst. 0");
			return (WRUNKNOWN);
		}
		if (blocks[21].lines != 1
		 || sscanf(ii1, "%u", &data->iinst[1]) < 1) {
			msgcgi("[iI]i1", "i. inst. 1");
			return (WRUNKNOWN);
		}
		if (blocks[22].lines != 1
		 || sscanf(ii2, "%u", &data->iinst[2]) < 1) {
			msgcgi("[iI]i2", "i. inst. 2");
			return (WRUNKNOWN);
		}

		/* Intensité maximale pour chaque phase. */

		if (blocks[23].lines != 1
		 || sscanf(im0, "%u", &data->imax[0]) < 1) {
			msgcgi("[iI]m0", "i. max. 0");
			return (WRUNKNOWN);
		}
		if (blocks[24].lines != 1
		 || sscanf(im1, "%u", &data->imax[1]) < 1) {
			msgcgi("[iI]m1", "i. max. 1");
			return (WRUNKNOWN);
		}
		if (blocks[25].lines != 1
		 || sscanf(im2, "%u", &data->imax[2]) < 1) {
			msgcgi("[iI]m2", "i. max. 2");
			return (WRUNKNOWN);
		}

		/* Index */
		{
			unsigned long ix0 = 0, ix1 = 0, ix2 = 0, ix3 = 0,
				ix4 = 0, ix5 = 0;

			if (blocks[26].lines != 1 || (strcmp(idx0, "Pas Dispo")
			 && sscanf(idx0, "%lu", &ix0) < 1)) {
				msgcgi("T[iI]0", "index 0");
				return (WRUNKNOWN);
			}
			if (blocks[27].lines != 1 || (strcmp(idx1, "Pas Dispo")
			 && sscanf(idx1, "%lu", &ix1) < 1)) {
				msgcgi("T[iI]1", "index 1");
				return (WRUNKNOWN);
			}
			if (blocks[28].lines != 1 || (strcmp(idx2, "Pas Dispo")
			 && sscanf(idx2, "%lu", &ix2) < 1)) {
				msgcgi("T[iI]2", "index 2");
				return (WRUNKNOWN);
			}
			if (blocks[29].lines != 1 || (strcmp(idx3, "Pas Dispo")
			 && sscanf(idx3, "%lu", &ix3) < 1)) {
				msgcgi("T[iI]3", "index 3");
				return (WRUNKNOWN);
			}
			if (blocks[30].lines != 1 || (strcmp(idx4, "Pas Dispo")
			 && sscanf(idx4, "%lu", &ix4) < 1)) {
				msgcgi("T[iI]4", "index 4");
				return (WRUNKNOWN);
			}
			if (blocks[31].lines != 1 || (strcmp(idx5, "Pas Dispo")
			 && sscanf(idx5, "%lu", &ix5) < 1)) {
				msgcgi("T[iI]5", "index 5");
				return (WRUNKNOWN);
			}

			data->index_base = ix0;
			data->index_hchp_hc = ix0;
			data->index_hchp_hp = ix1;
			data->index_tempo_hcjb = ix0;
			data->index_tempo_hpjb = ix1;
			data->index_tempo_hcjw = ix2;
			data->index_tempo_hpjw = ix3;
			data->index_tempo_hcjr = ix4;
			data->index_tempo_hpjr = ix5;
			data->index_ejp_hn = ix0;
			data->index_ejp_pm = ix1;
		}
	}

	*ticp = &tic;
	return (WROK);
}

/* ---
 * Définition de l'interface.
 * --- */

static wesif_t ipif = {
	free_ip_wes,
	NULL, NULL,
	setwesids,
	getwestime, NULL,
	getwesnet, NULL,
	getwestic, NULL
};