/[AnywhereTS-MSSQL]/trunk/TSAdminTool/DHCP.cs
ViewVC logotype

Contents of /trunk/TSAdminTool/DHCP.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 47 - (show annotations) (download)
Thu Jul 12 14:29:34 2012 UTC (8 years ago) by william
File size: 11229 byte(s)
+ fix compilation errors

1 using System;
2 using System.ServiceProcess;
3 using System.Collections.Generic;
4 using System.Text;
5 using System.Reflection; // For path (assembly)
6 using System.IO; // For Path
7 using System.Security; // Security Exception
8 using System.Windows.Forms; // Message Box
9 using System.Text.RegularExpressions; // For validate MAC address
10 using log4net;
11 namespace AnywhereTS
12 {
13 public static class AtsDhcp // DHCP Support
14 {
15 static public string TFTPD_SERVICE_NAME="TS TFTP";
16 static public string strTftpd32IP; // The IP of the nic used by AnywhereTS TFTPD32
17 static public int intPoolSize; // The IP pool Size used by AnywhereTS TFTPD32
18 static public int intDHCP_IP; // The IP of the nic used by AnywhereTS TFTPD32
19 static public int intNetMask; // The netmask AnywhereTS TFTPD32
20 static public int intGateway; // The Gateway AnywhereTS TFTPD32
21 static public int intDNS; // The DNS AnywhereTS TFTPD32
22 static public int intIP_Pool; // The first ip used by AnywhereTS TFTPD32 DHCP
23 static public int intDHCPon; // The mode of TFTPD 32 5= dhcp + tftp 1=TFTP
24
25 // Constructor
26 static AtsDhcp()
27 {
28 }
29
30 public static void OpenTFTP32regKey()
31 {
32 intIP_Pool = Int32.Parse(ATSGlobals.GetRegValue(ATSGlobals.strTFTPD32RegRoot + @"\DHCP", @"IP_Pool"));
33 intPoolSize = Int32.Parse(ATSGlobals.GetRegValue(ATSGlobals.strTFTPD32RegRoot + @"\DHCP", @"PoolSize"));
34 intNetMask = Int32.Parse(ATSGlobals.GetRegValue(ATSGlobals.strTFTPD32RegRoot + @"\DHCP", "Mask"));
35 intGateway = Int32.Parse(ATSGlobals.GetRegValue(ATSGlobals.strTFTPD32RegRoot + @"\DHCP", "Gateway"));
36 intDNS = Int32.Parse(ATSGlobals.GetRegValue(ATSGlobals.strTFTPD32RegRoot + @"\DHCP", "DNS"));
37 strTftpd32IP = ATSGlobals.GetRegValue(ATSGlobals.strTFTPD32RegRoot, "LocalIP");
38 intDHCPon = Int32.Parse(ATSGlobals.GetRegValue(ATSGlobals.strTFTPD32RegRoot, "Services"));
39 //error
40 //AtsDhcp.intIP_Pool = &H1400DF0A
41 //AtsDhcp.intDNS = &H100DF0A
42 //AtsDhcp.intNetMask = &HFFFF
43 //AtsDhcp.intPoolSize = &HC8
44 //AtsDhcp.intGateway = &H100DF0A
45 //AtsDhcp.strTftpd32IP = "10.223.0.1"
46 //AtsDhcp.intDHCPon = 1
47 //On Error GoTo 0
48 //return
49 }
50 //Configure TFTPD32. bolDHCP = true -> DHCP enabled
51 public static void ConfTFTPD32(bool bolDHCP)
52 {
53 StopTFTPD32();
54 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "LastWindowPos", "60 49 860 642 ");
55 //objRegkey.SetValue("BaseDirectory", Globals.strTFTPdir)
56 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "TftpPort", 69);
57 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "Hide", 0);
58 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "WinSize", 0);
59 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "Negociate", 1);
60 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "PXECompatibility", 0);
61 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "DirText", 0);
62 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "ShowProgressBar", 0);
63 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "Timeout", 3);
64 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "MaxRetransmit", 6);
65 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "SecurityLevel", 1);
66 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "UnixStrings", 1);
67 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "Beep", 0);
68 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "VirtualRoot", 1);
69 //If Globals.intNoOfValidAdapters = 1 Then
70 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "LocalIP", strTftpd32IP);//fix mee
71 if (bolDHCP)
72 {
73 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "Services", 5);//DHCP + TFTP
74 }
75 else
76 {
77 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "Services", 1);//TFTP only
78 }
79 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "TftpLogFile", "");
80 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "SaveSyslogFile", "");
81 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot + @"\DHCP", "IP_Pool", intIP_Pool);
82 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot + @"\DHCP", "PoolSize", intPoolSize);
83 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot + @"\DHCP", "Mask", intNetMask);
84 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot + @"\DHCP", "Gateway", intGateway);
85 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot + @"\DHCP", "DNS", intDNS);
86 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot + @"\DHCP", "BootFile", "client.zpxe");
87 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot + @"\DHCP", "DomainName", "");
88 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot + @"\DHCP", "AddOptionNumber1", "");
89 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot + @"\DHCP", "AddOptionValue1", 0);
90 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot + @"\DHCP", "AddOptionNumber2", "");
91 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot + @"\DHCP", "AddOptionValue2", 0);
92 StartTFTPD32();
93 //Return
94 }
95 public static void StartTFTPD32()
96 {
97 System.ServiceProcess.ServiceController srvController = new System.ServiceProcess.ServiceController(TFTPD_SERVICE_NAME);
98 try
99 {
100 srvController.Start();
101 srvController.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(50));
102 }
103 catch(Exception e)
104 {
105 MessageBox.Show(@"Could not start the TFTPD service.");
106 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
107 {
108 Logging.ATSAdminLog.Error("Could not start the TFTPD service.");
109 }
110 }
111 }
112 public static void StopTFTPD32()
113 {
114 ServiceController srvController = new ServiceController(TFTPD_SERVICE_NAME);
115 if (srvController.CanStop)
116 {
117 try
118 {
119 srvController.Stop();
120 srvController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(30));
121 }
122 catch(Exception e)
123 {
124 MessageBox.Show(@"Could not stop the TFTPD service.");
125 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
126 {
127 Logging.ATSAdminLog.Error("Could not stop the TFTPD service.");
128 }
129 }
130 }
131 }
132 public static bool UDPPortInUse(int intPort)
133 {
134 System.Net.IPAddress localAddress;
135 localAddress=System.Net.Dns.GetHostEntry("localhost").AddressList[0];
136 System.Net.Sockets.Socket udpSocket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
137 System.Net.IPEndPoint localEndpoint = new System.Net.IPEndPoint(localAddress, intPort);
138 bool retVal = false;
139 try
140 {
141 udpSocket.Bind(localEndpoint);
142 }
143 catch
144 {
145 retVal = true;
146 }
147 udpSocket.Close();
148 return retVal;
149 }
150 public static bool CheckForLocalTFTPD()
151 {
152 return UDPPortInUse(69);
153 }
154 public static bool CheckForLocalDHCP()
155 {
156 return UDPPortInUse(67);
157 }
158 public static bool IsTFTPD32Running()
159 {
160 ServiceController srvController = new ServiceController(TFTPD_SERVICE_NAME);
161 return srvController.Status == ServiceControllerStatus.Running;
162 }
163 //Check if the TFTPD32 service is installed on this computer
164 public static bool HasTFTPD32()
165 {
166 ServiceController[] arrController;
167 int i;
168 arrController = ServiceController.GetServices();
169 for (i = 0; i <= arrController.Length - 1; i ++)
170 {
171 if (arrController[i].DisplayName == TFTPD_SERVICE_NAME)
172 return true;
173 }
174 return false;
175 }
176
177 //Configures TFTPD mode
178 public static void ConfigureTFTPD32service(bool bolTFTP, bool bolDHCP)
179 {
180 string strTFTPD32root;
181 StopTFTPD32();
182 strTFTPD32root = ATSGlobals.GetATSRegValueString("TFTP_root");
183 if (bolDHCP == false)
184 if (bolTFTP == false)
185 {
186 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "Services", 2); //TFTP client "no service"
187 }
188 else
189 {
190 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "Services", 1); //TFTP Server no DHCP Server
191 }
192 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "BaseDirectory", strTFTPD32root);
193 if (bolDHCP == true)
194 {
195 if (bolTFTP == false)
196 {
197 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "Services", 4);//DHCP Server no TFTP Server
198 }
199 else
200 {
201 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "Services", 5);//DHCP Server and TFTP Server
202 }
203 }
204 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot, "BaseDirectory", strTFTPD32root);
205 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot + @"\DHCP", "IP_Pool", intIP_Pool);
206 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot + @"\DHCP", "PoolSize", intPoolSize);
207 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot + @"\DHCP", "DNS", intDNS);
208 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot + @"\DHCP", "Mask", intNetMask);
209 ATSGlobals.SetRegValue(ATSGlobals.strTFTPD32RegRoot + @"\DHCP", "Gateway", intGateway);
210 if (bolTFTP || bolDHCP )
211 {
212 StartTFTPD32();
213 }
214 }
215
216
217 // Create all needed registry values for TFTPD32
218 public static void CreateRegistryValues()
219 {
220 }
221
222 }
223 }

  ViewVC Help
Powered by ViewVC 1.1.22