Skip to content

Commit

Permalink
Clean up search_taps() function
Browse files Browse the repository at this point in the history
Firstly, make it capable of returning an intptr_t (which will be a HANDLE)
which is generated by the callback function. Or -1 on all errors.

Secondly, add an explicit break after calling the callback, so that we
aren't making assumptions about what a zero result means.

Finally, add an 'all' argument to avoid said break, so that it can still
be used for the '--list-taps' functionality as was originally envisaged.

Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
  • Loading branch information
David Woodhouse authored and David Woodhouse committed Feb 14, 2014
1 parent fa20adc commit d785447
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions tun-win32.c
Expand Up @@ -52,16 +52,17 @@
#define ADAPTERS_KEY CONTROL_KEY "Class\\" NETDEV_GUID
#define CONNECTIONS_KEY CONTROL_KEY "Network\\" NETDEV_GUID

typedef int (tap_callback)(struct openconnect_info *vpninfo, char *idx, char *name);
typedef intptr_t (tap_callback)(struct openconnect_info *vpninfo, char *idx, char *name);

static int search_taps(struct openconnect_info *vpninfo, tap_callback *cb)
static intptr_t search_taps(struct openconnect_info *vpninfo, tap_callback *cb, int all)
{
LONG status;
HKEY adapters_key;
DWORD len;
char buf[40], name[40];
char keyname[strlen(CONNECTIONS_KEY) + sizeof(buf) + 1 + strlen("\\Connection")];
int i = 0, ret = 0, found = 0;
int i = 0, found = 0;
intptr_t ret;

status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ADAPTERS_KEY, 0,
KEY_READ, &adapters_key);
Expand All @@ -70,13 +71,13 @@ static int search_taps(struct openconnect_info *vpninfo, tap_callback *cb)
_("Error accessing registry key for network adapters\n"));
return -EIO;
}
while (!ret) {
while (1) {
len = sizeof(buf);
status = RegEnumKeyEx(adapters_key, i++, buf, &len,
NULL, NULL, NULL, NULL);
if (status) {
if (status != ERROR_NO_MORE_ITEMS)
ret = -EIO;
ret = -1;
break;
}

Expand Down Expand Up @@ -116,6 +117,8 @@ static int search_taps(struct openconnect_info *vpninfo, tap_callback *cb)
}

ret = cb(vpninfo, buf, name);
if (!all)
break;
}

RegCloseKey(adapters_key);
Expand All @@ -127,7 +130,7 @@ static int search_taps(struct openconnect_info *vpninfo, tap_callback *cb)
return ret;
}

static int open_tun(struct openconnect_info *vpninfo, char *guid, char *name)
static intptr_t open_tun(struct openconnect_info *vpninfo, char *guid, char *name)
{
char devname[80];
HANDLE tun_fh;
Expand Down Expand Up @@ -198,7 +201,7 @@ static int open_tun(struct openconnect_info *vpninfo, char *guid, char *name)

int os_setup_tun(struct openconnect_info *vpninfo)
{
if (search_taps(vpninfo, open_tun) != 1)
if (search_taps(vpninfo, open_tun, 0) != 1)
return -1;

return 0;
Expand Down

0 comments on commit d785447

Please sign in to comment.