comparison libpurple/network.c @ 26213:ff4212a5268f

propagate from branch 'im.pidgin.pidgin' (head 431618de0f30a6938f7e14d2d61ee5d7738acd59) to branch 'im.pidgin.pidgin.vv' (head 8df00cb1a28baa69d0a68e0e96af201ec7d87c09)
author Marcus Lundblad <ml@update.uu.se>
date Mon, 02 Mar 2009 18:47:27 +0000
parents 4b8c4870b13a af42303654a5
children 5d1140b0b10a
comparison
equal deleted inserted replaced
25446:52fbda23e398 26213:ff4212a5268f
46 #include "nat-pmp.h" 46 #include "nat-pmp.h"
47 #include "network.h" 47 #include "network.h"
48 #include "prefs.h" 48 #include "prefs.h"
49 #include "stun.h" 49 #include "stun.h"
50 #include "upnp.h" 50 #include "upnp.h"
51 #include "dnsquery.h"
51 52
52 /* 53 /*
53 * Calling sizeof(struct ifreq) isn't always correct on 54 * Calling sizeof(struct ifreq) isn't always correct on
54 * Mac OS X (and maybe others). 55 * Mac OS X (and maybe others).
55 */ 56 */
97 #endif 98 #endif
98 99
99 #if defined(HAVE_NETWORKMANAGER) || defined(_WIN32) 100 #if defined(HAVE_NETWORKMANAGER) || defined(_WIN32)
100 static gboolean force_online; 101 static gboolean force_online;
101 #endif 102 #endif
103
104 /* Cached IP addresses for STUN and TURN servers (set globally in prefs) */
105 static gchar *stun_ip = NULL;
106 static gchar *turn_ip = NULL;
102 107
103 const unsigned char * 108 const unsigned char *
104 purple_network_ip_atoi(const char *ip) 109 purple_network_ip_atoi(const char *ip)
105 { 110 {
106 static unsigned char ret[4]; 111 static unsigned char ret[4];
721 switch(state) 726 switch(state)
722 { 727 {
723 case NM_STATE_CONNECTED: 728 case NM_STATE_CONNECTED:
724 /* Call res_init in case DNS servers have changed */ 729 /* Call res_init in case DNS servers have changed */
725 res_init(); 730 res_init();
731 /* update STUN IP in case we it changed (theoretically we could
732 have gone from IPv4 to IPv6, f.ex. or we were previously
733 offline */
734 purple_network_set_stun_server(
735 purple_prefs_get_string("/purple/network/stun_server"));
736 purple_network_set_turn_server(
737 purple_prefs_get_string("/purple/network/turn_server"));
738
726 if (ui_ops != NULL && ui_ops->network_connected != NULL) 739 if (ui_ops != NULL && ui_ops->network_connected != NULL)
727 ui_ops->network_connected(); 740 ui_ops->network_connected();
728 break; 741 break;
729 case NM_STATE_ASLEEP: 742 case NM_STATE_ASLEEP:
730 case NM_STATE_CONNECTING: 743 case NM_STATE_CONNECTING:
782 } 795 }
783 } 796 }
784 797
785 #endif 798 #endif
786 799
800 static void
801 purple_network_ip_lookup_cb(GSList *hosts, gpointer data,
802 const char *error_message)
803 {
804 const gchar **ip = (const gchar **) data;
805
806 if (error_message) {
807 purple_debug_error("network", "lookup of IP address failed: %s\n",
808 error_message);
809 g_slist_free(hosts);
810 return;
811 }
812
813 if (hosts && g_slist_next(hosts)) {
814 struct sockaddr *addr = g_slist_next(hosts)->data;
815 char dst[INET6_ADDRSTRLEN];
816
817 if (addr->sa_family == AF_INET6) {
818 inet_ntop(addr->sa_family, &((struct sockaddr_in6 *) addr)->sin6_addr,
819 dst, sizeof(dst));
820 } else {
821 inet_ntop(addr->sa_family, &((struct sockaddr_in *) addr)->sin_addr,
822 dst, sizeof(dst));
823 }
824
825 *ip = g_strdup(dst);
826 purple_debug_info("network", "set IP address: %s\n", *ip);
827 }
828
829 g_slist_free(hosts);
830 }
831
832 void
833 purple_network_set_stun_server(const gchar *stun_server)
834 {
835 if (stun_server && stun_server[0] != '\0') {
836 if (purple_network_is_available()) {
837 purple_debug_info("network", "running DNS query for STUN server\n");
838 purple_dnsquery_a(stun_server, 3478, purple_network_ip_lookup_cb,
839 &stun_ip);
840 } else {
841 purple_debug_info("network",
842 "network is unavailable, don't try to update STUN IP");
843 }
844 } else if (stun_ip) {
845 g_free(stun_ip);
846 stun_ip = NULL;
847 }
848 }
849
850 void
851 purple_network_set_turn_server(const gchar *turn_server)
852 {
853 if (turn_server && turn_server[0] != '\0') {
854 if (purple_network_is_available()) {
855 purple_debug_info("network", "running DNS query for TURN server\n");
856 purple_dnsquery_a(turn_server,
857 purple_prefs_get_int("/purple/network/turn_port"),
858 purple_network_ip_lookup_cb, &turn_ip);
859 } else {
860 purple_debug_info("network",
861 "network is unavailable, don't try to update TURN IP");
862 }
863 } else if (turn_ip) {
864 g_free(turn_ip);
865 turn_ip = NULL;
866 }
867 }
868
869
870 const gchar *
871 purple_network_get_stun_ip(void)
872 {
873 return stun_ip;
874 }
875
876 const gchar *
877 purple_network_get_turn_ip(void)
878 {
879 return turn_ip;
880 }
881
787 void * 882 void *
788 purple_network_get_handle(void) 883 purple_network_get_handle(void)
789 { 884 {
790 static int handle; 885 static int handle;
791 886
814 } 909 }
815 } 910 }
816 #endif 911 #endif
817 912
818 purple_prefs_add_none ("/purple/network"); 913 purple_prefs_add_none ("/purple/network");
914 purple_prefs_add_string("/purple/network/stun_server", "");
915 purple_prefs_add_string("/purple/network/turn_server", "");
916 purple_prefs_add_int ("/purple/network/turn_port", 3478);
917 purple_prefs_add_string("/purple/network/turn_username", "");
918 purple_prefs_add_string("/purple/network/turn_password", "");
819 purple_prefs_add_bool ("/purple/network/auto_ip", TRUE); 919 purple_prefs_add_bool ("/purple/network/auto_ip", TRUE);
820 purple_prefs_add_string("/purple/network/public_ip", ""); 920 purple_prefs_add_string("/purple/network/public_ip", "");
821 purple_prefs_add_bool ("/purple/network/map_ports", TRUE); 921 purple_prefs_add_bool ("/purple/network/map_ports", TRUE);
822 purple_prefs_add_bool ("/purple/network/ports_range_use", FALSE); 922 purple_prefs_add_bool ("/purple/network/ports_range_use", FALSE);
823 purple_prefs_add_int ("/purple/network/ports_range_start", 1024); 923 purple_prefs_add_int ("/purple/network/ports_range_start", 1024);
852 purple_signal_register(purple_network_get_handle(), "network-configuration-changed", 952 purple_signal_register(purple_network_get_handle(), "network-configuration-changed",
853 purple_marshal_VOID, NULL, 0); 953 purple_marshal_VOID, NULL, 0);
854 954
855 purple_pmp_init(); 955 purple_pmp_init();
856 purple_upnp_init(); 956 purple_upnp_init();
957
958 purple_network_set_stun_server(
959 purple_prefs_get_string("/purple/network/stun_server"));
960 purple_network_set_turn_server(
961 purple_prefs_get_string("/purple/network/turn_server"));
857 } 962 }
858 963
859 void 964 void
860 purple_network_uninit(void) 965 purple_network_uninit(void)
861 { 966 {
893 g_static_mutex_unlock(&mutex); 998 g_static_mutex_unlock(&mutex);
894 999
895 #endif 1000 #endif
896 purple_signal_unregister(purple_network_get_handle(), 1001 purple_signal_unregister(purple_network_get_handle(),
897 "network-configuration-changed"); 1002 "network-configuration-changed");
898 } 1003
1004 if (stun_ip)
1005 g_free(stun_ip);
1006 }