refactor option handling code

pull/1/head
Heiko Hund 2010-04-09 06:01:30 +02:00
parent b0c1bd562c
commit a6e6d88115
18 changed files with 666 additions and 682 deletions

View File

@ -33,7 +33,7 @@
#include "options.h"
#include "registry.h"
extern struct options o;
extern options_t o;
static const LANGID fallbackLangId = MAKELANGID(LANG_ENGLISH, SUBLANG_NEUTRAL);
static LANGID gui_language;

20
main.c
View File

@ -54,7 +54,7 @@ TCHAR szClassName[ ] = _T("OpenVPN-GUI");
TCHAR szTitleText[ ] = _T("OpenVPN");
/* Options structure */
struct options o;
options_t o;
int WINAPI WinMain (HINSTANCE hThisInstance,
UNUSED HINSTANCE hPrevInstance,
@ -68,7 +68,7 @@ int WINAPI WinMain (HINSTANCE hThisInstance,
/* initialize options to default state */
init_options (&o);
InitOptions(&o);
#ifdef DEBUG
/* Open debug file for output */
@ -109,7 +109,7 @@ int WINAPI WinMain (HINSTANCE hThisInstance,
/* Parse command-line options */
Createargcargv(&o, GetCommandLine());
ProcessCommandLine(&o, GetCommandLine());
/* Check if a previous instance is already running. */
if ((FindWindow (szClassName, NULL)) != NULL)
@ -224,7 +224,7 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
StopOpenVPN(LOWORD(wParam) - IDM_DISCONNECTMENU);
}
if ( (LOWORD(wParam) >= IDM_STATUSMENU) && (LOWORD(wParam) < IDM_STATUSMENU + MAX_CONFIGS) ) {
ShowWindow(o.cnn[LOWORD(wParam) - IDM_STATUSMENU].hwndStatus, SW_SHOW);
ShowWindow(o.conn[LOWORD(wParam) - IDM_STATUSMENU].hwndStatus, SW_SHOW);
}
if ( (LOWORD(wParam) >= IDM_VIEWLOGMENU) && (LOWORD(wParam) < IDM_VIEWLOGMENU + MAX_CONFIGS) ) {
ViewLog(LOWORD(wParam) - IDM_VIEWLOGMENU);
@ -281,13 +281,13 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
/* Suspend running connections */
for (i=0; i<o.num_configs; i++)
{
if (o.cnn[i].connect_status == CONNECTED)
if (o.conn[i].state == connected)
SuspendOpenVPN(i);
}
/* Wait for all connections to suspend */
for (i=0; i<10; i++, Sleep(500))
if (CountConnectedState(SUSPENDING) == 0) break;
if (CountConnState(suspending) == 0) break;
}
return FALSE;
@ -296,11 +296,11 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM
for (i=0; i<o.num_configs; i++)
{
/* Restart suspend connections */
if (o.cnn[i].connect_status == SUSPENDED)
if (o.conn[i].state == suspended)
StartOpenVPN(i);
/* If some connection never reached SUSPENDED state */
if (o.cnn[i].connect_status == SUSPENDING)
if (o.conn[i].state == suspending)
StopOpenVPN(i);
}
return FALSE;
@ -392,7 +392,7 @@ void CloseApplication(HWND hwnd)
{
int i, ask_exit=0;
if (o.service_running == SERVICE_CONNECTED)
if (o.service_state == service_connected)
{
if (MessageBox(NULL, LoadLocalizedString(IDS_NFO_SERVICE_ACTIVE_EXIT), _T("Exit OpenVPN"), MB_YESNO) == IDNO)
{
@ -401,7 +401,7 @@ void CloseApplication(HWND hwnd)
}
for (i=0; i < o.num_configs; i++) {
if (o.cnn[i].connect_status != 0) {
if (o.conn[i].state != disconnected) {
ask_exit=1;
break;
}

168
openvpn.c
View File

@ -36,6 +36,7 @@
#include "main.h"
#include "openvpn.h"
#include "openvpn_monitor_process.h"
#include "openvpn_config.h"
#include "openvpn-gui-res.h"
#include "options.h"
#include "scripts.h"
@ -44,7 +45,7 @@
#include "passphrase.h"
#include "localization.h"
extern struct options o;
extern options_t o;
/*
* Creates a unique exit_event name based on the
@ -52,15 +53,15 @@ extern struct options o;
*/
int CreateExitEvent(int config)
{
o.cnn[config].exit_event = NULL;
o.conn[config].exit_event = NULL;
if (o.oldversion == 1)
{
_sntprintf_0(o.cnn[config].exit_event_name, _T("openvpn_exit"));
o.cnn[config].exit_event = CreateEvent (NULL,
_sntprintf_0(o.conn[config].exit_event_name, _T("openvpn_exit"));
o.conn[config].exit_event = CreateEvent (NULL,
TRUE,
FALSE,
o.cnn[config].exit_event_name);
if (o.cnn[config].exit_event == NULL)
o.conn[config].exit_event_name);
if (o.conn[config].exit_event == NULL)
{
if (GetLastError() == ERROR_ACCESS_DENIED)
{
@ -70,22 +71,22 @@ int CreateExitEvent(int config)
else
{
/* error creating exit event */
ShowLocalizedMsg(IDS_ERR_CREATE_EVENT, o.cnn[config].exit_event_name);
ShowLocalizedMsg(IDS_ERR_CREATE_EVENT, o.conn[config].exit_event_name);
}
return(false);
}
}
else
{
_sntprintf_0(o.cnn[config].exit_event_name, _T("openvpngui_exit_event_%d"), config);
o.cnn[config].exit_event = CreateEvent (NULL,
_sntprintf_0(o.conn[config].exit_event_name, _T("openvpngui_exit_event_%d"), config);
o.conn[config].exit_event = CreateEvent (NULL,
TRUE,
FALSE,
o.cnn[config].exit_event_name);
if (o.cnn[config].exit_event == NULL)
o.conn[config].exit_event_name);
if (o.conn[config].exit_event == NULL)
{
/* error creating exit event */
ShowLocalizedMsg(IDS_ERR_CREATE_EVENT, o.cnn[config].exit_event_name);
ShowLocalizedMsg(IDS_ERR_CREATE_EVENT, o.conn[config].exit_event_name);
return(false);
}
}
@ -159,8 +160,8 @@ int StartOpenVPN(int config)
{
for (i=0; i < o.num_configs; i++)
{
if ((o.cnn[i].connect_status != DISCONNECTED) &&
(o.cnn[i].connect_status != DISCONNECTING))
if ((o.conn[i].state != disconnected) &&
(o.conn[i].state != disconnecting))
{
is_connected=1;
break;
@ -183,8 +184,8 @@ int StartOpenVPN(int config)
}
/* Clear connection unique vars */
o.cnn[config].failed_psw = 0;
CLEAR (o.cnn[config].ip);
o.conn[config].failed_psw = 0;
CLEAR (o.conn[config].ip);
/* Create our exit event */
if (!CreateExitEvent(config))
@ -209,13 +210,13 @@ int StartOpenVPN(int config)
if (o.oldversion == 1)
{
_sntprintf_0(command_line, _T("openvpn --config \"%s\" %s"),
o.cnn[config].config_file, proxy_string);
o.conn[config].config_file, proxy_string);
}
else
{
_sntprintf_0(command_line, _T("openvpn --service %s 0 --config \"%s\" %s"),
o.cnn[config].exit_event_name,
o.cnn[config].config_file,
o.conn[config].exit_event_name,
o.conn[config].config_file,
proxy_string);
}
@ -298,7 +299,7 @@ int StartOpenVPN(int config)
{
/* Close Handle failed */
ShowLocalizedMsg(IDS_ERR_CLOSE_HANDLE_TMP);
CloseHandle (o.cnn[config].exit_event);
CloseHandle (o.conn[config].exit_event);
return(0);
}
hOutputReadTmp=NULL;
@ -323,7 +324,7 @@ int StartOpenVPN(int config)
TRUE,
priority | CREATE_NO_WINDOW,
NULL,
o.cnn[config].config_dir,
o.conn[config].config_dir,
&start_info,
&proc_info))
{
@ -331,7 +332,7 @@ int StartOpenVPN(int config)
ShowLocalizedMsg(IDS_ERR_CREATE_PROCESS,
o.exe_path,
command_line,
o.cnn[config].config_dir);
o.conn[config].config_dir);
goto failed;
}
@ -347,7 +348,7 @@ int StartOpenVPN(int config)
{
/* CloseHandle failed */
ShowLocalizedMsg(IDS_ERR_CLOSE_HANDLE);
CloseHandle (o.cnn[config].exit_event);
CloseHandle (o.conn[config].exit_event);
return(false);
}
hOutputWrite = NULL;
@ -355,11 +356,11 @@ int StartOpenVPN(int config)
hErrorWrite = NULL;
/* Save StdIn and StdOut handles in our options struct */
o.cnn[config].hStdIn = hInputWrite;
o.cnn[config].hStdOut = hOutputRead;
o.conn[config].hStdIn = hInputWrite;
o.conn[config].hStdOut = hOutputRead;
/* Save Process Handle */
o.cnn[config].hProcess=proc_info.hProcess;
o.conn[config].hProcess=proc_info.hProcess;
/* Start Thread to show Status Dialog */
@ -378,7 +379,7 @@ int StartOpenVPN(int config)
return(true);
failed:
if (o.cnn[config].exit_event) CloseHandle (o.cnn[config].exit_event);
if (o.conn[config].exit_event) CloseHandle (o.conn[config].exit_event);
if (hOutputWrite) CloseHandle (hOutputWrite);
if (hOutputRead) CloseHandle (hOutputRead);
if (hInputWrite) CloseHandle (hInputWrite);
@ -391,32 +392,32 @@ failed:
void StopOpenVPN(int config)
{
o.cnn[config].connect_status = DISCONNECTING;
o.conn[config].state = disconnecting;
if (o.cnn[config].exit_event) {
if (o.conn[config].exit_event) {
/* Run Disconnect script */
RunDisconnectScript(config, false);
EnableWindow(GetDlgItem(o.cnn[config].hwndStatus, ID_DISCONNECT), FALSE);
EnableWindow(GetDlgItem(o.cnn[config].hwndStatus, ID_RESTART), FALSE);
SetMenuStatus(config, DISCONNECTING);
EnableWindow(GetDlgItem(o.conn[config].hwndStatus, ID_DISCONNECT), FALSE);
EnableWindow(GetDlgItem(o.conn[config].hwndStatus, ID_RESTART), FALSE);
SetMenuStatus(config, disconnecting);
/* UserInfo: waiting for OpenVPN termination... */
SetDlgItemText(o.cnn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_WAIT_TERM));
SetEvent(o.cnn[config].exit_event);
SetDlgItemText(o.conn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_WAIT_TERM));
SetEvent(o.conn[config].exit_event);
}
}
void SuspendOpenVPN(int config)
{
o.cnn[config].connect_status = SUSPENDING;
o.cnn[config].restart = true;
o.conn[config].state = suspending;
o.conn[config].restart = true;
if (o.cnn[config].exit_event) {
EnableWindow(GetDlgItem(o.cnn[config].hwndStatus, ID_DISCONNECT), FALSE);
EnableWindow(GetDlgItem(o.cnn[config].hwndStatus, ID_RESTART), FALSE);
SetMenuStatus(config, DISCONNECTING);
SetDlgItemText(o.cnn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_WAIT_TERM));
SetEvent(o.cnn[config].exit_event);
if (o.conn[config].exit_event) {
EnableWindow(GetDlgItem(o.conn[config].hwndStatus, ID_DISCONNECT), FALSE);
EnableWindow(GetDlgItem(o.conn[config].hwndStatus, ID_RESTART), FALSE);
SetMenuStatus(config, disconnecting);
SetDlgItemText(o.conn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_WAIT_TERM));
SetEvent(o.conn[config].exit_event);
}
}
@ -426,13 +427,13 @@ void StopAllOpenVPN()
int i;
for(i=0; i < o.num_configs; i++) {
if(o.cnn[i].connect_status != DISCONNECTED)
if(o.conn[i].state != disconnected)
StopOpenVPN(i);
}
/* Wait for all connections to terminate (Max 5 sec) */
for (i=0; i<20; i++, Sleep(250))
if (CountConnectedState(DISCONNECTED) == o.num_configs) break;
if (CountConnState(disconnected) == o.num_configs) break;
}
@ -516,12 +517,12 @@ BOOL CALLBACK StatusDialogFunc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lP
switch (LOWORD(wParam)) {
case ID_DISCONNECT:
SetFocus(GetDlgItem(o.cnn[config].hwndStatus, ID_EDT_LOG));
SetFocus(GetDlgItem(o.conn[config].hwndStatus, ID_EDT_LOG));
StopOpenVPN(config);
return TRUE;
case ID_HIDE:
if (o.cnn[config].connect_status != DISCONNECTED)
if (o.conn[config].state != disconnected)
{
ShowWindow(hwndDlg, SW_HIDE);
}
@ -532,8 +533,8 @@ BOOL CALLBACK StatusDialogFunc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lP
return TRUE;
case ID_RESTART:
SetFocus(GetDlgItem(o.cnn[config].hwndStatus, ID_EDT_LOG));
o.cnn[config].restart = true;
SetFocus(GetDlgItem(o.conn[config].hwndStatus, ID_EDT_LOG));
o.conn[config].restart = true;
StopOpenVPN(config);
return TRUE;
}
@ -543,14 +544,14 @@ BOOL CALLBACK StatusDialogFunc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lP
if (wParam == TRUE)
{
config = (UINT) GetProp(hwndDlg, cfgProp);
if (o.cnn[config].hwndStatus)
SetFocus(GetDlgItem(o.cnn[config].hwndStatus, ID_EDT_LOG));
if (o.conn[config].hwndStatus)
SetFocus(GetDlgItem(o.conn[config].hwndStatus, ID_EDT_LOG));
}
return FALSE;
case WM_CLOSE:
config = (UINT) GetProp(hwndDlg, cfgProp);
if (o.cnn[config].connect_status != DISCONNECTED)
if (o.conn[config].state != disconnected)
{
ShowWindow(hwndDlg, SW_HIDE);
}
@ -587,7 +588,7 @@ int AutoStartConnections()
for (i=0; i < o.num_configs; i++)
{
if (o.cnn[i].auto_connect)
if (o.conn[i].auto_connect)
StartOpenVPN(i);
}
@ -604,7 +605,7 @@ int VerifyAutoConnections()
match = false;
for (j=0; j < MAX_CONFIGS; j++)
{
if (_tcsicmp(o.cnn[j].config_file, o.auto_connect[i]) == 0)
if (_tcsicmp(o.conn[j].config_file, o.auto_connect[i]) == 0)
{
match=true;
break;
@ -858,42 +859,27 @@ int CheckVersion()
return(1);
}
/* Return num of connections with Status = CheckVal */
int CountConnectedState(int CheckVal)
{
int i;
int count=0;
for (i=0; i < o.num_configs; i++)
{
if (o.cnn[i].connect_status == CheckVal)
count++;
}
return (count);
}
void CheckAndSetTrayIcon()
{
/* Show green icon if service is running */
if (o.service_running == SERVICE_CONNECTED)
if (o.service_state == service_connected)
{
SetTrayIcon(CONNECTED);
SetTrayIcon(connected);
return;
}
/* Change tray icon if no more connections is running */
if (CountConnectedState(CONNECTED) != 0)
SetTrayIcon(CONNECTED);
if (CountConnState(connected) != 0)
SetTrayIcon(connected);
else
{
if ((CountConnectedState(CONNECTING) != 0) ||
(CountConnectedState(RECONNECTING) != 0) ||
(o.service_running == SERVICE_CONNECTING))
SetTrayIcon(CONNECTING);
if ((CountConnState(connecting) != 0) ||
(CountConnState(reconnecting) != 0) ||
(o.service_state == service_connecting))
SetTrayIcon(connecting);
else
SetTrayIcon(DISCONNECTED);
SetTrayIcon(disconnected);
}
}
@ -905,31 +891,31 @@ void ThreadOpenVPNStatus(int config)
MSG messages;
/* Cut of extention from config filename. */
_tcsncpy(conn_name, o.cnn[config].config_file, _tsizeof(conn_name));
_tcsncpy(conn_name, o.conn[config].config_file, _tsizeof(conn_name));
conn_name[_tcslen(conn_name) - (_tcslen(o.ext_string)+1)]=0;
if (o.cnn[config].restart)
if (o.conn[config].restart)
{
/* UserInfo: Connecting */
SetDlgItemText(o.cnn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_CONNECTING));
SetStatusWinIcon(o.cnn[config].hwndStatus, ID_ICO_CONNECTING);
EnableWindow(GetDlgItem(o.cnn[config].hwndStatus, ID_DISCONNECT), TRUE);
EnableWindow(GetDlgItem(o.cnn[config].hwndStatus, ID_RESTART), TRUE);
SetFocus(GetDlgItem(o.cnn[config].hwndStatus, ID_EDT_LOG));
o.cnn[config].restart = false;
SetDlgItemText(o.conn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_CONNECTING));
SetStatusWinIcon(o.conn[config].hwndStatus, ID_ICO_CONNECTING);
EnableWindow(GetDlgItem(o.conn[config].hwndStatus, ID_DISCONNECT), TRUE);
EnableWindow(GetDlgItem(o.conn[config].hwndStatus, ID_RESTART), TRUE);
SetFocus(GetDlgItem(o.conn[config].hwndStatus, ID_EDT_LOG));
o.conn[config].restart = false;
}
else
{
/* Create and Show Status Dialog */
o.cnn[config].hwndStatus = CreateLocalizedDialogParam(ID_DLG_STATUS, StatusDialogFunc, config);
if (!o.cnn[config].hwndStatus)
o.conn[config].hwndStatus = CreateLocalizedDialogParam(ID_DLG_STATUS, StatusDialogFunc, config);
if (!o.conn[config].hwndStatus)
ExitThread(1);
/* UserInfo: Connecting */
SetDlgItemText(o.cnn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_CONNECTING));
SetWindowText(o.cnn[config].hwndStatus, LoadLocalizedString(IDS_NFO_CONNECTION_XXX, conn_name));
SetDlgItemText(o.conn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_CONNECTING));
SetWindowText(o.conn[config].hwndStatus, LoadLocalizedString(IDS_NFO_CONNECTION_XXX, conn_name));
if (o.silent_connection[0]=='0')
ShowWindow(o.cnn[config].hwndStatus, SW_SHOW);
ShowWindow(o.conn[config].hwndStatus, SW_SHOW);
}
@ -948,7 +934,7 @@ void ThreadOpenVPNStatus(int config)
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
if(!IsDialogMessage(o.cnn[config].hwndStatus, &messages))
if(!IsDialogMessage(o.conn[config].hwndStatus, &messages))
{
TranslateMessage(&messages);
DispatchMessage(&messages);

View File

@ -33,7 +33,6 @@ BOOL CALLBACK StatusDialogFunc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lP
int AutoStartConnections();
int VerifyAutoConnections();
int CheckVersion();
int CountConnectedState(int CheckVal);
void CheckAndSetTrayIcon();
void SetStatusWinIcon(HWND hwndDlg, int IconID);
void ThreadOpenVPNStatus(int status) __attribute__ ((noreturn));

View File

@ -35,7 +35,7 @@ typedef enum
match_dir
} match_t;
extern struct options o;
extern options_t o;
static match_t
match(const WIN32_FIND_DATA *find, const TCHAR *ext)
@ -65,7 +65,7 @@ ConfigAlreadyExists(TCHAR *newconfig)
int i;
for (i = 0; i < o.num_configs; ++i)
{
if (_tcsicmp(o.cnn[i].config_file, newconfig) == 0)
if (_tcsicmp(o.conn[i].config_file, newconfig) == 0)
return true;
}
return false;
@ -75,7 +75,7 @@ ConfigAlreadyExists(TCHAR *newconfig)
static void
AddConfigFileToList(int config, TCHAR *filename, TCHAR *config_dir)
{
connection_t *conn = &o.cnn[config];
connection_t *conn = &o.conn[config];
int i;
_tcsncpy(conn->config_file, filename, _tsizeof(conn->config_file) - 1);
@ -178,3 +178,39 @@ BuildFileList()
}
}
/*
* Returns TRUE if option exist in config file.
*/
bool
ConfigFileOptionExist(int config, const char *option)
{
FILE *fp;
char line[256];
TCHAR path[MAX_PATH];
_tcsncpy(path, o.conn[config].config_dir, _tsizeof(path));
if (path[_tcslen(path) - 1] != _T('\\'))
_tcscat(path, _T("\\"));
_tcsncat(path, o.conn[config].config_file, _tsizeof(path) - _tcslen(path) - 1);
fp = _tfopen(path, _T("r"));
if (fp == NULL)
{
ShowLocalizedMsg(IDS_ERR_OPEN_CONFIG, path);
return false;
}
while (fgets(line, sizeof(line), fp))
{
if (strncmp(line, option, sizeof(option)) == 0)
{
fclose(fp);
return true;
}
}
fclose(fp);
return false;
}

View File

@ -22,6 +22,9 @@
#ifndef OPENVPN_CONFIG_H
#define OPENVPN_CONFIG_H
#include "main.h"
void BuildFileList();
bool ConfigFileOptionExist(int, const char *);
#endif

View File

@ -38,7 +38,7 @@
#include "tray.h"
#include "localization.h"
extern struct options o;
extern options_t o;
/* Wait for a complete line (CR/LF) and return it.
* Return values:
@ -203,29 +203,29 @@ void monitor_openvpnlog_while_connecting(int config, char *line)
RunConnectScript(config, false);
/* Save time when we got connected. */
o.cnn[config].connected_since = time(NULL);
o.conn[config].connected_since = time(NULL);
o.cnn[config].connect_status = CONNECTED;
SetMenuStatus(config, CONNECTED);
SetTrayIcon(CONNECTED);
o.conn[config].state = connected;
SetMenuStatus(config, connected);
SetTrayIcon(connected);
/* Remove Proxy Auth file */
DeleteFile(o.proxy_authfile);
/* Zero psw attempt counter */
o.cnn[config].failed_psw_attempts = 0;
o.conn[config].failed_psw_attempts = 0;
/* UserInfo: Connected */
SetDlgItemText(o.cnn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_CONNECTED));
SetStatusWinIcon(o.cnn[config].hwndStatus, ID_ICO_CONNECTED);
SetDlgItemText(o.conn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_CONNECTED));
SetStatusWinIcon(o.conn[config].hwndStatus, ID_ICO_CONNECTED);
/* Show Tray Balloon msg */
if (o.show_balloon[0] != '0')
{
LoadLocalizedStringBuf(msg, _tsizeof(msg), IDS_NFO_NOW_CONNECTED, o.cnn[config].config_name);
if (_tcslen(o.cnn[config].ip) > 0)
LoadLocalizedStringBuf(msg, _tsizeof(msg), IDS_NFO_NOW_CONNECTED, o.conn[config].config_name);
if (_tcslen(o.conn[config].ip) > 0)
{
ShowTrayBalloon(msg, LoadLocalizedString(IDS_NFO_ASSIGN_IP, o.cnn[config].ip));
ShowTrayBalloon(msg, LoadLocalizedString(IDS_NFO_ASSIGN_IP, o.conn[config].ip));
}
else
{
@ -234,7 +234,7 @@ void monitor_openvpnlog_while_connecting(int config, char *line)
}
/* Hide Status Window */
ShowWindow(o.cnn[config].hwndStatus, SW_HIDE);
ShowWindow(o.conn[config].hwndStatus, SW_HIDE);
}
/* Check for failed passphrase log message */
@ -244,9 +244,9 @@ void monitor_openvpnlog_while_connecting(int config, char *line)
(strstr(line, "Received AUTH_FAILED control message") != NULL) ||
(strstr(line, "Auth username is empty") != NULL))
{
o.cnn[config].failed_psw_attempts++;
o.cnn[config].failed_psw=1;
o.cnn[config].restart=true;
o.conn[config].failed_psw_attempts++;
o.conn[config].failed_psw=1;
o.conn[config].restart=true;
}
/* Check for "certificate has expired" message */
@ -280,10 +280,10 @@ void monitor_openvpnlog_while_connecting(int config, char *line)
#ifdef _UNICODE
/* Convert the IP address to Unicode */
o.cnn[config].ip[0] = _T('\0');
MultiByteToWideChar(CP_ACP, 0, ip_addr, -1, o.cnn[config].ip, _tsizeof(o.cnn[config].ip));
o.conn[config].ip[0] = _T('\0');
MultiByteToWideChar(CP_ACP, 0, ip_addr, -1, o.conn[config].ip, _tsizeof(o.conn[config].ip));
#else
strncpy(o.cnn[config].ip, ip_addr, sizeof(o.cnn[config].ip));
strncpy(o.conn[config].ip, ip_addr, sizeof(o.conn[config].ip));
#endif
}
}
@ -298,12 +298,12 @@ void monitor_openvpnlog_while_connected(int config, char *line)
if (strstr(line, "process restarting") != NULL)
{
/* Set connect_status = ReConnecting */
o.cnn[config].connect_status = RECONNECTING;
o.conn[config].state = reconnecting;
CheckAndSetTrayIcon();
/* Set Status Window Controls "ReConnecting" */
SetDlgItemText(o.cnn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_RECONNECTING));
SetStatusWinIcon(o.cnn[config].hwndStatus, ID_ICO_CONNECTING);
SetDlgItemText(o.conn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_RECONNECTING));
SetStatusWinIcon(o.conn[config].hwndStatus, ID_ICO_CONNECTING);
}
}
@ -319,20 +319,20 @@ void monitor_openvpnlog_while_reconnecting(int config, char *line)
/* Check for Connected message */
if (strstr(line, o.connect_string) != NULL)
{
o.cnn[config].connect_status = CONNECTED;
SetTrayIcon(CONNECTED);
o.conn[config].state = connected;
SetTrayIcon(connected);
/* Set Status Window Controls "Connected" */
SetDlgItemText(o.cnn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_CONNECTED));
SetStatusWinIcon(o.cnn[config].hwndStatus, ID_ICO_CONNECTED);
SetDlgItemText(o.conn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_CONNECTED));
SetStatusWinIcon(o.conn[config].hwndStatus, ID_ICO_CONNECTED);
/* Show Tray Balloon msg */
if (o.show_balloon[0] == '2')
{
LoadLocalizedStringBuf(msg, _tsizeof(msg), IDS_NFO_NOW_CONNECTED, o.cnn[config].config_name);
if (_tcslen(o.cnn[config].ip) > 0)
LoadLocalizedStringBuf(msg, _tsizeof(msg), IDS_NFO_NOW_CONNECTED, o.conn[config].config_name);
if (_tcslen(o.conn[config].ip) > 0)
{
ShowTrayBalloon(msg, LoadLocalizedString(IDS_NFO_ASSIGN_IP, o.cnn[config].ip));
ShowTrayBalloon(msg, LoadLocalizedString(IDS_NFO_ASSIGN_IP, o.conn[config].ip));
}
else
{
@ -348,9 +348,9 @@ void monitor_openvpnlog_while_reconnecting(int config, char *line)
(strstr(line, "Received AUTH_FAILED control message") != NULL) ||
(strstr(line, "Auth username is empty") != NULL))
{
o.cnn[config].failed_psw_attempts++;
o.cnn[config].failed_psw=1;
o.cnn[config].restart=true;
o.conn[config].failed_psw_attempts++;
o.conn[config].failed_psw=1;
o.conn[config].restart=true;
}
/* Check for "certificate has expired" message */
@ -384,10 +384,10 @@ void monitor_openvpnlog_while_reconnecting(int config, char *line)
#ifdef _UNICODE
/* Convert the IP address to Unicode */
o.cnn[config].ip[0] = _T('\0');
MultiByteToWideChar(CP_ACP, 0, ip_addr, -1, o.cnn[config].ip, _tsizeof(o.cnn[config].ip));
o.conn[config].ip[0] = _T('\0');
MultiByteToWideChar(CP_ACP, 0, ip_addr, -1, o.conn[config].ip, _tsizeof(o.conn[config].ip));
#else
strncpy(o.cnn[config].ip, ip_addr, sizeof(o.cnn[config].ip));
strncpy(o.conn[config].ip, ip_addr, sizeof(o.conn[config].ip));
#endif
}
}
@ -416,25 +416,25 @@ void WatchOpenVPNProcess(int config)
filemode[0] = _T('a');
/* Set Connect_Status = "Connecting" */
o.cnn[config].connect_status = CONNECTING;
o.conn[config].state = connecting;
/* Set Tray Icon = "Connecting" if no other connections are running */
CheckAndSetTrayIcon();
/* Set MenuStatus = "Connecting" */
SetMenuStatus(config, CONNECTING);
SetMenuStatus(config, connecting);
/* Clear failed_password flag */
o.cnn[config].failed_psw = 0;
o.conn[config].failed_psw = 0;
/* Open log file */
if ((fd=_tfopen(o.cnn[config].log_path, filemode)) == NULL)
ShowLocalizedMsg(IDS_ERR_OPEN_LOG_WRITE, o.cnn[config].log_path);
if ((fd=_tfopen(o.conn[config].log_path, filemode)) == NULL)
ShowLocalizedMsg(IDS_ERR_OPEN_LOG_WRITE, o.conn[config].log_path);
LogWindow = GetDlgItem(o.cnn[config].hwndStatus, ID_EDT_LOG);
LogWindow = GetDlgItem(o.conn[config].hwndStatus, ID_EDT_LOG);
while(TRUE)
{
if ((ret=ReadLineFromStdOut(o.cnn[config].hStdOut, config, line)) == 1)
if ((ret=ReadLineFromStdOut(o.conn[config].hStdOut, config, line)) == 1)
{
/* Do nothing if line length = 0 */
@ -469,13 +469,13 @@ void WatchOpenVPNProcess(int config)
SendMessage(LogWindow, EM_REPLACESEL, FALSE, (LPARAM) line);
#endif
if (o.cnn[config].connect_status == CONNECTING) /* Connecting state */
if (o.conn[config].state == connecting) /* Connecting state */
monitor_openvpnlog_while_connecting(config, line);
if (o.cnn[config].connect_status == CONNECTED) /* Connected state */
if (o.conn[config].state == connected) /* Connected state */
monitor_openvpnlog_while_connected(config, line);
if (o.cnn[config].connect_status == RECONNECTING) /* ReConnecting state */
if (o.conn[config].state == reconnecting) /* ReConnecting state */
monitor_openvpnlog_while_reconnecting(config, line);
}
else break;
@ -489,56 +489,56 @@ void WatchOpenVPNProcess(int config)
if (fd != NULL) fclose(fd);
/* Close StdIn/StdOut handles */
CloseHandle(o.cnn[config].hStdIn);
CloseHandle(o.cnn[config].hStdOut);
CloseHandle(o.conn[config].hStdIn);
CloseHandle(o.conn[config].hStdOut);
/* Close exitevent handle */
CloseHandle(o.cnn[config].exit_event);
o.cnn[config].exit_event = NULL;
CloseHandle(o.conn[config].exit_event);
o.conn[config].exit_event = NULL;
/* Enable/Disable menuitems for this connections */
SetMenuStatus(config, DISCONNECTING);
SetMenuStatus(config, disconnecting);
/* Remove Proxy Auth file */
DeleteFile(o.proxy_authfile);
/* Process died outside our control */
if(o.cnn[config].connect_status == CONNECTED)
if(o.conn[config].state == connected)
{
/* Zero psw attempt counter */
o.cnn[config].failed_psw_attempts = 0;
o.conn[config].failed_psw_attempts = 0;
/* Set connect_status = "Not Connected" */
o.cnn[config].connect_status=DISCONNECTED;
o.conn[config].state=disconnected;
/* Change tray icon if no more connections is running */
CheckAndSetTrayIcon();
/* Show Status Window */
SetDlgItemText(o.cnn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_DISCONNECTED));
SetStatusWinIcon(o.cnn[config].hwndStatus, ID_ICO_DISCONNECTED);
EnableWindow(GetDlgItem(o.cnn[config].hwndStatus, ID_DISCONNECT), FALSE);
EnableWindow(GetDlgItem(o.cnn[config].hwndStatus, ID_RESTART), FALSE);
SetForegroundWindow(o.cnn[config].hwndStatus);
ShowWindow(o.cnn[config].hwndStatus, SW_SHOW);
ShowLocalizedMsg(IDS_NFO_CONN_TERMINATED, o.cnn[config].config_name);
SetDlgItemText(o.conn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_DISCONNECTED));
SetStatusWinIcon(o.conn[config].hwndStatus, ID_ICO_DISCONNECTED);
EnableWindow(GetDlgItem(o.conn[config].hwndStatus, ID_DISCONNECT), FALSE);
EnableWindow(GetDlgItem(o.conn[config].hwndStatus, ID_RESTART), FALSE);
SetForegroundWindow(o.conn[config].hwndStatus);
ShowWindow(o.conn[config].hwndStatus, SW_SHOW);
ShowLocalizedMsg(IDS_NFO_CONN_TERMINATED, o.conn[config].config_name);
/* Close Status Window */
SendMessage(o.cnn[config].hwndStatus, WM_CLOSE, 0, 0);
SendMessage(o.conn[config].hwndStatus, WM_CLOSE, 0, 0);
}
/* We have failed to connect */
else if(o.cnn[config].connect_status == CONNECTING)
else if(o.conn[config].state == connecting)
{
/* Set connect_status = "DisConnecting" */
o.cnn[config].connect_status=DISCONNECTING;
o.conn[config].state=disconnecting;
/* Change tray icon if no more connections is running */
CheckAndSetTrayIcon();
if ((o.cnn[config].failed_psw) &&
(o.cnn[config].failed_psw_attempts < o.psw_attempts))
if ((o.conn[config].failed_psw) &&
(o.conn[config].failed_psw_attempts < o.psw_attempts))
{
/* Restart OpenVPN to make another attempt to connect */
PostMessage(o.hWnd, WM_COMMAND, (WPARAM) IDM_CONNECTMENU + config, 0);
@ -546,42 +546,42 @@ void WatchOpenVPNProcess(int config)
else
{
/* Show Status Window */
SetDlgItemText(o.cnn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_FAILED));
SetStatusWinIcon(o.cnn[config].hwndStatus, ID_ICO_DISCONNECTED);
EnableWindow(GetDlgItem(o.cnn[config].hwndStatus, ID_DISCONNECT), FALSE);
EnableWindow(GetDlgItem(o.cnn[config].hwndStatus, ID_RESTART), FALSE);
SetForegroundWindow(o.cnn[config].hwndStatus);
ShowWindow(o.cnn[config].hwndStatus, SW_SHOW);
SetDlgItemText(o.conn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_FAILED));
SetStatusWinIcon(o.conn[config].hwndStatus, ID_ICO_DISCONNECTED);
EnableWindow(GetDlgItem(o.conn[config].hwndStatus, ID_DISCONNECT), FALSE);
EnableWindow(GetDlgItem(o.conn[config].hwndStatus, ID_RESTART), FALSE);
SetForegroundWindow(o.conn[config].hwndStatus);
ShowWindow(o.conn[config].hwndStatus, SW_SHOW);
/* Zero psw attempt counter */
o.cnn[config].failed_psw_attempts = 0;
o.conn[config].failed_psw_attempts = 0;
ShowLocalizedMsg(IDS_NFO_CONN_FAILED, o.cnn[config].config_name);
ShowLocalizedMsg(IDS_NFO_CONN_FAILED, o.conn[config].config_name);
/* Set connect_status = "Not Connected" */
o.cnn[config].connect_status=DISCONNECTED;
o.conn[config].state=disconnected;
/* Close Status Window */
SendMessage(o.cnn[config].hwndStatus, WM_CLOSE, 0, 0);
SendMessage(o.conn[config].hwndStatus, WM_CLOSE, 0, 0);
/* Reset restart flag */
o.cnn[config].restart=false;
o.conn[config].restart=false;
}
}
/* We have failed to reconnect */
else if(o.cnn[config].connect_status == RECONNECTING)
else if(o.conn[config].state == reconnecting)
{
/* Set connect_status = "DisConnecting" */
o.cnn[config].connect_status=DISCONNECTING;
o.conn[config].state=disconnecting;
/* Change tray icon if no more connections is running */
CheckAndSetTrayIcon();
if ((o.cnn[config].failed_psw) &&
(o.cnn[config].failed_psw_attempts < o.psw_attempts))
if ((o.conn[config].failed_psw) &&
(o.conn[config].failed_psw_attempts < o.psw_attempts))
{
/* Restart OpenVPN to make another attempt to connect */
PostMessage(o.hWnd, WM_COMMAND, (WPARAM) IDM_CONNECTMENU + config, 0);
@ -589,42 +589,42 @@ void WatchOpenVPNProcess(int config)
else
{
/* Show Status Window */
SetDlgItemText(o.cnn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_FAILED_RECONN));
SetStatusWinIcon(o.cnn[config].hwndStatus, ID_ICO_DISCONNECTED);
EnableWindow(GetDlgItem(o.cnn[config].hwndStatus, ID_DISCONNECT), FALSE);
EnableWindow(GetDlgItem(o.cnn[config].hwndStatus, ID_RESTART), FALSE);
SetForegroundWindow(o.cnn[config].hwndStatus);
ShowWindow(o.cnn[config].hwndStatus, SW_SHOW);
SetDlgItemText(o.conn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_FAILED_RECONN));
SetStatusWinIcon(o.conn[config].hwndStatus, ID_ICO_DISCONNECTED);
EnableWindow(GetDlgItem(o.conn[config].hwndStatus, ID_DISCONNECT), FALSE);
EnableWindow(GetDlgItem(o.conn[config].hwndStatus, ID_RESTART), FALSE);
SetForegroundWindow(o.conn[config].hwndStatus);
ShowWindow(o.conn[config].hwndStatus, SW_SHOW);
/* Zero psw attempt counter */
o.cnn[config].failed_psw_attempts = 0;
o.conn[config].failed_psw_attempts = 0;
ShowLocalizedMsg(IDS_NFO_RECONN_FAILED, o.cnn[config].config_name);
ShowLocalizedMsg(IDS_NFO_RECONN_FAILED, o.conn[config].config_name);
/* Set connect_status = "Not Connected" */
o.cnn[config].connect_status=DISCONNECTED;
o.conn[config].state=disconnected;
/* Close Status Window */
SendMessage(o.cnn[config].hwndStatus, WM_CLOSE, 0, 0);
SendMessage(o.conn[config].hwndStatus, WM_CLOSE, 0, 0);
/* Reset restart flag */
o.cnn[config].restart=false;
o.conn[config].restart=false;
}
}
/* We have chosed to Disconnect */
else if(o.cnn[config].connect_status == DISCONNECTING)
else if(o.conn[config].state == disconnecting)
{
/* Zero psw attempt counter */
o.cnn[config].failed_psw_attempts = 0;
o.conn[config].failed_psw_attempts = 0;
/* Set connect_status = "Not Connected" */
o.cnn[config].connect_status=DISCONNECTED;
o.conn[config].state=disconnected;
/* Change tray icon if no more connections is running */
CheckAndSetTrayIcon();
if (o.cnn[config].restart)
if (o.conn[config].restart)
{
/* Restart OpenVPN */
StartOpenVPN(config);
@ -632,26 +632,26 @@ void WatchOpenVPNProcess(int config)
else
{
/* Close Status Window */
SendMessage(o.cnn[config].hwndStatus, WM_CLOSE, 0, 0);
SendMessage(o.conn[config].hwndStatus, WM_CLOSE, 0, 0);
}
}
/* We have chosed to Suspend */
else if(o.cnn[config].connect_status == SUSPENDING)
else if(o.conn[config].state == suspending)
{
/* Zero psw attempt counter */
o.cnn[config].failed_psw_attempts = 0;
o.conn[config].failed_psw_attempts = 0;
/* Set connect_status = "SUSPENDED" */
o.cnn[config].connect_status=SUSPENDED;
SetDlgItemText(o.cnn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_SUSPENDED));
o.conn[config].state=suspended;
SetDlgItemText(o.conn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_SUSPENDED));
/* Change tray icon if no more connections is running */
CheckAndSetTrayIcon();
}
/* Enable/Disable menuitems for this connections */
SetMenuStatus(config, DISCONNECTED);
SetMenuStatus(config, disconnected);
}

306
options.c
View File

@ -2,6 +2,7 @@
* OpenVPN-GUI -- A Windows GUI for OpenVPN.
*
* Copyright (C) 2004 Mathias Sundman <mathias@nilings.se>
* 2010 Heiko Hund <heikoh@users.sf.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -31,234 +32,131 @@
#include "openvpn-gui-res.h"
#include "localization.h"
extern struct options o;
#define streq(x, y) (_tcscmp((x), (y)) == 0)
void
init_options (struct options *opt)
{
CLEAR (*opt);
}
int Createargcargv(struct options *options, TCHAR *command_line)
{
int argc;
TCHAR **argv;
TCHAR* arg;
int myindex;
// count the arguments
argc = 0;
arg = command_line;
while (arg[0] != 0) {
while (arg[0] != 0 && arg[0] == ' ') {
arg++;
}
if (arg[0] != 0) {
argc++;
if (arg[0] == '\"') {
arg++;
while (arg[0] != 0 && arg[0] != '\"') {
arg++;
}
}
while (arg[0] != 0 && arg[0] != ' ') {
arg++;
}
}
}
// tokenize the arguments
argv = (TCHAR**) malloc(argc * sizeof(TCHAR*));
arg = command_line;
myindex = 0;
while (arg[0] != 0) {
while (arg[0] != 0 && arg[0] == ' ') {
arg++;
}
if (arg[0] != 0) {
if (arg[0] == '\"') {
arg++;
argv[myindex] = arg;
myindex++;
while (arg[0] != 0 && arg[0] != '\"') {
arg++;
}
if (arg[0] != 0) {
arg[0] = 0;
arg++;
}
while (arg[0] != 0 && arg[0] != ' ') {
arg++;
}
}
else {
argv[myindex] = arg;
myindex++;
while (arg[0] != 0 && arg[0] != ' ') {
arg++;
}
if (arg[0] != 0) {
arg[0] = 0;
arg++;
}
}
}
}
parse_argv(options, argc, argv);
free(argv);
return(0);
}
extern options_t o;
static int
add_option (struct options *options,
int i,
TCHAR *p[])
add_option(options_t *options, int i, TCHAR **p)
{
if (streq (p[0], _T("help")))
if (streq(p[0], _T("help")))
{
TCHAR usagecaption[200];
LoadLocalizedStringBuf(usagecaption, _tsizeof(usagecaption), IDS_NFO_USAGECAPTION);
MessageBox(NULL, LoadLocalizedString(IDS_NFO_USAGE), usagecaption, MB_OK);
TCHAR caption[200];
LoadLocalizedStringBuf(caption, _tsizeof(caption), IDS_NFO_USAGECAPTION);
MessageBox(NULL, LoadLocalizedString(IDS_NFO_USAGE), caption, MB_OK);
exit(0);
}
else if (streq (p[0], _T("connect")) && p[1])
else if (streq(p[0], _T("connect")) && p[1])
{
++i;
static int auto_connect_nr=0;
static int auto_connect_nr = 0;
if (auto_connect_nr == MAX_CONFIGS)
{
/* Too many configs */
ShowLocalizedMsg(IDS_ERR_MANY_CONFIGS, MAX_CONFIGS);
exit(1);
}
options->auto_connect[auto_connect_nr] = p[1];
auto_connect_nr++;
options->auto_connect[auto_connect_nr++] = p[1];
}
else if (streq (p[0], _T("exe_path")) && p[1])
else if (streq(p[0], _T("exe_path")) && p[1])
{
++i;
_tcsncpy(options->exe_path, p[1], _tsizeof(options->exe_path) - 1);
}
else if (streq (p[0], _T("config_dir")) && p[1])
else if (streq(p[0], _T("config_dir")) && p[1])
{
++i;
_tcsncpy(options->config_dir, p[1], _tsizeof(options->config_dir) - 1);
}
else if (streq (p[0], _T("ext_string")) && p[1])
else if (streq(p[0], _T("ext_string")) && p[1])
{
++i;
_tcsncpy(options->ext_string, p[1], _tsizeof(options->ext_string) - 1);
}
else if (streq (p[0], _T("log_dir")) && p[1])
else if (streq(p[0], _T("log_dir")) && p[1])
{
++i;
_tcsncpy(options->log_dir, p[1], _tsizeof(options->log_dir) - 1);
}
else if (streq (p[0], _T("priority_string")) && p[1])
else if (streq(p[0], _T("priority_string")) && p[1])
{
++i;
_tcsncpy(options->priority_string, p[1], _tsizeof(options->priority_string) - 1);
}
else if (streq (p[0], _T("append_string")) && p[1])
else if (streq(p[0], _T("append_string")) && p[1])
{
++i;
_tcsncpy(options->append_string, p[1], _tsizeof(options->append_string) - 1);
}
else if (streq (p[0], _T("log_viewer")) && p[1])
else if (streq(p[0], _T("log_viewer")) && p[1])
{
++i;
_tcsncpy(options->log_viewer, p[1], _tsizeof(options->log_viewer) - 1);
}
else if (streq (p[0], _T("editor")) && p[1])
else if (streq(p[0], _T("editor")) && p[1])
{
++i;
_tcsncpy(options->editor, p[1], _tsizeof(options->editor) - 1);
}
else if (streq (p[0], _T("allow_edit")) && p[1])
else if (streq(p[0], _T("allow_edit")) && p[1])
{
++i;
_tcsncpy(options->allow_edit, p[1], _tsizeof(options->allow_edit) - 1);
}
else if (streq (p[0], _T("allow_service")) && p[1])
else if (streq(p[0], _T("allow_service")) && p[1])
{
++i;
_tcsncpy(options->allow_service, p[1], _tsizeof(options->allow_service) - 1);
}
else if (streq (p[0], _T("allow_password")) && p[1])
else if (streq(p[0], _T("allow_password")) && p[1])
{
++i;
_tcsncpy(options->allow_password, p[1], _tsizeof(options->allow_password) - 1);
}
else if (streq (p[0], _T("allow_proxy")) && p[1])
else if (streq(p[0], _T("allow_proxy")) && p[1])
{
++i;
_tcsncpy(options->allow_proxy, p[1], _tsizeof(options->allow_proxy) - 1);
}
else if (streq (p[0], _T("show_balloon")) && p[1])
else if (streq(p[0], _T("show_balloon")) && p[1])
{
++i;
_tcsncpy(options->show_balloon, p[1], _tsizeof(options->show_balloon) - 1);
}
else if (streq (p[0], _T("service_only")) && p[1])
else if (streq(p[0], _T("service_only")) && p[1])
{
++i;
_tcsncpy(options->service_only, p[1], _tsizeof(options->service_only) - 1);
}
else if (streq (p[0], _T("show_script_window")) && p[1])
else if (streq(p[0], _T("show_script_window")) && p[1])
{
++i;
_tcsncpy(options->show_script_window, p[1], _tsizeof(options->show_script_window) - 1);
}
else if (streq (p[0], _T("silent_connection")) && p[1])
else if (streq(p[0], _T("silent_connection")) && p[1])
{
++i;
_tcsncpy(options->silent_connection, p[1], _tsizeof(options->silent_connection) - 1);
}
else if (streq (p[0], _T("passphrase_attempts")) && p[1])
else if (streq(p[0], _T("passphrase_attempts")) && p[1])
{
++i;
_tcsncpy(options->psw_attempts_string, p[1], _tsizeof(options->psw_attempts_string) - 1);
}
else if (streq (p[0], _T("connectscript_timeout")) && p[1])
else if (streq(p[0], _T("connectscript_timeout")) && p[1])
{
++i;
_tcsncpy(options->connectscript_timeout_string, p[1], _tsizeof(options->connectscript_timeout_string) - 1);
}
else if (streq (p[0], _T("disconnectscript_timeout")) && p[1])
else if (streq(p[0], _T("disconnectscript_timeout")) && p[1])
{
++i;
_tcsncpy(options->disconnectscript_timeout_string, p[1],
_tsizeof(options->disconnectscript_timeout_string) - 1);
_tcsncpy(options->disconnectscript_timeout_string, p[1], _tsizeof(options->disconnectscript_timeout_string) - 1);
}
else if (streq (p[0], _T("preconnectscript_timeout")) && p[1])
else if (streq(p[0], _T("preconnectscript_timeout")) && p[1])
{
++i;
_tcsncpy(options->preconnectscript_timeout_string, p[1],
_tsizeof(options->preconnectscript_timeout_string) - 1);
_tcsncpy(options->preconnectscript_timeout_string, p[1], _tsizeof(options->preconnectscript_timeout_string) - 1);
}
else
{
@ -269,10 +167,9 @@ add_option (struct options *options,
return i;
}
void
parse_argv (struct options* options,
int argc,
TCHAR *argv[])
static void
parse_argv(options_t *options, int argc, TCHAR **argv)
{
int i, j;
@ -280,15 +177,15 @@ parse_argv (struct options* options,
for (i = 1; i < argc; ++i)
{
TCHAR *p[MAX_PARMS];
CLEAR (p);
CLEAR(p);
p[0] = argv[i];
if (_tcsncmp(p[0], _T("--"), 2))
if (_tcsncmp(p[0], _T("--"), 2) != 0)
{
/* Missing -- before option. */
ShowLocalizedMsg(IDS_ERR_BAD_PARAMETER, p[0]);
exit(0);
}
else
p[0] += 2;
for (j = 1; j < MAX_PARMS; ++j)
@ -296,48 +193,101 @@ parse_argv (struct options* options,
if (i + j < argc)
{
TCHAR *arg = argv[i + j];
if (_tcsncmp (arg, _T("--"), 2))
p[j] = arg;
else
if (_tcsncmp(arg, _T("--"), 2) == 0)
break;
p[j] = arg;
}
}
i = add_option (options, i, p);
i = add_option(options, i, p);
}
}
/*
* Returns TRUE if option exist in config file.
*/
int ConfigFileOptionExist(int config, const char *option)
void
InitOptions(options_t *opt)
{
FILE *fp;
char line[256];
TCHAR configfile_path[MAX_PATH];
_tcsncpy(configfile_path, o.cnn[config].config_dir, _tsizeof(configfile_path));
if (configfile_path[_tcslen(configfile_path) - 1] != _T('\\'))
_tcscat(configfile_path, _T("\\"));
_tcsncat(configfile_path, o.cnn[config].config_file,
_tsizeof(configfile_path) - _tcslen(configfile_path) - 1);
if (!(fp=_tfopen(configfile_path, _T("r"))))
{
/* can't open config file */
ShowLocalizedMsg(IDS_ERR_OPEN_CONFIG, configfile_path);
return(0);
}
while (fgets(line, sizeof (line), fp))
{
if ((strncmp(line, option, sizeof(option)) == 0))
{
fclose(fp);
return(1);
}
}
fclose(fp);
return(0);
CLEAR(*opt);
}
void
ProcessCommandLine(options_t *options, TCHAR *command_line)
{
TCHAR **argv;
TCHAR *pos = command_line;
int argc = 0;
/* Count the arguments */
do
{
while (*pos == _T(' '))
++pos;
if (*pos == _T('\0'))
break;
++argc;
while (*pos != _T('\0') && *pos != _T(' '))
++pos;
}
while (*pos != _T('\0'));
if (argc == 0)
return;
/* Tokenize the arguments */
argv = (TCHAR**) malloc(argc * sizeof(TCHAR*));
pos = command_line;
argc = 0;
do
{
while (*pos == _T(' '))
pos++;
if (*pos == _T('\0'))
break;
if (*pos == _T('\"'))
{
argv[argc++] = ++pos;
while (*pos != _T('\0') && *pos != _T('\"'))
++pos;
}
else
{
argv[argc++] = pos;
while (*pos != _T('\0') && *pos != _T(' '))
++pos;
}
if (*pos == _T('\0'))
break;
*pos++ = _T('\0');
}
while (*pos != _T('\0'));
parse_argv(options, argc, argv);
free(argv);
}
/* Return num of connections with state = check */
int
CountConnState(conn_state_t check)
{
int i;
int count = 0;
for (i = 0; i < o.num_configs; ++i)
{
if (o.conn[i].state == check)
++count;
}
return count;
}

115
options.h
View File

@ -2,6 +2,7 @@
* OpenVPN-GUI -- A Windows GUI for OpenVPN.
*
* Copyright (C) 2004 Mathias Sundman <mathias@nilings.se>
* 2010 Heiko Hund <heikoh@users.sf.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -20,6 +21,9 @@
*
*/
#ifndef OPTIONS_H
#define OPTIONS_H
#include <windows.h>
#include <time.h>
@ -27,68 +31,89 @@
* Maximum number of parameters associated with an option,
* including the option name itself.
*/
#define MAX_PARMS 5
#define MAX_CONFIGS 50 /* Max number of config-files to read. */
#define MAX_CONFIG_SUBDIRS 50 /* Max number of subdirs to scan */
#define MAX_PARMS 5 /* May number of parameters per option */
#define MAX_CONFIGS 50 /* Max number of config-files to read */
#define MAX_CONFIG_SUBDIRS 50 /* Max number of subdirs to scan for configs */
/* connect_status STATES */
#define DISCONNECTED 0
#define CONNECTING 1
#define CONNECTED 2
#define RECONNECTING 3
#define DISCONNECTING 4
#define SUSPENDING 5
#define SUSPENDED 6
/* OpenVPN Service STATES */
#define SERVICE_NOACCESS -1
#define SERVICE_DISCONNECTED 0
#define SERVICE_CONNECTING 1
#define SERVICE_CONNECTED 2
/* connection states */
typedef enum {
disconnected,
connecting,
reconnecting,
connected,
disconnecting,
suspending,
suspended
} conn_state_t;
/* Connections parameters */
typedef struct
{
typedef struct {
TCHAR config_file[MAX_PATH]; /* Name of the config file */
TCHAR config_name[MAX_PATH]; /* Name of the connection */
TCHAR config_dir[MAX_PATH]; /* Path to this configs dir */
TCHAR log_path[MAX_PATH]; /* Path to Logfile */
TCHAR ip[16]; /* Assigned IP address for this connection */
TCHAR exit_event_name[50]; /* Exit Event name for this connection */
int connect_status; /* 0=Not Connected 1=Connecting
2=Connected 3=Reconnecting 4=Disconnecting */
int auto_connect; /* true=AutoConnect at startup */
int failed_psw; /* 1=OpenVPN failed because of wrong psw */
int failed_psw_attempts; /* # of failed attempts maid to enter psw */
int restart; /* true=Restart connection after termination */
BOOL auto_connect; /* AutoConnect at startup id TRUE */
BOOL restart; /* Restart connection after termination if TRUE*/
BOOL failed_psw; /* TRUE if OpenVPN failed because of wrong psw */
conn_state_t state; /* State the connection currently is in */
int failed_psw_attempts; /* # of failed attempts made to enter psw */
time_t connected_since; /* Time when the connection was established */
HANDLE exit_event;
HANDLE hProcess;
HANDLE hStdOut;
HANDLE hStdIn;
HWND hwndStatus; /* Handle to Status Dialog Window */
HWND hwndStatus;
} connection_t;
typedef enum {
service_noaccess = -1,
service_disconnected = 0,
service_connecting = 1,
service_connected = 2
} service_state_t;
typedef enum {
config,
browser,
manual
} proxy_source_t;
typedef enum {
http,
socks
} proxy_t;
/* All options used within OpenVPN GUI */
struct options
{
typedef struct {
/* Array of configs to autostart */
const TCHAR *auto_connect[MAX_CONFIGS];
/* Connection parameters */
connection_t cnn[MAX_CONFIGS]; /* Connection structure */
connection_t conn[MAX_CONFIGS]; /* Connection structure */
int num_configs; /* Number of configs */
int oldversion; /* 1=OpenVPN version below 2.0-beta6 */
BOOL oldversion; /* OpenVPN version below 2.0-beta6 if TRUE */
service_state_t service_state; /* State of the OpenVPN Service */
char connect_string[100]; /* String to look for to report connected */
int psw_attempts; /* Number of psw attemps to allow */
int connectscript_timeout; /* Connect Script execution timeout (sec) */
int disconnectscript_timeout; /* Disconnect Script execution timeout (sec) */
int preconnectscript_timeout; /* Preconnect Script execution timeout (sec) */
int service_running; /* true if OpenVPN Service is started */
HWND hWnd; /* Main Window Handle */
HINSTANCE hInstance;
/* Proxy Settings */
proxy_source_t proxy_source; /* Where to get proxy information from */
proxy_t proxy_type; /* The type of proxy to use */
BOOL proxy_http_auth; /* TRUE is proxy authentication is used */
TCHAR proxy_http_address[100]; /* HTTP Proxy Address */
TCHAR proxy_http_port[6]; /* HTTP Proxy Port */
TCHAR proxy_socks_address[100]; /* SOCKS Proxy Address */
TCHAR proxy_socks_port[6]; /* SOCKS Proxy Address */
TCHAR proxy_authfile[100]; /* Path to proxy auth file */
/* Registry values */
TCHAR exe_path[MAX_PATH];
@ -113,24 +138,16 @@ struct options
TCHAR disconnectscript_timeout_string[4];
TCHAR preconnectscript_timeout_string[4];
/* Proxy Settings */
int proxy_source; /* 0=OpenVPN config, 1=IE, 2=Manual */
int proxy_type; /* 0=HTTP, 1=SOCKS */
int proxy_http_auth; /* 0=Auth Disabled, 1=Auth Enabled */
TCHAR proxy_http_address[100]; /* HTTP Proxy Address */
TCHAR proxy_http_port[6]; /* HTTP Proxy Port */
TCHAR proxy_socks_address[100]; /* SOCKS Proxy Address */
TCHAR proxy_socks_port[6]; /* SOCKS Proxy Address */
TCHAR proxy_authfile[100]; /* Path to proxy auth file */
/* Debug file pointer */
#ifdef DEBUG
FILE *debug_fp;
#endif
};
#define streq(x, y) (!_tcscmp((x), (y)))
void init_options (struct options *o);
int Createargcargv(struct options* options, TCHAR* command_line);
void parse_argv (struct options* options, int argc, TCHAR *argv[]);
int ConfigFileOptionExist(int config, const char *option);
HWND hWnd;
HINSTANCE hInstance;
} options_t;
void InitOptions(options_t *);
void ProcessCommandLine(options_t *, TCHAR *);
int CountConnState(conn_state_t);
#endif

View File

@ -37,7 +37,7 @@
#endif
WCHAR passphrase[256];
extern struct options o;
extern options_t o;
int ConvertUnicode2Ascii(WCHAR str_unicode[], char str_ascii[], unsigned int str_ascii_size)
{
@ -85,14 +85,14 @@ void CheckPrivateKeyPassphrasePrompt (char *line, int config)
CLEAR(passphrase_ascii);
ConvertUnicode2Ascii(passphrase, passphrase_ascii, sizeof(passphrase_ascii));
if (!WriteFile(o.cnn[config].hStdIn, passphrase_ascii,
if (!WriteFile(o.conn[config].hStdIn, passphrase_ascii,
strlen(passphrase_ascii), &nCharsWritten, NULL))
{
/* PassPhrase -> stdin failed */
ShowLocalizedMsg(IDS_ERR_PASSPHRASE2STDIN);
}
}
if (!WriteFile(o.cnn[config].hStdIn, "\r\n",
if (!WriteFile(o.conn[config].hStdIn, "\r\n",
2, &nCharsWritten, NULL))
{
/* CR -> stdin failed */
@ -120,7 +120,7 @@ void CheckPrivateKeyPassphrasePrompt (char *line, int config)
CLEAR(passphrase_ascii);
ConvertUnicode2Ascii(passphrase, passphrase_ascii, sizeof(passphrase_ascii));
if (!WriteFile(o.cnn[config].hStdIn, passphrase_ascii,
if (!WriteFile(o.conn[config].hStdIn, passphrase_ascii,
strlen(passphrase_ascii), &nCharsWritten, NULL))
{
/* PassPhrase -> stdin failed */
@ -129,7 +129,7 @@ void CheckPrivateKeyPassphrasePrompt (char *line, int config)
}
else
{
if (!WriteFile(o.cnn[config].hStdIn, "\n",
if (!WriteFile(o.conn[config].hStdIn, "\n",
1, &nCharsWritten, NULL))
{
/* CR -> stdin failed */
@ -163,7 +163,7 @@ void CheckAuthUsernamePrompt (char *line, int config)
if (strlen(user_auth.username) > 0)
{
if (!WriteFile(o.cnn[config].hStdIn, user_auth.username,
if (!WriteFile(o.conn[config].hStdIn, user_auth.username,
strlen(user_auth.username), &nCharsWritten, NULL))
{
ShowLocalizedMsg(IDS_ERR_AUTH_USERNAME2STDIN);
@ -171,7 +171,7 @@ void CheckAuthUsernamePrompt (char *line, int config)
}
else
{
if (!WriteFile(o.cnn[config].hStdIn, "\n",
if (!WriteFile(o.conn[config].hStdIn, "\n",
1, &nCharsWritten, NULL))
{
ShowLocalizedMsg(IDS_ERR_CR2STDIN);
@ -180,7 +180,7 @@ void CheckAuthUsernamePrompt (char *line, int config)
if (strlen(user_auth.password) > 0)
{
if (!WriteFile(o.cnn[config].hStdIn, user_auth.password,
if (!WriteFile(o.conn[config].hStdIn, user_auth.password,
strlen(user_auth.password), &nCharsWritten, NULL))
{
ShowLocalizedMsg(IDS_ERR_AUTH_PASSWORD2STDIN);
@ -188,7 +188,7 @@ void CheckAuthUsernamePrompt (char *line, int config)
}
else
{
if (!WriteFile(o.cnn[config].hStdIn, "\n",
if (!WriteFile(o.conn[config].hStdIn, "\n",
1, &nCharsWritten, NULL))
{
ShowLocalizedMsg(IDS_ERR_CR2STDIN);
@ -331,7 +331,7 @@ void ChangePassphraseThread(int config)
int keyfile_format=0;
/* Cut of extention from config filename. */
_tcsncpy(conn_name, o.cnn[config].config_file, _tsizeof(conn_name));
_tcsncpy(conn_name, o.conn[config].config_file, _tsizeof(conn_name));
conn_name[_tcslen(conn_name) - (_tcslen(o.ext_string)+1)]=0;
/* Get Key filename from config file */
@ -594,7 +594,7 @@ int ParseKeyFilenameLine(int config, TCHAR *keyfilename, size_t keyfilenamesize,
/* Prepend filename with configdir path if needed */
if ((keyfilename[0] != '\\') && (keyfilename[0] != '/') && (keyfilename[1] != ':'))
{
_tcsncpy(temp_filename, o.cnn[config].config_dir, _tsizeof(temp_filename));
_tcsncpy(temp_filename, o.conn[config].config_dir, _tsizeof(temp_filename));
if (temp_filename[_tcslen(temp_filename) - 1] != '\\')
_tcscat(temp_filename, _T("\\"));
_tcsncat(temp_filename, keyfilename,
@ -820,10 +820,10 @@ int GetKeyFilename(int config, TCHAR *keyfilename, size_t keyfilenamesize, int *
int found_pkcs12=0;
TCHAR configfile_path[MAX_PATH];
_tcsncpy(configfile_path, o.cnn[config].config_dir, _tsizeof(configfile_path));
_tcsncpy(configfile_path, o.conn[config].config_dir, _tsizeof(configfile_path));
if (!(configfile_path[_tcslen(configfile_path)-1] == '\\'))
_tcscat(configfile_path, _T("\\"));
_tcsncat(configfile_path, o.cnn[config].config_file,
_tcsncat(configfile_path, o.conn[config].config_file,
_tsizeof(configfile_path) - _tcslen(configfile_path) - 1);
if (!(fp=_tfopen(configfile_path, _T("r"))))

42
proxy.c
View File

@ -33,7 +33,7 @@
#include "openvpn-gui-res.h"
#include "localization.h"
extern struct options o;
extern options_t o;
bool CALLBACK ProxySettingsDialogFunc (HWND hwndDlg, UINT msg, WPARAM wParam, UNUSED LPARAM lParam)
{
@ -181,7 +181,7 @@ int CheckProxySettings(HWND hwndDlg)
void LoadProxySettings(HWND hwndDlg)
{
/* Set Proxy type, address and port */
if (o.proxy_type == 0) /* HTTP Proxy */
if (o.proxy_type == http) /* HTTP Proxy */
{
CheckRadioButton(hwndDlg, ID_RB_PROXY_HTTP, ID_RB_PROXY_SOCKS, ID_RB_PROXY_HTTP);
SetDlgItemText(hwndDlg, ID_EDT_PROXY_ADDRESS, o.proxy_http_address);
@ -197,11 +197,11 @@ void LoadProxySettings(HWND hwndDlg)
if (o.proxy_http_auth) CheckDlgButton(hwndDlg, ID_CB_PROXY_AUTH, BST_CHECKED);
/* Set Proxy Settings Source */
if (o.proxy_source == 0)
if (o.proxy_source == config)
SendMessage(GetDlgItem(hwndDlg, ID_RB_PROXY_OPENVPN), BM_CLICK, 0, 0);
if (o.proxy_source == 1)
if (o.proxy_source == browser)
SendMessage(GetDlgItem(hwndDlg, ID_RB_PROXY_MSIE), BM_CLICK, 0, 0);
if (o.proxy_source == 2)
if (o.proxy_source == manual)
SendMessage(GetDlgItem(hwndDlg, ID_RB_PROXY_MANUAL), BM_CLICK, 0, 0);
}
@ -216,24 +216,24 @@ void SaveProxySettings(HWND hwndDlg)
/* Save Proxy Settings Source */
if (IsDlgButtonChecked(hwndDlg, ID_RB_PROXY_OPENVPN) == BST_CHECKED)
{
o.proxy_source = 0;
o.proxy_source = config;
proxy_source_string[0] = _T('0');
}
if (IsDlgButtonChecked(hwndDlg, ID_RB_PROXY_MSIE) == BST_CHECKED)
{
o.proxy_source = 1;
o.proxy_source = browser;
proxy_source_string[0] = _T('1');
}
if (IsDlgButtonChecked(hwndDlg, ID_RB_PROXY_MANUAL) == BST_CHECKED)
{
o.proxy_source = 2;
o.proxy_source = manual;
proxy_source_string[0] = _T('2');
}
/* Save Proxy type, address and port */
if (IsDlgButtonChecked(hwndDlg, ID_RB_PROXY_HTTP) == BST_CHECKED)
{
o.proxy_type = 0;
o.proxy_type = http;
proxy_type_string[0] = _T('0');
GetDlgItemText(hwndDlg, ID_EDT_PROXY_ADDRESS, o.proxy_http_address,
@ -247,7 +247,7 @@ void SaveProxySettings(HWND hwndDlg)
}
else
{
o.proxy_type = 1;
o.proxy_type = socks;
proxy_type_string[0] = _T('1');
GetDlgItemText(hwndDlg, ID_EDT_PROXY_ADDRESS, o.proxy_socks_address,
@ -332,17 +332,17 @@ void GetProxyRegistrySettings()
_tsizeof(proxy_http_auth_string));
if (proxy_source_string[0] == _T('1'))
o.proxy_source=1;
o.proxy_source = config;
if (proxy_source_string[0] == _T('2'))
o.proxy_source=2;
o.proxy_source = browser;
if (proxy_source_string[0] == _T('3'))
o.proxy_source=3;
o.proxy_source = manual;
if (proxy_type_string[0] == _T('1'))
o.proxy_type=1;
o.proxy_type = socks;
if (proxy_http_auth_string[0] == _T('1'))
o.proxy_http_auth=1;
o.proxy_http_auth = TRUE;
RegCloseKey(regkey);
}
@ -467,11 +467,11 @@ void ConstructProxyCmdLine(TCHAR *proxy_string_ptr, unsigned int size)
CLEAR(proxy_string);
if (o.proxy_source == PROXY_SOURCE_MANUAL)
if (o.proxy_source == manual)
{
if (o.proxy_type == PROXY_TYPE_HTTP)
if (o.proxy_type == http)
{
if (o.proxy_http_auth == PROXY_HTTP_ASK_AUTH)
if (o.proxy_http_auth)
{
/* Ask for Proxy username/password */
LocalizedDialogBox(ID_DLG_PROXY_AUTH, ProxyAuthDialogFunc);
@ -487,7 +487,7 @@ void ConstructProxyCmdLine(TCHAR *proxy_string_ptr, unsigned int size)
o.proxy_http_port);
}
}
if (o.proxy_type == PROXY_TYPE_SOCKS)
if (o.proxy_type == socks)
{
_sntprintf_0(proxy_string, _T("--socks-proxy %s %s"),
o.proxy_socks_address,
@ -495,7 +495,7 @@ void ConstructProxyCmdLine(TCHAR *proxy_string_ptr, unsigned int size)
}
}
else if (o.proxy_source == PROXY_SOURCE_IE)
else if (o.proxy_source == browser)
{
TCHAR host[64];
TCHAR port[6];
@ -504,7 +504,7 @@ void ConstructProxyCmdLine(TCHAR *proxy_string_ptr, unsigned int size)
if (GetIeHttpProxy(host, &hostlen, port, &portlen) && hostlen != 0)
{
if (o.proxy_http_auth == PROXY_HTTP_ASK_AUTH)
if (o.proxy_http_auth)
{
/* Ask for Proxy username/password */
LocalizedDialogBox(ID_DLG_PROXY_AUTH, ProxyAuthDialogFunc);

View File

@ -19,14 +19,6 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define PROXY_SOURCE_OPENVPN 0
#define PROXY_SOURCE_IE 1
#define PROXY_SOURCE_MANUAL 2
#define PROXY_TYPE_HTTP 0
#define PROXY_TYPE_SOCKS 1
#define PROXY_HTTP_NO_AUTH 0
#define PROXY_HTTP_ASK_AUTH 1
BOOL CALLBACK ProxySettingsDialogFunc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);
int CheckProxySettings(HWND hwndDlg);
void LoadProxySettings(HWND hwndDlg);

View File

@ -32,7 +32,7 @@
#include "registry.h"
#include "localization.h"
extern struct options o;
extern options_t o;
int
GetRegistryKeys()

View File

@ -29,7 +29,7 @@
#include "options.h"
#include "localization.h"
extern struct options o;
extern options_t o;
void RunConnectScript(int config, int run_as_service)
{
@ -45,10 +45,10 @@ void RunConnectScript(int config, int run_as_service)
int i, TimeOut;
/* Cut of extention from config filename and add "_up.bat". */
_tcsncpy(batch_file, o.cnn[config].config_file, _tsizeof(batch_file));
_tcsncpy(batch_file, o.conn[config].config_file, _tsizeof(batch_file));
batch_file[_tcslen(batch_file) - (_tcslen(o.ext_string)+1)]=0;
_tcsncat(batch_file, _T("_up.bat"), _tsizeof(batch_file) - _tcslen(batch_file) - 1);
_sntprintf_0(command_line, _T("%s\\%s"), o.cnn[config].config_dir, batch_file);
_sntprintf_0(command_line, _T("%s\\%s"), o.conn[config].config_dir, batch_file);
/* Return if no script exists */
@ -63,7 +63,7 @@ void RunConnectScript(int config, int run_as_service)
if (!run_as_service)
{
/* UserInfo: Connect Script running */
SetDlgItemText(o.cnn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_CONN_SCRIPT));
SetDlgItemText(o.conn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_CONN_SCRIPT));
}
CLEAR (start_info);
@ -87,7 +87,7 @@ void RunConnectScript(int config, int run_as_service)
((o.show_script_window[0] == '1') ? (DWORD) CREATE_NEW_CONSOLE :
(DWORD) CREATE_NO_WINDOW),
NULL,
o.cnn[config].config_dir, //start-up dir
o.conn[config].config_dir, //start-up dir
&start_info,
&proc_info))
{
@ -146,9 +146,9 @@ void RunDisconnectScript(int config, int run_as_service)
int i, TimeOut;
/* Append "_down.bat" to config name. */
_tcsncpy(batch_file, o.cnn[config].config_name, _tsizeof(batch_file));
_tcsncpy(batch_file, o.conn[config].config_name, _tsizeof(batch_file));
_tcsncat(batch_file, _T("_down.bat"), _tsizeof(batch_file) - _tcslen(batch_file) - 1);
_sntprintf_0(command_line, _T("%s\\%s"), o.cnn[config].config_dir, batch_file);
_sntprintf_0(command_line, _T("%s\\%s"), o.conn[config].config_dir, batch_file);
/* Return if no script exists */
@ -163,7 +163,7 @@ void RunDisconnectScript(int config, int run_as_service)
if (!run_as_service)
{
/* UserInfo: Disconnect Script running */
SetDlgItemText(o.cnn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_DISCONN_SCRIPT));
SetDlgItemText(o.conn[config].hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_DISCONN_SCRIPT));
}
CLEAR (start_info);
@ -187,7 +187,7 @@ void RunDisconnectScript(int config, int run_as_service)
((o.show_script_window[0] == '1') ? (DWORD) CREATE_NEW_CONSOLE :
(DWORD) CREATE_NO_WINDOW),
NULL,
o.cnn[config].config_dir, //start-up dir
o.conn[config].config_dir, //start-up dir
&start_info,
&proc_info))
{
@ -226,9 +226,9 @@ void RunPreconnectScript(int config)
int i, TimeOut;
/* Append "_pre.bat" to config name. */
_tcsncpy(batch_file, o.cnn[config].config_name, _tsizeof(batch_file));
_tcsncpy(batch_file, o.conn[config].config_name, _tsizeof(batch_file));
_tcsncat(batch_file, _T("_pre.bat"), _tsizeof(batch_file) - _tcslen(batch_file) - 1);
_sntprintf_0(command_line, _T("%s\\%s"), o.cnn[config].config_dir, batch_file);
_sntprintf_0(command_line, _T("%s\\%s"), o.conn[config].config_dir, batch_file);
/* Return if no script exists */
@ -261,7 +261,7 @@ void RunPreconnectScript(int config)
((o.show_script_window[0] == '1') ? (DWORD) CREATE_NEW_CONSOLE :
(DWORD) CREATE_NO_WINDOW),
NULL,
o.cnn[config].config_dir, //start-up dir
o.conn[config].config_dir, //start-up dir
&start_info,
&proc_info))
{

View File

@ -32,7 +32,7 @@
#include "openvpn-gui-res.h"
#include "localization.h"
extern struct options o;
extern options_t o;
int MyStartService()
{
@ -46,7 +46,7 @@ int MyStartService()
int i;
/* Set Service Status = Connecting */
o.service_running = SERVICE_CONNECTING;
o.service_state = service_connecting;
SetServiceMenuStatus();
CheckAndSetTrayIcon();
@ -157,7 +157,7 @@ int MyStartService()
RunConnectScript(i, true);
/* Set Service Status = Connected */
o.service_running = SERVICE_CONNECTED;
o.service_state = service_connected;
SetServiceMenuStatus();
CheckAndSetTrayIcon();
@ -169,7 +169,7 @@ int MyStartService()
failed:
/* Set Service Status = Disconnecting */
o.service_running = SERVICE_DISCONNECTED;
o.service_state = service_disconnected;
SetServiceMenuStatus();
CheckAndSetTrayIcon();
return(false);
@ -220,7 +220,7 @@ int MyStopService()
return(false);
}
o.service_running = SERVICE_DISCONNECTED;
o.service_state = service_disconnected;
SetServiceMenuStatus();
CheckAndSetTrayIcon();
return(true);
@ -253,7 +253,7 @@ int CheckServiceStatus()
SC_MANAGER_CONNECT); // Connect rights
if (NULL == schSCManager) {
o.service_running = SERVICE_NOACCESS;
o.service_state = service_noaccess;
SetServiceMenuStatus();
return(false);
}
@ -266,7 +266,7 @@ int CheckServiceStatus()
if (schService == NULL) {
/* open vpn service failed */
ShowLocalizedMsg(IDS_ERR_OPEN_VPN_SERVICE);
o.service_running = SERVICE_NOACCESS;
o.service_state = service_noaccess;
SetServiceMenuStatus();
return(false);
}
@ -282,16 +282,16 @@ int CheckServiceStatus()
if (ssStatus.dwCurrentState == SERVICE_RUNNING)
{
o.service_running = SERVICE_CONNECTED;
o.service_state = service_connected;
SetServiceMenuStatus();
SetTrayIcon(CONNECTED);
SetTrayIcon(connected);
return(true);
}
else
{
o.service_running = SERVICE_DISCONNECTED;
o.service_state = service_disconnected;
SetServiceMenuStatus();
SetTrayIcon(DISCONNECTED);
SetTrayIcon(disconnected);
return(false);
}
}

71
tray.c
View File

@ -50,7 +50,7 @@ HMENU hMenuConn[MAX_CONFIGS];
HMENU hMenuService;
NOTIFYICONDATA ni;
extern struct options o;
extern options_t o;
// Mouse clicks on tray
void OnNotifyTray(LPARAM lParam)
@ -63,7 +63,7 @@ void OnNotifyTray(LPARAM lParam)
switch(lParam) {
case WM_RBUTTONDOWN:
/* Re-read configs and re-create menus if no connection is running */
if (CountConnectedState(DISCONNECTED) == o.num_configs)
if (CountConnState(disconnected) == o.num_configs)
{
DestroyPopupMenus();
BuildFileList();
@ -82,11 +82,11 @@ void OnNotifyTray(LPARAM lParam)
if (o.service_only[0]=='1')
{
/* Start OpenVPN Service */
if (o.service_running == SERVICE_DISCONNECTED)
if (o.service_state == service_disconnected)
{
MyStartService();
}
else if (o.service_running == SERVICE_CONNECTED)
else if (o.service_state == service_connected)
{
/* Stop OpenVPN service */
if (MessageBox(NULL, LoadLocalizedString(IDS_MENU_ASK_STOP_SERVICE), _T(PACKAGE_NAME), MB_YESNO | MB_SETFOREGROUND) == IDYES)
@ -101,7 +101,7 @@ void OnNotifyTray(LPARAM lParam)
connected_config = -1;
for (i=0; i < o.num_configs; i++)
{
if(o.cnn[i].connect_status != DISCONNECTED)
if(o.conn[i].state != disconnected)
{
if (connected_config == -1)
{
@ -116,19 +116,19 @@ void OnNotifyTray(LPARAM lParam)
}
if (connected_config != -1)
{
ShowWindow(o.cnn[connected_config].hwndStatus, SW_SHOW);
SetForegroundWindow(o.cnn[connected_config].hwndStatus);
ShowWindow(o.conn[connected_config].hwndStatus, SW_SHOW);
SetForegroundWindow(o.conn[connected_config].hwndStatus);
}
/* Re-read configs and re-create menus if no connection is running */
if (CountConnectedState(DISCONNECTED) == o.num_configs)
if (CountConnState(disconnected) == o.num_configs)
{
DestroyPopupMenus();
BuildFileList();
CreatePopupMenus();
/* Start connection if only one config exist */
if ((o.num_configs == 1) && (o.cnn[0].connect_status == DISCONNECTED))
if ((o.num_configs == 1) && (o.conn[0].state == disconnected))
StartOpenVPN(0);
}
}
@ -152,7 +152,6 @@ void OnDestroyTray()
void CreatePopupMenus()
{
int i;
//extern struct options o;
/* Create Main menu */
hMenu=CreatePopupMenu();
@ -230,14 +229,14 @@ void CreateItemList()
AppendMenu(hMenu,MF_STRING ,IDM_ABOUT, LoadLocalizedString(IDS_MENU_ABOUT));
AppendMenu(hMenu,MF_STRING ,IDM_CLOSE, LoadLocalizedString(IDS_MENU_CLOSE));
SetMenuStatus(0, DISCONNECTED);
SetMenuStatus(0, disconnected);
}
else
{
/* Create Main menu with all connections */
for (i=0; i < o.num_configs; i++)
AppendMenu(hMenu,MF_POPUP,(UINT) hMenuConn[i],o.cnn[i].config_name);
AppendMenu(hMenu,MF_POPUP,(UINT) hMenuConn[i],o.conn[i].config_name);
if (o.num_configs > 0)
AppendMenu(hMenu,MF_SEPARATOR,0,0);
if (o.allow_service[0]=='1' && o.service_only[0]=='0')
@ -279,7 +278,7 @@ void CreateItemList()
}
#endif
SetMenuStatus(i, DISCONNECTED);
SetMenuStatus(i, disconnected);
}
}
@ -331,7 +330,7 @@ void ShowTrayIcon()
* connected=1 -> Connecting
* connected=2 -> Connected
*/
void SetTrayIcon(int connected)
void SetTrayIcon(conn_state_t connected)
{
TCHAR msg[500];
TCHAR msg_connected[100];
@ -353,14 +352,14 @@ void SetTrayIcon(int connected)
first_conn=1;
for (i=0; i < o.num_configs; i++)
{
if(o.cnn[i].connect_status == CONNECTED)
if(o.conn[i].state == connected)
{
/* Append connection name to Icon Tip Msg */
if (first_conn)
_tcsncat(msg, msg_connected, _tsizeof(msg) - _tcslen(msg) - 1);
else
_tcsncat(msg, _T(", "), _tsizeof(msg) - _tcslen(msg) - 1);
_tcsncat(msg, o.cnn[i].config_name, _tsizeof(msg) - _tcslen(msg) - 1);
_tcsncat(msg, o.conn[i].config_name, _tsizeof(msg) - _tcslen(msg) - 1);
first_conn=0;
config=i;
}
@ -369,32 +368,32 @@ void SetTrayIcon(int connected)
first_conn=1;
for (i=0; i < o.num_configs; i++)
{
if((o.cnn[i].connect_status == CONNECTING) ||
(o.cnn[i].connect_status == RECONNECTING))
if((o.conn[i].state == connecting) ||
(o.conn[i].state == reconnecting))
{
/* Append connection name to Icon Tip Msg */
if (first_conn)
_tcsncat(msg, msg_connecting, _tsizeof(msg) - _tcslen(msg) - 1);
else
_tcsncat(msg, _T(", "), _tsizeof(msg) - _tcslen(msg) - 1);
_tcsncat(msg, o.cnn[i].config_name, _tsizeof(msg) - _tcslen(msg) - 1);
_tcsncat(msg, o.conn[i].config_name, _tsizeof(msg) - _tcslen(msg) - 1);
first_conn=0;
}
}
if (CountConnectedState(CONNECTED) == 1)
if (CountConnState(connected) == 1)
{
/* Append "Connected Since and Assigned IP msg" */
time_t con_time;
con_time=time(NULL);
_tcsftime(connected_since, _tsizeof(connected_since), _T("%b %d, %H:%M"),
localtime(&o.cnn[config].connected_since));
localtime(&o.conn[config].connected_since));
_tcsncat(msg, LoadLocalizedString(IDS_TIP_CONNECTED_SINCE), _tsizeof(msg) - _tcslen(msg) - 1);
_tcsncat(msg, connected_since, _tsizeof(msg) - _tcslen(msg) - 1);
if (_tcslen(o.cnn[config].ip) > 0)
if (_tcslen(o.conn[config].ip) > 0)
{
TCHAR assigned_ip[100];
_sntprintf_0(assigned_ip, LoadLocalizedString(IDS_TIP_ASSIGNED_IP), o.cnn[config].ip);
_sntprintf_0(assigned_ip, LoadLocalizedString(IDS_TIP_ASSIGNED_IP), o.conn[config].ip);
_tcsncat(msg, assigned_ip, _tsizeof(msg) - _tcslen(msg) - 1);
}
}
@ -427,7 +426,7 @@ void ShowTrayBalloon(TCHAR *infotitle_msg, TCHAR *info_msg)
}
void SetMenuStatus (int config, int bCheck)
void SetMenuStatus (int config, conn_state_t state)
{
/* bCheck values:
* 0 - Not Connected
@ -441,25 +440,25 @@ void SetMenuStatus (int config, int bCheck)
if (o.num_configs == 1)
{
if (bCheck == DISCONNECTED)
if (state == disconnected)
{
EnableMenuItem(hMenu, IDM_CONNECTMENU, MF_ENABLED);
EnableMenuItem(hMenu, IDM_DISCONNECTMENU, MF_GRAYED);
EnableMenuItem(hMenu, IDM_STATUSMENU, MF_GRAYED);
}
if (bCheck == CONNECTING)
if (state == connecting)
{
EnableMenuItem(hMenu, IDM_CONNECTMENU, MF_GRAYED);
EnableMenuItem(hMenu, IDM_DISCONNECTMENU, MF_ENABLED);
EnableMenuItem(hMenu, IDM_STATUSMENU, MF_ENABLED);
}
if (bCheck == CONNECTED)
if (state == connected)
{
EnableMenuItem(hMenu, IDM_CONNECTMENU, MF_GRAYED);
EnableMenuItem(hMenu, IDM_DISCONNECTMENU, MF_ENABLED);
EnableMenuItem(hMenu, IDM_STATUSMENU, MF_ENABLED);
}
if (bCheck == DISCONNECTING)
if (state == disconnecting)
{
EnableMenuItem(hMenu, IDM_CONNECTMENU, MF_GRAYED);
EnableMenuItem(hMenu, IDM_DISCONNECTMENU, MF_GRAYED);
@ -468,29 +467,29 @@ void SetMenuStatus (int config, int bCheck)
}
else
{
iState = ((bCheck == CONNECTED) || (bCheck == DISCONNECTING)) ?
iState = ((state == connected) || (state == disconnecting)) ?
MF_CHECKED : MF_UNCHECKED ;
CheckMenuItem (hMenu, (UINT) hMenuConn[config], iState) ;
if (bCheck == DISCONNECTED)
if (state == disconnected)
{
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_CONNECTMENU + config, MF_ENABLED);
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_DISCONNECTMENU + config, MF_GRAYED);
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_STATUSMENU + config, MF_GRAYED);
}
if (bCheck == CONNECTING)
if (state == connecting)
{
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_CONNECTMENU + config, MF_GRAYED);
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_DISCONNECTMENU + config, MF_ENABLED);
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_STATUSMENU + config, MF_ENABLED);
}
if (bCheck == CONNECTED)
if (state == connected)
{
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_CONNECTMENU + config, MF_GRAYED);
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_DISCONNECTMENU + config, MF_ENABLED);
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_STATUSMENU + config, MF_ENABLED);
}
if (bCheck == DISCONNECTING)
if (state == disconnecting)
{
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_CONNECTMENU + config, MF_GRAYED);
EnableMenuItem(hMenuConn[config], (UINT_PTR)IDM_DISCONNECTMENU + config, MF_GRAYED);
@ -513,15 +512,15 @@ void SetServiceMenuStatus()
hMenuHandle = hMenuService;
if ((o.service_running == SERVICE_NOACCESS) ||
(o.service_running == SERVICE_CONNECTING))
if ((o.service_state == service_noaccess) ||
(o.service_state == service_connecting))
{
/* Service is disabled */
EnableMenuItem(hMenuHandle, IDM_SERVICE_START, MF_GRAYED);
EnableMenuItem(hMenuHandle, IDM_SERVICE_STOP, MF_GRAYED);
EnableMenuItem(hMenuHandle, IDM_SERVICE_RESTART, MF_GRAYED);
}
else if (o.service_running == SERVICE_CONNECTED)
else if (o.service_state == service_connected)
{
/* Service is running */
EnableMenuItem(hMenuHandle, IDM_SERVICE_START, MF_GRAYED);

6
tray.h
View File

@ -19,6 +19,8 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "options.h"
#define WM_NOTIFYICONTRAY (WM_APP + 1)
//Popup Menu items
@ -52,10 +54,10 @@ void DestroyPopupMenus(); //Destroy popup menus
void OnNotifyTray(LPARAM lParam); //Tray message (mouse clicks on tray icon)
void OnDestroyTray(void); //WM_DESTROY message
void ShowTrayIcon(); //Put app icon in systray
void SetTrayIcon(int connected); //Change systray icon
void SetTrayIcon(conn_state_t connected); //Change systray icon
BOOL LoadAppIcon(); //Application icon
void CreateItemList(); //Crate Popup menu
void SetMenuStatus (int config, int bCheck); //Mark connection as connected/disconnected
void SetMenuStatus (int config, conn_state_t state); //Mark connection as connected/disconnected
void SetServiceMenuStatus(); //Diabled Service menu items.
void ShowTrayBalloon(TCHAR *infotitle_msg, TCHAR *info_msg);

View File

@ -30,7 +30,7 @@
#include "openvpn-gui-res.h"
#include "localization.h"
extern struct options o;
extern options_t o;
void ViewLog(int config)
{
@ -46,7 +46,7 @@ void ViewLog(int config)
CLEAR (sa);
CLEAR (sd);
_sntprintf_0(filename, _T("%s \"%s\""), o.log_viewer, o.cnn[config].log_path);
_sntprintf_0(filename, _T("%s \"%s\""), o.log_viewer, o.conn[config].log_path);
/* fill in STARTUPINFO struct */
GetStartupInfo(&start_info);
@ -88,7 +88,7 @@ void EditConfig(int config)
CLEAR (sa);
CLEAR (sd);
_sntprintf_0(filename, _T("%s \"%s\\%s\""), o.editor, o.cnn[config].config_dir, o.cnn[config].config_file);
_sntprintf_0(filename, _T("%s \"%s\\%s\""), o.editor, o.conn[config].config_dir, o.conn[config].config_file);
/* fill in STARTUPINFO struct */
GetStartupInfo(&start_info);
@ -105,7 +105,7 @@ void EditConfig(int config)
TRUE,
CREATE_NEW_CONSOLE,
NULL,
o.cnn[config].config_dir, //start-up dir
o.conn[config].config_dir, //start-up dir
&start_info,
&proc_info))
{