Option Explicit
Option Base 1

Sub About_Click ()
    aboutForm.Show 1
End Sub

Sub ControlButton_Click ()


End Sub

Sub DisplayInformation (DisplayString As String, RealString As String)

    If NetworkInformation.ListCount = Screen_Size Then
	NetworkInformation.RemoveItem 0
    End If
    
    NetworkInformation.AddItem Mid$(DisplayString, 2)
    
    If counter <= Screen_Size Then
	counter = counter + 1
    End If
    
    Screen_Lines(Screen_Counter + 1) = RealString
    Screen_Counter = (Screen_Counter + 1) Mod Screen_Size

    If counter > Screen_Size Then
	Screen_Top = (Screen_Counter + Screen_Size - 1) Mod Screen_Size + 1
    End If

End Sub

Sub DisplayRip (ripString As String)
    
    ripForm.RipNetwork = FormatHexAddress(Mid$(ripString, 1, 4))
    ripForm.RipHops = FormatDecimalAddress(Mid$(ripString, 6, 1))
    ripForm.RipTicks = FormatDecimalAddress(Mid$(ripString, 8, 1))
    
    ripForm.Show 1

End Sub

Sub DisplaySap (sapString As String)
    
    SapForm.SapServerType = FormatHexAddress(Mid$(sapString, 1, 2))
    SapForm.SapServerName = Mid$(sapString, 3, 48)
    SapForm.SapServerAddress = "[" & FormatHexAddress(Mid$(sapString, 51, 4)) & "][" & FormatHexAddress(Mid$(sapString, 55, 6)) & "][" & FormatHexAddress(Mid$(sapString, 61, 2)) & "]"
    SapForm.SapServerInterveningNetworks = FormatDecimalAddress(Mid$(sapString, 64, 1))
    
    SapForm.Show 1
    
End Sub

Sub EndButton_Click ()
    End
End Sub

Sub exitButton_Click ()
    End
End Sub

Sub Form_Load ()

    RipPausedFlag = False
    SapPausedFlag = False
    Screen_Counter = 0
    Screen_Top = 0
    counter = 0

End Sub

Function FormatDecimalAddress (inString As String) As String
    Dim nwstring, outString As String
    Dim index As Integer
    Dim number As Integer

    'Pretty printing for the hexidecimal network and node addresses
    outString = ""
    nwstring = inString
    For index = 1 To Len(nwstring)
	outString = outString & Asc(Mid$(nwstring, index, 1))
    Next index

    FormatDecimalAddress = outString

End Function

Function FormatHexAddress (inString As String) As String
    Dim nwstring, outString As String
    Dim index As Integer
    Dim number As Integer

    'Pretty printing for the hexidecimal network and node addresses
    outString = ""
    nwstring = inString
    For index = 1 To Len(nwstring)
       outString = outString & Format$(Hex$(Asc(Mid$(nwstring, index, 1))), "00")
    Next index

    FormatHexAddress = outString
End Function

Function FormatRIP (network As RipNetwork, realRip As String) As String

    Dim ripString As String
    
    ripString = FormatRipAddress(network.network, network.hops, network.ticks)
    realRip = network.network & network.hops & network.ticks
    FormatRIP = ripString

End Function

Function FormatRipAddress (inNetwork As String, inHops As String, inTicks As String) As String
    
    Dim nwstring, outString As String
    Dim index As Integer
    Dim number As Integer

    'Pretty printing for the hexidecimal network and node addresses
    outString = ""
    nwstring = Mid$(inNetwork, 1, 4)
    For index = 1 To Len(nwstring)
       outString = outString & Format$(Hex$(Asc(Mid$(nwstring, index, 1))), "00")
    Next index

    If Asc(inHops) > 9 Then
	outString = outString & " "
    Else
	outString = outString & "  "
    End If
	
'    outString = outString & inHops
    outString = outString & FormatDecimalAddress(Mid$(inHops, 2, 1))

    outString = outString & "/"
    
'    outString = outString & inTicks
    outString = outString & FormatDecimalAddress(Mid$(inTicks, 2, 1))
    
    FormatRipAddress = outString
End Function

Function FormatSAP (server As SapServer, realSap As String) As String
    
    Dim workString As String
    Dim net As Integer
    
    net = Asc(Mid$(server.InterveningNetworks, 2, 1))
    If net > 9 Then
	FormatSAP = RightPad(server.ServerName, 12) & " " & net
    Else
	FormatSAP = RightPad(server.ServerName, 12) & "  " & net
    End If
    realSap = server.serverType & server.ServerName
    realSap = realSap & server.ServerAddress.network & server.ServerAddress.node & server.ServerAddress.socket & server.InterveningNetworks

End Function

Function LowByte (inWord As Integer)

    inWord = inWord Mod 256
    LowByte = inWord

End Function

Sub NetworkInformation_DblClick ()

    Dim selected As Integer
    Dim selectedString As String

    selected = NetworkInformation.ListIndex

    If selected >= 0 Then
	selected = (selected + Screen_Top) Mod Screen_Size
	'MsgBox "Clicked line " & selected
	selectedString = Screen_Lines(selected + 1)
	Select Case Mid$(selectedString, 1, 1)
	    Case Chr$(0)
		DisplaySap Mid$(selectedString, 2)
	    Case Chr$(1)
		DisplayRip Mid$(selectedString, 2)
	End Select
    End If

End Sub

Function RightPad (inString As String, inLength As Integer) As String

    Dim index As Integer
    Dim workString As String

    workString = Space$(inLength)

    For index = 1 To inLength

	If Mid$(inString, index, 1) = Chr(0) Then
	    Exit For
	End If
	Mid$(workString, index, 1) = Mid$(inString, index, 1)
    Next

    RightPad = workString

End Function

Sub RipControl_ReceiveData ()
    
    Dim ReceivedRip As RipPacket
    Dim ccode As Integer
    Dim entries As Integer
    Dim index As Integer
    Dim ripString As String
    Dim realRipString As String

    If RipPausedFlag = False Then
	ccode = TranslateRIP(ReceivedRip, entries)
	For index = 1 To entries
	    ripString = FormatRIP(ReceivedRip.ripNetworks(index), realRipString)
	    ripString = Chr$(1) & ripString
	    realRipString = Chr$(1) & realRipString
	    DisplayInformation ripString, realRipString
	Next index
    End If
End Sub

Sub RipPause_Click ()
    If RipPausedFlag = False Then
	RipPausedFlag = True
	RipPause.Caption = "Resume &Rip"
    Else
	RipPausedFlag = False
	RipPause.Caption = "Pause &Rip"
    End If

End Sub

Sub SapControl_ReceiveData ()
    
    Dim ReceivedSAP As PeriodicSap
    Dim ccode As Integer
    Dim entries As Integer
    Dim index As Integer
    Dim sapString As String
    Dim realSapString As String

    If SapPausedFlag = False Then
	ccode = TranslateSAP(ReceivedSAP, entries)
	For index = 1 To entries
	    sapString = FormatSAP(ReceivedSAP.sapServers(index), realSapString)
	    sapString = Chr$(0) & sapString
	    realSapString = Chr$(0) & realSapString
	    DisplayInformation sapString, realSapString
	Next index
    End If

End Sub

Sub SapPause_Click ()
    If SapPausedFlag = False Then
	SapPausedFlag = True
	SapPause.Caption = "Resume &Sap"
    Else
	SapPausedFlag = False
	SapPause.Caption = "Pause &Sap"
    End If


End Sub

Function TranslateRIP (ReceivedRip As RipPacket, numEntries As Integer) As Integer
    
    Dim Received As String
    Dim length As Integer

    'IPX
    Received = RipControl.Received
    ReceivedRip.Operation = Mid$(Received, 1, 2)

    length = Len(Received)
    length = length - 2
    numEntries = 0

    While length > 0

	ReceivedRip.ripNetworks(numEntries + 1).network = Mid$(Received, 3 + numEntries * 8, 4)
	ReceivedRip.ripNetworks(numEntries + 1).hops = Mid$(Received, 7 + numEntries * 8, 2)
	ReceivedRip.ripNetworks(numEntries + 1).ticks = Mid$(Received, 9 + numEntries * 8, 2)
	length = length - 8
	numEntries = numEntries + 1

    Wend




End Function

Function TranslateSAP (ReceivedSAP As PeriodicSap, numEntries As Integer) As Integer
    
    Dim Received As String
    Dim length As Integer

    'IPX
    Received = SAPControl.Received
    length = Len(Received)
    ReceivedSAP.SAPPacketType = Mid$(Received, 2, 1)
    length = length - 2
    numEntries = 0

    While length > 0
	ReceivedSAP.sapServers(numEntries + 1).serverType = Mid$(Received, 3 + numEntries * 64, 2)
	ReceivedSAP.sapServers(numEntries + 1).ServerName = Mid$(Received, 5 + numEntries * 64, 48)
	ReceivedSAP.sapServers(numEntries + 1).ServerAddress.network = Mid$(Received, 53 + numEntries * 64, 4)
	ReceivedSAP.sapServers(numEntries + 1).ServerAddress.node = Mid$(Received, 57 + numEntries * 64, 6)
	ReceivedSAP.sapServers(numEntries + 1).ServerAddress.socket = Mid$(Received, 63 + numEntries * 64, 2)
	ReceivedSAP.sapServers(numEntries + 1).InterveningNetworks = Mid$(Received, 65 + numEntries * 64, 2)
	
	length = length - 64
	numEntries = numEntries + 1

    Wend

End Function

Sub userPictureDrop_DblClick ()
    aboutForm.Show 1
End Sub

