---
name: "sg_get_network_iface_stats_r"
section: "3"
platform: "linux"
package: "libstatgrab"
version: "0.92.1-2"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/sg_get_network_iface_stats_r"
---

# sg\_get\_network\_iface\_stats\_r(3)

get network interface statistics

## NAME

sg\_get\_network\_iface\_stats, sg\_get\_network\_iface\_stats\_r, sg\_free\_network\_iface\_stats, sg\_network\_iface\_compare\_name - get network interface statistics

## SYNOPSIS

```
FC#include <statgrab.h>F[0]
```

FCsg\_network\_iface\_stats \***sg\_get\_network\_iface\_stats**F\[0\]  FC(size\_t \**entries*);F\[0\]

FCsg\_network\_iface\_stats \***sg\_get\_network\_iface\_stats\_r**F\[0\]  FC(size\_t \**entries*);F\[0\]

FCsg\_error **sg\_free\_network\_iface\_stats**F\[0\]  FC(sg\_network\_iface\_stats \**data*);F\[0\]

FCint **sg\_network\_iface\_compare\_name**F\[0\]  FC(const void \**va*, const void \**vb*);F\[0\]

## DESCRIPTION

The functions FC**sg\_get\_network\_iface\_stats**F\[0\]() and FC**sg\_get\_network\_iface\_stats\_r**F\[0\]() return statistics about the network interfaces in the machine. Specifically, they return the speed of the interface, the duplex state, and whether it is currently up. Both functions take an optional FCentriesF\[0\] parameter, which points (when given) to a size\_t to take the number of returned vector entries.

**API Shortcut**

| | | |
| --- | --- | --- |
| function | returns | data owner |
| sg\_get\_network\_iface\_stats | FCsg\_network\_iface\_statsF\[0\] \* | libstatgrab (thread local) |
| sg\_get\_network\_iface\_stats\_r | FCsg\_network\_iface\_statsF\[0\] \* | caller |

The FCsg\_network\_iface\_statsF\[0\] vectors received from FC**sg\_get\_network\_iface\_stats\_r**F\[0\]() must be freed using FC**sg\_free\_network\_iface\_stats**F\[0\]() when not needed any more. The caller is responsible for doing it.

Additionally a support function for FC**qsort**F\[0\](3) in available: FC**sg\_network\_iface\_compare\_name**F\[0\]().

**Sort Example**

```
FC
size_t entries;
sg_network_iface_stats *network_iface_stats = NULL;
while( NULL != ( network_iface_stats = sg_get_network_iface_stats(&entries) ) ) {
    /* order entries alphabetically using the mountpoint */
    qsort( network_iface_stats, entries, sizeof(network_iface_stats[0]), &sg_network_iface_compare_name );
    show_network_iface_stats( network_iface_stats );
}
        F[0]
```

## RETURN VALUES

The FC**sg\_get\_network\_iface\_stats**F\[0\] returns a pointer to a structure of type FCsg\_network\_iface\_statsF\[0\].

```
FC
typedef enum{
        SG_IFACE_DUPLEX_FULL,
        SG_IFACE_DUPLEX_HALF,
        SG_IFACE_DUPLEX_UNKNOWN
}sg_iface_duplex;
    F[0]
```

Note: The FCSG\_IFACE\_DUPLEX\_UNKNOWNF\[0\] value could mean that duplex hasn't been negotiated yet.

```
FC
typedef struct {
        char *interface_name;
        int speed;
        sg_iface_duplex duplex;
        int up;
        time_t systime;
} sg_network_iface_stats;
    F[0]
```

- FCinterface\_nameF\[0\]

  The name known to the operating system. (eg. on linux it might be eth0)

- FCspeedF\[0\]

  The speed of the interface, in megabits/sec.

- FCduplexF\[0\]

  The duplex state the interface is in. See sg\_iface\_duplex for permitted values.

- FCupF\[0\]

  Whether the interface is up.

- FCsystimeF\[0\]

  The timestamp when the above stats where collected in seconds since epoch

## BUGS

Getting specific details may require elevated privileges, eg. on Linux the interface speed isn't visible to non-privileged processes.

The compare functions exist rather for backward compatibility than for functionality enhancements. Limited flexibility (e.g. reverse order) and lack of optimising opportunities for the compiler leads to the recommendation to implement the required compare routines locally.

## SEE ALSO

[**statgrab**(3)](/statgrab/3.md)

## WEBSITE

⟨https://libstatgrab.org/⟩
