KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > server > auth > passthru > AuthSessionFactory


1 /*
2  * Copyright (C) 2006 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.server.auth.passthru;
18
19 import java.io.IOException JavaDoc;
20 import java.net.InetAddress JavaDoc;
21 import java.net.UnknownHostException JavaDoc;
22
23 import org.alfresco.filesys.netbios.NetBIOSName;
24 import org.alfresco.filesys.netbios.NetBIOSNameList;
25 import org.alfresco.filesys.netbios.NetBIOSSession;
26 import org.alfresco.filesys.netbios.RFCNetBIOSProtocol;
27 import org.alfresco.filesys.server.auth.PasswordEncryptor;
28 import org.alfresco.filesys.smb.DataType;
29 import org.alfresco.filesys.smb.Dialect;
30 import org.alfresco.filesys.smb.DialectSelector;
31 import org.alfresco.filesys.smb.NetworkSession;
32 import org.alfresco.filesys.smb.PCShare;
33 import org.alfresco.filesys.smb.PacketType;
34 import org.alfresco.filesys.smb.Protocol;
35 import org.alfresco.filesys.smb.SMBException;
36 import org.alfresco.filesys.util.IPAddress;
37 import org.alfresco.filesys.util.StringList;
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41 /**
42  * <p>
43  * The AuthSessionFactory static class is used to create sessions to remote shared resources using
44  * the SMB/CIFS protocol. A PCShare object is used to specify the remote node and share details, as
45  * well as required access control details.
46  */

47 public final class AuthSessionFactory
48 {
49
50     // Debug logging
51

52     private static final Log logger = LogFactory.getLog("org.alfresco.smb.protocol.auth");
53
54     // Constants
55

56     private static final int BROADCAST_LOOKUP_TIMEOUT = 4000; // ms
57

58     // Default SMB dialect list
59

60     private static DialectSelector m_defDialects;
61
62     // Session index, used to make each session request call id unique
63

64     private static int m_sessIdx = 1;
65
66     // Local domain name, if known
67

68     private static String JavaDoc m_localDomain = null;
69
70     // Local domains browse master, if known
71

72     private static String JavaDoc m_localBrowseMaster = null;
73
74     // Default session packet buffer size
75

76     private static int m_defPktSize = 4096 + RFCNetBIOSProtocol.HEADER_LEN;
77
78     // List of local TCP/IP addresses
79

80     private static InetAddress JavaDoc[] m_localAddrList;
81
82     // Password encryptor
83

84     private static PasswordEncryptor m_encryptor = new PasswordEncryptor();
85
86     static
87     {
88
89         // Initialize the default dialect list
90

91         m_defDialects = new DialectSelector();
92         m_defDialects.AddDialect(Dialect.Core);
93         m_defDialects.AddDialect(Dialect.CorePlus);
94         m_defDialects.AddDialect(Dialect.DOSLanMan1);
95         m_defDialects.AddDialect(Dialect.DOSLanMan2);
96         m_defDialects.AddDialect(Dialect.LanMan1);
97         m_defDialects.AddDialect(Dialect.LanMan2);
98         m_defDialects.AddDialect(Dialect.LanMan2_1);
99         m_defDialects.AddDialect(Dialect.NT);
100     }
101
102     // Default user name, password and domain used by methods that create their own connections.
103

104     private static String JavaDoc m_defUserName = "";
105     private static String JavaDoc m_defPassword = "";
106     private static String JavaDoc m_defDomain = "?";
107
108     // Primary and secondary protocols to connect with
109

110     private static int m_primaryProto = Protocol.TCPNetBIOS;
111     private static int m_secondaryProto = Protocol.NativeSMB;
112
113     // NetBIOS port to connect to when setting up a new session. The default port is 139.
114

115     private static int m_netbiosPort = RFCNetBIOSProtocol.PORT;
116
117     // NetBIOS name scope
118

119     private static String JavaDoc m_netBIOSScopeId = null;
120     
121     // Flag to enable extended security exchanges in the session setup
122

123     private static boolean m_useExtendedSec = true;
124
125     /**
126      * Build an SMB negotiate dialect packet.
127      *
128      * @param pkt SMBPacket to build the negotiate request
129      * @param dlct SMB dialects to negotiate
130      * @param pid Process id to be used by this new session
131      * @return StringList
132      */

133
134     private final static StringList BuildNegotiatePacket(SMBPacket pkt, DialectSelector dlct, int pid)
135     {
136
137         // Initialize the SMB packet header fields
138

139         pkt.setCommand(PacketType.Negotiate);
140         pkt.setProcessId(pid);
141
142         // If the NT dialect is enabled set the Unicode flag in the request flags
143

144         int flags2 = 0;
145         
146         if (dlct.hasDialect(Dialect.NT))
147             flags2 += SMBPacket.FLG2_UNICODE;
148         
149         if ( useExtendedSecurity())
150             flags2 += SMBPacket.FLG2_EXTENDEDSECURITY;
151             
152         pkt.setFlags2(flags2);
153
154         // Build the SMB dialect list
155

156         StringBuilder JavaDoc dia = new StringBuilder JavaDoc();
157         StringList diaList = new StringList();
158
159         // Loop through all SMB dialect types and add the appropriate dialect strings
160
// to the negotiate packet.
161

162         int d = Dialect.Core;
163
164         while (d < Dialect.Max)
165         {
166
167             // Check if the current dialect is selected
168

169             if (dlct.hasDialect(d))
170             {
171
172                 // Search the SMB dialect type string list and add all strings for the
173
// current dialect
174

175                 for (int i = 0; i < Dialect.NumberOfDialects(); i++)
176                 {
177
178                     // Check if the current dialect string should be added to the list
179

180                     if (Dialect.DialectType(i) == d)
181                     {
182
183                         // Get the current SMB dialect string
184

185                         String JavaDoc curDialect = Dialect.DialectString(i);
186                         diaList.addString(curDialect);
187
188                         // Add the current SMB dialect type string to the negotiate packet
189

190                         dia.append(DataType.Dialect);
191                         dia.append(curDialect);
192                         dia.append((char) 0x00);
193                     }
194                 }
195             }
196
197             // Update the current dialect type
198

199             d++;
200         }
201
202         // Copy the dialect strings to the SMB packet
203

204         pkt.setBytes(dia.toString().getBytes());
205
206         // Return the dialect strings
207

208         return diaList;
209     }
210
211     /**
212      * Return the default SMB packet size
213      *
214      * @return Default SMB packet size to allocate.
215      */

216     protected final static int DefaultPacketSize()
217     {
218         return m_defPktSize;
219     }
220
221     /**
222      * Return the list of SMB dialects that will be negotiated when a new session is created.
223      *
224      * @return DialectSelector List of enabled SMB dialects.
225      */

226     public final static DialectSelector getDefaultDialects()
227     {
228         return m_defDialects;
229     }
230
231     /**
232      * Return the default domain name
233      *
234      * @return String
235      */

236     public static String JavaDoc getDefaultDomain()
237     {
238         return m_defDomain;
239     }
240
241     /**
242      * Return the default password.
243      *
244      * @return java.lang.String
245      */

246     public static String JavaDoc getDefaultPassword()
247     {
248         return m_defPassword;
249     }
250
251     /**
252      * Return the default user name.
253      *
254      * @return java.lang.String
255      */

256     public static String JavaDoc getDefaultUserName()
257     {
258         return m_defUserName;
259     }
260
261     /**
262      * Return the NetBIOS scope id, or null if not set
263      *
264      * @return String
265      */

266     public static String JavaDoc getNetBIOSNameScope()
267     {
268         return m_netBIOSScopeId;
269     }
270
271     /**
272      * Return the NetBIOS socket number that new sessions are connected to.
273      *
274      * @return int NetBIOS session socket number.
275      */

276     public static int getNetBIOSPort()
277     {
278         return m_netbiosPort;
279     }
280
281     /**
282      * Return the primary connection protocol (either Protocol.TCPNetBIOS or Protocol.NativeSMB)
283      *
284      * @return int
285      */

286     public static final int getPrimaryProtocol()
287     {
288         return m_primaryProto;
289     }
290
291     /**
292      * Return the secondary connection protocol (Protocol.TCPNetBIOS, Protocol.NativeSMB or
293      * Protocol.None)
294      *
295      * @return int
296      */

297     public static final int getSecondaryProtocol()
298     {
299         return m_secondaryProto;
300     }
301
302     /**
303      * Return the next session id
304      *
305      * @return int
306      */

307     private static synchronized int getSessionId()
308     {
309         int sessId = m_sessIdx++ + (NetBIOSSession.getJVMIndex() * 100);
310         return sessId;
311     }
312
313     /**
314      * Get the list of local TCP/IP addresses
315      *
316      * @return InetAddress[]
317      */

318     private static synchronized InetAddress JavaDoc[] getLocalTcpipAddresses()
319     {
320
321         // Get the list of local TCP/IP addresses
322

323         if (m_localAddrList == null)
324         {
325             try
326             {
327                 m_localAddrList = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
328             }
329             catch (UnknownHostException JavaDoc ex)
330             {
331             }
332         }
333
334         // Return the address list
335

336         return m_localAddrList;
337     }
338
339     /**
340      * Determine if the NetBIOS name scope is set
341      *
342      * @return boolean
343      */

344     public final static boolean hasNetBIOSNameScope()
345     {
346         return m_netBIOSScopeId != null ? true : false;
347     }
348
349     /**
350      * Determine if extended security exchanges are enabled
351      *
352      * @return boolean
353      */

354     public static final boolean useExtendedSecurity()
355     {
356         return m_useExtendedSec;
357     }
358     
359     /**
360      * Open a session to a remote server, negotiate an SMB dialect and get the returned challenge
361      * key. Returns an AuthenticateSession which can then be used to provide passthru
362      * authentication.
363      *
364      * @param shr Remote server share and access control details.
365      * @param tmo Timeout value in milliseconds
366      * @return AuthenticateSession for the new session, else null.
367      * @exception java.io.IOException If an I/O error occurs.
368      * @exception java.net.UnknownHostException Remote node is unknown.
369      * @exception SMBException Failed to setup a new session.
370      */

371     public static AuthenticateSession OpenAuthenticateSession(PCShare shr, int tmo) throws java.io.IOException JavaDoc,
372             java.net.UnknownHostException JavaDoc, SMBException
373     {
374
375         // Open an authentication session
376

377         return OpenAuthenticateSession(shr, tmo, null);
378     }
379
380     /**
381      * Open a session to a remote server, negotiate an SMB dialect and get the returned challenge
382      * key. Returns an AuthenticateSession which can then be used to provide passthru
383      * authentication.
384      *
385      * @param shr Remote server share and access control details.
386      * @param tmo Timeout value in milliseconds
387      * @param dia SMB dialects to negotiate for this session.
388      * @return AuthenticateSession for the new session, else null.
389      * @exception java.io.IOException If an I/O error occurs.
390      * @exception java.net.UnknownHostException Remote node is unknown.
391      * @exception SMBException Failed to setup a new session.
392      */

393     public static AuthenticateSession OpenAuthenticateSession(PCShare shr, int tmo, DialectSelector dia)
394             throws java.io.IOException JavaDoc, java.net.UnknownHostException JavaDoc, SMBException
395     {
396
397         // Build a unique caller name
398

399         int pid = getSessionId();
400
401         StringBuilder JavaDoc nameBuf = new StringBuilder JavaDoc(InetAddress.getLocalHost().getHostName() + "_" + pid);
402         String JavaDoc localName = nameBuf.toString();
403
404         // Debug
405

406         if (logger.isDebugEnabled())
407             logger.debug("New auth session from " + localName + " to " + shr.toString());
408
409         // Connect to the requested server
410

411         NetworkSession netSession = null;
412
413         switch (getPrimaryProtocol())
414         {
415
416         // NetBIOS connection
417

418         case Protocol.TCPNetBIOS:
419             netSession = connectNetBIOSSession(shr.getNodeName(), localName, tmo);
420             break;
421
422         // Native SMB connection
423

424         case Protocol.NativeSMB:
425             netSession = connectNativeSMBSession(shr.getNodeName(), localName, tmo);
426             break;
427         }
428
429         // If the connection was not made using the primary protocol try the secondary protocol, if
430
// configured
431

432         if (netSession == null)
433         {
434
435             // Try the secondary protocol
436

437             switch (getSecondaryProtocol())
438             {
439
440             // NetBIOS connection
441

442             case Protocol.TCPNetBIOS:
443                 netSession = connectNetBIOSSession(shr.getNodeName(), localName, tmo);
444                 break;
445
446             // Native SMB connection
447

448             case Protocol.NativeSMB:
449                 netSession = connectNativeSMBSession(shr.getNodeName(), localName, tmo);
450                 break;
451             }
452         }
453
454         // Check if we connected to the remote host
455

456         if (netSession == null)
457             throw new IOException JavaDoc("Failed to connect to host, " + shr.getNodeName());
458
459         // Debug
460

461         if (logger.isDebugEnabled())
462             logger.debug("Connected session, protocol : " + netSession.getProtocolName());
463
464         // Build a protocol negotiation SMB packet, and send it to the remote
465
// file server.
466

467         SMBPacket pkt = new SMBPacket();
468         DialectSelector selDialect = dia;
469
470         if (selDialect == null)
471         {
472
473             // Use the default SMB dialect list
474

475             selDialect = new DialectSelector();
476             selDialect.copyFrom(m_defDialects);
477         }
478
479         // Build the negotiate SMB dialect packet and exchange with the remote server
480

481         StringList diaList = BuildNegotiatePacket(pkt, selDialect, pid);
482         pkt.ExchangeLowLevelSMB(netSession, pkt, true);
483
484         // Determine the selected SMB dialect
485

486         String JavaDoc diaStr = diaList.getStringAt(pkt.getParameter(0));
487         int dialectId = Dialect.DialectType(diaStr);
488
489         // DEBUG
490

491         if (logger.isDebugEnabled())
492             logger.debug("SessionFactory: Negotiated SMB dialect " + diaStr);
493
494         if (dialectId == Dialect.Unknown)
495             throw new java.io.IOException JavaDoc("Unknown SMB dialect");
496
497         // Create the authenticate session
498

499         AuthenticateSession authSess = new AuthenticateSession(shr, netSession, dialectId, pkt);
500         return authSess;
501     }
502
503     /**
504      * Set the default domain.
505      *
506      * @param domain String
507      */

508     public static void setDefaultDomain(String JavaDoc domain)
509     {
510         m_defDomain = domain;
511     }
512
513     /**
514      * Set the default password.
515      *
516      * @param pwd java.lang.String
517      */

518     public static void setDefaultPassword(String JavaDoc pwd)
519     {
520         m_defPassword = pwd;
521     }
522
523     /**
524      * Set the default user name.
525      *
526      * @param user java.lang.String
527      */

528     public static void setDefaultUserName(String JavaDoc user)
529     {
530         m_defUserName = user;
531     }
532
533     /**
534      * Enable/disable the use of extended security exchanges
535      *
536      * @param ena boolean
537      */

538     public static final void setExtendedSecurity(boolean ena)
539     {
540         m_useExtendedSec = ena;
541     }
542     
543     /**
544      * Set the NetBIOS socket number to be used when setting up new sessions. The default socket is
545      * 139.
546      *
547      * @param port int
548      */

549     public static void setNetBIOSPort(int port)
550     {
551         m_netbiosPort = port;
552     }
553
554     /**
555      * Set the NetBIOS scope id
556      *
557      * @param scope String
558      */

559     public static void setNetBIOSNameScope(String JavaDoc scope)
560     {
561         if (scope != null && scope.startsWith("."))
562             m_netBIOSScopeId = scope.substring(1);
563         else
564             m_netBIOSScopeId = scope;
565     }
566
567     /**
568      * Set the protocol connection order
569      *
570      * @param pri Primary connection protocol
571      * @param sec Secondary connection protocol, or none
572      * @return boolean
573      */

574     public static final boolean setProtocolOrder(int pri, int sec)
575     {
576
577         // Primary protocol must be specified
578

579         if (pri != Protocol.TCPNetBIOS && pri != Protocol.NativeSMB)
580             return false;
581
582         // Primary and secondary must be different
583

584         if (pri == sec)
585             return false;
586
587         // Save the settings
588

589         m_primaryProto = pri;
590         m_secondaryProto = sec;
591
592         return true;
593     }
594
595     /**
596      * Set the subnet mask string for network broadcast requests If the subnet mask is not set a
597      * default broadcast mask for the TCP/IP address class will be used.
598      *
599      * @param subnet Subnet mask string, in 'nnn.nnn.nnn.nnn' format.
600      */

601     public final static void setSubnetMask(String JavaDoc subnet)
602     {
603         NetBIOSSession.setSubnetMask(subnet);
604     }
605
606     /**
607      * Setup the default SMB dialects to be negotiated when creating new sessions.
608      */

609     private static void SetupDefaultDialects()
610     {
611
612         // Initialize the default dialect list
613

614         if (m_defDialects != null)
615             m_defDialects = new DialectSelector();
616         else
617             m_defDialects.ClearAll();
618
619         // Always enable core protocol
620

621         m_defDialects.AddDialect(Dialect.Core);
622         m_defDialects.AddDialect(Dialect.CorePlus);
623         m_defDialects.AddDialect(Dialect.DOSLanMan1);
624         m_defDialects.AddDialect(Dialect.DOSLanMan2);
625         m_defDialects.AddDialect(Dialect.LanMan1);
626         m_defDialects.AddDialect(Dialect.LanMan2);
627         m_defDialects.AddDialect(Dialect.LanMan2_1);
628         m_defDialects.AddDialect(Dialect.NT);
629     }
630
631     /**
632      * Connect a NetBIOS network session
633      *
634      * @param toName Host name/address to connect to
635      * @param fromName Local host name/address
636      * @param tmo Timeout in seconds
637      * @return NetworkSession
638      * @exception IOException If a network error occurs
639      */

640     private static final NetworkSession connectNetBIOSSession(String JavaDoc toName, String JavaDoc fromName, int tmo)
641             throws IOException JavaDoc
642     {
643
644         // Connect to the requested server
645

646         NetBIOSSession nbSession = new NetBIOSSession(tmo, getNetBIOSPort());
647
648         // Check if the remote host is specified as a TCP/IP address
649

650         String JavaDoc toAddr = null;
651         NetBIOSName nbName = null;
652
653         if (IPAddress.isNumericAddress(toName))
654         {
655
656             try
657             {
658
659                 // Get a list of NetBIOS names from the remote host
660

661                 toAddr = toName;
662                 NetBIOSNameList nameList = NetBIOSSession.FindNamesForAddress(toAddr);
663
664                 // Find the server service
665

666                 nbName = nameList.findName(NetBIOSName.FileServer, false);
667                 if (nbName == null)
668                     throw new IOException JavaDoc("Server service not running");
669
670                 // Set the remote name
671

672                 toName = nbName.getName();
673             }
674             catch (UnknownHostException JavaDoc ex)
675             {
676                 return null;
677             }
678         }
679         else
680         {
681             IOException JavaDoc savedException = null;
682
683             try
684             {
685
686                 // Find the remote host and get a list of the network addresses it is using
687

688                 nbName = NetBIOSSession.FindName(toName, NetBIOSName.FileServer, 500);
689             }
690             catch (IOException JavaDoc ex)
691             {
692                 savedException = ex;
693             }
694
695             // If the NetBIOS name was not found then check if the local system has the name
696

697             if (nbName == null)
698             {
699
700                 // Get a list of NetBIOS names for the local system
701

702                 NetBIOSNameList localList = NetBIOSSession.FindNamesForAddress(InetAddress.getLocalHost()
703                         .getHostAddress());
704                 if (localList != null)
705                 {
706                     nbName = localList.findName(toName, NetBIOSName.FileServer, false);
707                     if (nbName != null)
708                         nbName.addIPAddress(InetAddress.getLocalHost().getAddress());
709                     else
710                         throw savedException;
711                 }
712             }
713         }
714
715         // Check if the NetBIOS name scope has been set, if so then update the names to add the
716
// scope id
717

718         if (hasNetBIOSNameScope())
719         {
720
721             // Add the NetBIOS scope id to the to/from NetBIOS names
722

723             toName = toName + "." + getNetBIOSNameScope();
724             fromName = fromName + "." + getNetBIOSNameScope();
725         }
726
727         // If the NetBIOS name has more than one TCP/IP address then find the best match for the
728
// client and
729
// try to connect on that address first, if that fails then we will have to try each address
730
// in turn.
731

732         if (nbName.numberOfAddresses() > 1)
733         {
734
735             // Get the local TCP/IP address list and search for a best match address to connect to
736
// the server on
737

738             InetAddress JavaDoc[] addrList = getLocalTcpipAddresses();
739             int addrIdx = nbName.findBestMatchAddress(addrList);
740
741             if (addrIdx != -1)
742             {
743
744                 try
745                 {
746
747                     // Get the server IP address
748

749                     String JavaDoc ipAddr = nbName.getIPAddressString(addrIdx);
750
751                     // DEBUG
752

753                     if (logger.isDebugEnabled())
754                         logger.debug("Server is multi-homed, trying to connect to " + ipAddr);
755
756                     // Open the session to the remote host
757

758                     nbSession.Open(toName, fromName, ipAddr);
759
760                     // Check if the session is connected
761

762                     if (nbSession.isConnected() == false)
763                     {
764
765                         // Close the session
766

767                         try
768                         {
769                             nbSession.Close();
770                         }
771                         catch (Exception JavaDoc ex)
772                         {
773                         }
774                     }
775                     else if (logger.isDebugEnabled() && nbSession.isConnected())
776                         logger.debug("Connected to address " + ipAddr);
777                 }
778                 catch (IOException JavaDoc ex)
779                 {
780                 }
781             }
782         }
783
784         // DEBUG
785

786         if (logger.isDebugEnabled() && nbSession.isConnected() == false
787                 && nbName.numberOfAddresses() > 1)
788             logger.debug("Server is multi-homed, trying all addresses");
789
790         // Loop through the available addresses for the remote file server until we get a successful
791
// connection, or all addresses have been used
792

793         IOException JavaDoc lastException = null;
794         int addrIdx = 0;
795
796         while (nbSession.isConnected() == false && addrIdx < nbName.numberOfAddresses())
797         {
798
799             try
800             {
801
802                 // Get the server IP address
803

804                 String JavaDoc ipAddr = nbName.getIPAddressString(addrIdx++);
805
806                 // DEBUG
807

808                 if (logger.isDebugEnabled())
809                     logger.debug("Trying address " + ipAddr);
810
811                 // Open the session to the remote host
812

813                 nbSession.Open(toName, fromName, ipAddr);
814
815                 // Check if the session is connected
816

817                 if (nbSession.isConnected() == false)
818                 {
819
820                     // Close the session
821

822                     try
823                     {
824                         nbSession.Close();
825                     }
826                     catch (Exception JavaDoc ex)
827                     {
828                     }
829                 }
830                 else if (logger.isDebugEnabled() && nbSession.isConnected())
831                     logger.debug("Connected to address " + ipAddr);
832             }
833             catch (IOException JavaDoc ex)
834             {
835
836                 // Save the last exception
837

838                 lastException = ex;
839             }
840         }
841
842         // Check if the session is connected
843

844         if (nbSession.isConnected() == false)
845         {
846
847             // If there is a saved exception rethrow it
848

849             if (lastException != null)
850                 throw lastException;
851
852             // Indicate that the session was not connected
853

854             return null;
855         }
856
857         // Return the network session
858

859         return nbSession;
860     }
861
862     /**
863      * Connect a native SMB network session
864      *
865      * @param toName Host name/address to connect to
866      * @param fromName Local host name/address
867      * @param tmo Timeout in seconds
868      * @return NetworkSession
869      * @exception IOException If a network error occurs
870      */

871     private static final NetworkSession connectNativeSMBSession(String JavaDoc toName, String JavaDoc fromName, int tmo)
872             throws IOException JavaDoc
873     {
874
875         // Connect to the requested server
876

877         TcpipSMBNetworkSession tcpSession = new TcpipSMBNetworkSession(tmo);
878
879         try
880         {
881
882             // Open the session
883

884             tcpSession.Open(toName, fromName, null);
885
886             // Check if the session is connected
887

888             if (tcpSession.isConnected() == false)
889             {
890
891                 // Close the session
892

893                 try
894                 {
895                     tcpSession.Close();
896                 }
897                 catch (Exception JavaDoc ex)
898                 {
899                 }
900
901                 // Return a null session
902

903                 return null;
904             }
905         }
906         catch (Exception JavaDoc ex)
907         {
908             try
909             {
910                 tcpSession.Close();
911             }
912             catch (Exception JavaDoc ex2)
913             {
914             }
915             tcpSession = null;
916         }
917
918         // Return the network session
919

920         return tcpSession;
921     }
922 }
Popular Tags