Sese Framework  x.y.z
A cross-platform framework
载入中...
搜索中...
未找到
Address.h
浏览该文件的文档.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
22#pragma once
23
24#ifdef _WIN32
25#ifdef __MINGW32__
26#define _WIN32_WINNET 0x602
27#endif
28#include <ws2tcpip.h>
29#else
30#include <sys/socket.h>
31#include <arpa/inet.h>
32#include <netdb.h>
33#endif
34
35#include "sese/Config.h"
36
37#include <memory>
38#include <string>
39#include <vector>
40
41
42namespace sese::net {
43
44template<typename T>
45static T CreateMask(uint32_t bits) {
46 return (1 << (sizeof(T) * 8 - bits)) - 1;
47}
48
52class Address {
53public:
54 using Ptr = std::shared_ptr<Address>;
55
56public:
57 static Ptr create(const sockaddr *address, socklen_t address_len);
58
59 static bool lookUp(std::vector<Address::Ptr> &result, const std::string &host, int family = AF_INET, int type = 0, int protocol = 0);
60
61 static Address::Ptr lookUpAny(const std::string &host, int family = AF_INET, int type = 0, int protocol = 0);
62
63public:
64 virtual ~Address() = default;
65 [[nodiscard]] virtual sockaddr *getRawAddress() const noexcept = 0;
66 [[nodiscard]] virtual socklen_t getRawAddressLength() const noexcept = 0;
67 [[nodiscard]] virtual std::string getAddress() const noexcept = 0;
68};
69
70extern "C" int inetPton(int af, const char *src, void *dst) noexcept;
71} // namespace sese::net