Archive: Test connection to specific address/port


Test connection to specific address/port
  Hello all,

I am creating an installer with NSIS, and I would like to validate the SMTP server name user enters by connecting to port 25 of this server. This is fully automatic and gives some guarantee that the SMTP server is entered correctly. Is there any way to try to connect to specific IP address/port from NSIS?


There is IPDll and you can try to use it. But I don't know about working with specific port. Try.


As far as I understand, IPDll is a thing to test IP addresses statically, just basing on some fixed rules, not trying to connect physically. But maybe I am wrong.


Working with sockets
  Hello
This function connect a socket to a ServerIP and Port and disconect this.
Useful if you want to know if a server service is running in a port.
Tested with windowsXp and Win98.


push "ServerIP"
push "PortToConect"
Call SocketToServerAndPort
pop $R0

At this moment $R0 is 0 (conect to socket OK) or -1 (conect to socket not OK)
note : Socket Version Dll required 2.2 . If you want to change the version, change on WSAStartup function.

Examples

Section "mysql" mysql
push "127.0.0.1"
push "3306"
Call SocketToServerAndPort
pop $R0
messagebox mb_ok "Mysql : return $R0"
SectionEnd

Section "web" web
push "127.0.0.1"
push "80"
Call SocketToServerAndPort
pop $R0
messagebox mb_ok "Web : return $R0"
SectionEnd

Section "smtp" smtp
push "127.0.0.1"
push "25"
Call SocketToServerAndPort
pop $R0
messagebox mb_ok "Smtp : return $R0"
SectionEnd


SocketToServerAndPort


Exch$0 ; PortToConect
Exch
Exch$1 ; ServeIP
Push$2 ; address struct wsadata
Push$3 ; WSAStartup return
>Push $4 ; socket return
>Push $5 ; ntohs return
>Push $6 ; inet_addr
Push$7 ; addres struct sockaddr_in
Push$8 ; connect return ¡¡
push$9 ; WSACleanup return
>push $R0 ;
>push $R1 ; Temp
push $R2; "
push $R3 ; wVersion
push $R4 ; szDescription
push $R5 ; wHighVersion
push $R6 ; szSystemStatus
push $R7 ; iMaxSockets
push $R8 ; iMaxUdpDg
push $R9 ; lpVendorInfo


!define struct_wsadata '(&i2,&i2,&t257,&t129,i,i,l)i'
System::Call '*${struct_wsadata} .r2' ; allocates memory for wsadata struct and writes address to $2
; 514 =2 + 256*2 = MAKEWORD (bLow,bHigh) = bLow + 256 * bHigh ==> version socket
; note : Socket Version Dll required 2.2 or higher
System::Call 'ws2_32::WSAStartup(i 514 ,i r2)i .r3'
IntCmp 0 $3 0 DllNotOK DllNotOK
System::Call '*$2${struct_wsadata}(.R3,.R4,.R5,.R6,.R7,.R8,.R9)'
IntOp $R3 $R3 / 256
IntOp $R1 $3 % 256
IntOp $R4 $R4 % 256
IntOp $R2 $R4 % 256
; From winsock.h :
; AF_INET = 2
; IPPROTO_TCP = 6
; SOCK_STREAM = 1
System::Call 'ws2_32::socket(i 2,i 1,i 6)i .r4'

System::Call 'ws2_32::ntohs(i $0) i.r5'
System::Call 'ws2_32::inet_addr(t "
$1") i.r6'

!define struct_sockaddr_in '(&i2,&i2,&i4,&t8)i'
System::Call '*${struct_sockaddr_in} .r7' ; allocates memory for sockaddr_in struct and writes address to $7
System::Call '*$7${struct_sockaddr_in}(2,r5,r6,)' ;

System::Call 'ws2_32::connect(i $4,i $7,i 16) i.r8'

System::Call 'ws2_32::WSACleanup()i .r9 '

;DetailPrint "***********************"
;DetailPrint "Port = $0"
;DetailPrint "Server = $1"
;DetailPrint "Version = $R3.$R1"
;DetailPrint "HighVersion = $R4.$R2"
;DetailPrint "Description = $R5"
;DetailPrint "SystemStatus = $R6"
;DetailPrint "MaxSockets = $R7"
;DetailPrint "MaxUdpDg= $R8"
;DetailPrint "VendorInfo = $R9"
;DetailPrint "Connect return $8"
;DetailPrint "***********************"

System::Free $7
System::Free $2

Goto done

DllNotOK:
MessageBox mb_ok "The WinSock DLL is unacceptable"
Goto done

done:

Pop $R9
Pop $R8
Pop $R7
Pop $R6
Pop $R5
Pop $R4
Pop $R3
Pop $R2
Pop $R1
Pop $R0
Pop $9
Pop $7
Pop $6
Pop $5
Pop $4
Pop $3
Pop $2
Pop $1
Pop $0
Exch $8 ;Stack = 0 (conect to socket OK) or -1 (conect to socket not OK)

FunctionEnd
>

Thanks a lot, that seems to be the one I need, will try that.