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