comparison libgaim/upnp.c @ 14354:01daacf7b771

[gaim-migrate @ 17060] Make gaim_url_fetch() cancelable and change Yahoo! to take advantage of the changes. Other stuff can be changed later, the important thing is that the API is there. committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Sun, 27 Aug 2006 21:13:30 +0000
parents 60b1bc8dbf37
children f283ebe419e4
comparison
equal deleted inserted replaced
14353:33dc9f22b528 14354:01daacf7b771
148 148
149 static void gaim_upnp_discover_send_broadcast(UPnPDiscoveryData *dd); 149 static void gaim_upnp_discover_send_broadcast(UPnPDiscoveryData *dd);
150 static void lookup_public_ip(void); 150 static void lookup_public_ip(void);
151 static void lookup_internal_ip(void); 151 static void lookup_internal_ip(void);
152 152
153
154 static void 153 static void
155 fire_discovery_callbacks(gboolean success) 154 fire_discovery_callbacks(gboolean success)
156 { 155 {
157 while(discovery_callbacks) { 156 while(discovery_callbacks) {
158 gpointer data; 157 gpointer data;
162 discovery_callbacks = g_slist_remove(discovery_callbacks, data); 161 discovery_callbacks = g_slist_remove(discovery_callbacks, data);
163 cb(success, data); 162 cb(success, data);
164 } 163 }
165 } 164 }
166 165
167
168 static gboolean 166 static gboolean
169 gaim_upnp_compare_device(const xmlnode* device, const gchar* deviceType) 167 gaim_upnp_compare_device(const xmlnode* device, const gchar* deviceType)
170 { 168 {
171 xmlnode* deviceTypeNode = xmlnode_get_child(device, "deviceType"); 169 xmlnode* deviceTypeNode = xmlnode_get_child(device, "deviceType");
172 char *tmp; 170 char *tmp;
181 g_free(tmp); 179 g_free(tmp);
182 180
183 return ret; 181 return ret;
184 } 182 }
185 183
186
187 static gboolean 184 static gboolean
188 gaim_upnp_compare_service(const xmlnode* service, const gchar* serviceType) 185 gaim_upnp_compare_service(const xmlnode* service, const gchar* serviceType)
189 { 186 {
190 xmlnode* serviceTypeNode; 187 xmlnode* serviceTypeNode;
191 char *tmp; 188 char *tmp;
205 ret = !g_ascii_strcasecmp(tmp, serviceType); 202 ret = !g_ascii_strcasecmp(tmp, serviceType);
206 g_free(tmp); 203 g_free(tmp);
207 204
208 return ret; 205 return ret;
209 } 206 }
210
211 207
212 static gchar* 208 static gchar*
213 gaim_upnp_parse_description_response(const gchar* httpResponse, gsize len, 209 gaim_upnp_parse_description_response(const gchar* httpResponse, gsize len,
214 const gchar* httpURL, const gchar* serviceType) 210 const gchar* httpURL, const gchar* serviceType)
215 { 211 {
356 352
357 return controlURL; 353 return controlURL;
358 } 354 }
359 355
360 static void 356 static void
361 upnp_parse_description_cb(void *data, const char *httpResponse, gsize len) 357 upnp_parse_description_cb(GaimUtilFetchUrlData *url_data, gpointer user_data,
362 { 358 const gchar *httpResponse, gsize len, const gchar *error_message)
363 UPnPDiscoveryData *dd = data; 359 {
360 UPnPDiscoveryData *dd = user_data;
364 gchar *control_url = NULL; 361 gchar *control_url = NULL;
365 362
366 if (len > 0) 363 if (len > 0)
367 control_url = gaim_upnp_parse_description_response( 364 control_url = gaim_upnp_parse_description_response(
368 httpResponse, len, dd->full_url, dd->service_type); 365 httpResponse, len, dd->full_url, dd->service_type);
429 /* Remove the timeout because everything it is waiting for has 426 /* Remove the timeout because everything it is waiting for has
430 * successfully completed */ 427 * successfully completed */
431 gaim_timeout_remove(dd->tima); 428 gaim_timeout_remove(dd->tima);
432 dd->tima = 0; 429 dd->tima = 0;
433 430
434 gaim_url_fetch_request(descriptionURL, TRUE, NULL, TRUE, httpRequest, 431 gaim_util_fetch_url_request(descriptionURL, TRUE, NULL, TRUE, httpRequest,
435 TRUE, upnp_parse_description_cb, dd); 432 TRUE, upnp_parse_description_cb, dd);
436 433
437 g_free(httpRequest); 434 g_free(httpRequest);
438 435
439 } 436 }
546 gaim_upnp_parse_discover_response(buf, len, dd); 543 gaim_upnp_parse_discover_response(buf, len, dd);
547 544
548 /* We'll either time out or continue successfully */ 545 /* We'll either time out or continue successfully */
549 } 546 }
550 547
551 void 548 static void
552 gaim_upnp_discover_send_broadcast(UPnPDiscoveryData *dd) 549 gaim_upnp_discover_send_broadcast(UPnPDiscoveryData *dd)
553 { 550 {
554 gchar *sendMessage = NULL; 551 gchar *sendMessage = NULL;
555 gsize totalSize; 552 gsize totalSize;
556 gboolean sentSuccess; 553 gboolean sentSuccess;
596 /* We have already done all our retries. Make sure that the callback 593 /* We have already done all our retries. Make sure that the callback
597 * doesn't get called before the original function returns */ 594 * doesn't get called before the original function returns */
598 gaim_timeout_add(10, gaim_upnp_discover_timeout, dd); 595 gaim_timeout_add(10, gaim_upnp_discover_timeout, dd);
599 } 596 }
600 597
601
602 void 598 void
603 gaim_upnp_discover(GaimUPnPCallback cb, gpointer cb_data) 599 gaim_upnp_discover(GaimUPnPCallback cb, gpointer cb_data)
604 { 600 {
605 /* Socket Setup Variables */ 601 /* Socket Setup Variables */
606 int sock; 602 int sock;
659 gaim_upnp_discover_send_broadcast(dd); 655 gaim_upnp_discover_send_broadcast(dd);
660 } 656 }
661 657
662 static void 658 static void
663 gaim_upnp_generate_action_message_and_send(const gchar* actionName, 659 gaim_upnp_generate_action_message_and_send(const gchar* actionName,
664 const gchar* actionParams, GaimURLFetchCallback cb, 660 const gchar* actionParams, GaimUtilFetchUrlCallback cb,
665 gpointer cb_data) 661 gpointer cb_data)
666 { 662 {
667 663
668 gchar* soapMessage; 664 gchar* soapMessage;
669 gchar* totalSendMessage; 665 gchar* totalSendMessage;
676 &port, &pathOfControl, NULL, NULL)) { 672 &port, &pathOfControl, NULL, NULL)) {
677 gaim_debug_error("upnp", 673 gaim_debug_error("upnp",
678 "generate_action_message_and_send(): Failed In Parse URL\n\n"); 674 "generate_action_message_and_send(): Failed In Parse URL\n\n");
679 /* XXX: This should probably be async */ 675 /* XXX: This should probably be async */
680 if(cb) 676 if(cb)
681 cb(cb_data, NULL, 0); 677 cb(NULL, cb_data, NULL, 0, NULL);
682 } 678 }
683 if(port == 0 || port == -1) { 679 if(port == 0 || port == -1) {
684 port = DEFAULT_HTTP_PORT; 680 port = DEFAULT_HTTP_PORT;
685 } 681 }
686 682
694 control_info.service_type, actionName, 690 control_info.service_type, actionName,
695 strlen(soapMessage), soapMessage); 691 strlen(soapMessage), soapMessage);
696 g_free(pathOfControl); 692 g_free(pathOfControl);
697 g_free(soapMessage); 693 g_free(soapMessage);
698 694
699 gaim_url_fetch_request(control_info.control_url, FALSE, NULL, TRUE, 695 gaim_util_fetch_url_request(control_info.control_url, FALSE, NULL, TRUE,
700 totalSendMessage, TRUE, cb, cb_data); 696 totalSendMessage, TRUE, cb, cb_data);
701 697
702 g_free(totalSendMessage); 698 g_free(totalSendMessage);
703 g_free(addressOfControl); 699 g_free(addressOfControl);
704 } 700 }
705
706 701
707 const gchar * 702 const gchar *
708 gaim_upnp_get_public_ip() 703 gaim_upnp_get_public_ip()
709 { 704 {
710 if (control_info.status == GAIM_UPNP_STATUS_DISCOVERED 705 if (control_info.status == GAIM_UPNP_STATUS_DISCOVERED
720 715
721 return NULL; 716 return NULL;
722 } 717 }
723 718
724 static void 719 static void
725 looked_up_public_ip_cb(gpointer data, const char *httpResponse, gsize len) 720 looked_up_public_ip_cb(GaimUtilFetchUrlData *url_data, gpointer user_data,
721 const gchar *httpResponse, gsize len, const gchar *error_message)
726 { 722 {
727 gchar* temp, *temp2; 723 gchar* temp, *temp2;
728 724
729 if(!httpResponse) 725 if ((error_message != NULL) || (httpResponse == NULL))
730 return; 726 return;
731 727
732 /* extract the ip, or see if there is an error */ 728 /* extract the ip, or see if there is an error */
733 if((temp = g_strstr_len(httpResponse, len, 729 if((temp = g_strstr_len(httpResponse, len,
734 "<NewExternalIPAddress")) == NULL) { 730 "<NewExternalIPAddress")) == NULL) {
752 sizeof(control_info.publicip)); 748 sizeof(control_info.publicip));
753 749
754 gaim_debug_info("upnp", "NAT Returned IP: %s\n", control_info.publicip); 750 gaim_debug_info("upnp", "NAT Returned IP: %s\n", control_info.publicip);
755 } 751 }
756 752
757 void 753 static void
758 lookup_public_ip() 754 lookup_public_ip()
759 { 755 {
760 gaim_upnp_generate_action_message_and_send("GetExternalIPAddress", "", 756 gaim_upnp_generate_action_message_and_send("GetExternalIPAddress", "",
761 looked_up_public_ip_cb, NULL); 757 looked_up_public_ip_cb, NULL);
762 } 758 }
792 } else 788 } else
793 gaim_debug_info("upnp", "Unable to look up local IP\n"); 789 gaim_debug_info("upnp", "Unable to look up local IP\n");
794 790
795 } 791 }
796 792
797 void 793 static void
798 lookup_internal_ip() 794 lookup_internal_ip()
799 { 795 {
800 gchar* addressOfControl; 796 gchar* addressOfControl;
801 int port = 0; 797 int port = 0;
802 798
819 815
820 g_free(addressOfControl); 816 g_free(addressOfControl);
821 } 817 }
822 818
823 static void 819 static void
824 done_port_mapping_cb(gpointer data, const gchar *httpResponse, gsize len) 820 done_port_mapping_cb(GaimUtilFetchUrlData *url_data, gpointer user_data,
825 { 821 const gchar *httpResponse, gsize len, const gchar *error_message)
826 UPnPMappingAddRemove *ar = data; 822 {
823 UPnPMappingAddRemove *ar = user_data;
827 824
828 gboolean success = TRUE; 825 gboolean success = TRUE;
829 826
830 /* determine if port mapping was a success */ 827 /* determine if port mapping was a success */
831 if(!httpResponse || g_strstr_len(httpResponse, len, HTTP_OK) == NULL) { 828 if ((error_message != NULL) || (httpResponse == NULL) ||
829 (g_strstr_len(httpResponse, len, HTTP_OK) == NULL))
830 {
832 gaim_debug_error("upnp", 831 gaim_debug_error("upnp",
833 "gaim_upnp_set_port_mapping(): Failed HTTP_OK\n\n%s\n\n", 832 "gaim_upnp_set_port_mapping(): Failed HTTP_OK\n\n%s\n\n",
834 httpResponse ? httpResponse : "(null)"); 833 httpResponse ? httpResponse : "(null)");
835 success = FALSE; 834 success = FALSE;
836 } else 835 } else