Sese Framework  x.y.z
A cross-platform framework
载入中...
搜索中...
未找到
IPv4Address.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
21#pragma once
22
23#include "sese/net/IPAddress.h"
24
25namespace sese::net {
26
30class IPv4Address final : public IPAddress {
31public:
32 using Ptr = std::shared_ptr<IPv4Address>;
33 static IPv4Address::Ptr create(const char *address, uint16_t port);
34 static IPv4Address::Ptr localhost(uint16_t port = 0);
35 static IPv4Address::Ptr any(uint16_t port = 0);
36
37public:
38 explicit IPv4Address(const sockaddr_in &address);
39 explicit IPv4Address(uint32_t address = INADDR_ANY, uint16_t port = 0);
40
41public:
42 [[nodiscard]] sockaddr *getRawAddress() const noexcept override;
43 [[nodiscard]] socklen_t getRawAddressLength() const noexcept override;
44 [[nodiscard]] std::string getAddress() const noexcept override;
45
46 [[nodiscard]] IPAddress::Ptr getBroadcastAddress(uint32_t prefix_len) const noexcept override;
47 [[nodiscard]] IPAddress::Ptr getNetworkAddress(uint32_t prefix_len) const noexcept override;
48 [[nodiscard]] IPAddress::Ptr getSubnetMask(uint32_t prefix_len) const noexcept override;
49
50 void setPort(uint16_t port) noexcept override { address.sin_port = ToBigEndian16(port); }
51 [[nodiscard]] uint16_t getPort() const noexcept override {
52 //#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || defined(_WIN32)
53 // return ByteSwap16(address.sin_port);
54 //#else
55 // return address.sin_port;
56 //#endif
57 return FromBigEndian16(address.sin_port);
58 }
59
60 void setFamily(uint16_t family) noexcept override {
61 address.sin_family = family;
62 }
63 uint16_t getFamily() noexcept override {
64 return address.sin_family;
65 }
66
67private:
68 sockaddr_in address{0};
69};
70
71} // namespace sese::net