sptk2 logo
SPTK Home Page
IPAddress.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#include <sptk5/sptk.h>
30
31#ifndef _WIN32
32#include <arpa/inet.h>
33#include <netdb.h>
34#include <netinet/in.h>
35#include <sptk5/Strings.h>
36#include <sys/socket.h>
37#include <sys/types.h>
38#include <sys/un.h>
39#include <unistd.h>
40
44using SOCKET = int;
45using SOCKET_ADDRESS_FAMILY = sa_family_t;
46
47#ifdef __APPLE__
48using socklen_t = int;
49#endif
50
54#define INVALID_SOCKET -1
55
56#else
57#include <winsock2.h>
58
59#include <windows.h>
60using socklen_t = int;
61using SOCKET_ADDRESS_FAMILY = unsigned short;
62#endif
63
64namespace sptk {
65
74class SP_EXPORT IPAddress
75{
79 union
80 {
81 sockaddr_in ipv4;
82 sockaddr_in6 ipv6;
83 sockaddr generic;
84 } m_address;
85
86 String m_addressStr;
87
88public:
93
98 explicit IPAddress(const sockaddr& address);
99
104 IPAddress(const IPAddress& other);
105
111
115 const sockaddr* address() const
116 {
117 return &m_address.generic;
118 }
119
124 size_t length() const
125 {
126 return addressLength(m_address.generic);
127 }
128
133 const String& toString() const
134 {
135 return m_addressStr;
136 }
137
143 static size_t addressLength(const sockaddr& address)
144 {
145 if (address.sa_family == AF_INET)
146 return sizeof(sockaddr_in);
147 return sizeof(sockaddr_in6);
148 }
149};
150
154} // namespace sptk
IPv4 and IPv6 address presentation.
Definition: IPAddress.h:75
const String & toString() const
Definition: IPAddress.h:133
static size_t addressLength(const sockaddr &address)
Definition: IPAddress.h:143
IPAddress(const IPAddress &other)
Copy constructor.
const sockaddr * address() const
Get address data.
Definition: IPAddress.h:115
IPAddress & operator=(const IPAddress &other)
Assignment.
IPAddress(const sockaddr &address)
Constructor.
IPAddress()
Default constructor.
size_t length() const
Definition: IPAddress.h:124
Definition: String.h:49

Fri Oct 14 2022 09:58:32: SPTK 5.4.1