comparison libpurple/dnsquery.c @ 27590:a08e84032814

merge of '2348ff22f0ff3453774b8b25b36238465580c609' and 'e76f11543c2a4aa05bdf584f087cbe3439029661'
author Paul Aurich <paul@darkrain42.org>
date Sun, 12 Jul 2009 05:43:38 +0000
parents 5345dfe48272
children 7fbf964c6c6c
comparison
equal deleted inserted replaced
27104:048bcf41deef 27590:a08e84032814
75 int fd_in, fd_out; 75 int fd_in, fd_out;
76 pid_t dns_pid; 76 pid_t dns_pid;
77 }; 77 };
78 78
79 static GSList *free_dns_children = NULL; 79 static GSList *free_dns_children = NULL;
80 /* TODO: Make me a GQueue when we require >= glib 2.4 */
80 static GSList *queued_requests = NULL; 81 static GSList *queued_requests = NULL;
81 82
82 static int number_of_dns_children = 0; 83 static int number_of_dns_children = 0;
83 84
84 /* 85 /*
115 #ifdef PURPLE_DNSQUERY_USE_FORK 116 #ifdef PURPLE_DNSQUERY_USE_FORK
116 /* 117 /*
117 * Add the resolver to the list of available resolvers, and set it 118 * Add the resolver to the list of available resolvers, and set it
118 * to NULL so that it doesn't get destroyed along with the query_data 119 * to NULL so that it doesn't get destroyed along with the query_data
119 */ 120 */
120 free_dns_children = g_slist_prepend(free_dns_children, query_data->resolver); 121 if (query_data->resolver)
121 query_data->resolver = NULL; 122 {
123 free_dns_children = g_slist_prepend(free_dns_children, query_data->resolver);
124 query_data->resolver = NULL;
125 }
122 #endif /* PURPLE_DNSQUERY_USE_FORK */ 126 #endif /* PURPLE_DNSQUERY_USE_FORK */
123 127
124 purple_dnsquery_destroy(query_data); 128 purple_dnsquery_destroy(query_data);
125 } 129 }
126 130
127 static void 131 static void
128 purple_dnsquery_failed(PurpleDnsQueryData *query_data, const gchar *error_message) 132 purple_dnsquery_failed(PurpleDnsQueryData *query_data, const gchar *error_message)
129 { 133 {
130 purple_debug_info("dnsquery", "%s\n", error_message); 134 purple_debug_error("dnsquery", "%s\n", error_message);
131 if (query_data->callback != NULL) 135 if (query_data->callback != NULL)
132 query_data->callback(NULL, query_data->data, error_message); 136 query_data->callback(NULL, query_data->data, error_message);
133 purple_dnsquery_destroy(query_data); 137 purple_dnsquery_destroy(query_data);
134 } 138 }
135 139
138 { 142 {
139 PurpleDnsQueryUiOps *ops = purple_dnsquery_get_ui_ops(); 143 PurpleDnsQueryUiOps *ops = purple_dnsquery_get_ui_ops();
140 144
141 if (ops && ops->resolve_host) 145 if (ops && ops->resolve_host)
142 return ops->resolve_host(query_data, purple_dnsquery_resolved, purple_dnsquery_failed); 146 return ops->resolve_host(query_data, purple_dnsquery_resolved, purple_dnsquery_failed);
147
148 return FALSE;
149 }
150
151 static gboolean
152 resolve_ip(PurpleDnsQueryData *query_data)
153 {
154 struct sockaddr_in sin;
155 if (inet_aton(query_data->hostname, &sin.sin_addr))
156 {
157 /*
158 * The given "hostname" is actually an IP address, so we
159 * don't need to do anything.
160 */
161 GSList *hosts = NULL;
162 sin.sin_family = AF_INET;
163 sin.sin_port = htons(query_data->port);
164 hosts = g_slist_append(hosts, GINT_TO_POINTER(sizeof(sin)));
165 hosts = g_slist_append(hosts, g_memdup(&sin, sizeof(sin)));
166 purple_dnsquery_resolved(query_data, hosts);
167
168 return TRUE;
169 }
143 170
144 return FALSE; 171 return FALSE;
145 } 172 }
146 173
147 #if defined(PURPLE_DNSQUERY_USE_FORK) 174 #if defined(PURPLE_DNSQUERY_USE_FORK)
613 PurpleDnsQueryData *query_data; 640 PurpleDnsQueryData *query_data;
614 641
615 query_data = data; 642 query_data = data;
616 query_data->timeout = 0; 643 query_data->timeout = 0;
617 644
645 if (resolve_ip(query_data))
646 {
647 /* resolve_ip calls purple_dnsquery_resolved */
648 return FALSE;
649 }
650
618 if (purple_dnsquery_ui_resolve(query_data)) 651 if (purple_dnsquery_ui_resolve(query_data))
619 { 652 {
620 /* The UI is handling the resolve; we're done */ 653 /* The UI is handling the resolve; we're done */
621 return FALSE; 654 return FALSE;
622 } 655 }
656
657 queued_requests = g_slist_append(queued_requests, query_data);
623 658
624 handle_next_queued_request(); 659 handle_next_queued_request();
625 660
626 return FALSE; 661 return FALSE;
627 } 662 }
647 if (*query_data->hostname == '\0') 682 if (*query_data->hostname == '\0')
648 { 683 {
649 purple_dnsquery_destroy(query_data); 684 purple_dnsquery_destroy(query_data);
650 g_return_val_if_reached(NULL); 685 g_return_val_if_reached(NULL);
651 } 686 }
652
653 queued_requests = g_slist_append(queued_requests, query_data);
654 687
655 purple_debug_info("dns", "DNS query for '%s' queued\n", query_data->hostname); 688 purple_debug_info("dns", "DNS query for '%s' queued\n", query_data->hostname);
656 689
657 query_data->timeout = purple_timeout_add(0, resolve_host, query_data); 690 query_data->timeout = purple_timeout_add(0, resolve_host, query_data);
658 691
755 788
756 static gboolean 789 static gboolean
757 resolve_host(gpointer data) 790 resolve_host(gpointer data)
758 { 791 {
759 PurpleDnsQueryData *query_data; 792 PurpleDnsQueryData *query_data;
760 struct sockaddr_in sin;
761 GError *err = NULL; 793 GError *err = NULL;
762 794
763 query_data = data; 795 query_data = data;
764 query_data->timeout = 0; 796 query_data->timeout = 0;
765 797
767 { 799 {
768 /* The UI is handling the resolve; we're done */ 800 /* The UI is handling the resolve; we're done */
769 return FALSE; 801 return FALSE;
770 } 802 }
771 803
772 if (inet_aton(query_data->hostname, &sin.sin_addr)) 804 if (!resolve_ip(query_data))
773 {
774 /*
775 * The given "hostname" is actually an IP address, so we
776 * don't need to do anything.
777 */
778 GSList *hosts = NULL;
779 sin.sin_family = AF_INET;
780 sin.sin_port = htons(query_data->port);
781 hosts = g_slist_append(hosts, GINT_TO_POINTER(sizeof(sin)));
782 hosts = g_slist_append(hosts, g_memdup(&sin, sizeof(sin)));
783 purple_dnsquery_resolved(query_data, hosts);
784 }
785 else
786 { 805 {
787 /* 806 /*
788 * Spin off a separate thread to perform the DNS lookup so 807 * Spin off a separate thread to perform the DNS lookup so
789 * that we don't block the UI. 808 * that we don't block the UI.
790 */ 809 */