annotate src/protocols/rendezvous/mdns.c @ 8612:219e9638e8f3

[gaim-migrate @ 9363] Make sending mDNS datagrams more object oriented. And allow for advertising PTR records. And some other changes. Just wanted to commit this to the public archives in case my computer it seized in a hostile buy-out by kopete. committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Thu, 08 Apr 2004 01:23:49 +0000
parents f3b928825a72
children fdff0f31002d
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 /*
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
33 * XXX - THIS DOESN'T DO BOUNDS CHECKING!!! DON'T USE IT ON AN UNTRUSTED
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
34 * NETWORK UNTIL IT DOES!!! THERE ARE POSSIBLE REMOTE ACCESS VIA BUFFER
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
35 * OVERFLOW SECURITY HOLES!!!
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
36 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
37
8546
9aa6017da568 [gaim-migrate @ 9288]
Mark Doliner <mark@kingant.net>
parents: 8487
diff changeset
38 #include "internal.h"
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
39 #include "debug.h"
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
40
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
41 #include "mdns.h"
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
42 #include "util.h"
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
43
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
44 /******************************************/
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
45 /* Functions for connection establishment */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
46 /******************************************/
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
47
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
48 int
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
49 mdns_establish_socket()
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
50 {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
51 int fd = -1;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
52 struct sockaddr_in addr;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
53 struct ip_mreq mreq;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
54 unsigned char loop;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
55 unsigned char ttl;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
56 int reuseaddr;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
57
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
58 gaim_debug_info("mdns", "Establishing multicast socket\n");
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
59
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
60 /* What's the difference between AF_INET and PF_INET? */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
61 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
62 gaim_debug_error("mdns", "Unable to create socket: %s\n", strerror(errno));
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
63 return -1;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
64 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
65
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
66 /* Make the socket non-blocking (although it shouldn't matter) */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
67 fcntl(fd, F_SETFL, O_NONBLOCK);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
68
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
69 /* Bind the socket to a local IP and port */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
70 addr.sin_family = AF_INET;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
71 addr.sin_port = htons(5353);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
72 addr.sin_addr.s_addr = INADDR_ANY;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
73 if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
74 gaim_debug_error("mdns", "Unable to bind socket to interface.\n");
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
75 close(fd);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
76 return -1;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
77 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
78
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
79 /* Ensure loopback is enabled (it should be enabled by default, by let's be sure) */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
80 loop = 1;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
81 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
82 gaim_debug_error("mdns", "Error calling setsockopt for IP_MULTICAST_LOOP\n");
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
83 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
84
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
85 /* Set TTL to 255--required by mDNS */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
86 ttl = 255;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
87 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
88 gaim_debug_error("mdns", "Error calling setsockopt for IP_MULTICAST_TTL\n");
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
89 close(fd);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
90 return -1;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
91 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
92
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
93 /* Join the .local multicast group */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
94 mreq.imr_multiaddr.s_addr = inet_addr("224.0.0.251");
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
95 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
96 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
97 gaim_debug_error("mdns", "Error calling setsockopt for IP_ADD_MEMBERSHIP\n");
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
98 close(fd);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
99 return -1;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
100 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
101
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
102 /* Make the local IP re-usable */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
103 reuseaddr = 1;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
104 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int)) == -1) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
105 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
106 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
107
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
108 return fd;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
109 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
110
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
111 static int
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
112 mdns_send_raw(int fd, unsigned int datalen, unsigned char *data)
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
113 {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
114 struct sockaddr_in addr;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
115 int n;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
116
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
117 addr.sin_family = AF_INET;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
118 addr.sin_port = htons(5353);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
119 addr.sin_addr.s_addr = inet_addr("224.0.0.251");
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
120 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
121
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
122 if (n == -1) {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
123 gaim_debug_error("mdns", "Error sending packet: %d\n", errno);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
124 return -1;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
125 } else if (n != datalen) {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
126 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
127 return -1;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
128 }
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
129
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
130 return 0;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
131 }
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
132
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
133 /***************************************/
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
134 /* Functions for sending mDNS messages */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
135 /***************************************/
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
136
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
137 static int
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
138 mdns_getlength_RR(const ResourceRecord *rr)
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
139 {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
140 int ret = 0;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
141
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
142 ret += strlen(rr->name) + 2;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
143 ret += 10;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
144
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
145 switch (rr->type) {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
146 case RENDEZVOUS_RRTYPE_PTR:
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
147 ret += strlen((const char *)rr->rdata) + 2;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
148 break;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
149 }
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
150
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
151 return ret;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
152 }
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
153
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
154 static int
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
155 mdns_put_name(char *data, int datalen, int offset, const char *name)
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
156 {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
157 int i = 0;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
158 char *b, *c;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
159
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
160 b = (char *)name;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
161 while ((c = strchr(b, '.'))) {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
162 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
163 memcpy(&data[offset + i], b, c - b); /* Domain-name segment */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
164 i += c - b; /* Increment the destination pointer */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
165 b = c + 1;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
166 }
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
167 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
168 strcpy(&data[offset + i], b); /* Domain-name segment */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
169 i += strlen(b) + 1; /* Increment the destination pointer */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
170
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
171 return i;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
172 }
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
173
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
174 static int
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
175 mdns_put_RR(char *data, int datalen, int offset, const ResourceRecord *rr)
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
176 {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
177 int i = 0;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
178
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
179 i += mdns_put_name(data, datalen, offset + i, rr->name);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
180 i += util_put16(&data[offset + i], rr->type);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
181 i += util_put16(&data[offset + i], rr->class);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
182 i += util_put32(&data[offset + i], rr->ttl);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
183 i += util_put16(&data[offset + i], rr->rdlength);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
184
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
185 switch (rr->type) {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
186 case RENDEZVOUS_RRTYPE_PTR:
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
187 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
188 break;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
189 }
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
190
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
191 return i;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
192 }
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 int
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
195 mdns_send_dns(int fd, const DNSPacket *dns)
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 int ret;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
198 unsigned int datalen;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
199 unsigned char *data;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
200 int offset;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
201 int i;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
202
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
203 /* 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
204 datalen = 0;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
205
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
206 /* Header */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
207 datalen += 12;
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 /* Questions */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
210 for (i = 0; i < dns->header.numquestions; i++)
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
211 datalen += strlen(dns->questions[i].name) + 2 + 4;
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 /* Resource records */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
214 for (i = 0; i < dns->header.numanswers; i++)
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
215 datalen += mdns_getlength_RR(&dns->answers[i]);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
216 for (i = 0; i < dns->header.numauthority; i++)
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
217 datalen += mdns_getlength_RR(&dns->authority[i]);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
218 for (i = 0; i < dns->header.numadditional; i++)
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
219 datalen += mdns_getlength_RR(&dns->additional[i]);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
220
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
221 /* Allocate a buffer */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
222 if (!(data = (unsigned char *)g_malloc(datalen))) {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
223 return -ENOMEM;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
224 }
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
225
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
226 /* Construct the datagram */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
227 /* Header */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
228 offset = 0;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
229 offset += util_put16(&data[offset], dns->header.id); /* ID */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
230 offset += util_put16(&data[offset], dns->header.flags);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
231 offset += util_put16(&data[offset], dns->header.numquestions); /* QDCOUNT */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
232 offset += util_put16(&data[offset], dns->header.numanswers); /* ANCOUNT */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
233 offset += util_put16(&data[offset], dns->header.numauthority); /* NSCOUNT */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
234 offset += util_put16(&data[offset], dns->header.numadditional); /* ARCOUNT */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
235
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
236 /* Questions */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
237 for (i = 0; i < dns->header.numquestions; i++) {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
238 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
239 offset += util_put16(&data[offset], dns->questions[i].type); /* QTYPE */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
240 offset += util_put16(&data[offset], dns->questions[i].class); /* QCLASS */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
241 }
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
242
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
243 /* Resource records */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
244 for (i = 0; i < dns->header.numanswers; i++)
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
245 offset += mdns_put_RR(data, datalen, offset, &dns->answers[i]);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
246 for (i = 0; i < dns->header.numauthority; i++)
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
247 offset += mdns_put_RR(data, datalen, offset, &dns->authority[i]);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
248 for (i = 0; i < dns->header.numadditional; i++)
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
249 offset += mdns_put_RR(data, datalen, offset, &dns->additional[i]);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
250
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
251 /* Send the datagram */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
252 ret = mdns_send_raw(fd, datalen, data);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
253
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
254 g_free(data);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
255
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
256 return ret;
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
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
259 int
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
260 mdns_query(int fd, const char *domain)
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
261 {
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
262 int ret;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
263 DNSPacket *dns;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
264
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
265 if (strlen(domain) > 255) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
266 return -EINVAL;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
267 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
268
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
269 dns = (DNSPacket *)g_malloc(sizeof(DNSPacket));
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
270 dns->header.id = 0x0000;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
271 dns->header.flags = 0x0000;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
272 dns->header.numquestions = 0x0001;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
273 dns->header.numanswers = 0x0000;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
274 dns->header.numauthority = 0x0000;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
275 dns->header.numadditional = 0x0000;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
276
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
277 dns->questions = (Question *)g_malloc(1 * sizeof(Question));
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
278 dns->questions[0].name = g_strdup(domain);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
279 dns->questions[0].type = RENDEZVOUS_RRTYPE_PTR;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
280 dns->questions[0].class = 0x8001;
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 dns->answers = NULL;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
283 dns->authority = NULL;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
284 dns->additional = NULL;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
285
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
286 mdns_send_dns(fd, dns);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
287
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
288 mdns_free(dns);
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 return ret;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
291 }
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
292
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
293 int
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
294 mdns_advertise_ptr(int fd, const char *name, const char *domain)
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
295 {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
296 int ret;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
297 DNSPacket *dns;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
298
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
299 if ((strlen(name) > 255) || (strlen(domain) > 255)) {
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
300 return -EINVAL;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
301 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
302
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
303 dns = (DNSPacket *)g_malloc(sizeof(DNSPacket));
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
304 dns->header.id = 0x0000;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
305 dns->header.flags = 0x8400;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
306 dns->header.numquestions = 0x0000;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
307 dns->header.numanswers = 0x0001;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
308 dns->header.numauthority = 0x0000;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
309 dns->header.numadditional = 0x0000;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
310 dns->questions = NULL;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
311
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
312 dns->answers = (ResourceRecord *)g_malloc(1 * sizeof(ResourceRecord));
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
313 dns->answers[0].name = g_strdup(name);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
314 dns->answers[0].type = RENDEZVOUS_RRTYPE_PTR;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
315 dns->answers[0].class = 0x0001;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
316 dns->answers[0].ttl = 0x00001c20;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
317 dns->answers[0].rdlength = strlen(domain) + 2;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
318 dns->answers[0].rdata = (void *)g_strdup(domain);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
319
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
320 dns->authority = NULL;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
321 dns->additional = NULL;
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
322
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
323 mdns_send_dns(fd, dns);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
324
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
325 mdns_free(dns);
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
326
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
327 return ret;
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
328 }
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
329
8612
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 /* Functions for parsing mDNS messages */
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
332 /***************************************/
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
333
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
334 /*
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
335 * XXX - Needs bounds checking!
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
336 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
337 * 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
338 * offset. This handles using domain name compression to jump around
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
339 * the data buffer, if needed.
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
340 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
341 * @return A null-terminated string representation of the domain name.
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
342 * This should be g_free'd when no longer needed.
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
343 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
344 static gchar *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
345 mdns_read_name(const char *data, int datalen, int dataoffset)
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
346 {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
347 GString *ret = g_string_new("");
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
348 unsigned char tmp;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
349
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
350 while ((tmp = util_get8(&data[dataoffset])) != 0) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
351 dataoffset++;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
352
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
353 if ((tmp & 0xc0) == 0) { /* First two bits are 00 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
354 if (*ret->str)
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
355 g_string_append_c(ret, '.');
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
356 g_string_append_len(ret, &data[dataoffset], tmp);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
357 dataoffset += tmp;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
358
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
359 } else if ((tmp & 0x40) == 0) { /* First two bits are 10 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
360 /* Reserved for future use */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
361
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
362 } else if ((tmp & 0x80) == 1) { /* First two bits are 01 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
363 /* Reserved for future use */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
364
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
365 } else { /* First two bits are 11 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
366 /* Jump to another position in the data */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
367 dataoffset = util_get8(&data[dataoffset]);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
368
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
369 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
370 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
371
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
372 return g_string_free(ret, FALSE);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
373 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
374
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
375 /*
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
376 * XXX - Needs bounds checking!
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
377 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
378 * Determine how many bytes long a portion of the domain name is
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
379 * at the given offset. This does NOT jump around the data array
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
380 * in the case of domain name compression.
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
381 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
382 * @return The length of the portion of the domain name.
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
383 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
384 static int
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
385 mdns_read_name_len(const char *data, int datalen, int dataoffset)
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
386 {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
387 int startoffset = dataoffset;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
388 unsigned char tmp;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
389
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
390 while ((tmp = util_get8(&data[dataoffset++])) != 0) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
391
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
392 if ((tmp & 0xc0) == 0) { /* First two bits are 00 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
393 dataoffset += tmp;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
394
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
395 } else if ((tmp & 0x40) == 0) { /* First two bits are 10 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
396 /* Reserved for future use */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
397
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
398 } else if ((tmp & 0x80) == 1) { /* First two bits are 01 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
399 /* Reserved for future use */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
400
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
401 } else { /* First two bits are 11 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
402 /* End of this portion of the domain name */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
403 dataoffset++;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
404 break;
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 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
407 }
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 return dataoffset - startoffset;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
410 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
411
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
412 /*
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
413 * XXX - Needs bounds checking!
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
414 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
415 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
416 static Question *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
417 mdns_read_questions(int numquestions, const char *data, int datalen, int *offset)
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
418 {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
419 Question *ret;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
420 int i;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
421
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
422 ret = (Question *)g_malloc0(numquestions * sizeof(Question));
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
423 for (i = 0; i < numquestions; i++) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
424 ret[i].name = mdns_read_name(data, 0, *offset);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
425 *offset += mdns_read_name_len(data, 0, *offset);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
426 ret[i].type = util_get16(&data[*offset]); /* QTYPE */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
427 *offset += 2;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
428 ret[i].class = util_get16(&data[*offset]); /* QCLASS */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
429 *offset += 2;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
430 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
431
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
432 return ret;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
433 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
434
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
435 /*
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
436 * Read in a chunk of data, probably a buddy icon.
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
437 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
438 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
439 static unsigned char *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
440 mdns_read_rr_rdata_null(const char *data, int datalen, int offset, unsigned short rdlength)
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
441 {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
442 unsigned char *ret = NULL;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
443
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
444 if (offset + rdlength > datalen)
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
445 return NULL;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
446
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
447 ret = (unsigned char *)g_malloc(rdlength);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
448 memcpy(ret, &data[offset], rdlength);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
449
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
450 return ret;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
451 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
452
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
453 /*
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
454 * XXX - Needs bounds checking!
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
455 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
456 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
457 static char *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
458 mdns_read_rr_rdata_ptr(const char *data, int datalen, int offset)
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
459 {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
460 char *ret = NULL;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
461
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
462 ret = mdns_read_name(data, datalen, offset);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
463
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
464 return ret;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
465 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
466
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
467 /*
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
468 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
469 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
470 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
471 static GHashTable *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
472 mdns_read_rr_rdata_txt(const char *data, int datalen, int offset, unsigned short rdlength)
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
473 {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
474 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
475 int endoffset = offset + rdlength;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
476 unsigned char tmp;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
477 char buf[256], *key, *value;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
478
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
479 while (offset < endoffset) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
480 /* Read in the length of the next name/value pair */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
481 tmp = util_get8(&data[offset]);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
482 offset++;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
483
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
484 /* Ensure packet is valid */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
485 if (offset + tmp > endoffset)
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
486 break;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
488 /* Read in the next name/value pair */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
489 strncpy(buf, &data[offset], tmp);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
490 offset += tmp;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
491
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
492 if (buf[0] == '=') {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
493 /* Name/value pairs beginning with = are silently ignored */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
494 continue;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
495 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
496
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
497 /* The value is a substring of buf, starting just after the = */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
498 buf[tmp] = '\0';
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
499 value = strchr(buf, '=');
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
500 if (value != NULL) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
501 value[0] = '\0';
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
502 value++;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
503 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
504
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
505 /* Make the key all lowercase */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
506 key = g_utf8_strdown(buf, -1);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
507 if (!g_hash_table_lookup(ret, key))
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
508 g_hash_table_insert(ret, key, g_strdup(value));
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
509 else
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
510 g_free(key);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
511 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
512
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
513 return ret;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
514 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
515
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
516 /*
8594
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
517 *
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
518 *
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
519 */
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
520 static ResourceRecordSRV *
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
521 mdns_read_rr_rdata_srv(const char *data, int datalen, int offset, unsigned short rdlength)
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
522 {
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
523 ResourceRecordSRV *ret = NULL;
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
524 int endoffset = offset + rdlength;
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
525
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
526 if (offset + 7 > endoffset)
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
527 return NULL;
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
528
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
529 ret = g_malloc(sizeof(ResourceRecordSRV));
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
530
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
531 /* Read in the priority */
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
532 ret->priority = util_get16(&data[offset]);
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
533 offset += 2;
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
534
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
535 /* Read in the weight */
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
536 ret->weight = util_get16(&data[offset]);
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
537 offset += 2;
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
538
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
539 /* Read in the port */
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
540 ret->port = util_get16(&data[offset]);
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
541 offset += 2;
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
542
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
543 /* Read in the target name */
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
544 /*
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
545 * XXX - RFC2782 says it's not supposed to be an alias...
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
546 * 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
547 */
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
548 ret->target = mdns_read_name(data, datalen, offset);
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
549
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
550 return ret;
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
551 }
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
552
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
553 /*
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
554 * XXX - Needs bounds checking!
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
555 *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
556 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
557 static ResourceRecord *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
558 mdns_read_rr(int numrecords, const char *data, int datalen, int *offset)
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
559 {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
560 ResourceRecord *ret;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
561 int i;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
562
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
563 ret = (ResourceRecord *)g_malloc0(numrecords * sizeof(ResourceRecord));
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
564 for (i = 0; i < numrecords; i++) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
565 ret[i].name = mdns_read_name(data, 0, *offset); /* NAME */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
566 *offset += mdns_read_name_len(data, 0, *offset);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
567 ret[i].type = util_get16(&data[*offset]); /* TYPE */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
568 *offset += 2;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
569 ret[i].class = util_get16(&data[*offset]); /* CLASS */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
570 *offset += 2;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
571 ret[i].ttl = util_get32(&data[*offset]); /* TTL */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
572 *offset += 4;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
573 ret[i].rdlength = util_get16(&data[*offset]); /* RDLENGTH */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
574 *offset += 2;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
575
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
576 /* RDATA */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
577 switch (ret[i].type) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
578 case RENDEZVOUS_RRTYPE_NULL:
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
579 ret[i].rdata = mdns_read_rr_rdata_null(data, datalen, *offset, ret[i].rdlength);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
580 break;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
581
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
582 case RENDEZVOUS_RRTYPE_PTR:
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
583 ret[i].rdata = mdns_read_rr_rdata_ptr(data, datalen, *offset);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
584 break;
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 case RENDEZVOUS_RRTYPE_TXT:
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
587 ret[i].rdata = mdns_read_rr_rdata_txt(data, datalen, *offset, ret[i].rdlength);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
588 break;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
589
8594
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
590 case RENDEZVOUS_RRTYPE_SRV:
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
591 ret[i].rdata = mdns_read_rr_rdata_srv(data, datalen, *offset, ret[i].rdlength);
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
592 break;
f3b928825a72 [gaim-migrate @ 9345]
Mark Doliner <mark@kingant.net>
parents: 8546
diff changeset
593
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
594 default:
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
595 ret[i].rdata = NULL;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
596 break;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
597 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
598 *offset += ret[i].rdlength;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
599 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
600
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
601 return ret;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
602 }
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 /*
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
605 * XXX - Needs bounds checking!
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 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
608 DNSPacket *
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
609 mdns_read(int fd)
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
610 {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
611 DNSPacket *ret = NULL;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
612 int i; /* Current position in datagram */
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
613 /* 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
614 /* char data[512]; */
8487
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
615 char data[10096];
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
616 int datalen;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
617 struct sockaddr_in addr;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
618 socklen_t addrlen;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
619
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
620 /* Read in an mDNS packet */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
621 addrlen = sizeof(struct sockaddr_in);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
622 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
623 gaim_debug_error("mdns", "Error reading packet: %d\n", errno);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
624 return NULL;
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 ret = (DNSPacket *)g_malloc0(sizeof(DNSPacket));
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
628
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
629 /* Parse the incoming packet, starting from 0 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
630 i = 0;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
631
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
632 /* The header section */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
633 ret->header.id = util_get16(&data[i]); /* ID */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
634 i += 2;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
635
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
636 /* For the flags, some bits must be 0 and some must be 1, the rest are ignored */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
637 ret->header.flags = util_get16(&data[i]); /* Flags (QR, OPCODE, AA, TC, RD, RA, Z, AD, CD, and RCODE */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
638 i += 2;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
639 if ((ret->header.flags & 0x8000) == 0) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
640 /* QR should be 1 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
641 g_free(ret);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
642 return NULL;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
643 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
644 if ((ret->header.flags & 0x7800) != 0) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
645 /* OPCODE should be all 0's */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
646 g_free(ret);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
647 return NULL;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
648 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
649
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
650 /* Read in the number of other things in the packet */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
651 ret->header.numquestions = util_get16(&data[i]);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
652 i += 2;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
653 ret->header.numanswers = util_get16(&data[i]);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
654 i += 2;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
655 ret->header.numauthority = util_get16(&data[i]);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
656 i += 2;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
657 ret->header.numadditional = util_get16(&data[i]);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
658 i += 2;
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 /* Read in all the questions */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
661 ret->questions = mdns_read_questions(ret->header.numquestions, data, datalen, &i);
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 /* Read in all resource records */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
664 ret->answers = mdns_read_rr(ret->header.numanswers, data, datalen, &i);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
665
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
666 /* Read in all authority records */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
667 ret->authority = mdns_read_rr(ret->header.numauthority, data, datalen, &i);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
668
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
669 /* Read in all additional records */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
670 ret->additional = mdns_read_rr(ret->header.numadditional, data, datalen, &i);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
671
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
672 /* We should be at the end of the packet */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
673 if (i != datalen) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
674 gaim_debug_error("mdns", "Finished parsing before end of DNS packet! Only parsed %d of %d bytes.", i, datalen);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
675 g_free(ret);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
676 return NULL;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
677 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
678
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
679 return ret;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
680 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
681
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
682 /**
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
683 * Free the rdata associated with a given resource record.
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
684 */
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
685 static void
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
686 mdns_free_rr_rdata(unsigned short type, void *rdata)
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
687 {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
688 switch (type) {
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
689 case RENDEZVOUS_RRTYPE_NULL:
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
690 case RENDEZVOUS_RRTYPE_PTR:
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
691 g_free(rdata);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
692 break;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
693
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
694 case RENDEZVOUS_RRTYPE_TXT:
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
695 g_hash_table_destroy(rdata);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
696 break;
8612
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
697
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
698 case RENDEZVOUS_RRTYPE_SRV:
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
699 g_free(((ResourceRecordSRV *)rdata)->target);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
700 g_free(rdata);
219e9638e8f3 [gaim-migrate @ 9363]
Mark Doliner <mark@kingant.net>
parents: 8594
diff changeset
701 break;
8487
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 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
704
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 * Free a given question
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 static void
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
709 mdns_free_q(Question *q)
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 g_free(q->name);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
712 }
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 /**
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
715 * Free a given resource record.
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 static void
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
718 mdns_free_rr(ResourceRecord *rr)
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 g_free(rr->name);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
721 mdns_free_rr_rdata(rr->type, rr->rdata);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
722 }
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
723
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
724 void
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
725 mdns_free(DNSPacket *dns)
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 int i;
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
728
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
729 for (i = 0; i < dns->header.numquestions; i++)
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
730 mdns_free_q(&dns->questions[i]);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
731 for (i = 0; i < dns->header.numanswers; i++)
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
732 mdns_free_rr(&dns->answers[i]);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
733 for (i = 0; i < dns->header.numauthority; i++)
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
734 mdns_free_rr(&dns->authority[i]);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
735 for (i = 0; i < dns->header.numadditional; i++)
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
736 mdns_free_rr(&dns->additional[i]);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
737
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
738 g_free(dns->questions);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
739 g_free(dns->answers);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
740 g_free(dns->authority);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
741 g_free(dns->additional);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
742 g_free(dns);
c3ffec7fab94 [gaim-migrate @ 9222]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
743 }