ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/CCSFinger/CCSFinger.cs
Revision: 725
Committed: 2006-03-31T04:13:36-08:00 (19 years, 3 months ago) by douglas
File size: 3841 byte(s)
Log Message:
Service Worky!

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 = "SetServiceStatus")]
50 public static extern bool SetServiceStatus(IntPtr hServiceStatus, SERVICE_STATUS lpServiceStatus);
51
52 [DllImport("Advapi32.dll", EntryPoint = "QueryServiceStatus")]
53 public static extern bool QueryServiceStatus(IntPtr hService, out SERVICE_STATUS lpServiceStatus);
54
55 protected override void OnStart(string[] args)
56 {
57 SERVICE_STATUS status;
58
59 QueryServiceStatus(ServiceHandle, out status);
60
61 status.dwCurrentState = CurrentState.SERVICE_START_PENDING;
62
63 SetServiceStatus(ServiceHandle, status);
64
65 thread = new Thread(new ThreadStart(Do));
66
67 thread.Start();
68
69 ExitCode = 0;
70
71 QueryServiceStatus(ServiceHandle, out status);
72
73 status.dwCurrentState = CurrentState.SERVICE_RUNNING;
74
75 SetServiceStatus(ServiceHandle, status);
76 }
77
78 protected override void OnStop()
79 {
80 SERVICE_STATUS status;
81
82 QueryServiceStatus(ServiceHandle, out status);
83
84 status.dwCurrentState = CurrentState.SERVICE_STOP_PENDING;
85
86 thread.Abort();
87 thread.Join();
88
89 ExitCode = 0;
90
91 QueryServiceStatus(ServiceHandle, out status);
92
93 status.dwCurrentState = CurrentState.SERVICE_STOPPED;
94 }
95
96 private void Do()
97 {
98 TcpListener listener = null;
99
100 try
101 {
102 listener = new TcpListener(IPAddress.Any, 79);
103
104 listener.Start();
105
106 while (true)
107 {
108 Socket socket = listener.AcceptSocket();
109
110 socket.ReceiveTimeout = 60000;
111
112 Thread thread = new Thread(new ParameterizedThreadStart(Do));
113
114 thread.Start(socket);
115 }
116 }
117 catch (SocketException exception)
118 {
119 Console.Error.WriteLine(exception);
120 }
121 catch (ThreadAbortException) {}
122 finally
123 {
124 listener.Stop();
125 }
126 }
127
128 private void Do(object socket_)
129 {
130 Socket socket = (Socket)socket_;
131 NetworkStream stream = new NetworkStream(socket);
132
133 try
134 {
135 byte[] buffer = new byte[1024];
136 int size = 0, byte_;
137
138 while (size != buffer.Length && (byte_ = stream.ReadByte()) != '\r' && byte_ != '\n' && byte_ != -1)
139 buffer[size++] = (byte)byte_;
140
141 string line = Encoding.ASCII.GetString(buffer, 0, size);
142
143 Console.WriteLine("{0} [{1}]", socket.RemoteEndPoint, line);
144
145 List<string> names = new List<string>();
146 bool full = false;
147
148 foreach (string name in line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
149 {
150 if (name.ToUpper() == "/W")
151 full = true;
152 else
153 names.Add(name);
154 }
155
156 StreamWriter writer = new StreamWriter(stream, new UTF8Encoding(false));
157
158 Finger finger = names.Count > 0 ? new Finger(writer, names) : new Finger(writer, full);
159
160 writer.Flush();
161 }
162 catch (IOException) {}
163
164 socket.Close(60);
165 }
166
167 public static int Main(string[] args)
168 {
169 ServiceBase.Run(new ServiceBase[] { new CCSFinger() });
170
171 return 0;
172 }
173 }