-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.c
183 lines (160 loc) · 3.92 KB
/
response.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
//meshDNS by rémi arnaud
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "meshDNS.h"
#include "config.h"
#include "parsing.h"
#include "request.h"
void addNameToInfo(name *n, linfo *infos){
name *tmp;
if (infos->names == NULL)
infos->names = malloc(sizeof(name));
tmp = infos->names;
while(tmp->next)
tmp = tmp->next;
tmp->next = n;
}
void addKeyToInfo(key *k, linfo *infos){
key *tmp;
if (infos->keys == NULL)
infos->keys = malloc(sizeof(key));
tmp = infos->keys;
while(tmp->next)
tmp = tmp->next;
tmp->next = k;
}
void responseName(mdns *req, linfo *infos){
name *n;
key *k;
struct sockaddr_in ip;
char found;
printf("response name: %s\n", &(req->request[RESPONSE]));
// memset ip 0
n = infos->names;
k = infos->keys;
found = 0;
while (!found && n){
if (strcmp(&(n->name[0]), &(req->request[REQUEST])) == 0)
found = 1;
else
n = n->next;
}
found = 0;
while (!found && k){
if (strcmp(&(k->key[0]), &(req->request[RESPONSE])) == 0)
found = 1;
else
k = k->next;
}
if (n == NULL){
n = newName(&(req->request[REQUEST]), NULL);
addNameToInfo(n, infos);
}
if (k == NULL){
k = newKey(ip, &(req->request[RESPONSE]), NULL);
addKeyToInfo(k, infos);
}
addKeyToName(k, n);
addNameToKey(n, k);
}
void responseKey(mdns *req, linfo *infos){
key *k;
key *kbis;
struct sockaddr_in ip;
struct sockaddr_in ipv;
char found;
// verbose
memcpy(&(ipv), &(req->request[RESPONSE]), sizeof(struct sockaddr_in));
printf("response key: %s\n", inet_ntoa(ipv.sin_addr));
k = infos->keys;
kbis = infos->keys;
found = 0;
while (!found && k){
if (strcmp(&(k->key[0]), &(req->request[REQUEST])))
found = 1;
else
k = k->next;
}
found = 0;
while (!found && kbis){
if (memcmp(&(kbis->ip), &(req->request[RESPONSE]), sizeof(struct sockaddr_in)) == 0)
found = 1;
else
kbis = kbis->next;
}
if (k && !kbis)
memcpy(&(k->ip), &(req->request[RESPONSE]), sizeof(struct sockaddr_in));
else if (!k && kbis)
strcpy(&(kbis->key[0]), &(req->request[REQUEST]));
else if (!k && !kbis){
memcpy(&(ip), &(req->request[RESPONSE]), sizeof(struct sockaddr_in));
k = newKey(ip, &(req->request[REQUEST]), NULL);
addKeyToInfo(k, infos);
}
}
void responseIpToName(mdns *req, linfo *infos){
name *n;
key *k;
struct sockaddr_in ip;
char found;
// memset ip 0
n = infos->names;
k = infos->keys;
found = 0;
while (!found && n){
if (strcmp(&(n->name[0]), &(req->request[RESPONSE])) == 0)
found = 1;
else
n = n->next;
}
found = 0;
while (!found && k){
if (memcmp(&(k->ip), &(req->request[REQUEST]), sizeof(struct sockaddr_in)) == 0)
found = 1;
else
k = k->next;
}
if (n == NULL){
n = newName(&(req->request[RESPONSE]), NULL);
addNameToInfo(n, infos);
}
if (k == NULL){
memcpy(&(ip), &(req->request[REQUEST]), sizeof(struct sockaddr_in));
k = newKey(ip, NULL, NULL);
addKeyToInfo(k, infos);
}
addKeyToName(k, n);
addNameToKey(n, k);
}
void responseIpToKey(mdns *req, linfo *infos){
key *k;
key *kbis;
struct sockaddr_in ip;
char found;
k = infos->keys;
kbis = infos->keys;
found = 0;
while (!found && k){
if (strcmp(&(k->key[0]), &(req->request[RESPONSE])))
found = 1;
else
k = k->next;
}
found = 0;
while (!found && kbis){
if (memcmp(&(kbis->ip), &(req->request[REQUEST]), sizeof(struct sockaddr_in)) == 0)
found = 1;
else
kbis = kbis->next;
}
if (k && !kbis)
memcpy(&(k->ip), &(req->request[REQUEST]), sizeof(struct sockaddr_in));
else if (!k && kbis)
strcpy(&(kbis->key[0]), &(req->request[RESPONSE]));
else if (!k && !kbis){
memcpy(&(ip), &(req->request[REQUEST]), sizeof(struct sockaddr_in));
k = newKey(ip, &(req->request[RESPONSE]), NULL);
addKeyToInfo(k, infos);
}
}