Skip to content

Commit

Permalink
Move tun_mainloop() to mainloop.c
Browse files Browse the repository at this point in the history
It can't live in tun.c because we're about to stop that being built on
Windows.

Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
  • Loading branch information
David Woodhouse authored and David Woodhouse committed Feb 13, 2014
1 parent 8707061 commit 4b3317e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 60 deletions.
60 changes: 60 additions & 0 deletions mainloop.c
Expand Up @@ -45,6 +45,66 @@ int queue_new_packet(struct pkt **q, void *buf, int len)
return 0;
}

static struct pkt *out_pkt;

/* This is here because it's generic and hence can't live in either of the
tun*.c files for specific platforms */
int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
{
int work_done = 0;

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;
}
out_pkt->len = len;
}

if (os_read_tun(vpninfo, out_pkt, new_pkt))
break;

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

queue_packet(&vpninfo->outgoing_queue, out_pkt);
out_pkt = NULL;

work_done = 1;
vpninfo->outgoing_qlen++;
if (vpninfo->outgoing_qlen == vpninfo->max_qlen) {
unmonitor_read_fd(vpninfo, tun);
break;
}
}
} else if (vpninfo->outgoing_qlen < vpninfo->max_qlen) {
monitor_read_fd(vpninfo, tun);
}

while (vpninfo->incoming_queue) {
struct pkt *this = vpninfo->incoming_queue;

if (os_write_tun(vpninfo, this))
break;

vpninfo->stats.rx_pkts++;
vpninfo->stats.rx_bytes += this->len;

vpninfo->incoming_queue = this->next;

free(this);
}
/* Work is not done if we just got rid of packets off the queue */
return work_done;
}

/* Return value:
* = 0, when successfully paused (may call again)
* = -EINTR, if aborted locally via cmd_fd
Expand Down
3 changes: 1 addition & 2 deletions openconnect-internal.h
Expand Up @@ -454,7 +454,6 @@ void set_script_env(struct openconnect_info *vpninfo);
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);
int os_write_tun(struct openconnect_info *vpninfo, struct pkt *pkt);
Expand Down Expand Up @@ -511,7 +510,7 @@ int openconnect_local_cert_md5(struct openconnect_info *vpninfo,
#endif

/* mainloop.c */
int vpn_add_pollfd(struct openconnect_info *vpninfo, int fd, short events);
int tun_mainloop(struct openconnect_info *vpninfo, int *timeout);
int queue_new_packet(struct pkt **q, void *buf, int len);
void queue_packet(struct pkt **q, struct pkt *new);
int keepalive_action(struct keepalive_info *ka, int *timeout);
Expand Down
58 changes: 0 additions & 58 deletions tun.c
Expand Up @@ -467,64 +467,6 @@ int openconnect_setup_tun_device(struct openconnect_info *vpninfo, char *vpnc_sc
return openconnect_setup_tun_fd(vpninfo, tun_fd);
}

static struct pkt *out_pkt;

int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
{
int work_done = 0;

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;
}
out_pkt->len = len;
}

if (os_read_tun(vpninfo, out_pkt, new_pkt))
break;

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

queue_packet(&vpninfo->outgoing_queue, out_pkt);
out_pkt = NULL;

work_done = 1;
vpninfo->outgoing_qlen++;
if (vpninfo->outgoing_qlen == vpninfo->max_qlen) {
unmonitor_read_fd(vpninfo, tun);
break;
}
}
} else if (vpninfo->outgoing_qlen < vpninfo->max_qlen) {
monitor_read_fd(vpninfo, tun);
}

while (vpninfo->incoming_queue) {
struct pkt *this = vpninfo->incoming_queue;

if (os_write_tun(vpninfo, this))
break;

vpninfo->stats.rx_pkts++;
vpninfo->stats.rx_bytes += this->len;

vpninfo->incoming_queue = this->next;

free(this);
}
/* Work is not done if we just got rid of packets off the queue */
return work_done;
}

void shutdown_tun(struct openconnect_info *vpninfo)
{
#ifdef _WIN32
Expand Down

0 comments on commit 4b3317e

Please sign in to comment.