ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/CCSFinger/CCSFinger.cs
Revision: 727
Committed: 2006-03-31T05:08:33-08:00 (19 years, 3 months ago) by douglas
File size: 4573 byte(s)
Log Message:
Maybe this will work, it seems to work here.

File Contents

# Content
1 // Douglas Thrift
2 //
3 // CCS Computer Science
4 //
5 // Windows Finger Daemon
6
7 using System;
8 using System.Collections.Generic;
9 using System.IO;
10 using System.Net;
11 using System.Net.Sockets;
12 using System.Runtime.InteropServices;
13 using System.ServiceProcess;
14 using System.Text;
15 using System.Threading;
16
17 public class CCSFinger : ServiceBase
18 {
19 public enum CurrentState : uint
20 {
21 SERVICE_STOPPED = 0x1,
22 SERVICE_START_PENDING = 0x2,
23 SERVICE_STOP_PENDING = 0x3,
24 SERVICE_RUNNING = 0x4,
25 SERVICE_CONTINUE_PENDING = 0x5,
26 SERVICE_PAUSE_PENDING = 0x6,
27 SERVICE_PAUSED = 0x7
28 }
29
30 [StructLayout(LayoutKind.Sequential)]
31 public struct SERVICE_STATUS
32 {
33 public uint dwServiceType;
34 public CurrentState dwCurrentState;
35 public uint dwControlsAccepted;
36 public uint dwWin32ExitCode;
37 public uint dwServiceSpecificExitCode;
38 public uint dwCheckPoint;
39 public uint dwWaitHint;
40 }
41
42 private Thread thread;
43
44 public CCSFinger()
45 {
46 ServiceName = "CCSFinger";
47 }
48
49 [DllImport("Advapi32.dll", EntryPoint = "QueryServiceStatus")]
50 public static extern bool QueryServiceStatus(IntPtr hService, IntPtr lpServiceStatus);
51
52 public static bool QueryServiceStatus(IntPtr hService, out SERVICE_STATUS lpServiceStatus)
53 {
54 IntPtr pointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SERVICE_STATUS)));
55 bool value = QueryServiceStatus(hService, pointer);
56
57 lpServiceStatus = (SERVICE_STATUS)Marshal.PtrToStructure(pointer, typeof(SERVICE_STATUS));
58
59 Marshal.FreeHGlobal(pointer);
60
61 return value;
62 }
63
64 [DllImport("Advapi32.dll", EntryPoint = "SetServiceStatus")]
65 public static extern bool SetServiceStatus(IntPtr hServiceStatus, IntPtr lpServiceStatus);
66
67 public static bool SetServiceStatus(IntPtr hServiceStatus, SERVICE_STATUS lpServiceStatus)
68 {
69 IntPtr pointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SERVICE_STATUS)));
70
71 Marshal.StructureToPtr(lpServiceStatus, pointer, false);
72
73 bool value = SetServiceStatus(hServiceStatus, pointer);
74
75 Marshal.FreeHGlobal(pointer);
76
77 return value;
78 }
79
80 protected override void OnStart(string[] args)
81 {
82 SERVICE_STATUS status;
83
84 QueryServiceStatus(ServiceHandle, out status);
85
86 status.dwCurrentState = CurrentState.SERVICE_START_PENDING;
87
88 SetServiceStatus(ServiceHandle, status);
89
90 thread = new Thread(new ThreadStart(Do));
91
92 thread.Start();
93
94 ExitCode = 0;
95
96 QueryServiceStatus(ServiceHandle, out status);
97
98 status.dwCurrentState = CurrentState.SERVICE_RUNNING;
99
100 SetServiceStatus(ServiceHandle, status);
101 }
102
103 protected override void OnStop()
104 {
105 SERVICE_STATUS status;
106
107 QueryServiceStatus(ServiceHandle, out status);
108
109 status.dwCurrentState = CurrentState.SERVICE_STOP_PENDING;
110
111 thread.Abort();
112 thread.Join();
113
114 ExitCode = 0;
115
116 QueryServiceStatus(ServiceHandle, out status);
117
118 status.dwCurrentState = CurrentState.SERVICE_STOPPED;
119 }
120
121 private void Do()
122 {
123 TcpListener listener = null;
124
125 try
126 {
127 listener = new TcpListener(IPAddress.Any, 79);
128
129 listener.Start();
130
131 while (true)
132 {
133 Socket socket = listener.AcceptSocket();
134
135 socket.ReceiveTimeout = 60000;
136
137 Thread thread = new Thread(new ParameterizedThreadStart(Do));
138
139 thread.Start(socket);
140 }
141 }
142 catch (SocketException exception)
143 {
144 Console.Error.WriteLine(exception);
145 }
146 catch (ThreadAbortException) {}
147 finally
148 {
149 listener.Stop();
150 }
151 }
152
153 private void Do(object socket_)
154 {
155 Socket socket = (Socket)socket_;
156 NetworkStream stream = new NetworkStream(socket);
157
158 try
159 {
160 byte[] buffer = new byte[1024];
161 int size = 0, byte_;
162
163 while (size != buffer.Length && (byte_ = stream.ReadByte()) != '\r' && byte_ != '\n' && byte_ != -1)
164 buffer[size++] = (byte)byte_;
165
166 string line = Encoding.ASCII.GetString(buffer, 0, size);
167
168 Console.WriteLine("{0} [{1}]", socket.RemoteEndPoint, line);
169
170 List<string> names = new List<string>();
171 bool full = false;
172
173 foreach (string name in line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
174 {
175 if (name.ToUpper() == "/W")
176 full = true;
177 else
178 names.Add(name);
179 }
180
181 StreamWriter writer = new StreamWriter(stream, new UTF8Encoding(false));
182
183 Finger finger = names.Count > 0 ? new Finger(writer, names) : new Finger(writer, full);
184
185 writer.Flush();
186 }
187 catch (IOException) {}
188
189 socket.Close(60);
190 }
191
192 public static int Main(string[] args)
193 {
194 ServiceBase.Run(new ServiceBase[] { new CCSFinger() });
195
196 return 0;
197 }
198 }