--- joko/ToolBox/Windows/VpnDial/src/Module_Main.bas 2005/10/06 20:15:34 1.3 +++ joko/ToolBox/Windows/VpnDial/src/Module_Main.bas 2005/10/08 01:01:45 1.7 @@ -15,8 +15,26 @@ ' http://www.activevb.de/rubriken/apikatalog/deklarationen/rasenumentries.html ' http://www.dotnet247.com/247reference/msgs/18/93960.aspx +' contains all ras entry objects Public RasEntries As New Collection +' globals to store connection name and state +Public ConnectionName As String +Public ConnectionOnline As Boolean + +' globals to store information about action to do on up|down +Enum ActionTypes + RUN_SCRIPT + ADD_ROUTE +End Enum +Public ActionType As ActionTypes + +Public ScriptName As String +Public RouteNet As String, RouteMask As String + +Const RouteMaskDefault As String = "255.255.255.0" + + Sub Main() Dim cmdline As New CommandLine @@ -59,7 +77,7 @@ ElseIf cmdline.hasSwitch("script") And success = True Then script_name = cmdline.getArgument("script") If script_name <> "" Then - script_args = Chr(34) & DetermineClientIP() & Chr(34) & " " & Chr(34) & DetermineServerIP & Chr(34) + script_args = Chr(34) & DetermineClientIP(conName) & Chr(34) & " " & Chr(34) & DetermineServerIP(conName) & Chr(34) Shell App.Path & "\" & script_name & " " & script_args, vbHide End If 'End If @@ -85,6 +103,36 @@ End If End If + + ' monitor + ElseIf cmdline.hasSwitch("monitor") Then + conName = cmdline.getArgument("monitor") + If conName <> "" Then + + ' run script + If cmdline.hasSwitch("script") Then + ActionType = RUN_SCRIPT + ScriptName = cmdline.getArgument("script") + End If + + ' add a route with target network via gateway + If cmdline.hasSwitch("net") Then + ActionType = ADD_ROUTE + RouteNet = cmdline.getArgument("net") + If cmdline.hasSwitch("mask") Then + RouteMask = cmdline.getArgument("mask") + Else + RouteMask = RouteMaskDefault + End If + End If + + 'Set rasItem = RasEntries(conName) + 'RasRetrieveConnectionHandler conName + ConnectionName = conName + ConnectionOnline = RasIsOnline(conName) + MonitorRASStatusAsync + End If + End If 'End If @@ -114,3 +162,53 @@ Next i End Sub + +' callback from MonitorRASStatusAsync +Public Sub detectOnlineOfflineChange() + Dim isOnline As Boolean + Dim script_name As String, script_args As String + Dim cmd As String + + isOnline = RasIsOnline(ConnectionName) + + If ConnectionOnline <> isOnline Then + 'MsgBox isOnline + + Select Case ActionType + + Case RUN_SCRIPT: + script_name = ScriptName + If script_name <> "" Then + script_args = Chr(34) & DetermineClientIP(ConnectionName) & Chr(34) & " " & Chr(34) & DetermineServerIP(ConnectionName) & Chr(34) + cmd = App.Path & "\" & script_name & " " & script_args + End If + + Case ADD_ROUTE: + ' connection goes online + If isOnline = True Then + script_name = "route" + script_args = "add " & RouteNet & " mask " & RouteMask & " " & DetermineClientIP(ConnectionName) + cmd = script_name & " " & script_args + + ' connection goes offline + Else + ' Nothing to do in this case + + End If + + End Select + + If cmd <> "" Then + 'MsgBox cmd + On Error Resume Next + Shell cmd, vbHide + 'Shell cmd, vbNormalFocus + 'If Err.Number <> 0 Then + ' MsgBox "Error while calling cmd: " & cmd & vbCrLf & "Error-Number: " & Err.Number + 'End If + On Error GoTo 0 + End If + + ConnectionOnline = isOnline + End If +End Sub