Author: days with QQ: 19632995MSN: jyu1221@hotmail.com Date: 2002.04.30
In order to facilitate VB enthusiasts, IP packets can be intercepted in the C language. I specifically write the following source code for VB developers.
The following is the source code for the TCP / IP package under Win2000 in VB. In VB6.0, Win2000 test passes, the place you need to pay attention is, 1. Must and local network cards, 2. Every time you get the data must be A delay. 3. The data is taken behind the array of buff. 4. Place the following code in a module.
'---------------------------- Code start ------------------ ------------------------------ Declare Function Bind LIB "WS2_32.dll" (Byval S As Long, AddR As Sock_Addr , ByVal namelen As Long) As LongDeclare Function closesocket Lib "ws2_32.dll" (ByVal s As Long) As LongDeclare Function connect Lib "ws2_32.dll" (ByVal s As Long, name As SOCK_ADDR, ByVal namelen As Integer) As LongDeclare Function inet_addr Lib "ws2_32.dll" (ByVal cp As String) As LongDeclare Function htons Lib "ws2_32.dll" (ByVal hostshort As Integer) As IntegerDeclare Function recv Lib "ws2_32.dll" (ByVal s As Long, buffer As Any, ByVal length As Long, ByVal flags As Long) As LongDeclare Function send Lib "ws2_32.dll" (ByVal s As Long, buffer As Any, ByVal length As Long, ByVal flags As Long) As LongDeclare Function shutdown Lib "ws2_32.dll" ( BYVAL S long al af As Long, ByVal type_specification As Long, ByVal protocol As Long) As LongDeclare Function WSACancelBlockingCall Lib "ws2_32.dll" () As LongDeclare Function WSACleanup Lib "ws2_32.dll" () As LongDeclare Function WSAGetLastError Lib "ws2_32.dll" ( ) As LongDeclare Function WSAStartup Lib "ws2_32.dll" (ByVal wVersionRequired As Integer, wsData As WSA_DATA) As LongDeclare Function WSASocketA Lib "ws2_32.dll" (ByVal af As Long, ByVal type1 As Long, ByVal protocol As Long, lpProtocolInfo As Long , g as long, byval dwflags as long) Declare function WSAIOCTL LIB "WS2_32.DLL"
(ByVal s As Long, ByVal dwIoControlCode As Long, lpvInBuffer As Long, ByVal cbInBuffer As Long, lpvOutBuffer As Long, ByVal cbOutBuffer As Long, lpcbBytesReturned As Long, lpOverlapped As Long, lpCompletionRoutine As Long) As LongDeclare Sub CopyMemory Lib "kernel32" Alias "RTLMOVEMEMORY" (Destination As Any, Source As Any, Byval Length As "
Public Declare Sub Sleep LIB "Kernel32" (Byval dwmilliseconds as long)
Public const wsadescription_len = 256public const wsasys_status_len = 128
Type WSA_DATA wVersion As Integer wHighVersion As Integer strDescription (WSADESCRIPTION_LEN 1) As Byte strSystemStatus (WSASYS_STATUS_LEN 1) As Byte iMaxSockets As Integer iMaxUdpDg As Integer lpVendorInfo As LongEnd Type
TYPE IN_ADDR S_ADDR AS LONGEND TYPE
TYPE SOCK_ADDR SIN_FAMILY AS INTEGER SIN_PORT AS INTEGER SIN_ADDR AS IN_ADDR SIN_ZERO (0 to 7) as Byteend Type
Type IPHeader lenver As Byte tos As Byte len As Integer ident As Integer flags As Integer ttl As Byte proto As Byte checksum As Integer sourceIP As Long destIP As LongEnd Type Const AF_INET = 2Const SOCK_RAW = 3Const IPPROTO_IP = 0Const IPPROTO_TCP = 6Const IPPROTO_UDP = 17Const MAX_PACK_LEN = 65535Const Socket_ERROR = -1 &
Private mwsadata as WSA_DATAPRIVATE M_HSOCKET AS Long
Private msalocaladdr as suck_addr
Private msaremoteaddr as suck_addr
Sub Main () Dim nResult As Long nResult = WSAStartup (& H202, mwsaData) If nResult <> WSANOERROR Then MsgBox "Error en WSAStartup" Exit Sub End If m_hSocket = socket (AF_INET, SOCK_RAW, IPPROTO_IP) If (m_hSocket = INVALID_SOCKET) Then MsgBox "Error in socket" Exit Sub End If msaLocalAddr.sin_family = AF_INET msaLocalAddr.sin_port = 0 msaLocalAddr.sin_addr.S_addr = inet_addr ( "192.168.1.125") 'It should be your own network card IP address nResult = bind (m_hSocket, msaLocalAddr , Len (msaLocalAddr)) If (nResult = SOCKET_ERROR) Then MsgBox "Error in bind" Exit Sub End If Dim InParamBuffer As Long Dim BytesRet As Long BytesRet = 0 InParamBuffer = 1nResult = ioctlsocket (m_hSocket, & H98000001, 1)
If nResult <> 0 Then MsgBox "ioctlsocket" Exit Sub End If Dim strData As String Dim nReceived As Long 'to intercept the data on the BUFF inside Dim Buff (0 To MAX_PACK_LEN) As Byte Dim IPH As IPHeader Do Until False' in this example I have been getting doevents nresult = recv (m_hsocket, buff (0), max_pack_len, 0) if Nresult = SOCKET_ERROR THEN MSGBOX "Error In Recvdata :: RECV" EXIT DO END IF COPYMEMORY IPH, BUFF (0), LEN (iPh) 'In order to facilitate access to Select Case IPH.proto Case IPPROTO_TCP' frmHookTcpip.Text1.SelText = HexIp2DotIp (IPH.sourceIP) 'frmHookTcpip.Text1.SelText = "----->"' frmHookTcpip.Text1.SelText = HexIp2DotIp (IPH. destIP) 'frmHookTcpip.Text1.SelText = vbCrLf Debug.Print HexIp2DotIp (IPH.sourceIP) & "----->" & HexIp2DotIp (IPH.destIP) End Select Loop nResult = shutdown (m_hSocket, 2) nResult = closesocket ( M_HSocket) Nresul T = wsacancelblockingcall nresult = wsacleanupend sub