国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

VB.net進階:VB.net下的Sniffer

2024-07-10 13:01:05
字體:
來源:轉載
供稿:網友
改寫后的代碼分成兩部分:receiver,用來偵聽;packetinfo,對數據包進行簡單的解析。
數據包的內容是通過receiver的datareceived事件返回來的。
每個函數都不長,容易看懂,注釋我就……咳咳。




imports system.net
imports system.net.sockets
imports system.threading

public class receiver

dim buffer as byte()
dim mvarbufferlength as integer = 4096
dim sck as socket
dim thrreceive as thread
dim mvarstopone as boolean = false

public event datareceived(byval data as byte(), byval length as integer)

sub new()
redim buffer(mvarbufferlength)

sck = new socket(addressfamily.internetwork, sockettype.raw, protocoltype.ip)
sck.blocking = false
sck.bind(new ipendpoint(dns.gethostbyname(dns.gethostname).addresslist(0), 0))

if not setsockoption() then throw new exception("unable to setup socket options")
end sub

public property bufferlength() as integer
get
return mvarbufferlength
end get
set(byval value as integer)
if not thrreceive is nothing then
if thrreceive.threadstate = threadstate.running then throw new exception("receiving thread is running. call stopreceive() first.")
end if
redim buffer(value)
mvarbufferlength = value
end set
end property

public property stopeveryonepackage() as boolean '指定是否接受一個數據包后就退出。用于測試。
get
return mvarstopone
end get
set(byval value as boolean)
mvarstopone = value
end set
end property

public sub startreceive()
stopreceive()
thrreceive = new thread(addressof subreceive)
thrreceive.start()
end sub

public sub stopreceive()
try
thrreceive.abort()
catch ex as exception
end try
end sub

private sub subreceive()
dim i as integer, ar as iasyncresult
dim b as byte()
while true
ar = sck.beginreceive(buffer, 0, buffer.length, socketflags.none, nothing, me)
i = sck.endreceive(ar)
redim b(i)
array.copy(buffer, 0, b, 0, i)
raiseevent datareceived(b, i)
thread.currentthread.sleep(10)
if me.stopeveryonepackage then exit while
end while
end sub

private function setsockoption() as boolean
try
sck.setsocketoption(socketoptionlevel.ip, socketoptionname.headerincluded, 1)
dim in_() as byte = {1, 0, 0, 0}
dim out_(4) as byte
dim sio_rcvall as long = &h98000001
sck.iocontrol(sio_rcvall, in_, out_)
if (bitconverter.toint32(out_, 0) <> 0) then return false
catch ex as socketexception
return false
end try
return true
end function
end class



------------------------------------------------------------------------------------------




imports system.net

public class packetinfo
dim data as byte()

sub new(byval packetdata as byte())
data = packetdata
end sub

public readonly property protocal() as system.net.sockets.protocoltype
get
select case getprotocal()
case 17
return net.sockets.protocoltype.udp
case 6
return net.sockets.protocoltype.tcp
case 1
return net.sockets.protocoltype.icmp
case else
return net.sockets.protocoltype.unknown
end select
end get
end property

public readonly property sender() as ipendpoint
get
if me.protocal = sockets.protocoltype.unknown then return nothing
return getsenderipendpoint()
end get
end property

public readonly property receiver() as ipendpoint
get
if me.protocal = sockets.protocoltype.unknown then return nothing
return getreceiveripendpoint()
end get
end property

public readonly property packetdata() as byte()
get
if me.protocal = sockets.protocoltype.unknown then return nothing
return getdata()
end get
end property



private function getprotocal() as integer
return data(9)
end function

private function getsenderipendpoint() as ipendpoint
return new ipendpoint(getsenderaddress, getsenderport)
end function

private function getreceiveripendpoint() as ipendpoint
return new ipendpoint(getreceiveraddress, getreceiverport)
end function

private function getsenderaddress() as ipaddress
return getaddress(12)
end function

private function getsenderport() as integer
return getport(20)
end function

private function getreceiveraddress() as ipaddress
return getaddress(16)
end function

private function getreceiverport() as integer
return getport(21)
end function

private function getaddress(byval startindex as integer) as ipaddress
dim b(3) as byte
array.copy(data, startindex, b, 0, 4)
return ipaddress.parse(string.format("{0}.{1}.{2}.{3}", b(0), b(1), b(2), b(3)))
end function

private function getport(byval startindex as integer) as integer
return data(startindex) * 256 + data(startindex + 1)
end function

private function getdata() as byte()
dim b as byte()
dim headerlength as integer

select case me.protocal
case sockets.protocoltype.tcp
headerlength = 40
case sockets.protocoltype.udp
headerlength = 28
end select
redim b(data.length - headerlength)
array.copy(data, headerlength, b, 0, data.length - headerlength)
return b
end function

end class



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 合山市| 光山县| 遵义县| 峡江县| 呼伦贝尔市| 靖边县| 定兴县| 台中县| 安阳县| 永昌县| 南康市| 夏邑县| 彰化市| 寿光市| 乐亭县| 大足县| 定南县| 宁夏| 焉耆| 柏乡县| 泗水县| 兴城市| 洛隆县| 沈丘县| 思茅市| 台东市| 塔城市| 行唐县| 盖州市| 湖州市| 富阳市| 莱阳市| 乌拉特中旗| 龙岩市| 绥化市| 商都县| 霍林郭勒市| 蓬莱市| 玉田县| 苍山县| 新乡市|