2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/port/config.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
|
|
|
|
#if !TD_WINDOWS
|
|
|
|
#include <arpa/inet.h>
|
2018-11-02 16:58:20 +01:00
|
|
|
#include <netinet/in.h>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace td {
|
2018-05-18 15:13:35 +02:00
|
|
|
|
|
|
|
Result<string> idn_to_ascii(CSlice host);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class SocketFd;
|
2018-05-18 15:13:35 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class IPAddress {
|
|
|
|
public:
|
|
|
|
IPAddress();
|
|
|
|
|
|
|
|
bool is_valid() const;
|
|
|
|
bool is_ipv4() const;
|
2018-06-29 19:36:27 +02:00
|
|
|
bool is_ipv6() const;
|
|
|
|
|
2018-11-15 18:10:20 +01:00
|
|
|
bool is_reserved() const;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
int get_port() const;
|
|
|
|
void set_port(int port);
|
|
|
|
|
2018-06-29 19:36:27 +02:00
|
|
|
uint32 get_ipv4() const;
|
|
|
|
Slice get_ipv6() const;
|
|
|
|
Slice get_ip_str() const;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
IPAddress get_any_addr() const;
|
|
|
|
|
2019-02-04 04:32:10 +01:00
|
|
|
static Result<IPAddress> get_ipv4_address(CSlice host);
|
|
|
|
static Result<IPAddress> get_ipv6_address(CSlice host);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status init_ipv6_port(CSlice ipv6, int port) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status init_ipv6_as_ipv4_port(CSlice ipv4, int port) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status init_ipv4_port(CSlice ipv4, int port) TD_WARN_UNUSED_RESULT;
|
2018-07-01 03:12:20 +02:00
|
|
|
Status init_host_port(CSlice host, int port, bool prefer_ipv6 = false) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status init_host_port(CSlice host, CSlice port, bool prefer_ipv6 = false) TD_WARN_UNUSED_RESULT;
|
2018-12-31 20:04:05 +01:00
|
|
|
Status init_host_port(CSlice host_port) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status init_socket_address(const SocketFd &socket_fd) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status init_peer_address(const SocketFd &socket_fd) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
friend bool operator==(const IPAddress &a, const IPAddress &b);
|
|
|
|
friend bool operator<(const IPAddress &a, const IPAddress &b);
|
|
|
|
|
2018-06-29 19:36:27 +02:00
|
|
|
// for internal usage only
|
|
|
|
const sockaddr *get_sockaddr() const;
|
|
|
|
size_t get_sockaddr_len() const;
|
|
|
|
int get_address_family() const;
|
2018-09-11 20:49:39 +02:00
|
|
|
static CSlice ipv4_to_str(uint32 ipv4);
|
2019-07-06 13:29:15 +02:00
|
|
|
static CSlice ipv6_to_str(Slice ipv6);
|
2018-08-13 19:15:09 +02:00
|
|
|
Status init_sockaddr(sockaddr *addr);
|
|
|
|
Status init_sockaddr(sockaddr *addr, socklen_t len) TD_WARN_UNUSED_RESULT;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
union {
|
|
|
|
sockaddr sockaddr_;
|
|
|
|
sockaddr_in ipv4_addr_;
|
|
|
|
sockaddr_in6 ipv6_addr_;
|
|
|
|
};
|
2018-09-07 02:41:21 +02:00
|
|
|
static constexpr socklen_t storage_size() {
|
2018-08-13 19:15:09 +02:00
|
|
|
return sizeof(ipv6_addr_);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
bool is_valid_;
|
|
|
|
|
|
|
|
void init_ipv4_any();
|
|
|
|
void init_ipv6_any();
|
|
|
|
};
|
|
|
|
StringBuilder &operator<<(StringBuilder &builder, const IPAddress &address);
|
|
|
|
|
|
|
|
} // namespace td
|