Nebula
Loading...
Searching...
No Matches
posixipaddress.h
Go to the documentation of this file.
1#pragma once
2#ifndef POSIX_POSIXIPADDRESS_H
3#define POSIX_POSIXIPADDRESS_H
4//------------------------------------------------------------------------------
26#include "core/types.h"
27#include "io/uri.h"
28#include <sys/socket.h>
29#include <netinet/in.h>
30
31//------------------------------------------------------------------------------
32namespace Posix
33{
35{
36public:
42 PosixIpAddress(const IO::URI& uri);
44 PosixIpAddress(const Util::String& hostName, ushort portNumber);
46 bool operator==(const PosixIpAddress& rhs) const;
48 bool operator<(const PosixIpAddress& rhs) const;
50 bool operator>(const PosixIpAddress& rhs) const;
52 void ExtractFromUri(const IO::URI& uri);
56 const Util::String& GetHostName() const;
58 void SetPort(ushort port);
60 ushort GetPort() const;
62 const Util::String& GetHostAddr() const;
63
64protected:
65 friend class PosixSocket;
66
68 void SetSockAddr(const sockaddr& addr);
70 const sockaddr& GetSockAddr() const;
72 static bool GetHostByName(const Util::String& hostName, sockaddr& outAddr);
74 static bool IsInetAddr(const in_addr* addr);
75
78 sockaddr addr;
79};
80
81//------------------------------------------------------------------------------
84inline bool
86{
87 n_assert(this->addr.sa_family == AF_INET);
88 n_assert(this->addr.sa_family == rhs.addr.sa_family);
89 sockaddr_in * myAddr = (sockaddr_in *)(&(this->addr));
90 sockaddr_in * otherAddr = (sockaddr_in *)(&(rhs.addr));
91 return ((myAddr->sin_addr.s_addr == otherAddr->sin_addr.s_addr) &&
92 (myAddr->sin_port == otherAddr->sin_port));
93}
94
95//------------------------------------------------------------------------------
98inline bool
100{
101 n_assert(this->addr.sa_family == AF_INET);
102 n_assert(this->addr.sa_family == rhs.addr.sa_family);
103 sockaddr_in * myAddr = (sockaddr_in *)(&(this->addr));
104 sockaddr_in * otherAddr = (sockaddr_in *)(&(rhs.addr));
105 if (myAddr->sin_addr.s_addr == otherAddr->sin_addr.s_addr)
106 {
107 return myAddr->sin_port < otherAddr->sin_port;
108 }
109 else
110 {
111 return myAddr->sin_addr.s_addr < otherAddr->sin_addr.s_addr;
112 }
113}
114
115//------------------------------------------------------------------------------
118inline bool
120{
121 n_assert(this->addr.sa_family == AF_INET);
122 n_assert(this->addr.sa_family == rhs.addr.sa_family);
123 sockaddr_in * myAddr = (sockaddr_in *)(&(this->addr));
124 sockaddr_in * otherAddr = (sockaddr_in *)(&(rhs.addr));
125 if (myAddr->sin_addr.s_addr == otherAddr->sin_addr.s_addr)
126 {
127 return myAddr->sin_port > otherAddr->sin_port;
128 }
129 else
130 {
131 return myAddr->sin_addr.s_addr > otherAddr->sin_addr.s_addr;
132 }
133}
134
135} // namespace Posix
136//------------------------------------------------------------------------------
137#endif
An URI object can split a Uniform Resource Identifier string into its components or build a string fr...
Definition uri.h:67
Represents an IP address, consisting of a IPv4 host address and a port number.
Definition posixipaddress.h:35
void SetHostName(const Util::String &hostName)
set host name
Definition posixipaddress.cc:141
bool operator>(const PosixIpAddress &rhs) const
greater-then operator
Definition posixipaddress.h:119
static bool IsInetAddr(const in_addr *addr)
return true if an address is an internet address (not class A,B,C)
Definition posixipaddress.cc:267
PosixIpAddress()
default constructor
Definition posixipaddress.cc:21
void SetPort(ushort port)
set port number
Definition posixipaddress.cc:118
ushort GetPort() const
get port number
Definition posixipaddress.cc:128
const sockaddr & GetSockAddr() const
get sockaddr_in field
Definition posixipaddress.cc:84
Util::String addrAsString
Definition posixipaddress.h:77
const Util::String & GetHostAddr() const
get the ip address resulting from the host name as string
Definition posixipaddress.cc:167
Util::String hostName
Definition posixipaddress.h:76
void ExtractFromUri(const IO::URI &uri)
extract host name and port number from URI
Definition posixipaddress.cc:97
sockaddr addr
Definition posixipaddress.h:78
static bool GetHostByName(const Util::String &hostName, sockaddr &outAddr)
perform address resolution, understands special host names
Definition posixipaddress.cc:189
void SetSockAddr(const sockaddr &addr)
set sockaddr_in directly
Definition posixipaddress.cc:70
bool operator==(const PosixIpAddress &rhs) const
equality operator
Definition posixipaddress.h:85
bool operator<(const PosixIpAddress &rhs) const
less-then operator
Definition posixipaddress.h:99
const Util::String & GetHostName() const
get host name
Definition posixipaddress.cc:157
A lowlevel socket wrapper class.
Definition posixsocket.h:33
#define n_assert(exp)
Definition debug.h:50
Posix implemention of a read-many write-few lock.
Definition posixsysfunc.cc:21
Nebula's universal string class.
Definition string.h:50
unsigned short ushort
Definition types.h:32