1 | /** \file Utility.h |
---|
2 | ** \date 2004-02-13 |
---|
3 | ** \author grymse@alhem.net |
---|
4 | **/ |
---|
5 | /* |
---|
6 | Copyright (C) 2004-2007 Anders Hedstrom |
---|
7 | |
---|
8 | This library is made available under the terms of the GNU GPL. |
---|
9 | |
---|
10 | If you would like to use this library in a closed-source application, |
---|
11 | a separate license agreement is available. For information about |
---|
12 | the closed-source license agreement for the C++ sockets library, |
---|
13 | please visit http://www.alhem.net/Sockets/license.html and/or |
---|
14 | email license@alhem.net. |
---|
15 | |
---|
16 | This program is free software; you can redistribute it and/or |
---|
17 | modify it under the terms of the GNU General Public License |
---|
18 | as published by the Free Software Foundation; either version 2 |
---|
19 | of the License, or (at your option) any later version. |
---|
20 | |
---|
21 | This program is distributed in the hope that it will be useful, |
---|
22 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
24 | GNU General Public License for more details. |
---|
25 | |
---|
26 | You should have received a copy of the GNU General Public License |
---|
27 | along with this program; if not, write to the Free Software |
---|
28 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
29 | */ |
---|
30 | #ifndef _SOCKETS_Utility_H |
---|
31 | #define _SOCKETS_Utility_H |
---|
32 | |
---|
33 | #include "sockets-config.h" |
---|
34 | #include <ctype.h> |
---|
35 | #include <string.h> |
---|
36 | #include <memory> |
---|
37 | #include "socket_include.h" |
---|
38 | #include <map> |
---|
39 | #include <string> |
---|
40 | |
---|
41 | #ifdef SOCKETS_NAMESPACE |
---|
42 | namespace SOCKETS_NAMESPACE { |
---|
43 | #endif |
---|
44 | |
---|
45 | #define TWIST_LEN 624 |
---|
46 | |
---|
47 | class SocketAddress; |
---|
48 | |
---|
49 | /** Conversion utilities. |
---|
50 | \ingroup util */ |
---|
51 | class Utility |
---|
52 | { |
---|
53 | /** |
---|
54 | The Mersenne Twister |
---|
55 | http://www.math.keio.ac.jp/~matumoto/emt.html |
---|
56 | */ |
---|
57 | class Rng { |
---|
58 | public: |
---|
59 | Rng(unsigned long seed); |
---|
60 | |
---|
61 | unsigned long Get(); |
---|
62 | |
---|
63 | private: |
---|
64 | int m_value; |
---|
65 | unsigned long m_tmp[TWIST_LEN]; |
---|
66 | }; |
---|
67 | class ncmap_compare { |
---|
68 | public: |
---|
69 | bool operator()(const std::string& x, const std::string& y) const { |
---|
70 | return strcasecmp(x.c_str(), y.c_str()) < 0; |
---|
71 | } |
---|
72 | }; |
---|
73 | public: |
---|
74 | template<typename Y> class ncmap : public std::map<std::string, Y, ncmap_compare> { |
---|
75 | public: |
---|
76 | ncmap() {} |
---|
77 | }; |
---|
78 | public: |
---|
79 | static std::string base64(const std::string& str_in); |
---|
80 | static std::string base64d(const std::string& str_in); |
---|
81 | static std::string l2string(long l); |
---|
82 | static std::string bigint2string(uint64_t l); |
---|
83 | static uint64_t atoi64(const std::string& str); |
---|
84 | static unsigned int hex2unsigned(const std::string& str); |
---|
85 | static std::string rfc1738_encode(const std::string& src); |
---|
86 | static std::string rfc1738_decode(const std::string& src); |
---|
87 | |
---|
88 | /** Checks whether a string is a valid ipv4/ipv6 ip number. */ |
---|
89 | static bool isipv4(const std::string&); |
---|
90 | /** Checks whether a string is a valid ipv4/ipv6 ip number. */ |
---|
91 | static bool isipv6(const std::string&); |
---|
92 | |
---|
93 | /** Hostname to ip resolution ipv4, not asynchronous. */ |
---|
94 | static bool u2ip(const std::string&, ipaddr_t&); |
---|
95 | static bool u2ip(const std::string&, struct sockaddr_in& sa, int ai_flags = 0); |
---|
96 | |
---|
97 | #ifdef ENABLE_IPV6 |
---|
98 | #ifdef IPPROTO_IPV6 |
---|
99 | /** Hostname to ip resolution ipv6, not asynchronous. */ |
---|
100 | static bool u2ip(const std::string&, struct in6_addr&); |
---|
101 | static bool u2ip(const std::string&, struct sockaddr_in6& sa, int ai_flags = 0); |
---|
102 | #endif |
---|
103 | #endif |
---|
104 | |
---|
105 | /** Reverse lookup of address to hostname */ |
---|
106 | static bool reverse(struct sockaddr *sa, socklen_t sa_len, std::string&, int flags = 0); |
---|
107 | static bool reverse(struct sockaddr *sa, socklen_t sa_len, std::string& hostname, std::string& service, int flags = 0); |
---|
108 | |
---|
109 | static bool u2service(const std::string& name, int& service, int ai_flags = 0); |
---|
110 | |
---|
111 | /** Convert binary ip address to string: ipv4. */ |
---|
112 | static void l2ip(const ipaddr_t,std::string& ); |
---|
113 | static void l2ip(const in_addr&,std::string& ); |
---|
114 | #ifdef ENABLE_IPV6 |
---|
115 | #ifdef IPPROTO_IPV6 |
---|
116 | /** Convert binary ip address to string: ipv6. */ |
---|
117 | static void l2ip(const struct in6_addr&,std::string& ,bool mixed = false); |
---|
118 | |
---|
119 | /** ipv6 address compare. */ |
---|
120 | static int in6_addr_compare(in6_addr,in6_addr); |
---|
121 | #endif |
---|
122 | #endif |
---|
123 | /** ResolveLocal (hostname) - call once before calling any GetLocal method. */ |
---|
124 | static void ResolveLocal(); |
---|
125 | /** Returns local hostname, ResolveLocal must be called once before using. |
---|
126 | \sa ResolveLocal */ |
---|
127 | static const std::string& GetLocalHostname(); |
---|
128 | /** Returns local ip, ResolveLocal must be called once before using. |
---|
129 | \sa ResolveLocal */ |
---|
130 | static ipaddr_t GetLocalIP(); |
---|
131 | /** Returns local ip number as string. |
---|
132 | \sa ResolveLocal */ |
---|
133 | static const std::string& GetLocalAddress(); |
---|
134 | #ifdef ENABLE_IPV6 |
---|
135 | #ifdef IPPROTO_IPV6 |
---|
136 | /** Returns local ipv6 ip. |
---|
137 | \sa ResolveLocal */ |
---|
138 | static const struct in6_addr& GetLocalIP6(); |
---|
139 | /** Returns local ipv6 address. |
---|
140 | \sa ResolveLocal */ |
---|
141 | static const std::string& GetLocalAddress6(); |
---|
142 | #endif |
---|
143 | #endif |
---|
144 | /** Set environment variable. |
---|
145 | \param var Name of variable to set |
---|
146 | \param value Value */ |
---|
147 | static void SetEnv(const std::string& var,const std::string& value); |
---|
148 | /** Convert sockaddr struct to human readable string. |
---|
149 | \param sa Ptr to sockaddr struct */ |
---|
150 | static std::string Sa2String(struct sockaddr *sa); |
---|
151 | |
---|
152 | /** Get current time in sec/microseconds. */ |
---|
153 | static void GetTime(struct timeval *); |
---|
154 | |
---|
155 | static std::auto_ptr<SocketAddress> CreateAddress(struct sockaddr *,socklen_t); |
---|
156 | |
---|
157 | static unsigned long ThreadID(); |
---|
158 | |
---|
159 | static std::string ToLower(const std::string& str); |
---|
160 | static std::string ToUpper(const std::string& str); |
---|
161 | |
---|
162 | static std::string ToString(double d); |
---|
163 | |
---|
164 | /** Returns a random 32-bit integer */ |
---|
165 | static unsigned long Rnd(); |
---|
166 | |
---|
167 | private: |
---|
168 | static std::string m_host; ///< local hostname |
---|
169 | static ipaddr_t m_ip; ///< local ip address |
---|
170 | static std::string m_addr; ///< local ip address in string format |
---|
171 | #ifdef ENABLE_IPV6 |
---|
172 | #ifdef IPPROTO_IPV6 |
---|
173 | static struct in6_addr m_local_ip6; ///< local ipv6 address |
---|
174 | #endif |
---|
175 | static std::string m_local_addr6; ///< local ipv6 address in string format |
---|
176 | #endif |
---|
177 | static bool m_local_resolved; ///< ResolveLocal has been called if true |
---|
178 | }; |
---|
179 | |
---|
180 | |
---|
181 | #ifdef SOCKETS_NAMESPACE |
---|
182 | } |
---|
183 | #endif |
---|
184 | |
---|
185 | #endif // _SOCKETS_Utility_H |
---|
186 | |
---|