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#include <cstring>
18
19#include "errorcode.h"
20
21namespace mlib {
22
24class inaddr
25{
26public:
27 explicit inaddr (unsigned long host = INADDR_ANY, unsigned short port = 0);
28 inaddr (const std::string& hostname, unsigned short port);
29 inaddr (const std::string& hostname, const std::string& service,
30 const std::string& proto = std::string ());
31
32 inaddr (const sockaddr& adr);
33 inaddr& operator= (const inaddr& rhs);
34
36 operator sockaddr* () const
37 {
38 return (sockaddr*)&sa;
39 };
40 bool operator== (const inaddr& other) const;
41 bool operator!= (const inaddr& other) const;
42
44 operator const sockaddr& () const
45 {
46 return *(sockaddr*)&sa;
47 };
48
50 operator sockaddr& ()
51 {
52 return *(sockaddr*)&sa;
53 };
54
56 unsigned short port () const
57 {
58 return ntohs (sa.sin_port);
59 };
60
62 void port (unsigned short p)
63 {
64 sa.sin_port = htons (p);
65 };
66
68 erc port (const std::string& service, const std::string& proto = std::string ());
69
71 unsigned host () const
72 {
73 return ntohl (sa.sin_addr.s_addr);
74 };
75
77 void host (unsigned int h)
78 {
79 sa.sin_addr.s_addr = htonl (h);
80 };
81
82 erc host (const std::string& hostname);
83
84 std::string hostname () const;
85
87 const char* ntoa () const;
88
90 bool is_multicast () const
91 {
92 return ((host () & 0xf0000000) == 0xe0000000);
93 };
94
95 static unsigned localhost ();
96
97private:
98 sockaddr_in sa;
99};
100
103{
104 memcpy (&sa, &rhs.sa, sizeof (sa));
105 return *this;
106}
107
109inline bool inaddr::operator== (const inaddr& other) const
110{
111 return (sa.sin_addr.s_addr == other.sa.sin_addr.s_addr)
112 && (sa.sin_port == other.sa.sin_port);
113}
114
116inline bool inaddr::operator!= (const inaddr& other) const
117{
118 return (!operator== (other));
119}
120
122inline std::string to_string (const mlib::inaddr& addr)
123{
124 return std::string (addr.ntoa ()) + ':' + std::to_string (addr.port ());
125}
126
127inline std::ostream& operator<< (std::ostream& strm, const mlib::inaddr& addr)
128{
129 strm << to_string (addr);
130 return strm;
131}
132
133} // namespace mlib
134
135
objects returned as a function result or thrown directly.
Definition errorcode.h:73
Wrapper for sockaddr structure.
Definition inaddr.h:25
static unsigned localhost()
Return local address in HOST order.
Definition inaddr.cpp:161
void host(unsigned int h)
set host address
Definition inaddr.h:77
std::string hostname() const
Find hostname from the host address in sockaddr.
Definition inaddr.cpp:141
void port(unsigned short p)
set port number
Definition inaddr.h:62
const char * ntoa() const
return host address in dotted format
Definition inaddr.cpp:201
inaddr(unsigned long host=INADDR_ANY, unsigned short port=0)
Fill sockaddr structure with host number and port.
Definition inaddr.cpp:35
unsigned short port() const
return port number in host order
Definition inaddr.h:56
unsigned host() const
return host address in host order
Definition inaddr.h:71
inaddr & operator=(const inaddr &rhs)
Assignment operator.
Definition inaddr.h:102
bool operator!=(const inaddr &other) const
Inequality operator.
Definition inaddr.h:116
bool is_multicast() const
check if it's multicast host
Definition inaddr.h:90
bool operator==(const inaddr &other) const
Equality operator.
Definition inaddr.h:109
Definition of mlib::erc and mlib::errfac classes.
std::string to_string(const mlib::inaddr &addr)
Convert an address to a string in format '<hostname>:<port>
Definition inaddr.h:122