Skip to content
This repository has been archived by the owner on Feb 9, 2018. It is now read-only.

Commit

Permalink
Fix for "Can't assign requested address" bug after update to Java
Browse files Browse the repository at this point in the history
1.6.0_45 on Mac OSX
  • Loading branch information
Raptor399 committed Apr 20, 2013
1 parent 80228d5 commit 98c85bc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.txt
Expand Up @@ -2,7 +2,6 @@ Changelog:
==========

1.81.0 - 2013-??-??

Engines:
FFmpeg:
- Add/fix audio channel options
Expand All @@ -14,6 +13,7 @@ Changelog:

Misc:
- Media Parser v1: fix audio channel parsing
- Fix for "Can't assign requested address" bug after update to Java 1.6.0_45 on Mac OSX

1.80.0 - 2013-04-06

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/net/pms/network/NetworkConfiguration.java
Expand Up @@ -148,7 +148,9 @@ public String toString() {
* {@link #getInstance()} to retrieve an instance.
*/
private NetworkConfiguration(Enumeration<NetworkInterface> networkInterfaces) {
checkNetworkInterface(networkInterfaces, null);
System.setProperty("java.net.preferIPv4Stack", "true");

checkNetworkInterface(networkInterfaces, null);
}

/**
Expand Down
57 changes: 27 additions & 30 deletions src/main/java/net/pms/network/UPNPHelper.java
Expand Up @@ -23,15 +23,17 @@
import java.net.BindException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet6Address;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.TimeZone;

import net.pms.PMS;
Expand Down Expand Up @@ -199,29 +201,33 @@ public static void sendAlive() {
* @throws IOException Signals that an I/O exception has occurred.
*/
private static MulticastSocket getNewMulticastSocket() throws IOException {
MulticastSocket ssdpSocket = new MulticastSocket();
ssdpSocket.setReuseAddress(true);
NetworkInterface ni = NetworkConfiguration.getInstance().getNetworkInterfaceByServerName();

if (ni != null) {
ssdpSocket.setNetworkInterface(ni);
NetworkInterface networkInterface = NetworkConfiguration.getInstance().getNetworkInterfaceByServerName();

// force IPv4 address
Enumeration<InetAddress> enm = ni.getInetAddresses();
if (networkInterface == null) {
networkInterface = PMS.get().getServer().getNetworkInterface();
}

while (enm.hasMoreElements()) {
InetAddress ia = enm.nextElement();
if (networkInterface == null) {
throw new IOException("No usable network interface found for UPnP multicast");
}

if (!(ia instanceof Inet6Address)) {
ssdpSocket.setInterface(ia);
break;
}
List<InetAddress> usableAddresses = new ArrayList<InetAddress>();
List<InetAddress> networkInterfaceAddresses = Collections.list(networkInterface.getInetAddresses());

for (InetAddress inetAddress : networkInterfaceAddresses) {
if (inetAddress != null && inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
usableAddresses.add(inetAddress);
}
} else if (PMS.get().getServer().getNetworkInterface() != null) {
LOGGER.trace("Setting multicast network interface: " + PMS.get().getServer().getNetworkInterface());
ssdpSocket.setNetworkInterface(PMS.get().getServer().getNetworkInterface());
}

if (usableAddresses.size() == 0) {
throw new IOException("No usable addresses found for UPnP multicast");
}

InetSocketAddress localAddress = new InetSocketAddress(usableAddresses.get(0), 0);
MulticastSocket ssdpSocket = new MulticastSocket(localAddress);
ssdpSocket.setReuseAddress(true);

LOGGER.trace("Sending message from multicast socket on network interface: " + ssdpSocket.getNetworkInterface());
LOGGER.trace("Multicast socket is on interface: " + ssdpSocket.getInterface());
ssdpSocket.setTimeToLive(32);
Expand Down Expand Up @@ -354,15 +360,6 @@ public void run() {
LOGGER.warn("Finally, acquiring port " + PMS.getConfiguration().getUpnpPort() + " was successful!");
}

NetworkInterface ni = NetworkConfiguration.getInstance().getNetworkInterfaceByServerName();

if (ni != null) {
multicastSocket.setNetworkInterface(ni);
} else if (PMS.get().getServer().getNetworkInterface() != null) {
LOGGER.trace("Setting multicast network interface: " + PMS.get().getServer().getNetworkInterface());
multicastSocket.setNetworkInterface(PMS.get().getServer().getNetworkInterface());
}

multicastSocket.setTimeToLive(4);
multicastSocket.setReuseAddress(true);
InetAddress upnpAddress = getUPNPAddress();
Expand Down Expand Up @@ -500,6 +497,6 @@ private static String buildMsg(String nt, String message) {
* @throws IOException Signals that an I/O exception has occurred.
*/
private static InetAddress getUPNPAddress() throws IOException {
return InetAddress.getByAddress(IPV4_UPNP_HOST, new byte[]{(byte) 239, (byte) 255, (byte) 255, (byte) 250});
return InetAddress.getByName(IPV4_UPNP_HOST);
}
}

0 comments on commit 98c85bc

Please sign in to comment.