Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.26 KB | None | 0 0
  1. Option Explicit
  2.  
  3. Private Const SIO_RCVALL = &H98000001
  4. Private Const SO_RCVTIMEO = &H1006
  5. Private Const AF_INET = 2
  6. Private Const INVALID_SOCKET = -1
  7. Public Const FD_READ = &H1&
  8. Private Const SOCK_STREAM = 1
  9. Private Const SOCK_RAW = 3
  10. Private Const IPPROTO_IP = 0
  11. Private Const WSA_DESCRIPTIONLEN = 256
  12. Private Const WSA_DescriptionSize = WSA_DESCRIPTIONLEN + 1
  13. Private Const WSA_SYS_STATUS_LEN = 128
  14. Private Const WSA_SysStatusSize = WSA_SYS_STATUS_LEN + 1
  15. Private Const INADDR_NONE = &HFFFF
  16. Private Const SOL_SOCKET = &HFFFF&
  17. Private Const hostent_size = 16
  18.  
  19. Private Type OSVERSIONINFO
  20. dwOSVersionInfoSize As Long
  21. dwMajorVersion As Long
  22. dwMinorVersion As Long
  23. dwBuildNumber As Long
  24. dwPlatformID As Long
  25. szCSDVersion As String * 128
  26. End Type
  27. Private Type WSADataType
  28. wVersion As Integer
  29. wHighVersion As Integer
  30. szDescription As String * WSA_DescriptionSize
  31. szSystemStatus As String * WSA_SysStatusSize
  32. iMaxSockets As Integer
  33. iMaxUdpDg As Integer
  34. lpVendorInfo As Long
  35. End Type
  36. Private Type HostEnt
  37. h_name As Long
  38. h_aliases As Long
  39. h_addrtype As Integer
  40. h_length As Integer
  41. h_addr_list As Long
  42. End Type
  43. Private Type sockaddr
  44. sin_family As Integer
  45. sin_port As Integer
  46. sin_addr As Long
  47. sin_zero As String * 8
  48. End Type
  49.  
  50. Public Type ipheader
  51. ip_verlen As Byte
  52. ip_tos As Byte
  53. ip_totallength As Integer
  54. ip_id As Integer
  55. ip_offset As Integer
  56. ip_ttl As Byte
  57. ip_protocol As Byte
  58. ip_checksum As Integer
  59. ip_srcaddr As Long
  60. ip_destaddr As Long
  61. End Type
  62.  
  63. Public Type tcpheader
  64. src_portno As Integer
  65. dst_portno As Integer
  66. Sequenceno As Long
  67. Acknowledgeno As Long
  68. DataOffset As Byte
  69. flag As Byte
  70. Windows As Integer
  71. checksum As Integer
  72. UrgentPointer As Integer
  73. End Type
  74.  
  75.  
  76. Public Type udpheader
  77. src_portno As Integer
  78. dst_portno As Integer
  79. udp_length As Integer
  80. udp_checksum As Integer
  81. End Type
  82.  
  83. Private Const SIO_GET_INTERFACE_LIST = &H4004747F
  84.  
  85. Private Type sockaddr_gen
  86. AddressIn As sockaddr
  87. filler(0 To 7) As Byte
  88. End Type
  89.  
  90. Private Type INTERFACE_INFO
  91. iiFlags As Long ' Interface flags
  92. iiAddress As sockaddr_gen ' Interface address
  93. iiBroadcastAddress As sockaddr_gen ' Broadcast address
  94. iiNetmask As sockaddr_gen ' Network mask
  95. End Type
  96. Private Type aINTERFACE_INFO
  97. interfaceinfo(0 To 7) As INTERFACE_INFO
  98. End Type
  99.  
  100. Private Declare Function bind Lib "WSOCK32.DLL" (ByVal s As Integer, addr As sockaddr, ByVal namelen As Integer) As Integer
  101. Private Declare Function setsockopt Lib "WSOCK32.DLL" (ByVal s As Long, ByVal Level As Long, ByVal optname As Long, optval As Any, ByVal optlen As Long) As Long
  102. Private Declare Function WSAIsBlocking Lib "WSOCK32.DLL" () As Long
  103. Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
  104. Public Declare Function recv Lib "WSOCK32.DLL" (ByVal s As Long, Buf As Any, ByVal buflen As Long, ByVal flags As Long) As Long
  105. Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVR As Long, lpWSAD As WSADataType) As Long
  106. Private Declare Function htons Lib "WSOCK32.DLL" (ByVal hostshort As Long) As Integer
  107. Public Declare Function ntohs Lib "WSOCK32.DLL" (ByVal netshort As Long) As Integer
  108. Private Declare Function socket Lib "WSOCK32.DLL" (ByVal af As Long, ByVal s_type As Long, ByVal protocol As Long) As Long
  109. Public Declare Function closesocket Lib "WSOCK32.DLL" (ByVal s As Long) As Long
  110. Public Declare Function WSAAsyncSelect Lib "WSOCK32.DLL" (ByVal s As Long, ByVal hWnd As Long, ByVal wMsg As Long, ByVal lEvent As Long) As Long
  111. Private Declare Function inet_addr Lib "WSOCK32.DLL" (ByVal cp As String) As Long
  112. Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal host_name As String) As Long
  113. Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal hostname As String, ByVal HostLen As Long) As Long
  114. Private Declare Function inet_ntoa Lib "WSOCK32.DLL" (ByVal inn As Long) As Long
  115. Private Declare Function WSACancelBlockingCall Lib "WSOCK32.DLL" () As Long
  116. Private Declare Function WSAIoctl Lib "ws2_32.dll" (ByVal s As Long, ByVal dwIoControlCode As Long, lpvInBuffer As Any, ByVal cbInBuffer As Long, lpvOutBuffer As Any, ByVal cbOutBuffer As Long, lpcbBytesReturned As Long, lpOverlapped As Long, lpCompletionRoutine As Long) As Long
  117. Public Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
  118. Private Declare Function WSAGetLastError Lib "WSOCK32" () As Long
  119. Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Any) As Long
  120. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef hpvDest As Any, ByRef hpvSource As Any, ByVal cbCopy As Long)
  121. Private Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As Any, ByVal hpvSource&, ByVal cbCopy&)
  122. Private saZero As sockaddr
  123. Private WSAStartedUp As Boolean
  124. Public lSocket As Long
  125.  
  126.  
  127. Public Function StartWinsock() As Boolean
  128. Dim StartupData As WSADataType
  129. If Not WSAStartedUp Then
  130. If Not WSAStartup(&H101, StartupData) Then
  131. WSAStartedUp = True
  132. Else
  133. WSAStartedUp = False
  134. End If
  135. End If
  136. StartWinsock = WSAStartedUp
  137. End Function
  138.  
  139. Public Sub EndWinsock()
  140. If WSAIsBlocking Then Call WSACancelBlockingCall
  141. WSACleanup
  142. WSAStartedUp = False
  143. End Sub
  144.  
  145. Public Function ConnectSock(ByVal host As String, ByVal Port As Long, ByVal HWndToMsg As Long) As Long ', ByVal Async As Integer) As Long
  146.  
  147. Dim SockIn As sockaddr
  148. Dim lngInBuffer As Long, _
  149. lngBytesReturned As Long, _
  150. lngOutBuffer As Long, _
  151. s As Long, _
  152. SelectOps As Long, _
  153. RCVTIMEO As Long
  154.  
  155. SockIn = saZero
  156. SockIn.sin_family = AF_INET
  157. SockIn.sin_port = htons(Port)
  158. If SockIn.sin_port = INVALID_SOCKET Then
  159. ConnectSock = INVALID_SOCKET
  160. MsgBox "INVALID_SOCKET"
  161. Exit Function
  162. End If
  163.  
  164. SockIn.sin_addr = GetHostByNameAlias(host$)
  165.  
  166. If SockIn.sin_addr = INADDR_NONE Then
  167. ConnectSock = INVALID_SOCKET
  168. MsgBox "INVALID_SOCKET"
  169. Exit Function
  170. End If
  171.  
  172.  
  173. s = socket(AF_INET, SOCK_RAW, IPPROTO_IP)
  174. If s < 0 Then
  175. ConnectSock = INVALID_SOCKET
  176. MsgBox "INVALID_SOCKET"
  177. Exit Function
  178. End If
  179.  
  180.  
  181. RCVTIMEO = 5000
  182. If setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (RCVTIMEO), 4) <> 0 Then
  183. MsgBox "setsockopt err"
  184. If s > 0 Then closesocket (s)
  185. Exit Function
  186. End If
  187.  
  188. If bind(s, SockIn, Len(SockIn)) <> 0 Then
  189. If s > 0 Then closesocket (s)
  190. MsgBox "bind err"
  191. Exit Function
  192. End If
  193.  
  194.  
  195. lngInBuffer = 1
  196. If WSAIoctl(s, SIO_RCVALL, lngInBuffer, Len(lngInBuffer), _
  197. lngOutBuffer, Len(lngOutBuffer), _
  198. lngBytesReturned, ByVal 0, ByVal 0) <> 0 Then
  199. If s > 0 Then closesocket (s)
  200. MsgBox "WSAIoctl err"
  201. Exit Function
  202. End If
  203.  
  204. SelectOps = FD_READ 'Or FD_WRITE Or FD_CONNECT Or FD_CLOSE
  205. If WSAAsyncSelect(s, HWndToMsg, WINSOCKMSG, ByVal SelectOps) <> 0 Then
  206. If s > 0 Then closesocket (s)
  207. ConnectSock = INVALID_SOCKET
  208. MsgBox "INVALID_SOCKET"
  209. Exit Function
  210. End If
  211.  
  212. ConnectSock = s
  213. End Function
  214.  
  215. Private Function GetHostByNameAlias(ByVal hostname$) As Long
  216. On Error Resume Next
  217. Dim phe As Long
  218. Dim heDestHost As HostEnt
  219. Dim addrList As Long
  220. Dim retIP As Long
  221. retIP = inet_addr(hostname)
  222. If retIP = INADDR_NONE Then
  223. phe = gethostbyname(hostname)
  224. If phe <> 0 Then
  225. CopyMemory heDestHost, ByVal phe, hostent_size
  226. CopyMemory addrList, ByVal heDestHost.h_addr_list, 4
  227. CopyMemory retIP, ByVal addrList, heDestHost.h_length
  228. Else
  229. retIP = INADDR_NONE
  230. End If
  231. End If
  232. GetHostByNameAlias = retIP
  233. If Err Then GetHostByNameAlias = INADDR_NONE
  234. End Function
  235.  
  236. Public Function GetAscIp(ByVal inn As Long) As String
  237. On Error Resume Next
  238. Dim lpStr As Long
  239. Dim nStr As Long
  240. Dim retString As String
  241. retString = String$(32, 0)
  242. lpStr = inet_ntoa(inn)
  243. If lpStr = 0 Then
  244. GetAscIp = "255.255.255.255"
  245. Exit Function
  246. End If
  247. nStr = lstrlen(lpStr)
  248. If nStr > 32 Then nStr = 32
  249. CopyMemory ByVal retString, ByVal lpStr, nStr
  250. retString = Left$(retString, nStr)
  251. GetAscIp = retString
  252. If Err Then GetAscIp = "255.255.255.255"
  253. End Function
  254.  
  255.  
  256.  
  257. Public Function wsck_enum_interfaces(ByRef str() As String) As Long
  258. Dim lngBytesReturned As Long
  259. Dim NumInterfaces As Integer
  260. Dim i As Integer
  261. Dim desc As String
  262. Dim buffer As aINTERFACE_INFO
  263. Dim lngSocketDescriptor As Long
  264. Call StartWinsock
  265. lngSocketDescriptor = socket(AF_INET, SOCK_STREAM, 0)
  266. If lngSocketDescriptor = 0 Then
  267. wsck_enum_interfaces = Err.LastDllError
  268. Exit Function
  269. End If
  270. If WSAIoctl(lngSocketDescriptor, _
  271. SIO_GET_INTERFACE_LIST, ByVal 0, ByVal 0, _
  272. buffer, 1024, lngBytesReturned, ByVal 0, ByVal 0) Then
  273. wsck_enum_interfaces = Err.LastDllError
  274. Exit Function
  275. End If
  276. NumInterfaces = CInt(lngBytesReturned / 76)
  277. ReDim str(NumInterfaces - 1)
  278. For i = 0 To NumInterfaces - 1
  279. str(i) = GetAscIp(buffer.interfaceinfo(i).iiAddress.AddressIn.sin_addr) & ";" & _
  280. GetAscIp(buffer.interfaceinfo(i).iiNetmask.AddressIn.sin_addr)
  281. Next i
  282. Call closesocket(lngSocketDescriptor)
  283. End Function
  284.  
  285. Public Function IsWindowsNT5() As Boolean
  286. Dim typOSInfo As OSVERSIONINFO
  287. typOSInfo.dwOSVersionInfoSize = Len(typOSInfo)
  288. Call GetVersionEx(typOSInfo)
  289. If typOSInfo.dwMajorVersion >= 5 Then IsWindowsNT5 = True
  290. End Function
  291.  
  292.  
  293.  
  294. Public Function HostByName(name As String) As String
  295. Dim hostname As String * 256
  296. Dim hostent_addr As Long
  297. Dim host As HostEnt
  298. Dim hostip_addr As Long
  299. Dim temp_ip_address() As Byte
  300. Dim i As Integer
  301. Dim ip_address As String
  302.  
  303. If gethostname(hostname, 256) = -1 Then
  304. MsgBox "Windows Sockets error " & WSAGetLastError()
  305.  
  306. GetHostByNameAlias name
  307. Exit Function
  308. Else
  309. hostname = Trim$(hostname)
  310. End If
  311. If Len(name) > 0 Then Mid(hostname, 1, Len(name)) = name
  312. hostent_addr = gethostbyname(hostname)
  313.  
  314. If hostent_addr = 0 Then
  315. 'MsgBox "Winsock.dll is not responding."
  316. HostByName = "Unknown"
  317. Exit Function
  318. End If
  319.  
  320. RtlMoveMemory host, hostent_addr, LenB(host)
  321. RtlMoveMemory hostip_addr, host.h_addr_list, 4
  322.  
  323. Do
  324. ReDim temp_ip_address(1 To host.h_length)
  325. RtlMoveMemory temp_ip_address(1), hostip_addr, host.h_length
  326.  
  327. For i = 1 To host.h_length
  328. ip_address = ip_address & temp_ip_address(i) & "."
  329. Next
  330. ip_address = Mid$(ip_address, 1, Len(ip_address) - 1)
  331.  
  332. HostByName = ip_address
  333.  
  334. ip_address = ""
  335. host.h_addr_list = host.h_addr_list + LenB(host.h_addr_list)
  336. RtlMoveMemory hostip_addr, host.h_addr_list, 4
  337. Loop While (hostip_addr <> 0)
  338.  
  339.  
  340.  
  341. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement