Revision: a8f5bc7a3cca
Author:   Jean Deruelle <[email protected]>
Date:     Tue Dec  4 15:52:49 2012
Log:      Fixes Issue 177

http://code.google.com/p/sipservlets/source/detail?r=a8f5bc7a3cca

Modified:
 /jain-sip-170.x/src/gov/nist/core/net/DefaultNetworkLayer.java
 /jain-sip-170.x/src/gov/nist/javax/sip/stack/IOHandler.java

=======================================
--- /jain-sip-170.x/src/gov/nist/core/net/DefaultNetworkLayer.java Mon Dec 3 18:06:27 2012 +++ /jain-sip-170.x/src/gov/nist/core/net/DefaultNetworkLayer.java Tue Dec 4 15:52:49 2012
@@ -105,8 +105,19 @@

     /* Added by Daniel J. Martinez Manzano <[email protected]> */
     public SSLSocket createSSLSocket(InetAddress address, int port)
-       throws IOException {
+       throws IOException {
+       return createSSLSocket(address, port, null);
+    }
+
+    /* Added by Daniel J. Martinez Manzano <[email protected]> */
+    public SSLSocket createSSLSocket(InetAddress address, int port,
+            InetAddress myAddress) throws IOException {
        SSLSocket sock = (SSLSocket) sslSocketFactory.createSocket();
+       if (myAddress != null) {
+ // trying to bind to the correct ipaddress (in case of multiple vip addresses by example)
+               // and let the JDK pick an ephemeral port
+               sock.bind(new InetSocketAddress(myAddress, 0));
+       }
        try {
                sock.connect(new InetSocketAddress(address, port), 8000);
        } catch (SocketTimeoutException e) {
@@ -114,17 +125,14 @@
        }
        return sock;
     }
-
-    /* Added by Daniel J. Martinez Manzano <[email protected]> */
-    public SSLSocket createSSLSocket(InetAddress address, int port,
-            InetAddress myAddress) throws IOException {
-       return createSSLSocket(address, port);
-    }

     public Socket createSocket(InetAddress address, int port,
             InetAddress myAddress) throws IOException {
        if (myAddress != null) {
                Socket sock = new Socket();
+ // trying to bind to the correct ipaddress (in case of multiple vip addresses by example)
+               // and let the JDK pick an ephemeral port
+               sock.bind(new InetSocketAddress(myAddress, 0));
                try {
                        sock.connect(new InetSocketAddress(address, port), 
8000);
                } catch (SocketTimeoutException e) {
@@ -163,18 +171,42 @@
                     InetAddress myAddress, int myPort)
         throws IOException
     {
-        if (myAddress != null)
-            return new Socket(address, port, myAddress, myPort);
-        else if (port != 0)
-        {
-            //myAddress is null (i.e. any)  but we have a port number
-            Socket sock = new Socket();
-            sock.bind(new InetSocketAddress(port));
-            sock.connect(new InetSocketAddress(address, port), 8000);
-            return sock;
+       if (myAddress != null) {
+               Socket sock = new Socket();
+ // trying to bind to the correct ipaddress (in case of multiple vip addresses by example)
+               // and let the JDK pick an ephemeral port
+               sock.bind(new InetSocketAddress(myAddress, 0));
+               try {
+                       sock.connect(new InetSocketAddress(address, port), 
8000);
+               } catch (SocketTimeoutException e) {
+ throw new ConnectException("Socket timeout error (8sec)" + address + ":" + port);
+               }
+               return sock;
         }
-        else
-            return new Socket(address, port);
+        else {
+               Socket sock =  new Socket();
+               if(myPort != 0) {
+                       sock.bind(new InetSocketAddress(port));
+               }
+               try {
+                       sock.connect(new InetSocketAddress(address, port), 
8000);
+               } catch (SocketTimeoutException e) {
+ throw new ConnectException("Socket timeout error (8sec)" + address + ":" + port);
+               }
+               return sock;
+        }
+//        if (myAddress != null)
+//            return new Socket(address, port, myAddress, myPort);
+//        else if (port != 0)
+//        {
+//            //myAddress is null (i.e. any)  but we have a port number
+//            Socket sock = new Socket();
+//            sock.bind(new InetSocketAddress(port));
+//            sock.connect(new InetSocketAddress(address, port), 8000);
+//            return sock;
+//        }
+//        else
+//            return new Socket(address, port);
     }

 }
=======================================
--- /jain-sip-170.x/src/gov/nist/javax/sip/stack/IOHandler.java Mon Dec 3 18:06:27 2012 +++ /jain-sip-170.x/src/gov/nist/javax/sip/stack/IOHandler.java Tue Dec 4 15:52:49 2012
@@ -269,7 +269,8 @@
         int length = bytes.length;
         if (logger.isLoggingEnabled(LogWriter.TRACE_DEBUG)) {
             logger.logDebug(
-                    "sendBytes " + transport + " inAddr "
+                    "sendBytes " + transport + " local inAddr "
+ + senderAddress.getHostAddress() + " remote inAddr "
                             + receiverAddress.getHostAddress() + " port = "
+ contactPort + " length = " + length + " isClient " + isClient );

@@ -304,7 +305,7 @@
// the IP address is on a per listening point basis.
                         try {
clientSock = sipStack.getNetworkLayer().createSocket(
-                                               receiverAddress, contactPort, 
senderAddress);
+ receiverAddress, contactPort, senderAddress, messageChannel.getPort()); } catch (SocketException e) { // We must catch the socket timeout exceptions here, any SocketException not just ConnectException
                                logger.logError("Problem connecting " +
receiverAddress + " " + contactPort + " " + senderAddress + " for message " + new String(bytes, "UTF-8"));
@@ -314,6 +315,10 @@
throw new SocketException(e.getClass() + " " + e.getMessage() + " " + e.getCause() + " Problem connecting " + receiverAddress + " " + contactPort + " " + senderAddress + " for message " + new String(bytes, "UTF-8"));
                         }
+ if (logger.isLoggingEnabled(LogWriter.TRACE_DEBUG)) {
+                            logger.logDebug(
+ "local inaddr = " + clientSock.getLocalAddress().getHostAddress());
+                        }
                         OutputStream outputStream = clientSock
                                 .getOutputStream();
                         writeChunks(outputStream, bytes, length);

Reply via email to