News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Check if a local IP is Used

Started by Farabi, October 07, 2010, 01:15:17 AM

Previous topic - Next topic

Farabi

Im maintaining a wireless access point, and make my laptop as a data source.
I want to check every computer entered my access point and I can do it by checking the IP number one by one. But the problem is, when an IP number is not used by any computer, the process wait for 20 second and more and it caused my laptop to wait until the time limit is reached, how to set the time limit to for example 2 second? or maybe do a polling each 5 second for each IP number

here is the code



fInetData struct
nSize dword 0
lpData dword 0
fInetData ends
MIB_TCPROW STRUCT
  dwState DWORD ?
  dwLocalAddr DWORD ?
  dwLocalPort DWORD ?
  dwRemoteAddr DWORD ?
  dwRemotePort DWORD ?
MIB_TCPROW ENDS

_MIB_IPADDRROW struct
            dwAddr dword 0;
            dwIndex dword 0;
            dwMask dword 0;
            dwBCastAddr dword 0;
            dwReasmSized dword 0;
  unused1 word 0;
  wType word 0;
_MIB_IPADDRROW ends

_MIB_IPADDRTABLE struct
    dwNumEntries dword 0
  table dword 0
_MIB_IPADDRTABLE ends

fInet struct
sock dword 0
lpszurl dword 0
fInet ends

TIMEVAL struct
sec dd 0
usec dd 0
TIMEVAL ENDS

WM_FSOCKET equ WM_USER + 0fh

fCheckConnection proc uses esi edi lps:dword,lpszHostName:dword,nPort:dword,nmsWaitDelay:dword
LOCAL a:sockaddr_in
LOCAL wsadata:WSADATA
LOCAL attempt:dword
LOCAL tv:TIMEVAL
; invoke WSAStartup,202h,addr wsadata
; .if eax!=0
; xor eax,eax
; dec eax
; ret
; .endif

assume esi:ptr fInet
mov esi,lps
invoke socket,AF_INET,SOCK_STREAM,0     ; Create a stream socket for internet use
.if eax!=INVALID_SOCKET

    mov [esi].sock,eax
;invoke WSAAsyncSelect,[esi].sock,hWnd,WM_FSOCKET,FD_CONNECT+FD_READ+FD_CLOSE+FD_WRITE+FD_ACCEPT
.else
invoke MessageBox,0,CADD("Internet connection initialization error"),CADD("Unknown cause"),MB_OK
.endif

push lpszHostName
pop [esi].lpszurl

mov a.sin_family, AF_INET
invoke htons, nPort
mov a.sin_port,ax
invoke gethostbyname, lpszHostName
.if eax==0
invoke PERR
ret
.endif
mov eax,[eax+12]

mov eax,[eax]                      ; copy the pointer to the actual IP address into eax
mov eax,[eax]                      ; copy IP address into eax
mov a.sin_addr,eax

xor ecx,ecx
mov attempt,ecx
loop_s:
; mov tv.sec,1
; mov tv.usec,1
; invoke setsockopt, [esi].sock, SOL_SOCKET, SO_RCVTIMEO , ADDR tv, sizeof tv
invoke connect,[esi].sock,addr a,sizeof a
; push eax
; invoke Sleep,nmsWaitDelay
; pop eax

; invoke send,[esi].sock,CADD("Test"),4,0
; invoke connect,[esi].sock,addr a,sizeof a
push eax
;invoke MessageBox,0,[esi].lpszurl,0,0
pop eax
assume esi:nothing

ret
fCheckConnection endp

fScanEach proc uses esi edi hWnd:dword,uMsg:dword,wParam:dword,lParam:dword
LOCAL buff[256]:dword
LOCAL buff2[256]:dword
LOCAL b:fInet
LOCAL adcnt:dword

invoke fInitInternet,0
invoke memfill,addr buff,1024,0
invoke memfill,addr buff2,1024,0

invoke lstrcat,addr buff,CADD("192.168.1.")
invoke dw2a,ip_num,addr buff2
invoke lstrcat,addr buff,addr buff2

invoke fCheckConnection,addr b,addr buff,139,50
.if eax==0
xor edx,edx
mov ecx,add_table
mov eax,ip_num
mov dword ptr [ecx+eax*4],1
invoke closesocket,b.sock
invoke MessageBox,0,addr buff,0,0
.endif

; .if ip_num==9
; invoke MessageBox,0,CADD("Done"),0,0
; .endif

invoke WSACleanup

ret
fScanEach endp

ScanComputer proc uses esi edi hWnd:dword,uMsg:dword,wParam:dword,lParam:dword
LOCAL buff[256]:dword
LOCAL buff2[256]:dword
LOCAL adcnt,off_data:dword

invoke fInitInternet,hwnd

; invoke GetLocalAddress,addr local_addr
; invoke MessageBox,0,addr local_addr,0,0
invoke mAlloc,256*4
mov add_table,eax

xor ecx,ecx
mov ip_num,ecx
loop_check:
push ecx
invoke fNewThread,fScanEach
invoke Sleep,100
inc ip_num
pop ecx
inc ecx
cmp ecx,10
jl loop_check

invoke WSACleanup

ret
ScanComputer endp

fNewThread proc lpThreadName:dword
LOCAL ThreadID:dword

mov  edx,lpThreadName
invoke CreateThread,NULL,NULL,edx,\
                                        NULL,NORMAL_PRIORITY_CLASS,\
                                        ADDR ThreadID
invoke CloseHandle,eax
ret
fNewThread endp


Here is how I used it

invoke fNewThread,addr ScanComputer

Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

qWord

hi,

what about using waitable timers an WaitForMultipleObject?

qWord
FPU in a trice: SmplMath
It's that simple!

Farabi

Quote from: qWord on October 07, 2010, 06:45:41 AM
hi,

what about using waitable timers an WaitForMultipleObject?

qWord

I dont know how to use that, can you give an example for that?
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"