aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llmessage/llhost.h
diff options
context:
space:
mode:
Diffstat (limited to 'linden/indra/llmessage/llhost.h')
-rw-r--r--linden/indra/llmessage/llhost.h152
1 files changed, 152 insertions, 0 deletions
diff --git a/linden/indra/llmessage/llhost.h b/linden/indra/llmessage/llhost.h
new file mode 100644
index 0000000..894e2de
--- /dev/null
+++ b/linden/indra/llmessage/llhost.h
@@ -0,0 +1,152 @@
1/**
2 * @file llhost.h
3 * @brief a LLHost uniquely defines a host (Simulator, Proxy or other)
4 * across the network
5 *
6 * Copyright (c) 2000-2007, Linden Research, Inc.
7 *
8 * The source code in this file ("Source Code") is provided by Linden Lab
9 * to you under the terms of the GNU General Public License, version 2.0
10 * ("GPL"), unless you have obtained a separate licensing agreement
11 * ("Other License"), formally executed by you and Linden Lab. Terms of
12 * the GPL can be found in doc/GPL-license.txt in this distribution, or
13 * online at http://secondlife.com/developers/opensource/gplv2
14 *
15 * There are special exceptions to the terms and conditions of the GPL as
16 * it is applied to this Source Code. View the full text of the exception
17 * in the file doc/FLOSS-exception.txt in this software distribution, or
18 * online at http://secondlife.com/developers/opensource/flossexception
19 *
20 * By copying, modifying or distributing this software, you acknowledge
21 * that you have read and understood your obligations described above,
22 * and agree to abide by those obligations.
23 *
24 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
25 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
26 * COMPLETENESS OR PERFORMANCE.
27 */
28
29#ifndef LL_LLHOST_H
30#define LL_LLHOST_H
31
32#include <iostream>
33#include <string>
34
35#include "net.h"
36
37#include "llstring.h"
38
39const U32 INVALID_PORT = 0;
40const U32 INVALID_HOST_IP_ADDRESS = 0x0;
41
42class LLHost {
43protected:
44 U32 mPort;
45 U32 mIP;
46public:
47
48 static LLHost invalid;
49
50 // CREATORS
51 LLHost()
52 : mPort(INVALID_PORT),
53 mIP(INVALID_HOST_IP_ADDRESS)
54 { } // STL's hash_map expect this T()
55
56 LLHost( U32 ipv4_addr, U32 port )
57 : mPort( port )
58 {
59 mIP = ipv4_addr;
60 }
61
62 LLHost( const char *ipv4_addr, U32 port )
63 : mPort( port )
64 {
65 mIP = ip_string_to_u32(ipv4_addr);
66 }
67
68 explicit LLHost(const U64 ip_port)
69 {
70 U32 ip = (U32)(ip_port >> 32);
71 U32 port = (U32)(ip_port & (U64)0xFFFFFFFF);
72 mIP = ip;
73 mPort = port;
74 }
75
76 explicit LLHost(const std::string& ip_and_port);
77
78 ~LLHost()
79 { }
80
81 // MANIPULATORS
82 void set( U32 ip, U32 port ) { mIP = ip; mPort = port; }
83 void set( const char* ipstr, U32 port ) { mIP = ip_string_to_u32(ipstr); mPort = port; }
84 void setAddress( const char* ipstr ) { mIP = ip_string_to_u32(ipstr); }
85 void setAddress( U32 ip ) { mIP = ip; }
86 void setPort( U32 port ) { mPort = port; }
87 BOOL setHostByName(const char *hname);
88
89 LLHost& operator=(const LLHost &rhs);
90 void invalidate() { mIP = INVALID_HOST_IP_ADDRESS; mPort = INVALID_PORT;};
91
92 // READERS
93 U32 getAddress() const { return mIP; }
94 U32 getPort() const { return mPort; }
95 BOOL isOk() const { return (mIP != INVALID_HOST_IP_ADDRESS) && (mPort != INVALID_PORT); }
96 size_t hash() const { return (mIP << 16) | (mPort & 0xffff); }
97 void getString(char* buffer, S32 length) const; // writes IP:port into buffer
98 void getIPString(char* buffer, S32 length) const; // writes IP into buffer
99 std::string getIPString() const;
100 void getHostName(char *buf, S32 len) const;
101 LLString getHostName() const;
102 std::string getIPandPort() const;
103
104 friend std::ostream& operator<< (std::ostream& os, const LLHost &hh);
105 friend std::istream& operator>> (std::istream& is, LLHost &hh);
106 friend bool operator==( const LLHost &lhs, const LLHost &rhs );
107 friend bool operator!=( const LLHost &lhs, const LLHost &rhs );
108 friend bool operator<(const LLHost &lhs, const LLHost &rhs);
109};
110
111
112// Function Object required for STL templates using LLHost as key
113class LLHostHash
114{
115public:
116 size_t operator() (const LLHost &hh) const { return hh.hash(); }
117};
118
119
120inline bool operator==( const LLHost &lhs, const LLHost &rhs )
121{
122 return (lhs.mIP == rhs.mIP) && (lhs.mPort == rhs.mPort);
123}
124
125inline bool operator!=( const LLHost &lhs, const LLHost &rhs )
126{
127 return (lhs.mIP != rhs.mIP) || (lhs.mPort != rhs.mPort);
128}
129
130inline bool operator<(const LLHost &lhs, const LLHost &rhs)
131{
132 if (lhs.mIP < rhs.mIP)
133 {
134 return true;
135 }
136 if (lhs.mIP > rhs.mIP)
137 {
138 return false;
139 }
140
141 if (lhs.mPort < rhs.mPort)
142 {
143 return true;
144 }
145 else
146 {
147 return false;
148 }
149}
150
151
152#endif // LL_LLHOST_H