Commit 887c9d97 authored by David Woodhouse's avatar David Woodhouse

Print error message strings on Windows

Signed-off-by: default avatarDavid Woodhouse <David.Woodhouse@intel.com>
parent 78078e1a
......@@ -228,6 +228,35 @@ int openconnect__inet_aton(const char *cp, struct in_addr *addr)
#endif
#ifdef _WIN32
char *openconnect__win32_strerror(DWORD err)
{
wchar_t *msgw;
char *msgutf8;
int nr_chars;
if (!FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL, err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&msgw, 0, NULL)) {
if (asprintf(&msgutf8, _("(error 0x%x)"), err) != -1)
return msgutf8;
fail:
return strdup(_("(Error while describing error!)"));
}
nr_chars = WideCharToMultiByte(CP_UTF8, 0, msgw, -1, NULL, 0, NULL, NULL);
msgutf8 = malloc(nr_chars);
if (!msgutf8)
goto fail;
WideCharToMultiByte(CP_UTF8, 0, msgw, -1, msgutf8, nr_chars, NULL, NULL);
LocalFree(msgw);
return msgutf8;
}
void openconnect__win32_sock_init()
{
WSADATA data;
......
......@@ -25,6 +25,7 @@ symver_time=
symver_getline=
symver_asprintf=
symver_vasprintf=
symver_win32_strerror=
case $host_os in
*linux* | *gnu*)
......@@ -54,6 +55,7 @@ case $host_os in
have_win=yes
# For asprintf()
AC_DEFINE(_GNU_SOURCE, 1, [_GNU_SOURCE])
symver_win32_strerror="openconnect__win32_strerror;"
;;
*)
# On FreeBSD the only way to get vsyslog() visible is to define
......@@ -139,6 +141,7 @@ AC_SUBST(SYMVER_TIME, $symver_time)
AC_SUBST(SYMVER_GETLINE, $symver_getline)
AC_SUBST(SYMVER_ASPRINTF, $symver_asprintf)
AC_SUBST(SYMVER_VASPRINTF, $symver_vasprintf)
AC_SUBST(SYMVER_WIN32_STRERROR, $symver_win32_strerror)
AS_COMPILER_FLAGS(WFLAGS,
"-Wall
......
......@@ -209,7 +209,7 @@ public final class LibTest {
lib.setCSDWrapper("csd.sh", null, null);
}
lib.parseURL(argv[0]);
lib.setSystemTrust(true);
int ret = lib.obtainCookie();
if (ret < 0)
die("obtainCookie() returned error");
......
......@@ -65,7 +65,7 @@ OPENCONNECT_4.1 {
OPENCONNECT_PRIVATE {
global: @SYMVER_TIME@ @SYMVER_GETLINE@ @SYMVER_JAVA@ @SYMVER_ASPRINTF@ @SYMVER_VASPRINTF@
global: @SYMVER_TIME@ @SYMVER_GETLINE@ @SYMVER_JAVA@ @SYMVER_ASPRINTF@ @SYMVER_VASPRINTF@ @SYMVER_WIN32_STRERROR@
openconnect_version_str;
openconnect_sha1;
openconnect_open_utf8;
......
......@@ -299,8 +299,10 @@ static char *convert_arg_to_utf8(char **argv, char *arg)
argv_w = CommandLineToArgvW(GetCommandLineW(), &argc_w);
if (!argv_w) {
fprintf(stderr, _("CommandLineToArgvW() failed: %lx\n"),
GetLastError());
char *errstr = openconnect__win32_strerror(GetLastError());
fprintf(stderr, _("CommandLineToArgvW() failed: %s\n"),
errstr);
free(errstr);
exit(1);
}
}
......@@ -346,7 +348,9 @@ static void read_stdin(char **string, int hidden)
}
if (!ReadConsoleW(stdinh, wbuf, sizeof(wbuf)/2, &nr_read, &rcc)) {
fprintf(stderr, _("ReadConsole() error %lx\n"), GetLastError());
char *errstr = openconnect__win32_strerror(GetLastError());
fprintf(stderr, _("ReadConsole() failed: %s\n"), errstr);
free(errstr);
goto out;
}
......@@ -357,8 +361,10 @@ static void read_stdin(char **string, int hidden)
nr_read = WideCharToMultiByte(CP_UTF8, 0, wbuf, -1, NULL, 0, NULL, NULL);
if (!nr_read) {
char *errstr = openconnect__win32_strerror(GetLastError());
fprintf(stderr, _("Error converting console input: %lx\n"),
GetLastError());
errstr);
free(errstr);
goto out;
}
buf = malloc(nr_read);
......@@ -368,9 +374,11 @@ static void read_stdin(char **string, int hidden)
}
if (!WideCharToMultiByte(CP_UTF8, 0, wbuf, -1, buf, nr_read, NULL, NULL)) {
char *errstr = openconnect__win32_strerror(GetLastError());
fprintf(stderr, _("Error converting console input: %s\n"),
errstr);
free(errstr);
free(buf);
fprintf(stderr, _("Error converting console input: %lx\n"),
GetLastError());
goto out;
}
......
......@@ -206,9 +206,11 @@ int openconnect_mainloop(struct openconnect_info *vpninfo,
events[nr_events++] = vpninfo->tun_rd_overlap.hEvent;
}
if (WaitForMultipleObjects(nr_events, events, FALSE, timeout) == WAIT_FAILED) {
char *errstr = openconnect__win32_strerror(GetLastError());
vpn_progress(vpninfo, PRG_ERR,
_("WaitForMultipleObjects failed: %lx\n"),
GetLastError());
_("WaitForMultipleObjects failed: %s\n"),
errstr);
free(errstr);
}
#else
memcpy(&rfds, &vpninfo->_select_rfds, sizeof(rfds));
......
......@@ -527,6 +527,7 @@ static inline int set_fd_cloexec(int fd)
#ifdef _WIN32
#define pipe(fds) _pipe(fds, 4096, O_BINARY)
void openconnect__win32_sock_init();
char *openconnect__win32_strerror(DWORD err);
#undef inet_pton
#define inet_pton openconnect__win32_inet_pton
int openconnect__win32_inet_pton(int af, const char *src, void *dst);
......
......@@ -363,9 +363,11 @@ int script_config_tun(struct openconnect_info *vpninfo, const char *reason)
}
if (ret < 0) {
char *errstr = openconnect__win32_strerror(GetLastError());
vpn_progress(vpninfo, PRG_ERR,
_("Failed to spawn script '%s' for %s: %d\n"),
vpninfo->vpnc_script, reason, (int)GetLastError());
_("Failed to spawn script '%s' for %s: %s\n"),
vpninfo->vpnc_script, reason, errstr);
free(errstr);
goto cleanup;
}
......
......@@ -178,10 +178,11 @@ static intptr_t open_tun(struct openconnect_info *vpninfo, char *guid, char *nam
if (!DeviceIoControl(tun_fh, TAP_IOCTL_GET_VERSION,
data, sizeof(&data), data, sizeof(data),
&len, NULL)) {
DWORD err = GetLastError();
char *errstr = openconnect__win32_strerror(GetLastError());
vpn_progress(vpninfo, PRG_ERR,
_("Failed to obtain TAP driver version: %lx\n"), err);
_("Failed to obtain TAP driver version: %s\n"), errstr);
free(errstr);
return -1;
}
if (data[0] < 9 || (data[0] == 9 && data[1] < 9)) {
......@@ -200,10 +201,11 @@ static intptr_t open_tun(struct openconnect_info *vpninfo, char *guid, char *nam
if (!DeviceIoControl(tun_fh, TAP_IOCTL_CONFIG_TUN,
data, sizeof(data), data, sizeof(data),
&len, NULL)) {
DWORD err = GetLastError();
char *errstr = openconnect__win32_strerror(GetLastError());
vpn_progress(vpninfo, PRG_ERR,
_("Failed to set TAP IP addresses: %lx\n"), err);
_("Failed to set TAP IP addresses: %s\n"), errstr);
free(errstr);
return -1;
}
......@@ -211,10 +213,11 @@ static intptr_t open_tun(struct openconnect_info *vpninfo, char *guid, char *nam
if (!DeviceIoControl(tun_fh, TAP_IOCTL_SET_MEDIA_STATUS,
data, sizeof(data[0]), data, sizeof(data[0]),
&len, NULL)) {
DWORD err = GetLastError();
char *errstr = openconnect__win32_strerror(GetLastError());
vpn_progress(vpninfo, PRG_ERR,
_("Failed to set TAP media status: %lx\n"), err);
_("Failed to set TAP media status: %s\n"), errstr);
free(errstr);
return -1;
}
if (!vpninfo->ifname)
......@@ -240,10 +243,13 @@ int os_read_tun(struct openconnect_info *vpninfo, struct pkt *pkt)
if (err == ERROR_IO_PENDING)
vpninfo->tun_rd_pending = 1;
else
else {
char *errstr = openconnect__win32_strerror(err);
vpn_progress(vpninfo, PRG_ERR,
_("Failed to read from TAP device: %lx\n"),
err);
_("Failed to read from TAP device: %s\n"),
errstr);
free(errstr);
}
return -1;
} else if (!GetOverlappedResult(vpninfo->tun_fh,
&vpninfo->tun_rd_overlap, &pkt_size,
......@@ -251,10 +257,12 @@ int os_read_tun(struct openconnect_info *vpninfo, struct pkt *pkt)
DWORD err = GetLastError();
if (err != ERROR_IO_INCOMPLETE) {
char *errstr = openconnect__win32_strerror(err);
vpninfo->tun_rd_pending = 0;
vpn_progress(vpninfo, PRG_ERR,
_("Failed to complete read from TAP device: %lx\n"),
err);
_("Failed to complete read from TAP device: %s\n"),
errstr);
free(errstr);
goto reread;
}
return -1;
......@@ -271,6 +279,7 @@ int os_write_tun(struct openconnect_info *vpninfo, struct pkt *pkt)
{
DWORD pkt_size = 0;
DWORD err;
char *errstr;
if (WriteFile(vpninfo->tun_fh, pkt->data, pkt->len, &pkt_size, &vpninfo->tun_wr_overlap)) {
vpn_progress(vpninfo, PRG_TRACE,
......@@ -292,8 +301,10 @@ int os_write_tun(struct openconnect_info *vpninfo, struct pkt *pkt)
}
err = GetLastError();
}
errstr = openconnect__win32_strerror(err);
vpn_progress(vpninfo, PRG_ERR,
_("Failed to write to TAP device: %lx\n"), err);
_("Failed to write to TAP device: %s\n"), errstr);
free(errstr);
return -1;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment