KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > netbios > NetworkSettings


1 /*
2  * Copyright (C) 2005 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.netbios;
18
19 import java.net.InetAddress JavaDoc;
20
21 /**
22  * The network settings class contains various Windows Networking settings that are needed by the
23  * NetBIOS and SMB layers.
24  */

25 public class NetworkSettings
26 {
27
28     // Broadcast mask for broadcast messages
29

30     private static String JavaDoc m_broadcastMask;
31
32     // Domain name/workgroup that this node is part of
33

34     private static String JavaDoc m_domain;
35
36     // Subnet mask address
37

38     private static InetAddress JavaDoc m_subnetAddr;
39
40     /**
41      * Determine the boradcast mask from the local hosts TCP/IP address
42      *
43      * @param addr TCP/IP address to set the broadcast mask for, in 'nnn.nnn.nnn.nnn' format.
44      */

45     public static String JavaDoc GenerateBroadcastMask(String JavaDoc addr) throws java.net.UnknownHostException JavaDoc
46     {
47
48         // Check if the broadcast mask has already been set
49

50         if (m_broadcastMask != null)
51             return m_broadcastMask;
52
53         // Set the TCP/IP address string
54

55         String JavaDoc localIP = addr;
56
57         if (localIP == null)
58             localIP = InetAddress.getLocalHost().getHostAddress();
59
60         // Find the location of the first dot in the TCP/IP address
61

62         int dotPos = localIP.indexOf('.');
63         if (dotPos != -1)
64         {
65
66             // Extract the leading IP address value
67

68             String JavaDoc ipStr = localIP.substring(0, dotPos);
69             int ipVal = Integer.valueOf(ipStr).intValue();
70
71             // Determine the broadcast mask to use
72

73             if (ipVal <= 127)
74             {
75
76                 // Class A address
77

78                 m_broadcastMask = "" + ipVal + ".255.255.255";
79             }
80             else if (ipVal <= 191)
81             {
82
83                 // Class B adddress
84

85                 dotPos++;
86                 while (localIP.charAt(dotPos) != '.' && dotPos < localIP.length())
87                     dotPos++;
88
89                 if (dotPos < localIP.length())
90                     m_broadcastMask = localIP.substring(0, dotPos) + ".255.255";
91             }
92             else if (ipVal <= 223)
93             {
94
95                 // Class C address
96

97                 dotPos++;
98                 int dotCnt = 1;
99
100                 while (dotCnt < 3 && dotPos < localIP.length())
101                 {
102
103                     // Check if the current character is a dot
104

105                     if (localIP.charAt(dotPos++) == '.')
106                         dotCnt++;
107                 }
108
109                 if (dotPos < localIP.length())
110                     m_broadcastMask = localIP.substring(0, dotPos - 1) + ".255";
111             }
112         }
113
114         // Check if the broadcast mask has been set, if not then use a general
115
// broadcast mask
116

117         if (m_broadcastMask == null)
118         {
119
120             // Invalid TCP/IP address string format, use a general broadcast mask
121
// for now.
122

123             m_broadcastMask = "255.255.255.255";
124         }
125
126         // Return the broadcast mask string
127

128         return m_broadcastMask;
129     }
130
131     /**
132      * Return the broadcast mask as an address.
133      *
134      * @return java.net.InetAddress
135      */

136     public final static InetAddress JavaDoc getBroadcastAddress() throws java.net.UnknownHostException JavaDoc
137     {
138
139         // Check if the subnet address is valid
140

141         if (m_subnetAddr == null)
142         {
143
144             // Generate the subnet mask
145

146             String JavaDoc subnet = GenerateBroadcastMask(null);
147             m_subnetAddr = InetAddress.getByName(subnet);
148         }
149
150         // Return the subnet mask address
151

152         return m_subnetAddr;
153     }
154
155     /**
156      * Get the broadcast mask.
157      *
158      * @return java.lang.String
159      */

160     public static String JavaDoc getBroadcastMask()
161     {
162         return m_broadcastMask;
163     }
164
165     /**
166      * Get the local domain/workgroup name.
167      */

168     public static String JavaDoc getDomain()
169     {
170         return m_domain;
171     }
172
173     /**
174      * Determine if the broadcast mask has been setup.
175      */

176     public static boolean hasBroadcastMask()
177     {
178         if (m_broadcastMask == null)
179             return false;
180         return true;
181     }
182
183     /**
184      * Set the broadcast mask to be used for broadcast packets.
185      *
186      * @param mask java.lang.String
187      */

188     public static void setBroadcastMask(String JavaDoc mask)
189     {
190         m_broadcastMask = mask;
191     }
192
193     /**
194      * Set the local domain/workgroup name.
195      *
196      * @param domain java.lang.String
197      */

198     public static void setDomain(String JavaDoc domain)
199     {
200         m_domain = domain;
201     }
202 }
Popular Tags