2016-06-15 03:32:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
|
2021-01-03 10:48:15 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2018-11-01 18:36:41 +00:00
|
|
|
#include "common.hpp"
|
|
|
|
#include "components/logger.hpp"
|
2021-01-03 10:48:15 +00:00
|
|
|
#include "errors.hpp"
|
|
|
|
#include "settings.hpp"
|
2018-11-01 18:36:41 +00:00
|
|
|
#include "utils/math.hpp"
|
|
|
|
|
2018-10-30 19:27:44 +00:00
|
|
|
#if WITH_LIBNL
|
|
|
|
#include <net/if.h>
|
|
|
|
|
|
|
|
struct nl_msg;
|
|
|
|
struct nlattr;
|
|
|
|
#else
|
|
|
|
#include <iwlib.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* wirless_tools 29 (and possibly earlier) redefines 'inline' in iwlib.h
|
|
|
|
* With clang this leads to a conflict in the POLYBAR_NS macro
|
|
|
|
* wirless_tools 30 doesn't have that issue anymore
|
|
|
|
*/
|
2016-06-15 03:32:35 +00:00
|
|
|
#ifdef inline
|
|
|
|
#undef inline
|
|
|
|
#endif
|
2018-10-30 19:27:44 +00:00
|
|
|
#endif
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2017-01-01 14:10:32 +00:00
|
|
|
class file_descriptor;
|
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
namespace net {
|
|
|
|
DEFINE_ERROR(network_error);
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
bool is_wireless_interface(const string& ifname);
|
2021-01-03 10:48:15 +00:00
|
|
|
std::string find_wireless_interface();
|
|
|
|
std::string find_wired_interface();
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
// types {{{
|
|
|
|
|
2016-10-18 07:34:35 +00:00
|
|
|
struct quality_range {
|
2016-11-14 15:27:58 +00:00
|
|
|
int val{0};
|
|
|
|
int max{0};
|
2016-10-18 07:34:35 +00:00
|
|
|
|
|
|
|
int percentage() const {
|
2016-12-23 07:13:32 +00:00
|
|
|
if (val < 0) {
|
|
|
|
return std::max(std::min(std::abs(math_util::percentage(val, max, -20)), 100), 0);
|
|
|
|
}
|
|
|
|
return std::max(std::min(math_util::percentage(val, 0, max), 100), 0);
|
2016-10-18 07:34:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
using bytes_t = unsigned int;
|
|
|
|
|
|
|
|
struct link_activity {
|
2016-11-14 15:27:58 +00:00
|
|
|
bytes_t transmitted{0};
|
|
|
|
bytes_t received{0};
|
2017-01-11 02:07:28 +00:00
|
|
|
std::chrono::system_clock::time_point time;
|
2016-06-15 03:32:35 +00:00
|
|
|
};
|
|
|
|
|
2016-10-18 07:34:35 +00:00
|
|
|
struct link_status {
|
|
|
|
string ip;
|
2018-06-10 20:51:43 +00:00
|
|
|
string ip6;
|
2016-11-14 15:27:58 +00:00
|
|
|
link_activity previous{};
|
|
|
|
link_activity current{};
|
2016-06-15 03:32:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// }}}
|
2016-11-02 19:22:45 +00:00
|
|
|
// class : network {{{
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
class network {
|
|
|
|
public:
|
2016-11-02 19:22:45 +00:00
|
|
|
explicit network(string interface);
|
2017-01-01 14:10:32 +00:00
|
|
|
virtual ~network() {}
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-11-14 11:42:58 +00:00
|
|
|
virtual bool query(bool accumulate = false);
|
2016-10-18 07:34:35 +00:00
|
|
|
virtual bool connected() const = 0;
|
2016-11-02 19:22:45 +00:00
|
|
|
virtual bool ping() const;
|
2016-10-18 07:34:35 +00:00
|
|
|
|
2016-11-02 19:22:45 +00:00
|
|
|
string ip() const;
|
2018-06-10 20:51:43 +00:00
|
|
|
string ip6() const;
|
2020-11-29 13:15:27 +00:00
|
|
|
string downspeed(int minwidth = 3, const string& unit = "B/s") const;
|
|
|
|
string upspeed(int minwidth = 3, const string& unit = "B/s") const;
|
2017-11-03 06:05:26 +00:00
|
|
|
void set_unknown_up(bool unknown = true);
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-10-18 07:34:35 +00:00
|
|
|
protected:
|
2018-11-28 15:09:00 +00:00
|
|
|
void check_tuntap_or_bridge();
|
2016-11-02 19:22:45 +00:00
|
|
|
bool test_interface() const;
|
2020-11-29 13:15:27 +00:00
|
|
|
string format_speedrate(float bytes_diff, int minwidth, const string& unit) const;
|
2018-06-10 20:51:43 +00:00
|
|
|
void query_ip6();
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2018-06-10 20:51:43 +00:00
|
|
|
const logger& m_log;
|
2017-01-01 14:10:32 +00:00
|
|
|
unique_ptr<file_descriptor> m_socketfd;
|
2016-11-14 15:27:58 +00:00
|
|
|
link_status m_status{};
|
2016-06-15 03:32:35 +00:00
|
|
|
string m_interface;
|
2016-11-14 15:27:58 +00:00
|
|
|
bool m_tuntap{false};
|
2018-11-28 15:09:00 +00:00
|
|
|
bool m_bridge{false};
|
2017-11-03 06:05:26 +00:00
|
|
|
bool m_unknown_up{false};
|
2016-06-15 03:32:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// }}}
|
2016-11-02 19:22:45 +00:00
|
|
|
// class : wired_network {{{
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
class wired_network : public network {
|
|
|
|
public:
|
2016-10-18 07:34:35 +00:00
|
|
|
explicit wired_network(string interface) : network(interface) {}
|
|
|
|
|
2016-11-14 11:42:58 +00:00
|
|
|
bool query(bool accumulate = false) override;
|
2016-11-02 19:22:45 +00:00
|
|
|
bool connected() const override;
|
|
|
|
string linkspeed() const;
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
private:
|
2016-11-14 15:27:58 +00:00
|
|
|
int m_linkspeed{0};
|
2016-06-15 03:32:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// }}}
|
2018-06-19 03:16:09 +00:00
|
|
|
|
|
|
|
#if WITH_LIBNL
|
|
|
|
// class : wireless_network {{{
|
|
|
|
|
|
|
|
class wireless_network : public network {
|
|
|
|
public:
|
|
|
|
wireless_network(string interface) : network(interface), m_ifid(if_nametoindex(interface.c_str())){};
|
|
|
|
|
|
|
|
bool query(bool accumulate = false) override;
|
|
|
|
bool connected() const override;
|
|
|
|
string essid() const;
|
|
|
|
int signal() const;
|
|
|
|
int quality() const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static int scan_cb(struct nl_msg* msg, void* instance);
|
|
|
|
|
|
|
|
bool associated_or_joined(struct nlattr** bss);
|
|
|
|
void parse_essid(struct nlattr** bss);
|
|
|
|
void parse_frequency(struct nlattr** bss);
|
|
|
|
void parse_quality(struct nlattr** bss);
|
|
|
|
void parse_signal(struct nlattr** bss);
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned int m_ifid{};
|
|
|
|
string m_essid{};
|
|
|
|
int m_frequency{};
|
|
|
|
quality_range m_signalstrength{};
|
|
|
|
quality_range m_linkquality{};
|
|
|
|
};
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
#else
|
2016-11-02 19:22:45 +00:00
|
|
|
// class : wireless_network {{{
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
class wireless_network : public network {
|
|
|
|
public:
|
2016-10-18 07:34:35 +00:00
|
|
|
wireless_network(string interface) : network(interface) {}
|
|
|
|
|
2016-11-14 11:42:58 +00:00
|
|
|
bool query(bool accumulate = false) override;
|
2016-11-02 19:22:45 +00:00
|
|
|
bool connected() const override;
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-11-02 19:22:45 +00:00
|
|
|
string essid() const;
|
|
|
|
int signal() const;
|
|
|
|
int quality() const;
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-10-18 07:34:35 +00:00
|
|
|
protected:
|
2016-11-02 19:22:45 +00:00
|
|
|
void query_essid(const int& socket_fd);
|
|
|
|
void query_quality(const int& socket_fd);
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
private:
|
2016-12-15 02:30:41 +00:00
|
|
|
shared_ptr<wireless_info> m_info{};
|
|
|
|
string m_essid{};
|
2016-11-14 15:27:58 +00:00
|
|
|
quality_range m_signalstrength{};
|
|
|
|
quality_range m_linkquality{};
|
2016-06-15 03:32:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// }}}
|
2018-06-19 03:16:09 +00:00
|
|
|
#endif
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-10-18 07:34:35 +00:00
|
|
|
using wireless_t = unique_ptr<wireless_network>;
|
|
|
|
using wired_t = unique_ptr<wired_network>;
|
2018-06-19 03:16:09 +00:00
|
|
|
} // namespace net
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS_END
|