comparison src/proxy.c @ 10237:2f6dcbaaabe0

[gaim-migrate @ 11373] Asynchronous host name resolution for win32. committer: Tailor Script <tailor@pidgin.im>
author Herman Bloggs <hermanator12002@yahoo.com>
date Mon, 22 Nov 2004 18:52:44 +0000
parents 374d63326461
children d0558652e0c4
comparison
equal deleted inserted replaced
10236:1a62febbf3d6 10237:2f6dcbaaabe0
629 req->data = data; 629 req->data = data;
630 req->inpa = gaim_input_add(req->fd_out, GAIM_INPUT_READ, host_resolved, req); 630 req->inpa = gaim_input_add(req->fd_out, GAIM_INPUT_READ, host_resolved, req);
631 631
632 return 0; 632 return 0;
633 } 633 }
634 #else /* __unix__ */ 634
635 elif defined _WIN32 /* end __unix__ */
636
637 /* Note: The below win32 implementation is actually platform independent.
638 Perhaps this can be used in place of the above platform dependent code.
639 */
640
641 typedef struct _dns_tdata {
642 char *hostname;
643 int port;
644 dns_callback_t callback;
645 gpointer data;
646 GSList *hosts;
647 char *errmsg;
648 } dns_tdata;
649
650 static gboolean dns_main_thread_cb(gpointer data) {
651 dns_tdata *td = (dns_tdata*)data;
652 td->callback(td->hosts, td->data, td->errmsg);
653 g_free(td->hostname);
654 g_free(td);
655 return FALSE;
656 }
657
658 static gpointer dns_thread(gpointer data) {
659 struct sockaddr_in sin;
660 dns_tdata *td = (dns_tdata*)data;
661 struct hostent *hp;
662
663 if(!(hp = gethostbyname(td->hostname)))
664 goto failure;
665 memset(&sin, 0, sizeof(struct sockaddr_in));
666 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
667 sin.sin_family = hp->h_addrtype;
668 sin.sin_port = htons(td->port);
669
670 td->hosts = g_slist_append(td->hosts, GINT_TO_POINTER(sizeof(sin)));
671 td->hosts = g_slist_append(td->hosts, g_memdup(&sin, sizeof(sin)));
672 /* back to main thread */
673 g_idle_add(dns_main_thread_cb, td);
674 return 0;
675 failure:
676 g_free(td->hostname);
677 g_free(td);
678 return 0;
679 }
680
681 int gaim_gethostbyname_async(const char *hostname, int port,
682 dns_callback_t callback, gpointer data) {
683 dns_tdata *td;
684 struct sockaddr_in sin;
685 GError* err = NULL;
686
687 if(inet_aton(hostname, &sin.sin_addr)) {
688 GSList *hosts = NULL;
689 sin.sin_family = AF_INET;
690 sin.sin_port = htons(port);
691 hosts = g_slist_append(hosts, GINT_TO_POINTER(sizeof(sin)));
692 hosts = g_slist_append(hosts, g_memdup(&sin, sizeof(sin)));
693 callback(hosts, data, NULL);
694 return 0;
695 }
696
697 gaim_debug_info("dns", "DNS Lookup for: %s\n", hostname);
698 td = g_new0(dns_tdata, 1);
699 td->hostname = g_strdup(hostname);
700 td->port = port;
701 td->callback = callback;
702 td->data = data;
703
704 if(!g_thread_create(dns_thread, td, FALSE, &err)) {
705 gaim_debug_error("dns", "DNS thread create failure: %s\n", err?err->message:"");
706 g_error_free(err);
707 g_free(td->hostname);
708 g_free(td);
709 return -1;
710 }
711 return 0;
712 }
713
714 #else /* not __unix__ or _WIN32 */
635 715
636 typedef struct { 716 typedef struct {
637 gpointer data; 717 gpointer data;
638 size_t addrlen; 718 size_t addrlen;
639 struct sockaddr *addr; 719 struct sockaddr *addr;
680 req->callback = callback; 760 req->callback = callback;
681 gaim_timeout_add(10, host_resolved, req); 761 gaim_timeout_add(10, host_resolved, req);
682 return 0; 762 return 0;
683 } 763 }
684 764
685 #endif /* __unix__ */ 765 #endif /* not __unix__ or _WIN32 */
686 766
687 static void 767 static void
688 no_one_calls(gpointer data, gint source, GaimInputCondition cond) 768 no_one_calls(gpointer data, gint source, GaimInputCondition cond)
689 { 769 {
690 struct PHB *phb = data; 770 struct PHB *phb = data;
1636 proxy_pref_cb, NULL); 1716 proxy_pref_cb, NULL);
1637 gaim_prefs_connect_callback(handle, "/core/proxy/username", 1717 gaim_prefs_connect_callback(handle, "/core/proxy/username",
1638 proxy_pref_cb, NULL); 1718 proxy_pref_cb, NULL);
1639 gaim_prefs_connect_callback(handle, "/core/proxy/password", 1719 gaim_prefs_connect_callback(handle, "/core/proxy/password",
1640 proxy_pref_cb, NULL); 1720 proxy_pref_cb, NULL);
1721 #ifdef _WIN32
1722 if(!g_thread_supported())
1723 g_thread_init(NULL);
1724 #endif
1641 } 1725 }
1642 1726
1643 void * 1727 void *
1644 gaim_proxy_get_handle() 1728 gaim_proxy_get_handle()
1645 { 1729 {