Files
fastfetch/src/modules/netio/netio.c
T

235 lines
8.2 KiB
C
Raw Normal View History

2023-09-26 16:26:15 +08:00
#include "common/printing.h"
#include "common/jsonconfig.h"
2025-07-30 14:30:36 +08:00
#include "common/size.h"
2023-09-27 00:05:37 +08:00
#include "detection/netio/netio.h"
#include "modules/netio/netio.h"
2023-09-26 16:26:15 +08:00
#include "util/stringUtils.h"
2023-09-27 00:05:37 +08:00
#define FF_NETIO_DISPLAY_NAME "Network IO"
2023-09-26 16:26:15 +08:00
2023-09-27 00:05:37 +08:00
static int sortInfs(const FFNetIOResult* left, const FFNetIOResult* right)
2023-09-26 16:26:15 +08:00
{
return ffStrbufComp(&left->name, &right->name);
}
2023-09-27 00:05:37 +08:00
static void formatKey(const FFNetIOOptions* options, FFNetIOResult* inf, uint32_t index, FFstrbuf* key)
2023-09-26 16:26:15 +08:00
{
if(options->moduleArgs.key.length == 0)
{
if(!inf->name.length)
ffStrbufSetF(&inf->name, "unknown %u", (unsigned) index);
2023-09-27 00:05:37 +08:00
ffStrbufSetF(key, FF_NETIO_DISPLAY_NAME " (%s)", inf->name.chars);
2023-09-26 16:26:15 +08:00
}
else
{
ffStrbufClear(key);
2024-12-11 10:47:02 +08:00
FF_PARSE_FORMAT_STRING_CHECKED(key, &options->moduleArgs.key, ((FFformatarg[]){
FF_FORMAT_ARG(index, "index"),
FF_FORMAT_ARG(inf->name, "name"),
FF_FORMAT_ARG(options->moduleArgs.keyIcon, "icon"),
}));
2023-09-26 16:26:15 +08:00
}
}
2023-09-27 00:05:37 +08:00
void ffPrintNetIO(FFNetIOOptions* options)
2023-09-26 16:26:15 +08:00
{
2023-09-27 00:05:37 +08:00
FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFNetIOResult));
const char* error = ffDetectNetIO(&result, options);
2023-09-26 16:26:15 +08:00
if(error)
{
ffPrintError(FF_NETIO_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
2023-09-26 16:26:15 +08:00
return;
}
ffListSort(&result, (const void*) sortInfs);
uint32_t index = 0;
FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate();
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
FF_STRBUF_AUTO_DESTROY buffer2 = ffStrbufCreate();
2023-09-27 00:05:37 +08:00
FF_LIST_FOR_EACH(FFNetIOResult, inf, result)
2023-09-26 16:26:15 +08:00
{
formatKey(options, inf, result.length == 1 ? 0 : index + 1, &key);
ffStrbufClear(&buffer);
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY);
2023-09-26 16:26:15 +08:00
2025-07-30 14:30:36 +08:00
ffSizeAppendNum(inf->rxBytes, &buffer);
2024-02-20 14:44:55 +08:00
if (!options->detectTotal) ffStrbufAppendS(&buffer, "/s");
ffStrbufAppendS(&buffer, " (IN) - ");
2025-07-30 14:30:36 +08:00
ffSizeAppendNum(inf->txBytes, &buffer);
2024-02-20 14:44:55 +08:00
if (!options->detectTotal) ffStrbufAppendS(&buffer, "/s");
ffStrbufAppendS(&buffer, " (OUT)");
if (inf->defaultRoute && !options->defaultRouteOnly)
2023-09-26 22:34:37 +08:00
ffStrbufAppendS(&buffer, " *");
2023-09-26 16:26:15 +08:00
ffStrbufPutTo(&buffer, stdout);
}
else
{
ffStrbufClear(&buffer2);
2025-07-30 14:30:36 +08:00
ffSizeAppendNum(inf->rxBytes, &buffer);
2024-02-20 14:44:55 +08:00
if (!options->detectTotal) ffStrbufAppendS(&buffer, "/s");
2025-07-30 14:30:36 +08:00
ffSizeAppendNum(inf->txBytes, &buffer2);
2024-02-20 14:44:55 +08:00
if (!options->detectTotal) ffStrbufAppendS(&buffer2, "/s");
2023-09-26 23:18:19 +08:00
2024-12-11 10:47:02 +08:00
FF_PRINT_FORMAT_CHECKED(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, ((FFformatarg[]){
FF_FORMAT_ARG(buffer, "rx-size"),
FF_FORMAT_ARG(buffer2, "tx-size"),
FF_FORMAT_ARG(inf->name, "ifname"),
FF_FORMAT_ARG(inf->defaultRoute, "is-default-route"),
FF_FORMAT_ARG(inf->txBytes, "tx-bytes"),
FF_FORMAT_ARG(inf->rxBytes, "rx-bytes"),
FF_FORMAT_ARG(inf->txPackets, "tx-packets"),
FF_FORMAT_ARG(inf->rxPackets, "rx-packets"),
FF_FORMAT_ARG(inf->rxErrors, "rx-errors"),
FF_FORMAT_ARG(inf->txErrors, "tx-errors"),
FF_FORMAT_ARG(inf->rxDrops, "rx-drops"),
FF_FORMAT_ARG(inf->txDrops, "tx-drops"),
}));
2023-09-26 16:26:15 +08:00
}
++index;
}
2023-09-26 22:34:37 +08:00
2023-09-27 00:05:37 +08:00
FF_LIST_FOR_EACH(FFNetIOResult, inf, result)
2023-09-26 22:34:37 +08:00
{
ffStrbufDestroy(&inf->name);
}
2023-09-26 16:26:15 +08:00
}
2023-09-27 00:05:37 +08:00
void ffParseNetIOJsonObject(FFNetIOOptions* options, yyjson_val* module)
2023-09-26 16:26:15 +08:00
{
yyjson_val *key, *val;
2023-09-26 16:26:15 +08:00
size_t idx, max;
yyjson_obj_foreach(module, idx, max, key, val)
2023-09-26 16:26:15 +08:00
{
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
continue;
if (unsafe_yyjson_equals_str(key, "namePrefix"))
2023-09-26 22:34:37 +08:00
{
ffStrbufSetJsonVal(&options->namePrefix, val);
2023-09-26 22:34:37 +08:00
continue;
}
if (unsafe_yyjson_equals_str(key, "defaultRouteOnly"))
2023-09-26 22:34:37 +08:00
{
options->defaultRouteOnly = yyjson_get_bool(val);
continue;
}
if (unsafe_yyjson_equals_str(key, "detectTotal"))
2024-02-20 14:44:55 +08:00
{
options->detectTotal = yyjson_get_bool(val);
continue;
}
if (unsafe_yyjson_equals_str(key, "waitTime"))
{
options->waitTime = (uint32_t) yyjson_get_uint(val);
continue;
}
ffPrintError(FF_NETIO_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
2023-09-26 16:26:15 +08:00
}
}
2023-10-24 14:41:29 +08:00
void ffGenerateNetIOJsonConfig(FFNetIOOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
2023-10-24 14:41:29 +08:00
yyjson_mut_obj_add_strbuf(doc, module, "namePrefix", &options->namePrefix);
2023-10-24 14:41:29 +08:00
yyjson_mut_obj_add_bool(doc, module, "defaultRouteOnly", options->defaultRouteOnly);
2023-10-24 14:41:29 +08:00
yyjson_mut_obj_add_bool(doc, module, "detectTotal", options->detectTotal);
2024-02-20 14:44:55 +08:00
yyjson_mut_obj_add_uint(doc, module, "waitTime", options->waitTime);
2023-10-24 14:41:29 +08:00
}
2023-10-23 10:14:22 +08:00
void ffGenerateNetIOJsonResult(FFNetIOOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
2023-09-26 16:26:15 +08:00
{
2023-09-27 00:05:37 +08:00
FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFNetIOResult));
const char* error = ffDetectNetIO(&result, options);
2023-09-26 16:26:15 +08:00
if(error)
{
yyjson_mut_obj_add_str(doc, module, "error", error);
return;
}
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
2023-09-27 00:05:37 +08:00
FF_LIST_FOR_EACH(FFNetIOResult, counter, result)
2023-09-26 16:26:15 +08:00
{
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
yyjson_mut_obj_add_strbuf(doc, obj, "name", &counter->name);
2023-09-26 23:18:19 +08:00
yyjson_mut_obj_add_bool(doc, obj, "defaultRoute", counter->defaultRoute);
2023-09-26 16:26:15 +08:00
yyjson_mut_obj_add_uint(doc, obj, "txBytes", counter->txBytes);
yyjson_mut_obj_add_uint(doc, obj, "rxBytes", counter->rxBytes);
yyjson_mut_obj_add_uint(doc, obj, "txPackets", counter->txPackets);
yyjson_mut_obj_add_uint(doc, obj, "rxPackets", counter->rxPackets);
yyjson_mut_obj_add_uint(doc, obj, "rxErrors", counter->rxErrors);
yyjson_mut_obj_add_uint(doc, obj, "txErrors", counter->txErrors);
yyjson_mut_obj_add_uint(doc, obj, "rxDrops", counter->rxDrops);
yyjson_mut_obj_add_uint(doc, obj, "txDrops", counter->txDrops);
2023-09-26 22:34:37 +08:00
}
2023-09-26 16:26:15 +08:00
2023-09-27 00:05:37 +08:00
FF_LIST_FOR_EACH(FFNetIOResult, inf, result)
2023-09-26 22:34:37 +08:00
{
ffStrbufDestroy(&inf->name);
2023-09-26 16:26:15 +08:00
}
}
2023-10-07 13:40:40 +08:00
void ffInitNetIOOptions(FFNetIOOptions* options)
{
ffOptionInitModuleArg(&options->moduleArgs, "󰾆");
ffStrbufInit(&options->namePrefix);
options->defaultRouteOnly =
#if __ANDROID__
false
#else
true
#endif
;
options->detectTotal = false;
options->waitTime = 1000;
}
void ffDestroyNetIOOptions(FFNetIOOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
ffStrbufDestroy(&options->namePrefix);
}
2025-08-03 22:50:56 +08:00
FFModuleBaseInfo ffNetIOModuleInfo = {
2024-12-11 10:47:02 +08:00
.name = FF_NETIO_MODULE_NAME,
.description = "Print network I/O throughput",
.initOptions = (void*) ffInitNetIOOptions,
.destroyOptions = (void*) ffDestroyNetIOOptions,
2024-12-11 10:47:02 +08:00
.parseJsonObject = (void*) ffParseNetIOJsonObject,
.printModule = (void*) ffPrintNetIO,
.generateJsonResult = (void*) ffGenerateNetIOJsonResult,
.generateJsonConfig = (void*) ffGenerateNetIOJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
{"Size of data received [per second] (formatted)", "rx-size"},
{"Size of data sent [per second] (formatted)", "tx-size"},
{"Interface name", "ifname"},
{"Is default route", "is-default-route"},
{"Size of data received [per second] (in bytes)", "rx-bytes"},
{"Size of data sent [per second] (in bytes)", "tx-bytes"},
{"Number of packets received [per second]", "rx-packets"},
{"Number of packets sent [per second]", "tx-packets"},
{"Number of errors received [per second]", "rx-errors"},
{"Number of errors sent [per second]", "tx-errors"},
{"Number of packets dropped when receiving [per second]", "rx-drops"},
{"Number of packets dropped when sending [per second]", "tx-drops"},
}))
};