diff --git a/misc.c b/misc.c index ec36fad..67d82c0 100644 --- a/misc.c +++ b/misc.c @@ -462,3 +462,22 @@ validate_input(const WCHAR *input, const WCHAR *exclude) exclude = L"\n"; return (wcspbrk(input, exclude) == NULL); } + +/* Concatenate two wide strings with a separator */ +void +wcs_concat2(WCHAR *dest, int len, const WCHAR *src1, const WCHAR *src2, const WCHAR *sep) +{ + int n = 0; + + if (!dest || len == 0) + return; + + if (src1 && src2 && src2[0]) + n = swprintf(dest, len, L"%s%s%s", src1, sep, src2); + else if (src1) + n = swprintf(dest, len, L"%s", src1); + + if (n < 0 || n >= len) /*swprintf failed */ + n = 0; + dest[n] = L'\0'; +} diff --git a/misc.h b/misc.h index 0480eeb..a15bae7 100644 --- a/misc.h +++ b/misc.h @@ -40,4 +40,6 @@ BOOL Base64Encode(const char *input, int input_len, char **output); int Base64Decode(const char *input, char **output); WCHAR *Widen(const char *utf8); BOOL validate_input(const WCHAR *input, const WCHAR *exclude); +/* Concatenate two wide strings with a separator */ +void wcs_concat2(WCHAR *dest, int len, const WCHAR *src1, const WCHAR *src2, const WCHAR *sep); #endif diff --git a/openvpn.c b/openvpn.c index e6155fe..8b8314a 100644 --- a/openvpn.c +++ b/openvpn.c @@ -200,6 +200,35 @@ OnLogLine(connection_t *c, char *line) SendMessage(logWnd, EM_REPLACESEL, FALSE, (LPARAM) _T("\n")); } +/* expect ipv4,remote,port,,,ipv6 */ +static void +parse_assigned_ip(connection_t *c, const char *msg) +{ + char *sep; + + /* extract local ipv4 address if available*/ + c->ip[0] = L'\0'; + sep = strchr(msg, ','); + if (sep == NULL) + return; + + /* Convert the IP address to Unicode */ + MultiByteToWideChar(CP_UTF8, 0, msg, sep-msg, c->ip, _countof(c->ip)); + + /* extract local ipv6 address if available */ + c->ipv6[0] = L'\0'; + /* skip 4 commas */ + for (int i = 0; i < 4 && sep; i++) + { + sep = strchr(sep + 1, ','); + } + if (!sep) + return; + + sep++; /* start of ipv6 address */ + /* Convert the IP address to Unicode */ + MultiByteToWideChar(CP_UTF8, 0, sep, -1, c->ipv6, _countof(c->ipv6)); +} /* * Handle a state change notification from the OpenVPN management interface @@ -227,21 +256,22 @@ OnStateChange(connection_t *c, char *data) return; *pos = '\0'; + if (strcmp(state, "CONNECTED") == 0) + { + parse_assigned_ip(c, pos + 1); + } if (strcmp(state, "CONNECTED") == 0 && strcmp(message, "SUCCESS") == 0) { + /* concatenate ipv4 and ipv6 addresses into one string */ + WCHAR ip_txt[256]; + WCHAR ip[64]; + wcs_concat2(ip, _countof(ip), c->ip, c->ipv6, L", "); + LoadLocalizedStringBuf(ip_txt, _countof(ip_txt), IDS_NFO_ASSIGN_IP, ip); + /* Run Connect Script */ if (c->state == connecting || c->state == resuming) RunConnectScript(c, false); - /* Save the local IP address if available */ - char *local_ip = pos + 1; - pos = strchr(local_ip, ','); - if (pos != NULL) - *pos = '\0'; - - /* Convert the IP address to Unicode */ - MultiByteToWideChar(CP_UTF8, 0, local_ip, -1, c->ip, _countof(c->ip)); - /* Show connection tray balloon */ if ((c->state == connecting && o.show_balloon != 0) || (c->state == resuming && o.show_balloon != 0) @@ -249,7 +279,7 @@ OnStateChange(connection_t *c, char *data) { TCHAR msg[256]; LoadLocalizedStringBuf(msg, _countof(msg), IDS_NFO_NOW_CONNECTED, c->config_name); - ShowTrayBalloon(msg, (_tcslen(c->ip) ? LoadLocalizedString(IDS_NFO_ASSIGN_IP, c->ip) : _T(""))); + ShowTrayBalloon(msg, (ip[0] ? ip_txt : _T(""))); } /* Save time when we got connected. */ diff --git a/options.h b/options.h index 85468be..3bb1730 100644 --- a/options.h +++ b/options.h @@ -99,6 +99,7 @@ struct 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 ipv6[46]; /* Assigned IPv6 address */ BOOL auto_connect; /* AutoConnect at startup id TRUE */ conn_state_t state; /* State the connection currently is in */ int failed_psw_attempts; /* # of failed attempts entering password(s) */ diff --git a/tray.c b/tray.c index 7227e5d..808eb1b 100644 --- a/tray.c +++ b/tray.c @@ -37,6 +37,7 @@ #include "openvpn_config.h" #include "openvpn-gui-res.h" #include "localization.h" +#include "misc.h" /* Popup Menus */ HMENU hMenu; @@ -272,14 +273,18 @@ SetTrayIcon(conn_state_t state) if (CountConnState(connected) == 1) { /* Append "Connected since and assigned IP" to message */ + const connection_t *c = &o.conn[config]; TCHAR time[50]; LocalizedTime(o.conn[config].connected_since, time, _countof(time)); _tcsncat(msg, LoadLocalizedString(IDS_TIP_CONNECTED_SINCE), _countof(msg) - _tcslen(msg) - 1); _tcsncat(msg, time, _countof(msg) - _tcslen(msg) - 1); - if (_tcslen(o.conn[config].ip) > 0) { - TCHAR *assigned_ip = LoadLocalizedString(IDS_TIP_ASSIGNED_IP, o.conn[config].ip); + if ( _tcslen(c->ip) > 0) { + /* concatenate ipv4 and ipv6 addresses into one string */ + WCHAR ip[64]; + wcs_concat2(ip, _countof(ip), c->ip, c->ipv6, L", "); + WCHAR *assigned_ip = LoadLocalizedString(IDS_TIP_ASSIGNED_IP, ip); _tcsncat(msg, assigned_ip, _countof(msg) - _tcslen(msg) - 1); } }