comparison libpurple/protocols/bonjour/mdns_howl.c @ 18798:ed1def07d86e

Refactor the bonjour mdns implementation abstraction in preparation for the avahi implementation. Also fix alias assignment from the server to populate the server alias instead of the local alias.
author Daniel Atallah <daniel.atallah@gmail.com>
date Sun, 05 Aug 2007 02:44:53 +0000
parents bf45ab338124
children 83719d57a8ee
comparison
equal deleted inserted replaced
18796:95fe5195bf98 18798:ed1def07d86e
13 * along with this program; if not, write to the Free Software 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. 14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */ 15 */
16 16
17 #include "internal.h" 17 #include "internal.h"
18 #include "mdns_howl.h" 18
19 19 #include "mdns_interface.h"
20 #include "debug.h" 20 #include "debug.h"
21 #include "buddy.h" 21 #include "buddy.h"
22 22
23 sw_result HOWL_API 23 #include <howl.h>
24
25 /* data used by howl bonjour implementation */
26 typedef struct _howl_impl_data {
27 sw_discovery session;
28 sw_discovery_oid session_id;
29 } HowlSessionImplData;
30
31 static sw_result HOWL_API
24 _publish_reply(sw_discovery discovery, sw_discovery_oid oid, 32 _publish_reply(sw_discovery discovery, sw_discovery_oid oid,
25 sw_discovery_publish_status status, sw_opaque extra) 33 sw_discovery_publish_status status, sw_opaque extra)
26 { 34 {
27 purple_debug_warning("bonjour", "_publish_reply --> Start\n"); 35 purple_debug_warning("bonjour", "_publish_reply --> Start\n");
28 36
44 } 52 }
45 53
46 return SW_OKAY; 54 return SW_OKAY;
47 } 55 }
48 56
49 sw_result HOWL_API 57 static sw_result HOWL_API
50 _resolve_reply(sw_discovery discovery, sw_discovery_oid oid, 58 _resolve_reply(sw_discovery discovery, sw_discovery_oid oid,
51 sw_uint32 interface_index, sw_const_string name, 59 sw_uint32 interface_index, sw_const_string name,
52 sw_const_string type, sw_const_string domain, 60 sw_const_string type, sw_const_string domain,
53 sw_ipv4_address address, sw_port port, 61 sw_ipv4_address address, sw_port port,
54 sw_octets text_record, sw_ulong text_record_len, 62 sw_octets text_record, sw_ulong text_record_len,
93 bonjour_buddy_add_to_purple(buddy); 101 bonjour_buddy_add_to_purple(buddy);
94 102
95 return SW_OKAY; 103 return SW_OKAY;
96 } 104 }
97 105
98 sw_result HOWL_API 106 static sw_result HOWL_API
99 _browser_reply(sw_discovery discovery, sw_discovery_oid oid, 107 _browser_reply(sw_discovery discovery, sw_discovery_oid oid,
100 sw_discovery_browse_status status, 108 sw_discovery_browse_status status,
101 sw_uint32 interface_index, sw_const_string name, 109 sw_uint32 interface_index, sw_const_string name,
102 sw_const_string type, sw_const_string domain, 110 sw_const_string type, sw_const_string domain,
103 sw_opaque_t extra) 111 sw_opaque_t extra)
152 } 160 }
153 161
154 return SW_OKAY; 162 return SW_OKAY;
155 } 163 }
156 164
157 int 165 static void
158 _mdns_publish(BonjourDnsSd *data, PublishType type) 166 _mdns_handle_event(gpointer data, gint source, PurpleInputCondition condition)
159 { 167 {
168 sw_discovery_read_socket((sw_discovery)data);
169 }
170
171 /****************************
172 * mdns_interface functions *
173 ****************************/
174
175 gboolean _mdns_init_session(BonjourDnsSd *data) {
176 HowlSessionImplData *idata = g_new0(HowlSessionImplData, 1);
177
178 if (sw_discovery_init(&idata->session) != SW_OKAY) {
179 purple_debug_error("bonjour", "Unable to initialize an mDNS session.\n");
180
181 /* In Avahi, sw_discovery_init frees data->session but doesn't clear it */
182 idata->session = NULL;
183
184 g_free(idata);
185
186 return FALSE;
187 }
188
189 data->mdns_impl_data = idata;
190
191 return TRUE;
192 }
193
194
195 gboolean _mdns_publish(BonjourDnsSd *data, PublishType type) {
160 sw_text_record dns_data; 196 sw_text_record dns_data;
161 sw_result publish_result = SW_OKAY; 197 sw_result publish_result = SW_OKAY;
162 char portstring[6]; 198 char portstring[6];
163 const char *jid, *aim, *email; 199 const char *jid, *aim, *email;
200 HowlSessionImplData *idata = data->mdns_impl_data;
201
202 g_return_val_if_fail(idata != NULL, FALSE);
164 203
165 /* Fill the data for the service */ 204 /* Fill the data for the service */
166 if (sw_text_record_init(&dns_data) != SW_OKAY) 205 if (sw_text_record_init(&dns_data) != SW_OKAY) {
167 {
168 purple_debug_error("bonjour", "Unable to initialize the data for the mDNS.\n"); 206 purple_debug_error("bonjour", "Unable to initialize the data for the mDNS.\n");
169 return -1; 207 return FALSE;
170 } 208 }
171 209
172 /* Convert the port to a string */ 210 /* Convert the port to a string */
173 snprintf(portstring, sizeof(portstring), "%d", data->port_p2pj); 211 snprintf(portstring, sizeof(portstring), "%d", data->port_p2pj);
174 212
206 sw_text_record_add_key_and_string_value(dns_data, "phsh", data->phsh); 244 sw_text_record_add_key_and_string_value(dns_data, "phsh", data->phsh);
207 245
208 /* TODO: ext, nick, node */ 246 /* TODO: ext, nick, node */
209 247
210 /* Publish the service */ 248 /* Publish the service */
211 switch (type) 249 switch (type) {
212 {
213 case PUBLISH_START: 250 case PUBLISH_START:
214 publish_result = sw_discovery_publish(data->session, 0, purple_account_get_username(data->account), ICHAT_SERVICE, NULL, 251 publish_result = sw_discovery_publish(idata->session, 0, purple_account_get_username(data->account), ICHAT_SERVICE, NULL,
215 NULL, data->port_p2pj, sw_text_record_bytes(dns_data), sw_text_record_len(dns_data), 252 NULL, data->port_p2pj, sw_text_record_bytes(dns_data), sw_text_record_len(dns_data),
216 _publish_reply, NULL, &data->session_id); 253 _publish_reply, NULL, &idata->session_id);
217 break; 254 break;
218 case PUBLISH_UPDATE: 255 case PUBLISH_UPDATE:
219 publish_result = sw_discovery_publish_update(data->session, data->session_id, 256 publish_result = sw_discovery_publish_update(idata->session, idata->session_id,
220 sw_text_record_bytes(dns_data), sw_text_record_len(dns_data)); 257 sw_text_record_bytes(dns_data), sw_text_record_len(dns_data));
221 break; 258 break;
222 } 259 }
223 if (publish_result != SW_OKAY) 260
224 { 261 if (publish_result != SW_OKAY) {
225 purple_debug_error("bonjour", "Unable to publish or change the status of the _presence._tcp service.\n"); 262 purple_debug_error("bonjour", "Unable to publish or change the status of the _presence._tcp service.\n");
226 return -1; 263 return FALSE;
227 } 264 }
228 265
229 /* Free the memory used by temp data */ 266 /* Free the memory used by temp data */
230 sw_text_record_fina(dns_data); 267 sw_text_record_fina(dns_data);
231 268
232 return 0; 269 return TRUE;
233 } 270 }
234 271
235 void 272 gboolean _mdns_browse(BonjourDnsSd *data) {
236 _mdns_handle_event(gpointer data, gint source, PurpleInputCondition condition) 273 HowlSessionImplData *idata = data->mdns_impl_data;
237 { 274
238 sw_discovery_read_socket((sw_discovery)data); 275 g_return_val_if_fail(idata != NULL, FALSE);
239 } 276
277 /* TODO: don't we need to hang onto this to cancel later? */
278 sw_discovery_oid session_id;
279
280 return (sw_discovery_browse(idata->session, 0, ICHAT_SERVICE, NULL, _browser_reply,
281 data->account, &session_id) == SW_OKAY);
282 }
283
284 guint _mdns_register_to_mainloop(BonjourDnsSd *data) {
285 HowlSessionImplData *idata = data->mdns_impl_data;
286
287 g_return_val_if_fail(idata != NULL, FALSE);
288
289 return purple_input_add(sw_discovery_socket(idata->session),
290 PURPLE_INPUT_READ, _mdns_handle_event, idata->session);
291 }
292
293 void _mdns_stop(BonjourDnsSd *data) {
294 HowlSessionImplData *idata = data->mdns_impl_data;
295
296 if (idata == NULL || idata->session == NULL)
297 return;
298
299 sw_discovery_cancel(idata->session, idata->session_id);
300
301 /* TODO: should this really be g_free()'d ??? */
302 g_free(idata->session);
303
304 g_free(idata);
305
306 data->mdns_impl_data = NULL;
307 }
308
309 void _mdns_init_buddy(BonjourBuddy *buddy) {
310 }
311
312 void _mdns_delete_buddy(BonjourBuddy *buddy) {
313 }
314