static void bonjour_bytestreams_connect(PurpleXfer *xfer) { PurpleBuddy *pb; PurpleAccount *account = NULL; PurpleHash *hash; XepXfer *xf; char dstaddr[41]; const gchar *name = NULL; unsigned char hashval[20]; char *p; int i; if(xfer == NULL) return; purple_debug_info("bonjour", "bonjour-bytestreams-connect.\n"); xf = purple_xfer_get_protocol_data(xfer); if(!xf) return; pb = xf->pb; name = purple_buddy_get_name(pb); account = purple_buddy_get_account(pb); p = g_strdup_printf("%s%s%s", xf->sid, name, bonjour_get_jid(account)); hash = purple_sha1_hash_new(); purple_hash_append(hash, (guchar *)p, strlen(p)); purple_hash_digest(hash, hashval, sizeof(hashval)); g_object_unref(G_OBJECT(hash)); g_free(p); memset(dstaddr, 0, 41); p = dstaddr; for(i = 0; i < 20; i++, p += 2) snprintf(p, 3, "%02x", hashval[i]); xf->proxy_info = purple_proxy_info_new(); purple_proxy_info_set_proxy_type(xf->proxy_info, PURPLE_PROXY_SOCKS5); purple_proxy_info_set_host(xf->proxy_info, xf->proxy_host); purple_proxy_info_set_port(xf->proxy_info, xf->proxy_port); xf->proxy_connection = purple_proxy_connect_socks5_account( purple_account_get_connection(account), account, xf->proxy_info, dstaddr, 0, bonjour_bytestreams_connect_cb, xfer); if(xf->proxy_connection == NULL) { xep_ft_si_reject(xf->data, xf->iq_id, purple_xfer_get_remote_user(xfer), "404", "cancel"); /* Cancel the connection */ purple_xfer_cancel_local(xfer); } }
static gboolean bonjour_jabber_send_stream_init(BonjourJabberConversation *bconv, int client_socket) { int ret, len; char *stream_start; const char *bname = bconv->buddy_name; if (bconv->pb != NULL) bname = purple_buddy_get_name(bconv->pb); /* If we have no idea who "to" is, use an empty string. * If we don't know now, it is because the other side isn't playing nice, so they can't complain. */ if (bname == NULL) bname = ""; stream_start = g_strdup_printf(DOCTYPE, bonjour_get_jid(bconv->account), bname); len = strlen(stream_start); bconv->sent_stream_start = PARTIALLY_SENT; /* Start the stream */ ret = send(client_socket, stream_start, len, 0); if (ret == -1 && errno == EAGAIN) ret = 0; else if (ret <= 0) { const char *err = g_strerror(errno); purple_debug_error("bonjour", "Error starting stream with buddy %s at %s error: %s\n", (*bname) ? bname : "(unknown)", bconv->ip, err ? err : "(null)"); if (bconv->pb) { PurpleConversation *conv; conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, bname, bconv->account); if (conv != NULL) purple_conversation_write(conv, NULL, _("Unable to send the message, the conversation couldn't be started."), PURPLE_MESSAGE_SYSTEM, time(NULL)); } close(client_socket); g_free(stream_start); return FALSE; } /* This is unlikely to happen */ if (ret < len) { struct _stream_start_data *ss = g_new(struct _stream_start_data, 1); ss->msg = g_strdup(stream_start + ret); bconv->stream_data = ss; /* Finish sending the stream start */ bconv->tx_handler = purple_input_add(client_socket, PURPLE_INPUT_WRITE, _start_stream, bconv); } else
static void bonjour_bytestreams_listen(int sock, gpointer data) { PurpleXfer *xfer = data; XepXfer *xf; XepIq *iq; xmlnode *query, *streamhost; gchar *port; GSList *local_ips; BonjourData *bd; purple_debug_info("bonjour", "Bonjour-bytestreams-listen. sock=%d.\n", sock); if (sock < 0 || xfer == NULL) { /*purple_xfer_cancel_local(xfer);*/ return; } xfer->watcher = purple_input_add(sock, PURPLE_INPUT_READ, bonjour_sock5_request_cb, xfer); xf = (XepXfer*)xfer->data; xf->listen_data = NULL; bd = xf->data; iq = xep_iq_new(bd, XEP_IQ_SET, xfer->who, bonjour_get_jid(bd->jabber_data->account), xf->sid); query = xmlnode_new_child(iq->node, "query"); xmlnode_set_namespace(query, "http://jabber.org/protocol/bytestreams"); xmlnode_set_attrib(query, "sid", xf->sid); xmlnode_set_attrib(query, "mode", "tcp"); xfer->local_port = purple_network_get_port_from_fd(sock); local_ips = bonjour_jabber_get_local_ips(sock); port = g_strdup_printf("%hu", (guint16)purple_xfer_get_local_port(xfer)); while(local_ips) { streamhost = xmlnode_new_child(query, "streamhost"); xmlnode_set_attrib(streamhost, "jid", xf->sid); xmlnode_set_attrib(streamhost, "host", local_ips->data); xmlnode_set_attrib(streamhost, "port", port); g_free(local_ips->data); local_ips = g_slist_delete_link(local_ips, local_ips); } g_free(port); xep_iq_send_and_free(iq); }
static void bonjour_bytestreams_connect_cb(gpointer data, gint source, const gchar *error_message) { PurpleXfer *xfer = data; XepXfer *xf = xfer->data; XepIq *iq; xmlnode *q_node, *tmp_node; BonjourData *bd; gboolean ret = FALSE; xf->proxy_connection = NULL; if(source < 0) { purple_debug_error("bonjour", "Error connecting via SOCKS5 to %s - %s\n", xf->proxy_host, error_message ? error_message : "(null)"); tmp_node = xmlnode_get_next_twin(xf->streamhost); ret = __xep_bytestreams_parse(xf->pb, xfer, tmp_node, xf->iq_id); if (!ret) { xep_ft_si_reject(xf->data, xf->iq_id, purple_xfer_get_remote_user(xfer), "404", "cancel"); /* Cancel the connection */ purple_xfer_cancel_local(xfer); } return; } purple_debug_info("bonjour", "Connected successfully via SOCKS5, starting transfer.\n"); bd = xf->data; /* Here, start the file transfer.*/ /* Notify Initiator of Connection */ iq = xep_iq_new(bd, XEP_IQ_RESULT, xfer->who, bonjour_get_jid(bd->jabber_data->account), xf->iq_id); q_node = xmlnode_new_child(iq->node, "query"); xmlnode_set_namespace(q_node, "http://jabber.org/protocol/bytestreams"); tmp_node = xmlnode_new_child(q_node, "streamhost-used"); xmlnode_set_attrib(tmp_node, "jid", xf->jid); xep_iq_send_and_free(iq); purple_xfer_start(xfer, source, NULL, -1); }
static void xep_ft_si_result(PurpleXfer *xfer, char *to) { xmlnode *si_node, *feature, *field, *value, *x; XepIq *iq; XepXfer *xf; BonjourData *bd; if(!to || !xfer) return; xf = xfer->data; if(!xf) return; bd = xf->data; purple_debug_info("bonjour", "xep file transfer stream initialization result.\n"); iq = xep_iq_new(bd, XEP_IQ_RESULT, to, bonjour_get_jid(bd->jabber_data->account), xf->iq_id); if(iq == NULL) return; si_node = xmlnode_new_child(iq->node, "si"); xmlnode_set_namespace(si_node, "http://jabber.org/protocol/si"); /*xmlnode_set_attrib(si_node, "profile", "http://jabber.org/protocol/si/profile/file-transfer");*/ feature = xmlnode_new_child(si_node, "feature"); xmlnode_set_namespace(feature, "http://jabber.org/protocol/feature-neg"); x = xmlnode_new_child(feature, "x"); xmlnode_set_namespace(x, "jabber:x:data"); xmlnode_set_attrib(x, "type", "submit"); field = xmlnode_new_child(x, "field"); xmlnode_set_attrib(field, "var", "stream-method"); value = xmlnode_new_child(field, "value"); xmlnode_insert_data(value, "http://jabber.org/protocol/bytestreams", -1); xep_iq_send_and_free(iq); }
static void xep_ft_si_reject(BonjourData *bd, const char *id, const char *to, const char *error_code, const char *error_type) { xmlnode *error_node; XepIq *iq; g_return_if_fail(error_code != NULL); g_return_if_fail(error_type != NULL); if(!to || !id) { purple_debug_info("bonjour", "xep file transfer stream initialization error.\n"); return; } iq = xep_iq_new(bd, XEP_IQ_ERROR, to, bonjour_get_jid(bd->jabber_data->account), id); if(iq == NULL) return; error_node = xmlnode_new_child(iq->node, "error"); xmlnode_set_attrib(error_node, "code", error_code); xmlnode_set_attrib(error_node, "type", error_type); /* TODO: Make this better */ if (purple_strequal(error_code, "403")) { xmlnode *tmp_node = xmlnode_new_child(error_node, "forbidden"); xmlnode_set_namespace(tmp_node, "urn:ietf:params:xml:ns:xmpp-stanzas"); tmp_node = xmlnode_new_child(error_node, "text"); xmlnode_set_namespace(tmp_node, "urn:ietf:params:xml:ns:xmpp-stanzas"); xmlnode_insert_data(tmp_node, "Offer Declined", -1); } else if (purple_strequal(error_code, "404")) { xmlnode *tmp_node = xmlnode_new_child(error_node, "item-not-found"); xmlnode_set_namespace(tmp_node, "urn:ietf:params:xml:ns:xmpp-stanzas"); } xep_iq_send_and_free(iq); }
static void xep_ft_si_offer(PurpleXfer *xfer, const gchar *to) { xmlnode *si_node, *feature, *field, *file, *x; XepIq *iq; XepXfer *xf = xfer->data; BonjourData *bd = NULL; char buf[32]; if(!xf) return; bd = xf->data; if(!bd) return; purple_debug_info("bonjour", "xep file transfer stream initialization offer-id=%d.\n", next_id); /* Assign stream id. */ g_free(xf->iq_id); xf->iq_id = g_strdup_printf("%u", next_id++); iq = xep_iq_new(xf->data, XEP_IQ_SET, to, bonjour_get_jid(bd->jabber_data->account), xf->iq_id); if(iq == NULL) return; /*Construct Stream initialization offer message.*/ si_node = xmlnode_new_child(iq->node, "si"); xmlnode_set_namespace(si_node, "http://jabber.org/protocol/si"); xmlnode_set_attrib(si_node, "profile", "http://jabber.org/protocol/si/profile/file-transfer"); g_free(xf->sid); xf->sid = g_strdup(xf->iq_id); xmlnode_set_attrib(si_node, "id", xf->sid); file = xmlnode_new_child(si_node, "file"); xmlnode_set_namespace(file, "http://jabber.org/protocol/si/profile/file-transfer"); xmlnode_set_attrib(file, "name", xfer->filename); g_snprintf(buf, sizeof(buf), "%" G_GSIZE_FORMAT, xfer->size); xmlnode_set_attrib(file, "size", buf); feature = xmlnode_new_child(si_node, "feature"); xmlnode_set_namespace(feature, "http://jabber.org/protocol/feature-neg"); x = xmlnode_new_child(feature, "x"); xmlnode_set_namespace(x, "jabber:x:data"); xmlnode_set_attrib(x, "type", "form"); field = xmlnode_new_child(x, "field"); xmlnode_set_attrib(field, "var", "stream-method"); xmlnode_set_attrib(field, "type", "list-single"); if (xf->mode & XEP_BYTESTREAMS) { xmlnode *option = xmlnode_new_child(field, "option"); xmlnode *value = xmlnode_new_child(option, "value"); xmlnode_insert_data(value, "http://jabber.org/protocol/bytestreams", -1); } if (xf->mode & XEP_IBB) { xmlnode *option = xmlnode_new_child(field, "option"); xmlnode *value = xmlnode_new_child(option, "value"); xmlnode_insert_data(value, "http://jabber.org/protocol/ibb", -1); } xep_iq_send_and_free(iq); }
gboolean _mdns_set_buddy_icon_data(BonjourDnsSd *data, gconstpointer avatar_data, gsize avatar_len) { AvahiSessionImplData *idata = data->mdns_impl_data; if (idata == NULL || idata->client == NULL) return FALSE; if (avatar_data != NULL) { gboolean new_group = FALSE; gchar *svc_name; int ret; AvahiPublishFlags flags = 0; if (idata->buddy_icon_group == NULL) { purple_debug_info("bonjour", "Setting new buddy icon.\n"); new_group = TRUE; idata->buddy_icon_group = avahi_entry_group_new(idata->client, _buddy_icon_group_cb, data); } else { purple_debug_info("bonjour", "Updating existing buddy icon.\n"); flags |= AVAHI_PUBLISH_UPDATE; } if (idata->buddy_icon_group == NULL) { purple_debug_error("bonjour", "Unable to initialize the buddy icon group (%s).\n", avahi_strerror(avahi_client_errno(idata->client))); return FALSE; } svc_name = g_strdup_printf("%s." LINK_LOCAL_RECORD_NAME "local", bonjour_get_jid(data->account)); ret = avahi_entry_group_add_record(idata->buddy_icon_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, flags, svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_NULL, 120, avatar_data, avatar_len); g_free(svc_name); if (ret < 0) { purple_debug_error("bonjour", "Failed to register buddy icon. Error: %s\n", avahi_strerror(ret)); if (new_group) { avahi_entry_group_free(idata->buddy_icon_group); idata->buddy_icon_group = NULL; } return FALSE; } if (new_group && (ret = avahi_entry_group_commit(idata->buddy_icon_group)) < 0) { purple_debug_error("bonjour", "Failed to commit buddy icon group. Error: %s\n", avahi_strerror(ret)); if (new_group) { avahi_entry_group_free(idata->buddy_icon_group); idata->buddy_icon_group = NULL; } return FALSE; } } else if (idata->buddy_icon_group != NULL) { purple_debug_info("bonjour", "Removing existing buddy icon.\n"); avahi_entry_group_free(idata->buddy_icon_group); idata->buddy_icon_group = NULL; } return TRUE; }
gboolean _mdns_publish(BonjourDnsSd *data, PublishType type, GSList *records) { int publish_result = 0; AvahiSessionImplData *idata = data->mdns_impl_data; AvahiStringList *lst = NULL; g_return_val_if_fail(idata != NULL, FALSE); if (!idata->group) { idata->group = avahi_entry_group_new(idata->client, _entry_group_cb, idata); if (!idata->group) { purple_debug_error("bonjour", "Unable to initialize the data for the mDNS (%s).\n", avahi_strerror(avahi_client_errno(idata->client))); return FALSE; } } while (records) { PurpleKeyValuePair *kvp = records->data; lst = avahi_string_list_add_pair(lst, kvp->key, kvp->value); records = records->next; } /* Publish the service */ switch (type) { case PUBLISH_START: publish_result = avahi_entry_group_add_service_strlst( idata->group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, bonjour_get_jid(data->account), LINK_LOCAL_RECORD_NAME, NULL, NULL, data->port_p2pj, lst); break; case PUBLISH_UPDATE: publish_result = avahi_entry_group_update_service_txt_strlst( idata->group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, bonjour_get_jid(data->account), LINK_LOCAL_RECORD_NAME, NULL, lst); break; } /* Free the memory used by temp data */ avahi_string_list_free(lst); if (publish_result < 0) { purple_debug_error("bonjour", "Failed to add the " LINK_LOCAL_RECORD_NAME " service. Error: %s\n", avahi_strerror(publish_result)); return FALSE; } if (type == PUBLISH_START && (publish_result = avahi_entry_group_commit(idata->group)) < 0) { purple_debug_error("bonjour", "Failed to commit " LINK_LOCAL_RECORD_NAME " service. Error: %s\n", avahi_strerror(publish_result)); return FALSE; } return TRUE; }
static void _browser_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, AvahiLookupResultFlags flags, void *userdata) { PurpleAccount *account = userdata; PurpleBuddy *pb = NULL; switch (event) { case AVAHI_BROWSER_FAILURE: purple_debug_error("bonjour", "_browser_callback - Failure: %s\n", avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b)))); /* TODO: This is an error that should be handled. */ break; case AVAHI_BROWSER_NEW: /* A new peer has joined the network and uses iChat bonjour */ purple_debug_info("bonjour", "_browser_callback - new service\n"); /* Make sure it isn't us */ if (purple_utf8_strcasecmp(name, bonjour_get_jid(account)) != 0) { if (!avahi_service_resolver_new(avahi_service_browser_get_client(b), interface, protocol, name, type, domain, protocol, 0, _resolver_callback, account)) { purple_debug_warning("bonjour", "_browser_callback -- Error initiating resolver: %s\n", avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b)))); } } break; case AVAHI_BROWSER_REMOVE: purple_debug_info("bonjour", "_browser_callback - Remove service\n"); pb = purple_find_buddy(account, name); if (pb != NULL) { BonjourBuddy *bb = purple_buddy_get_protocol_data(pb); AvahiBuddyImplData *b_impl; GSList *l; AvahiSvcResolverData *rd_search; g_return_if_fail(bb != NULL); b_impl = bb->mdns_impl_data; /* There may be multiple presences, we should only get rid of this one */ rd_search = g_new0(AvahiSvcResolverData, 1); rd_search->interface = interface; rd_search->protocol = protocol; rd_search->name = (gchar *) name; rd_search->type = (gchar *) type; rd_search->domain = (gchar *) domain; l = g_slist_find_custom(b_impl->resolvers, rd_search, _find_resolver_data); g_free(rd_search); if (l != NULL) { AvahiSvcResolverData *rd = l->data; b_impl->resolvers = g_slist_remove(b_impl->resolvers, rd); /* This IP is no longer available */ if (rd->ip != NULL) { bb->ips = g_slist_remove(bb->ips, rd->ip); g_free((gchar *) rd->ip); } _cleanup_resolver_data(rd); /* If this was the last resolver, remove the buddy */ if (b_impl->resolvers == NULL) bonjour_buddy_signed_off(pb); } } break; case AVAHI_BROWSER_ALL_FOR_NOW: case AVAHI_BROWSER_CACHE_EXHAUSTED: break; default: purple_debug_info("bonjour", "Unrecognized Service browser event: %d.\n", event); } }