-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.c
176 lines (155 loc) · 3.85 KB
/
request.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
//meshDNS by rémi arnaud
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include "meshDNS.h"
#include "config.h"
#include "parsing.h"
#include "request.h"
resp *requestName(mdns *req, linfo *infos){
// envoi d'un name, reponse une/key des
printf("receive name request\n");
resp *r = NULL;
resp *rbis;
char found = 0;
name *n;
lkey *lk;
n = infos->names;
if (n == NULL)
printf("eez\n");
while (!found && n){
printf("test name: %s\n", &(n->name[0]));
if (strcmp(&(n->name[0]), &(req->request[0])) == 0)
found = 1;
else
n = n->next;
}
if (!found){
printf("not found \n");
return NULL;
}
printf("name %s found\n", &(n->name[0]));
if (n->lKey == NULL)
return NULL;
r = malloc(sizeof(resp));
// error malloc
rbis = r;
lk = n->lKey;
while (lk){
rbis->req = malloc(sizeof(mdns));
// error malloc
memset(rbis->req, 0, sizeof(mdns));
rbis->req->infos = req->infos | RESPOND;
rbis->req->type = req->type;
strcpy(&(rbis->req->request[REQUEST]), &(req->request[0]));
strcpy(&(rbis->req->request[RESPONSE]), &(lk->key->key[0]));
printf("will send response: %s\n", &(lk->key->key[0]));
//generateHash();
lk = lk->next;
if (lk){
rbis->next = malloc(sizeof(resp));
// error malloc
rbis = rbis->next;
}
else
rbis->next = NULL;
}
return r;
}
resp *requestKey(mdns *req, linfo *infos){
//envoi d'une key, réponse ip
resp *r = NULL;
char found = 0;
key *keys;
keys = infos->keys;
while (!found && keys){
if (strcmp(&(keys->key[0]), &(req->request[0])) == 0)
found = 1;
else
keys = keys->next;
}
if (!found)
return NULL;
//si ip pas encore set (normalement 0.0.0.0)
if (strcmp(inet_ntoa(keys->ip.sin_addr), "0.0.0.0") == 0)
return NULL;
r = malloc(sizeof(resp));
r->next = NULL;
r->req = malloc(sizeof(mdns));
memset(r->req, 0, sizeof(mdns));
r->req->infos = req->infos | RESPOND;
r->req->type = req->type;
strcpy(&(r->req->request[REQUEST]), &(req->request[0]));
memcpy(&(r->req->request[RESPONSE]), &(keys->ip), sizeof(struct sockaddr_in));
return r;
}
resp *requestIpToKey(mdns *req, linfo *infos){
resp *r = NULL;
char found = 0;
key *keys;
keys = infos->keys;
while (!found && keys){
if (memcmp(&(keys->ip), &(req->request[0]), sizeof(struct sockaddr_in)) == 0)
found = 1;
else
keys = keys->next;
}
if (!found)
return NULL;
r = malloc(sizeof(resp));
r->next = NULL;
r->req = malloc(sizeof(mdns));
memset(r->req, 0, sizeof(mdns));
r->req->infos = req->infos | RESPOND;
r->req->type = req->type;
memcpy(&(r->req->request[REQUEST]), &(req->request[0]), sizeof(struct sockaddr_in));
strcpy(&(r->req->request[RESPONSE]), &(keys->key[0]));
return r;
}
resp *requestIpToName(mdns *req, linfo *infos){
resp *r = NULL;
resp *rbis;
char found = 0;
key *keys;
lname *ln;
keys = infos->keys;
while (!found && keys){
if (memcmp(&(keys->ip), &(req->request[0]), sizeof(struct sockaddr_in)) == 0)
found = 1;
else
keys = keys->next;
}
if (!found)
return NULL;
r = malloc(sizeof(resp));
// error malloc
rbis = r;
ln = keys->lName;
while (ln){
rbis->req = malloc(sizeof(mdns));
// error malloc
memset(rbis->req, 0, sizeof(mdns));
rbis->req->infos = req->infos | RESPOND;
rbis->req->type = req->type;
memcpy(&(rbis->req->request[REQUEST]), &(req->request[0]), sizeof(struct sockaddr_in));
strcpy(&(rbis->req->request[RESPONSE]), &(ln->name->name[0]));
//generateHash();
ln = ln->next;
if (ln){
rbis->next = malloc(sizeof(resp));
// error malloc
rbis = rbis->next;
}
else
rbis->next = NULL;
}
return r;
}
void freeResp(resp *r){
if (r == NULL)
return;
freeResp(r->next);
free(r->req);
free(r);
}