From 4b3317e376fb8d4e9214826aff0f60ca98c89976 Mon Sep 17 00:00:00 2001 From: David Woodhouse Date: Thu, 13 Feb 2014 23:18:55 +0000 Subject: [PATCH] Move tun_mainloop() to mainloop.c It can't live in tun.c because we're about to stop that being built on Windows. Signed-off-by: David Woodhouse --- mainloop.c | 60 ++++++++++++++++++++++++++++++++++++++++++ openconnect-internal.h | 3 +-- tun.c | 58 ---------------------------------------- 3 files changed, 61 insertions(+), 60 deletions(-) diff --git a/mainloop.c b/mainloop.c index 05ddd788..28358c5f 100644 --- a/mainloop.c +++ b/mainloop.c @@ -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 diff --git a/openconnect-internal.h b/openconnect-internal.h index fc4a715f..dffba1be 100644 --- a/openconnect-internal.h +++ b/openconnect-internal.h @@ -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); @@ -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); diff --git a/tun.c b/tun.c index b7aec60d..b5208e0d 100644 --- a/tun.c +++ b/tun.c @@ -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