Commit 4b3317e3 authored by David Woodhouse's avatar David Woodhouse

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: default avatarDavid Woodhouse <David.Woodhouse@intel.com>
parent 87070617
......@@ -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
......
......@@ -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);
......
......@@ -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
......
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