2
* Copyright (C) 2005 Jeremie Miller
3
* Copyright (C) 2005,2006 Justin Karneges
5
* Permission is hereby granted, free of charge, to any person obtaining a
6
* copy of this software and associated documentation files (the
7
* "Software"), to deal in the Software without restriction, including
8
* without limitation the rights to use, copy, modify, merge, publish,
9
* distribute, sublicense, and/or sell copies of the Software, and to
10
* permit persons to whom the Software is furnished to do so, subject to
11
* the following conditions:
13
* The above copyright notice and this permission notice shall be included
14
* in all copies or substantial portions of the Software.
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
#include "jdns_mdnsd.h"
30
#define QTYPE_A JDNS_RTYPE_A
31
#define QTYPE_AAAA JDNS_RTYPE_AAAA
32
#define QTYPE_MX JDNS_RTYPE_MX
33
#define QTYPE_SRV JDNS_RTYPE_SRV
34
#define QTYPE_CNAME JDNS_RTYPE_CNAME
35
#define QTYPE_PTR JDNS_RTYPE_PTR
36
#define QTYPE_TXT JDNS_RTYPE_TXT
37
#define QTYPE_HINFO JDNS_RTYPE_HINFO
38
#define QTYPE_NS JDNS_RTYPE_NS
39
#define QTYPE_ANY JDNS_RTYPE_ANY
41
// size of query/publish hashes
45
// brute force garbage cleanup frequency, rarely needed (daily default)
48
// maximum number of items to cache. if an attacker on the LAN advertises
49
// a million services, we don't want to crash our program trying to collect
50
// them all. any dns records received beyond this max are ignored. this
51
// means the attacker would succeed in DoS'ing the multicast dns network,
52
// but he wouldn't succeed in running our program out of memory.
53
#define MAX_CACHE 16384
55
#define bzero(p, size) memset(p, 0, size)
57
/* messy, but it's the best/simplest balance I can find at the moment
58
Some internal data types, and a few hashes: querys, answers, cached, and records (published, unique and shared)
59
Each type has different semantics for processing, both for timeouts, incoming, and outgoing I/O
60
They inter-relate too, like records affect the querys they are relevant to
61
Nice things about MDNS: we only publish once (and then ask asked), and only query once, then just expire records we've got cached
68
unsigned long int nexttry;
70
int (*answer)(mdnsda, void *);
72
struct query *next, *list;
80
unsigned char to6[16];
81
unsigned short int port;
88
struct mdnsda_struct rr;
95
struct mdnsda_struct rr;
96
char unique; // # of checks performed to ensure
98
void (*pubresult)(int, char *, int, void *);
100
struct mdnsdr_struct *next, *list;
106
unsigned long int expireall, checkqlist;
107
struct mytimeval now, sleep, pause, probe, publish;
109
struct cached *cache[LPRIME];
111
struct mdnsdr_struct *published[SPRIME], *probing, *a_now, *a_pause, *a_publish;
112
struct unicast *uanswers;
113
struct query *queries[SPRIME], *qlist;
114
int (*cb_time_now)(struct mdnsd_struct *dp, void *arg);
115
int (*cb_rand_int)(struct mdnsd_struct *dp, void *arg);
120
void mygettimeofday(mdnsd d, struct mytimeval *tv)
123
//gettimeofday(&t, 0);
124
//tv->tv_sec = t.tv_sec;
125
//tv->tv_usec = t.tv_usec;
127
int msec = d->cb_time_now(d, d->cb_arg);
128
tv->tv_sec = msec / 1000;
129
tv->tv_usec = (msec % 1000) * 1000;
132
void query_free(struct query *q)
138
void mdnsda_content_free(struct mdnsda_struct *rr)
143
jdns_free(rr->rdata);
145
jdns_free(rr->rdname);
148
int _namehash(const char *s)
150
const unsigned char *name = (const unsigned char *)s;
151
unsigned long h = 0, g;
154
{ /* do some fancy bitwanking on the string */
155
h = (h << 4) + (unsigned long)(*name++);
156
if ((g = (h & 0xF0000000UL))!=0)
164
// case-insensitive hash
165
int _namehash_nocase(const char *s)
168
char *low = jdns_strdup(s);
170
for(n = 0; n < len; ++n)
171
low[n] = tolower(low[n]);
177
// basic linked list and hash primitives
178
struct query *_q_next(mdnsd d, struct query *q, char *host, int type)
180
if(q == 0) q = d->queries[_namehash_nocase(host) % SPRIME];
182
for(;q != 0; q = q->next)
183
if(q->type == type && jdns_domain_cmp((unsigned char *)q->name, (unsigned char *)host))
187
struct cached *_c_next(mdnsd d, struct cached *c, char *host, int type)
189
if(c == 0) c = d->cache[_namehash_nocase(host) % LPRIME];
191
for(;c != 0; c = c->next)
192
if((type == c->rr.type || type == 255) && jdns_domain_cmp(c->rr.name, (unsigned char *)host))
196
mdnsdr _r_next(mdnsd d, mdnsdr r, char *host, int type)
198
if(r == 0) r = d->published[_namehash_nocase(host) % SPRIME];
200
for(;r != 0; r = r->next)
201
if(type == r->rr.type && jdns_domain_cmp(r->rr.name, (unsigned char *)host))
207
int _rr_len(mdnsda rr)
209
int len = 12; // name is always compressed (dup of earlier), plus normal stuff
210
if(rr->rdata) len += rr->rdlen;
211
if(rr->rdname) len += strlen((char *)rr->rdname); // worst case
213
if(rr->type == QTYPE_PTR) len += 6; // srv record stuff
219
int _a_match(struct resource *r, mdnsda a)
220
{ // compares new rdata with known a, painfully
221
if(strcmp((char *)r->name,(char *)a->name) || r->type != a->type) return 0;
222
if(r->type == QTYPE_SRV && !strcmp((char *)r->known.srv.name,(char *)a->rdname) && a->srv.port == r->known.srv.port && a->srv.weight == r->known.srv.weight && a->srv.priority == r->known.srv.priority) return 1;
223
if((r->type == QTYPE_PTR || r->type == QTYPE_NS || r->type == QTYPE_CNAME) && !strcmp((char *)a->rdname,(char *)r->known.ns.name)) return 1;
224
if(r->rdlength == a->rdlen && !memcmp(r->rdata,a->rdata,r->rdlength)) return 1;
229
int _a_match(const jdns_rr_t *r, mdnsda a)
231
if(r->type != a->type || !jdns_domain_cmp(r->owner, a->name))
233
if(r->type == JDNS_RTYPE_SRV)
235
if(jdns_domain_cmp(r->data.server->name, a->rdname)
236
&& r->data.server->port == a->srv.port
237
&& r->data.server->priority == a->srv.priority
238
&& r->data.server->weight == a->srv.weight
242
else if(r->type == JDNS_RTYPE_PTR || r->type == JDNS_RTYPE_NS || r->type == JDNS_RTYPE_CNAME)
244
if(jdns_domain_cmp(r->data.name, a->rdname))
247
else if(r->rdlength == a->rdlen && !memcmp(r->rdata, a->rdata, r->rdlength))
253
// compare time values easily
254
int _tvdiff(struct mytimeval old, struct mytimeval new)
257
if(old.tv_sec != new.tv_sec) udiff = (new.tv_sec - old.tv_sec) * 1000000;
258
return (new.tv_usec - old.tv_usec) + udiff;
261
// make sure not already on the list, then insert
262
void _r_push(mdnsdr *list, mdnsdr r)
265
for(cur = *list; cur != 0; cur = cur->list)
271
// set this r to probing, set next probe time
272
void _r_probe(mdnsd d, mdnsdr r)
278
// force any r out right away, if valid
279
void _r_publish(mdnsd d, mdnsdr r)
281
if(r->unique && r->unique < 5) return; // probing already
283
d->publish.tv_sec = d->now.tv_sec; d->publish.tv_usec = d->now.tv_usec;
284
_r_push(&d->a_publish,r);
288
void _r_send(mdnsd d, mdnsdr r)
293
if(d->a_publish == r)
294
d->a_publish = r->list;
295
_r_push(&d->a_now, r);
300
{ // being published, make sure that happens soon
301
d->publish.tv_sec = d->now.tv_sec; d->publish.tv_usec = d->now.tv_usec;
305
{ // known unique ones can be sent asap
306
_r_push(&d->a_now,r);
309
// set d->pause.tv_usec to random 20-120 msec
310
d->pause.tv_sec = d->now.tv_sec;
311
//d->pause.tv_usec = d->now.tv_usec + ((d->now.tv_usec % 100) + 20) * 1000;
312
d->pause.tv_usec = d->now.tv_usec;
313
d->pause.tv_usec += ((d->cb_rand_int(d, d->cb_arg) % 100) + 20) * 1000;
314
_r_push(&d->a_pause,r);
317
// create generic unicast response struct
318
void _u_push(mdnsd d, mdnsdr r, int id, const jdns_address_t *addr, unsigned short int port)
321
u = (struct unicast *)jdns_alloc(sizeof(struct unicast));
322
bzero(u,sizeof(struct unicast));
328
memcpy(u->to6, addr->addr.v6, 16);
333
u->to = addr->addr.v4;
336
u->next = d->uanswers;
340
void _q_reset(mdnsd d, struct query *q)
342
struct cached *cur = 0;
345
while((cur = _c_next(d,cur,q->name,q->type)))
346
if(q->nexttry == 0 || cur->rr.ttl - 7 < q->nexttry) q->nexttry = cur->rr.ttl - 7;
347
if(q->nexttry != 0 && q->nexttry < d->checkqlist) d->checkqlist = q->nexttry;
350
void _q_done(mdnsd d, struct query *q)
351
{ // no more query, update all it's cached entries, remove from lists
352
struct cached *c = 0;
354
int i = _namehash_nocase(q->name) % SPRIME;
355
while((c = _c_next(d,c,q->name,q->type))) c->q = 0;
356
if(d->qlist == q) d->qlist = q->list;
358
for(cur=d->qlist;cur->list != q;cur = cur->list);
361
if(d->queries[i] == q) d->queries[i] = q->next;
363
for(cur=d->queries[i];cur->next != q;cur = cur->next);
369
void _r_done(mdnsd d, mdnsdr r)
370
{ // buh-bye, remove from hash and free
372
int i = _namehash_nocase((char *)r->rr.name) % SPRIME;
376
d->a_pause = r->list;
377
if(d->a_publish == r)
378
d->a_publish = r->list;
379
if(d->published[i] == r) d->published[i] = r->next;
381
for(cur=d->published[i];cur && cur->next != r;cur = cur->next);
382
if(cur) cur->next = r->next;
384
mdnsda_content_free(&r->rr);
388
void _q_answer(mdnsd d, struct cached *c)
389
{ // call the answer function with this cached entry
390
if(c->rr.ttl <= d->now.tv_sec) c->rr.ttl = 0;
391
if(c->q->answer(&c->rr,c->q->arg) == -1) _q_done(d, c->q);
394
void _conflict(mdnsd d, mdnsdr r)
396
r->pubresult(0, (char *)r->rr.name,r->rr.type,r->arg);
400
void _published(mdnsd d, mdnsdr r)
403
r->pubresult(1, (char *)r->rr.name,r->rr.type,r->arg);
406
void _c_expire(mdnsd d, struct cached **list)
407
{ // expire any old entries in this list
408
struct cached *next, *cur = *list, *last = 0;
412
if(d->now.tv_sec >= cur->rr.ttl)
414
if(last) last->next = next;
415
if(*list == cur) *list = next; // update list pointer if the first one expired
417
if(cur->q) _q_answer(d,cur);
418
mdnsda_content_free(&cur->rr);
427
// brute force expire any old cached records
431
for(i=0;i<LPRIME;i++)
432
if(d->cache[i]) _c_expire(d,&d->cache[i]);
433
d->expireall = d->now.tv_sec + GC;
436
struct cached *_find_exact(mdnsd d, const jdns_rr_t *r)
438
struct cached *c = 0;
441
c = _c_next(d, c, (char *)r->owner, r->type);
444
if(_a_match(r, &c->rr))
450
void _cache(mdnsd d, const jdns_rr_t *r)
453
int i = _namehash_nocase((char *)r->owner) % LPRIME;
454
struct cached *same_value;
456
// do we already have it?
457
//printf("cache: checking for entry: [%s] [%d]\n", r->owner, r->type);
458
same_value = _find_exact(d, r);
461
//printf("already have entry of same value\n");
464
if(r->qclass == 32768 + d->class)
466
// simulate removal of all records for this question,
467
// except if the value hasn't changed
469
while((c = _c_next(d,c,(char *)r->owner,r->type)))
474
_c_expire(d,&d->cache[i]);
476
// we may have expired same_value here, so check for it again
477
same_value = _find_exact(d, r);
483
same_value->rr.ttl = 0;
484
_c_expire(d,&d->cache[i]);
490
//printf("updating ttl only\n");
492
// only update ttl (this code directly copied from below)
493
same_value->rr.ttl = d->now.tv_sec + (r->ttl / 2) + 8;
494
same_value->rr.real_ttl = r->ttl;
498
//printf("cache: inserting entry: [%s] [%d]\n", r->owner, r->type);
499
if(d->cache_count >= MAX_CACHE)
502
c = (struct cached *)jdns_alloc(sizeof(struct cached));
503
bzero(c,sizeof(struct cached));
504
c->rr.name = (unsigned char *)jdns_strdup((char *)r->owner);
505
c->rr.type = r->type;
506
c->rr.ttl = d->now.tv_sec + (r->ttl / 2) + 8; // XXX hack for now, BAD SPEC, start retrying just after half-waypoint, then expire
507
c->rr.real_ttl = r->ttl;
508
c->rr.rdlen = r->rdlength;
509
c->rr.rdata = jdns_copy_array(r->rdata, r->rdlength);
513
c->rr.ip = r->data.address->addr.v4;
518
c->rr.rdname = (unsigned char *)jdns_strdup((const char *)r->data.name);
521
c->rr.rdname = (unsigned char *)jdns_strdup((const char *)r->data.server->name);
522
c->rr.srv.port = r->data.server->port;
523
c->rr.srv.weight = r->data.server->weight;
524
c->rr.srv.priority = r->data.server->priority;
527
c->next = d->cache[i];
529
if((c->q = _q_next(d, 0, (char *)r->owner, r->type)))
531
if(c->q && c->q->nexttry == 0)
533
//printf("cache insert, but nexttry == 0\n");
535
if(d->checkqlist == 0)
536
d->checkqlist = c->q->nexttry;
537
//printf("after reset: q->nexttry=%d d->checkqlist=%d\n", c->q->nexttry, d->checkqlist);
542
void _a_copy(struct message *m, mdnsda a)
543
{ // copy the data bits only
544
if(a->rdata) { message_rdata_raw(m, a->rdata, a->rdlen); return; }
545
if(a->ip) message_rdata_long(m, a->ip);
546
if(a->type == QTYPE_SRV) message_rdata_srv(m, a->srv.priority, a->srv.weight, a->srv.port, a->rdname);
547
else if(a->rdname) message_rdata_name(m, a->rdname);
551
void _a_copyq(jdns_list_t *dest, unsigned char *name, unsigned short type, unsigned short class)
553
jdns_packet_question_t *q = jdns_packet_question_new();
554
q->qname = jdns_string_new();
555
jdns_string_set_cstr(q->qname, (char *)name);
558
jdns_list_insert(dest, q, -1);
559
jdns_packet_question_delete(q);
562
void _a_copy(jdns_list_t *dest, unsigned char *name, unsigned short type, unsigned short class, unsigned long int ttl, mdnsda a)
564
jdns_packet_resource_t *r = jdns_packet_resource_new();
565
r->qname = jdns_string_new();
566
jdns_string_set_cstr(r->qname, (char *)name);
571
jdns_packet_resource_add_bytes(r, a->rdata, a->rdlen);
574
unsigned long int ip;
576
jdns_packet_resource_add_bytes(r, (unsigned char *)&ip, 4);
578
else if(a->type == QTYPE_SRV)
580
unsigned short priority, weight, port;
582
priority = htons(a->srv.priority);
583
weight = htons(a->srv.weight);
584
port = htons(a->srv.port);
585
name = jdns_string_new();
586
jdns_string_set_cstr(name, (const char *)a->rdname);
587
jdns_packet_resource_add_bytes(r, (unsigned char *)&priority, 2);
588
jdns_packet_resource_add_bytes(r, (unsigned char *)&weight, 2);
589
jdns_packet_resource_add_bytes(r, (unsigned char *)&port, 2);
590
jdns_packet_resource_add_name(r, name);
591
jdns_string_delete(name);
596
name = jdns_string_new();
597
jdns_string_set_cstr(name, (const char *)a->rdname);
598
jdns_packet_resource_add_name(r, name);
599
jdns_string_delete(name);
601
jdns_list_insert(dest, r, -1);
602
jdns_packet_resource_delete(r);
606
int _r_out(mdnsd d, struct message *m, mdnsdr *list)
607
{ // copy a published record into an outgoing message
610
while((r = *list) != 0 && message_packet_len(m) + _rr_len(&r->rr) < d->frame)
615
message_an(m, r->rr.name, r->rr.type, (unsigned short)(d->class + 32768), r->rr.ttl);
617
message_an(m, r->rr.name, r->rr.type, (unsigned short)d->class, r->rr.ttl);
619
if(r->rr.ttl == 0) _r_done(d,r);
625
int _r_out(mdnsd d, jdns_packet_t *m, mdnsdr *list)
626
{ // copy a published record into an outgoing message
628
unsigned short class;
630
while((r = *list) != 0)
634
class = r->unique ? d->class | 0x8000 : d->class;
635
_a_copy(m->answerRecords, r->rr.name, r->rr.type, class, r->rr.ttl, &r->rr);
636
if(r->rr.ttl == 0) _r_done(d,r);
642
mdnsd mdnsd_new(int class, int frame, int port, int (*time_now)(mdnsd d, void *arg), int (*rand_int)(mdnsd d, void *arg), void *arg)
646
d = (mdnsd)jdns_alloc(sizeof(struct mdnsd_struct));
647
bzero(d,sizeof(struct mdnsd_struct));
648
d->cb_time_now = time_now;
649
d->cb_rand_int = rand_int;
651
mygettimeofday(d, &d->now);
652
d->expireall = d->now.tv_sec + GC;
660
void mdnsd_shutdown(mdnsd d)
661
{ // shutting down, zero out ttl and push out all records
665
for(i=0;i<SPRIME;i++)
666
for(cur = d->published[i]; cur != 0;)
670
cur->list = d->a_now;
677
void mdnsd_flush(mdnsd d)
679
// set all querys to 0 tries
681
// set all mdnsdr to probing
682
// reset all answer lists
687
void mdnsd_free(mdnsd d)
691
// loop through all hashes, free everything
692
// free answers if any
694
for(i = 0; i < LPRIME; ++i)
698
struct cached *cur = d->cache[i];
699
d->cache[i] = cur->next;
700
mdnsda_content_free(&cur->rr);
705
for(i = 0; i < SPRIME; ++i)
707
while(d->published[i])
709
struct mdnsdr_struct *cur = d->published[i];
710
d->published[i] = cur->next;
711
mdnsda_content_free(&cur->rr);
718
struct unicast *u = d->uanswers;
719
d->uanswers = u->next;
723
for(i = 0; i < SPRIME; ++i)
727
struct query *cur = d->queries[i];
728
d->queries[i] = cur->next;
736
void mdnsd_in(mdnsd d, const jdns_packet_t *m, const jdns_response_t *resp, const jdns_address_t *addr, unsigned short int port)
741
if(d->shutdown) return;
743
mygettimeofday(d, &d->now);
747
for(i=0;i<m->questions->count;i++)
748
{ // process each query
749
jdns_packet_question_t *pq = (jdns_packet_question_t *)m->questions->item[i];
751
if(pq->qclass != d->class || (r = _r_next(d,0,(char *)pq->qname->data,pq->qtype)) == 0) continue;
753
// send the matching unicast reply
754
if(port != d->port) _u_push(d,r,m->id,addr,port);
756
for(;r != 0; r = _r_next(d,r,(char *)pq->qname->data,pq->qtype))
757
{ // check all of our potential answers
758
if(r->unique && r->unique < 5)
759
{ // probing state, check for conflicts
760
for(j=0;j<resp->authorityCount;j++)
761
{ // check all to-be answers against our own
762
jdns_rr_t *ns = resp->authorityRecords[j];
763
if(pq->qtype != ns->type || !jdns_domain_cmp(pq->qname->data, ns->owner)) continue;
764
if(!_a_match(ns,&r->rr))
766
_conflict(d,r); // answer isn't ours, conflict!
768
// r is invalid after conflict, start all over
775
for(j=0;j<resp->answerCount;j++)
776
{ // check the known answers for this question
777
jdns_rr_t *an = resp->answerRecords[j];
778
if(pq->qtype != an->type || !jdns_domain_cmp(pq->qname->data, an->owner)) continue;
779
if(_a_match(an,&r->rr)) break; // they already have this answer
781
if(j == resp->answerCount) _r_send(d,r);
787
for(i=0;i<resp->answerCount;i++)
788
{ // process each answer, check for a conflict, and cache
789
jdns_rr_t *an = resp->answerRecords[i];
790
if((r = _r_next(d,0,(char *)an->owner,an->type)) != 0 && r->unique && _a_match(an,&r->rr) == 0) _conflict(d,r);
794
// cache additional records
795
for(i=0;i<resp->additionalCount;i++)
797
jdns_rr_t *an = resp->additionalRecords[i];
802
int mdnsd_out(mdnsd d, jdns_packet_t **_m, jdns_address_t **addr, unsigned short int *port)
808
mygettimeofday(d, &d->now);
809
//bzero(m,sizeof(struct message));
810
m = jdns_packet_new();
812
// defaults, multicast
813
*port = 0; //htons(5353);
815
// *ip = 0; //inet_addr("224.0.0.251");
820
{ // send out individual unicast answers
821
struct unicast *u = d->uanswers;
822
d->uanswers = u->next;
825
*addr = jdns_address_new();
827
jdns_address_set_ipv6(*addr, u->to6);
829
jdns_address_set_ipv4(*addr, u->to);
831
_a_copyq(m->questions, u->r->rr.name, u->r->rr.type, (unsigned short)d->class);
832
_a_copy(m->answerRecords, u->r->rr.name, u->r->rr.type, (unsigned short)d->class, u->r->rr.ttl, &u->r->rr);
838
//printf("OUT: probing %X now %X pause %X publish %X\n",d->probing,d->a_now,d->a_pause,d->a_publish);
840
// accumulate any immediate responses
841
if(d->a_now) { ret += _r_out(d, m, &d->a_now); }
843
if(d->a_publish && _tvdiff(d->now,d->publish) <= 0)
844
{ // check to see if it's time to send the publish retries (and unlink if done)
845
mdnsdr next, cur = d->a_publish, last = 0;
846
unsigned short class;
847
while(cur /*&& message_packet_len(m) + _rr_len(&cur->rr) < d->frame*/ )
851
class = cur->unique ? d->class | 0x8000 : d->class;
852
_a_copy(m->answerRecords, cur->rr.name, cur->rr.type, class, cur->rr.ttl, &cur->rr);
854
if(cur->rr.ttl != 0 && cur->tries < 4)
860
if(d->a_publish == cur) d->a_publish = next;
861
if(last) last->list = next;
862
if(cur->rr.ttl == 0) _r_done(d,cur);
867
d->publish.tv_sec = d->now.tv_sec + 2;
868
d->publish.tv_usec = d->now.tv_usec;
872
// if we're in shutdown, we're done
876
// check if a_pause is ready
877
if(d->a_pause && _tvdiff(d->now, d->pause) <= 0) ret += _r_out(d, m, &d->a_pause);
879
// now process questions
885
if(d->probing && _tvdiff(d->now,d->probe) <= 0)
888
for(r = d->probing; r != 0;)
889
{ // scan probe list to ask questions and process published
891
{ // done probing, publish
892
mdnsdr next = r->list;
894
d->probing = r->list;
896
last->list = r->list;
904
//message_qd(m, r->rr.name, r->rr.type, (unsigned short)d->class);
905
_a_copyq(m->questions, r->rr.name, r->rr.type, (unsigned short)d->class);
909
for(r = d->probing; r != 0; last = r, r = r->list)
910
{ // scan probe list again to append our to-be answers
912
_a_copy(m->authorityRecords, r->rr.name, r->rr.type, (unsigned short)d->class, r->rr.ttl, &r->rr);
916
{ // process probes again in the future
917
d->probe.tv_sec = d->now.tv_sec;
918
d->probe.tv_usec = d->now.tv_usec + 250000;
923
if(d->checkqlist && d->now.tv_sec >= d->checkqlist)
924
{ // process qlist for retries or expirations
927
unsigned long int nextbest = 0;
929
// ask questions first, track nextbest time
930
for(q = d->qlist; q != 0; q = q->list)
931
if(q->nexttry > 0 && q->nexttry <= d->now.tv_sec && q->tries < 3)
932
_a_copyq(m->questions, (unsigned char *)q->name, (unsigned short)q->type, (unsigned short)d->class);
933
else if(q->nexttry > 0 && (nextbest == 0 || q->nexttry < nextbest))
934
nextbest = q->nexttry;
936
// include known answers, update questions
937
for(q = d->qlist; q != 0; q = q->list)
939
if(q->nexttry == 0 || q->nexttry > d->now.tv_sec) continue;
941
{ // done retrying, expire and reset
942
_c_expire(d,&d->cache[_namehash_nocase(q->name) % LPRIME]);
947
q->nexttry = d->now.tv_sec + ++q->tries;
948
if(nextbest == 0 || q->nexttry < nextbest)
949
nextbest = q->nexttry;
950
// if room, add all known good entries
952
while((c = _c_next(d,c,q->name,q->type)) != 0 && c->rr.ttl > d->now.tv_sec + 8 /* && message_packet_len(m) + _rr_len(&c->rr) < d->frame */)
954
_a_copy(m->answerRecords, (unsigned char *)q->name, (unsigned short)q->type, (unsigned short)d->class, (unsigned long int)(c->rr.ttl - d->now.tv_sec), &c->rr);
957
d->checkqlist = nextbest;
960
if(d->now.tv_sec > d->expireall)
967
jdns_packet_delete(m);
972
struct mytimeval *mdnsd_sleep(mdnsd d)
976
d->sleep.tv_sec = d->sleep.tv_usec = 0;
977
#define RET while(d->sleep.tv_usec > 1000000) {d->sleep.tv_sec++;d->sleep.tv_usec -= 1000000;} return &d->sleep;
979
// first check for any immediate items to handle
980
if(d->uanswers || d->a_now) return &d->sleep;
982
mygettimeofday(d, &d->now);
985
{ // then check for paused answers
986
if((usec = _tvdiff(d->now,d->pause)) > 0) d->sleep.tv_usec = usec;
991
{ // now check for probe retries
992
if((usec = _tvdiff(d->now,d->probe)) > 0) d->sleep.tv_usec = usec;
997
{ // now check for publish retries
998
if((usec = _tvdiff(d->now,d->publish)) > 0) d->sleep.tv_usec = usec;
1003
{ // also check for queries with known answer expiration/retry
1004
if((sec = d->checkqlist - d->now.tv_sec) > 0) d->sleep.tv_sec = sec;
1008
// last resort, next gc expiration
1009
if((sec = d->expireall - d->now.tv_sec) > 0) d->sleep.tv_sec = sec;
1013
void mdnsd_query(mdnsd d, char *host, int type, int (*answer)(mdnsda a, void *arg), void *arg)
1016
struct cached *cur = 0;
1017
int i = _namehash_nocase(host) % SPRIME;
1018
if(!(q = _q_next(d,0,host,type)))
1021
q = (struct query *)jdns_alloc(sizeof(struct query));
1022
bzero(q,sizeof(struct query));
1023
q->name = jdns_strdup(host);
1025
q->next = d->queries[i];
1027
d->qlist = d->queries[i] = q;
1030
while((cur = _c_next(d,cur,q->name,q->type)))
1032
cur->q = q; // any cached entries should be associated
1033
_q_answer(d,cur); // and reported!
1036
q->nexttry = d->checkqlist = d->now.tv_sec; // new questin, immediately send out
1040
{ // no answer means we don't care anymore
1048
mdnsda mdnsd_list(mdnsd d, char *host, int type, mdnsda last)
1050
return (mdnsda)_c_next(d,(struct cached *)last,host,type);
1053
mdnsdr mdnsd_shared(mdnsd d, char *host, int type, long int ttl)
1055
int i = _namehash_nocase(host) % SPRIME;
1057
r = (mdnsdr)jdns_alloc(sizeof(struct mdnsdr_struct));
1058
bzero(r,sizeof(struct mdnsdr_struct));
1059
r->rr.name = (unsigned char *)jdns_strdup(host);
1062
r->next = d->published[i];
1063
d->published[i] = r;
1067
mdnsdr mdnsd_unique(mdnsd d, char *host, int type, long int ttl, void (*pubresult)(int result, char *host, int type, void *arg), void *arg)
1070
r = mdnsd_shared(d,host,type,ttl);
1071
r->pubresult = pubresult;
1074
_r_push(&d->probing,r);
1075
d->probe.tv_sec = d->now.tv_sec;
1076
d->probe.tv_usec = d->now.tv_usec;
1080
void mdnsd_done(mdnsd d, mdnsdr r)
1083
if(r->unique && r->unique < 5)
1084
{ // probing yet, zap from that list first!
1085
if(d->probing == r) d->probing = r->list;
1087
for(cur=d->probing;cur->list != r;cur = cur->list);
1088
cur->list = r->list;
1097
void mdnsd_set_raw(mdnsd d, mdnsdr r, char *data, int len)
1100
jdns_free(r->rr.rdata);
1101
r->rr.rdata = jdns_copy_array((unsigned char*)data, len);
1106
void mdnsd_set_host(mdnsd d, mdnsdr r, char *name)
1108
jdns_free(r->rr.rdname);
1109
r->rr.rdname = (unsigned char *)jdns_strdup(name);
1113
void mdnsd_set_ip(mdnsd d, mdnsdr r, unsigned long int ip)
1119
void mdnsd_set_srv(mdnsd d, mdnsdr r, int priority, int weight, int port, char *name)
1121
r->rr.srv.priority = priority;
1122
r->rr.srv.weight = weight;
1123
r->rr.srv.port = port;
1124
mdnsd_set_host(d,r,name);