-
Notifications
You must be signed in to change notification settings - Fork 17
/
ip.h
71 lines (60 loc) · 2 KB
/
ip.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#pragma once
/*
* Copyright (C) 2020 by Thomas Early N7TAE
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <iostream>
#include <cstring>
#include <chrono>
#include <strings.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
class CIp
{
public:
// constructors
CIp();
CIp(const char *address, int family = AF_UNSPEC, int type = SOCK_DGRAM, uint16_t port = 0U);
CIp(const int family, const uint16_t port = 0U, const char *address = nullptr);
// initializer for empty constructor
void Initialize(const int family, const uint16_t port = 0U, const char *address = nullptr);
// comparison operators
bool operator==(const CIp &rhs) const;
bool operator!=(const CIp &rhs) const;
// state methods
bool IsSet() const { return is_set; }
bool AddressIsZero() const;
void ClearAddress();
const char *GetAddress() const;
operator const char *() const { return GetAddress(); }
friend std::ostream &operator<<(std::ostream &stream, const CIp &Ip);
int GetFamily() const;
uint16_t GetPort() const;
size_t GetSize() const;
// modifiers
void SetPort(const uint16_t newport);
// for i/o
struct sockaddr *GetPointer();
const struct sockaddr *GetCPointer() const;
void Clear();
private:
struct sockaddr_storage addr;
mutable char straddr[INET6_ADDRSTRLEN];
bool is_set;
};