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

# User Rev Content
1 douglas 719 // Douglas Thrift
2 douglas 710 //
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 douglas 725 using System.Runtime.InteropServices;
13 douglas 710 using System.ServiceProcess;
14     using System.Text;
15     using System.Threading;
16    
17     public class CCSFinger : ServiceBase
18     {
19 douglas 725 public enum CurrentState : uint
20 douglas 710 {
21 douglas 725 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 douglas 710
30 douglas 725 [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 douglas 710
42 douglas 725 private Thread thread;
43 douglas 718
44 douglas 725 public CCSFinger()
45     {
46     ServiceName = "CCSFinger";
47     }
48 douglas 718
49 douglas 727 [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 douglas 725 [DllImport("Advapi32.dll", EntryPoint = "SetServiceStatus")]
65 douglas 727 public static extern bool SetServiceStatus(IntPtr hServiceStatus, IntPtr lpServiceStatus);
66 douglas 710
67 douglas 727 public static bool SetServiceStatus(IntPtr hServiceStatus, SERVICE_STATUS lpServiceStatus)
68     {
69     IntPtr pointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SERVICE_STATUS)));
70 douglas 710
71 douglas 727 Marshal.StructureToPtr(lpServiceStatus, pointer, false);
72    
73     bool value = SetServiceStatus(hServiceStatus, pointer);
74    
75     Marshal.FreeHGlobal(pointer);
76    
77     return value;
78     }
79    
80 douglas 725 protected override void OnStart(string[] args)
81     {
82     SERVICE_STATUS status;
83 douglas 710
84 douglas 725 QueryServiceStatus(ServiceHandle, out status);
85 douglas 710
86 douglas 725 status.dwCurrentState = CurrentState.SERVICE_START_PENDING;
87 douglas 718
88 douglas 725 SetServiceStatus(ServiceHandle, status);
89 douglas 718
90 douglas 725 thread = new Thread(new ThreadStart(Do));
91 douglas 710
92 douglas 725 thread.Start();
93 douglas 718
94 douglas 725 ExitCode = 0;
95 douglas 710
96 douglas 725 QueryServiceStatus(ServiceHandle, out status);
97 douglas 710
98 douglas 725 status.dwCurrentState = CurrentState.SERVICE_RUNNING;
99 douglas 721
100 douglas 725 SetServiceStatus(ServiceHandle, status);
101     }
102 douglas 718
103 douglas 725 protected override void OnStop()
104     {
105     SERVICE_STATUS status;
106 douglas 710
107 douglas 725 QueryServiceStatus(ServiceHandle, out status);
108 douglas 710
109 douglas 725 status.dwCurrentState = CurrentState.SERVICE_STOP_PENDING;
110 douglas 710
111 douglas 725 thread.Abort();
112     thread.Join();
113 douglas 710
114 douglas 725 ExitCode = 0;
115 douglas 718
116 douglas 725 QueryServiceStatus(ServiceHandle, out status);
117 douglas 720
118 douglas 725 status.dwCurrentState = CurrentState.SERVICE_STOPPED;
119 douglas 710 }
120    
121 douglas 725 private void Do()
122 douglas 710 {
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 douglas 722 Socket socket = listener.AcceptSocket();
134 douglas 721
135 douglas 722 socket.ReceiveTimeout = 60000;
136 douglas 721
137 douglas 710 Thread thread = new Thread(new ParameterizedThreadStart(Do));
138    
139 douglas 722 thread.Start(socket);
140 douglas 710 }
141     }
142     catch (SocketException exception)
143     {
144     Console.Error.WriteLine(exception);
145     }
146 douglas 725 catch (ThreadAbortException) {}
147 douglas 710 finally
148     {
149     listener.Stop();
150     }
151 douglas 720 }
152 douglas 710
153 douglas 725 private void Do(object socket_)
154 douglas 710 {
155 douglas 722 Socket socket = (Socket)socket_;
156     NetworkStream stream = new NetworkStream(socket);
157 douglas 710
158 douglas 722 try
159     {
160     byte[] buffer = new byte[1024];
161     int size = 0, byte_;
162 douglas 710
163 douglas 722 while (size != buffer.Length && (byte_ = stream.ReadByte()) != '\r' && byte_ != '\n' && byte_ != -1)
164     buffer[size++] = (byte)byte_;
165 douglas 710
166 douglas 722 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 douglas 710 }
187 douglas 722 catch (IOException) {}
188 douglas 710
189 douglas 722 socket.Close(60);
190 douglas 710 }
191    
192     public static int Main(string[] args)
193     {
194 douglas 725 ServiceBase.Run(new ServiceBase[] { new CCSFinger() });
195 douglas 710
196     return 0;
197     }
198     }