Jump to content

Simple code work strange


Nnet
 Share

Recommended Posts

Hello, i have this code

 

Func Minuta () 

   if (TimerDiff($time1)>1000) then
            While $radek<="255"
            local $index=_GUICtrlListView_FindText( $packetwindow, $radek) 
            local $cas=_GUICtrlListView_GetItemText($packetwindow,$index , 2) 
            _GUICtrlListView_SetItemText($packetwindow, $index, Abs($cas+"1"), 2) 
            $radek+=1
            _GUICtrlListView_EnsureVisible($packetwindow, $index)
            if $cas>"50" Then
            ConsoleWrite($cas & @CRLF)
            
            EndIf
         Wend
         $radek = "0"
         $time1=TimerInit()


    endif
EndFunc

I have 2 problems with this code.
1.  Every cycle i want +1 to number but its make +2 or +3  have no idea why
2.  I want write to concele numbers bigger than 50 but it write to console specificly numbers 6,7,8 than 50-99 than 500-999 O.o Have no idea why.

Pls. do you know what happening here?

Edited by Nnet
Link to comment
Share on other sites

Hi Nnet, welcome to the forum.

Things that stand out for me.

{1} When you quote a number, you are really saying that number is a string. I suggest you don't do that in this instance, so get rid of the double quotes.

{2} You should declare $radek (i.e. Global $radek at the start of your script).

{3} You really shouldn't keep declaring the Locals all the time inside your loop. Declare them once, before the loop.

{4} I am a bit lost as to what all your code is doing, but then I don't know what $packetwindow is etc, and which number are you referring to - $radek or $cas ?

{5} Obviously there is more code than what you provided, and perhaps we need an overall description of what you are trying to do.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

Ok here its all code.
All code works properly only func minuta works how i said. Im allready know it coz not all indexes are there so im tryed make corection witch dont work too :(
 

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiListView.au3>
#include <StaticConstants.au3>
#include <ComboConstants.au3>
#include <GUIListViewEx.au3>
#include <Winpcap.au3>
#include <Date.au3>


Global $src=0
Global $time1=TimerInit()
Global $radek =Number(0)
$winpcap=_PcapSetup()
If ($winpcap=-1) Then
    MsgBox(16,"Nebyl nalezen ovladač WinPAP!")
    exit
EndIf

$pcap_devices=_PcapGetDeviceList()
If ($pcap_devices=-1) Then
    MsgBox(16,"Prostě chyba, nejsou žádné síťové adaptéry!",_PcapGetLastError())
    exit
EndIf

GUICreate("Časovač Navision", 600, 500)
$interface=GUICtrlCreateCombo("", 80, 15, 300,default,$CBS_DROPDOWNLIST)

For $i = 0 to Ubound($pcap_devices)-1
    GUICtrlSetData(-1, $pcap_devices[$i][1])
Next
$filter=GUICtrlCreateInPut ("port 139", 500, 15, 90)

$start=GUICtrlCreateButton ( "Start", 20, 460, 60)
$stop=GUICtrlCreateButton ( "Stop", 110, 460,60)
GUICtrlSetState (-1, $GUI_DISABLE )
$clear=GUICtrlCreateButton ( "Vymazat", 200, 460,60)

GUICtrlSetState (-1, $GUI_DISABLE )

GUICtrlSetStyle(GUICtrlCreateLabel ( "Adaptér :", 8, 20, 60),$SS_RIGHT)
GUICtrlSetStyle(GUICtrlCreateLabel ( "Filtr :", 430, 18, 60),$SS_RIGHT)
$packetwindow = GUICtrlCreateListView("č.|Naposledy aktivní|Neaktivní|Délka|Packet", 10, 50, 580, 395)
_GUICtrlListView_SetColumn($packetwindow,0,"číslo",40,1)
_GUICtrlListView_SetColumnWidth($packetwindow, 1, 80)
_GUICtrlListView_SetColumn($packetwindow,3,"Délka",40,1)
_GUICtrlListView_SetColumnWidth($packetwindow, 3, 60)
_GUICtrlListView_SetColumn($packetwindow,2,"Neaktivní",40,1)
_GUICtrlListView_SetColumnWidth($packetwindow, 2, 60)
_GUICtrlListView_SetColumnWidth($packetwindow, 4, 290)
GUISetState()

$i=0
$pcap=0
$packet=0
$pcapfile=0

Do
    $msg = GUIGetMsg()

    If ($msg=$start) Then

        $prom=0

        $int=""

            For $n = 0 to Ubound($pcap_devices)-1
                If $pcap_devices[$n][1]=GUICtrlRead($interface) Then
                    $int=$pcap_devices[$n][0]
                    ExitLoop
                EndIf
            Next

        $pcap=_PcapStartCapture($int,GUICtrlRead($filter),$prom)
        If ($pcap=-1) Then
            MsgBox(16,"Chyba čtení dat!",_PcapGetLastError())
            ContinueLoop
        EndIf
        $linktype=_PcapGetLinkType($pcap)
        If ($linktype[1]<>"EN10MB") Then
            MsgBox(16,"Chyba, funguje jen pro Ethernet!")
            ContinueLoop
        Endif

        GUICtrlSetState ($stop, $GUI_ENABLE)
        GUICtrlSetState ($start, $GUI_DISABLE)

    EndIf

    If ($msg=$stop) Then
        If IsPtr($pcapfile) Then
            _PcapStopCaptureFile($pcapfile)
            $pcapfile=0
        EndIf
        if Not IsInt($pcap) Then _PcapStopCapture($pcap)
        $pcap=0
        GUICtrlSetState ($stop, $GUI_DISABLE)
        GUICtrlSetState ($start, $GUI_ENABLE)

    EndIf

    If ($msg=$clear) Then
        _PcapGetStats($pcap)
        _GUICtrlListView_DeleteAllItems($packetwindow)

    EndIf



    If IsPtr($pcap) Then    ; If $pcap is a Ptr, then the capture is running
      $time0=TimerInit()

      While (TimerDiff($time0)<500) ; Retrieve packets from queue for maximum 500ms before returning to main loop, not to "hang" the window for user
         $packet=_PcapGetPacket($pcap)
         Call("Minuta")
         If IsInt($packet) Then ExitLoop
         Call("MyDissector", $packet[3])


            ;ConsoleWrite($src & @CRLF)
            ;ConsoleWrite(MyDissector($packet[3]) & @CRLF)
            If MyDissector($packet[3]) <> "0" Then
               Local $textnajdi=_GUICtrlListView_FindText( $packetwindow, $ippart)
               If $textnajdi="-1" then
               GUICtrlCreateListViewItem($ippart&"|"&StringTrimRight($packet[0],7)&"|"&"0"&"|"&$packet[2]&"|"&MyDissector($packet[3]), $packetwindow)
               ;$data=$packet[3]
               _GUICtrlListView_EnsureVisible($packetwindow, $ippart)

               Else
               ;local $rozdil=_DateAdd ( $sType, $iNumber, $sDate )
               if $packet[2]>="61" then
                     _GUICtrlListView_SetItemText($packetwindow, $textnajdi, StringTrimRight($packet[0],7), 1)
                     _GUICtrlListView_SetItemText($packetwindow, $textnajdi, "0", 2)
                     _GUICtrlListView_EnsureVisible($packetwindow, $ippart)
                  EndIf

               EndIf

            If IsPtr($pcapfile) Then _PcapWriteLastPacket($pcapfile)
            EndIf
            ;_GUICtrlListView_EnsureVisible($packetwindow, $ippart)
        Wend
    EndIf

Until $msg=$GUI_EVENT_CLOSE

If IsPtr($pcapfile) Then _PcapStopCaptureFile($pcapfile)    ; A file is still open: close it
if IsPtr($pcap) Then _PcapStopCapture($pcap)    ; A capture is still running: close it
_PcapFree()

Exit

Func Minuta () ;každou minutu přičte 1 k neaktivnimu času
    local $cas
   ;ConsoleWrite(TimerDiff($time1) & @CRLF)
   if (TimerDiff($time1)>1000) then
         While $radek<=255
            $radek+=1
            local $index=_GUICtrlListView_FindText( $packetwindow, $radek) ;najde text podle konce ip a dodá jeho adresu
               If $index="-1" then

               elseif $index<>"-1" then
                  $cas=_GUICtrlListView_GetItemText($packetwindow,$index , 2) ;přečte na řádku hodnotu aktuálního neaktivního času
                  local $cas2=Number($cas+1)
                  _GUICtrlListView_SetItemText($packetwindow, $index, $cas2, 2) ;zapíše novou hodnotu zvětšenou o 1
                     if $cas>50 Then
                        ConsoleWrite("XX" & $cas & @CRLF)
                        ;Odpojit ()
                     EndIf
               EndIf

         Wend
      $radek = 0
      $time1=TimerInit()
   endif
EndFunc

Func Odpojit () ;pošle odpojovací packet
$broadcastmac="FFFFFFFFFFFF" ; broacast
$mymac=@YEAR ; my mac address in hex
$ethertype="0800"   ; ethertype = IP
$checkSum="1a35"   ; Just manual for now...

$mypacket="0x"&$broadcastmac&$mymac&$ethertype&"4500014800010000641155a500000000ffffffff004400430134" & $checkSum & "01010600ca1d0c4a0100000000000000000000000000000000000000" & $mymac & "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ; stick together to a binary string !
_PcapSendPacket($pcap,$mypacket) ; sends a valid ethernet broadcast !
EndFunc

Func MyDissector ($data) ;třídič paketů

    Local $ethertype=BinaryMid ( $data, 13 ,2 )

    If $ethertype="0x0800" Then

        Global $src=Number(BinaryMid ($data, 27 ,1))&"."&Number(BinaryMid ($data, 28 ,1))&"."&Number(BinaryMid ($data, 29 ,1))&"."&Number(BinaryMid ($data, 30 ,1))
        Global $dst=Number(BinaryMid ($data, 31 ,1))&"."&Number(BinaryMid ($data, 32 ,1))&"."&Number(BinaryMid ($data, 33 ,1))&"."&Number(BinaryMid ($data, 34 ,1))
        Global $ippart=Number(BinaryMid ($data, 30 ,1))
        Switch BinaryMid ($data, 24 ,1)
            Case "0x01"
               return "0"
            Case "0x02"
               return "0"
            Case "0x06"
                Global $srcport=Number(BinaryMid ($data, 35 ,1))*256+Number(BinaryMid ($data, 36 ,1))
                Global $dstport=Number(BinaryMid ($data, 37 ,1))*256+Number(BinaryMid ($data, 38 ,1))
                Global $flags=BinaryMid ($data, 48 ,1)
                Local $f=""
                If BitAND($flags,0x01) Then $f="Fin "
                If BitAND($flags,0x02) Then $f&="Syn "
                If BitAND($flags,0x04) Then $f&="Rst "
                If BitAND($flags,0x08) Then $f&="Psh "
                If BitAND($flags,0x10) Then $f&="Ack "
                If BitAND($flags,0x20) Then $f&="Urg "
                If BitAND($flags,0x40) Then $f&="Ecn "
                If BitAND($flags,0x80) Then $f&="Cwr "
                $f=StringTrimRight(StringReplace($f," ",","),1)
                ;ConsoleWrite(BinaryMid ( $data, 13 ,2 ) & "TCP("&$f&") "&$src&":"&$srcport&" -> "&$dst&":"&$dstport &"..." &$ippart & @CRLF & BinaryMid($data,1,255) & @CRLF )
                return "TCP("&$f&") "&$src&":"&$srcport&" -> "&$dst&":"&$dstport &"..." &$ippart

            Case "0x11"
                return "0"
            Case Else
                return "0"
        EndSwitch

    ;ElseIf $ethertype="0x060800" Then

 EndIf

return "0"

EndFunc

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...