-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdht.c
223 lines (192 loc) · 5.17 KB
/
dht.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include "dht.h"
// affiche une information
void print_info(char * flag, char * msg) {
printf(" \e[34m[%s]\e[0m\t %s\n", flag, msg);
}
// affiche une information contenant un string à intégrer
void print_info_str(char * flag, char * msg, char * str) {
char buffer[BUFF_MAX_LENGTH];
sprintf(buffer, msg, str);
print_info(flag, buffer);
}
// affiche une erreur
void print_error(char * msg) {
printf(" \e[31m[ERR]\e[0m\t %s\n", msg);
}
// affiche un message d'erreur contenant un char à intégrer
void print_error_char(char * msg, char c) {
char buffer[BUFF_MAX_LENGTH];
sprintf(buffer, msg, c);
print_error(buffer);
}
// constructeur pour une liste d'ip
dht_ips new_dht_ips(char * ip) {
dht_ips ips = malloc(sizeof(struct s_dht_ips));
if (ips == NULL) {
fprintf(stderr, "Le malloc n'a pas fonctionné...\n");
exit(EXIT_FAILURE);
}
strcpy(ips->ip, ip);
ips->next = NULL;
return ips;
}
// libère la liste chaînée d'IP
void free_dht_ips(dht_ips i) {
if (i == NULL) return;
if (i->next != NULL) free_dht_ips(i->next);
free(i);
}
// constructeur pour une dht
dht new_dht(char * file) {
int str_len;
dht d = malloc(sizeof(struct s_dht));
if (d == NULL) {
fprintf(stderr, "Le malloc n'a pas fonctionné\n");
exit(EXIT_FAILURE);
}
d->next = NULL;
// cas de l'initialisation
if (file == NULL) {
d->ttl = INT_MAX;
return d;
}
d->ttl = HASH_TTL;
str_len = strlen(file);
if (str_len < 65) {
fprintf(stderr, " [WARN]\t Hash trop petit.\n");
return NULL;
} else if (str_len > HASH_MAX_LENGTH) {
fprintf(stderr, " [WARN]\t Hash trop long.\n");
return NULL;
}
strcpy(d->file, file);
return d;
}
// permet d'ajouter un IP à une liste d'IPs
void ips_add(dht_ips i, char * ip) {
int matched_ip = 0;
if (i == NULL) {
fprintf(stderr, "La liste d'IPs n'est pas initialisée.\n");
exit(EXIT_FAILURE);
}
// on place le curseur à la fin, sauf si on rencontre déjà l'IP en question
while (i->next != NULL) {
if (i->ip != NULL && !strcmp(i->ip, ip)) {
matched_ip = 1;
break;
}
i = i->next;
}
// prise en compte du dernier cas
if (i->ip != NULL && !strcmp(i->ip, ip)) matched_ip = 1;
// si on a pas encore vu passer l'IP dans la liste, on l'ajoute
if (!matched_ip) {
i->next = new_dht_ips(ip);
}
}
// permet d'ajouter un couple (hash, ip) à la DHT
void dht_add(dht d, char * hash, char * ip) {
int matched_hash = 0;
if (d == NULL) {
fprintf(stderr, "La DHT n'est pas initialisée.\n");
exit(EXIT_FAILURE);
}
// on place le curseur à la fin, sauf si on rencontre le même hash
while (d->next != NULL) {
d = d->next;
if (d->file != NULL && !strcmp(d->file, hash)) {
matched_hash = 1;
break;
}
}
if (matched_hash) { // dans le cas d'un hash déjà connu
d->ttl = HASH_TTL;
if (d->ips == NULL) d->ips = new_dht_ips(ip);
else ips_add(d->ips, ip);
} else { // dans le cas d'un nouveau hash
dht n = new_dht(hash);
if (n != NULL && n->file != NULL) {
d->next = n;
d->next->ips = new_dht_ips(ip);
} else {
free(n);
}
}
}
// fonction qui affiche une liste d'IPs
void dht_print_ips(dht_ips i) {
dht_ips p = i;
if (p == NULL) return;
while (p->next != NULL) {
print_info_str("I", " - %s", p->ip);
p = p->next;
}
print_info_str("I", " - %s", p->ip);
}
// affichage du contenu de la DHT
void dht_print(dht d) {
if (d == NULL) return;
if (d->file != NULL && strlen(d->file) > 0) {
if (d->ips != NULL) {
char b_info[BUFF_MAX_LENGTH];
sprintf(b_info, "'%s' [TTL=%d] chez :", d->file, d->ttl);
print_info("I", b_info);
dht_print_ips(d->ips);
} else {
print_info_str("I", "Personne ne possède le hash '%s'.", d->file);
}
}
dht_print(d->next);
}
// retourne un pointeur vers le hash
dht dht_find_hash(dht d, char * hash) {
if (d == NULL) return NULL;
// on parcourt toute la table
while (d->next != NULL) {
d = d->next;
if (d->file != NULL && !strcmp(d->file, hash)) {
return d;
}
}
return NULL;
}
// supprime un hash de la dht
void dht_delete_hash(dht d, char * hash) {
if (d == NULL) return;
dht prev = d, t;
// on parcourt toute la table
while (d->next != NULL) {
prev = d;
d = d->next;
if (d->file != NULL && !strcmp(d->file, hash)) {
t = prev->next; // l'élément courant
prev->next = d->next;
free_dht_ips(d->ips);
free(t);
return;
}
}
}
// convertit un nom de domaine en adresse IP
int convert_ndd_to_ip(char * ndd, char * ipstr) {
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_DGRAM;
if (getaddrinfo(ndd, NULL, &hints, &res) == 0) {
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *) res->ai_addr;
inet_ntop(res->ai_family, &(ipv6->sin6_addr), ipstr, INET6_ADDRSTRLEN);
freeaddrinfo(res);
return 1;
}
return 0;
}
// indique si une chaîne de caractères est une IPv6 valide ou non
int is_valid_ip(char * ip) {
struct sockaddr_in6 sa;
return inet_pton(AF_INET6, ip, &(sa.sin6_addr)) != 0;
}