-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtda_dns.c
361 lines (294 loc) · 10.8 KB
/
tda_dns.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
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
#include "tda_dns.h"
#include "ab.h"
#include "pila.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <strings.h>
#define ARGS_DNS_SEND 6
#define ARGS_DNS_GET_IP 5
#define ARGS_DNS_ADD_DOMAIN 5
#define ARGS_DNS_DELETE_DOMAIN 4
#define DOMAIN_NAME_MAX 256
#define DOMAIN_TAG_MAX 64
#define IP_MAX 16
#define DASH "-"
#define DOT "."
#define MAX_LINE 300
#define FOUNDED 256
#define NOT_FOUNDED 255
#define NO_DATA 254
#define RES_OK 0
#define RES_ERROR 1
#define RES_MEM_ERROR -1
#define RES_HOJA_BORRADA 200
/******************************* Declaraciones de funciones *****************************************/
int findDNS(TAB *tree, TPila *url,char* domain, int mov);
void tdomainCopy(tdomain *dst, tdomain *src);
int getData(TAB *tree, TPila *url,char* domain, int mov,tdomain* td);
int deleteData(TAB* tree,TPila* url,char* domain,int mov);
void breakDomain(char *domain, TPila *pile);
void breakDomain(char *domain, TPila *pile);
int domainExists(TAB a,char* domain);
int addSubDomain(TAB* a,const tdomain* d,TPila pila);
int findDomain(TAB* ab, const int mov, char* domain);
int searchPlace(TAB* ab,tdomain* domain,int* mov);
int orderInsert(TAB *tree, tdomain domain);
int AB_Borrar_Hoja(TAB *a);
/******************************************************************************************************/
/******************************************** Implementacion de primitivas **********************************************/
int createDNS(tdns *dns, int dataSize) {
AB_Crear(&(dns->ab), dataSize);
dns->dataSize = dataSize;
return RES_OK;
}
void destroyDNS(tdns *dns) {
AB_Vaciar(&(dns->ab));
free(dns);
}
int addDomain(tdns* dns,char* url,const tdomain* td) {
TPila pila_dominio;
printf("addDomain: inicia la insercion.\n");
breakDomain(url,&pila_dominio); /*acá adentro crea la pila*/
return addSubDomain(&(dns->ab),td,pila_dominio);
}
void getValue(tdns* dns, char* url, tdomain* td){
char domain[DOMAIN_TAG_MAX];
TPila pila;
if(AB_Vacio(dns->ab))
return;
breakDomain(url,&pila);
P_Sacar(&pila,domain);
if(getData(&(dns->ab),&pila,domain,RAIZ,td)!=RES_OK) {
printf("No se pudo obtener el dato.\n");
}
}
int urlExists(tdns dns, char* url){
TPila pila;
char domain[DOMAIN_TAG_MAX];
breakDomain(url,&pila);
P_Sacar(&pila,domain);
return findDNS(&(dns.ab),&pila,domain,RAIZ);
}
void deleteDomain(tdns *dns, char* url) {
char domain[DOMAIN_TAG_MAX];
TPila pila;
printf("deleteDomain: Entré en deleteDomain.\n");
breakDomain(url,&pila);
P_Sacar(&pila,domain);
if(deleteData(&(dns->ab),&pila,domain,RAIZ)!=RES_OK) {
printf("No se pudo borrar el dato.\n");
}
}
/************************************ Funciones utilizadas ************************************************************/
int findDNS(TAB *tree, TPila *url,char* domain, int mov) {
tdomain tdaux;
/* si existe en el arbol actual, tomo el corriente, y sigo la búsqueda del siguiente dominio en el árbol del corriente*/
if(findDomain(tree,RAIZ,domain)==RES_OK) {
if(P_Vacia(*url)) return RES_OK;
printf("findDNS: Existe el subdominio = %s.\n",domain);
printf("findDNS: como existe este subdominio paso al siguiente nivel del arbol.\n");
AB_ElemCte(*tree,&tdaux);
P_Sacar(url,domain);
printf("findDNS: Siguiente subdominio = %s.\n",domain);
return findDNS(&(tdaux.subab),url,domain,RAIZ);
} else
printf("findDNS: no existe el subdomino, no existe la url.\n");
return RES_ERROR;
}
void tdomainCopy(tdomain *dst, tdomain *src) {
if (src->domain)
strcpy(dst->domain, src->domain);
if (src->ip)
strcpy(dst->ip, src->ip);
if (&(src->subab)!=NULL)
memcpy(&(dst->subab),&(src->subab), sizeof(TAB));
}
int getData(TAB *tree, TPila *url,char* domain, int mov,tdomain* td) {
int res;
int error;
tdomain aux;
printf("Entré en getData.\n");
if(AB_Vacio(*tree)) return RES_ERROR; /* arbol vacio, no lo encontré*/
AB_MoverCte(tree, mov,&error);
if(error!=RES_OK) {
printf("No puedo mover el corriente.\n");
return RES_ERROR; /* no lo encontre */
}
AB_ElemCte(*tree, &aux);
printf("Obtuve este corriente: %s.\n",aux.domain);
res = strcmp(aux.domain,domain);
printf("La comparación dio: %s %s %i\n",aux.domain,domain,res);
if (res < 0) {
printf("Voy a la derecha.\n");
return getData(tree,url,domain,DER,td);
} else if (res > 0) {
printf("Voy a la izquierda.\n");
return getData(tree,url,domain,IZQ,td);
} else {
if (P_Vacia(*url)) {
tdomainCopy(td,&aux);
printf("Encontré el nodo %s.\n",td->domain);
return RES_OK; /*lo encontramos*/
} else {
P_Sacar(url,domain);
printf("Sigo iterando: %s\n",domain);
return getData(&(aux.subab),url,domain,RAIZ,td);
}
}
}
int deleteData(TAB* tree,TPila* url,char* domain,int mov) {
int res;
int error;
tdomain aux;
printf("deleteData: entre en deleteData\n");
if(AB_Vacio(*tree)) return RES_ERROR; /* arbol vacio, no lo encontré*/
AB_MoverCte(tree, mov,&error);
printf("deleteData: muevo el corriente.\n");
if(error) return RES_ERROR; /* no lo encontre */
AB_ElemCte(*tree, &aux);
printf("deleteData: el elem del corriente.\n");
res = strcasecmp(aux.domain,domain);
if (res < 0) {
printf("deleteData: me muevo para la derecha.\n");
return deleteData(tree,url,domain,DER);
} else if (res > 0) {
printf("deleteData: me muevo para la izquierda.\n");
return deleteData(tree,url,domain,IZQ);
} else {
printf("deleteData: encontré un match para el subdominio");
if (P_Vacia(*url)) { /** estoy en la hoja que tengo que borrar**/
printf("deleteData: la pila esta vacia.\n");
if(!AB_Vacio(aux.subab)) {
printf("deleteData: el arbol en el elem no esta vacio, entonces no es hoja.\n");
return RES_ERROR;
}
printf("deleteData: el arbol en el elem esta vacio, entonces es hoja.\n");
if(AB_Borrar_Hoja(tree)!=RES_OK) {
printf("deleteData: no se pudo borar la hoja.\n");
return RES_ERROR;
}
AB_ElemCte(*tree, &aux);
printf("deleteData: el corriente es = %s.\n",aux.domain);
printf("deleteData: Se borro el dominio.\n");
return RES_OK; /*lo encontramos*/
} else {
P_Sacar(url,domain);
printf("deleteData: sigo buscando.\n");
return deleteData(&(aux.subab),url,domain,RAIZ);
}
}
}
void breakDomain(char *domain, TPila *pile) {
char *pointer = NULL;
char buffer[DOMAIN_TAG_MAX];
char* url_aux;
url_aux = (char*)malloc(sizeof(char)*strlen(domain)+1);
if(!url_aux) printf("breakDomain: no hay recursos para variable auxiliar.\n");
strcpy(url_aux,domain);
P_Crear(pile, sizeof(char) * DOMAIN_TAG_MAX);
pointer = strtok(url_aux, DOT);
printf("breakDomain: url = %s\n",url_aux);
while (pointer) {
printf("breakDomain: subdominio = %s\n",pointer);
strcpy(buffer, pointer);
P_Agregar(pile, buffer);
pointer = strtok(NULL, DOT);
}
}
int domainExists(TAB a,char* domain) {
return findDomain(&a,RAIZ,domain);
}
int addSubDomain(TAB* a,const tdomain* d,TPila pila) {
char subdominio[DOMAIN_TAG_MAX];
tdomain domain;
tdomain aux;
int inser;
/*me fijo si la pila está vacía, si lo está ya se terminó de cargar el dominio o se generó mal la pila*/
if(P_Vacia(pila)) {
printf("addSubDomain: Pila vacia.\n");
return RES_OK;
}
/*saco un elemento de la pila*/
if(P_Sacar(&pila,subdominio)!=TRUE) {
printf("addSubDomain: No se pudo sacar un elemento de la pila.\n");
return RES_ERROR;
};
/* si existe en el arbol actual, tomo el corriente, y sigo la búsqueda del siguiente dominio en el árbol del corriente; luego modifico el árbol actual */
if(domainExists(*a,subdominio)==RES_OK) { /*lo encontró*/
printf("addSubDomain: Existe el subdominio.\n");
if(P_Vacia(pila)) {
printf("addSubDomain: ya existe la hoja.\n");
/*la hoja ya existe*/
return RES_ERROR;
}
AB_ElemCte(*a,&domain);
printf("addSubDomain: como existe este subdominio paso al siguiente nivel del arbol.\n");
if(addSubDomain(&(domain.subab),d,pila)!=RES_OK) return RES_ERROR;
AB_ModifCte(a,&domain);
return RES_OK;
}
else
{
printf("addSubDomain: No existe el subdominio.\n");
/* si no está en el árbol actual el subdominio, puede ser porque se encuentra en una hoja o porque el subdominio todavía no existe pero no es hoja*/
if(P_Vacia(pila)){
/* estoy en una hoja , inserto*/
inser = orderInsert(a,*d);
printf("addSubDomain: Estoy en la hoja, ya insertado: %i.\n",inser);
printf("addSubDomain: %s\n",d->domain);
return inser;
}
else
{
printf("addSubDomain: Tengo que insertar un nuevo subdominio para seguir.\n");
/* todavía estoy entre las ramas*/
strcpy(aux.domain,subdominio);
AB_Crear(&(aux.subab),sizeof(tdomain));
orderInsert(a,aux);
AB_ElemCte(*a,&domain);
printf("addSubDomain: %s\n",domain.domain);
printf("addSubDomain: paso al siguiente nivel del arbol.\n");
if(addSubDomain(&(domain.subab),d,pila)!=RES_OK) return RES_ERROR;
AB_ModifCte(a,&domain);
printf("addSubDomain:el subdominio agregado es %s.\n",d->domain);
printf("addSubDomain: Actualice correctamente.\n");
return RES_OK;
}
}
}
int findDomain(TAB* ab, const int mov, char* domain){
int error;
tdomain Aux;
int cmp;
AB_MoverCte(ab, mov, &error);
if(error==RES_ERROR)
return RES_ERROR;
AB_ElemCte(*ab,&Aux);
cmp = strcmp(domain,Aux.domain);
if(cmp==0) return RES_OK;
if(cmp<0) return findDomain(ab,IZQ,domain);
else return findDomain(ab,DER,domain);
}
int searchPlace(TAB* ab,tdomain* domain,int* mov) {
tdomain tdaux;
int cmp;
int error;
AB_MoverCte(ab,*mov,&error);
if(error==RES_ERROR) return RES_ERROR;
AB_ElemCte(*ab,&tdaux);
cmp = strcmp(domain->domain,tdaux.domain);
if(cmp==0) return RES_OK;
if(cmp<0) *mov=IZQ;
else *mov=DER;
return searchPlace(ab,domain,mov);
}
int orderInsert(TAB *tree, tdomain domain) {
int error;
int search = 0;
int mov = RAIZ ;
search = searchPlace(tree,&domain,&mov);
if(search==RES_OK) return RES_ERROR;
AB_Insertar(tree,mov,&domain,&error);
return error;
}