-
Notifications
You must be signed in to change notification settings - Fork 4
/
mdns-scan.c
237 lines (173 loc) · 5.5 KB
/
mdns-scan.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
/* $Id: mdns-scan.c 56 2004-12-21 17:04:19Z lennart $ */
/***
This file is part of mdns-scan.
mdns-scan is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
mdns-scan is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with mdns-scan; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
***/
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "dns.h"
#include "query.h"
#include "util.h"
#define SERVICE_RR "_services._dns-sd._udp.local"
#define HASH_TABLE_SIZE 1023
struct hash_entry {
unsigned hash;
char *name;
struct hash_entry *next;
};
struct hash_entry *hash_table[HASH_TABLE_SIZE];
static unsigned hash_func(const char *c) {
unsigned hash = 0;
for (; *c; c++)
hash = 31 * hash + *c;
return hash;
}
static void handle_service(const char *service) {
struct hash_entry *e;
unsigned h, i;
i = (h = hash_func(service)) % HASH_TABLE_SIZE;
if (hash_table[i]) {
for (e = hash_table[i]; e; e = e->next)
if (e->hash == h)
if (!strcmp(e->name, service))
return;
}
e = malloc(sizeof(struct hash_entry));
assert(e);
e->hash = h;
e->name = strdup(service);
assert(e->name);
e->next = hash_table[i];
hash_table[i] = e;
fprintf(stderr, "+ %s\n", service);
}
static void free_hash_table(void) {
unsigned i;
struct hash_entry **e;
for (i = 0, e = hash_table; i < HASH_TABLE_SIZE; i++, e++) {
while (*e) {
struct hash_entry *n = (*e)->next;
free((*e)->name);
free(*e);
*e = n;
}
}
}
static int send_query(int fd, const char*name, uint16_t type) {
int ret = -1;
struct dns_packet *p = NULL;
assert(fd >= 0);
if (!(p = dns_packet_new())) {
fprintf(stderr, "Failed to allocate DNS packet.\n");
goto finish;
}
dns_packet_set_field(p, DNS_FIELD_FLAGS, DNS_FLAGS(0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
if (!dns_packet_append_name(p, name)) {
fprintf(stderr, "Bad host name\n");
goto finish;
}
dns_packet_append_uint16(p, type);
dns_packet_append_uint16(p, DNS_CLASS_IN);
dns_packet_set_field(p, DNS_FIELD_QDCOUNT, 1);
if (mdns_send_dns_packet(fd, p) < 0)
goto finish;
ret = 0;
finish:
if (p)
dns_packet_free(p);
return ret;
}
static int handle_packet(int fd, struct dns_packet *p) {
for (;;) {
char pname[256];
uint16_t type, class;
uint32_t rr_ttl;
uint16_t rdlength;
if (dns_packet_consume_name(p, pname, sizeof(pname)) < 0 ||
dns_packet_consume_uint16(p, &type) < 0 ||
dns_packet_consume_uint16(p, &class) < 0 ||
dns_packet_consume_uint32(p, &rr_ttl) < 0 ||
dns_packet_consume_uint16(p, &rdlength) < 0) {
break;
}
/* Remove mDNS cache flush bit */
class &= ~0x8000;
if (type == DNS_TYPE_PTR &&
class == DNS_CLASS_IN &&
strcmp(pname, SERVICE_RR) == 0) {
char service[256];
if (dns_packet_consume_name(p, service, sizeof(service)) < 0)
break;
if (send_query(fd, service, DNS_TYPE_PTR) < 0)
return -1;
} else if (type == DNS_TYPE_PTR &&
class == DNS_CLASS_IN &&
(strstr(pname, "._tcp.") || strstr(pname, "._udp."))) {
char service[256];
if (dns_packet_consume_name(p, service, sizeof(service)) < 0)
break;
handle_service(service);
} else
if (dns_packet_consume_seek(p, rdlength) < 0)
break;
}
return 0;
}
static char rotdash(void) {
static const char *rd = "/-\\|\\-";
static const char*c = NULL;
if (!c || !*c)
c = rd;
return *(c++);
}
static int event_loop(int fd) {
struct timeval tv;
for (;;) {
fprintf(stderr, "Browsing ... %c \r", rotdash());
if (send_query(fd, SERVICE_RR, DNS_TYPE_PTR) < 0)
return -1;
gettimeofday(&tv, NULL);
timeval_add(&tv, 1000000);
for (;;) {
struct dns_packet *p = NULL;
uint8_t ttl;
int r;
if ((r = mdns_recv_dns_packet(fd, &p, &ttl, &tv)) < 0)
return -1;
if (!p) /* Timeout */
break;
assert(p);
if (ttl == 0xFF && dns_packet_check_valid_response(p) >= 0) {
/* valid packet */
handle_packet(fd, p);
}
dns_packet_free(p);
}
}
}
int main(int argc, char*argv[]) {
int fd = -1;
int ret = 1;
memset(&hash_table, 0, sizeof(hash_table));
if ((fd = mdns_open_socket()) < 0)
goto finish;
ret = event_loop(fd) < 0 ? 1 : 0;
finish:
if (fd >= 0)
close(fd);
free_hash_table();
return ret;
}