/* Jungle Monkey * Copyright (C) 1999-2001 The Regents of the University of Michigan * * 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 "ggui.h" #include "jmutil.h" #include "btp/b_conn.h" #include "ggui_config.h" gboolean jm_running = FALSE; gchar* jm_home = NULL; gchar* jm_config_home = NULL; gchar* jm_hostname = NULL; GInetAddr* jm_iface = NULL; GInetAddr* jm_btp_iface = NULL; BPeer* jm_btp_bpeer = NULL; gint jm_btp_last_port = 0; GInetAddr* jm_mtp_iface = NULL; MtpServer* jm_mtp_server = NULL; gint jm_mtp_last_port = 0; GURL* jm_mtp_rvous_url = NULL; gboolean jm_mtp_rvous_locally = TRUE; gboolean jm_mtp_mirror_on_download = FALSE; gboolean jmutil_init (gchar* hostname_name, gboolean use_dns, gchar* interface_name, gint btp_port, gboolean force_btp_port, gint mtp_port, gboolean force_mtp_port, gchar* rendezvous_url_str) { g_return_val_if_fail (!jm_running, TRUE); if (!hostname_name && !ggui_config_get_bool("/Jungle Monkey/Network/Auto detect hostname", TRUE)) { hostname_name = ggui_config_get_string("/Jungle Monkey/Network/Hostname", NULL); } if (ggui_config_get_bool("/Jungle Monkey/Network/Auto detect hostname", TRUE)) { if (!use_dns) use_dns = ggui_config_get_bool("/Jungle Monkey/Network/Use DNS", FALSE); } if (!interface_name && !ggui_config_get_bool("/Jungle Monkey/Network/Auto detect interface", TRUE)) { interface_name = ggui_config_get_string("/Jungle Monkey/Network/Interface", NULL); } if (!btp_port && !ggui_config_get_bool("/Jungle Monkey/Channels/Use Default BTP Port", TRUE)) { btp_port = ggui_config_get_int("/Jungle Monkey/Channels/BTP Port", 0); } if (!force_btp_port) { force_btp_port = !ggui_config_get_bool("/Jungle Monkey/Channels/Use any BTP Port if unavailable", TRUE); } jm_btp_last_port = ggui_config_get_int ("/Jungle Monkey/General/BTP last port", 0); if (!btp_port && jm_btp_last_port) btp_port = jm_btp_last_port; if (!mtp_port && !ggui_config_get_bool("/Jungle Monkey/Files/Use Default MTP Port", TRUE)) { mtp_port = ggui_config_get_int("/Jungle Monkey/Files/MTP Port", 0); } if (!force_mtp_port) { force_mtp_port = !ggui_config_get_bool("/Jungle Monkey/Files/Use any MTP Port if unavailable", TRUE); } jm_mtp_last_port = ggui_config_get_int ("/Jungle Monkey/General/MTP last port", 0); if (!mtp_port && jm_mtp_last_port) mtp_port = jm_mtp_last_port; jm_mtp_rvous_locally = ggui_config_get_bool("/Jungle Monkey/Files/Rendezvous locally", TRUE); if (!rendezvous_url_str && !jm_mtp_rvous_locally) { rendezvous_url_str = ggui_config_get_string("/Jungle Monkey/Files/Rendezvous URL", NULL); } jm_mtp_mirror_on_download = ggui_config_get_bool ("/Jungle Monkey/Files/Automirror", TRUE); /* If an interface was specified, make sure it resoves to an address */ if (interface_name) { /* Convert interface_name to InetAddr. */ jm_iface = gnet_inetaddr_new (interface_name, 0); if (!jm_iface) { ggui_show_message("error", _("Bad interface address: %s. \n" "The address must be of an interface on your \n" "machine, preferably in dotted decimal notation.\n" "Use /sbin/ifconfig to list the interfaces on \n" "your machine. You can set the interface \n" "manually under Settings/Preferences/Network. \n" "JM will not work until this is fixed."), interface_name); goto error; } } /* Otherwise, auto-detect the interface */ else { jm_iface = gnet_inetaddr_autodetect_internet_interface (); if (!jm_iface) { ggui_show_message("error", _("Could not auto-detect an internet interface. \n" "You can set the interface manually under \n" "Settings/Preferences/Network or by using \n" "the --interface command line option. Use \n" "/sbin/ifconfig to list the interfaces on your \n" "machine. JM will not work until this is \n" "fixed.")); goto error; } } /* If a hostname was specified, use it */ if (hostname_name) { jm_hostname = g_strdup (hostname_name); } /* Autodetect the hostname */ else { if (use_dns) jm_hostname = gnet_inetaddr_get_name (jm_iface); if (!jm_hostname || !gnet_inetaddr_is_internet_domainname (jm_hostname)) jm_hostname = gnet_inetaddr_get_canonical_name (jm_iface); g_assert (jm_hostname); if (!gnet_inetaddr_is_internet_domainname (jm_hostname)) { ggui_show_message("error", _("Could not auto-detect the hostname. \n" "We detected %s, but it does not \n" "look like a valid internet hostname. You \n" "can set the address manually under \n" "Settings/Preferences/Network or by using the \n" "--hostname command line option. JM will not \n" "work until this is fixed."), jm_hostname); goto error; } } if (rendezvous_url_str) { jm_mtp_rvous_url = gnet_url_new (rendezvous_url_str); jm_mtp_rvous_locally = FALSE; if (!jm_mtp_rvous_url) { ggui_show_message ("error-", _("Bad rendezvous URL: %s\n"), rendezvous_url_str); goto error; } gnet_url_set_protocol (jm_mtp_rvous_url, "mtp"); } /* Start BTP */ jm_btp_iface = gnet_inetaddr_clone (jm_iface); gnet_inetaddr_set_port (jm_btp_iface, btp_port); jm_btp_bpeer = b_peer_new (jm_hostname, jm_btp_iface, force_btp_port); if (!jm_btp_bpeer) { ggui_show_message ("error", _("Error starting BTP peer\n")); goto error; } gnet_inetaddr_set_port (jm_btp_iface, jm_btp_bpeer->port); /* Start MTP */ jm_mtp_iface = gnet_inetaddr_clone (jm_iface); gnet_inetaddr_set_port (jm_mtp_iface, mtp_port); jm_mtp_server = mtp_server_new (jm_hostname, jm_mtp_iface, force_mtp_port, MTP_SERVER_TYPE_BOTH, FALSE); if (!jm_mtp_server) { ggui_show_message ("error", _("Error staring MTP server\n")); goto error; } gnet_inetaddr_set_port (jm_mtp_iface, jm_mtp_server->server->port); /* Set MTP URL if there isn't one */ if (!jm_mtp_rvous_url) { jm_mtp_rvous_locally = TRUE; jm_mtp_rvous_url = gnet_url_new_fields ("mtp", jm_hostname, jm_mtp_server->server->port, NULL); } /* Warn if BTP or MTP port changed */ if (jm_btp_last_port && jm_btp_bpeer->port != jm_btp_last_port) ggui_show_message ("warning", _("The BTP port changed. The URLs for\n" "your channels, chats, and searches\n" "have changed.\n")); if (jm_mtp_last_port && jm_mtp_server->server->port != jm_mtp_last_port) ggui_show_message ("warning", _("The MTP port changed. The URLs for\n" "your files have changed.\n")); jm_running = TRUE; return FALSE; error: jmutil_shutdown (); return TRUE; } gboolean jmutil_shutdown (void) { jm_running = FALSE; g_free (jm_hostname); jm_hostname = NULL; gnet_inetaddr_delete (jm_iface); jm_iface = NULL; gnet_inetaddr_delete (jm_btp_iface); jm_btp_iface = NULL; b_peer_delete (jm_btp_bpeer); jm_btp_bpeer = NULL; gnet_inetaddr_delete (jm_mtp_iface); jm_mtp_iface = NULL; mtp_server_delete (jm_mtp_server); jm_mtp_server = NULL; gnet_url_delete (jm_mtp_rvous_url); jm_mtp_rvous_url = NULL; jm_mtp_rvous_locally = TRUE; jm_mtp_mirror_on_download = FALSE; return (b_conn_num_conns == 0); }