KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
addresshelper.h
1/**
2 * AddressHelper
3 *
4 * Some static helper methods for address resolution and parsing
5 *
6 * Stephen Punak, July 08 2019
7 */
8#ifndef ADDRESSHELPER_H
9#define ADDRESSHELPER_H
10#include <QHostAddress>
11#include <QString>
12#include "kanoopcommon.h"
13
14/**
15 * @brief Static helper methods for network address resolution and parsing.
16 */
17class KANOOP_EXPORT AddressHelper
18{
19public:
20 /**
21 * @brief Parse a combined "address:port" string into its components.
22 * @param addressString Combined address and port string (e.g. "192.168.1.1:8080")
23 * @param address Output string receiving the address portion
24 * @param port Output variable receiving the port number
25 * @return true if parsing succeeded, false otherwise
26 */
27 static bool tryParseAddressPort(const QString& addressString, QString& address, quint16& port);
28
29 /**
30 * @brief Parse a combined "address:port" string into a QHostAddress and port.
31 * @param addressString Combined address and port string (e.g. "192.168.1.1:8080")
32 * @param address Output QHostAddress receiving the parsed address
33 * @param port Output variable receiving the port number
34 * @return true if parsing succeeded, false otherwise
35 */
36 static bool tryParseAddressPort(const QString& addressString, QHostAddress& address, quint16& port);
37
38 /**
39 * @brief Resolve a hostname to its IPv4 address.
40 * @param hostName Hostname or IP address string to resolve
41 * @return Resolved QHostAddress, or QHostAddress() on failure
42 */
43 static QHostAddress resolveIPv4Address(const QString& hostName);
44
45 /**
46 * @brief Get the local machine's primary IPv4 address.
47 * @return The local IPv4 QHostAddress
48 */
49 static QHostAddress getLocalIP();
50
51 /**
52 * @brief Get the local machine's IPv4 address, filtered by allowed interface names.
53 * @param allowedInterfaces List of interface name patterns to consider
54 * @return The matching local IPv4 QHostAddress
55 */
56 static QHostAddress getLocalIP(const QStringList& allowedInterfaces);
57
58private:
59 /** @brief Return the index of the first regex in regexList that matches ifName. */
60 static int indexOfRegEx(const QStringList &regexList, const QString& ifName);
61};
62
63#endif // ADDRESSHELPER_H
AddressHelper.
static QHostAddress getLocalIP()
Get the local machine's primary IPv4 address.
static bool tryParseAddressPort(const QString &addressString, QHostAddress &address, quint16 &port)
Parse a combined "address:port" string into a QHostAddress and port.
static QHostAddress resolveIPv4Address(const QString &hostName)
Resolve a hostname to its IPv4 address.
static QHostAddress getLocalIP(const QStringList &allowedInterfaces)
Get the local machine's IPv4 address, filtered by allowed interface names.
static bool tryParseAddressPort(const QString &addressString, QString &address, quint16 &port)
Parse a combined "address:port" string into its components.