Author Topic: WSA and Threading In Windows  (Read 5449 times)

Offline RagingGrim

  • Jr. Member
  • *
  • Posts: 28
WSA and Threading In Windows
« on: January 26, 2015, 01:51:37 PM »
So in my previous posts I mentioned a expo for which I will be entering.

Quick Recap :p
Basically it's just a server and client written in nasm using the WSA , the clients should monitor a few things on their computers
( eg if the user is present  , running processes etc etc ) . I have to finish this before June...Julyish? I doubt it'll be that hard to finish by then but I am in my matric year so I'm kind of piled up with work right now.

I was wondering if I'm on the right track thusfar.

Code: [Select]
extern _WSAStartup
extern _socket
extern _bind
extern _listen
extern _HeapReAlloc
extern _GetProcessHeap
extern _HeapAlloc
extern _HeapFree
extern _accept
extern _recv
extern _ExitProcess
extern __cprintf
extern _CreateThread
extern _bind
extern _ExitWindowsEx
extern _WSAGetLastError
extern __getch
extern _htons
global main
section .bss
WSADATA resb 400 ;Checked in C
sockaddr_inA resb 16 ;Checked in C
rcvBuffer resb 200
clientSockets resd 10
section .data
errFormat db "ERRORCODE : %d",0ah,0
counterGeneral dd 0
counterSocket dd 0
ProcessHeap dd 0
Counter dd 0
socketServer dd 0
intFormat db "%d",0ah,0
strFormat db "%s",0
shutdownCMD db "Shutdown",0ah,0
strShuttingDown db "Shutting Down",0
strClientFormat db "Client (%d) Connected!",0ah,0

section .text
call _GetProcessHeap
mov dword [ProcessHeap],eax
;1.) Init WSA
push dword WSADATA
push 514 ;MakeWord(2,2)
call _WSAStartup
CMP eax,byte 0
JNE lblErrExit

push 0
push 0
push dword [ProcessHeap]
call _HeapAlloc
CMP eax,0
JE lblErrExit
mov dword [clientSockets],eax
;2.) Create A Socket
push 6 ; TCP
push 1 ; SOCK_STREAM
push 2 ; AF_INET
call _socket
cmp eax,-1 ;SOCK_ERROR
JE lblErrExit
mov dword [socketServer],eax

;3.) Bind The Socket
mov word [sockaddr_inA+0],2 ;AF_INET
push 968
call _htons
mov dword [sockaddr_inA+2],eax
mov dword [sockaddr_inA+6],0b0 ;ALL IP's

push 16
push sockaddr_inA
push dword [socketServer]
call _bind
CMP eax,0
JNE lblErrExit

;4.) Listen For A Connection

push 0
push dword [socketServer]
call _listen
CMP eax,0
JNE lblErrExit

;5.1) Spawn A Thread
;5.2.) Accept A Connection
push 0
push 0
push 0
push lblAccept
push 9
push 0
call _CreateThread

;5.3) Create a dynamic array for clientSockets
;LPVOID WINAPI HeapAlloc(  _In_  HANDLE hHeap,  _In_  DWORD dwFlags,  _In_  SIZE_T dwBytes


push 0
push 0
push dword [socketServer]
call _accept
cmp eax,-1
JE lblErrExit

mov ebx,eax
push eax
push strClientFormat
call __cprintf
add esp,8

add dword [Counter],1
mov edx,dword [Counter]
mov dword [clientSockets+edx],ebx ;Save Client Handle EDIT

push 0
push 0
push ebx
push lblRecv
push 9
push 0
call _CreateThread
JMP lblAccept

;6.) Recv data
;client handle ebp + 8

push 0
push 255
push rcvBuffer
push dword [ebp+8]
call _recv

push rcvBuffer
push strFormat
call __cprintf
add esp,8

push rcvBuffer
push shutdownCMD
call strcmp
cmp eax,1
JE lblShutdown
mov dword [rcvBuffer],0
JMP lblRecv_loop

call _WSAGetLastError
push eax
push errFormat
call __cprintf
add esp , 8
call __getch
call _ExitProcess

mov eax,0
inc eax
CMP byte [eax + ebx],0
JNE strlen_loop

mov ebx,[esp+4]
mov ecx,[esp+8]
mov edx,1
mov al,byte [ebx]
mov ah,byte [ecx]
cmp ah,al
jne lblstrcmp_err
cmp ah,0
je lblstrcmp_done
inc ebx
inc ecx
jmp lblstrcmp_loop
mov edx,0
mov eax,edx

push strShuttingDown
push strFormat
call __cprintf
push 0
push 0
call _ExitWindowsEx

You'll notice I don't call any of the heap functions because I don't intend on using them for the prototype. I suppose I'll be rewriting this program quite a few times before submitting it . My competition is a guy from my class who will be writing a green screen program in python. I'm not sure but I think he's capable of pulling it off ( he has won quite a few awards for IT the last few years ). 

Once again this is just a  prototype , I only want to know if I'm doing the basic things correctly ^^

Also I'd like to add that I'm binding the socket to all available ip addresses . At first I was able to connect to the server from my phone via a netcat application for android ( SimpleNetcat ) but now I can't do that anymore. However when using netcat on my computer to connect to the ipv4 address of my computer ( not localhost ) it works just fine. Any possible reason for why it suddenly stopped working ? ^^ I should probably try to disable my antivirus.

Also if I send more than one string ( depending on what I send ) it seems like something is going wrong with the __cprintf function ( obviously it's my mistake though ) . Am I clearing the string correctly after receiving? 

Offline RagingGrim

  • Jr. Member
  • *
  • Posts: 28
Re: WSA and Threading In Windows
« Reply #1 on: February 11, 2015, 03:34:38 PM »
Ill just answer this myself.

I couldnt connect to the server because i forgot my ip wasnt assigned statically. The error with the string was trivial. The SimpleCat application sends EVERY recorded character including the new line char. I was in effect comparing String\0 to String\n\0

Im still battling on whether to finish this in asm or C though :/