MLIB
Loading...
Searching...
No Matches
inaddr.h
Go to the documentation of this file.
1#pragma once
2/*
3 MLIB Library
4 (c) Mircea Neacsu 2002-2023. Licensed under MIT License.
5 See README file for full license terms.
6*/
7
9
10#if __has_include("defs.h")
11#include "defs.h"
12#endif
13
14#include "safe_winsock.h"
15#include <ostream>
16
17#include "errorcode.h"
18
19namespace mlib {
20
22class inaddr
23{
24public:
25 inaddr (unsigned long host = INADDR_ANY, unsigned short port = 0);
26 inaddr (const std::string& hostname, unsigned short port);
27 inaddr (const std::string& hostname, const std::string& service,
28 const std::string& proto = std::string ());
29
30 inaddr (const sockaddr& adr);
31 inaddr& operator= (const inaddr& rhs);
32
34 operator sockaddr* () const
35 {
36 return (sockaddr*)&sa;
37 };
38 bool operator== (const inaddr& other) const;
39 bool operator!= (const inaddr& other) const;
40
42 operator const sockaddr& () const
43 {
44 return *(sockaddr*)&sa;
45 };
46
48 operator sockaddr& ()
49 {
50 return *(sockaddr*)&sa;
51 };
52
54 unsigned short port () const
55 {
56 return ntohs (sa.sin_port);
57 };
58
60 void port (unsigned short p)
61 {
62 sa.sin_port = htons (p);
63 };
64
66 erc port (const std::string& service, const std::string& proto = std::string ());
67
69 unsigned host () const
70 {
71 return ntohl (sa.sin_addr.s_addr);
72 };
73
75 void host (unsigned int h)
76 {
77 sa.sin_addr.s_addr = htonl (h);
78 };
79
80 erc host (const std::string& hostname);
81
82 std::string hostname () const;
83
85 const char* ntoa () const;
86
88 bool is_multicast () const
89 {
90 return ((host () & 0xf0000000) == 0xe0000000);
91 };
92
93 static unsigned localhost ();
94
95private:
96 sockaddr_in sa;
97};
98
101{
102 memcpy (&sa, &rhs.sa, sizeof (sa));
103 return *this;
104}
105
107inline bool inaddr::operator== (const inaddr& other) const
108{
109 return (sa.sin_addr.S_un.S_addr == other.sa.sin_addr.S_un.S_addr)
110 && (sa.sin_port == other.sa.sin_port);
111}
112
114inline bool inaddr::operator!= (const inaddr& other) const
115{
116 return (!operator== (other));
117}
118
119} // namespace mlib
120
122inline std::ostream& operator<< (std::ostream& strm, const mlib::inaddr& addr)
123{
124 strm << addr.ntoa () << ':' << addr.port ();
125 return strm;
126}
objects returned as a function result or thrown directly.
Definition errorcode.h:68
sockaddr wrapper
Definition inaddr.h:23
static unsigned localhost()
Return local address in HOST order.
Definition inaddr.cpp:162
void host(unsigned int h)
set host address
Definition inaddr.h:75
std::string hostname() const
Find hostname from the host address in sockaddr.
Definition inaddr.cpp:139
void port(unsigned short p)
set port number
Definition inaddr.h:60
const char * ntoa() const
return host address in dotted format
Definition inaddr.cpp:198
unsigned short port() const
return port number in host order
Definition inaddr.h:54
unsigned host() const
return host address in host order
Definition inaddr.h:69
inaddr & operator=(const inaddr &rhs)
Assignment operator.
Definition inaddr.h:100
bool operator!=(const inaddr &other) const
Inequality operator.
Definition inaddr.h:114
bool is_multicast() const
check if it's multicast host
Definition inaddr.h:88
bool operator==(const inaddr &other) const
Equality operator.
Definition inaddr.h:107
Definition of erc and erfac classes.
std::ostream & operator<<(std::ostream &strm, const mlib::inaddr &addr)
Serialize an address as '<hostname>:<port>'.
Definition inaddr.h:122