Skip to content

Commit

Permalink
Factor out os_read_tun() from tun_mainloop()
Browse files Browse the repository at this point in the history
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
  • Loading branch information
David Woodhouse authored and David Woodhouse committed Feb 13, 2014
1 parent f90b442 commit 4d58e4f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 35 deletions.
1 change: 1 addition & 0 deletions openconnect-internal.h
Expand Up @@ -456,6 +456,7 @@ int script_config_tun(struct openconnect_info *vpninfo, const char *reason);
/* tun.c */
int tun_mainloop(struct openconnect_info *vpninfo, int *timeout);
void shutdown_tun(struct openconnect_info *vpninfo);
int os_read_tun(struct openconnect_info *vpninfo, struct pkt *pkt, int new_pkt);

/* dtls.c */
unsigned char unhex(const char *data);
Expand Down
31 changes: 31 additions & 0 deletions tun-win32.c
Expand Up @@ -203,3 +203,34 @@ int win32_setup_tun(struct openconnect_info *vpninfo)

return 0;
}

int os_read_tun(struct openconnect_info *vpninfo, struct pkt *pkt, int new_pkt)
{
DWORD pkt_size;

/* For newly-allocated packets we have to trigger the read. */
if (new_pkt) {
if (!ReadFile(vpninfo->tun_fh, pkt->data, pkt->len, &pkt_size, &vpninfo->tun_rd_overlap)) {
DWORD err = GetLastError();
if (err != ERROR_IO_PENDING)
vpn_progress(vpninfo, PRG_ERR,
_("Failed to read from TAP device: %lx\n"),
err);
return -1;
}
} else {
/* IF it isn't a new packet, then there was already a pending read on it. */
if (!GetOverlappedResult(vpninfo->tun_fh, &vpninfo->tun_rd_overlap, &pkt_size, FALSE)) {
DWORD err = GetLastError();

if (err != ERROR_IO_INCOMPLETE)
vpn_progress(vpninfo, PRG_ERR,
_("Failed to complete read from TAP device: %lx\n"),
err);
return -1;
}
}

pkt->len = pkt_size;
return 0;
}
60 changes: 25 additions & 35 deletions tun.c
Expand Up @@ -377,6 +377,26 @@ int openconnect_setup_tun_script(struct openconnect_info *vpninfo, char *tun_scr

return openconnect_setup_tun_fd(vpninfo, fds[0]);
}

int os_read_tun(struct openconnect_info *vpninfo, struct pkt *pkt, int new_pkt)
{
int prefix_size = 0;
int len;

#ifdef TUN_HAS_AF_PREFIX
if (!vpninfo->script_tun)
prefix_size = sizeof(int);
#endif

/* Sanity. Just non-blocking reads on a select()able file descriptor... */
len = read(vpninfo->tun_fd, pkt->data - prefix_size, pkt->len + prefix_size);
if (len <= prefix_size)
return -1;

pkt->len = len - prefix_size;
return 0;
}

#endif /* !_WIN32 */

int openconnect_setup_tun_device(struct openconnect_info *vpninfo, char *vpnc_script, char *ifname)
Expand Down Expand Up @@ -407,57 +427,27 @@ static struct pkt *out_pkt;
int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
{
int work_done = 0;
int prefix_size = 0;
#ifdef _WIN32
DWORD pkt_size = 0;
#endif

#ifdef TUN_HAS_AF_PREFIX
if (!vpninfo->script_tun)
prefix_size = sizeof(int);
#endif

if (read_fd_monitored(vpninfo, tun)) {
while (1) {
int len = vpninfo->ip_info.mtu;
int new_pkt = 0;

if (!out_pkt) {
new_pkt = 1;
out_pkt = malloc(sizeof(struct pkt) + len);
if (!out_pkt) {
vpn_progress(vpninfo, PRG_ERR, "Allocation failed\n");
break;
}
#ifdef _WIN32
if (!ReadFile(vpninfo->tun_fh, out_pkt->data, len, &pkt_size, &vpninfo->tun_rd_overlap)) {
DWORD err = GetLastError();
if (err != ERROR_IO_PENDING)
vpn_progress(vpninfo, PRG_ERR,
_("Failed to read from TAP device: %lx\n"),
err);
break;
}
len = pkt_size;
} else {
/* if out_pkt was already non-NULL then there was lready a pending read on it. */
if (!GetOverlappedResult(vpninfo->tun_fh, &vpninfo->tun_rd_overlap, &pkt_size, FALSE)) {
DWORD err = GetLastError();

if (err != ERROR_IO_INCOMPLETE)
vpn_progress(vpninfo, PRG_ERR,
_("Failed to complete read from TAP device: %lx\n"),
err);
break;
}
len = pkt_size;
#endif /* _WIN32 */
out_pkt->len = len;
}
#ifndef _WIN32
/* Sanity. Just non-blocking reads on a select()able file descriptor... */
len = read(vpninfo->tun_fd, out_pkt->data - prefix_size, len + prefix_size);
#endif
if (len <= prefix_size)

if (os_read_tun(vpninfo, out_pkt, new_pkt))
break;
out_pkt->len = len - prefix_size;

vpninfo->stats.tx_pkts++;
vpninfo->stats.tx_bytes += out_pkt->len;
Expand Down

0 comments on commit 4d58e4f

Please sign in to comment.