Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use netlink instead of Wireless Extensions #1879

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build-and-test-linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ jobs:
libimlib2-dev \
libircclient-dev \
libiw-dev \
libnl-3-dev\
libnl-genl-3-dev\
libnl-route-3-dev\
liblua5.3-dev \
libmicrohttpd-dev \
libmysqlclient-dev \
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ jobs:
libimlib2-dev \
libircclient-dev \
libiw-dev \
libnl-3-dev\
libnl-genl-3-dev\
libnl-route-3-dev\
liblua5.3-dev \
libmicrohttpd-dev \
libmysqlclient-dev \
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/publish-appimage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ jobs:
libimlib2-dev \
libircclient-dev \
libiw-dev \
libnl-3-dev\
libnl-genl-3-dev\
libnl-route-3-dev\
liblua5.3-dev \
libmicrohttpd-dev \
libmysqlclient-dev \
Expand Down
14 changes: 14 additions & 0 deletions 3rdparty/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
if(BUILD_WLAN AND OS_LINUX)
set(linux_net_libs_INCLUDE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/linux")

# Sourced from linux kernel
# SHA: 18daea77cca626f590fb140fc11e3a43c5d41354
# https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/
# Only added structures and data that's used by conky to avoid cloning entire kernel

set(conky_includes ${conky_includes} ${linux_net_libs_INCLUDE_PATH})
endif(BUILD_WLAN AND OS_LINUX)

# Reexport includes and libs
set(conky_includes ${conky_includes} PARENT_SCOPE)
set(conky_libs ${conky_libs} PARENT_SCOPE)
4 changes: 4 additions & 0 deletions 3rdparty/linux/linux/ieee80211.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef __CONKY_LINUX_IEEE80211_H__
#define __CONKY_LINUX_IEEE80211_H__

#endif /* __CONKY_LINUX_IEEE80211_H__ */
2,097 changes: 2,097 additions & 0 deletions 3rdparty/linux/uapi/linux/nl80211.h

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ configure_file(${CMAKE_MODULE_PATH}/build.h.in ${CMAKE_BINARY_DIR}/build.h)

set(conky_sources ${CMAKE_BINARY_DIR}/config.h ${CMAKE_BINARY_DIR}/build.h)

add_subdirectory(3rdparty)

# Finally, add some code
add_subdirectory(lua)
add_subdirectory(data)
Expand Down
25 changes: 12 additions & 13 deletions cmake/ConkyPlatformChecks.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -286,21 +286,20 @@ if(BUILD_MYSQL)
endif(BUILD_MYSQL)

if(BUILD_WLAN AND OS_LINUX)
set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
check_include_files(iwlib.h IWLIB_H)
find_package(NL REQUIRED)

if(NOT IWLIB_H)
message(FATAL_ERROR "Unable to find iwlib.h")
endif(NOT IWLIB_H)

find_library(IWLIB_LIB NAMES iw)

if(NOT IWLIB_LIB)
message(FATAL_ERROR "Unable to find libiw.so")
endif(NOT IWLIB_LIB)
if(NOT NL_FOUND)
message(FATAL_ERROR "Unable to find netlink library")
endif()
if(NOT HAVE_LIBNL_ROUTE)
message(FATAL_ERROR "Unable to find netlink route library")
endif()
if(NOT HAVE_LIBNL_GENL)
message(FATAL_ERROR "Unable to find netlink genl library")
endif()

set(conky_libs ${conky_libs} ${IWLIB_LIB})
check_function_exists(iw_sockets_open IWLIB_SOCKETS_OPEN_FUNC)
set(conky_includes ${conky_includes} ${NL_INCLUDE_DIRS})
set(conky_libs ${conky_libs} ${NL_LIBRARIES})
endif(BUILD_WLAN AND OS_LINUX)

if(BUILD_PORT_MONITORS)
Expand Down
123 changes: 123 additions & 0 deletions cmake/FindNL.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Find the native netlink includes and library
#
# If they exist, differentiate between versions 1, 2 and 3.
# Version 1 does not have netlink/version.h
# Version 2 started separating libraries (libnl{,-genl,-route}).
# Version 3 (>= 3.2) started appending the major version number as suffix to
# library names (libnl-3)
#
# NL_INCLUDE_DIRS - where to find libnl.h, etc.
# NL_LIBRARIES - List of libraries when using libnl.
# NL_FOUND - True if libnl found.

# Based on wireshark FindNL:
# https://github.com/wireshark/wireshark/blob/master/cmake/modules/FindNL.cmake
# Modified so it doesn't make any hard requirements for libraries used by the
# project.

if(NL_LIBRARIES AND NL_INCLUDE_DIRS)
# in cache already
SET(NL_FOUND TRUE)
else()
SET( SEARCHPATHS
/opt/local
/sw
/usr
/usr/local
)

find_package(PkgConfig)
pkg_check_modules(NL3 libnl-3.0 libnl-genl-3.0 libnl-route-3.0)
if(NOT NL3_FOUND)
pkg_search_module(NL2 libnl-2.0)
if (NL2_FOUND)
mark_as_advanced(NL2_INCLUDE_DIR NL2_LIBRARY)
endif()
endif()

# Try to find NL 2.0, 3.0 or 3.1 (/usr/include/netlink/version.h) or
# NL >= 3.2 (/usr/include/libnl3/netlink/version.h)
find_path(NL3_INCLUDE_DIR
PATH_SUFFIXES
include/libnl3
include
NAMES
netlink/version.h
HINTS
"${NL3_libnl-3.0_includeDIR}"
"${NL2_includeDIR}"
PATHS
$(SEARCHPATHS)
)

# NL version >= 2
if(NL3_INCLUDE_DIR)
find_library(NL3_LIBRARY
NAMES
nl-3 nl
PATH_SUFFIXES
lib64 lib
HINTS
"${NL3_libnl-3.0_LIBDIR}"
"${NL2_LIBDIR}"
PATHS
$(SEARCHPATHS)
)

if(NL3_LIBRARY)
set(NL_LIBRARIES ${NL_LIBRARIES} ${NL3_LIBRARY})
set(NL_INCLUDE_DIRS ${NL_INCLUDE_DIRS} ${NL3_INCLUDE_DIR})
set(HAVE_LIBNL 1)
mark_as_advanced(NL3_LIBRARY HAVE_LIBNL)
else()

endif()

find_library(NLGENL_LIBRARY
NAMES
nl-genl-3 nl-genl
PATH_SUFFIXES
lib64 lib
HINTS
"${NL3_libnl-genl-3.0_LIBDIR}"
"${NL2_LIBDIR}"
PATHS
$(SEARCHPATHS)
)

if(NLGENL_LIBRARY)
mark_as_advanced(NLGENL_LIBRARY)
set(NL_LIBRARIES ${NL_LIBRARIES} ${NLGENL_LIBRARY})
set(HAVE_LIBNL_GENL 1)
endif()

find_library(NLROUTE_LIBRARY
NAMES
nl-route-3 nl-route
PATH_SUFFIXES
lib64 lib
HINTS
"${NL3_libnl-route-3.0_LIBDIR}"
"${NL2_LIBDIR}"
PATHS
$(SEARCHPATHS)
)

if(NLROUTE_LIBRARY)
mark_as_advanced(NLROUTE_LIBRARY)
set(NL_LIBRARIES ${NL_LIBRARIES} ${NLROUTE_LIBRARY})
set(HAVE_LIBNL_ROUTE 1)
endif()

if(NOT NL3_FOUND AND NL2_FOUND)
set(HAVE_LIBNL2 1)
else()
set(HAVE_LIBNL3 1)
endif()
endif()
endif()

include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(NL DEFAULT_MSG NL_LIBRARIES NL_INCLUDE_DIRS)
mark_as_advanced(NL_LIBRARIES NL_INCLUDE_DIRS)

5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ if(BUILD_MYSQL)
set(optional_sources ${optional_sources} ${mysql})
endif(BUILD_MYSQL)

if(BUILD_WLAN)
set(netlink-conky netlink-conky.cc netlink-conky.h)
set(optional_sources ${optional_sources} ${netlink-conky})
endif(BUILD_WLAN)

if(BUILD_MOC)
set(moc moc.cc moc.h)
set(optional_sources ${optional_sources} ${moc})
Expand Down
95 changes: 14 additions & 81 deletions src/linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
#endif

#ifdef BUILD_WLAN
#include <iwlib.h>
#include "netlink-conky.h"
#endif

struct sysfs {
Expand Down Expand Up @@ -486,18 +486,19 @@ void print_gateway_ip(struct text_object *obj, char *p,
snprintf(p, p_max_size, "%s", gw_info.ip);
}

#ifdef BUILD_WLAN
static net_device_cache nl_cache;
#endif /* BUILD_WLAN */

void update_net_interfaces(FILE *net_dev_fp, bool is_first_update,
double time_between_updates) {
/* read each interface */
#ifdef BUILD_WLAN
// wireless info variables
struct wireless_info *winfo;
struct iwreq wrq;
#endif
nl_cache.update();
#endif /* BUILD_WLAN */

for (int i = 0; i < MAX_NET_INTERFACES; i++) {
struct net_stat *ns;
char *s, *p;
char *name, *p;
long long r, t, last_recv, last_trans;

/* quit only after all non-header lines from /proc/net/dev parsed */
Expand All @@ -509,7 +510,7 @@ void update_net_interfaces(FILE *net_dev_fp, bool is_first_update,
* of the interface name */
while (*p != '\0' && isspace((unsigned char)*p)) { p++; }

s = p;
name = p;

/* increment p until the end of the interface name has been reached */
while (*p != '\0' && *p != ':') { p++; }
Expand All @@ -519,14 +520,13 @@ void update_net_interfaces(FILE *net_dev_fp, bool is_first_update,
p++;

/* get pointer to interface statistics with the interface name in s */
ns = get_net_stat(s, nullptr, NULL);
ns = get_net_stat(name, nullptr, NULL);
ns->up = 1;
memset(&(ns->addr.sa_data), 0, 14);

memset(ns->addrs, 0,
17 * MAX_NET_INTERFACES +
1); /* Up to 17 chars per ip, max MAX_NET_INTERFACES interfaces.
Nasty memory usage... */
/* Up to 17 chars per ip, max MAX_NET_INTERFACES interfaces. Nasty memory
* usage... */
memset(ns->addrs, 0, 17 * MAX_NET_INTERFACES + 1);

/* bytes packets errs drop fifo frame compressed multicast|bytes ... */
sscanf(p, "%lld %*d %*d %*d %*d %*d %*d %*d %lld",
Expand Down Expand Up @@ -625,74 +625,7 @@ void update_net_interfaces(FILE *net_dev_fp, bool is_first_update,
}

#ifdef BUILD_WLAN
/* update wireless info */
winfo = (struct wireless_info *)malloc(sizeof(struct wireless_info));
memset(winfo, 0, sizeof(struct wireless_info));

int skfd = iw_sockets_open();
if (iw_get_basic_config(skfd, s, &(winfo->b)) > -1) {
// set present winfo variables
if (iw_get_range_info(skfd, s, &(winfo->range)) >= 0) {
winfo->has_range = 1;
}
if (iw_get_stats(skfd, s, &(winfo->stats), &winfo->range,
winfo->has_range) >= 0) {
winfo->has_stats = 1;
}
if (iw_get_ext(skfd, s, SIOCGIWAP, &wrq) >= 0) {
winfo->has_ap_addr = 1;
memcpy(&(winfo->ap_addr), &(wrq.u.ap_addr), sizeof(sockaddr));
}

// get bitrate
if (iw_get_ext(skfd, s, SIOCGIWRATE, &wrq) >= 0) {
memcpy(&(winfo->bitrate), &(wrq.u.bitrate), sizeof(iwparam));
iw_print_bitrate(ns->bitrate, 16, winfo->bitrate.value);
}

// get link quality
if (winfo->has_range && winfo->has_stats) {
bool has_qual_level = (winfo->stats.qual.level != 0) ||
(winfo->stats.qual.updated & IW_QUAL_DBM);

if (has_qual_level &&
!(winfo->stats.qual.updated & IW_QUAL_QUAL_INVALID)) {
ns->link_qual = winfo->stats.qual.qual;

if (winfo->range.max_qual.qual > 0) {
ns->link_qual_max = winfo->range.max_qual.qual;
}
}
}

// get ap mac
if (winfo->has_ap_addr) { iw_sawap_ntop(&winfo->ap_addr, ns->ap); }

// get essid
if (winfo->b.has_essid) {
if (winfo->b.essid_on) {
snprintf(ns->essid, 34, "%s", winfo->b.essid);
} else {
snprintf(ns->essid, 34, "%s", "off/any");
}
}

// get channel and freq
if (winfo->b.has_freq) {
if (winfo->has_range == 1) {
ns->channel = iw_freq_to_channel(winfo->b.freq, &(winfo->range));
iw_print_freq_value(ns->freq, 16, winfo->b.freq);
} else {
ns->channel = 0;
ns->freq[0] = 0;
}
}

snprintf(ns->mode, 16, "%s", iw_operation_mode[winfo->b.mode]);
}

iw_sockets_close(skfd);
free(winfo);
nl_cache.populate_interface(ns, name);
#endif
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@
*
*/

#include <getopt.h>
#include <cstdio>
#include <iostream>

#include "build.h"
#include "config.h"

#include "conky.h"
#include "display-output.hh"
#include "lua-config.hh"

#include <getopt.h>
#include <cstdio>
#include <iostream>

#ifdef BUILD_X11
#include "x11.h"
#endif /* BUILD_X11 */
Expand Down
2 changes: 1 addition & 1 deletion src/net_stat.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct net_stat {
int channel;
char freq[16];
char bitrate[16];
char mode[16];
char mode[64];
int link_qual;
int link_qual_max;
char ap[18];
Expand Down
Loading
Loading