Skip to content

Commit

Permalink
library: Add new callback for obtaining TX/RX statistics
Browse files Browse the repository at this point in the history
The UI may want to periodically request byte/packet counts from the
library without exiting the mainloop.  So we'll allow it to write
OC_CMD_STATS to the cmd_fd and then receive a callback.

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
  • Loading branch information
cernekee committed Jan 15, 2014
1 parent e1b4dfa commit 6361a46
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions libopenconnect.map.in
Expand Up @@ -51,6 +51,7 @@ OPENCONNECT_3.1 {
openconnect_set_protect_socket_handler;
openconnect_set_mobile_info;
openconnect_set_xmlpost;
openconnect_set_stats_handler;
} OPENCONNECT_3.0;

OPENCONNECT_PRIVATE {
Expand Down
6 changes: 6 additions & 0 deletions library.c
Expand Up @@ -556,3 +556,9 @@ void openconnect_set_protect_socket_handler(struct openconnect_info *vpninfo,
{
vpninfo->protect_socket = protect_socket;
}

void openconnect_set_stats_handler(struct openconnect_info *vpninfo,
openconnect_stats_vfn stats_handler)
{
vpninfo->stats_handler = stats_handler;
}
2 changes: 2 additions & 0 deletions openconnect-internal.h
Expand Up @@ -288,6 +288,8 @@ struct openconnect_info {
struct pkt *outgoing_queue;
int outgoing_qlen;
int max_qlen;
struct oc_stats stats;
openconnect_stats_vfn stats_handler;

socklen_t peer_addrlen;
struct sockaddr *peer_addr;
Expand Down
14 changes: 14 additions & 0 deletions openconnect.h
Expand Up @@ -187,6 +187,13 @@ struct oc_vpn_option {
struct oc_vpn_option *next;
};

struct oc_stats {
uint64_t tx_pkts;
uint64_t tx_bytes;
uint64_t rx_pkts;
uint64_t rx_bytes;
};

/****************************************************************************/

#define PRG_ERR 0
Expand All @@ -197,6 +204,7 @@ struct oc_vpn_option {
/* byte commands to write into the cmd_fd */
#define OC_CMD_CANCEL 'x'
#define OC_CMD_PAUSE 'p'
#define OC_CMD_STATS 's'

#define RECONNECT_INTERVAL_MIN 10
#define RECONNECT_INTERVAL_MAX 100
Expand Down Expand Up @@ -378,6 +386,12 @@ typedef void (*openconnect_protect_socket_vfn) (void *privdata, int fd);
void openconnect_set_protect_socket_handler(struct openconnect_info *vpninfo,
openconnect_protect_socket_vfn protect_socket);

/* Callback for obtaining traffic stats via OC_CMD_STATS.
*/
typedef void (*openconnect_stats_vfn) (void *privdata, const struct oc_stats *stats);
void openconnect_set_stats_handler(struct openconnect_info *vpninfo,
openconnect_stats_vfn stats_handler);

/* SSL certificate capabilities. openconnect_has_pkcs11_support() means that we
can accept PKCS#11 URLs in place of filenames, for the certificate and key. */
int openconnect_has_pkcs11_support(void);
Expand Down
3 changes: 3 additions & 0 deletions ssl.c
Expand Up @@ -554,6 +554,9 @@ void check_cmd_fd(struct openconnect_info *vpninfo, fd_set *fds)
case OC_CMD_PAUSE:
vpninfo->got_pause_cmd = 1;
break;
case OC_CMD_STATS:
if (vpninfo->stats_handler)
vpninfo->stats_handler(vpninfo->cbdata, &vpninfo->stats);
}
}

Expand Down
6 changes: 6 additions & 0 deletions tun.c
Expand Up @@ -736,6 +736,9 @@ int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
break;
out_pkt->len = len - prefix_size;

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

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

Expand All @@ -758,6 +761,9 @@ int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
unsigned char *data = this->data;
int len = this->len;

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

#ifdef TUN_HAS_AF_PREFIX
if (!vpninfo->script_tun) {
struct ip *iph = (void *)data;
Expand Down

0 comments on commit 6361a46

Please sign in to comment.