annotate src/protocols/rendezvous/mdns.c @ 8791:407355e05a0a

[gaim-migrate @ 9553] These files haven't been touched in ages, but someone was only able to compile Zephyr when <internal.h> was changed to "internal.h", so here we go. committer: Tailor Script <tailor@pidgin.im>
author Christian Hammond <chipx86@chipx86.com>
date Sat, 24 Apr 2004 09:00:37 +0000
parents dbbf5470ba05
children 8212661dc3cc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
1 /**
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
2 * @file mdns.c Multicast DNS connection code used by rendezvous.
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
3 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
4 * gaim
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
5 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
6 * Gaim is the legal property of its developers, whose names are too numerous
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
7 * to list here. Please refer to the COPYRIGHT file distributed with this
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
8 * source distribution.
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
9 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
10 * This program is free software; you can redistribute it and/or modify
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
11 * it under the terms of the GNU General Public License as published by
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
12 * the Free Software Foundation; either version 2 of the License, or
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
13 * (at your option) any later version.
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
14 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
15 * This program is distributed in the hope that it will be useful,
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
18 * GNU General Public License for more details.
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
19 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
20 * You should have received a copy of the GNU General Public License
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
21 * along with this program; if not, write to the Free Software
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
23 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
24 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
25
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
26 /*
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
27 * If you want to understand this, read RFC1035 and
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
28 * draft-cheshire-dnsext-multicastdns.txt, and buy
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
29 * me a doughnut. thx k bye.
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
30 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
31
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
32 /*
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
33 * XXX - This entire file could use another pair of eyes to audit for
8738
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
34 * any possible buffer overflow exploits. It doesn't even HAVE to be
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
35 * a pair, neither--one eye would suffice. Oh, snap, somebody call
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
36 * One Eyed Willie.
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
37 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
38
8546
9aa6017da568 [gaim-migrate @ 9288]
Mark Doliner <mark@kingant.net>
parents: 8487
diff changeset
39 #include "internal.h"
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
40 #include "debug.h"
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
41
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
42 #include "mdns.h"
8738
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
43 #include "mdns_cache.h"
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
44 #include "util.h"
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
45
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
46 /******************************************/
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
47 /* Functions for freeing a DNS structure */
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
48 /******************************************/
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
49
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
50 /**
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
51 * Free the rdata associated with a given resource record.
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
52 */
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
53 static void
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
54 mdns_free_rr_rdata(unsigned short type, void *rdata)
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
55 {
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
56 if (rdata == NULL)
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
57 return;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
58
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
59 switch (type) {
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
60 case RENDEZVOUS_RRTYPE_NULL:
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
61 case RENDEZVOUS_RRTYPE_PTR:
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
62 g_free(rdata);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
63 break;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
64
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
65 case RENDEZVOUS_RRTYPE_TXT:
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
66 g_hash_table_destroy(rdata);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
67 break;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
68
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
69 case RENDEZVOUS_RRTYPE_SRV:
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
70 g_free(((ResourceRecordRDataSRV *)rdata)->target);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
71 g_free(rdata);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
72 break;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
73 }
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
74 }
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
75
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
76 /**
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
77 * Free a given question
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
78 */
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
79 static void
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
80 mdns_free_q(Question *q)
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
81 {
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
82 g_free(q->name);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
83 }
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
84
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
85 /**
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
86 * Free a given resource record.
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
87 */
8738
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
88 void
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
89 mdns_free_rr(ResourceRecord *rr)
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
90 {
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
91 g_free(rr->name);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
92 mdns_free_rr_rdata(rr->type, rr->rdata);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
93 }
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
94
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
95 void
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
96 mdns_free(DNSPacket *dns)
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
97 {
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
98 int i;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
99
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
100 for (i = 0; i < dns->header.numquestions; i++)
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
101 mdns_free_q(&dns->questions[i]);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
102 for (i = 0; i < dns->header.numanswers; i++)
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
103 mdns_free_rr(&dns->answers[i]);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
104 for (i = 0; i < dns->header.numauthority; i++)
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
105 mdns_free_rr(&dns->authority[i]);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
106 for (i = 0; i < dns->header.numadditional; i++)
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
107 mdns_free_rr(&dns->additional[i]);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
108
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
109 g_free(dns->questions);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
110 g_free(dns->answers);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
111 g_free(dns->authority);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
112 g_free(dns->additional);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
113 g_free(dns);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
114 }
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
115
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
116 /******************************************/
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
117 /* Functions for connection establishment */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
118 /******************************************/
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
119
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
120 int
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
121 mdns_establish_socket()
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
122 {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
123 int fd = -1;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
124 struct sockaddr_in addr;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
125 struct ip_mreq mreq;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
126 unsigned char loop;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
127 unsigned char ttl;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
128 int reuseaddr;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
129
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
130 gaim_debug_info("mdns", "Establishing multicast socket\n");
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
131
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
132 /* What's the difference between AF_INET and PF_INET? */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
133 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
134 gaim_debug_error("mdns", "Unable to create socket: %s\n", strerror(errno));
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
135 return -1;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
136 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
137
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
138 /* Make the socket non-blocking (although it shouldn't matter) */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
139 fcntl(fd, F_SETFL, O_NONBLOCK);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
140
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
141 /* Bind the socket to a local IP and port */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
142 addr.sin_family = AF_INET;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
143 addr.sin_port = htons(5353);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
144 addr.sin_addr.s_addr = INADDR_ANY;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
145 if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
146 gaim_debug_error("mdns", "Unable to bind socket to interface.\n");
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
147 close(fd);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
148 return -1;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
149 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
150
8631
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
151 /* Ensure loopback is enabled (it should be enabled by default, but let's be sure) */
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
152 loop = 1;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
153 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(unsigned char)) == -1) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
154 gaim_debug_error("mdns", "Error calling setsockopt for IP_MULTICAST_LOOP\n");
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
155 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
156
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
157 /* Set TTL to 255--required by mDNS */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
158 ttl = 255;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
159 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(unsigned char)) == -1) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
160 gaim_debug_error("mdns", "Error calling setsockopt for IP_MULTICAST_TTL\n");
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
161 close(fd);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
162 return -1;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
163 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
164
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
165 /* Join the .local multicast group */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
166 mreq.imr_multiaddr.s_addr = inet_addr("224.0.0.251");
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
167 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
168 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(struct ip_mreq)) == -1) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
169 gaim_debug_error("mdns", "Error calling setsockopt for IP_ADD_MEMBERSHIP\n");
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
170 close(fd);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
171 return -1;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
172 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
173
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
174 /* Make the local IP re-usable */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
175 reuseaddr = 1;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
176 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int)) == -1) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
177 gaim_debug_error("mdns", "Error calling setsockopt for SO_REUSEADDR: %s\n", strerror(errno));
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
178 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
179
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
180 return fd;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
181 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
182
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
183 /**
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
184 * Multicast raw data over a file descriptor.
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
185 *
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
186 * @param fd A file descriptor that is a socket bound to a UDP port.
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
187 * @param datalen The length of the data you wish to send.
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
188 * @param data The data you wish to send.
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
189 * @return 0 on success, otherwise return -1.
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
190 */
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
191 static int
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
192 mdns_send_raw(int fd, unsigned int datalen, unsigned char *data)
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
193 {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
194 struct sockaddr_in addr;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
195 int n;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
196
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
197 addr.sin_family = AF_INET;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
198 addr.sin_port = htons(5353);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
199 addr.sin_addr.s_addr = inet_addr("224.0.0.251");
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
200 n = sendto(fd, data, datalen, 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
201
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
202 if (n == -1) {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
203 gaim_debug_error("mdns", "Error sending packet: %d\n", errno);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
204 return -1;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
205 } else if (n != datalen) {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
206 gaim_debug_error("mdns", "Only sent %d of %d bytes of data.\n", n, datalen);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
207 return -1;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
208 }
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
209
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
210 return 0;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
211 }
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
212
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
213 /***************************************/
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
214 /* Functions for sending mDNS messages */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
215 /***************************************/
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
216
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
217 static int
8631
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
218 mdns_getlength_name(const void *name)
8629
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
219 {
8631
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
220 return strlen((const char *)name) + 2;
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
221 }
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
222
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
223 static int
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
224 mdns_getlength_RR_rdata(unsigned short type, const void *rdata)
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
225 {
8629
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
226 int rdlength = 0;
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
227
8631
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
228 switch (type) {
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
229 case RENDEZVOUS_RRTYPE_PTR:
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
230 rdlength = mdns_getlength_name(rdata);
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
231 break;
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
232
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
233 case RENDEZVOUS_RRTYPE_TXT: {
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
234 GSList *cur;
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
235 ResourceRecordRDataTXTNode *node;
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
236
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
237 for (cur = (GSList *)rdata; cur != NULL; cur = cur->next) {
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
238 node = (ResourceRecordRDataTXTNode *)cur->data;
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
239 rdlength += 1 + strlen(node->name);
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
240 if (node->value != NULL)
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
241 rdlength += 1 + strlen(node->value);
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
242 }
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
243 } break;
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
244
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
245 case RENDEZVOUS_RRTYPE_SRV:
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
246 rdlength = 6 + mdns_getlength_name(((const ResourceRecordRDataSRV *)rdata)->target);
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
247 break;
8629
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
248 }
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
249
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
250 return rdlength;
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
251 }
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
252
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
253 static int
8631
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
254 mdns_getlength_RR(ResourceRecord *rr)
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
255 {
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
256 return mdns_getlength_name(rr->name) + 10 + rr->rdlength;
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
257 }
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
258
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
259 static int
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
260 mdns_put_name(char *data, unsigned int datalen, unsigned int offset, const char *name)
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
261 {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
262 int i = 0;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
263 char *b, *c;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
264
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
265 b = (char *)name;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
266 while ((c = strchr(b, '.'))) {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
267 i += util_put8(&data[offset + i], c - b); /* Length of domain-name segment */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
268 memcpy(&data[offset + i], b, c - b); /* Domain-name segment */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
269 i += c - b; /* Increment the destination pointer */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
270 b = c + 1;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
271 }
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
272 i += util_put8(&data[offset + i], strlen(b)); /* Length of domain-name segment */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
273 strcpy(&data[offset + i], b); /* Domain-name segment */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
274 i += strlen(b) + 1; /* Increment the destination pointer */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
275
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
276 return i;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
277 }
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
278
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
279 static int
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
280 mdns_put_RR(char *data, unsigned int datalen, unsigned int offset, const ResourceRecord *rr)
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
281 {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
282 int i = 0;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
283
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
284 i += mdns_put_name(data, datalen, offset + i, rr->name);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
285 i += util_put16(&data[offset + i], rr->type);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
286 i += util_put16(&data[offset + i], rr->class);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
287 i += util_put32(&data[offset + i], rr->ttl);
8631
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
288 i += util_put16(&data[offset + i], rr->rdlength);
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
289
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
290 switch (rr->type) {
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
291 case RENDEZVOUS_RRTYPE_NULL:
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
292 memcpy(&data[offset + i], rr->rdata, rr->rdlength);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
293 i += rr->rdlength;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
294 break;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
295
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
296 case RENDEZVOUS_RRTYPE_PTR:
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
297 i += mdns_put_name(data, datalen, offset + i, (const char *)rr->rdata);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
298 break;
8629
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
299
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
300 case RENDEZVOUS_RRTYPE_TXT: {
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
301 GSList *cur;
8631
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
302 ResourceRecordRDataTXTNode *node;
8629
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
303 int mylength;
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
304
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
305 for (cur = (GSList *)rr->rdata; cur != NULL; cur = cur->next) {
8631
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
306 node = (ResourceRecordRDataTXTNode *)cur->data;
8629
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
307 mylength = 1 + strlen(node->name);
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
308 if (node->value)
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
309 mylength += 1 + strlen(node->value);
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
310 i += util_put8(&data[offset + i], mylength - 1);
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
311 memcpy(&data[offset + i], node->name, strlen(node->name));
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
312 i += strlen(node->name);
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
313 if (node->value) {
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
314 data[offset + i] = '=';
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
315 i++;
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
316 memcpy(&data[offset + i], node->value, strlen(node->value));
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
317 i += strlen(node->value);
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
318 }
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
319 }
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
320 } break;
8631
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
321
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
322 case RENDEZVOUS_RRTYPE_SRV: {
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
323 ResourceRecordRDataSRV *srv = rr->rdata;
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
324 i += util_put16(&data[offset + i], 0);
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
325 i += util_put16(&data[offset + i], 0);
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
326 i += util_put16(&data[offset + i], srv->port);
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
327 i += mdns_put_name(data, datalen, offset + i, srv->target);
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
328 } break;
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
329 }
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
330
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
331 return i;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
332 }
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
333
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
334 int
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
335 mdns_send_dns(int fd, const DNSPacket *dns)
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
336 {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
337 int ret;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
338 unsigned int datalen;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
339 unsigned char *data;
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
340 unsigned int offset;
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
341 int i;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
342
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
343 /* Calculate the length of the buffer we'll need to hold the DNS packet */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
344 datalen = 0;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
345
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
346 /* Header */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
347 datalen += 12;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
348
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
349 /* Questions */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
350 for (i = 0; i < dns->header.numquestions; i++)
8631
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
351 datalen += mdns_getlength_name(dns->questions[i].name) + 4;
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
352
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
353 /* Resource records */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
354 for (i = 0; i < dns->header.numanswers; i++)
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
355 datalen += mdns_getlength_RR(&dns->answers[i]);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
356 for (i = 0; i < dns->header.numauthority; i++)
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
357 datalen += mdns_getlength_RR(&dns->authority[i]);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
358 for (i = 0; i < dns->header.numadditional; i++)
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
359 datalen += mdns_getlength_RR(&dns->additional[i]);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
360
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
361 /* Allocate a buffer */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
362 if (!(data = (unsigned char *)g_malloc(datalen))) {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
363 return -ENOMEM;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
364 }
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
365
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
366 /* Construct the datagram */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
367 /* Header */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
368 offset = 0;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
369 offset += util_put16(&data[offset], dns->header.id); /* ID */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
370 offset += util_put16(&data[offset], dns->header.flags);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
371 offset += util_put16(&data[offset], dns->header.numquestions); /* QDCOUNT */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
372 offset += util_put16(&data[offset], dns->header.numanswers); /* ANCOUNT */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
373 offset += util_put16(&data[offset], dns->header.numauthority); /* NSCOUNT */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
374 offset += util_put16(&data[offset], dns->header.numadditional); /* ARCOUNT */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
375
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
376 /* Questions */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
377 for (i = 0; i < dns->header.numquestions; i++) {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
378 offset += mdns_put_name(data, datalen, offset, dns->questions[i].name); /* QNAME */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
379 offset += util_put16(&data[offset], dns->questions[i].type); /* QTYPE */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
380 offset += util_put16(&data[offset], dns->questions[i].class); /* QCLASS */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
381 }
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
382
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
383 /* Resource records */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
384 for (i = 0; i < dns->header.numanswers; i++)
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
385 offset += mdns_put_RR(data, datalen, offset, &dns->answers[i]);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
386 for (i = 0; i < dns->header.numauthority; i++)
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
387 offset += mdns_put_RR(data, datalen, offset, &dns->authority[i]);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
388 for (i = 0; i < dns->header.numadditional; i++)
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
389 offset += mdns_put_RR(data, datalen, offset, &dns->additional[i]);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
390
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
391 /* Send the datagram */
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
392 /* Offset can be shorter than datalen because of name compression */
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
393 ret = mdns_send_raw(fd, offset, data);
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
394
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
395 g_free(data);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
396
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
397 return ret;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
398 }
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
399
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
400 int
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
401 mdns_query(int fd, const char *domain, unsigned short type)
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
402 {
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
403 int ret;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
404 DNSPacket *dns;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
405
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
406 if (strlen(domain) > 255) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
407 return -EINVAL;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
408 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
409
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
410 dns = (DNSPacket *)g_malloc(sizeof(DNSPacket));
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
411 dns->header.id = 0x0000;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
412 dns->header.flags = 0x0000;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
413 dns->header.numquestions = 0x0001;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
414 dns->header.numanswers = 0x0000;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
415 dns->header.numauthority = 0x0000;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
416 dns->header.numadditional = 0x0000;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
417
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
418 dns->questions = (Question *)g_malloc(1 * sizeof(Question));
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
419 dns->questions[0].name = g_strdup(domain);
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
420 dns->questions[0].type = type;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
421 dns->questions[0].class = 0x0001;
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
422
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
423 dns->answers = NULL;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
424 dns->authority = NULL;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
425 dns->additional = NULL;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
426
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
427 mdns_send_dns(fd, dns);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
428
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
429 mdns_free(dns);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
430
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
431 return ret;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
432 }
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
433
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
434 int
8738
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
435 mdns_send_rr(int fd, ResourceRecord *rr)
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
436 {
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
437 int ret;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
438 DNSPacket *dns;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
439
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
440 dns = (DNSPacket *)g_malloc(sizeof(DNSPacket));
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
441 dns->header.id = 0x0000;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
442 dns->header.flags = 0x8400;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
443 dns->header.numquestions = 0x0000;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
444 dns->header.numanswers = 0x0001;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
445 dns->header.numauthority = 0x0000;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
446 dns->header.numadditional = 0x0000;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
447 dns->questions = NULL;
8738
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
448 dns->answers = rr;
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
449 dns->authority = NULL;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
450 dns->additional = NULL;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
451
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
452 mdns_send_dns(fd, dns);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
453
8738
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
454 /* The rr should be freed by the caller of this function */
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
455 dns->header.numanswers = 0x0000;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
456 dns->answers = NULL;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
457
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
458 mdns_free(dns);
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
459
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
460 return ret;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
461 }
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
462
8629
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
463 int
8738
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
464 mdns_advertise_null(int fd, const char *name, const char *rdata, unsigned short rdlength)
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
465 {
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
466 int ret;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
467 ResourceRecord *rr;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
468
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
469 if ((strlen(name) > 255)) {
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
470 return -EINVAL;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
471 }
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
472
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
473 rr = (ResourceRecord *)g_malloc(sizeof(ResourceRecord));
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
474 rr->name = g_strdup(name);
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
475 rr->type = RENDEZVOUS_RRTYPE_NULL;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
476 rr->class = 0x0001;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
477 rr->ttl = 0x00001c20;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
478 rr->rdlength = rdlength;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
479 rr->rdata = (void *)rdata;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
480
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
481 mdns_send_rr(fd, rr);
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
482
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
483 /* The rdata should be freed by the caller of this function */
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
484 rr->rdata = NULL;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
485
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
486 mdns_free_rr(rr);
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
487
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
488 return ret;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
489 }
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
490
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
491 int
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
492 mdns_advertise_ptr(int fd, const char *name, const char *domain)
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
493 {
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
494 int ret;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
495 ResourceRecord *rr;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
496
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
497 if ((strlen(name) > 255) || (strlen(domain) > 255)) {
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
498 return -EINVAL;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
499 }
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
500
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
501 rr = (ResourceRecord *)g_malloc(sizeof(ResourceRecord));
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
502 rr->name = g_strdup(name);
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
503 rr->type = RENDEZVOUS_RRTYPE_PTR;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
504 rr->class = 0x8001;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
505 rr->ttl = 0x00001c20;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
506 rr->rdata = (void *)g_strdup(domain);
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
507 rr->rdlength = mdns_getlength_RR_rdata(rr->type, rr->rdata);
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
508
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
509 mdns_send_rr(fd, rr);
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
510
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
511 mdns_free_rr(rr);
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
512
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
513 return ret;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
514 }
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
515
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
516 int
8629
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
517 mdns_advertise_txt(int fd, const char *name, const GSList *rdata)
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
518 {
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
519 int ret;
8738
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
520 ResourceRecord *rr;
8629
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
521
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
522 if ((strlen(name) > 255)) {
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
523 return -EINVAL;
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
524 }
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
525
8738
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
526 rr = (ResourceRecord *)g_malloc(sizeof(ResourceRecord));
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
527 rr->name = g_strdup(name);
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
528 rr->type = RENDEZVOUS_RRTYPE_TXT;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
529 rr->class = 0x8001;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
530 rr->ttl = 0x00001c20;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
531 rr->rdata = (void *)rdata;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
532 rr->rdlength = mdns_getlength_RR_rdata(rr->type, rr->rdata);
8629
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
533
8738
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
534 mdns_send_rr(fd, rr);
8629
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
535
8631
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
536 /* The rdata should be freed by the caller of this function */
8738
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
537 rr->rdata = NULL;
8631
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
538
8738
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
539 mdns_free_rr(rr);
8631
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
540
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
541 return ret;
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
542 }
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
543
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
544 int
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
545 mdns_advertise_srv(int fd, const char *name, unsigned short port, const char *target)
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
546 {
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
547 int ret;
8738
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
548 ResourceRecord *rr;
8631
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
549 ResourceRecordRDataSRV *rdata;
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
550
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
551 if ((strlen(target) > 255)) {
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
552 return -EINVAL;
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
553 }
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
554
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
555 rdata = g_malloc(sizeof(ResourceRecordRDataSRV));
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
556 rdata->port = port;
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
557 rdata->target = g_strdup(target);
8631
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
558
8738
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
559 rr = (ResourceRecord *)g_malloc(sizeof(ResourceRecord));
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
560 rr->name = g_strdup(name);
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
561 rr->type = RENDEZVOUS_RRTYPE_SRV;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
562 rr->class = 0x8001;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
563 rr->ttl = 0x00001c20;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
564 rr->rdata = rdata;
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
565 rr->rdlength = mdns_getlength_RR_rdata(rr->type, rr->rdata);
8631
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
566
8738
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
567 mdns_send_rr(fd, rr);
8631
7b8f2818f38a [gaim-migrate @ 9383]
Mark Doliner <mark@kingant.net>
parents: 8629
diff changeset
568
8738
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
569 mdns_free_rr(rr);
8629
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
570
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
571 return ret;
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
572 }
fdff0f31002d [gaim-migrate @ 9381]
Mark Doliner <mark@kingant.net>
parents: 8612
diff changeset
573
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
574 /***************************************/
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
575 /* Functions for parsing mDNS messages */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
576 /***************************************/
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
577
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
578 /*
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
579 * Read in a domain name from the given buffer starting at the given
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
580 * offset. This handles using domain name compression to jump around
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
581 * the data buffer, if needed.
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
582 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
583 * @return A null-terminated string representation of the domain name.
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
584 * This should be g_free'd when no longer needed.
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
585 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
586 static gchar *
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
587 mdns_read_name(const char *data, int datalen, unsigned int offset)
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
588 {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
589 GString *ret = g_string_new("");
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
590 unsigned char tmp, newoffset;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
591
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
592 while ((offset <= datalen) && ((tmp = util_get8(&data[offset])) != 0)) {
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
593 offset++;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
594
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
595 if ((tmp & 0xc0) == 0) { /* First two bits are 00 */
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
596 if (offset + tmp > datalen)
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
597 /* Attempt to read past end of data! Bailing! */
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
598 return g_string_free(ret, TRUE);
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
599 if (*ret->str != '\0')
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
600 g_string_append_c(ret, '.');
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
601 g_string_append_len(ret, &data[offset], tmp);
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
602 offset += tmp;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
603
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
604 } else if ((tmp & 0x40) == 0) { /* First two bits are 10 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
605 /* Reserved for future use */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
606
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
607 } else if ((tmp & 0x80) == 1) { /* First two bits are 01 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
608 /* Reserved for future use */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
609
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
610 } else { /* First two bits are 11 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
611 /* Jump to another position in the data */
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
612 newoffset = util_get8(&data[offset]);
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
613 if (newoffset >= offset)
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
614 /* Invalid pointer! Could lead to infinite recursion! Bailing! */
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
615 return g_string_free(ret, TRUE);
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
616 offset = newoffset;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
617 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
618 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
619
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
620 if (offset > datalen)
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
621 return g_string_free(ret, TRUE);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
622
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
623 return g_string_free(ret, FALSE);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
624 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
625
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
626 /*
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
627 * Determine how many bytes long a portion of the domain name is
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
628 * at the given offset. This does NOT jump around the data array
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
629 * in the case of domain name compression.
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
630 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
631 * @return The length of the portion of the domain name.
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
632 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
633 static int
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
634 mdns_read_name_len(const char *data, unsigned int datalen, unsigned int offset)
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
635 {
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
636 int startoffset = offset;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
637 unsigned char tmp;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
638
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
639 while ((offset <= datalen) && ((tmp = util_get8(&data[offset])) != 0)) {
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
640 offset++;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
641
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
642 if ((tmp & 0xc0) == 0) { /* First two bits are 00 */
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
643 if (offset + tmp > datalen)
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
644 /* Attempt to read past end of data! Bailing! */
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
645 return 0;
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
646 offset += tmp;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
647
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
648 } else if ((tmp & 0x40) == 0) { /* First two bits are 10 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
649 /* Reserved for future use */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
650
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
651 } else if ((tmp & 0x80) == 1) { /* First two bits are 01 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
652 /* Reserved for future use */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
653
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
654 } else { /* First two bits are 11 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
655 /* End of this portion of the domain name */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
656 break;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
657
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
658 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
659 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
660
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
661 return offset - startoffset + 1;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
662 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
663
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
664 /*
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
665 *
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
666 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
667 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
668 static Question *
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
669 mdns_read_questions(int numquestions, const char *data, unsigned int datalen, int *offset)
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
670 {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
671 Question *ret;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
672 int i;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
673
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
674 ret = (Question *)g_malloc0(numquestions * sizeof(Question));
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
675 for (i = 0; i < numquestions; i++) {
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
676 ret[i].name = mdns_read_name(data, datalen, *offset);
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
677 *offset += mdns_read_name_len(data, datalen, *offset);
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
678 if (*offset + 4 > datalen)
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
679 break;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
680 ret[i].type = util_get16(&data[*offset]); /* QTYPE */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
681 *offset += 2;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
682 ret[i].class = util_get16(&data[*offset]); /* QCLASS */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
683 *offset += 2;
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
684 if (*offset > datalen)
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
685 break;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
686 }
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
687
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
688 /* Malformed packet check */
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
689 if (i < numquestions) {
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
690 for (i = 0; i < numquestions; i++)
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
691 g_free(ret[i].name);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
692 g_free(ret);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
693 return NULL;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
694 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
695
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
696 return ret;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
697 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
698
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
699 /*
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
700 * Read in a chunk of data, probably a buddy icon.
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
701 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
702 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
703 static unsigned char *
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
704 mdns_read_rr_rdata_null(const char *data, unsigned int datalen, unsigned int offset, unsigned short rdlength)
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
705 {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
706 unsigned char *ret = NULL;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
707
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
708 if (offset + rdlength > datalen)
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
709 return NULL;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
710
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
711 ret = (unsigned char *)g_malloc(rdlength);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
712 memcpy(ret, &data[offset], rdlength);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
713
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
714 return ret;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
715 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
716
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
717 /*
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
718 * Read in a compressed name.
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
719 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
720 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
721 static char *
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
722 mdns_read_rr_rdata_ptr(const char *data, unsigned int datalen, unsigned int offset)
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
723 {
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
724 return mdns_read_name(data, datalen, offset);
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
725 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
726
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
727 /*
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
728 * Read in a list of name=value pairs into a GHashTable.
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
729 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
730 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
731 static GHashTable *
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
732 mdns_read_rr_rdata_txt(const char *data, unsigned int datalen, unsigned int offset, unsigned short rdlength)
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
733 {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
734 GHashTable *ret = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
735 int endoffset = offset + rdlength;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
736 unsigned char tmp;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
737 char buf[256], *key, *value;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
738
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
739 while (offset < endoffset) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
740 /* Read in the length of the next name/value pair */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
741 tmp = util_get8(&data[offset]);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
742 offset++;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
743
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
744 /* Malformed packet check */
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
745 if (offset + tmp > endoffset)
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
746 break;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
747
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
748 /* Read in the next name/value pair */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
749 strncpy(buf, &data[offset], tmp);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
750 offset += tmp;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
751
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
752 if (buf[0] == '=') {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
753 /* Name/value pairs beginning with = are silently ignored */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
754 continue;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
755 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
756
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
757 /* The value is a substring of buf, starting just after the = */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
758 buf[tmp] = '\0';
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
759 value = strchr(buf, '=');
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
760 if (value != NULL) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
761 value[0] = '\0';
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
762 value++;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
763 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
764
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
765 /* Make the key all lowercase */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
766 key = g_utf8_strdown(buf, -1);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
767 if (!g_hash_table_lookup(ret, key))
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
768 g_hash_table_insert(ret, key, g_strdup(value));
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
769 else
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
770 g_free(key);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
771 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
772
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
773 /* Malformed packet check */
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
774 if ((offset > datalen) || (offset != endoffset)) {
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
775 g_hash_table_destroy(ret);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
776 return NULL;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
777 }
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
778
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
779 return ret;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
780 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
781
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
782 /*
8594
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
783 *
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
784 *
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
785 */
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
786 static ResourceRecordSRV *
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
787 mdns_read_rr_rdata_srv(const char *data, unsigned int datalen, unsigned int offset, unsigned short rdlength)
8594
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
788 {
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
789 ResourceRecordSRV *ret = NULL;
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
790 int endoffset = offset + rdlength;
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
791
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
792 /* Malformed packet check */
8594
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
793 if (offset + 7 > endoffset)
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
794 return NULL;
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
795
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
796 ret = g_malloc(sizeof(ResourceRecordSRV));
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
797
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
798 /* Read in the priority */
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
799 ret->priority = util_get16(&data[offset]);
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
800 offset += 2;
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
801
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
802 /* Read in the weight */
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
803 ret->weight = util_get16(&data[offset]);
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
804 offset += 2;
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
805
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
806 /* Read in the port */
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
807 ret->port = util_get16(&data[offset]);
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
808 offset += 2;
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
809
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
810 /* Read in the target name */
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
811 /*
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
812 * XXX - RFC2782 says it's not supposed to be an alias...
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
813 * but it was in the packet capture I looked at from iChat.
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
814 */
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
815 ret->target = mdns_read_name(data, datalen, offset);
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
816 offset += mdns_read_name_len(data, datalen, offset);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
817
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
818 /* Malformed packet check */
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
819 if ((offset > endoffset) || (ret->target == NULL)) {
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
820 g_free(ret->target);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
821 g_free(ret);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
822 return NULL;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
823 }
8594
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
824
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
825 return ret;
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
826 }
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
827
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
828 /*
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
829 *
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
830 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
831 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
832 static ResourceRecord *
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
833 mdns_read_rr(int numrecords, const char *data, unsigned int datalen, int *offset)
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
834 {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
835 ResourceRecord *ret;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
836 int i;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
837
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
838 ret = (ResourceRecord *)g_malloc0(numrecords * sizeof(ResourceRecord));
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
839 for (i = 0; i < numrecords; i++) {
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
840 /* NAME */
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
841 ret[i].name = mdns_read_name(data, datalen, *offset);
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
842 *offset += mdns_read_name_len(data, datalen, *offset);
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
843
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
844 /* Malformed packet check */
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
845 if (*offset + 10 > datalen)
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
846 break;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
847
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
848 /* TYPE */
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
849 ret[i].type = util_get16(&data[*offset]);
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
850 *offset += 2;
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
851
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
852 /* CLASS */
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
853 ret[i].class = util_get16(&data[*offset]);
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
854 *offset += 2;
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
855
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
856 /* TTL */
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
857 ret[i].ttl = util_get32(&data[*offset]);
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
858 *offset += 4;
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
859
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
860 /* RDLENGTH */
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
861 ret[i].rdlength = util_get16(&data[*offset]);
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
862 *offset += 2;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
863
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
864 /* RDATA */
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
865 if (ret[i].type == RENDEZVOUS_RRTYPE_NULL) {
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
866 ret[i].rdata = mdns_read_rr_rdata_null(data, datalen, *offset, ret[i].rdlength);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
867 if (ret[i].rdata == NULL)
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
868 break;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
869
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
870 } else if (ret[i].type == RENDEZVOUS_RRTYPE_PTR) {
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
871 ret[i].rdata = mdns_read_rr_rdata_ptr(data, datalen, *offset);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
872 if (ret[i].rdata == NULL)
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
873 break;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
874
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
875 } else if (ret[i].type == RENDEZVOUS_RRTYPE_TXT) {
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
876 ret[i].rdata = mdns_read_rr_rdata_txt(data, datalen, *offset, ret[i].rdlength);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
877 if (ret[i].rdata == NULL)
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
878 break;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
879
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
880 } else if (ret[i].type == RENDEZVOUS_RRTYPE_SRV) {
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
881 ret[i].rdata = mdns_read_rr_rdata_srv(data, datalen, *offset, ret[i].rdlength);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
882 if (ret[i].rdata == NULL)
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
883 break;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
884
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
885 }
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
886
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
887 /* Malformed packet check */
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
888 *offset += ret[i].rdlength;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
889 if (*offset > datalen)
8594
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
890 break;
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
891 }
8594
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
892
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
893 /* Malformed packet check */
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
894 if (i < numrecords) {
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
895 for (i = 0; i < numrecords; i++) {
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
896 g_free(ret[i].name);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
897 mdns_free_rr_rdata(ret[i].type, ret[i].rdata);
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
898 }
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
899 g_free(ret);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
900 return NULL;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
901 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
902
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
903 return ret;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
904 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
905
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
906 /*
8679
f39884263db0 [gaim-migrate @ 9432]
Mark Doliner <mark@kingant.net>
parents: 8636
diff changeset
907 * If invalid data is encountered at any point when parsing data
8735
92cbf9713795 [gaim-migrate @ 9490]
Christian Hammond <chipx86@chipx86.com>
parents: 8679
diff changeset
908 * then the entire packet is discarded and NULL is returned.
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
909 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
910 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
911 DNSPacket *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
912 mdns_read(int fd)
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
913 {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
914 DNSPacket *ret = NULL;
8738
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
915 int i;
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
916 int offset; /* Current position in datagram */
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
917 /* XXX - Find out what to use as a maximum incoming UDP packet size */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
918 /* char data[512]; */
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
919 char data[10096];
8634
bcb09cc97b63 [gaim-migrate @ 9386]
Mark Doliner <mark@kingant.net>
parents: 8631
diff changeset
920 unsigned int datalen;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
921 struct sockaddr_in addr;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
922 socklen_t addrlen;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
923
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
924 /* Read in an mDNS packet */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
925 addrlen = sizeof(struct sockaddr_in);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
926 if ((datalen = recvfrom(fd, data, sizeof(data), 0, (struct sockaddr *)&addr, &addrlen)) == -1) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
927 gaim_debug_error("mdns", "Error reading packet: %d\n", errno);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
928 return NULL;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
929 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
930
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
931 ret = (DNSPacket *)g_malloc0(sizeof(DNSPacket));
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
932
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
933 /* Parse the incoming packet, starting from 0 */
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
934 offset = 0;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
935
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
936 if (offset + 12 > datalen) {
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
937 g_free(ret);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
938 return NULL;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
939 }
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
940
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
941 /* The header section */
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
942 ret->header.id = util_get16(&data[offset]); /* ID */
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
943 offset += 2;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
944
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
945 /* For the flags, some bits must be 0 and some must be 1, the rest are ignored */
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
946 ret->header.flags = util_get16(&data[offset]); /* Flags (QR, OPCODE, AA, TC, RD, RA, Z, AD, CD, and RCODE */
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
947 offset += 2;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
948 if ((ret->header.flags & 0x8000) == 0) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
949 /* QR should be 1 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
950 g_free(ret);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
951 return NULL;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
952 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
953 if ((ret->header.flags & 0x7800) != 0) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
954 /* OPCODE should be all 0's */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
955 g_free(ret);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
956 return NULL;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
957 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
958
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
959 /* Read in the number of other things in the packet */
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
960 ret->header.numquestions = util_get16(&data[offset]);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
961 offset += 2;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
962 ret->header.numanswers = util_get16(&data[offset]);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
963 offset += 2;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
964 ret->header.numauthority = util_get16(&data[offset]);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
965 offset += 2;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
966 ret->header.numadditional = util_get16(&data[offset]);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
967 offset += 2;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
968
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
969 /* Read in all the questions */
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
970 ret->questions = mdns_read_questions(ret->header.numquestions, data, datalen, &offset);
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
971
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
972 /* Read in all resource records */
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
973 ret->answers = mdns_read_rr(ret->header.numanswers, data, datalen, &offset);
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
974
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
975 /* Read in all authority records */
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
976 ret->authority = mdns_read_rr(ret->header.numauthority, data, datalen, &offset);
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
977
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
978 /* Read in all additional records */
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
979 ret->additional = mdns_read_rr(ret->header.numadditional, data, datalen, &offset);
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
980
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
981 /* Malformed packet check */
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
982 if ((ret->header.numquestions > 0 && ret->questions == NULL) ||
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
983 (ret->header.numanswers > 0 && ret->answers == NULL) ||
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
984 (ret->header.numauthority > 0 && ret->authority == NULL) ||
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
985 (ret->header.numadditional > 0 && ret->additional == NULL)) {
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
986 gaim_debug_error("mdns", "Received an invalid DNS packet.\n");
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
987 return NULL;
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
988 }
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
989
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
990 /* We should be at the end of the packet */
8636
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
991 if (offset != datalen) {
005c96dab551 [gaim-migrate @ 9388]
Mark Doliner <mark@kingant.net>
parents: 8634
diff changeset
992 gaim_debug_error("mdns", "Finished parsing before end of DNS packet! Only parsed %d of %d bytes.", offset, datalen);
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
993 g_free(ret);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
994 return NULL;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
995 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
996
8738
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
997 #if 0
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
998 for (i = 0; i < ret->header.numanswers; i++)
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
999 mdns_cache_add(&ret->answers[i]);
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
1000 for (i = 0; i < ret->header.numauthority; i++)
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
1001 mdns_cache_add(&ret->authority[i]);
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
1002 for (i = 0; i < ret->header.numadditional; i++)
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
1003 mdns_cache_add(&ret->additional[i]);
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
1004 for (i = 0; i < ret->header.numquestions; i++)
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
1005 mdns_cache_respond(fd, &ret->questions[i]);
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
1006 #endif
dbbf5470ba05 [gaim-migrate @ 9493]
Mark Doliner <mark@kingant.net>
parents: 8735
diff changeset
1007
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
1008 return ret;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
1009 }