Skip to content

Commit

Permalink
Add IPv6 bytes stats (#421)
Browse files Browse the repository at this point in the history
IPv6 bytes stats are now shown in the Stats activity and exposed in the CaptureCtrl API

Co-authored-by: emanuele-f <[email protected]>
  • Loading branch information
myzhan and emanuele-f authored Mar 11, 2024
1 parent ebd5b58 commit 4018db9
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ public static void notifyCaptureStopped(Context ctx, CaptureStats stats) {
private static void putStats(Intent intent, CaptureStats stats) {
intent.putExtra("bytes_sent", stats.bytes_sent);
intent.putExtra("bytes_rcvd", stats.bytes_rcvd);
intent.putExtra("ipv6_bytes_sent", stats.ipv6_bytes_sent);
intent.putExtra("ipv6_bytes_rcvd", stats.ipv6_bytes_rcvd);
intent.putExtra("bytes_dumped", stats.pcap_dump_size);
intent.putExtra("pkts_sent", stats.pkts_sent);
intent.putExtra("pkts_rcvd", stats.pkts_rcvd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
public class StatsActivity extends BaseActivity implements MenuProvider {
private TextView mBytesSent;
private TextView mBytesRcvd;
private TextView mIPv6BytesSent;
private TextView mIPv6BytesRcvd;
private TextView mIPv6BytesPercentage;
private TextView mPacketsSent;
private TextView mPacketsRcvd;
private TextView mActiveConns;
Expand All @@ -69,6 +72,9 @@ protected void onCreate(Bundle savedInstanceState) {
mTable = findViewById(R.id.table);
mBytesSent = findViewById(R.id.bytes_sent);
mBytesRcvd = findViewById(R.id.bytes_rcvd);
mIPv6BytesSent = findViewById(R.id.ipv6_bytes_sent);
mIPv6BytesRcvd = findViewById(R.id.ipv6_bytes_rcvd);
mIPv6BytesPercentage = findViewById(R.id.ipv6_bytes_percentage);
mPacketsSent = findViewById(R.id.packets_sent);
mPacketsRcvd = findViewById(R.id.packets_rcvd);
mActiveConns = findViewById(R.id.active_connections);
Expand Down Expand Up @@ -98,6 +104,16 @@ protected void onCreate(Bundle savedInstanceState) {
private void updateStats(CaptureStats stats) {
mBytesSent.setText(Utils.formatBytes(stats.bytes_sent));
mBytesRcvd.setText(Utils.formatBytes(stats.bytes_rcvd));

mIPv6BytesSent.setText(Utils.formatBytes(stats.ipv6_bytes_sent));
mIPv6BytesRcvd.setText(Utils.formatBytes(stats.ipv6_bytes_rcvd));

long tot_bytes = stats.bytes_sent + stats.bytes_rcvd;
long percentage = (tot_bytes > 0) ?
((stats.ipv6_bytes_sent + stats.ipv6_bytes_rcvd) * 100 / tot_bytes) : 0;
mIPv6BytesPercentage.setText(
String.format(Utils.getPrimaryLocale(this),"%d%%", percentage));

mPacketsSent.setText(Utils.formatIntShort(stats.pkts_sent));
mPacketsRcvd.setText(Utils.formatIntShort(stats.pkts_rcvd));
mActiveConns.setText(Utils.formatNumber(this, stats.active_conns));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ private void refreshFilterInfo() {
private void onStatsUpdate(CaptureStats stats) {
Log.d("MainReceiver", "Got StatsUpdate: bytes_sent=" + stats.pkts_sent + ", bytes_rcvd=" +
stats.bytes_rcvd + ", pkts_sent=" + stats.pkts_sent + ", pkts_rcvd=" + stats.pkts_rcvd);

mCaptureStatus.setText(Utils.formatBytes(stats.bytes_sent + stats.bytes_rcvd));
mCaptureStatus.setText(Utils.formatBytes(CaptureService.getBytes()));
}

private Pair<String, Drawable> getAppFilterTextAndIcon(@NonNull Context context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class CaptureStats implements Serializable {
public String alloc_summary;
public long bytes_sent;
public long bytes_rcvd;
public long ipv6_bytes_sent;
public long ipv6_bytes_rcvd;
public long pcap_dump_size;
public int pkts_sent;
public int pkts_rcvd;
Expand All @@ -38,13 +40,16 @@ public class CaptureStats implements Serializable {

/* Invoked by native code */
public void setData(String _alloc_summary,
long _bytes_sent, long _bytes_rcvd, long _pcap_dump_size,
int _pkts_sent, int _pkts_rcvd,
long _bytes_sent, long _bytes_rcvd,
long _ipv6_bytes_sent, long _ipv6_bytes_rcvd,
long _pcap_dump_size, int _pkts_sent, int _pkts_rcvd,
int _pkts_dropped, int _num_dropped_conns, int _num_open_sockets,
int _max_fd, int _active_conns, int _tot_conns, int _num_dns_queries) {
alloc_summary = _alloc_summary;
bytes_sent = _bytes_sent;
bytes_rcvd = _bytes_rcvd;
ipv6_bytes_sent = _ipv6_bytes_sent;
ipv6_bytes_rcvd = _ipv6_bytes_rcvd;
pcap_dump_size = _pcap_dump_size;
pkts_sent = _pkts_sent;
pkts_rcvd = _pkts_rcvd;
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/jni/core/jni_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ static void sendStatsDump(pcapdroid_t *pd) {
(*env)->CallVoidMethod(env, stats_obj, mids.statsSetData,
allocs_summary,
capstats->sent_bytes, capstats->rcvd_bytes,
capstats->ipv6_sent_bytes, capstats->ipv6_rcvd_bytes,
(jlong)(pd->pcap_dump.dumper ? pcap_get_dump_size(pd->pcap_dump.dumper) : 0),
capstats->sent_pkts, capstats->rcvd_pkts,
min(pd->num_dropped_pkts, INT_MAX), pd->num_dropped_connections,
Expand Down Expand Up @@ -536,7 +537,7 @@ static void init_jni(JNIEnv *env) {
mids.connUpdateSetInfo = jniGetMethodID(env, cls.conn_update, "setInfo", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V");
mids.connUpdateSetPayload = jniGetMethodID(env, cls.conn_update, "setPayload", "(Ljava/util/ArrayList;Z)V");
mids.statsInit = jniGetMethodID(env, cls.stats, "<init>", "()V");
mids.statsSetData = jniGetMethodID(env, cls.stats, "setData", "(Ljava/lang/String;JJJIIIIIIIII)V");
mids.statsSetData = jniGetMethodID(env, cls.stats, "setData", "(Ljava/lang/String;JJJJJIIIIIIIII)V");
mids.blacklistStatusInit = jniGetMethodID(env, cls.blacklist_status, "<init>", "(Ljava/lang/String;I)V");
mids.listSize = jniGetMethodID(env, cls.list, "size", "()I");
mids.listGet = jniGetMethodID(env, cls.list, "get", "(I)Ljava/lang/Object;");
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/jni/core/pcapdroid.c
Original file line number Diff line number Diff line change
Expand Up @@ -1131,11 +1131,17 @@ void pd_account_stats(pcapdroid_t *pd, pkt_context_t *pctx) {
data->sent_bytes += pkt->len;
pd->capture_stats.sent_pkts++;
pd->capture_stats.sent_bytes += pkt->len;
if(pkt->tuple.ipver == 6) {
pd->capture_stats.ipv6_sent_bytes += pkt->len;
}
} else {
data->rcvd_pkts++;
data->rcvd_bytes += pkt->len;
pd->capture_stats.rcvd_pkts++;
pd->capture_stats.rcvd_bytes += pkt->len;
if(pkt->tuple.ipver == 6) {
pd->capture_stats.ipv6_rcvd_bytes += pkt->len;
}
}

/* New stats to notify */
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/jni/core/pcapdroid.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
typedef struct {
jlong sent_bytes;
jlong rcvd_bytes;
jlong ipv6_sent_bytes;
jlong ipv6_rcvd_bytes;
jint sent_pkts;
jint rcvd_pkts;

Expand Down
54 changes: 54 additions & 0 deletions app/src/main/res/layout/activity_stats.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,60 @@ android:layout_height="fill_parent">
android:textIsSelectable="true" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="4dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:textStyle="bold"
android:text="@string/ipv6_bytes_sent" />
<TextView
android:id="@+id/ipv6_bytes_sent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:textIsSelectable="true" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="4dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:textStyle="bold"
android:text="@string/ipv6_bytes_rcvd" />
<TextView
android:id="@+id/ipv6_bytes_rcvd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:textIsSelectable="true" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="4dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:textStyle="bold"
android:text="@string/ipv6_bytes_percentage" />
<TextView
android:id="@+id/ipv6_bytes_percentage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:textIsSelectable="true" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
<string name="open_sockets">开放套接字</string>
<string name="bytes_sent">已发送字节数</string>
<string name="bytes_rcvd">已接收字节数</string>
<string name="ipv6_bytes_sent">已发送IPv6字节数</string>
<string name="ipv6_bytes_rcvd">已接收IPv6字节数</string>
<string name="ipv6_bytes_percentage">IPv6字节数占比</string>
<string name="packets_sent">已发送数据包</string>
<string name="packets_rcvd">已接收数据包</string>
<string name="dns_queries">DNS 查询</string>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
<string name="open_sockets">Open sockets</string>
<string name="bytes_sent">Bytes sent</string>
<string name="bytes_rcvd">Bytes received</string>
<string name="ipv6_bytes_sent">IPv6 bytes sent</string>
<string name="ipv6_bytes_rcvd">IPv6 bytes received</string>
<string name="ipv6_bytes_percentage">IPv6 bytes percentage</string>
<string name="packets_sent">Packets sent</string>
<string name="packets_rcvd">Packets received</string>
<string name="dns_queries">DNS queries</string>
Expand Down
2 changes: 2 additions & 0 deletions docs/app_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ In the result of the `stop` and `get_status` actions and in the broadcast of `Ca
| pkts_sent | int | 50 | packets sent |
| pkts_rcvd | int | 50 | packets received |
| pkts_dropped | int | 50 | in root mode, number of packets not analyzed and not dumped |
| ipv6_bytes_sent | long | 74 | IPv6 bytes sent (from the device to the Internet) |
| ipv6_bytes_recv | long | 74 | IPv6 bytes received (from the Internet to the device) |

## Dumping PCAP to file

Expand Down

0 comments on commit 4018db9

Please sign in to comment.