MLIB
Loading...
Searching...
No Matches
inaddr.h
Go to the documentation of this file.
1/*
2 Copyright (c) Mircea Neacsu (2014-2025) Licensed under MIT License.
3 This file is part of MLIB project. See LICENSE file for full license terms.
4*/
5
7
8#pragma once
9
10
11#if __has_include("defs.h")
12#include "defs.h"
13#endif
14
15#include "safe_winsock.h"
16#include <ostream>
17
18#include "errorcode.h"
19
20namespace mlib {
21
23class inaddr
24{
25public:
26 inaddr (unsigned long host = INADDR_ANY, unsigned short port = 0);
27 inaddr (const std::string& hostname, unsigned short port);
28 inaddr (const std::string& hostname, const std::string& service,
29 const std::string& proto = std::string ());
30
31 inaddr (const sockaddr& adr);
32 inaddr& operator= (const inaddr& rhs);
33
35 operator sockaddr* () const
36 {
37 return (sockaddr*)&sa;
38 };
39 bool operator== (const inaddr& other) const;
40 bool operator!= (const inaddr& other) const;
41
43 operator const sockaddr& () const
44 {
45 return *(sockaddr*)&sa;
46 };
47
49 operator sockaddr& ()
50 {
51 return *(sockaddr*)&sa;
52 };
53
55 unsigned short port () const
56 {
57 return ntohs (sa.sin_port);
58 };
59
61 void port (unsigned short p)
62 {
63 sa.sin_port = htons (p);
64 };
65
67 erc port (const std::string& service, const std::string& proto = std::string ());
68
70 unsigned host () const
71 {
72 return ntohl (sa.sin_addr.s_addr);
73 };
74
76 void host (unsigned int h)
77 {
78 sa.sin_addr.s_addr = htonl (h);
79 };
80
81 erc host (const std::string& hostname);
82
83 std::string hostname () const;
84
86 const char* ntoa () const;
87
89 bool is_multicast () const
90 {
91 return ((host () & 0xf0000000) == 0xe0000000);
92 };
93
94 static unsigned localhost ();
95
96private:
97 sockaddr_in sa;
98};
99
102{
103 memcpy (&sa, &rhs.sa, sizeof (sa));
104 return *this;
105}
106
108inline bool inaddr::operator== (const inaddr& other) const
109{
110 return (sa.sin_addr.S_un.S_addr == other.sa.sin_addr.S_un.S_addr)
111 && (sa.sin_port == other.sa.sin_port);
112}
113
115inline bool inaddr::operator!= (const inaddr& other) const
116{
117 return (!operator== (other));
118}
119
120} // namespace mlib
121
123inline std::string to_string (const mlib::inaddr& addr)
124{
125 return std::string (addr.ntoa ()) + ':' + std::to_string(addr.port ());
126}
127
128inline std::ostream& operator<< (std::ostream& strm, const mlib::inaddr& addr)
129{
130 strm << to_string(addr);
131 return strm;
132}
133
objects returned as a function result or thrown directly.
Definition errorcode.h:73
Wrapper for sockaddr structure.
Definition inaddr.h:24
static unsigned localhost()
Return local address in HOST order.
Definition inaddr.cpp:155
void host(unsigned int h)
set host address
Definition inaddr.h:76
std::string hostname() const
Find hostname from the host address in sockaddr.
Definition inaddr.cpp:132
void port(unsigned short p)
set port number
Definition inaddr.h:61
const char * ntoa() const
return host address in dotted format
Definition inaddr.cpp:191
inaddr(unsigned long host=INADDR_ANY, unsigned short port=0)
Fill sockaddr structure with host number and port.
Definition inaddr.cpp:26
unsigned short port() const
return port number in host order
Definition inaddr.h:55
unsigned host() const
return host address in host order
Definition inaddr.h:70
inaddr & operator=(const inaddr &rhs)
Assignment operator.
Definition inaddr.h:101
bool operator!=(const inaddr &other) const
Inequality operator.
Definition inaddr.h:115
bool is_multicast() const
check if it's multicast host
Definition inaddr.h:89
bool operator==(const inaddr &other) const
Equality operator.
Definition inaddr.h:108
Definition of mlib::erc and mlib::errfac classes.
std::ostream & operator<<(std::ostream &strm, const mlib::inaddr &addr)
Serialize an address as '<hostname>:<port>'.
Definition inaddr.h:128
std::string to_string(const mlib::inaddr &addr)
Convert an address to a string in format '<hostname>:<port>
Definition inaddr.h:123