Mercurial > pidgin
annotate src/protocols/bonjour/dns_sd.c @ 13906:b986b6e2441b
[gaim-migrate @ 16400]
part of sf patch #1490646, from Jonty Wareing & Jono Cole
"The user can now specify their AIM and Jabber ID in the
preferences"
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Sun, 02 Jul 2006 21:52:06 +0000 |
parents | b10030f6eab7 |
children | 8264f52a1142 |
rev | line source |
---|---|
11477 | 1 /* |
2 * This program is free software; you can redistribute it and/or modify | |
3 * it under the terms of the GNU General Public License as published by | |
4 * the Free Software Foundation; either version 2 of the License, or | |
5 * (at your option) any later version. | |
6 * | |
7 * This program is distributed in the hope that it will be useful, | |
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 * GNU Library General Public License for more details. | |
11 * | |
12 * You should have received a copy of the GNU General Public License | |
13 * along with this program; if not, write to the Free Software | |
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
15 */ | |
16 | |
17 #include <string.h> | |
18 | |
19 #include "dns_sd.h" | |
20 #include "bonjour.h" | |
21 #include "buddy.h" | |
22 #include "debug.h" | |
23 | |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
24 /* Private data */ |
11477 | 25 |
11539 | 26 typedef struct _dns_sd_packet |
27 { | |
28 gchar *name; | |
29 gchar *txtvers; | |
30 gchar *version; | |
31 gchar *first; | |
32 gchar *last; | |
11477 | 33 gint port_p2pj; |
11539 | 34 gchar *phsh; |
35 gchar *status; | |
36 gchar *message; | |
37 gchar *email; | |
38 gchar *vc; | |
39 gchar *jid; | |
40 gchar *AIM; | |
41 } dns_sd_packet; | |
11477 | 42 |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
43 /* End private data */ |
11477 | 44 |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
45 /* Private functions */ |
11477 | 46 |
11539 | 47 static sw_result HOWL_API |
48 _publish_reply(sw_discovery discovery, sw_discovery_oid oid, | |
49 sw_discovery_publish_status status, sw_opaque extra) | |
11477 | 50 { |
11539 | 51 gaim_debug_warning("bonjour", "_publish_reply --> Start\n"); |
52 | |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
53 /* Check the answer from the mDNS daemon */ |
11539 | 54 switch (status) |
55 { | |
11477 | 56 case SW_DISCOVERY_PUBLISH_STARTED : |
57 gaim_debug_info("bonjour", "_publish_reply --> Service started\n"); | |
58 break; | |
59 case SW_DISCOVERY_PUBLISH_STOPPED : | |
60 gaim_debug_info("bonjour", "_publish_reply --> Service stopped\n"); | |
61 break; | |
62 case SW_DISCOVERY_PUBLISH_NAME_COLLISION : | |
63 gaim_debug_info("bonjour", "_publish_reply --> Name collision\n"); | |
64 break; | |
65 case SW_DISCOVERY_PUBLISH_INVALID : | |
66 gaim_debug_info("bonjour", "_publish_reply --> Service invalid\n"); | |
67 break; | |
68 } | |
11539 | 69 |
11477 | 70 return SW_OKAY; |
71 } | |
72 | |
11539 | 73 static sw_result HOWL_API |
74 _resolve_reply(sw_discovery discovery, sw_discovery_oid oid, | |
75 sw_uint32 interface_index, sw_const_string name, | |
76 sw_const_string type, sw_const_string domain, | |
77 sw_ipv4_address address, sw_port port, | |
78 sw_octets text_record, sw_ulong text_record_len, | |
79 sw_opaque extra) | |
11477 | 80 { |
11539 | 81 BonjourBuddy *buddy; |
82 GaimAccount *account = (GaimAccount*)extra; | |
83 gchar *txtvers = NULL; | |
84 gchar *version = NULL; | |
85 gchar *first = NULL; | |
11477 | 86 gint port_p2pj = -1; |
11539 | 87 gchar *phsh = NULL; |
88 gchar *status = NULL; | |
89 gchar *email = NULL; | |
90 gchar *last = NULL; | |
91 gchar *jid = NULL; | |
92 gchar *AIM = NULL; | |
93 gchar *vc = NULL; | |
94 gchar *msg = NULL; | |
11477 | 95 gint address_length = 16; |
11539 | 96 gchar *ip = NULL; |
11477 | 97 sw_text_record_iterator iterator; |
11498 | 98 char key[SW_TEXT_RECORD_MAX_LEN]; |
99 char value[SW_TEXT_RECORD_MAX_LEN]; | |
11477 | 100 sw_uint32 value_length; |
101 | |
102 sw_discovery_cancel(discovery, oid); | |
11539 | 103 |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
104 /* Get the ip as a string */ |
11477 | 105 ip = malloc(address_length); |
106 sw_ipv4_address_name(address, ip, address_length); | |
107 | |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
108 /* Obtain the parameters from the text_record */ |
11539 | 109 if ((text_record_len > 0) && (text_record) && (*text_record != '\0')) |
110 { | |
11477 | 111 sw_text_record_iterator_init(&iterator, text_record, text_record_len); |
11539 | 112 while (sw_text_record_iterator_next(iterator, key, (sw_octet *)value, &value_length) == SW_OKAY) |
113 { | |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
114 /* Compare the keys with the possible ones and save them on */ |
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
115 /* the appropiate place of the buddy_list */ |
11477 | 116 if (strcmp(key, "txtvers") == 0) { |
117 txtvers = g_strdup(value); | |
118 } else if (strcmp(key, "version") == 0) { | |
119 version = g_strdup(value); | |
120 } else if (strcmp(key, "1st") == 0) { | |
121 first = g_strdup(value); | |
122 } else if (strcmp(key, "port.p2pj") == 0) { | |
123 port_p2pj = atoi(value); | |
124 } else if (strcmp(key, "status") == 0) { | |
125 status = g_strdup(value); | |
126 } else if (strcmp(key, "email") == 0) { | |
127 email = g_strdup(value); | |
128 } else if (strcmp(key, "last") == 0) { | |
129 last = g_strdup(value); | |
130 } else if (strcmp(key, "jid") == 0) { | |
131 jid = g_strdup(value); | |
132 } else if (strcmp(key, "AIM") == 0) { | |
133 AIM = g_strdup(value); | |
134 } else if (strcmp(key, "vc") == 0) { | |
135 vc = g_strdup(value); | |
136 } else if (strcmp(key, "phsh") == 0) { | |
137 phsh = g_strdup(value); | |
138 } else if (strcmp(key, "msg") == 0) { | |
139 msg = g_strdup(value); | |
140 } | |
141 } | |
142 } | |
143 | |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
144 /* Put the parameters of the text_record in a buddy and add the buddy to */ |
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
145 /* the buddy list */ |
13649 | 146 buddy = bonjour_buddy_new(name, first, port_p2pj, phsh, |
11539 | 147 status, email, last, jid, AIM, vc, ip, msg); |
148 | |
149 if (bonjour_buddy_check(buddy) == FALSE) | |
150 { | |
13649 | 151 bonjour_buddy_delete(buddy); |
11477 | 152 return SW_DISCOVERY_E_UNKNOWN; |
153 } | |
11539 | 154 |
11496 | 155 /* Add or update the buddy in our buddy list */ |
156 bonjour_buddy_add_to_gaim(account, buddy); | |
11539 | 157 |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
158 /* Free all the temporal strings */ |
11477 | 159 g_free(txtvers); |
160 g_free(version); | |
161 g_free(first); | |
162 g_free(last); | |
163 g_free(status); | |
164 g_free(email); | |
165 g_free(jid); | |
166 g_free(AIM); | |
167 g_free(vc); | |
168 g_free(phsh); | |
169 g_free(msg); | |
11539 | 170 |
11477 | 171 return SW_OKAY; |
172 } | |
173 | |
11539 | 174 static sw_result HOWL_API |
175 _browser_reply(sw_discovery discovery, sw_discovery_oid oid, | |
176 sw_discovery_browse_status status, | |
177 sw_uint32 interface_index, sw_const_string name, | |
178 sw_const_string type, sw_const_string domain, | |
179 sw_opaque_t extra) | |
11477 | 180 { |
181 sw_discovery_resolve_id rid; | |
11539 | 182 GaimAccount *account = (GaimAccount*)extra; |
183 GaimBuddy *gb = NULL; | |
184 | |
185 switch (status) | |
186 { | |
11477 | 187 case SW_DISCOVERY_BROWSE_INVALID: |
188 gaim_debug_warning("bonjour", "_browser_reply --> Invalid\n"); | |
189 break; | |
190 case SW_DISCOVERY_BROWSE_RELEASE: | |
191 gaim_debug_warning("bonjour", "_browser_reply --> Release\n"); | |
192 break; | |
193 case SW_DISCOVERY_BROWSE_ADD_DOMAIN: | |
194 gaim_debug_warning("bonjour", "_browser_reply --> Add domain\n"); | |
195 break; | |
196 case SW_DISCOVERY_BROWSE_ADD_DEFAULT_DOMAIN: | |
197 gaim_debug_warning("bonjour", "_browser_reply --> Add default domain\n"); | |
198 break; | |
199 case SW_DISCOVERY_BROWSE_REMOVE_DOMAIN: | |
200 gaim_debug_warning("bonjour", "_browser_reply --> Remove domain\n"); | |
201 break; | |
202 case SW_DISCOVERY_BROWSE_ADD_SERVICE: | |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
203 /* A new peer has join the network and uses iChat bonjour */ |
11477 | 204 gaim_debug_info("bonjour", "_browser_reply --> Add service\n"); |
11539 | 205 if (g_ascii_strcasecmp(name, account->username) != 0) |
206 { | |
207 if (sw_discovery_resolve(discovery, interface_index, name, type, | |
208 domain, _resolve_reply, extra, &rid) != SW_OKAY) | |
209 { | |
11477 | 210 gaim_debug_warning("bonjour", "_browser_reply --> Cannot send resolve\n"); |
211 } | |
212 } | |
213 break; | |
214 case SW_DISCOVERY_BROWSE_REMOVE_SERVICE: | |
215 gaim_debug_info("bonjour", "_browser_reply --> Remove service\n"); | |
216 gb = gaim_find_buddy((GaimAccount*)extra, name); | |
11539 | 217 if (gb != NULL) |
218 { | |
11477 | 219 bonjour_buddy_delete(gb->proto_data); |
220 gaim_blist_remove_buddy(gb); | |
221 } | |
222 break; | |
223 case SW_DISCOVERY_BROWSE_RESOLVED: | |
224 gaim_debug_info("bonjour", "_browse_reply --> Resolved\n"); | |
225 break; | |
226 default: | |
227 break; | |
228 } | |
11539 | 229 |
11477 | 230 return SW_OKAY; |
231 } | |
232 | |
11677 | 233 static int |
11539 | 234 _dns_sd_publish(BonjourDnsSd *data, PublishType type) |
11477 | 235 { |
236 sw_text_record dns_data; | |
11773
92cf092f194e
[gaim-migrate @ 14064]
Gary Kramlich <grim@reaperworld.com>
parents:
11678
diff
changeset
|
237 sw_result publish_result = SW_OKAY; |
11477 | 238 |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
239 /* Fill the data for the service */ |
11539 | 240 if (sw_text_record_init(&dns_data) != SW_OKAY) |
241 { | |
11927 | 242 gaim_debug_error("bonjour", "Unable to initialize the data for the mDNS.\n"); |
11477 | 243 return -1; |
244 } | |
245 | |
246 sw_text_record_add_key_and_string_value(dns_data, "txtvers", data->txtvers); | |
247 sw_text_record_add_key_and_string_value(dns_data, "version", data->version); | |
248 sw_text_record_add_key_and_string_value(dns_data, "1st", data->first); | |
249 sw_text_record_add_key_and_string_value(dns_data, "last", data->last); | |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
250 /* sw_text_record_add_key_and_string_value(dns_data, "port.p2pj", itoa(data->port_p2pj)); */ |
11477 | 251 sw_text_record_add_key_and_string_value(dns_data, "port.p2pj", BONJOUR_DEFAULT_PORT); |
252 sw_text_record_add_key_and_string_value(dns_data, "phsh", data->phsh); | |
253 sw_text_record_add_key_and_string_value(dns_data, "status", data->status); | |
254 sw_text_record_add_key_and_string_value(dns_data, "vc", data->vc); | |
13906 | 255 |
256 if ((data->email != NULL) && (*data->email != '\0')) | |
257 sw_text_record_add_key_and_string_value(dns_data, "email", data->email); | |
258 | |
259 if ((data->jid != NULL) && (*data->jid != '\0')) | |
260 sw_text_record_add_key_and_string_value(dns_data, "jid", data->jid); | |
261 | |
262 if ((data->AIM != NULL) && (*data->AIM != '\0')) | |
263 sw_text_record_add_key_and_string_value(dns_data, "AIM", data->AIM); | |
264 | |
265 if ((data->msg != NULL) && (*data->msg != '\0')) | |
266 sw_text_record_add_key_and_string_value(dns_data, "msg", data->msg); | |
11477 | 267 |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
268 /* Publish the service */ |
11539 | 269 switch (type) |
270 { | |
11477 | 271 case PUBLISH_START: |
12104 | 272 publish_result = sw_discovery_publish(data->session, 0, data->name, ICHAT_SERVICE, NULL, |
11539 | 273 NULL, data->port_p2pj, sw_text_record_bytes(dns_data), sw_text_record_len(dns_data), |
12104 | 274 _publish_reply, NULL, &data->session_id); |
11477 | 275 break; |
276 case PUBLISH_UPDATE: | |
12104 | 277 publish_result = sw_discovery_publish_update(data->session, data->session_id, |
11477 | 278 sw_text_record_bytes(dns_data), sw_text_record_len(dns_data)); |
279 break; | |
280 } | |
11539 | 281 if (publish_result != SW_OKAY) |
282 { | |
11477 | 283 gaim_debug_error("bonjour", "Unable to publish or change the status of the _presence._tcp service."); |
284 return -1; | |
285 } | |
286 | |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
287 /* Free the memory used by temp data */ |
11477 | 288 sw_text_record_fina(dns_data); |
289 | |
290 return 0; | |
291 } | |
292 | |
11678 | 293 static void |
294 _dns_sd_handle_packets(gpointer data, gint source, GaimInputCondition condition) | |
11477 | 295 { |
12104 | 296 sw_discovery_read_socket((sw_discovery)data); |
11477 | 297 } |
298 | |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
299 /* End private functions */ |
11477 | 300 |
301 /** | |
302 * Allocate space for the dns-sd data. | |
303 */ | |
11539 | 304 BonjourDnsSd * |
305 bonjour_dns_sd_new() | |
11477 | 306 { |
12035
52eaf0f0db4c
[gaim-migrate @ 14328]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11927
diff
changeset
|
307 BonjourDnsSd *data = g_new0(BonjourDnsSd, 1); |
11539 | 308 |
11477 | 309 return data; |
310 } | |
311 | |
312 /** | |
313 * Deallocate the space of the dns-sd data. | |
314 */ | |
11539 | 315 void |
316 bonjour_dns_sd_free(BonjourDnsSd *data) | |
11477 | 317 { |
11496 | 318 g_free(data->first); |
319 g_free(data->last); | |
320 g_free(data->email); | |
11477 | 321 g_free(data); |
322 } | |
323 | |
324 /** | |
325 * Send a new dns-sd packet updating our status. | |
326 */ | |
11539 | 327 void |
328 bonjour_dns_sd_send_status(BonjourDnsSd *data, const char *status, const char *status_message) | |
11477 | 329 { |
11496 | 330 g_free(data->status); |
331 g_free(data->msg); | |
11477 | 332 |
333 data->status = g_strdup(status); | |
334 data->msg = g_strdup(status_message); | |
335 | |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
336 /* Update our text record with the new status */ |
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
337 _dns_sd_publish(data, PUBLISH_UPDATE); /* <--We must control the errors */ |
11477 | 338 } |
339 | |
340 /** | |
11496 | 341 * Advertise our presence within the dns-sd daemon and start browsing |
342 * for other bonjour peers. | |
11477 | 343 */ |
11927 | 344 gboolean |
11539 | 345 bonjour_dns_sd_start(BonjourDnsSd *data) |
346 { | |
11678 | 347 GaimAccount *account; |
348 GaimConnection *gc; | |
11477 | 349 gint dns_sd_socket; |
350 sw_discovery_oid session_id; | |
11539 | 351 |
11678 | 352 account = data->account; |
353 gc = gaim_account_get_connection(account); | |
354 | |
11927 | 355 /* Initialize the dns-sd data and session */ |
12104 | 356 if (sw_discovery_init(&data->session) != SW_OKAY) |
11539 | 357 { |
11927 | 358 gaim_debug_error("bonjour", "Unable to initialize an mDNS session.\n"); |
13063 | 359 |
360 /* In Avahi, sw_discovery_init frees data->session but doesn't clear it */ | |
361 data->session = NULL; | |
362 | |
11927 | 363 return FALSE; |
11477 | 364 } |
11539 | 365 |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
366 /* Publish our bonjour IM client at the mDNS daemon */ |
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
367 _dns_sd_publish(data, PUBLISH_START); /* <--We must control the errors */ |
11539 | 368 |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
369 /* Advise the daemon that we are waiting for connections */ |
12104 | 370 if (sw_discovery_browse(data->session, 0, ICHAT_SERVICE, NULL, _browser_reply, |
11539 | 371 data->account, &session_id) != SW_OKAY) |
372 { | |
11477 | 373 gaim_debug_error("bonjour", "Unable to get service."); |
11927 | 374 return FALSE; |
11477 | 375 } |
11539 | 376 |
11829
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
377 /* Get the socket that communicates with the mDNS daemon and bind it to a */ |
4669e7461968
[gaim-migrate @ 14120]
Richard Laager <rlaager@wiktel.com>
parents:
11773
diff
changeset
|
378 /* callback that will handle the dns_sd packets */ |
12104 | 379 dns_sd_socket = sw_discovery_socket(data->session); |
11678 | 380 gc->inpa = gaim_input_add(dns_sd_socket, GAIM_INPUT_READ, |
381 _dns_sd_handle_packets, data->session); | |
11927 | 382 |
383 return TRUE; | |
11477 | 384 } |
385 | |
386 /** | |
387 * Unregister the "_presence._tcp" service at the mDNS daemon. | |
388 */ | |
11927 | 389 void |
11539 | 390 bonjour_dns_sd_stop(BonjourDnsSd *data) |
11477 | 391 { |
11678 | 392 GaimAccount *account; |
393 GaimConnection *gc; | |
394 | |
11927 | 395 if (data->session == NULL) |
396 return; | |
397 | |
12104 | 398 sw_discovery_cancel(data->session, data->session_id); |
11539 | 399 |
11678 | 400 account = data->account; |
401 gc = gaim_account_get_connection(account); | |
402 gaim_input_remove(gc->inpa); | |
12093 | 403 |
404 g_free(data->session); | |
405 data->session = NULL; | |
11477 | 406 } |