From: <dat...@us...> - 2006-08-17 03:13:23
|
Revision: 16805 Author: datallah Date: 2006-08-16 20:13:15 -0700 (Wed, 16 Aug 2006) ViewCVS: http://svn.sourceforge.net/gaim/?rev=16805&view=rev Log Message: ----------- Revision: 16804 Author: datallah Date: 10:40:44 PM, Wednesday, August 16, 2006 Message: Sadrul fixed this buglet which prevented the global buddy icon from being applied to new accounts ---- Modified : /trunk/src/gtkaccount.c Revision: 16801 Author: datallah Date: 9:06:27 PM, Wednesday, August 16, 2006 Message: Make the silc key permission checks safer and cover more cases (e.g. private key exists, but is not readable by you). ---- Modified : /trunk/src/protocols/silc/util.c Revision: 16800 Author: seanegan Date: 4:50:16 PM, Wednesday, August 16, 2006 Message: segfault fix ---- Modified : /trunk/src/gtkstatusbox.c Revision: 16799 Author: sadrul Date: 3:38:57 PM, Wednesday, August 16, 2006 Message: Use the proper setting-name. "seanegan: sadrul: Looks good. You may want to set the non-global-buddyicon account setting if use-global-buddyicon is not set" Also, if the user sets the buddy-icon from a per-account box, it unsets the use-global-budduicon setting for that account. ---- Modified : /trunk/src/gtkstatusbox.c Revision: 16798 Author: sadrul Date: 3:21:04 PM, Wednesday, August 16, 2006 Message: Do not show the icon-box for an account if it doesn't support buddyicons in the per-account statusboxes. Allow the peraccount boxes to change the icon only for that account. ---- Modified : /trunk/src/gtkblist.c Modified : /trunk/src/gtkstatusbox.c Revision: 16797 Author: seanegan Date: 3:05:07 PM, Wednesday, August 16, 2006 Message: fixes ---- Modified : /trunk/src/gtkstatusbox.c Revision: 16796 Author: datallah Date: 2:55:17 PM, Wednesday, August 16, 2006 Message: Fix bug #1541223 ---- Modified : /trunk/src/protocols/simple/simple.c Revision: 16795 Author: seanegan Date: 1:31:29 PM, Wednesday, August 16, 2006 Message: Now with 'compiling' feature ---- Modified : /trunk/src/gtkstatusbox.c Revision: 16794 Author: seanegan Date: 1:26:57 PM, Wednesday, August 16, 2006 Message: Only one file selector ---- Modified : /trunk/src/gtkstatusbox.c Revision: 16788 Author: datallah Date: 7:13:19 AM, Wednesday, August 16, 2006 Message: gdk_threads_init() is causing problems on win32 for some reason. ---- Modified : /trunk/src/gtkmain.c Modified Paths: -------------- branches/v2_0_0/src/gtkaccount.c branches/v2_0_0/src/gtkmain.c branches/v2_0_0/src/gtkstatusbox.c branches/v2_0_0/src/protocols/silc/util.c branches/v2_0_0/src/protocols/simple/simple.c Modified: branches/v2_0_0/src/gtkaccount.c =================================================================== --- branches/v2_0_0/src/gtkaccount.c 2006-08-17 02:40:44 UTC (rev 16804) +++ branches/v2_0_0/src/gtkaccount.c 2006-08-17 03:13:15 UTC (rev 16805) @@ -1119,7 +1119,7 @@ gaim_account_set_alias(account, NULL); /* Buddy Icon */ - if (gaim_account_get_ui_bool(account, GAIM_GTK_UI, "use-global-buddyicon", TRUE) == + if (new || gaim_account_get_ui_bool(account, GAIM_GTK_UI, "use-global-buddyicon", TRUE) == gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->icon_check))) { icon_change = TRUE; } Modified: branches/v2_0_0/src/gtkmain.c =================================================================== --- branches/v2_0_0/src/gtkmain.c 2006-08-17 02:40:44 UTC (rev 16804) +++ branches/v2_0_0/src/gtkmain.c 2006-08-17 03:13:15 UTC (rev 16805) @@ -650,7 +650,9 @@ * threadify dbus when that gets initialized. Ugh. */ if (!g_thread_supported()) g_thread_init(NULL); +#ifndef _WIN32 gdk_threads_init(); +#endif #endif /* Glib has threads */ gui_check = gtk_init_check(&argc, &argv); Modified: branches/v2_0_0/src/gtkstatusbox.c =================================================================== --- branches/v2_0_0/src/gtkstatusbox.c 2006-08-17 02:40:44 UTC (rev 16804) +++ branches/v2_0_0/src/gtkstatusbox.c 2006-08-17 03:13:15 UTC (rev 16805) @@ -78,6 +78,7 @@ static void gtk_gaim_status_box_forall (GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data); static void do_colorshift (GdkPixbuf *dest, GdkPixbuf *src, int shift); +static void icon_choose_cb(const char *filename, gpointer data); static void (*combo_box_size_request)(GtkWidget *widget, GtkRequisition *requisition); static void (*combo_box_size_allocate)(GtkWidget *widget, GtkAllocation *allocation); @@ -220,7 +221,65 @@ update_to_reflect_account_status(status_box, account, newstatus); } +static gboolean +icon_box_press_cb(GtkWidget *widget, GdkEventButton *event, GtkGaimStatusBox *box) +{ + if (box->buddy_icon_sel) { + gtk_window_present(GTK_WINDOW(box->buddy_icon_sel)); + return FALSE; + } + + box->buddy_icon_sel = gaim_gtk_buddy_icon_chooser_new(NULL, icon_choose_cb, box); + gtk_widget_show_all(box->buddy_icon_sel); + return FALSE; +} + +static gboolean +icon_box_enter_cb(GtkWidget *widget, GdkEventCrossing *event, GtkGaimStatusBox *box) +{ + gdk_window_set_cursor(widget->window, box->hand_cursor); + gtk_image_set_from_pixbuf(GTK_IMAGE(box->icon), box->buddy_icon_hover); + return FALSE; +} + +static gboolean +icon_box_leave_cb(GtkWidget *widget, GdkEventCrossing *event, GtkGaimStatusBox *box) +{ + gdk_window_set_cursor(widget->window, box->arrow_cursor); + gtk_image_set_from_pixbuf(GTK_IMAGE(box->icon), box->buddy_icon) ; + return FALSE; +} + static void +setup_icon_box(GtkGaimStatusBox *status_box) +{ + if (status_box->account && + !gaim_account_get_ui_bool(status_box->account, GAIM_GTK_UI, "use-global-buddyicon", TRUE)) + { + char *string = gaim_buddy_icons_get_full_path(gaim_account_get_buddy_icon(status_box->account)); + gtk_gaim_status_box_set_buddy_icon(status_box, string); + g_free(string); + } + else + { + gtk_gaim_status_box_set_buddy_icon(status_box, gaim_prefs_get_string("/gaim/gtk/accounts/buddyicon")); + } + status_box->icon = gtk_image_new_from_pixbuf(status_box->buddy_icon); + status_box->icon_box = gtk_event_box_new(); + status_box->hand_cursor = gdk_cursor_new (GDK_HAND2); + status_box->arrow_cursor = gdk_cursor_new (GDK_LEFT_PTR); + + g_signal_connect(G_OBJECT(status_box->icon_box), "enter-notify-event", G_CALLBACK(icon_box_enter_cb), status_box); + g_signal_connect(G_OBJECT(status_box->icon_box), "leave-notify-event", G_CALLBACK(icon_box_leave_cb), status_box); + g_signal_connect(G_OBJECT(status_box->icon_box), "button-press-event", G_CALLBACK(icon_box_press_cb), status_box); + + gtk_container_add(GTK_CONTAINER(status_box->icon_box), status_box->icon); + + gtk_widget_show_all(status_box->icon_box); + gtk_widget_set_parent(status_box->icon_box, GTK_WIDGET(status_box)); +} + +static void gtk_gaim_status_box_set_property(GObject *object, guint param_id, const GValue *value, GParamSpec *pspec) { @@ -236,10 +295,18 @@ statusbox->status_changed_signal = 0; } - if (statusbox->account) + if (statusbox->account) { + GaimPlugin *plug = gaim_plugins_find_with_id(gaim_account_get_protocol_id(statusbox->account)); + GaimPluginProtocolInfo *prplinfo = GAIM_PLUGIN_PROTOCOL_INFO(plug); + if (prplinfo && prplinfo->icon_spec.format != NULL) { + setup_icon_box(statusbox); + } statusbox->status_changed_signal = gaim_signal_connect(gaim_accounts_get_handle(), "account-status-changed", statusbox, GAIM_CALLBACK(account_status_changed_cb), statusbox); + } else { + setup_icon_box(statusbox); + } gtk_gaim_status_box_regenerate(statusbox); break; @@ -924,15 +991,33 @@ if (filename) { GList *accounts; - for (accounts = gaim_accounts_get_all(); accounts != NULL; accounts = accounts->next) { - GaimAccount *account = accounts->data; - if (gaim_account_get_ui_bool(account, GAIM_GTK_UI, "use-global-buddy-icon", TRUE) && - GAIM_PLUGIN_PROTOCOL_INFO(gaim_find_prpl(gaim_account_get_protocol_id(account)))->icon_spec.format) { - char *icon = gaim_gtk_convert_buddy_icon(gaim_find_prpl(gaim_account_get_protocol_id(account)), - filename); - gaim_account_set_buddy_icon(account, icon); - g_free(icon); + + if (box->account) { + GaimPlugin *plug = gaim_find_prpl(gaim_account_get_protocol_id(box->account)); + if (plug) { + GaimPluginProtocolInfo *prplinfo = GAIM_PLUGIN_PROTOCOL_INFO(plug); + if (prplinfo && prplinfo->icon_spec.format) { + char *icon = gaim_gtk_convert_buddy_icon(plug, filename); + gaim_account_set_buddy_icon(box->account, icon); + g_free(icon); + gaim_account_set_ui_bool(box->account, GAIM_GTK_UI, "use-global-buddyicon", FALSE); + } } + } else { + for (accounts = gaim_accounts_get_all(); accounts != NULL; accounts = accounts->next) { + GaimAccount *account = accounts->data; + GaimPlugin *plug = gaim_find_prpl(gaim_account_get_protocol_id(account)); + if (plug) { + GaimPluginProtocolInfo *prplinfo = GAIM_PLUGIN_PROTOCOL_INFO(plug); + if (prplinfo != NULL && + gaim_account_get_ui_bool(account, GAIM_GTK_UI, "use-global-buddyicon", TRUE) && + prplinfo->icon_spec.format) { + char *icon = gaim_gtk_convert_buddy_icon(plug, filename); + gaim_account_set_buddy_icon(account, icon); + g_free(icon); + } + } + } } gtk_gaim_status_box_set_buddy_icon(box, filename); } @@ -940,37 +1025,6 @@ box->buddy_icon_sel = NULL; } -static gboolean -icon_box_press_cb(GtkWidget *widget, GdkEventButton *event, GtkGaimStatusBox *box) -{ - GtkWidget *filesel; - - if (box->buddy_icon_sel) { - gtk_window_present(GTK_WINDOW(box->buddy_icon_sel)); - return FALSE; - } - - filesel = gaim_gtk_buddy_icon_chooser_new(NULL, icon_choose_cb, box); - gtk_widget_show_all(filesel); - return FALSE; -} - -static gboolean -icon_box_enter_cb(GtkWidget *widget, GdkEventCrossing *event, GtkGaimStatusBox *box) -{ - gdk_window_set_cursor(widget->window, box->hand_cursor); - gtk_image_set_from_pixbuf(GTK_IMAGE(box->icon), box->buddy_icon_hover); - return FALSE; -} - -static gboolean -icon_box_leave_cb(GtkWidget *widget, GdkEventCrossing *event, GtkGaimStatusBox *box) -{ - gdk_window_set_cursor(widget->window, box->arrow_cursor); - gtk_image_set_from_pixbuf(GTK_IMAGE(box->icon), box->buddy_icon) ; - return FALSE; -} - static void gtk_gaim_status_box_init (GtkGaimStatusBox *status_box) { @@ -987,18 +1041,6 @@ status_box->vsep = gtk_vseparator_new(); status_box->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE); - status_box->buddy_icon = gdk_pixbuf_new_from_file("/home/seanegan/p1120233.jpg", NULL); - status_box->icon = gtk_image_new_from_pixbuf(status_box->buddy_icon); - status_box->icon_box = gtk_event_box_new(); - status_box->hand_cursor = gdk_cursor_new (GDK_HAND2); - status_box->arrow_cursor = gdk_cursor_new (GDK_LEFT_PTR); - - g_signal_connect(G_OBJECT(status_box->icon_box), "enter-notify-event", G_CALLBACK(icon_box_enter_cb), status_box); - g_signal_connect(G_OBJECT(status_box->icon_box), "leave-notify-event", G_CALLBACK(icon_box_leave_cb), status_box); - g_signal_connect(G_OBJECT(status_box->icon_box), "button-press-event", G_CALLBACK(icon_box_press_cb), status_box); - - gtk_container_add(GTK_CONTAINER(status_box->icon_box), status_box->icon); - status_box->store = gtk_list_store_new(NUM_COLUMNS, G_TYPE_INT, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER); status_box->dropdown_store = gtk_list_store_new(NUM_COLUMNS, G_TYPE_INT, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER); gtk_combo_box_set_model(GTK_COMBO_BOX(status_box), GTK_TREE_MODEL(status_box->dropdown_store)); @@ -1011,7 +1053,6 @@ gtk_box_pack_start(GTK_BOX(status_box->hbox), status_box->vsep, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(status_box->hbox), status_box->arrow, FALSE, FALSE, 0); gtk_widget_show_all(status_box->toggle_button); - gtk_widget_show_all(status_box->icon_box); #if GTK_CHECK_VERSION(2,4,0) gtk_button_set_focus_on_click(GTK_BUTTON(status_box->toggle_button), FALSE); #endif @@ -1062,7 +1103,6 @@ #endif gtk_widget_set_parent(status_box->vbox, GTK_WIDGET(status_box)); gtk_widget_set_parent(status_box->toggle_button, GTK_WIDGET(status_box)); - gtk_widget_set_parent(status_box->icon_box, GTK_WIDGET(status_box)); GTK_BIN(status_box)->child = status_box->toggle_button; gtk_box_pack_start(GTK_BOX(status_box->vbox), status_box->sw, TRUE, TRUE, 0); @@ -1166,34 +1206,37 @@ parent_alc = *allocation; parent_alc.height = MAX(1,req.height); parent_alc.y += 3; - parent_alc.width -= (parent_alc.height + 3); - combo_box_size_allocate(widget, &parent_alc); - icon_alc = *allocation; - icon_alc.height = MAX(1,req.height); - icon_alc.width = icon_alc.height; - icon_alc.x = allocation->width - icon_alc.width; - icon_alc.y += 3; + if (status_box->icon_box) + { + parent_alc.width -= (parent_alc.height + 3); + icon_alc = *allocation; + icon_alc.height = MAX(1,req.height); + icon_alc.width = icon_alc.height; + icon_alc.x = allocation->width - icon_alc.width; + icon_alc.y += 3; - if (status_box->icon_size != icon_alc.height) - { - if ((status_box->buddy_icon_path != NULL) && - (*status_box->buddy_icon_path != '\0')) + if (status_box->icon_size != icon_alc.height) { - scaled = gdk_pixbuf_new_from_file_at_scale(status_box->buddy_icon_path, - icon_alc.height, icon_alc.width, FALSE, NULL); - status_box->buddy_icon_hover = gdk_pixbuf_copy(scaled); - do_colorshift(status_box->buddy_icon_hover, status_box->buddy_icon_hover, 30); - g_object_unref(status_box->buddy_icon); - status_box->buddy_icon = scaled; - gtk_image_set_from_pixbuf(GTK_IMAGE(status_box->icon), status_box->buddy_icon); + if ((status_box->buddy_icon_path != NULL) && + (*status_box->buddy_icon_path != '\0')) + { + scaled = gdk_pixbuf_new_from_file_at_scale(status_box->buddy_icon_path, + icon_alc.height, icon_alc.width, FALSE, NULL); + g_object_unref(status_box->buddy_icon_hover); + status_box->buddy_icon_hover = gdk_pixbuf_copy(scaled); + do_colorshift(status_box->buddy_icon_hover, status_box->buddy_icon_hover, 30); + g_object_unref(status_box->buddy_icon); + status_box->buddy_icon = scaled; + gtk_image_set_from_pixbuf(GTK_IMAGE(status_box->icon), status_box->buddy_icon); + } + status_box->icon_size = icon_alc.height; } - status_box->icon_size = icon_alc.height; + gtk_widget_size_allocate(status_box->icon_box, &icon_alc); } - gtk_widget_size_allocate((GTK_GAIM_STATUS_BOX(widget))->icon_box, &icon_alc); - - gtk_widget_size_allocate((GTK_GAIM_STATUS_BOX(widget))->toggle_button, &parent_alc); + combo_box_size_allocate(widget, &parent_alc); + gtk_widget_size_allocate(status_box->toggle_button, &parent_alc); widget->allocation = *allocation; } @@ -1221,7 +1264,8 @@ (* callback) (status_box->vbox, callback_data); (* callback) (status_box->toggle_button, callback_data); (* callback) (status_box->arrow, callback_data); - (* callback) (status_box->icon_box, callback_data); + if (status_box->icon_box) + (* callback) (status_box->icon_box, callback_data); } combo_box_forall(container, include_internals, callback, callback_data); @@ -1230,7 +1274,7 @@ GtkWidget * gtk_gaim_status_box_new() { - return g_object_new(GTK_GAIM_TYPE_STATUS_BOX, NULL); + return g_object_new(GTK_GAIM_TYPE_STATUS_BOX, "account", NULL, NULL); } GtkWidget * @@ -1351,7 +1395,8 @@ } } - gaim_prefs_set_string("/gaim/gtk/accounts/buddyicon", filename); + if (box->account == NULL) + gaim_prefs_set_string("/gaim/gtk/accounts/buddyicon", filename); } const char* Modified: branches/v2_0_0/src/protocols/silc/util.c =================================================================== --- branches/v2_0_0/src/protocols/silc/util.c 2006-08-17 02:40:44 UTC (rev 16804) +++ branches/v2_0_0/src/protocols/silc/util.c 2006-08-17 03:13:15 UTC (rev 16805) @@ -234,25 +234,44 @@ } #endif - fd = open(file_private_key, O_RDONLY); - if ((g_stat(file_private_key, &st)) == -1) { + if ((fd = g_open(file_private_key, O_RDONLY)) != -1) { + if ((fstat(fd, &st)) == -1) { + gaim_debug_error("silc", "Couldn't stat '%s' private key, error: %s\n", + file_private_key, strerror(errno)); + close(fd); + return FALSE; + } + } else if ((g_stat(file_private_key, &st)) == -1) { /* If file doesn't exist */ if (errno == ENOENT) { gaim_connection_update_progress(gc, _("Creating SILC key pair..."), 1, 5); - silc_create_key_pair(SILCGAIM_DEF_PKCS, + if (!silc_create_key_pair(SILCGAIM_DEF_PKCS, SILCGAIM_DEF_PKCS_LEN, file_public_key, file_private_key, NULL, (gc->password == NULL) ? "" : gc->password, - NULL, NULL, NULL, FALSE); - if (fd != -1) - close(fd); - fd = open(file_private_key, O_RDONLY); - g_stat(file_private_key, &st); + NULL, NULL, NULL, FALSE)) { + gaim_debug_error("silc", "Couldn't create key pair\n"); + return FALSE; + } + + if ((fd = g_open(file_private_key, O_RDONLY)) != -1) { + if ((fstat(fd, &st)) == -1) { + gaim_debug_error("silc", "Couldn't stat '%s' private key, error: %s\n", + file_private_key, strerror(errno)); + close(fd); + return FALSE; + } + } + /* This shouldn't really happen because silc_create_key_pair() + * will set the permissions */ + else if ((g_stat(file_private_key, &st)) == -1) { + gaim_debug_error("silc", "Couldn't stat '%s' private key, error: %s\n", + file_private_key, strerror(errno)); + return FALSE; + } } else { gaim_debug_error("silc", "Couldn't stat '%s' private key, error: %s\n", file_private_key, strerror(errno)); - if (fd != -1) - close(fd); return FALSE; } } @@ -270,7 +289,7 @@ if ((st.st_mode & 0777) != 0600) { gaim_debug_warning("silc", "Wrong permissions in your private key file `%s'!\n" "Trying to change them ...\n", file_private_key); - if ((fd != -1) && (fchmod(fd, S_IRUSR | S_IWUSR)) == -1) { + if ((fd == -1) || (fchmod(fd, S_IRUSR | S_IWUSR)) == -1) { gaim_debug_error("silc", "Failed to change permissions for private key file!\n" "Permissions for your private key file must be 0600.\n"); Modified: branches/v2_0_0/src/protocols/simple/simple.c =================================================================== --- branches/v2_0_0/src/protocols/simple/simple.c 2006-08-17 02:40:44 UTC (rev 16804) +++ branches/v2_0_0/src/protocols/simple/simple.c 2006-08-17 03:13:15 UTC (rev 16805) @@ -604,7 +604,7 @@ const gchar *body, struct sip_dialog *dialog, TransCallback tc) { struct simple_account_data *sip = gc->proto_data; char *callid = dialog ? g_strdup(dialog->callid) : gencallid(); - char *auth = ""; + char *auth = NULL; const char *addh = ""; gchar *branch = genbranch(); gchar *tag = NULL; @@ -662,7 +662,7 @@ ++sip->cseq, method, callid, - auth, + auth ? auth : "", addh, strlen(body), body); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <the...@us...> - 2006-08-17 08:42:03
|
Revision: 16812 Author: thekingant Date: 2006-08-17 01:41:44 -0700 (Thu, 17 Aug 2006) ViewCVS: http://svn.sourceforge.net/gaim/?rev=16812&view=rev Log Message: ----------- Backport SVN revisions 16807-16811 from HEAD to v2_0_0 Original commit messages: 16807: This function was used by the chat userlist buttons, which are gone 16808: gaim_ssl_connect's are now cancelable (without crashing, anyway) This was relatively easy, because the PRPLs already keep a reference to the GaimSslConnection. I just needed to update the core ssl code to keep track of the GaimProxyConnectInfo, and to call gaim_proxy_connect_cancel() when gaim_ssl_close() is called 16809: gaim_ssl_connect's are now cancelable (without crashing, anyway) This was relatively easy, because the PRPLs already keep a reference to the GaimSslConnection. I just needed to update the core ssl code to keep track of the GaimProxyConnectInfo, and to call gaim_proxy_connect_cancel() when gaim_ssl_close() is called 16810: sf patch #1541754, from Sadrul Habib Chowdhury "Setting an account to 'offline' while it is connecting doesn't cause the connection to stop. This patch fixes that." 16811: Split the DNS query stuff out into it's own file. Eventually we should move the dnssrv code into this same file. Maybe even share some code? Also the first steps toward cancelable DNS queries. Modified Paths: -------------- branches/v2_0_0/plugins/ssl/ssl-gnutls.c branches/v2_0_0/plugins/ssl/ssl-nss.c branches/v2_0_0/src/Makefile.am branches/v2_0_0/src/Makefile.mingw branches/v2_0_0/src/gtkconv.c branches/v2_0_0/src/protocols/irc/irc.c branches/v2_0_0/src/protocols/simple/simple.c branches/v2_0_0/src/proxy.c branches/v2_0_0/src/proxy.h branches/v2_0_0/src/prpl.c branches/v2_0_0/src/sslconn.c branches/v2_0_0/src/sslconn.h branches/v2_0_0/src/stun.c Added Paths: ----------- branches/v2_0_0/src/dnsquery.c branches/v2_0_0/src/dnsquery.h Modified: branches/v2_0_0/plugins/ssl/ssl-gnutls.c =================================================================== --- branches/v2_0_0/plugins/ssl/ssl-gnutls.c 2006-08-17 07:44:52 UTC (rev 16811) +++ branches/v2_0_0/plugins/ssl/ssl-gnutls.c 2006-08-17 08:41:44 UTC (rev 16812) @@ -100,27 +100,11 @@ static void -ssl_gnutls_connect_cb(gpointer data, gint source, GaimInputCondition cond) +ssl_gnutls_connect(GaimSslConnection *gsc) { - GaimSslConnection *gsc = (GaimSslConnection *)data; GaimSslGnutlsData *gnutls_data; static const int cert_type_priority[2] = { GNUTLS_CRT_X509, 0 }; - /* - * TODO: Uh, this needs to somehow check to make sure that gsc is - * still valid before actually doing anything. - */ - - if(source < 0) { - if(gsc->error_cb != NULL) - gsc->error_cb(gsc, GAIM_SSL_CONNECT_FAILED, gsc->connect_cb_data); - - gaim_ssl_close(gsc); - return; - } - - gsc->fd = source; - gnutls_data = g_new0(GaimSslGnutlsData, 1); gsc->private_data = gnutls_data; @@ -133,7 +117,7 @@ gnutls_credentials_set(gnutls_data->session, GNUTLS_CRD_CERTIFICATE, xcred); - gnutls_transport_set_ptr(gnutls_data->session, GINT_TO_POINTER(source)); + gnutls_transport_set_ptr(gnutls_data->session, GINT_TO_POINTER(gsc->fd)); gnutls_data->handshake_handler = gaim_input_add(gsc->fd, GAIM_INPUT_READ, ssl_gnutls_handshake_cb, gsc); @@ -204,7 +188,7 @@ { ssl_gnutls_init, ssl_gnutls_uninit, - ssl_gnutls_connect_cb, + ssl_gnutls_connect, ssl_gnutls_close, ssl_gnutls_read, ssl_gnutls_write Modified: branches/v2_0_0/plugins/ssl/ssl-nss.c =================================================================== --- branches/v2_0_0/plugins/ssl/ssl-nss.c 2006-08-17 07:44:52 UTC (rev 16811) +++ branches/v2_0_0/plugins/ssl/ssl-nss.c 2006-08-17 08:41:44 UTC (rev 16812) @@ -237,16 +237,13 @@ } static void -ssl_nss_connect_cb(gpointer data, gint source, GaimInputCondition cond) +ssl_nss_connect(GaimSslConnection *gsc) { - GaimSslConnection *gsc = (GaimSslConnection *)data; GaimSslNssData *nss_data = g_new0(GaimSslNssData, 1); PRSocketOptionData socket_opt; gsc->private_data = nss_data; - gsc->fd = source; - nss_data->fd = PR_ImportTCPSocket(gsc->fd); if (nss_data->fd == NULL) @@ -359,7 +356,7 @@ { ssl_nss_init, ssl_nss_uninit, - ssl_nss_connect_cb, + ssl_nss_connect, ssl_nss_close, ssl_nss_read, ssl_nss_write Modified: branches/v2_0_0/src/Makefile.am =================================================================== --- branches/v2_0_0/src/Makefile.am 2006-08-17 07:44:52 UTC (rev 16811) +++ branches/v2_0_0/src/Makefile.am 2006-08-17 08:41:44 UTC (rev 16812) @@ -77,6 +77,7 @@ core.c \ debug.c \ desktopitem.c \ + dnsquery.c \ eventloop.c \ ft.c \ idle.c \ @@ -125,6 +126,7 @@ dbus-maybe.h \ debug.h \ desktopitem.h \ + dnsquery.h \ eventloop.h \ ft.h \ idle.h \ Modified: branches/v2_0_0/src/Makefile.mingw =================================================================== --- branches/v2_0_0/src/Makefile.mingw 2006-08-17 07:44:52 UTC (rev 16811) +++ branches/v2_0_0/src/Makefile.mingw 2006-08-17 08:41:44 UTC (rev 16812) @@ -95,6 +95,7 @@ conversation.c \ core.c \ debug.c \ + dnsquery.c \ dnssrv.c \ eventloop.c \ ft.c \ Copied: branches/v2_0_0/src/dnsquery.c (from rev 16811, trunk/src/dnsquery.c) =================================================================== --- branches/v2_0_0/src/dnsquery.c (rev 0) +++ branches/v2_0_0/src/dnsquery.c 2006-08-17 08:41:44 UTC (rev 16812) @@ -0,0 +1,774 @@ +/** + * @file dnsquery.c DNS query API + * @ingroup core + * + * gaim + * + * Gaim is the legal property of its developers, whose names are too numerous + * to list here. Please refer to the COPYRIGHT file distributed with this + * source distribution. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include "internal.h" +#include "debug.h" +#include "ntlm.h" +#include "util.h" + +struct _GaimDnsqueryData { +}; + +/************************************************************************** + * Global DNS query API + **************************************************************************/ + +#if defined(__unix__) || defined(__APPLE__) + +/* + * This structure represents both a pending DNS request and + * a free child process. + */ +typedef struct _DnsqueryResolverprocess DnsqueryResolverprocess; +struct _DnsqueryResolverprocess { + char *host; + int port; + GaimProxyDnsConnectFunction callback; + gpointer data; + guint inpa; + int fd_in, fd_out; + pid_t dns_pid; +}; + +//static GSList *all_dns_children = NULL; +static GSList *free_dns_children = NULL; +static GQueue *queued_requests = NULL; + +static int number_of_dns_children = 0; + +static const int MAX_DNS_CHILDREN = 2; + +typedef struct { + char hostname[512]; + int port; +} dns_params_t; + +typedef struct { + dns_params_t params; + GaimProxyDnsConnectFunction callback; + gpointer data; +} queued_dns_request_t; + +/* + * Begin the DNS resolver child process functions. + */ +#ifdef HAVE_SIGNAL_H +static void +trap_gdb_bug() +{ + const char *message = + "Gaim's DNS child got a SIGTRAP signal.\n" + "This can be caused by trying to run gaim inside gdb.\n" + "There is a known gdb bug which prevents this. Supposedly gaim\n" + "should have detected you were using gdb and used an ugly hack,\n" + "check cope_with_gdb_brokenness() in dnsquery.c.\n\n" + "For more info about this bug, see http://sources.redhat.com/ml/gdb/2001-07/msg00349.html\n"; + fputs("\n* * *\n",stderr); + fputs(message,stderr); + fputs("* * *\n\n",stderr); + execlp("xmessage","xmessage","-center", message, NULL); + _exit(1); +} +#endif + +static void +cope_with_gdb_brokenness() +{ +#ifdef __linux__ + static gboolean already_done = FALSE; + char s[256], e[512]; + int n; + pid_t ppid; + + if (already_done) + return; + + already_done = TRUE; + ppid = getppid(); + snprintf(s, sizeof(s), "/proc/%d/exe", ppid); + n = readlink(s, e, sizeof(e)); + if (n < 0) + return; + + e[MIN(n,sizeof(e)-1)] = '\0'; + + if (strstr(e,"gdb")) + { + gaim_debug_info("dnsquery", + "Debugger detected, performing useless query...\n"); + gethostbyname("x.x.x.x.x"); + } +#endif +} + +/** + * When doing DNS queries on Unix and OS-X, we fork off a separate + * process and communicate with it using pipes. This function is + * called shortly after the child is forked, and the function exits + * the process after it's no longer needed. + */ +static void +gaim_dns_resolverprocess(int child_out, int child_in, gboolean show_debug) +{ + dns_params_t dns_params; + const size_t zero = 0; + int rc; +#ifdef HAVE_GETADDRINFO + struct addrinfo hints, *res, *tmp; + char servname[20]; +#else + struct sockaddr_in sin; + const size_t addrlen = sizeof(sin); +#endif + +#ifdef HAVE_SIGNAL_H + signal(SIGHUP, SIG_DFL); + signal(SIGINT, SIG_DFL); + signal(SIGQUIT, SIG_DFL); + signal(SIGCHLD, SIG_DFL); + signal(SIGTERM, SIG_DFL); + signal(SIGTRAP, trap_gdb_bug); +#endif + + /* + * We resolve 1 host name for each iteration of this + * while loop. + * + * The top half of this reads in the hostname and port + * number from the socket with our parent. The bottom + * half of this resolves the IP (blocking) and sends + * the result back to our parent, when finished. + */ + while (1) + { + const char ch = 'Y'; + fd_set fds; + struct timeval tv = { .tv_sec = 40 , .tv_usec = 0 }; + + FD_ZERO(&fds); + FD_SET(child_in, &fds); + rc = select(child_in + 1, &fds, NULL, NULL, &tv); + if (rc < 0) { + if (show_debug) + printf("dns[%d]: select failed\n", getpid()); + close(child_out); + close(child_in); + _exit(1); + } + if (rc == 0) { + if (show_debug) + printf("dns[%d]: nobody needs me... =(\n", getpid()); + break; + } + + rc = read(child_in, &dns_params, sizeof(dns_params_t)); + if (rc < 0) { + if (show_debug) + printf("dns[%d]: read failed\n", getpid()); + close(child_out); + close(child_in); + _exit(1); + } + if (rc == 0) { + if (show_debug) + printf("dns[%d]: Oops, father has gone, wait for me, wait...!\n", getpid()); + break; + } + if (dns_params.hostname[0] == '\0') { + printf("dns[%d]: hostname = \"\" (port = %d)!!!\n", getpid(), dns_params.port); + close(child_out); + close(child_in); + _exit(1); + } + /* Tell our parent that we read the data successfully */ + write(child_out, &ch, sizeof(ch)); + + /* We have the hostname and port, now resolve the IP */ + +#ifdef HAVE_GETADDRINFO + g_snprintf(servname, sizeof(servname), "%d", dns_params.port); + memset(&hints, 0, sizeof(hints)); + + /* + * This is only used to convert a service + * name to a port number. As we know we are + * passing a number already, we know this + * value will not be really used by the C + * library. + */ + hints.ai_socktype = SOCK_STREAM; + rc = getaddrinfo(dns_params.hostname, servname, &hints, &res); + write(child_out, &rc, sizeof(rc)); + if (rc != 0) { + close(child_out); + if (show_debug) + printf("dns[%d] Error: getaddrinfo returned %d\n", + getpid(), rc); + dns_params.hostname[0] = '\0'; + continue; + } + for (tmp = res; tmp != NULL; tmp = tmp->ai_next) + { + size_t ai_addrlen = tmp->ai_addrlen; + write(child_out, &ai_addrlen, sizeof(ai_addrlen)); + write(child_out, tmp->ai_addr, tmp->ai_addrlen); + } + freeaddrinfo(res); + write(child_out, &zero, sizeof(zero)); +#else + if (!inet_aton(dns_params.hostname, &sin.sin_addr)) + { + struct hostent *hp; + + hp = gethostbyname(dns_params.hostname); + if (hp == NULL) + { + if (show_debug) + printf("DNS Error: %d\n", h_errno); + write(child_out, &h_errno, sizeof(int)); + close(child_out); + close(child_in); + _exit(1); + } + memset(&sin, 0, sizeof(struct sockaddr_in)); + memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); + sin.sin_family = hp->h_addrtype; + } else + sin.sin_family = AF_INET; + + sin.sin_port = htons(dns_params.port); + write(child_out, &addrlen, sizeof(addrlen)); + write(child_out, &sin, addrlen); + write(child_out, &zero, sizeof(zero)); +#endif + dns_params.hostname[0] = '\0'; + } + + close(child_out); + close(child_in); + + _exit(0); +} + +static DnsqueryResolverprocess * +gaim_dns_new_resolverprocess(gboolean show_debug) +{ + DnsqueryResolverprocess *req; + int child_out[2], child_in[2]; + + /* Create pipes for communicating with the child process */ + if (pipe(child_out) || pipe(child_in)) { + gaim_debug_error("dnsquery", + "Could not create pipes: %s\n", strerror(errno)); + return NULL; + } + + req = g_new(DnsqueryResolverprocess, 1); + + cope_with_gdb_brokenness(); + + /* Fork! */ + req->dns_pid = fork(); + + /* If we are the child process... */ + if (req->dns_pid == 0) { + /* We should not access the parent's side of the pipes, so close them */ + close(child_out[0]); + close(child_in[1]); + + gaim_dns_resolverprocess(child_out[1], child_in[0], show_debug); + /* The thread calls _exit() rather than returning, so we never get here */ + } + + /* We should not access the child's side of the pipes, so close them */ + close(child_out[1]); + close(child_in[0]); + if (req->dns_pid == -1) { + gaim_debug_error("dnsquery", + "Could not create child process for DNS: %s\n", + strerror(errno)); + g_free(req); + return NULL; + } + + req->fd_out = child_out[0]; + req->fd_in = child_in[1]; + number_of_dns_children++; + gaim_debug_info("dnsquery", + "Created new DNS child %d, there are now %d children.\n", + req->dns_pid, number_of_dns_children); + + return req; +} +/* + * End the DNS resolver child process functions. + */ + +/* + * Begin the functions for dealing with the DNS child processes. + */ +static void +req_free(DnsqueryResolverprocess *req) +{ + g_return_if_fail(req != NULL); + + close(req->fd_in); + close(req->fd_out); + + g_free(req->host); + g_free(req); + + number_of_dns_children--; +} + +static int +send_dns_request_to_child(DnsqueryResolverprocess *req, dns_params_t *dns_params) +{ + char ch; + int rc; + pid_t pid; + + /* This waitpid might return the child's PID if it has recently + * exited, or it might return an error if it exited "long + * enough" ago that it has already been reaped; in either + * instance, we can't use it. */ + if ((pid = waitpid (req->dns_pid, NULL, WNOHANG)) > 0) { + gaim_debug_warning("dnsquery", + "DNS child %d no longer exists\n", req->dns_pid); + return -1; + } else if (pid < 0) { + gaim_debug_warning("dnsquery", + "Wait for DNS child %d failed: %s\n", + req->dns_pid, strerror(errno)); + return -1; + } + + /* Let's contact this lost child! */ + rc = write(req->fd_in, dns_params, sizeof(*dns_params)); + if (rc < 0) { + gaim_debug_error("dnsquery", + "Unable to write to DNS child %d: %d\n", + req->dns_pid, strerror(errno)); + close(req->fd_in); + return -1; + } + + g_return_val_if_fail(rc == sizeof(*dns_params), -1); + + /* Did you hear me? (This avoids some race conditions) */ + rc = read(req->fd_out, &ch, sizeof(ch)); + if (rc != 1 || ch != 'Y') + { + gaim_debug_warning("dnsquery", + "DNS child %d not responding. Killing it!\n", + req->dns_pid); + kill(req->dns_pid, SIGKILL); + return -1; + } + + gaim_debug_info("dnsquery", + "Successfully sent DNS request to child %d\n", req->dns_pid); + + return 0; +} + +static void +host_resolved(gpointer data, gint source, GaimInputCondition cond); + +static void +release_dns_child(DnsqueryResolverprocess *req) +{ + g_free(req->host); + req->host = NULL; + + if (queued_requests && !g_queue_is_empty(queued_requests)) { + queued_dns_request_t *r = g_queue_pop_head(queued_requests); + req->host = g_strdup(r->params.hostname); + req->port = r->params.port; + req->callback = r->callback; + req->data = r->data; + + gaim_debug_info("dnsquery", + "Processing queued DNS query for '%s' with child %d\n", + req->host, req->dns_pid); + + if (send_dns_request_to_child(req, &(r->params)) != 0) { + req_free(req); + req = NULL; + + gaim_debug_warning("dnsquery", + "Intent of process queued query of '%s' failed, " + "requeueing...\n", r->params.hostname); + g_queue_push_head(queued_requests, r); + } else { + req->inpa = gaim_input_add(req->fd_out, GAIM_INPUT_READ, host_resolved, req); + g_free(r); + } + + } else { + req->host = NULL; + req->callback = NULL; + req->data = NULL; + free_dns_children = g_slist_append(free_dns_children, req); + } +} + +static void +host_resolved(gpointer data, gint source, GaimInputCondition cond) +{ + DnsqueryResolverprocess *req = (DnsqueryResolverprocess*)data; + int rc, err; + GSList *hosts = NULL; + struct sockaddr *addr = NULL; + size_t addrlen; + + gaim_debug_info("dnsquery", "Got response for '%s'\n", req->host); + gaim_input_remove(req->inpa); + + rc = read(req->fd_out, &err, sizeof(err)); + if ((rc == 4) && (err != 0)) + { + gchar *message; +#ifdef HAVE_GETADDRINFO + message = g_strdup_printf("DNS error: %s (pid=%d)", + gai_strerror(err), req->dns_pid); +#else + message = g_strdup_printf("DNS error: %d (pid=%d)", + err, req->dns_pid); +#endif + gaim_debug_error("dnsquery", "%s\n", message); + req->callback(NULL, req->data, message); + g_free(message); + release_dns_child(req); + return; + } + + if (rc > 0) + { + while (rc > 0) { + rc = read(req->fd_out, &addrlen, sizeof(addrlen)); + if (rc > 0 && addrlen > 0) { + addr = g_malloc(addrlen); + rc = read(req->fd_out, addr, addrlen); + hosts = g_slist_append(hosts, GINT_TO_POINTER(addrlen)); + hosts = g_slist_append(hosts, addr); + } else { + break; + } + } + } else if (rc == -1) { + gchar *message; + message = g_strdup_printf("Error reading from DNS child: %s", strerror(errno)); + gaim_debug_error("dnsquery", "%s\n", message); + req->callback(NULL, req->data, message); + g_free(message); + req_free(req); + return; + } else if (rc == 0) { + gchar *message; + close(req->fd_out); + message = g_strdup_printf("EOF reading from DNS child"); + gaim_debug_error("dnsquery", "%s\n", message); + req->callback(NULL, req->data, message); + g_free(message); + req_free(req); + return; + } + +/* wait4(req->dns_pid, NULL, WNOHANG, NULL); */ + + req->callback(hosts, req->data, NULL); + + release_dns_child(req); +} +/* + * End the functions for dealing with the DNS child processes. + */ + +GaimDnsqueryData * +gaim_dnsquery_a(const char *hostname, int port, GaimDnsqueryConnectFunction callback, gpointer data) +{ + GaimDnsqueryData *query_data; + DnsqueryResolverprocess *req = NULL; + dns_params_t dns_params; + gchar *host_temp; + gboolean show_debug; + + show_debug = gaim_debug_is_enabled(); + + host_temp = g_strstrip(g_strdup(hostname)); + strncpy(dns_params.hostname, host_temp, sizeof(dns_params.hostname) - 1); + g_free(host_temp); + dns_params.hostname[sizeof(dns_params.hostname) - 1] = '\0'; + dns_params.port = port; + + /* + * If we have any children, attempt to have them perform the DNS + * query. If we're able to send the query to a child, then req + * will be set to the DnsqueryResolverprocess. Otherwise, req will + * be NULL and we'll need to create a new DNS request child. + */ + while (free_dns_children != NULL) + { + req = free_dns_children->data; + free_dns_children = g_slist_remove(free_dns_children, req); + + if (send_dns_request_to_child(req, &dns_params) == 0) + /* We found an acceptable child, yay */ + break; + + req_free(req); + req = NULL; + } + + /* We need to create a new DNS request child */ + if (req == NULL) + { + if (number_of_dns_children >= MAX_DNS_CHILDREN) + { + queued_dns_request_t *r = g_new(queued_dns_request_t, 1); + memcpy(&(r->params), &dns_params, sizeof(dns_params)); + r->callback = callback; + r->data = data; + if (!queued_requests) + queued_requests = g_queue_new(); + g_queue_push_tail(queued_requests, r); + + gaim_debug_info("dnsquery", + "DNS query for '%s' queued\n", dns_params.hostname); + + return query_data; + } + + req = gaim_dns_new_resolverprocess(show_debug); + if (req == NULL) + { + gaim_debug_error("dnsquery", "oh dear, this is going to explode, I give up\n"); + return NULL; + } + send_dns_request_to_child(req, &dns_params); + } + + req->host = g_strdup(hostname); + req->port = port; + req->callback = callback; + req->data = data; + req->inpa = gaim_input_add(req->fd_out, GAIM_INPUT_READ, host_resolved, req); + + return query_data; +} + +#elif defined _WIN32 /* end __unix__ || __APPLE__ */ + +typedef struct _dns_tdata { + char *hostname; + int port; + GaimProxyDnsConnectFunction callback; + gpointer data; + GSList *hosts; + char *errmsg; +} dns_tdata; + +static gboolean dns_main_thread_cb(gpointer data) +{ + dns_tdata *td = (dns_tdata*)data; + if (td->errmsg != NULL) { + gaim_debug_info("dnsquery", "%s\n", td->errmsg); + } + td->callback(td->hosts, td->data, td->errmsg); + g_free(td->hostname); + g_free(td->errmsg); + g_free(td); + return FALSE; +} + +static gpointer +dns_thread(gpointer data) +{ + +#ifdef HAVE_GETADDRINFO + int rc; + struct addrinfo hints, *res, *tmp; + char servname[20]; +#else + struct sockaddr_in sin; + struct hostent *hp; +#endif + dns_tdata *td = (dns_tdata*)data; + +#ifdef HAVE_GETADDRINFO + g_snprintf(servname, sizeof(servname), "%d", td->port); + memset(&hints, 0, sizeof(hints)); + + /* This is only used to convert a service + * name to a port number. As we know we are + * passing a number already, we know this + * value will not be really used by the C + * library. + */ + hints.ai_socktype = SOCK_STREAM; + rc = getaddrinfo(td->hostname, servname, &hints, &res); + if (rc == 0) + { + tmp = res; + while(res) { + td->hosts = g_slist_append(td->hosts, + GSIZE_TO_POINTER(res->ai_addrlen)); + td->hosts = g_slist_append(td->hosts, + g_memdup(res->ai_addr, res->ai_addrlen)); + res = res->ai_next; + } + freeaddrinfo(tmp); + } + else + { + td->errmsg = g_strdup_printf("DNS getaddrinfo(\"%s\", \"%s\") error: %d", td->hostname, servname, rc); + } +#else + hp = gethostbyname(td->hostname); + if (hp != NULL) + { + memset(&sin, 0, sizeof(struct sockaddr_in)); + memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); + sin.sin_family = hp->h_addrtype; + sin.sin_port = htons(td->port); + + td->hosts = g_slist_append(td->hosts, + GSIZE_TO_POINTER(sizeof(sin))); + td->hosts = g_slist_append(td->hosts, + g_memdup(&sin, sizeof(sin))); + } else { + td->errmsg = g_strdup_printf("DNS gethostbyname(\"%s\") error: %d", td->hostname, h_errno); + } +#endif + /* back to main thread */ + g_idle_add(dns_main_thread_cb, td); + + return 0; +} + +GaimDnsqueryData * +gaim_dnsquery_a(const char *hostname, int port, + GaimProxyDnsConnectFunction callback, gpointer data) +{ + GaimDnsqueryData *query_data; + dns_tdata *td; + struct sockaddr_in sin; + GError* err = NULL; + + if (inet_aton(hostname, &sin.sin_addr)) + { + GSList *hosts = NULL; + sin.sin_family = AF_INET; + sin.sin_port = htons(port); + hosts = g_slist_append(hosts, GINT_TO_POINTER(sizeof(sin))); + hosts = g_slist_append(hosts, g_memdup(&sin, sizeof(sin))); + callback(hosts, data, NULL); + return query_data; + } + + gaim_debug_info("dnsquery", "DNS Lookup for: %s\n", hostname); + td = g_new0(dns_tdata, 1); + td->hostname = g_strdup(hostname); + td->port = port; + td->callback = callback; + td->data = data; + + if (!g_thread_create(dns_thread, td, FALSE, &err)) + { + gaim_debug_error("dnsquery", "DNS thread create failure: %s\n", err?err->message:""); + g_error_free(err); + g_free(td->hostname); + g_free(td); + return NULL; + } + + return query_data; +} + +#else /* not __unix__ or __APPLE__ or _WIN32 */ + +typedef struct { + gpointer data; + size_t addrlen; + struct sockaddr *addr; + GaimProxyDnsConnectFunction callback; +} DnsqueryResolverprocess; + +static gboolean host_resolved(gpointer data) +{ + DnsqueryResolverprocess *req = (DnsqueryResolverprocess*)data; + GSList *hosts = NULL; + hosts = g_slist_append(hosts, GINT_TO_POINTER(req->addrlen)); + hosts = g_slist_append(hosts, req->addr); + req->callback(hosts, req->data, NULL); + g_free(req); + return FALSE; +} + +GaimDnsqueryData * +gaim_dnsquery_a(const char *hostname, int port, + GaimProxyDnsConnectFunction callback, gpointer data) +{ + GaimDnsqueryData *query_data; + struct sockaddr_in sin; + DnsqueryResolverprocess *req; + + if (!inet_aton(hostname, &sin.sin_addr)) + { + struct hostent *hp; + hp = gethostbyname(hostname); + if (hp == NULL) + { + gaim_debug_error("dnsquery", + "gaim_gethostbyname(\"%s\", %d) failed: %d\n", + hostname, port, h_errno); + return NULL; + } + memset(&sin, 0, sizeof(struct sockaddr_in)); + memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); + sin.sin_family = hp->h_addrtype; + } else + sin.sin_family = AF_INET; + sin.sin_port = htons(port); + + req = g_new(DnsqueryResolverprocess, 1); + req->addr = (struct sockaddr*) g_memdup(&sin, sizeof(sin)); + req->addrlen = sizeof(sin); + req->data = data; + req->callback = callback; + gaim_timeout_add(10, host_resolved, req); + + return query_data; +} + +#endif /* not __unix__ or __APPLE__ or _WIN32 */ + +void +gaim_dnsquery_destroy(GaimDnsqueryData *query_data) +{ + g_free(query_data); +} Copied: branches/v2_0_0/src/dnsquery.h (from rev 16811, trunk/src/dnsquery.h) =================================================================== --- branches/v2_0_0/src/dnsquery.h (rev 0) +++ branches/v2_0_0/src/dnsquery.h 2006-08-17 08:41:44 UTC (rev 16812) @@ -0,0 +1,76 @@ +/** + * @file dnsquery.h DNS query API + * @ingroup core + * + * gaim + * + * Gaim is the legal property of its developers, whose names are too numerous + * to list here. Please refer to the COPYRIGHT file distributed with this + * source distribution. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifndef _GAIM_DNSQUERY_H_ +#define _GAIM_DNSQUERY_H_ + +#include <glib.h> + +typedef struct _GaimDnsqueryData GaimDnsqueryData; + +/** + * The "hosts" parameter is a linked list containing pairs of + * one size_t addrlen and one struct sockaddr *addr. + */ +typedef void (*GaimDnsqueryConnectFunction)(GSList *hosts, gpointer data, const char *error_message); + + +#ifdef __cplusplus +extern "C" { +#endif + +/**************************************************************************/ +/** @name DNS query API */ +/**************************************************************************/ +/*@{*/ + +/** + * Do an asynchronous DNS query. + * + * @param hostname The hostname to resolve + * @param port A portnumber which is stored in the struct sockaddr + * @param callback Callback to call after resolving + * @param data Extra data for the callback function + * + * @return NULL if there was an error, or a reference to a data + * structure that can be used to cancel the pending + * connection, if needed. + */ +GaimDnsqueryData *gaim_dnsquery_a(const char *hostname, int port, GaimDnsqueryConnectFunction callback, gpointer data); + +/** + * Cancel a DNS query. + * + * @param query_data A pointer to the DNS query data that you want + * to cancel. + */ +void gaim_dnsquery_destroy(GaimDnsqueryData *query_data); + +/*@}*/ + +#ifdef __cplusplus +} +#endif + +#endif /* _GAIM_DNSQUERY_H_ */ Modified: branches/v2_0_0/src/gtkconv.c =================================================================== --- branches/v2_0_0/src/gtkconv.c 2006-08-17 07:44:52 UTC (rev 16811) +++ branches/v2_0_0/src/gtkconv.c 2006-08-17 08:41:44 UTC (rev 16812) @@ -1360,29 +1360,6 @@ } static void -chat_im_button_cb(GtkWidget *widget, GaimGtkConversation *gtkconv) -{ - GaimGtkChatPane *gtkchat; - GtkTreeIter iter; - GtkTreeModel *model; - GtkTreeSelection *sel; - char *name; - - gtkchat = gtkconv->u.chat; - - model = gtk_tree_view_get_model(GTK_TREE_VIEW(gtkchat->list)); - sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(gtkchat->list)); - - if (gtk_tree_selection_get_selected(sel, NULL, &iter)) - gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, CHAT_USERS_NAME_COLUMN, &name, -1); - else - return; - - chat_do_im(gtkconv, name); - g_free(name); -} - -static void ignore_cb(GtkWidget *w, GaimGtkConversation *gtkconv) { GaimConversation *conv = gtkconv->active_conv; Modified: branches/v2_0_0/src/protocols/irc/irc.c =================================================================== --- branches/v2_0_0/src/protocols/irc/irc.c 2006-08-17 07:44:52 UTC (rev 16811) +++ branches/v2_0_0/src/protocols/irc/irc.c 2006-08-17 08:41:44 UTC (rev 16812) @@ -450,7 +450,7 @@ g_free(irc->inbuf); if (irc->gsc) { gaim_ssl_close(irc->gsc); - } else if (irc->fd > 0) { + } else if (irc->fd >= 0) { close(irc->fd); } if (irc->timer) Modified: branches/v2_0_0/src/protocols/simple/simple.c =================================================================== --- branches/v2_0_0/src/protocols/simple/simple.c 2006-08-17 07:44:52 UTC (rev 16811) +++ branches/v2_0_0/src/protocols/simple/simple.c 2006-08-17 08:41:44 UTC (rev 16812) @@ -30,6 +30,7 @@ #include "blist.h" #include "conversation.h" #include "debug.h" +#include "dnsquery.h" #include "notify.h" #include "privacy.h" #include "prpl.h" @@ -1621,7 +1622,7 @@ } else { /* UDP */ gaim_debug_info("simple", "using udp with server %s and port %d\n", hostname, port); - gaim_gethostbyname_async(hostname, port, simple_udp_host_resolved, sip); + gaim_dnsquery_a(hostname, port, simple_udp_host_resolved, sip); } } Modified: branches/v2_0_0/src/proxy.c =================================================================== --- branches/v2_0_0/src/proxy.c 2006-08-17 07:44:52 UTC (rev 16811) +++ branches/v2_0_0/src/proxy.c 2006-08-17 08:41:44 UTC (rev 16812) @@ -31,6 +31,7 @@ #include "internal.h" #include "cipher.h" +#include "dnsquery.h" #include "debug.h" #include "notify.h" #include "ntlm.h" @@ -46,6 +47,7 @@ int fd; guint inpa; GaimProxyInfo *gpi; + GaimDnsqueryData *query_data; /** * This contains alternating length/char* values. The char* @@ -307,6 +309,11 @@ connect_infos = g_slist_remove(connect_infos, connect_info); + /* + if (connect_info->query_data != NULL) + gaim_dnsquery_destroy(connect_info->query_data); + */ + while (connect_info->hosts != NULL) { /* Discard the length... */ @@ -351,681 +358,7 @@ gaim_proxy_connect_info_destroy(connect_info); } -#if defined(__unix__) || defined(__APPLE__) - -/* - * This structure represents both a pending DNS request and - * a free child process. - */ -typedef struct { - char *host; - int port; - GaimProxyDnsConnectFunction callback; - gpointer data; - guint inpa; - int fd_in, fd_out; - pid_t dns_pid; -} pending_dns_request_t; - -static GSList *free_dns_children = NULL; -static GQueue *queued_requests = NULL; - -static int number_of_dns_children = 0; - -static const int MAX_DNS_CHILDREN = 2; - -typedef struct { - char hostname[512]; - int port; -} dns_params_t; - -typedef struct { - dns_params_t params; - GaimProxyDnsConnectFunction callback; - gpointer data; -} queued_dns_request_t; - -/* - * Begin the DNS resolver child process functions. - */ -#ifdef HAVE_SIGNAL_H static void -trap_gdb_bug() -{ - const char *message = - "Gaim's DNS child got a SIGTRAP signal.\n" - "This can be caused by trying to run gaim inside gdb.\n" - "There is a known gdb bug which prevents this. Supposedly gaim\n" - "should have detected you were using gdb and used an ugly hack,\n" - "check cope_with_gdb_brokenness() in proxy.c.\n\n" - "For more info about this bug, see http://sources.redhat.com/ml/gdb/2001-07/msg00349.html\n"; - fputs("\n* * *\n",stderr); - fputs(message,stderr); - fputs("* * *\n\n",stderr); - execlp("xmessage","xmessage","-center", message, NULL); - _exit(1); -} -#endif - -static void -cope_with_gdb_brokenness() -{ -#ifdef __linux__ - static gboolean already_done = FALSE; - char s[256], e[512]; - int n; - pid_t ppid; - - if(already_done) - return; - already_done = TRUE; - ppid = getppid(); - snprintf(s, sizeof(s), "/proc/%d/exe", ppid); - n = readlink(s, e, sizeof(e)); - if(n < 0) - return; - - e[MIN(n,sizeof(e)-1)] = '\0'; - - if(strstr(e,"gdb")) { - gaim_debug_info("dns", - "Debugger detected, performing useless query...\n"); - gethostbyname("x.x.x.x.x"); - } -#endif -} - -static void -gaim_dns_resolverthread(int child_out, int child_in, gboolean show_debug) -{ - dns_params_t dns_params; - const size_t zero = 0; - int rc; -#ifdef HAVE_GETADDRINFO - struct addrinfo hints, *res, *tmp; - char servname[20]; -#else - struct sockaddr_in sin; - const size_t addrlen = sizeof(sin); -#endif - -#ifdef HAVE_SIGNAL_H - signal(SIGHUP, SIG_DFL); - signal(SIGINT, SIG_DFL); - signal(SIGQUIT, SIG_DFL); - signal(SIGCHLD, SIG_DFL); - signal(SIGTERM, SIG_DFL); - signal(SIGTRAP, trap_gdb_bug); -#endif - - /* - * We resolve 1 host name for each iteration of this - * while loop. - * - * The top half of this reads in the hostname and port - * number from the socket with our parent. The bottom - * half of this resolves the IP (blocking) and sends - * the result back to our parent, when finished. - */ - while (1) { - const char ch = 'Y'; - fd_set fds; - struct timeval tv = { .tv_sec = 40 , .tv_usec = 0 }; - FD_ZERO(&fds); - FD_SET(child_in, &fds); - rc = select(child_in + 1, &fds, NULL, NULL, &tv); - if (!rc) { - if (show_debug) - printf("dns[%d]: nobody needs me... =(\n", getpid()); - break; - } - rc = read(child_in, &dns_params, sizeof(dns_params_t)); - if (rc < 0) { - perror("read()"); - break; - } - if (rc == 0) { - if (show_debug) - printf("dns[%d]: Oops, father has gone, wait for me, wait...!\n", getpid()); - _exit(0); - } - if (dns_params.hostname[0] == '\0') { - printf("dns[%d]: hostname = \"\" (port = %d)!!!\n", getpid(), dns_params.port); - _exit(1); - } - /* Tell our parent that we read the data successfully */ - write(child_out, &ch, sizeof(ch)); - - /* We have the hostname and port, now resolve the IP */ - -#ifdef HAVE_GETADDRINFO - g_snprintf(servname, sizeof(servname), "%d", dns_params.port); - memset(&hints, 0, sizeof(hints)); - - /* This is only used to convert a service - * name to a port number. As we know we are - * passing a number already, we know this - * value will not be really used by the C - * library. - */ - hints.ai_socktype = SOCK_STREAM; - rc = getaddrinfo(dns_params.hostname, servname, &hints, &res); - write(child_out, &rc, sizeof(rc)); - if (rc != 0) { - close(child_out); - if (show_debug) - printf("dns[%d] Error: getaddrinfo returned %d\n", - getpid(), rc); - dns_params.hostname[0] = '\0'; - continue; - } - tmp = res; - while (res) { - size_t ai_addrlen = res->ai_addrlen; - write(child_out, &ai_addrlen, sizeof(ai_addrlen)); - write(child_out, res->ai_addr, res->ai_addrlen); - res = res->ai_next; - } - freeaddrinfo(tmp); - write(child_out, &zero, sizeof(zero)); -#else - if (!inet_aton(dns_params.hostname, &sin.sin_addr)) { - struct hostent *hp; - if (!(hp = gethostbyname(dns_params.hostname))) { - write(child_out, &h_errno, sizeof(int)); - close(child_out); - if (show_debug) - printf("DNS Error: %d\n", h_errno); - _exit(0); - } - memset(&sin, 0, sizeof(struct sockaddr_in)); - memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); - sin.sin_family = hp->h_addrtype; - } else - sin.sin_family = AF_INET; - - sin.sin_port = htons(dns_params.port); - write(child_out, &addrlen, sizeof(addrlen)); - write(child_out, &sin, addrlen); - write(child_out, &zero, sizeof(zero)); -#endif - dns_params.hostname[0] = '\0'; - } - - close(child_out); - close(child_in); - - _exit(0); -} - -static pending_dns_request_t * -gaim_dns_new_resolverthread(gboolean show_debug) -{ - pending_dns_request_t *req; - int child_out[2], child_in[2]; - - /* Create pipes for communicating with the child process */ - if (pipe(child_out) || pipe(child_in)) { - gaim_debug_error("dns", - "Could not create pipes: %s\n", strerror(errno)); - return NULL; - } - - req = g_new(pending_dns_request_t, 1); - - cope_with_gdb_brokenness(); - - /* Fork! */ - req->dns_pid = fork(); - - /* If we are the child process... */ - if (req->dns_pid == 0) { - /* We should not access the parent's side of the pipes, so close them */ - close(child_out[0]); - close(child_in[1]); - - gaim_dns_resolverthread(child_out[1], child_in[0], show_debug); - /* The thread calls _exit() rather than returning, so we never get here */ - } - - /* We should not access the child's side of the pipes, so close them */ - close(child_out[1]); - close(child_in[0]); - if (req->dns_pid == -1) { - gaim_debug_error("dns", - "Could not create child process for DNS: %s\n", - strerror(errno)); - g_free(req); - return NULL; - } - - req->fd_out = child_out[0]; - req->fd_in = child_in[1]; - number_of_dns_children++; - gaim_debug_info("dns", - "Created new DNS child %d, there are now %d children.\n", - req->dns_pid, number_of_dns_children); - - return req; -} -/* - * End the DNS resolver child process functions. - */ - -/* - * Begin the functions for dealing with the DNS child processes. - */ -static void -req_free(pending_dns_request_t *req) -{ - g_return_if_fail(req != NULL); - - close(req->fd_in); - close(req->fd_out); - - g_free(req->host); - g_free(req); - - number_of_dns_children--; -} - -static int -send_dns_request_to_child(pending_dns_request_t *req, dns_params_t *dns_params) -{ - char ch; - int rc; - pid_t pid; - - /* This waitpid might return the child's PID if it has recently - * exited, or it might return an error if it exited "long - * enough" ago that it has already been reaped; in either - * instance, we can't use it. */ - if ((pid = waitpid (req->dns_pid, NULL, WNOHANG)) > 0) { - gaim_debug_warning("dns", - "DNS child %d no longer exists\n", req->dns_pid); - return -1; - } else if (pid < 0) { - gaim_debug_warning("dns", - "Wait for DNS child %d failed: %s\n", - req->dns_pid, strerror(errno)); - return -1; - } - - /* Let's contact this lost child! */ - rc = write(req->fd_in, dns_params, sizeof(*dns_params)); - if (rc < 0) { - gaim_debug_error("dns", - "Unable to write to DNS child %d: %d\n", - req->dns_pid, strerror(errno)); - close(req->fd_in); - return -1; - } - - g_return_val_if_fail(rc == sizeof(*dns_params), -1); - - /* Did you hear me? (This avoids some race conditions) */ - rc = read(req->fd_out, &ch, sizeof(ch)); - if (rc != 1 || ch != 'Y') - { - gaim_debug_warning("dns", - "DNS child %d not responding. Killing it!\n", - req->dns_pid); - kill(req->dns_pid, SIGKILL); - return -1; - } - - gaim_debug_info("dns", - "Successfully sent DNS request to child %d\n", req->dns_pid); - - return 0; -} - -static void -host_resolved(gpointer data, gint source, GaimInputCondition cond); - -static void -release_dns_child(pending_dns_request_t *req) -{ - g_free(req->host); - req->host = NULL; - - if (queued_requests && !g_queue_is_empty(queued_requests)) { - queued_dns_request_t *r = g_queue_pop_head(queued_requests); - req->host = g_strdup(r->params.hostname); - req->port = r->params.port; - req->callback = r->callback; - req->data = r->data; - - gaim_debug_info("dns", - "Processing queued DNS query for '%s' with child %d\n", - req->host, req->dns_pid); - - if (send_dns_request_to_child(req, &(r->params)) != 0) { - req_free(req); - req = NULL; - - gaim_debug_warning("dns", - "Intent of process queued query of '%s' failed, " - "requeueing...\n", r->params.hostname); - g_queue_push_head(queued_requests, r); - } else { - req->inpa = gaim_input_add(req->fd_out, GAIM_INPUT_READ, host_resolved, req); - g_free(r); - } - - } else { - req->host = NULL; - req->callback = NULL; - req->data = NULL; - free_dns_children = g_slist_append(free_dns_children, req); - } -} - -static void -host_resolved(gpointer data, gint source, GaimInputCondition cond) -{ - pending_dns_request_t *req = (pending_dns_request_t*)data; - int rc, err; - GSList *hosts = NULL; - struct sockaddr *addr = NULL; - size_t addrlen; - - gaim_debug_info("dns", "Got response for '%s'\n", req->host); - gaim_input_remove(req->inpa); - - rc = read(req->fd_out, &err, sizeof(err)); - if ((rc == 4) && (err != 0)) - { - char message[1024]; -#ifdef HAVE_GETADDRINFO - g_snprintf(message, sizeof(message), "DNS error: %s (pid=%d)", - gai_strerror(err), req->dns_pid); -#else - g_snprintf(message, sizeof(message), "DNS error: %d (pid=%d)", - err, req->dns_pid); -#endif - gaim_debug_error("dns", "%s\n", message); - req->callback(NULL, req->data, message); - release_dns_child(req); - return; - } - if (rc > 0) - { - while (rc > 0) { - rc = read(req->fd_out, &addrlen, sizeof(addrlen)); - if (rc > 0 && addrlen > 0) { - addr = g_malloc(addrlen); - rc = read(req->fd_out, addr, addrlen); - hosts = g_slist_append(hosts, GINT_TO_POINTER(addrlen)); - hosts = g_slist_append(hosts, addr); - } else { - break; - } - } - } else if (rc == -1) { - char message[1024]; - g_snprintf(message, sizeof(message), "Error reading from DNS child: %s",strerror(errno)); - gaim_debug_error("dns", "%s\n", message); - req->callback(NULL, req->data, message); - req_free(req); - return; - } else if (rc == 0) { - char message[1024]; - g_snprintf(message, sizeof(message), "EOF reading from DNS child"); - close(req->fd_out); - gaim_debug_error("dns", "%s\n", message); - req->callback(NULL, req->data, message); - req_free(req); - return; - } - -/* wait4(req->dns_pid, NULL, WNOHANG, NULL); */ - - req->callback(hosts, req->data, NULL); - - release_dns_child(req); -} -/* - * End the functions for dealing with the DNS child processes. - */ - -int -gaim_gethostbyname_async(const char *hostname, int port, GaimProxyDnsConnectFunction callback, gpointer data) -{ - pending_dns_request_t *req = NULL; - dns_params_t dns_params; - gchar *host_temp; - gboolean show_debug; - - show_debug = gaim_debug_is_enabled(); - - host_temp = g_strstrip(g_strdup(hostname)); - strncpy(dns_params.hostname, host_temp, sizeof(dns_params.hostname) - 1); - g_free(host_temp); - dns_params.hostname[sizeof(dns_params.hostname) - 1] = '\0'; - dns_params.port = port; - - /* - * If we have any children, attempt to have them perform the DNS - * query. If we're able to send the query to a child, then req - * will be set to the pending_dns_request_t. Otherwise, req will - * be NULL and we'll need to create a new DNS request child. - */ - while (free_dns_children != NULL) { - req = free_dns_children->data; - free_dns_children = g_slist_remove(free_dns_children, req); - - if (send_dns_request_to_child(req, &dns_params) == 0) - /* We found an acceptable child, yay */ - break; - - req_free(req); - req = NULL; - } - - /* We need to create a new DNS request child */ - if (req == NULL) { - if (number_of_dns_children >= MAX_DNS_CHILDREN) { - queued_dns_request_t *r = g_new(queued_dns_request_t, 1); - memcpy(&(r->params), &dns_params, sizeof(dns_params)); - r->callback = callback; - r->data = data; - if (!queued_requests) - queued_requests = g_queue_new(); - g_queue_push_tail(queued_requests, r); - - gaim_debug_info("dns", - "DNS query for '%s' queued\n", dns_params.hostname); - - return 0; - } - - req = gaim_dns_new_resolverthread(show_debug); - if (req == NULL) - { - gaim_debug_error("proxy", "oh dear, this is going to explode, I give up\n"); - return -1; - } - send_dns_request_to_child(req, &dns_params); - } - - req->host = g_strdup(hostname); - req->port = port; - req->callback = callback; - req->data = data; - req->inpa = gaim_input_add(req->fd_out, GAIM_INPUT_READ, host_resolved, req); - - return 0; -} - -#elif defined _WIN32 /* end __unix__ || __APPLE__ */ - -typedef struct _dns_tdata { - char *hostname; - int port; - GaimProxyDnsConnectFunction callback; - gpointer data; - GSList *hosts; - char *errmsg; -} dns_tdata; - -static gboolean dns_main_thread_cb(gpointer data) { - dns_tdata *td = (dns_tdata*)data; - if (td->errmsg != NULL) { - gaim_debug_info("dns", "%s\n", td->errmsg); - } - td->callback(td->hosts, td->data, td->errmsg); - g_free(td->hostname); - g_free(td->errmsg); - g_free(td); - return FALSE; -} - -static gpointer dns_thread(gpointer data) { - -#ifdef HAVE_GETADDRINFO - int rc; - struct addrinfo hints, *res, *tmp; - char servname[20]; -#else - struct sockaddr_in sin; - struct hostent *hp; -#endif - dns_tdata *td = (dns_tdata*)data; - -#ifdef HAVE_GETADDRINFO - g_snprintf(servname, sizeof(servname), "%d", td->port); - memset(&hints,0,sizeof(hints)); - - /* This is only used to convert a service - * name to a port number. As we know we are - * passing a number already, we know this - * value will not be really used by the C - * library. - */ - hints.ai_socktype = SOCK_STREAM; - if ((rc = getaddrinfo(td->hostname, servname, &hints, &res)) == 0) { - tmp = res; - while(res) { - td->hosts = g_slist_append(td->hosts, - GSIZE_TO_POINTER(res->ai_addrlen)); - td->hosts = g_slist_append(td->hosts, - g_memdup(res->ai_addr, res->ai_addrlen)); - res = res->ai_next; - } - freeaddrinfo(tmp); - } else { - td->errmsg = g_strdup_printf("DNS getaddrinfo(\"%s\", \"%s\") error: %d", td->hostname, servname, rc); - } -#else - if ((hp = gethostbyname(td->hostname))) { - memset(&sin, 0, sizeof(struct sockaddr_in)); - memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); - sin.sin_family = hp->h_addrtype; - sin.sin_port = htons(td->port); - - td->hosts = g_slist_append(td->hosts, - GSIZE_TO_POINTER(sizeof(sin))); - td->hosts = g_slist_append(td->hosts, - g_memdup(&sin, sizeof(sin))); - } else { - td->errmsg = g_strdup_printf("DNS gethostbyname(\"%s\") error: %d", td->hostname, h_errno); - } -#endif - /* back to main thread */ - g_idle_add(dns_main_thread_cb, td); - return 0; -} - -int -gaim_gethostbyname_async(const char *hostname, int port, - GaimProxyDnsConnectFunction callback, gpointer data) -{ - dns_tdata *td; - struct sockaddr_in sin; - GError* err = NULL; - - if(inet_aton(hostname, &sin.sin_addr)) { - GSList *hosts = NULL; - sin.sin_family = AF_INET; - sin.sin_port = htons(port); - hosts = g_slist_append(hosts, GINT_TO_POINTER(sizeof(sin))); - hosts = g_slist_append(hosts, g_memdup(&sin, sizeof(sin))); - callback(hosts, data, NULL); - return 0; - } - - gaim_debug_info("dns", "DNS Lookup for: %s\n", hostname); - td = g_new0(dns_tdata, 1); - td->hostname = g_strdup(hostname); - td->port = port; - td->callback = callback; - td->data = data; - - if(!g_thread_create(dns_thread, td, FALSE, &err)) { - gaim_debug_error("dns", "DNS thread create failure: %s\n", err?err->message:""); - g_error_free(err); - g_free(td->hostname); - g_free(td); - return -1; - } - return 0; -} - -#else /* not __unix__ or __APPLE__ or _WIN32 */ - -typedef struct { - gpointer data; - size_t addrlen; - struct sockaddr *addr; - GaimProxyDnsConnectFunction callback; -} pending_dns_request_t; - -static gboolean host_resolved(gpointer data) -{ - pending_dns_request_t *req = (pending_dns_request_t*)data; - GSList *hosts = NULL; - hosts = g_slist_append(hosts, GINT_TO_POINTER(req->addrlen)); - hosts = g_slist_append(hosts, req->addr); - req->callback(hosts, req->data, NULL); - g_free(req); - return FALSE; -} - -int -gaim_gethostbyname_async(const char *hostname, int port, - GaimProxyDnsConnectFunction callback, gpointer data) -{ - struct sockaddr_in sin; - pending_dns_request_t *req; - - if (!inet_aton(hostname, &sin.sin_addr)) { - struct hostent *hp; - if(!(hp = gethostbyname(hostname))) { - gaim_debug_error("dns", - "gaim_gethostbyname(\"%s\", %d) failed: %d\n", - hostname, port, h_errno); - return -1; - } - memset(&sin, 0, sizeof(struct sockaddr_in)); - memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); - sin.sin_family = hp->h_addrtype; - } else - sin.sin_family = AF_INET; - sin.sin_port = htons(port); - - req = g_new(pending_dns_request_t, 1); - req->addr = (struct sockaddr*) g_memdup(&sin, sizeof(sin)); - req->addrlen = sizeof(sin); - req->data = data; - req->callback = callback; - gaim_timeout_add(10, host_resolved, req); - return 0; -} - -#endif /* not __unix__ or __APPLE__ or _WIN32 */ - -static void no_one_calls(gpointer data, gint source, GaimInputCondition cond) { GaimProxyConnectInfo *connect_info = data; @@ -2368,8 +1701,9 @@ return NULL; } - if (gaim_gethostbyname_async(connecthost, - connectport, connection_host_resolved, connect_info) != 0) + connect_info->query_data = gaim_dnsquery_a(connecthost, + connectport, connection_host_resolved, connect_info); + if (connect_info->query_data == NULL) { gaim_proxy_connect_info_destroy(connect_info); return NULL; @@ -2401,8 +1735,9 @@ connect_info->port = port; connect_info->gpi = gpi; - if (gaim_gethostbyname_async(gaim_proxy_info_get_host(gpi), - gaim_proxy_info_get_port(gpi), connection_host_resolved, connect_info) != 0) + connect_info->query_data = gaim_dnsquery_a(gaim_proxy_info_get_host(gpi), + gaim_proxy_info_get_port(gpi), connection_host_resolved, connect_info); + if (connect_info->query_data == NULL) { gaim_proxy_connect_info_destroy(connect_info); return NULL; Modified: branches/v2_0_0/src/proxy.h =================================================================== --- branches/v2_0_0/src/proxy.h 2006-08-17 07:44:52 UTC (rev 16811) +++ branches/v2_0_0/src/proxy.h 2006-08-17 08:41:44 UTC (rev 16812) @@ -26,6 +26,7 @@ #define _GAIM_PROXY_H_ #include <glib.h> +#include "dnsquery.h" #include "eventloop.h" /** @@ -56,6 +57,7 @@ } GaimProxyInfo; +typedef struct _GaimDnsQueryData GaimDnsQueryData; typedef struct _GaimProxyConnectInfo GaimProxyConnectInfo; typedef void (*GaimProxyConnectFunction)(gpointer data, gint source, const gchar *error_message); @@ -278,18 +280,6 @@ */ void gaim_proxy_connect_cancel(GaimProxyConnectInfo *connect_info); -/** - * Do an async dns query - * - * @param hostname The hostname to resolve - * @param port A portnumber which is stored in the struct sockaddr - * @param callback Callback to call after resolving - * @param data Extra data for the callback function - * - * @return Zero indicates the connection is pending. Any other value indicates failure. - */ -int gaim_gethostbyname_async(const char *hostname, int port, GaimProxyDnsConnectFunction callback, gpointer data); - /*@}*/ #ifdef __cplusplus Modified: branches/v2_0_0/src/prpl.c =================================================================== --- branches/v2_0_0/src/prpl.c 2006-08-17 07:44:52 UTC (rev 16811) +++ branches/v2_0_0/src/prpl.c 2006-08-17 08:41:44 UTC (rev 16812) @@ -171,6 +171,12 @@ return; } + if (!gaim_status_is_online(new_status)) + { + if (!gaim_account_is_disconnected(account)) + gaim_account_disconnect(account); + return; + } if (gaim_account_is_connecting(account)) /* @@ -191,13 +197,6 @@ { prpl_info->set_status(account, new_status); } - - if (!gaim_status_is_online(new_status)) - { - if (!gaim_account_is_disconnected(account)) - gaim_account_disconnect(account); - return; - } } void Modified: branches/v2_0_0/src/sslconn.c =================================================================== --- branches/v2_0_0/src/sslconn.c 2006-08-17 07:44:52 UTC (rev 16811) +++ branches/v2_0_0/src/sslconn.c 2006-08-17 08:41:44 UTC (rev 16812) @@ -45,10 +45,14 @@ gaim_plugin_load(plugin); ops = gaim_ssl_get_ops(); - if (ops != NULL && ops->init != NULL) - return ops->init(); - else + if ((ops == NULL) || (ops->init == NULL) || (ops->uninit == NULL) || + (ops->connect == NULL) || (ops->close == NULL) || + (ops->read == NULL) || (ops->write == NULL)) + { return FALSE; + } + + return ops->init(); } gboolean @@ -62,25 +66,42 @@ #endif } +static void +gaim_ssl_connect_cb(gpointer data, gint source, const gchar *error_message) +{ + GaimSslConnection *gsc; + GaimSslOps *ops; + + gsc = data; + gsc->connect_info = NULL; + + if (source < 0) + { + if (gsc->error_cb != NULL) + gsc->error_cb(gsc, GAIM_SSL_CONNECT_FAILED, gsc->connect_cb_data); + + gaim_ssl_close(gsc); + return; + } + + gsc->fd = source; + + ops = gaim_ssl_get_ops(); + ops->connect(gsc); +} + GaimSslConnection * gaim_ssl_connect(GaimAccount *account, const char *host, int port, GaimSslInputFunction func, GaimSslErrorFunction error_func, void *data) { GaimSslConnection *gsc; - GaimSslOps *ops; - GaimProxyConnectInfo *connect_info; g_return_val_if_fail(host != NULL, NULL); g_return_val_if_fail(port != 0 && port != -1, NULL); g_return_val_if_fail(func != NULL, NULL); g_return_val_if_fail(gaim_ssl_is_supported(), NULL); - ops = gaim_ssl_get_ops(); - - g_return_val_if_fail(ops != NULL, NULL); - g_return_val_if_fail(ops->connect_cb != NULL, NULL); - if (!_ssl_initialized) { if (!ssl_init()) @@ -89,15 +110,16 @@ gsc = g_new0(GaimSslConnection, 1); + gsc->fd = -1; gsc->host = g_strdup(host); gsc->port = port; gsc->connect_cb_data = data; gsc->connect_cb = func; gsc->error_cb = error_func; - connect_info = gaim_proxy_connect(account, host, port, ops->connect_cb, gsc); + gsc->connect_info = gaim_proxy_connect(account, host, port, gaim_ssl_connect_cb, gsc); - if (connect_info == NULL) + if (gsc->connect_info == NULL) { g_free(gsc->host); g_free(gsc); @@ -141,11 +163,6 @@ g_return_val_if_fail(func != NULL, NULL); g_return_val_if_fail(gaim_ssl_is_supported(), NULL); - ops = gaim_ssl_get_ops(); - - g_return_val_if_fail(ops != NULL, NULL); - g_return_val_if_fail(ops->connect_cb != NULL, NULL); - if (!_ssl_initialized) { if (!ssl_init()) @@ -157,8 +174,10 @@ gsc->connect_cb_data = data; gsc->connect_cb = func; gsc->error_cb = error_func; + gsc->fd = fd; - ops->connect_cb(gsc, fd, GAIM_INPUT_READ); + ops = gaim_ssl_get_ops(); + ops->connect(gsc); return (GaimSslConnection *)gsc; } @@ -171,14 +190,15 @@ g_return_if_fail(gsc != NULL); ops = gaim_ssl_get_ops(); + (ops->close)(gsc); - if (gsc->inpa) + if (gsc->connect_info != NULL) + gaim_proxy_connect_cancel(gsc->connect_info); + + if (gsc->inpa > 0) gaim_input_remove(gsc->inpa); - if (ops != NULL && ops->close != NULL) - (ops->close)(gsc); - - if (gsc->fd != -1) + if (gsc->fd >= 0) close(gsc->fd); g_free(gsc->host); @@ -195,11 +215,7 @@ g_return_val_if_fail(len > 0, 0); ops = gaim_ssl_get_ops(); - - if (ops != NULL && (ops->read) != NULL) - return (ops->read)(gsc, data, len); - - return 0; + return (ops->read)(gsc, data, len); } size_t @@ -212,11 +228,7 @@ g_return_val_if_fail(len > 0, 0); ops = gaim_ssl_get_ops(); - - if (ops != NULL && (ops->write) != NULL) - return (ops->write)(gsc, data, len); - - return 0; + return (ops->write)(gsc, data, len); } void @@ -245,9 +257,7 @@ return; ops = gaim_ssl_get_ops(); + ops->uninit(); - if (ops != NULL && ops->uninit != NULL) - ops->uninit(); - _ssl_initialized = FALSE; } Modified: branches/v2_0_0/src/sslconn.h =================================================================== --- branches/v2_0_0/src/sslconn.h 2006-08-17 07:44:52 UTC (rev 16811) +++ branches/v2_0_0/src/sslconn.h 2006-08-17 08:41:44 UTC (rev 16812) @@ -54,6 +54,7 @@ int fd; int inpa; + GaimProxyConnectInfo *connect_info; void *private_data; }; @@ -61,13 +62,13 @@ /** * SSL implementation operations structure. * - * Every SSL implementation must provide one of these and register it. + * Every SSL implementation must provide all of these and register it. */ typedef struct { gboolean (*init)(void); void (*uninit)(void); - GaimInputFunction connect_cb; + void (*connect)(GaimSslConnection *gsc); void (*close)(GaimSslConnection *gsc); size_t (*read)(GaimSslConnection *gsc, void *data, size_t len); size_t (*write)(GaimSslConnection *gsc, const void *data, size_t len); Modified: branches/v2_0_0/src/stun.c =================================================================== --- branches/v2_0_0/src/stun.c 2006-08-17 07:44:52 UTC (rev 16811) +++ branches/v2_0_0/src/stun.c 2006-08-17 08:41:44 UTC (rev 16812) @@ -40,6 +40,7 @@ #include "debug.h" #include "account.h" +#include "dnsquery.h" #include "dnssrv.h" #include "network.h" #include "proxy.h" @@ -359,7 +360,7 @@ gaim_debug_info("stun", "got %d SRV responses, server: %s, port: %d\n", results, servername, port); - gaim_gethostbyname_async(servername, port, hbn_cb, NULL); + gaim_dnsquery_a(servername, port, hbn_cb, NULL); g_free(resp); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <the...@us...> - 2006-08-17 10:04:28
|
Revision: 16813 Author: thekingant Date: 2006-08-17 03:04:19 -0700 (Thu, 17 Aug 2006) ViewCVS: http://svn.sourceforge.net/gaim/?rev=16813&view=rev Log Message: ----------- Revert SVN revision 16811. Kevin says it's crashing for him and I'm tired. ViewCVS Links: ------------- http://svn.sourceforge.net/gaim/?rev=16811&view=rev Modified Paths: -------------- branches/v2_0_0/src/Makefile.am branches/v2_0_0/src/Makefile.mingw branches/v2_0_0/src/protocols/simple/simple.c branches/v2_0_0/src/proxy.c branches/v2_0_0/src/proxy.h branches/v2_0_0/src/stun.c Removed Paths: ------------- branches/v2_0_0/src/dnsquery.c branches/v2_0_0/src/dnsquery.h Modified: branches/v2_0_0/src/Makefile.am =================================================================== --- branches/v2_0_0/src/Makefile.am 2006-08-17 08:41:44 UTC (rev 16812) +++ branches/v2_0_0/src/Makefile.am 2006-08-17 10:04:19 UTC (rev 16813) @@ -77,7 +77,6 @@ core.c \ debug.c \ desktopitem.c \ - dnsquery.c \ eventloop.c \ ft.c \ idle.c \ @@ -126,7 +125,6 @@ dbus-maybe.h \ debug.h \ desktopitem.h \ - dnsquery.h \ eventloop.h \ ft.h \ idle.h \ Modified: branches/v2_0_0/src/Makefile.mingw =================================================================== --- branches/v2_0_0/src/Makefile.mingw 2006-08-17 08:41:44 UTC (rev 16812) +++ branches/v2_0_0/src/Makefile.mingw 2006-08-17 10:04:19 UTC (rev 16813) @@ -95,7 +95,6 @@ conversation.c \ core.c \ debug.c \ - dnsquery.c \ dnssrv.c \ eventloop.c \ ft.c \ Deleted: branches/v2_0_0/src/dnsquery.c =================================================================== --- branches/v2_0_0/src/dnsquery.c 2006-08-17 08:41:44 UTC (rev 16812) +++ branches/v2_0_0/src/dnsquery.c 2006-08-17 10:04:19 UTC (rev 16813) @@ -1,774 +0,0 @@ -/** - * @file dnsquery.c DNS query API - * @ingroup core - * - * gaim - * - * Gaim is the legal property of its developers, whose names are too numerous - * to list here. Please refer to the COPYRIGHT file distributed with this - * source distribution. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - */ - -#include "internal.h" -#include "debug.h" -#include "ntlm.h" -#include "util.h" - -struct _GaimDnsqueryData { -}; - -/************************************************************************** - * Global DNS query API - **************************************************************************/ - -#if defined(__unix__) || defined(__APPLE__) - -/* - * This structure represents both a pending DNS request and - * a free child process. - */ -typedef struct _DnsqueryResolverprocess DnsqueryResolverprocess; -struct _DnsqueryResolverprocess { - char *host; - int port; - GaimProxyDnsConnectFunction callback; - gpointer data; - guint inpa; - int fd_in, fd_out; - pid_t dns_pid; -}; - -//static GSList *all_dns_children = NULL; -static GSList *free_dns_children = NULL; -static GQueue *queued_requests = NULL; - -static int number_of_dns_children = 0; - -static const int MAX_DNS_CHILDREN = 2; - -typedef struct { - char hostname[512]; - int port; -} dns_params_t; - -typedef struct { - dns_params_t params; - GaimProxyDnsConnectFunction callback; - gpointer data; -} queued_dns_request_t; - -/* - * Begin the DNS resolver child process functions. - */ -#ifdef HAVE_SIGNAL_H -static void -trap_gdb_bug() -{ - const char *message = - "Gaim's DNS child got a SIGTRAP signal.\n" - "This can be caused by trying to run gaim inside gdb.\n" - "There is a known gdb bug which prevents this. Supposedly gaim\n" - "should have detected you were using gdb and used an ugly hack,\n" - "check cope_with_gdb_brokenness() in dnsquery.c.\n\n" - "For more info about this bug, see http://sources.redhat.com/ml/gdb/2001-07/msg00349.html\n"; - fputs("\n* * *\n",stderr); - fputs(message,stderr); - fputs("* * *\n\n",stderr); - execlp("xmessage","xmessage","-center", message, NULL); - _exit(1); -} -#endif - -static void -cope_with_gdb_brokenness() -{ -#ifdef __linux__ - static gboolean already_done = FALSE; - char s[256], e[512]; - int n; - pid_t ppid; - - if (already_done) - return; - - already_done = TRUE; - ppid = getppid(); - snprintf(s, sizeof(s), "/proc/%d/exe", ppid); - n = readlink(s, e, sizeof(e)); - if (n < 0) - return; - - e[MIN(n,sizeof(e)-1)] = '\0'; - - if (strstr(e,"gdb")) - { - gaim_debug_info("dnsquery", - "Debugger detected, performing useless query...\n"); - gethostbyname("x.x.x.x.x"); - } -#endif -} - -/** - * When doing DNS queries on Unix and OS-X, we fork off a separate - * process and communicate with it using pipes. This function is - * called shortly after the child is forked, and the function exits - * the process after it's no longer needed. - */ -static void -gaim_dns_resolverprocess(int child_out, int child_in, gboolean show_debug) -{ - dns_params_t dns_params; - const size_t zero = 0; - int rc; -#ifdef HAVE_GETADDRINFO - struct addrinfo hints, *res, *tmp; - char servname[20]; -#else - struct sockaddr_in sin; - const size_t addrlen = sizeof(sin); -#endif - -#ifdef HAVE_SIGNAL_H - signal(SIGHUP, SIG_DFL); - signal(SIGINT, SIG_DFL); - signal(SIGQUIT, SIG_DFL); - signal(SIGCHLD, SIG_DFL); - signal(SIGTERM, SIG_DFL); - signal(SIGTRAP, trap_gdb_bug); -#endif - - /* - * We resolve 1 host name for each iteration of this - * while loop. - * - * The top half of this reads in the hostname and port - * number from the socket with our parent. The bottom - * half of this resolves the IP (blocking) and sends - * the result back to our parent, when finished. - */ - while (1) - { - const char ch = 'Y'; - fd_set fds; - struct timeval tv = { .tv_sec = 40 , .tv_usec = 0 }; - - FD_ZERO(&fds); - FD_SET(child_in, &fds); - rc = select(child_in + 1, &fds, NULL, NULL, &tv); - if (rc < 0) { - if (show_debug) - printf("dns[%d]: select failed\n", getpid()); - close(child_out); - close(child_in); - _exit(1); - } - if (rc == 0) { - if (show_debug) - printf("dns[%d]: nobody needs me... =(\n", getpid()); - break; - } - - rc = read(child_in, &dns_params, sizeof(dns_params_t)); - if (rc < 0) { - if (show_debug) - printf("dns[%d]: read failed\n", getpid()); - close(child_out); - close(child_in); - _exit(1); - } - if (rc == 0) { - if (show_debug) - printf("dns[%d]: Oops, father has gone, wait for me, wait...!\n", getpid()); - break; - } - if (dns_params.hostname[0] == '\0') { - printf("dns[%d]: hostname = \"\" (port = %d)!!!\n", getpid(), dns_params.port); - close(child_out); - close(child_in); - _exit(1); - } - /* Tell our parent that we read the data successfully */ - write(child_out, &ch, sizeof(ch)); - - /* We have the hostname and port, now resolve the IP */ - -#ifdef HAVE_GETADDRINFO - g_snprintf(servname, sizeof(servname), "%d", dns_params.port); - memset(&hints, 0, sizeof(hints)); - - /* - * This is only used to convert a service - * name to a port number. As we know we are - * passing a number already, we know this - * value will not be really used by the C - * library. - */ - hints.ai_socktype = SOCK_STREAM; - rc = getaddrinfo(dns_params.hostname, servname, &hints, &res); - write(child_out, &rc, sizeof(rc)); - if (rc != 0) { - close(child_out); - if (show_debug) - printf("dns[%d] Error: getaddrinfo returned %d\n", - getpid(), rc); - dns_params.hostname[0] = '\0'; - continue; - } - for (tmp = res; tmp != NULL; tmp = tmp->ai_next) - { - size_t ai_addrlen = tmp->ai_addrlen; - write(child_out, &ai_addrlen, sizeof(ai_addrlen)); - write(child_out, tmp->ai_addr, tmp->ai_addrlen); - } - freeaddrinfo(res); - write(child_out, &zero, sizeof(zero)); -#else - if (!inet_aton(dns_params.hostname, &sin.sin_addr)) - { - struct hostent *hp; - - hp = gethostbyname(dns_params.hostname); - if (hp == NULL) - { - if (show_debug) - printf("DNS Error: %d\n", h_errno); - write(child_out, &h_errno, sizeof(int)); - close(child_out); - close(child_in); - _exit(1); - } - memset(&sin, 0, sizeof(struct sockaddr_in)); - memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); - sin.sin_family = hp->h_addrtype; - } else - sin.sin_family = AF_INET; - - sin.sin_port = htons(dns_params.port); - write(child_out, &addrlen, sizeof(addrlen)); - write(child_out, &sin, addrlen); - write(child_out, &zero, sizeof(zero)); -#endif - dns_params.hostname[0] = '\0'; - } - - close(child_out); - close(child_in); - - _exit(0); -} - -static DnsqueryResolverprocess * -gaim_dns_new_resolverprocess(gboolean show_debug) -{ - DnsqueryResolverprocess *req; - int child_out[2], child_in[2]; - - /* Create pipes for communicating with the child process */ - if (pipe(child_out) || pipe(child_in)) { - gaim_debug_error("dnsquery", - "Could not create pipes: %s\n", strerror(errno)); - return NULL; - } - - req = g_new(DnsqueryResolverprocess, 1); - - cope_with_gdb_brokenness(); - - /* Fork! */ - req->dns_pid = fork(); - - /* If we are the child process... */ - if (req->dns_pid == 0) { - /* We should not access the parent's side of the pipes, so close them */ - close(child_out[0]); - close(child_in[1]); - - gaim_dns_resolverprocess(child_out[1], child_in[0], show_debug); - /* The thread calls _exit() rather than returning, so we never get here */ - } - - /* We should not access the child's side of the pipes, so close them */ - close(child_out[1]); - close(child_in[0]); - if (req->dns_pid == -1) { - gaim_debug_error("dnsquery", - "Could not create child process for DNS: %s\n", - strerror(errno)); - g_free(req); - return NULL; - } - - req->fd_out = child_out[0]; - req->fd_in = child_in[1]; - number_of_dns_children++; - gaim_debug_info("dnsquery", - "Created new DNS child %d, there are now %d children.\n", - req->dns_pid, number_of_dns_children); - - return req; -} -/* - * End the DNS resolver child process functions. - */ - -/* - * Begin the functions for dealing with the DNS child processes. - */ -static void -req_free(DnsqueryResolverprocess *req) -{ - g_return_if_fail(req != NULL); - - close(req->fd_in); - close(req->fd_out); - - g_free(req->host); - g_free(req); - - number_of_dns_children--; -} - -static int -send_dns_request_to_child(DnsqueryResolverprocess *req, dns_params_t *dns_params) -{ - char ch; - int rc; - pid_t pid; - - /* This waitpid might return the child's PID if it has recently - * exited, or it might return an error if it exited "long - * enough" ago that it has already been reaped; in either - * instance, we can't use it. */ - if ((pid = waitpid (req->dns_pid, NULL, WNOHANG)) > 0) { - gaim_debug_warning("dnsquery", - "DNS child %d no longer exists\n", req->dns_pid); - return -1; - } else if (pid < 0) { - gaim_debug_warning("dnsquery", - "Wait for DNS child %d failed: %s\n", - req->dns_pid, strerror(errno)); - return -1; - } - - /* Let's contact this lost child! */ - rc = write(req->fd_in, dns_params, sizeof(*dns_params)); - if (rc < 0) { - gaim_debug_error("dnsquery", - "Unable to write to DNS child %d: %d\n", - req->dns_pid, strerror(errno)); - close(req->fd_in); - return -1; - } - - g_return_val_if_fail(rc == sizeof(*dns_params), -1); - - /* Did you hear me? (This avoids some race conditions) */ - rc = read(req->fd_out, &ch, sizeof(ch)); - if (rc != 1 || ch != 'Y') - { - gaim_debug_warning("dnsquery", - "DNS child %d not responding. Killing it!\n", - req->dns_pid); - kill(req->dns_pid, SIGKILL); - return -1; - } - - gaim_debug_info("dnsquery", - "Successfully sent DNS request to child %d\n", req->dns_pid); - - return 0; -} - -static void -host_resolved(gpointer data, gint source, GaimInputCondition cond); - -static void -release_dns_child(DnsqueryResolverprocess *req) -{ - g_free(req->host); - req->host = NULL; - - if (queued_requests && !g_queue_is_empty(queued_requests)) { - queued_dns_request_t *r = g_queue_pop_head(queued_requests); - req->host = g_strdup(r->params.hostname); - req->port = r->params.port; - req->callback = r->callback; - req->data = r->data; - - gaim_debug_info("dnsquery", - "Processing queued DNS query for '%s' with child %d\n", - req->host, req->dns_pid); - - if (send_dns_request_to_child(req, &(r->params)) != 0) { - req_free(req); - req = NULL; - - gaim_debug_warning("dnsquery", - "Intent of process queued query of '%s' failed, " - "requeueing...\n", r->params.hostname); - g_queue_push_head(queued_requests, r); - } else { - req->inpa = gaim_input_add(req->fd_out, GAIM_INPUT_READ, host_resolved, req); - g_free(r); - } - - } else { - req->host = NULL; - req->callback = NULL; - req->data = NULL; - free_dns_children = g_slist_append(free_dns_children, req); - } -} - -static void -host_resolved(gpointer data, gint source, GaimInputCondition cond) -{ - DnsqueryResolverprocess *req = (DnsqueryResolverprocess*)data; - int rc, err; - GSList *hosts = NULL; - struct sockaddr *addr = NULL; - size_t addrlen; - - gaim_debug_info("dnsquery", "Got response for '%s'\n", req->host); - gaim_input_remove(req->inpa); - - rc = read(req->fd_out, &err, sizeof(err)); - if ((rc == 4) && (err != 0)) - { - gchar *message; -#ifdef HAVE_GETADDRINFO - message = g_strdup_printf("DNS error: %s (pid=%d)", - gai_strerror(err), req->dns_pid); -#else - message = g_strdup_printf("DNS error: %d (pid=%d)", - err, req->dns_pid); -#endif - gaim_debug_error("dnsquery", "%s\n", message); - req->callback(NULL, req->data, message); - g_free(message); - release_dns_child(req); - return; - } - - if (rc > 0) - { - while (rc > 0) { - rc = read(req->fd_out, &addrlen, sizeof(addrlen)); - if (rc > 0 && addrlen > 0) { - addr = g_malloc(addrlen); - rc = read(req->fd_out, addr, addrlen); - hosts = g_slist_append(hosts, GINT_TO_POINTER(addrlen)); - hosts = g_slist_append(hosts, addr); - } else { - break; - } - } - } else if (rc == -1) { - gchar *message; - message = g_strdup_printf("Error reading from DNS child: %s", strerror(errno)); - gaim_debug_error("dnsquery", "%s\n", message); - req->callback(NULL, req->data, message); - g_free(message); - req_free(req); - return; - } else if (rc == 0) { - gchar *message; - close(req->fd_out); - message = g_strdup_printf("EOF reading from DNS child"); - gaim_debug_error("dnsquery", "%s\n", message); - req->callback(NULL, req->data, message); - g_free(message); - req_free(req); - return; - } - -/* wait4(req->dns_pid, NULL, WNOHANG, NULL); */ - - req->callback(hosts, req->data, NULL); - - release_dns_child(req); -} -/* - * End the functions for dealing with the DNS child processes. - */ - -GaimDnsqueryData * -gaim_dnsquery_a(const char *hostname, int port, GaimDnsqueryConnectFunction callback, gpointer data) -{ - GaimDnsqueryData *query_data; - DnsqueryResolverprocess *req = NULL; - dns_params_t dns_params; - gchar *host_temp; - gboolean show_debug; - - show_debug = gaim_debug_is_enabled(); - - host_temp = g_strstrip(g_strdup(hostname)); - strncpy(dns_params.hostname, host_temp, sizeof(dns_params.hostname) - 1); - g_free(host_temp); - dns_params.hostname[sizeof(dns_params.hostname) - 1] = '\0'; - dns_params.port = port; - - /* - * If we have any children, attempt to have them perform the DNS - * query. If we're able to send the query to a child, then req - * will be set to the DnsqueryResolverprocess. Otherwise, req will - * be NULL and we'll need to create a new DNS request child. - */ - while (free_dns_children != NULL) - { - req = free_dns_children->data; - free_dns_children = g_slist_remove(free_dns_children, req); - - if (send_dns_request_to_child(req, &dns_params) == 0) - /* We found an acceptable child, yay */ - break; - - req_free(req); - req = NULL; - } - - /* We need to create a new DNS request child */ - if (req == NULL) - { - if (number_of_dns_children >= MAX_DNS_CHILDREN) - { - queued_dns_request_t *r = g_new(queued_dns_request_t, 1); - memcpy(&(r->params), &dns_params, sizeof(dns_params)); - r->callback = callback; - r->data = data; - if (!queued_requests) - queued_requests = g_queue_new(); - g_queue_push_tail(queued_requests, r); - - gaim_debug_info("dnsquery", - "DNS query for '%s' queued\n", dns_params.hostname); - - return query_data; - } - - req = gaim_dns_new_resolverprocess(show_debug); - if (req == NULL) - { - gaim_debug_error("dnsquery", "oh dear, this is going to explode, I give up\n"); - return NULL; - } - send_dns_request_to_child(req, &dns_params); - } - - req->host = g_strdup(hostname); - req->port = port; - req->callback = callback; - req->data = data; - req->inpa = gaim_input_add(req->fd_out, GAIM_INPUT_READ, host_resolved, req); - - return query_data; -} - -#elif defined _WIN32 /* end __unix__ || __APPLE__ */ - -typedef struct _dns_tdata { - char *hostname; - int port; - GaimProxyDnsConnectFunction callback; - gpointer data; - GSList *hosts; - char *errmsg; -} dns_tdata; - -static gboolean dns_main_thread_cb(gpointer data) -{ - dns_tdata *td = (dns_tdata*)data; - if (td->errmsg != NULL) { - gaim_debug_info("dnsquery", "%s\n", td->errmsg); - } - td->callback(td->hosts, td->data, td->errmsg); - g_free(td->hostname); - g_free(td->errmsg); - g_free(td); - return FALSE; -} - -static gpointer -dns_thread(gpointer data) -{ - -#ifdef HAVE_GETADDRINFO - int rc; - struct addrinfo hints, *res, *tmp; - char servname[20]; -#else - struct sockaddr_in sin; - struct hostent *hp; -#endif - dns_tdata *td = (dns_tdata*)data; - -#ifdef HAVE_GETADDRINFO - g_snprintf(servname, sizeof(servname), "%d", td->port); - memset(&hints, 0, sizeof(hints)); - - /* This is only used to convert a service - * name to a port number. As we know we are - * passing a number already, we know this - * value will not be really used by the C - * library. - */ - hints.ai_socktype = SOCK_STREAM; - rc = getaddrinfo(td->hostname, servname, &hints, &res); - if (rc == 0) - { - tmp = res; - while(res) { - td->hosts = g_slist_append(td->hosts, - GSIZE_TO_POINTER(res->ai_addrlen)); - td->hosts = g_slist_append(td->hosts, - g_memdup(res->ai_addr, res->ai_addrlen)); - res = res->ai_next; - } - freeaddrinfo(tmp); - } - else - { - td->errmsg = g_strdup_printf("DNS getaddrinfo(\"%s\", \"%s\") error: %d", td->hostname, servname, rc); - } -#else - hp = gethostbyname(td->hostname); - if (hp != NULL) - { - memset(&sin, 0, sizeof(struct sockaddr_in)); - memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); - sin.sin_family = hp->h_addrtype; - sin.sin_port = htons(td->port); - - td->hosts = g_slist_append(td->hosts, - GSIZE_TO_POINTER(sizeof(sin))); - td->hosts = g_slist_append(td->hosts, - g_memdup(&sin, sizeof(sin))); - } else { - td->errmsg = g_strdup_printf("DNS gethostbyname(\"%s\") error: %d", td->hostname, h_errno); - } -#endif - /* back to main thread */ - g_idle_add(dns_main_thread_cb, td); - - return 0; -} - -GaimDnsqueryData * -gaim_dnsquery_a(const char *hostname, int port, - GaimProxyDnsConnectFunction callback, gpointer data) -{ - GaimDnsqueryData *query_data; - dns_tdata *td; - struct sockaddr_in sin; - GError* err = NULL; - - if (inet_aton(hostname, &sin.sin_addr)) - { - GSList *hosts = NULL; - sin.sin_family = AF_INET; - sin.sin_port = htons(port); - hosts = g_slist_append(hosts, GINT_TO_POINTER(sizeof(sin))); - hosts = g_slist_append(hosts, g_memdup(&sin, sizeof(sin))); - callback(hosts, data, NULL); - return query_data; - } - - gaim_debug_info("dnsquery", "DNS Lookup for: %s\n", hostname); - td = g_new0(dns_tdata, 1); - td->hostname = g_strdup(hostname); - td->port = port; - td->callback = callback; - td->data = data; - - if (!g_thread_create(dns_thread, td, FALSE, &err)) - { - gaim_debug_error("dnsquery", "DNS thread create failure: %s\n", err?err->message:""); - g_error_free(err); - g_free(td->hostname); - g_free(td); - return NULL; - } - - return query_data; -} - -#else /* not __unix__ or __APPLE__ or _WIN32 */ - -typedef struct { - gpointer data; - size_t addrlen; - struct sockaddr *addr; - GaimProxyDnsConnectFunction callback; -} DnsqueryResolverprocess; - -static gboolean host_resolved(gpointer data) -{ - DnsqueryResolverprocess *req = (DnsqueryResolverprocess*)data; - GSList *hosts = NULL; - hosts = g_slist_append(hosts, GINT_TO_POINTER(req->addrlen)); - hosts = g_slist_append(hosts, req->addr); - req->callback(hosts, req->data, NULL); - g_free(req); - return FALSE; -} - -GaimDnsqueryData * -gaim_dnsquery_a(const char *hostname, int port, - GaimProxyDnsConnectFunction callback, gpointer data) -{ - GaimDnsqueryData *query_data; - struct sockaddr_in sin; - DnsqueryResolverprocess *req; - - if (!inet_aton(hostname, &sin.sin_addr)) - { - struct hostent *hp; - hp = gethostbyname(hostname); - if (hp == NULL) - { - gaim_debug_error("dnsquery", - "gaim_gethostbyname(\"%s\", %d) failed: %d\n", - hostname, port, h_errno); - return NULL; - } - memset(&sin, 0, sizeof(struct sockaddr_in)); - memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); - sin.sin_family = hp->h_addrtype; - } else - sin.sin_family = AF_INET; - sin.sin_port = htons(port); - - req = g_new(DnsqueryResolverprocess, 1); - req->addr = (struct sockaddr*) g_memdup(&sin, sizeof(sin)); - req->addrlen = sizeof(sin); - req->data = data; - req->callback = callback; - gaim_timeout_add(10, host_resolved, req); - - return query_data; -} - -#endif /* not __unix__ or __APPLE__ or _WIN32 */ - -void -gaim_dnsquery_destroy(GaimDnsqueryData *query_data) -{ - g_free(query_data); -} Deleted: branches/v2_0_0/src/dnsquery.h =================================================================== --- branches/v2_0_0/src/dnsquery.h 2006-08-17 08:41:44 UTC (rev 16812) +++ branches/v2_0_0/src/dnsquery.h 2006-08-17 10:04:19 UTC (rev 16813) @@ -1,76 +0,0 @@ -/** - * @file dnsquery.h DNS query API - * @ingroup core - * - * gaim - * - * Gaim is the legal property of its developers, whose names are too numerous - * to list here. Please refer to the COPYRIGHT file distributed with this - * source distribution. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ -#ifndef _GAIM_DNSQUERY_H_ -#define _GAIM_DNSQUERY_H_ - -#include <glib.h> - -typedef struct _GaimDnsqueryData GaimDnsqueryData; - -/** - * The "hosts" parameter is a linked list containing pairs of - * one size_t addrlen and one struct sockaddr *addr. - */ -typedef void (*GaimDnsqueryConnectFunction)(GSList *hosts, gpointer data, const char *error_message); - - -#ifdef __cplusplus -extern "C" { -#endif - -/**************************************************************************/ -/** @name DNS query API */ -/**************************************************************************/ -/*@{*/ - -/** - * Do an asynchronous DNS query. - * - * @param hostname The hostname to resolve - * @param port A portnumber which is stored in the struct sockaddr - * @param callback Callback to call after resolving - * @param data Extra data for the callback function - * - * @return NULL if there was an error, or a reference to a data - * structure that can be used to cancel the pending - * connection, if needed. - */ -GaimDnsqueryData *gaim_dnsquery_a(const char *hostname, int port, GaimDnsqueryConnectFunction callback, gpointer data); - -/** - * Cancel a DNS query. - * - * @param query_data A pointer to the DNS query data that you want - * to cancel. - */ -void gaim_dnsquery_destroy(GaimDnsqueryData *query_data); - -/*@}*/ - -#ifdef __cplusplus -} -#endif - -#endif /* _GAIM_DNSQUERY_H_ */ Modified: branches/v2_0_0/src/protocols/simple/simple.c =================================================================== --- branches/v2_0_0/src/protocols/simple/simple.c 2006-08-17 08:41:44 UTC (rev 16812) +++ branches/v2_0_0/src/protocols/simple/simple.c 2006-08-17 10:04:19 UTC (rev 16813) @@ -30,7 +30,6 @@ #include "blist.h" #include "conversation.h" #include "debug.h" -#include "dnsquery.h" #include "notify.h" #include "privacy.h" #include "prpl.h" @@ -1622,7 +1621,7 @@ } else { /* UDP */ gaim_debug_info("simple", "using udp with server %s and port %d\n", hostname, port); - gaim_dnsquery_a(hostname, port, simple_udp_host_resolved, sip); + gaim_gethostbyname_async(hostname, port, simple_udp_host_resolved, sip); } } Modified: branches/v2_0_0/src/proxy.c =================================================================== --- branches/v2_0_0/src/proxy.c 2006-08-17 08:41:44 UTC (rev 16812) +++ branches/v2_0_0/src/proxy.c 2006-08-17 10:04:19 UTC (rev 16813) @@ -31,7 +31,6 @@ #include "internal.h" #include "cipher.h" -#include "dnsquery.h" #include "debug.h" #include "notify.h" #include "ntlm.h" @@ -47,7 +46,6 @@ int fd; guint inpa; GaimProxyInfo *gpi; - GaimDnsqueryData *query_data; /** * This contains alternating length/char* values. The char* @@ -309,11 +307,6 @@ connect_infos = g_slist_remove(connect_infos, connect_info); - /* - if (connect_info->query_data != NULL) - gaim_dnsquery_destroy(connect_info->query_data); - */ - while (connect_info->hosts != NULL) { /* Discard the length... */ @@ -358,7 +351,681 @@ gaim_proxy_connect_info_destroy(connect_info); } +#if defined(__unix__) || defined(__APPLE__) + +/* + * This structure represents both a pending DNS request and + * a free child process. + */ +typedef struct { + char *host; + int port; + GaimProxyDnsConnectFunction callback; + gpointer data; + guint inpa; + int fd_in, fd_out; + pid_t dns_pid; +} pending_dns_request_t; + +static GSList *free_dns_children = NULL; +static GQueue *queued_requests = NULL; + +static int number_of_dns_children = 0; + +static const int MAX_DNS_CHILDREN = 2; + +typedef struct { + char hostname[512]; + int port; +} dns_params_t; + +typedef struct { + dns_params_t params; + GaimProxyDnsConnectFunction callback; + gpointer data; +} queued_dns_request_t; + +/* + * Begin the DNS resolver child process functions. + */ +#ifdef HAVE_SIGNAL_H static void +trap_gdb_bug() +{ + const char *message = + "Gaim's DNS child got a SIGTRAP signal.\n" + "This can be caused by trying to run gaim inside gdb.\n" + "There is a known gdb bug which prevents this. Supposedly gaim\n" + "should have detected you were using gdb and used an ugly hack,\n" + "check cope_with_gdb_brokenness() in proxy.c.\n\n" + "For more info about this bug, see http://sources.redhat.com/ml/gdb/2001-07/msg00349.html\n"; + fputs("\n* * *\n",stderr); + fputs(message,stderr); + fputs("* * *\n\n",stderr); + execlp("xmessage","xmessage","-center", message, NULL); + _exit(1); +} +#endif + +static void +cope_with_gdb_brokenness() +{ +#ifdef __linux__ + static gboolean already_done = FALSE; + char s[256], e[512]; + int n; + pid_t ppid; + + if(already_done) + return; + already_done = TRUE; + ppid = getppid(); + snprintf(s, sizeof(s), "/proc/%d/exe", ppid); + n = readlink(s, e, sizeof(e)); + if(n < 0) + return; + + e[MIN(n,sizeof(e)-1)] = '\0'; + + if(strstr(e,"gdb")) { + gaim_debug_info("dns", + "Debugger detected, performing useless query...\n"); + gethostbyname("x.x.x.x.x"); + } +#endif +} + +static void +gaim_dns_resolverthread(int child_out, int child_in, gboolean show_debug) +{ + dns_params_t dns_params; + const size_t zero = 0; + int rc; +#ifdef HAVE_GETADDRINFO + struct addrinfo hints, *res, *tmp; + char servname[20]; +#else + struct sockaddr_in sin; + const size_t addrlen = sizeof(sin); +#endif + +#ifdef HAVE_SIGNAL_H + signal(SIGHUP, SIG_DFL); + signal(SIGINT, SIG_DFL); + signal(SIGQUIT, SIG_DFL); + signal(SIGCHLD, SIG_DFL); + signal(SIGTERM, SIG_DFL); + signal(SIGTRAP, trap_gdb_bug); +#endif + + /* + * We resolve 1 host name for each iteration of this + * while loop. + * + * The top half of this reads in the hostname and port + * number from the socket with our parent. The bottom + * half of this resolves the IP (blocking) and sends + * the result back to our parent, when finished. + */ + while (1) { + const char ch = 'Y'; + fd_set fds; + struct timeval tv = { .tv_sec = 40 , .tv_usec = 0 }; + FD_ZERO(&fds); + FD_SET(child_in, &fds); + rc = select(child_in + 1, &fds, NULL, NULL, &tv); + if (!rc) { + if (show_debug) + printf("dns[%d]: nobody needs me... =(\n", getpid()); + break; + } + rc = read(child_in, &dns_params, sizeof(dns_params_t)); + if (rc < 0) { + perror("read()"); + break; + } + if (rc == 0) { + if (show_debug) + printf("dns[%d]: Oops, father has gone, wait for me, wait...!\n", getpid()); + _exit(0); + } + if (dns_params.hostname[0] == '\0') { + printf("dns[%d]: hostname = \"\" (port = %d)!!!\n", getpid(), dns_params.port); + _exit(1); + } + /* Tell our parent that we read the data successfully */ + write(child_out, &ch, sizeof(ch)); + + /* We have the hostname and port, now resolve the IP */ + +#ifdef HAVE_GETADDRINFO + g_snprintf(servname, sizeof(servname), "%d", dns_params.port); + memset(&hints, 0, sizeof(hints)); + + /* This is only used to convert a service + * name to a port number. As we know we are + * passing a number already, we know this + * value will not be really used by the C + * library. + */ + hints.ai_socktype = SOCK_STREAM; + rc = getaddrinfo(dns_params.hostname, servname, &hints, &res); + write(child_out, &rc, sizeof(rc)); + if (rc != 0) { + close(child_out); + if (show_debug) + printf("dns[%d] Error: getaddrinfo returned %d\n", + getpid(), rc); + dns_params.hostname[0] = '\0'; + continue; + } + tmp = res; + while (res) { + size_t ai_addrlen = res->ai_addrlen; + write(child_out, &ai_addrlen, sizeof(ai_addrlen)); + write(child_out, res->ai_addr, res->ai_addrlen); + res = res->ai_next; + } + freeaddrinfo(tmp); + write(child_out, &zero, sizeof(zero)); +#else + if (!inet_aton(dns_params.hostname, &sin.sin_addr)) { + struct hostent *hp; + if (!(hp = gethostbyname(dns_params.hostname))) { + write(child_out, &h_errno, sizeof(int)); + close(child_out); + if (show_debug) + printf("DNS Error: %d\n", h_errno); + _exit(0); + } + memset(&sin, 0, sizeof(struct sockaddr_in)); + memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); + sin.sin_family = hp->h_addrtype; + } else + sin.sin_family = AF_INET; + + sin.sin_port = htons(dns_params.port); + write(child_out, &addrlen, sizeof(addrlen)); + write(child_out, &sin, addrlen); + write(child_out, &zero, sizeof(zero)); +#endif + dns_params.hostname[0] = '\0'; + } + + close(child_out); + close(child_in); + + _exit(0); +} + +static pending_dns_request_t * +gaim_dns_new_resolverthread(gboolean show_debug) +{ + pending_dns_request_t *req; + int child_out[2], child_in[2]; + + /* Create pipes for communicating with the child process */ + if (pipe(child_out) || pipe(child_in)) { + gaim_debug_error("dns", + "Could not create pipes: %s\n", strerror(errno)); + return NULL; + } + + req = g_new(pending_dns_request_t, 1); + + cope_with_gdb_brokenness(); + + /* Fork! */ + req->dns_pid = fork(); + + /* If we are the child process... */ + if (req->dns_pid == 0) { + /* We should not access the parent's side of the pipes, so close them */ + close(child_out[0]); + close(child_in[1]); + + gaim_dns_resolverthread(child_out[1], child_in[0], show_debug); + /* The thread calls _exit() rather than returning, so we never get here */ + } + + /* We should not access the child's side of the pipes, so close them */ + close(child_out[1]); + close(child_in[0]); + if (req->dns_pid == -1) { + gaim_debug_error("dns", + "Could not create child process for DNS: %s\n", + strerror(errno)); + g_free(req); + return NULL; + } + + req->fd_out = child_out[0]; + req->fd_in = child_in[1]; + number_of_dns_children++; + gaim_debug_info("dns", + "Created new DNS child %d, there are now %d children.\n", + req->dns_pid, number_of_dns_children); + + return req; +} +/* + * End the DNS resolver child process functions. + */ + +/* + * Begin the functions for dealing with the DNS child processes. + */ +static void +req_free(pending_dns_request_t *req) +{ + g_return_if_fail(req != NULL); + + close(req->fd_in); + close(req->fd_out); + + g_free(req->host); + g_free(req); + + number_of_dns_children--; +} + +static int +send_dns_request_to_child(pending_dns_request_t *req, dns_params_t *dns_params) +{ + char ch; + int rc; + pid_t pid; + + /* This waitpid might return the child's PID if it has recently + * exited, or it might return an error if it exited "long + * enough" ago that it has already been reaped; in either + * instance, we can't use it. */ + if ((pid = waitpid (req->dns_pid, NULL, WNOHANG)) > 0) { + gaim_debug_warning("dns", + "DNS child %d no longer exists\n", req->dns_pid); + return -1; + } else if (pid < 0) { + gaim_debug_warning("dns", + "Wait for DNS child %d failed: %s\n", + req->dns_pid, strerror(errno)); + return -1; + } + + /* Let's contact this lost child! */ + rc = write(req->fd_in, dns_params, sizeof(*dns_params)); + if (rc < 0) { + gaim_debug_error("dns", + "Unable to write to DNS child %d: %d\n", + req->dns_pid, strerror(errno)); + close(req->fd_in); + return -1; + } + + g_return_val_if_fail(rc == sizeof(*dns_params), -1); + + /* Did you hear me? (This avoids some race conditions) */ + rc = read(req->fd_out, &ch, sizeof(ch)); + if (rc != 1 || ch != 'Y') + { + gaim_debug_warning("dns", + "DNS child %d not responding. Killing it!\n", + req->dns_pid); + kill(req->dns_pid, SIGKILL); + return -1; + } + + gaim_debug_info("dns", + "Successfully sent DNS request to child %d\n", req->dns_pid); + + return 0; +} + +static void +host_resolved(gpointer data, gint source, GaimInputCondition cond); + +static void +release_dns_child(pending_dns_request_t *req) +{ + g_free(req->host); + req->host = NULL; + + if (queued_requests && !g_queue_is_empty(queued_requests)) { + queued_dns_request_t *r = g_queue_pop_head(queued_requests); + req->host = g_strdup(r->params.hostname); + req->port = r->params.port; + req->callback = r->callback; + req->data = r->data; + + gaim_debug_info("dns", + "Processing queued DNS query for '%s' with child %d\n", + req->host, req->dns_pid); + + if (send_dns_request_to_child(req, &(r->params)) != 0) { + req_free(req); + req = NULL; + + gaim_debug_warning("dns", + "Intent of process queued query of '%s' failed, " + "requeueing...\n", r->params.hostname); + g_queue_push_head(queued_requests, r); + } else { + req->inpa = gaim_input_add(req->fd_out, GAIM_INPUT_READ, host_resolved, req); + g_free(r); + } + + } else { + req->host = NULL; + req->callback = NULL; + req->data = NULL; + free_dns_children = g_slist_append(free_dns_children, req); + } +} + +static void +host_resolved(gpointer data, gint source, GaimInputCondition cond) +{ + pending_dns_request_t *req = (pending_dns_request_t*)data; + int rc, err; + GSList *hosts = NULL; + struct sockaddr *addr = NULL; + size_t addrlen; + + gaim_debug_info("dns", "Got response for '%s'\n", req->host); + gaim_input_remove(req->inpa); + + rc = read(req->fd_out, &err, sizeof(err)); + if ((rc == 4) && (err != 0)) + { + char message[1024]; +#ifdef HAVE_GETADDRINFO + g_snprintf(message, sizeof(message), "DNS error: %s (pid=%d)", + gai_strerror(err), req->dns_pid); +#else + g_snprintf(message, sizeof(message), "DNS error: %d (pid=%d)", + err, req->dns_pid); +#endif + gaim_debug_error("dns", "%s\n", message); + req->callback(NULL, req->data, message); + release_dns_child(req); + return; + } + if (rc > 0) + { + while (rc > 0) { + rc = read(req->fd_out, &addrlen, sizeof(addrlen)); + if (rc > 0 && addrlen > 0) { + addr = g_malloc(addrlen); + rc = read(req->fd_out, addr, addrlen); + hosts = g_slist_append(hosts, GINT_TO_POINTER(addrlen)); + hosts = g_slist_append(hosts, addr); + } else { + break; + } + } + } else if (rc == -1) { + char message[1024]; + g_snprintf(message, sizeof(message), "Error reading from DNS child: %s",strerror(errno)); + gaim_debug_error("dns", "%s\n", message); + req->callback(NULL, req->data, message); + req_free(req); + return; + } else if (rc == 0) { + char message[1024]; + g_snprintf(message, sizeof(message), "EOF reading from DNS child"); + close(req->fd_out); + gaim_debug_error("dns", "%s\n", message); + req->callback(NULL, req->data, message); + req_free(req); + return; + } + +/* wait4(req->dns_pid, NULL, WNOHANG, NULL); */ + + req->callback(hosts, req->data, NULL); + + release_dns_child(req); +} +/* + * End the functions for dealing with the DNS child processes. + */ + +int +gaim_gethostbyname_async(const char *hostname, int port, GaimProxyDnsConnectFunction callback, gpointer data) +{ + pending_dns_request_t *req = NULL; + dns_params_t dns_params; + gchar *host_temp; + gboolean show_debug; + + show_debug = gaim_debug_is_enabled(); + + host_temp = g_strstrip(g_strdup(hostname)); + strncpy(dns_params.hostname, host_temp, sizeof(dns_params.hostname) - 1); + g_free(host_temp); + dns_params.hostname[sizeof(dns_params.hostname) - 1] = '\0'; + dns_params.port = port; + + /* + * If we have any children, attempt to have them perform the DNS + * query. If we're able to send the query to a child, then req + * will be set to the pending_dns_request_t. Otherwise, req will + * be NULL and we'll need to create a new DNS request child. + */ + while (free_dns_children != NULL) { + req = free_dns_children->data; + free_dns_children = g_slist_remove(free_dns_children, req); + + if (send_dns_request_to_child(req, &dns_params) == 0) + /* We found an acceptable child, yay */ + break; + + req_free(req); + req = NULL; + } + + /* We need to create a new DNS request child */ + if (req == NULL) { + if (number_of_dns_children >= MAX_DNS_CHILDREN) { + queued_dns_request_t *r = g_new(queued_dns_request_t, 1); + memcpy(&(r->params), &dns_params, sizeof(dns_params)); + r->callback = callback; + r->data = data; + if (!queued_requests) + queued_requests = g_queue_new(); + g_queue_push_tail(queued_requests, r); + + gaim_debug_info("dns", + "DNS query for '%s' queued\n", dns_params.hostname); + + return 0; + } + + req = gaim_dns_new_resolverthread(show_debug); + if (req == NULL) + { + gaim_debug_error("proxy", "oh dear, this is going to explode, I give up\n"); + return -1; + } + send_dns_request_to_child(req, &dns_params); + } + + req->host = g_strdup(hostname); + req->port = port; + req->callback = callback; + req->data = data; + req->inpa = gaim_input_add(req->fd_out, GAIM_INPUT_READ, host_resolved, req); + + return 0; +} + +#elif defined _WIN32 /* end __unix__ || __APPLE__ */ + +typedef struct _dns_tdata { + char *hostname; + int port; + GaimProxyDnsConnectFunction callback; + gpointer data; + GSList *hosts; + char *errmsg; +} dns_tdata; + +static gboolean dns_main_thread_cb(gpointer data) { + dns_tdata *td = (dns_tdata*)data; + if (td->errmsg != NULL) { + gaim_debug_info("dns", "%s\n", td->errmsg); + } + td->callback(td->hosts, td->data, td->errmsg); + g_free(td->hostname); + g_free(td->errmsg); + g_free(td); + return FALSE; +} + +static gpointer dns_thread(gpointer data) { + +#ifdef HAVE_GETADDRINFO + int rc; + struct addrinfo hints, *res, *tmp; + char servname[20]; +#else + struct sockaddr_in sin; + struct hostent *hp; +#endif + dns_tdata *td = (dns_tdata*)data; + +#ifdef HAVE_GETADDRINFO + g_snprintf(servname, sizeof(servname), "%d", td->port); + memset(&hints,0,sizeof(hints)); + + /* This is only used to convert a service + * name to a port number. As we know we are + * passing a number already, we know this + * value will not be really used by the C + * library. + */ + hints.ai_socktype = SOCK_STREAM; + if ((rc = getaddrinfo(td->hostname, servname, &hints, &res)) == 0) { + tmp = res; + while(res) { + td->hosts = g_slist_append(td->hosts, + GSIZE_TO_POINTER(res->ai_addrlen)); + td->hosts = g_slist_append(td->hosts, + g_memdup(res->ai_addr, res->ai_addrlen)); + res = res->ai_next; + } + freeaddrinfo(tmp); + } else { + td->errmsg = g_strdup_printf("DNS getaddrinfo(\"%s\", \"%s\") error: %d", td->hostname, servname, rc); + } +#else + if ((hp = gethostbyname(td->hostname))) { + memset(&sin, 0, sizeof(struct sockaddr_in)); + memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); + sin.sin_family = hp->h_addrtype; + sin.sin_port = htons(td->port); + + td->hosts = g_slist_append(td->hosts, + GSIZE_TO_POINTER(sizeof(sin))); + td->hosts = g_slist_append(td->hosts, + g_memdup(&sin, sizeof(sin))); + } else { + td->errmsg = g_strdup_printf("DNS gethostbyname(\"%s\") error: %d", td->hostname, h_errno); + } +#endif + /* back to main thread */ + g_idle_add(dns_main_thread_cb, td); + return 0; +} + +int +gaim_gethostbyname_async(const char *hostname, int port, + GaimProxyDnsConnectFunction callback, gpointer data) +{ + dns_tdata *td; + struct sockaddr_in sin; + GError* err = NULL; + + if(inet_aton(hostname, &sin.sin_addr)) { + GSList *hosts = NULL; + sin.sin_family = AF_INET; + sin.sin_port = htons(port); + hosts = g_slist_append(hosts, GINT_TO_POINTER(sizeof(sin))); + hosts = g_slist_append(hosts, g_memdup(&sin, sizeof(sin))); + callback(hosts, data, NULL); + return 0; + } + + gaim_debug_info("dns", "DNS Lookup for: %s\n", hostname); + td = g_new0(dns_tdata, 1); + td->hostname = g_strdup(hostname); + td->port = port; + td->callback = callback; + td->data = data; + + if(!g_thread_create(dns_thread, td, FALSE, &err)) { + gaim_debug_error("dns", "DNS thread create failure: %s\n", err?err->message:""); + g_error_free(err); + g_free(td->hostname); + g_free(td); + return -1; + } + return 0; +} + +#else /* not __unix__ or __APPLE__ or _WIN32 */ + +typedef struct { + gpointer data; + size_t addrlen; + struct sockaddr *addr; + GaimProxyDnsConnectFunction callback; +} pending_dns_request_t; + +static gboolean host_resolved(gpointer data) +{ + pending_dns_request_t *req = (pending_dns_request_t*)data; + GSList *hosts = NULL; + hosts = g_slist_append(hosts, GINT_TO_POINTER(req->addrlen)); + hosts = g_slist_append(hosts, req->addr); + req->callback(hosts, req->data, NULL); + g_free(req); + return FALSE; +} + +int +gaim_gethostbyname_async(const char *hostname, int port, + GaimProxyDnsConnectFunction callback, gpointer data) +{ + struct sockaddr_in sin; + pending_dns_request_t *req; + + if (!inet_aton(hostname, &sin.sin_addr)) { + struct hostent *hp; + if(!(hp = gethostbyname(hostname))) { + gaim_debug_error("dns", + "gaim_gethostbyname(\"%s\", %d) failed: %d\n", + hostname, port, h_errno); + return -1; + } + memset(&sin, 0, sizeof(struct sockaddr_in)); + memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); + sin.sin_family = hp->h_addrtype; + } else + sin.sin_family = AF_INET; + sin.sin_port = htons(port); + + req = g_new(pending_dns_request_t, 1); + req->addr = (struct sockaddr*) g_memdup(&sin, sizeof(sin)); + req->addrlen = sizeof(sin); + req->data = data; + req->callback = callback; + gaim_timeout_add(10, host_resolved, req); + return 0; +} + +#endif /* not __unix__ or __APPLE__ or _WIN32 */ + +static void no_one_calls(gpointer data, gint source, GaimInputCondition cond) { GaimProxyConnectInfo *connect_info = data; @@ -1701,9 +2368,8 @@ return NULL; } - connect_info->query_data = gaim_dnsquery_a(connecthost, - connectport, connection_host_resolved, connect_info); - if (connect_info->query_data == NULL) + if (gaim_gethostbyname_async(connecthost, + connectport, connection_host_resolved, connect_info) != 0) { gaim_proxy_connect_info_destroy(connect_info); return NULL; @@ -1735,9 +2401,8 @@ connect_info->port = port; connect_info->gpi = gpi; - connect_info->query_data = gaim_dnsquery_a(gaim_proxy_info_get_host(gpi), - gaim_proxy_info_get_port(gpi), connection_host_resolved, connect_info); - if (connect_info->query_data == NULL) + if (gaim_gethostbyname_async(gaim_proxy_info_get_host(gpi), + gaim_proxy_info_get_port(gpi), connection_host_resolved, connect_info) != 0) { gaim_proxy_connect_info_destroy(connect_info); return NULL; Modified: branches/v2_0_0/src/proxy.h =================================================================== --- branches/v2_0_0/src/proxy.h 2006-08-17 08:41:44 UTC (rev 16812) +++ branches/v2_0_0/src/proxy.h 2006-08-17 10:04:19 UTC (rev 16813) @@ -26,7 +26,6 @@ #define _GAIM_PROXY_H_ #include <glib.h> -#include "dnsquery.h" #include "eventloop.h" /** @@ -57,7 +56,6 @@ } GaimProxyInfo; -typedef struct _GaimDnsQueryData GaimDnsQueryData; typedef struct _GaimProxyConnectInfo GaimProxyConnectInfo; typedef void (*GaimProxyConnectFunction)(gpointer data, gint source, const gchar *error_message); @@ -280,6 +278,18 @@ */ void gaim_proxy_connect_cancel(GaimProxyConnectInfo *connect_info); +/** + * Do an async dns query + * + * @param hostname The hostname to resolve + * @param port A portnumber which is stored in the struct sockaddr + * @param callback Callback to call after resolving + * @param data Extra data for the callback function + * + * @return Zero indicates the connection is pending. Any other value indicates failure. + */ +int gaim_gethostbyname_async(const char *hostname, int port, GaimProxyDnsConnectFunction callback, gpointer data); + /*@}*/ #ifdef __cplusplus Modified: branches/v2_0_0/src/stun.c =================================================================== --- branches/v2_0_0/src/stun.c 2006-08-17 08:41:44 UTC (rev 16812) +++ branches/v2_0_0/src/stun.c 2006-08-17 10:04:19 UTC (rev 16813) @@ -40,7 +40,6 @@ #include "debug.h" #include "account.h" -#include "dnsquery.h" #include "dnssrv.h" #include "network.h" #include "proxy.h" @@ -360,7 +359,7 @@ gaim_debug_info("stun", "got %d SRV responses, server: %s, port: %d\n", results, servername, port); - gaim_dnsquery_a(servername, port, hbn_cb, NULL); + gaim_gethostbyname_async(servername, port, hbn_cb, NULL); g_free(resp); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <the...@us...> - 2006-08-18 09:03:36
|
Revision: 16837 Author: thekingant Date: 2006-08-18 02:03:13 -0700 (Fri, 18 Aug 2006) ViewCVS: http://svn.sourceforge.net/gaim/?rev=16837&view=rev Log Message: ----------- Backport SVN revisions 16833-16836 from HEAD to v2_0_0 Original commit messages: 16833: Split the DNS query code out into its own file without actually 16834: Only doing 2 DNS requests at a time seems a little restrictive. 16835: Change the DNS query function to return a struct that will eventually allow the query to be canceled. Also rename two of the DNS query things 16836: Not quite sure how I managed to do this Modified Paths: -------------- branches/v2_0_0/src/Makefile.am branches/v2_0_0/src/Makefile.mingw branches/v2_0_0/src/protocols/oscar/oscar.c branches/v2_0_0/src/protocols/simple/simple.c branches/v2_0_0/src/proxy.c branches/v2_0_0/src/proxy.h branches/v2_0_0/src/stun.c Added Paths: ----------- branches/v2_0_0/src/dnsquery.c branches/v2_0_0/src/dnsquery.h Modified: branches/v2_0_0/src/Makefile.am =================================================================== --- branches/v2_0_0/src/Makefile.am 2006-08-18 08:55:08 UTC (rev 16836) +++ branches/v2_0_0/src/Makefile.am 2006-08-18 09:03:13 UTC (rev 16837) @@ -77,6 +77,7 @@ core.c \ debug.c \ desktopitem.c \ + dnsquery.c \ eventloop.c \ ft.c \ idle.c \ @@ -125,6 +126,7 @@ dbus-maybe.h \ debug.h \ desktopitem.h \ + dnsquery.h \ eventloop.h \ ft.h \ idle.h \ Modified: branches/v2_0_0/src/Makefile.mingw =================================================================== --- branches/v2_0_0/src/Makefile.mingw 2006-08-18 08:55:08 UTC (rev 16836) +++ branches/v2_0_0/src/Makefile.mingw 2006-08-18 09:03:13 UTC (rev 16837) @@ -95,6 +95,7 @@ conversation.c \ core.c \ debug.c \ + dnsquery.c \ dnssrv.c \ eventloop.c \ ft.c \ Copied: branches/v2_0_0/src/dnsquery.c (from rev 16835, trunk/src/dnsquery.c) =================================================================== --- branches/v2_0_0/src/dnsquery.c (rev 0) +++ branches/v2_0_0/src/dnsquery.c 2006-08-18 09:03:13 UTC (rev 16837) @@ -0,0 +1,713 @@ +/** + * @file dnsquery.c DNS query API + * @ingroup core + * + * gaim + * + * Gaim is the legal property of its developers, whose names are too numerous + * to list here. Please refer to the COPYRIGHT file distributed with this + * source distribution. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include "internal.h" +#include "debug.h" +#include "notify.h" +#include "prefs.h" +#include "dnsquery.h" +#include "util.h" + +/************************************************************************** + * DNS query API + **************************************************************************/ + +struct _GaimDnsQueryData { +}; + +#if defined(__unix__) || defined(__APPLE__) + +#define MAX_DNS_CHILDREN 4 + +/* + * This structure represents both a pending DNS request and + * a free child process. + */ +typedef struct { + char *host; + int port; + GaimDnsQueryConnectFunction callback; + gpointer data; + guint inpa; + int fd_in, fd_out; + pid_t dns_pid; +} pending_dns_request_t; + +static GSList *free_dns_children = NULL; +static GQueue *queued_requests = NULL; + +static int number_of_dns_children = 0; + +typedef struct { + char hostname[512]; + int port; +} dns_params_t; + +typedef struct { + dns_params_t params; + GaimDnsQueryConnectFunction callback; + gpointer data; +} queued_dns_request_t; + +/* + * Begin the DNS resolver child process functions. + */ +#ifdef HAVE_SIGNAL_H +static void +trap_gdb_bug() +{ + const char *message = + "Gaim's DNS child got a SIGTRAP signal.\n" + "This can be caused by trying to run gaim inside gdb.\n" + "There is a known gdb bug which prevents this. Supposedly gaim\n" + "should have detected you were using gdb and used an ugly hack,\n" + "check cope_with_gdb_brokenness() in dnsquery.c.\n\n" + "For more info about this bug, see http://sources.redhat.com/ml/gdb/2001-07/msg00349.html\n"; + fputs("\n* * *\n",stderr); + fputs(message,stderr); + fputs("* * *\n\n",stderr); + execlp("xmessage","xmessage","-center", message, NULL); + _exit(1); +} +#endif + +static void +cope_with_gdb_brokenness() +{ +#ifdef __linux__ + static gboolean already_done = FALSE; + char s[256], e[512]; + int n; + pid_t ppid; + + if(already_done) + return; + already_done = TRUE; + ppid = getppid(); + snprintf(s, sizeof(s), "/proc/%d/exe", ppid); + n = readlink(s, e, sizeof(e)); + if(n < 0) + return; + + e[MIN(n,sizeof(e)-1)] = '\0'; + + if(strstr(e,"gdb")) { + gaim_debug_info("dns", + "Debugger detected, performing useless query...\n"); + gethostbyname("x.x.x.x.x"); + } +#endif +} + +static void +gaim_dns_resolverthread(int child_out, int child_in, gboolean show_debug) +{ + dns_params_t dns_params; + const size_t zero = 0; + int rc; +#ifdef HAVE_GETADDRINFO + struct addrinfo hints, *res, *tmp; + char servname[20]; +#else + struct sockaddr_in sin; + const size_t addrlen = sizeof(sin); +#endif + +#ifdef HAVE_SIGNAL_H + signal(SIGHUP, SIG_DFL); + signal(SIGINT, SIG_DFL); + signal(SIGQUIT, SIG_DFL); + signal(SIGCHLD, SIG_DFL); + signal(SIGTERM, SIG_DFL); + signal(SIGTRAP, trap_gdb_bug); +#endif + + /* + * We resolve 1 host name for each iteration of this + * while loop. + * + * The top half of this reads in the hostname and port + * number from the socket with our parent. The bottom + * half of this resolves the IP (blocking) and sends + * the result back to our parent, when finished. + */ + while (1) { + const char ch = 'Y'; + fd_set fds; + struct timeval tv = { .tv_sec = 40 , .tv_usec = 0 }; + FD_ZERO(&fds); + FD_SET(child_in, &fds); + rc = select(child_in + 1, &fds, NULL, NULL, &tv); + if (!rc) { + if (show_debug) + printf("dns[%d]: nobody needs me... =(\n", getpid()); + break; + } + rc = read(child_in, &dns_params, sizeof(dns_params_t)); + if (rc < 0) { + perror("read()"); + break; + } + if (rc == 0) { + if (show_debug) + printf("dns[%d]: Oops, father has gone, wait for me, wait...!\n", getpid()); + _exit(0); + } + if (dns_params.hostname[0] == '\0') { + printf("dns[%d]: hostname = \"\" (port = %d)!!!\n", getpid(), dns_params.port); + _exit(1); + } + /* Tell our parent that we read the data successfully */ + write(child_out, &ch, sizeof(ch)); + + /* We have the hostname and port, now resolve the IP */ + +#ifdef HAVE_GETADDRINFO + g_snprintf(servname, sizeof(servname), "%d", dns_params.port); + memset(&hints, 0, sizeof(hints)); + + /* This is only used to convert a service + * name to a port number. As we know we are + * passing a number already, we know this + * value will not be really used by the C + * library. + */ + hints.ai_socktype = SOCK_STREAM; + rc = getaddrinfo(dns_params.hostname, servname, &hints, &res); + write(child_out, &rc, sizeof(rc)); + if (rc != 0) { + close(child_out); + if (show_debug) + printf("dns[%d] Error: getaddrinfo returned %d\n", + getpid(), rc); + dns_params.hostname[0] = '\0'; + continue; + } + tmp = res; + while (res) { + size_t ai_addrlen = res->ai_addrlen; + write(child_out, &ai_addrlen, sizeof(ai_addrlen)); + write(child_out, res->ai_addr, res->ai_addrlen); + res = res->ai_next; + } + freeaddrinfo(tmp); + write(child_out, &zero, sizeof(zero)); +#else + if (!inet_aton(dns_params.hostname, &sin.sin_addr)) { + struct hostent *hp; + if (!(hp = gethostbyname(dns_params.hostname))) { + write(child_out, &h_errno, sizeof(int)); + close(child_out); + if (show_debug) + printf("DNS Error: %d\n", h_errno); + _exit(0); + } + memset(&sin, 0, sizeof(struct sockaddr_in)); + memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); + sin.sin_family = hp->h_addrtype; + } else + sin.sin_family = AF_INET; + + sin.sin_port = htons(dns_params.port); + write(child_out, &addrlen, sizeof(addrlen)); + write(child_out, &sin, addrlen); + write(child_out, &zero, sizeof(zero)); +#endif + dns_params.hostname[0] = '\0'; + } + + close(child_out); + close(child_in); + + _exit(0); +} + +static pending_dns_request_t * +gaim_dns_new_resolverthread(gboolean show_debug) +{ + pending_dns_request_t *req; + int child_out[2], child_in[2]; + + /* Create pipes for communicating with the child process */ + if (pipe(child_out) || pipe(child_in)) { + gaim_debug_error("dns", + "Could not create pipes: %s\n", strerror(errno)); + return NULL; + } + + req = g_new(pending_dns_request_t, 1); + + cope_with_gdb_brokenness(); + + /* Fork! */ + req->dns_pid = fork(); + + /* If we are the child process... */ + if (req->dns_pid == 0) { + /* We should not access the parent's side of the pipes, so close them */ + close(child_out[0]); + close(child_in[1]); + + gaim_dns_resolverthread(child_out[1], child_in[0], show_debug); + /* The thread calls _exit() rather than returning, so we never get here */ + } + + /* We should not access the child's side of the pipes, so close them */ + close(child_out[1]); + close(child_in[0]); + if (req->dns_pid == -1) { + gaim_debug_error("dns", + "Could not create child process for DNS: %s\n", + strerror(errno)); + g_free(req); + return NULL; + } + + req->fd_out = child_out[0]; + req->fd_in = child_in[1]; + number_of_dns_children++; + gaim_debug_info("dns", + "Created new DNS child %d, there are now %d children.\n", + req->dns_pid, number_of_dns_children); + + return req; +} +/* + * End the DNS resolver child process functions. + */ + +/* + * Begin the functions for dealing with the DNS child processes. + */ +static void +req_free(pending_dns_request_t *req) +{ + g_return_if_fail(req != NULL); + + close(req->fd_in); + close(req->fd_out); + + g_free(req->host); + g_free(req); + + number_of_dns_children--; +} + +static int +send_dns_request_to_child(pending_dns_request_t *req, dns_params_t *dns_params) +{ + char ch; + int rc; + pid_t pid; + + /* This waitpid might return the child's PID if it has recently + * exited, or it might return an error if it exited "long + * enough" ago that it has already been reaped; in either + * instance, we can't use it. */ + if ((pid = waitpid (req->dns_pid, NULL, WNOHANG)) > 0) { + gaim_debug_warning("dns", + "DNS child %d no longer exists\n", req->dns_pid); + return -1; + } else if (pid < 0) { + gaim_debug_warning("dns", + "Wait for DNS child %d failed: %s\n", + req->dns_pid, strerror(errno)); + return -1; + } + + /* Let's contact this lost child! */ + rc = write(req->fd_in, dns_params, sizeof(*dns_params)); + if (rc < 0) { + gaim_debug_error("dns", + "Unable to write to DNS child %d: %d\n", + req->dns_pid, strerror(errno)); + close(req->fd_in); + return -1; + } + + g_return_val_if_fail(rc == sizeof(*dns_params), -1); + + /* Did you hear me? (This avoids some race conditions) */ + rc = read(req->fd_out, &ch, sizeof(ch)); + if (rc != 1 || ch != 'Y') + { + gaim_debug_warning("dns", + "DNS child %d not responding. Killing it!\n", + req->dns_pid); + kill(req->dns_pid, SIGKILL); + return -1; + } + + gaim_debug_info("dns", + "Successfully sent DNS request to child %d\n", req->dns_pid); + + return 0; +} + +static void +host_resolved(gpointer data, gint source, GaimInputCondition cond); + +static void +release_dns_child(pending_dns_request_t *req) +{ + g_free(req->host); + req->host = NULL; + + if (queued_requests && !g_queue_is_empty(queued_requests)) { + queued_dns_request_t *r = g_queue_pop_head(queued_requests); + req->host = g_strdup(r->params.hostname); + req->port = r->params.port; + req->callback = r->callback; + req->data = r->data; + + gaim_debug_info("dns", + "Processing queued DNS query for '%s' with child %d\n", + req->host, req->dns_pid); + + if (send_dns_request_to_child(req, &(r->params)) != 0) { + req_free(req); + req = NULL; + + gaim_debug_warning("dns", + "Intent of process queued query of '%s' failed, " + "requeueing...\n", r->params.hostname); + g_queue_push_head(queued_requests, r); + } else { + req->inpa = gaim_input_add(req->fd_out, GAIM_INPUT_READ, host_resolved, req); + g_free(r); + } + + } else { + req->host = NULL; + req->callback = NULL; + req->data = NULL; + free_dns_children = g_slist_append(free_dns_children, req); + } +} + +static void +host_resolved(gpointer data, gint source, GaimInputCondition cond) +{ + pending_dns_request_t *req = (pending_dns_request_t*)data; + int rc, err; + GSList *hosts = NULL; + struct sockaddr *addr = NULL; + size_t addrlen; + + gaim_debug_info("dns", "Got response for '%s'\n", req->host); + gaim_input_remove(req->inpa); + + rc = read(req->fd_out, &err, sizeof(err)); + if ((rc == 4) && (err != 0)) + { + char message[1024]; +#ifdef HAVE_GETADDRINFO + g_snprintf(message, sizeof(message), "DNS error: %s (pid=%d)", + gai_strerror(err), req->dns_pid); +#else + g_snprintf(message, sizeof(message), "DNS error: %d (pid=%d)", + err, req->dns_pid); +#endif + gaim_debug_error("dns", "%s\n", message); + req->callback(NULL, req->data, message); + release_dns_child(req); + return; + } + if (rc > 0) + { + while (rc > 0) { + rc = read(req->fd_out, &addrlen, sizeof(addrlen)); + if (rc > 0 && addrlen > 0) { + addr = g_malloc(addrlen); + rc = read(req->fd_out, addr, addrlen); + hosts = g_slist_append(hosts, GINT_TO_POINTER(addrlen)); + hosts = g_slist_append(hosts, addr); + } else { + break; + } + } + } else if (rc == -1) { + char message[1024]; + g_snprintf(message, sizeof(message), "Error reading from DNS child: %s",strerror(errno)); + gaim_debug_error("dns", "%s\n", message); + req->callback(NULL, req->data, message); + req_free(req); + return; + } else if (rc == 0) { + char message[1024]; + g_snprintf(message, sizeof(message), "EOF reading from DNS child"); + close(req->fd_out); + gaim_debug_error("dns", "%s\n", message); + req->callback(NULL, req->data, message); + req_free(req); + return; + } + +/* wait4(req->dns_pid, NULL, WNOHANG, NULL); */ + + req->callback(hosts, req->data, NULL); + + release_dns_child(req); +} +/* + * End the functions for dealing with the DNS child processes. + */ + +GaimDnsQueryData * +gaim_dnsquery_a(const char *hostname, int port, GaimDnsQueryConnectFunction callback, gpointer data) +{ + pending_dns_request_t *req = NULL; + dns_params_t dns_params; + gchar *host_temp; + gboolean show_debug; + + show_debug = gaim_debug_is_enabled(); + + host_temp = g_strstrip(g_strdup(hostname)); + strncpy(dns_params.hostname, host_temp, sizeof(dns_params.hostname) - 1); + g_free(host_temp); + dns_params.hostname[sizeof(dns_params.hostname) - 1] = '\0'; + dns_params.port = port; + + /* + * If we have any children, attempt to have them perform the DNS + * query. If we're able to send the query to a child, then req + * will be set to the pending_dns_request_t. Otherwise, req will + * be NULL and we'll need to create a new DNS request child. + */ + while (free_dns_children != NULL) { + req = free_dns_children->data; + free_dns_children = g_slist_remove(free_dns_children, req); + + if (send_dns_request_to_child(req, &dns_params) == 0) + /* We found an acceptable child, yay */ + break; + + req_free(req); + req = NULL; + } + + /* We need to create a new DNS request child */ + if (req == NULL) { + if (number_of_dns_children >= MAX_DNS_CHILDREN) { + queued_dns_request_t *r = g_new(queued_dns_request_t, 1); + memcpy(&(r->params), &dns_params, sizeof(dns_params)); + r->callback = callback; + r->data = data; + if (!queued_requests) + queued_requests = g_queue_new(); + g_queue_push_tail(queued_requests, r); + + gaim_debug_info("dns", + "DNS query for '%s' queued\n", dns_params.hostname); + + return (GaimDnsQueryData *)1; + } + + req = gaim_dns_new_resolverthread(show_debug); + if (req == NULL) + { + gaim_debug_error("proxy", "oh dear, this is going to explode, I give up\n"); + return NULL; + } + send_dns_request_to_child(req, &dns_params); + } + + req->host = g_strdup(hostname); + req->port = port; + req->callback = callback; + req->data = data; + req->inpa = gaim_input_add(req->fd_out, GAIM_INPUT_READ, host_resolved, req); + + return (GaimDnsQueryData *)1; +} + +#elif defined _WIN32 /* end __unix__ || __APPLE__ */ + +typedef struct _dns_tdata { + char *hostname; + int port; + GaimDnsQueryConnectFunction callback; + gpointer data; + GSList *hosts; + char *errmsg; +} dns_tdata; + +static gboolean dns_main_thread_cb(gpointer data) { + dns_tdata *td = (dns_tdata*)data; + if (td->errmsg != NULL) { + gaim_debug_info("dns", "%s\n", td->errmsg); + } + td->callback(td->hosts, td->data, td->errmsg); + g_free(td->hostname); + g_free(td->errmsg); + g_free(td); + return FALSE; +} + +static gpointer dns_thread(gpointer data) { + +#ifdef HAVE_GETADDRINFO + int rc; + struct addrinfo hints, *res, *tmp; + char servname[20]; +#else + struct sockaddr_in sin; + struct hostent *hp; +#endif + dns_tdata *td = (dns_tdata*)data; + +#ifdef HAVE_GETADDRINFO + g_snprintf(servname, sizeof(servname), "%d", td->port); + memset(&hints,0,sizeof(hints)); + + /* This is only used to convert a service + * name to a port number. As we know we are + * passing a number already, we know this + * value will not be really used by the C + * library. + */ + hints.ai_socktype = SOCK_STREAM; + if ((rc = getaddrinfo(td->hostname, servname, &hints, &res)) == 0) { + tmp = res; + while(res) { + td->hosts = g_slist_append(td->hosts, + GSIZE_TO_POINTER(res->ai_addrlen)); + td->hosts = g_slist_append(td->hosts, + g_memdup(res->ai_addr, res->ai_addrlen)); + res = res->ai_next; + } + freeaddrinfo(tmp); + } else { + td->errmsg = g_strdup_printf("DNS getaddrinfo(\"%s\", \"%s\") error: %d", td->hostname, servname, rc); + } +#else + if ((hp = gethostbyname(td->hostname))) { + memset(&sin, 0, sizeof(struct sockaddr_in)); + memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); + sin.sin_family = hp->h_addrtype; + sin.sin_port = htons(td->port); + + td->hosts = g_slist_append(td->hosts, + GSIZE_TO_POINTER(sizeof(sin))); + td->hosts = g_slist_append(td->hosts, + g_memdup(&sin, sizeof(sin))); + } else { + td->errmsg = g_strdup_printf("DNS gethostbyname(\"%s\") error: %d", td->hostname, h_errno); + } +#endif + /* back to main thread */ + g_idle_add(dns_main_thread_cb, td); + return 0; +} + +GaimDnsQueryData * +gaim_dnsquery_a(const char *hostname, int port, + GaimDnsQueryConnectFunction callback, gpointer data) +{ + dns_tdata *td; + struct sockaddr_in sin; + GError* err = NULL; + + if(inet_aton(hostname, &sin.sin_addr)) { + GSList *hosts = NULL; + sin.sin_family = AF_INET; + sin.sin_port = htons(port); + hosts = g_slist_append(hosts, GINT_TO_POINTER(sizeof(sin))); + hosts = g_slist_append(hosts, g_memdup(&sin, sizeof(sin))); + callback(hosts, data, NULL); + return (GaimDnsQueryData *)1; + } + + gaim_debug_info("dns", "DNS Lookup for: %s\n", hostname); + td = g_new0(dns_tdata, 1); + td->hostname = g_strdup(hostname); + td->port = port; + td->callback = callback; + td->data = data; + + if(!g_thread_create(dns_thread, td, FALSE, &err)) { + gaim_debug_error("dns", "DNS thread create failure: %s\n", err?err->message:""); + g_error_free(err); + g_free(td->hostname); + g_free(td); + return NULL; + } + return (GaimDnsQueryData *)1; +} + +#else /* not __unix__ or __APPLE__ or _WIN32 */ + +typedef struct { + gpointer data; + size_t addrlen; + struct sockaddr *addr; + GaimDnsQueryConnectFunction callback; +} pending_dns_request_t; + +static gboolean host_resolved(gpointer data) +{ + pending_dns_request_t *req = (pending_dns_request_t*)data; + GSList *hosts = NULL; + hosts = g_slist_append(hosts, GINT_TO_POINTER(req->addrlen)); + hosts = g_slist_append(hosts, req->addr); + req->callback(hosts, req->data, NULL); + g_free(req); + return FALSE; +} + +GaimDnsQueryData * +gaim_dnsquery_a(const char *hostname, int port, + GaimDnsQueryConnectFunction callback, gpointer data) +{ + struct sockaddr_in sin; + pending_dns_request_t *req; + + if (!inet_aton(hostname, &sin.sin_addr)) { + struct hostent *hp; + if(!(hp = gethostbyname(hostname))) { + gaim_debug_error("dns", + "gaim_gethostbyname(\"%s\", %d) failed: %d\n", + hostname, port, h_errno); + return NULL; + } + memset(&sin, 0, sizeof(struct sockaddr_in)); + memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); + sin.sin_family = hp->h_addrtype; + } else + sin.sin_family = AF_INET; + sin.sin_port = htons(port); + + req = g_new(pending_dns_request_t, 1); + req->addr = (struct sockaddr*) g_memdup(&sin, sizeof(sin)); + req->addrlen = sizeof(sin); + req->data = data; + req->callback = callback; + gaim_timeout_add(10, host_resolved, req); + return (GaimDnsQueryData *)1; +} + +#endif /* not __unix__ or __APPLE__ or _WIN32 */ Copied: branches/v2_0_0/src/dnsquery.h (from rev 16835, trunk/src/dnsquery.h) =================================================================== --- branches/v2_0_0/src/dnsquery.h (rev 0) +++ branches/v2_0_0/src/dnsquery.h 2006-08-18 09:03:13 UTC (rev 16837) @@ -0,0 +1,71 @@ +/** + * @file dnsquery.h DNS query API + * @ingroup core + * + * gaim + * + * Gaim is the legal property of its developers, whose names are too numerous + * to list here. Please refer to the COPYRIGHT file distributed with this + * source distribution. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifndef _GAIM_DNSQUERY_H_ +#define _GAIM_DNSQUERY_H_ + +#include <glib.h> +#include "eventloop.h" + +typedef struct _GaimDnsQueryData GaimDnsQueryData; + +/** + * The "hosts" parameter is a linked list containing pairs of + * one size_t addrlen and one struct sockaddr *addr. + */ +typedef void (*GaimDnsQueryConnectFunction)(GSList *hosts, gpointer data, const char *error_message); + + +#include "account.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/**************************************************************************/ +/** @name DNS query API */ +/**************************************************************************/ +/*@{*/ + +/** + * Do an async dns query + * + * @param hostname The hostname to resolve + * @param port A portnumber which is stored in the struct sockaddr + * @param callback Callback to call after resolving + * @param data Extra data for the callback function + * + * @return NULL if there was an error, otherwise return a reference to + * a data structure that can be used to cancel the pending + * DNS query, if needed. + */ +GaimDnsQueryData *gaim_dnsquery_a(const char *hostname, int port, GaimDnsQueryConnectFunction callback, gpointer data); + +/*@}*/ + +#ifdef __cplusplus +} +#endif + +#endif /* _GAIM_DNSQUERY_H_ */ Modified: branches/v2_0_0/src/protocols/oscar/oscar.c =================================================================== --- branches/v2_0_0/src/protocols/oscar/oscar.c 2006-08-18 08:55:08 UTC (rev 16836) +++ branches/v2_0_0/src/protocols/oscar/oscar.c 2006-08-18 09:03:13 UTC (rev 16837) @@ -2887,7 +2887,7 @@ } else /* Set an empty message so that we know not to show "pending" */ - gaim_status_set_attr_string(status, "message", message); + gaim_status_set_attr_string(status, "message", ""); gaim_blist_update_buddy_status(b, status); } Modified: branches/v2_0_0/src/protocols/simple/simple.c =================================================================== --- branches/v2_0_0/src/protocols/simple/simple.c 2006-08-18 08:55:08 UTC (rev 16836) +++ branches/v2_0_0/src/protocols/simple/simple.c 2006-08-18 09:03:13 UTC (rev 16837) @@ -29,6 +29,7 @@ #include "accountopt.h" #include "blist.h" #include "conversation.h" +#include "dnsquery.h" #include "debug.h" #include "notify.h" #include "privacy.h" @@ -1621,7 +1622,7 @@ } else { /* UDP */ gaim_debug_info("simple", "using udp with server %s and port %d\n", hostname, port); - gaim_gethostbyname_async(hostname, port, simple_udp_host_resolved, sip); + gaim_dnsquery_a(hostname, port, simple_udp_host_resolved, sip); } } Modified: branches/v2_0_0/src/proxy.c =================================================================== --- branches/v2_0_0/src/proxy.c 2006-08-18 08:55:08 UTC (rev 16836) +++ branches/v2_0_0/src/proxy.c 2006-08-18 09:03:13 UTC (rev 16837) @@ -32,6 +32,7 @@ #include "internal.h" #include "cipher.h" #include "debug.h" +#include "dnsquery.h" #include "notify.h" #include "ntlm.h" #include "prefs.h" @@ -46,6 +47,7 @@ int fd; guint inpa; GaimProxyInfo *gpi; + GaimDnsQueryData *query_data; /** * This contains alternating length/char* values. The char* @@ -351,681 +353,7 @@ gaim_proxy_connect_info_destroy(connect_info); } -#if defined(__unix__) || defined(__APPLE__) - -/* - * This structure represents both a pending DNS request and - * a free child process. - */ -typedef struct { - char *host; - int port; - GaimProxyDnsConnectFunction callback; - gpointer data; - guint inpa; - int fd_in, fd_out; - pid_t dns_pid; -} pending_dns_request_t; - -static GSList *free_dns_children = NULL; -static GQueue *queued_requests = NULL; - -static int number_of_dns_children = 0; - -static const int MAX_DNS_CHILDREN = 2; - -typedef struct { - char hostname[512]; - int port; -} dns_params_t; - -typedef struct { - dns_params_t params; - GaimProxyDnsConnectFunction callback; - gpointer data; -} queued_dns_request_t; - -/* - * Begin the DNS resolver child process functions. - */ -#ifdef HAVE_SIGNAL_H static void -trap_gdb_bug() -{ - const char *message = - "Gaim's DNS child got a SIGTRAP signal.\n" - "This can be caused by trying to run gaim inside gdb.\n" - "There is a known gdb bug which prevents this. Supposedly gaim\n" - "should have detected you were using gdb and used an ugly hack,\n" - "check cope_with_gdb_brokenness() in proxy.c.\n\n" - "For more info about this bug, see http://sources.redhat.com/ml/gdb/2001-07/msg00349.html\n"; - fputs("\n* * *\n",stderr); - fputs(message,stderr); - fputs("* * *\n\n",stderr); - execlp("xmessage","xmessage","-center", message, NULL); - _exit(1); -} -#endif - -static void -cope_with_gdb_brokenness() -{ -#ifdef __linux__ - static gboolean already_done = FALSE; - char s[256], e[512]; - int n; - pid_t ppid; - - if(already_done) - return; - already_done = TRUE; - ppid = getppid(); - snprintf(s, sizeof(s), "/proc/%d/exe", ppid); - n = readlink(s, e, sizeof(e)); - if(n < 0) - return; - - e[MIN(n,sizeof(e)-1)] = '\0'; - - if(strstr(e,"gdb")) { - gaim_debug_info("dns", - "Debugger detected, performing useless query...\n"); - gethostbyname("x.x.x.x.x"); - } -#endif -} - -static void -gaim_dns_resolverthread(int child_out, int child_in, gboolean show_debug) -{ - dns_params_t dns_params; - const size_t zero = 0; - int rc; -#ifdef HAVE_GETADDRINFO - struct addrinfo hints, *res, *tmp; - char servname[20]; -#else - struct sockaddr_in sin; - const size_t addrlen = sizeof(sin); -#endif - -#ifdef HAVE_SIGNAL_H - signal(SIGHUP, SIG_DFL); - signal(SIGINT, SIG_DFL); - signal(SIGQUIT, SIG_DFL); - signal(SIGCHLD, SIG_DFL); - signal(SIGTERM, SIG_DFL); - signal(SIGTRAP, trap_gdb_bug); -#endif - - /* - * We resolve 1 host name for each iteration of this - * while loop. - * - * The top half of this reads in the hostname and port - * number from the socket with our parent. The bottom - * half of this resolves the IP (blocking) and sends - * the result back to our parent, when finished. - */ - while (1) { - const char ch = 'Y'; - fd_set fds; - struct timeval tv = { .tv_sec = 40 , .tv_usec = 0 }; - FD_ZERO(&fds); - FD_SET(child_in, &fds); - rc = select(child_in + 1, &fds, NULL, NULL, &tv); - if (!rc) { - if (show_debug) - printf("dns[%d]: nobody needs me... =(\n", getpid()); - break; - } - rc = read(child_in, &dns_params, sizeof(dns_params_t)); - if (rc < 0) { - perror("read()"); - break; - } - if (rc == 0) { - if (show_debug) - printf("dns[%d]: Oops, father has gone, wait for me, wait...!\n", getpid()); - _exit(0); - } - if (dns_params.hostname[0] == '\0') { - printf("dns[%d]: hostname = \"\" (port = %d)!!!\n", getpid(), dns_params.port); - _exit(1); - } - /* Tell our parent that we read the data successfully */ - write(child_out, &ch, sizeof(ch)); - - /* We have the hostname and port, now resolve the IP */ - -#ifdef HAVE_GETADDRINFO - g_snprintf(servname, sizeof(servname), "%d", dns_params.port); - memset(&hints, 0, sizeof(hints)); - - /* This is only used to convert a service - * name to a port number. As we know we are - * passing a number already, we know this - * value will not be really used by the C - * library. - */ - hints.ai_socktype = SOCK_STREAM; - rc = getaddrinfo(dns_params.hostname, servname, &hints, &res); - write(child_out, &rc, sizeof(rc)); - if (rc != 0) { - close(child_out); - if (show_debug) - printf("dns[%d] Error: getaddrinfo returned %d\n", - getpid(), rc); - dns_params.hostname[0] = '\0'; - continue; - } - tmp = res; - while (res) { - size_t ai_addrlen = res->ai_addrlen; - write(child_out, &ai_addrlen, sizeof(ai_addrlen)); - write(child_out, res->ai_addr, res->ai_addrlen); - res = res->ai_next; - } - freeaddrinfo(tmp); - write(child_out, &zero, sizeof(zero)); -#else - if (!inet_aton(dns_params.hostname, &sin.sin_addr)) { - struct hostent *hp; - if (!(hp = gethostbyname(dns_params.hostname))) { - write(child_out, &h_errno, sizeof(int)); - close(child_out); - if (show_debug) - printf("DNS Error: %d\n", h_errno); - _exit(0); - } - memset(&sin, 0, sizeof(struct sockaddr_in)); - memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); - sin.sin_family = hp->h_addrtype; - } else - sin.sin_family = AF_INET; - - sin.sin_port = htons(dns_params.port); - write(child_out, &addrlen, sizeof(addrlen)); - write(child_out, &sin, addrlen); - write(child_out, &zero, sizeof(zero)); -#endif - dns_params.hostname[0] = '\0'; - } - - close(child_out); - close(child_in); - - _exit(0); -} - -static pending_dns_request_t * -gaim_dns_new_resolverthread(gboolean show_debug) -{ - pending_dns_request_t *req; - int child_out[2], child_in[2]; - - /* Create pipes for communicating with the child process */ - if (pipe(child_out) || pipe(child_in)) { - gaim_debug_error("dns", - "Could not create pipes: %s\n", strerror(errno)); - return NULL; - } - - req = g_new(pending_dns_request_t, 1); - - cope_with_gdb_brokenness(); - - /* Fork! */ - req->dns_pid = fork(); - - /* If we are the child process... */ - if (req->dns_pid == 0) { - /* We should not access the parent's side of the pipes, so close them */ - close(child_out[0]); - close(child_in[1]); - - gaim_dns_resolverthread(child_out[1], child_in[0], show_debug); - /* The thread calls _exit() rather than returning, so we never get here */ - } - - /* We should not access the child's side of the pipes, so close them */ - close(child_out[1]); - close(child_in[0]); - if (req->dns_pid == -1) { - gaim_debug_error("dns", - "Could not create child process for DNS: %s\n", - strerror(errno)); - g_free(req); - return NULL; - } - - req->fd_out = child_out[0]; - req->fd_in = child_in[1]; - number_of_dns_children++; - gaim_debug_info("dns", - "Created new DNS child %d, there are now %d children.\n", - req->dns_pid, number_of_dns_children); - - return req; -} -/* - * End the DNS resolver child process functions. - */ - -/* - * Begin the functions for dealing with the DNS child processes. - */ -static void -req_free(pending_dns_request_t *req) -{ - g_return_if_fail(req != NULL); - - close(req->fd_in); - close(req->fd_out); - - g_free(req->host); - g_free(req); - - number_of_dns_children--; -} - -static int -send_dns_request_to_child(pending_dns_request_t *req, dns_params_t *dns_params) -{ - char ch; - int rc; - pid_t pid; - - /* This waitpid might return the child's PID if it has recently - * exited, or it might return an error if it exited "long - * enough" ago that it has already been reaped; in either - * instance, we can't use it. */ - if ((pid = waitpid (req->dns_pid, NULL, WNOHANG)) > 0) { - gaim_debug_warning("dns", - "DNS child %d no longer exists\n", req->dns_pid); - return -1; - } else if (pid < 0) { - gaim_debug_warning("dns", - "Wait for DNS child %d failed: %s\n", - req->dns_pid, strerror(errno)); - return -1; - } - - /* Let's contact this lost child! */ - rc = write(req->fd_in, dns_params, sizeof(*dns_params)); - if (rc < 0) { - gaim_debug_error("dns", - "Unable to write to DNS child %d: %d\n", - req->dns_pid, strerror(errno)); - close(req->fd_in); - return -1; - } - - g_return_val_if_fail(rc == sizeof(*dns_params), -1); - - /* Did you hear me? (This avoids some race conditions) */ - rc = read(req->fd_out, &ch, sizeof(ch)); - if (rc != 1 || ch != 'Y') - { - gaim_debug_warning("dns", - "DNS child %d not responding. Killing it!\n", - req->dns_pid); - kill(req->dns_pid, SIGKILL); - return -1; - } - - gaim_debug_info("dns", - "Successfully sent DNS request to child %d\n", req->dns_pid); - - return 0; -} - -static void -host_resolved(gpointer data, gint source, GaimInputCondition cond); - -static void -release_dns_child(pending_dns_request_t *req) -{ - g_free(req->host); - req->host = NULL; - - if (queued_requests && !g_queue_is_empty(queued_requests)) { - queued_dns_request_t *r = g_queue_pop_head(queued_requests); - req->host = g_strdup(r->params.hostname); - req->port = r->params.port; - req->callback = r->callback; - req->data = r->data; - - gaim_debug_info("dns", - "Processing queued DNS query for '%s' with child %d\n", - req->host, req->dns_pid); - - if (send_dns_request_to_child(req, &(r->params)) != 0) { - req_free(req); - req = NULL; - - gaim_debug_warning("dns", - "Intent of process queued query of '%s' failed, " - "requeueing...\n", r->params.hostname); - g_queue_push_head(queued_requests, r); - } else { - req->inpa = gaim_input_add(req->fd_out, GAIM_INPUT_READ, host_resolved, req); - g_free(r); - } - - } else { - req->host = NULL; - req->callback = NULL; - req->data = NULL; - free_dns_children = g_slist_append(free_dns_children, req); - } -} - -static void -host_resolved(gpointer data, gint source, GaimInputCondition cond) -{ - pending_dns_request_t *req = (pending_dns_request_t*)data; - int rc, err; - GSList *hosts = NULL; - struct sockaddr *addr = NULL; - size_t addrlen; - - gaim_debug_info("dns", "Got response for '%s'\n", req->host); - gaim_input_remove(req->inpa); - - rc = read(req->fd_out, &err, sizeof(err)); - if ((rc == 4) && (err != 0)) - { - char message[1024]; -#ifdef HAVE_GETADDRINFO - g_snprintf(message, sizeof(message), "DNS error: %s (pid=%d)", - gai_strerror(err), req->dns_pid); -#else - g_snprintf(message, sizeof(message), "DNS error: %d (pid=%d)", - err, req->dns_pid); -#endif - gaim_debug_error("dns", "%s\n", message); - req->callback(NULL, req->data, message); - release_dns_child(req); - return; - } - if (rc > 0) - { - while (rc > 0) { - rc = read(req->fd_out, &addrlen, sizeof(addrlen)); - if (rc > 0 && addrlen > 0) { - addr = g_malloc(addrlen); - rc = read(req->fd_out, addr, addrlen); - hosts = g_slist_append(hosts, GINT_TO_POINTER(addrlen)); - hosts = g_slist_append(hosts, addr); - } else { - break; - } - } - } else if (rc == -1) { - char message[1024]; - g_snprintf(message, sizeof(message), "Error reading from DNS child: %s",strerror(errno)); - gaim_debug_error("dns", "%s\n", message); - req->callback(NULL, req->data, message); - req_free(req); - return; - } else if (rc == 0) { - char message[1024]; - g_snprintf(message, sizeof(message), "EOF reading from DNS child"); - close(req->fd_out); - gaim_debug_error("dns", "%s\n", message); - req->callback(NULL, req->data, message); - req_free(req); - return; - } - -/* wait4(req->dns_pid, NULL, WNOHANG, NULL); */ - - req->callback(hosts, req->data, NULL); - - release_dns_child(req); -} -/* - * End the functions for dealing with the DNS child processes. - */ - -int -gaim_gethostbyname_async(const char *hostname, int port, GaimProxyDnsConnectFunction callback, gpointer data) -{ - pending_dns_request_t *req = NULL; - dns_params_t dns_params; - gchar *host_temp; - gboolean show_debug; - - show_debug = gaim_debug_is_enabled(); - - host_temp = g_strstrip(g_strdup(hostname)); - strncpy(dns_params.hostname, host_temp, sizeof(dns_params.hostname) - 1); - g_free(host_temp); - dns_params.hostname[sizeof(dns_params.hostname) - 1] = '\0'; - dns_params.port = port; - - /* - * If we have any children, attempt to have them perform the DNS - * query. If we're able to send the query to a child, then req - * will be set to the pending_dns_request_t. Otherwise, req will - * be NULL and we'll need to create a new DNS request child. - */ - while (free_dns_children != NULL) { - req = free_dns_children->data; - free_dns_children = g_slist_remove(free_dns_children, req); - - if (send_dns_request_to_child(req, &dns_params) == 0) - /* We found an acceptable child, yay */ - break; - - req_free(req); - req = NULL; - } - - /* We need to create a new DNS request child */ - if (req == NULL) { - if (number_of_dns_children >= MAX_DNS_CHILDREN) { - queued_dns_request_t *r = g_new(queued_dns_request_t, 1); - memcpy(&(r->params), &dns_params, sizeof(dns_params)); - r->callback = callback; - r->data = data; - if (!queued_requests) - queued_requests = g_queue_new(); - g_queue_push_tail(queued_requests, r); - - gaim_debug_info("dns", - "DNS query for '%s' queued\n", dns_params.hostname); - - return 0; - } - - req = gaim_dns_new_resolverthread(show_debug); - if (req == NULL) - { - gaim_debug_error("proxy", "oh dear, this is going to explode, I give up\n"); - return -1; - } - send_dns_request_to_child(req, &dns_params); - } - - req->host = g_strdup(hostname); - req->port = port; - req->callback = callback; - req->data = data; - req->inpa = gaim_input_add(req->fd_out, GAIM_INPUT_READ, host_resolved, req); - - return 0; -} - -#elif defined _WIN32 /* end __unix__ || __APPLE__ */ - -typedef struct _dns_tdata { - char *hostname; - int port; - GaimProxyDnsConnectFunction callback; - gpointer data; - GSList *hosts; - char *errmsg; -} dns_tdata; - -static gboolean dns_main_thread_cb(gpointer data) { - dns_tdata *td = (dns_tdata*)data; - if (td->errmsg != NULL) { - gaim_debug_info("dns", "%s\n", td->errmsg); - } - td->callback(td->hosts, td->data, td->errmsg); - g_free(td->hostname); - g_free(td->errmsg); - g_free(td); - return FALSE; -} - -static gpointer dns_thread(gpointer data) { - -#ifdef HAVE_GETADDRINFO - int rc; - struct addrinfo hints, *res, *tmp; - char servname[20]; -#else - struct sockaddr_in sin; - struct hostent *hp; -#endif - dns_tdata *td = (dns_tdata*)data; - -#ifdef HAVE_GETADDRINFO - g_snprintf(servname, sizeof(servname), "%d", td->port); - memset(&hints,0,sizeof(hints)); - - /* This is only used to convert a service - * name to a port number. As we know we are - * passing a number already, we know this - * value will not be really used by the C - * library. - */ - hints.ai_socktype = SOCK_STREAM; - if ((rc = getaddrinfo(td->hostname, servname, &hints, &res)) == 0) { - tmp = res; - while(res) { - td->hosts = g_slist_append(td->hosts, - GSIZE_TO_POINTER(res->ai_addrlen)); - td->hosts = g_slist_append(td->hosts, - g_memdup(res->ai_addr, res->ai_addrlen)); - res = res->ai_next; - } - freeaddrinfo(tmp); - } else { - td->errmsg = g_strdup_printf("DNS getaddrinfo(\"%s\", \"%s\") error: %d", td->hostname, servname, rc); - } -#else - if ((hp = gethostbyname(td->hostname))) { - memset(&sin, 0, sizeof(struct sockaddr_in)); - memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); - sin.sin_family = hp->h_addrtype; - sin.sin_port = htons(td->port); - - td->hosts = g_slist_append(td->hosts, - GSIZE_TO_POINTER(sizeof(sin))); - td->hosts = g_slist_append(td->hosts, - g_memdup(&sin, sizeof(sin))); - } else { - td->errmsg = g_strdup_printf("DNS gethostbyname(\"%s\") error: %d", td->hostname, h_errno); - } -#endif - /* back to main thread */ - g_idle_add(dns_main_thread_cb, td); - return 0; -} - -int -gaim_gethostbyname_async(const char *hostname, int port, - GaimProxyDnsConnectFunction callback, gpointer data) -{ - dns_tdata *td; - struct sockaddr_in sin; - GError* err = NULL; - - if(inet_aton(hostname, &sin.sin_addr)) { - GSList *hosts = NULL; - sin.sin_family = AF_INET; - sin.sin_port = htons(port); - hosts = g_slist_append(hosts, GINT_TO_POINTER(sizeof(sin))); - hosts = g_slist_append(hosts, g_memdup(&sin, sizeof(sin))); - callback(hosts, data, NULL); - return 0; - } - - gaim_debug_info("dns", "DNS Lookup for: %s\n", hostname); - td = g_new0(dns_tdata, 1); - td->hostname = g_strdup(hostname); - td->port = port; - td->callback = callback; - td->data = data; - - if(!g_thread_create(dns_thread, td, FALSE, &err)) { - gaim_debug_error("dns", "DNS thread create failure: %s\n", err?err->message:""); - g_error_free(err); - g_free(td->hostname); - g_free(td); - return -1; - } - return 0; -} - -#else /* not __unix__ or __APPLE__ or _WIN32 */ - -typedef struct { - gpointer data; - size_t addrlen; - struct sockaddr *addr; - GaimProxyDnsConnectFunction callback; -} pending_dns_request_t; - -static gboolean host_resolved(gpointer data) -{ - pending_dns_request_t *req = (pending_dns_request_t*)data; - GSList *hosts = NULL; - hosts = g_slist_append(hosts, GINT_TO_POINTER(req->addrlen)); - hosts = g_slist_append(hosts, req->addr); - req->callback(hosts, req->data, NULL); - g_free(req); - return FALSE; -} - -int -gaim_gethostbyname_async(const char *hostname, int port, - GaimProxyDnsConnectFunction callback, gpointer data) -{ - struct sockaddr_in sin; - pending_dns_request_t *req; - - if (!inet_aton(hostname, &sin.sin_addr)) { - struct hostent *hp; - if(!(hp = gethostbyname(hostname))) { - gaim_debug_error("dns", - "gaim_gethostbyname(\"%s\", %d) failed: %d\n", - hostname, port, h_errno); - return -1; - } - memset(&sin, 0, sizeof(struct sockaddr_in)); - memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length); - sin.sin_family = hp->h_addrtype; - } else - sin.sin_family = AF_INET; - sin.sin_port = htons(port); - - req = g_new(pending_dns_request_t, 1); - req->addr = (struct sockaddr*) g_memdup(&sin, sizeof(sin)); - req->addrlen = sizeof(sin); - req->data = data; - req->callback = callback; - gaim_timeout_add(10, host_resolved, req); - return 0; -} - -#endif /* not __unix__ or __APPLE__ or _WIN32 */ - -static void no_one_calls(gpointer data, gint source, GaimInputCondition cond) { GaimProxyConnectInfo *connect_info = data; @@ -2368,8 +1696,9 @@ return NULL; } - if (gaim_gethostbyname_async(connecthost, - connectport, connection_host_resolved, connect_info) != 0) + connect_info->query_data = gaim_dnsquery_a(connecthost, + connectport, connection_host_resolved, connect_info); + if (connect_info->query_data == NULL) { gaim_proxy_connect_info_destroy(connect_info); return NULL; @@ -2401,8 +1730,11 @@ connect_info->port = port; connect_info->gpi = gpi; - if (gaim_gethostbyname_async(gaim_proxy_info_get_host(gpi), - gaim_proxy_info_get_port(gpi), connection_host_resolved, connect_info) != 0) + connect_info->query_data = + gaim_dnsquery_a(gaim_proxy_info_get_host(gpi), + gaim_proxy_info_get_port(gpi), + connection_host_resolved, connect_info); + if (connect_info->query_data == NULL) { gaim_proxy_connect_info_destroy(connect_info); return NULL; Modified: branches/v2_0_0/src/proxy.h =================================================================== --- branches/v2_0_0/src/proxy.h 2006-08-18 08:55:08 UTC (rev 16836) +++ branches/v2_0_0/src/proxy.h 2006-08-18 09:03:13 UTC (rev 16837) @@ -60,13 +60,7 @@ typedef void (*GaimProxyConnectFunction)(gpointer data, gint source, const gchar *error_message); -/** - * The "hosts" parameter is a linked list containing pairs of - * one size_t addrlen and one struct sockaddr *addr. - */ -typedef void (*GaimProxyDnsConnectFunction)(GSList *hosts, gpointer data, const char *error_message); - #include "account.h" #ifdef __cplusplus @@ -278,18 +272,6 @@ */ void gaim_proxy_connect_cancel(GaimProxyConnectInfo *connect_info); -/** - * Do an async dns query - * - * @param hostname The hostname to resolve - * @param port A portnumber which is stored in the struct sockaddr - * @param callback Callback to call after resolving - * @param data Extra data for the callback function - * - * @return Zero indicates the connection is pending. Any other value indicates failure. - */ -int gaim_gethostbyname_async(const char *hostname, int port, GaimProxyDnsConnectFunction callback, gpointer data); - /*@}*/ #ifdef __cplusplus Modified: branches/v2_0_0/src/stun.c =================================================================== --- branches/v2_0_0/src/stun.c 2006-08-18 08:55:08 UTC (rev 16836) +++ branches/v2_0_0/src/stun.c 2006-08-18 09:03:13 UTC (rev 16837) @@ -40,6 +40,7 @@ #include "debug.h" #include "account.h" +#include "dnsquery.h" #include "dnssrv.h" #include "network.h" #include "proxy.h" @@ -359,7 +360,7 @@ gaim_debug_info("stun", "got %d SRV responses, server: %s, port: %d\n", results, servername, port); - gaim_gethostbyname_async(servername, port, hbn_cb, NULL); + gaim_dnsquery_a(servername, port, hbn_cb, NULL); g_free(resp); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |