00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef _SOCKETS_Utility_H
00033 #define _SOCKETS_Utility_H
00034
00035 #include "sockets-config.h"
00036 #include <ctype.h>
00037 #include <memory>
00038 #include "socket_include.h"
00039 #include <map>
00040 #include <string>
00041 #include <cstring>
00042
00043 #ifdef SOCKETS_NAMESPACE
00044 namespace SOCKETS_NAMESPACE {
00045 #endif
00046
00047 #define TWIST_LEN 624
00048
00049 class SocketAddress;
00050
00053 class Utility
00054 {
00059 class Rng {
00060 public:
00061 Rng(unsigned long seed);
00062
00063 unsigned long Get();
00064
00065 private:
00066 int m_value;
00067 unsigned long m_tmp[TWIST_LEN];
00068 };
00069 class ncmap_compare {
00070 public:
00071 bool operator()(const std::string& x, const std::string& y) const;
00072 };
00073 public:
00074 template<typename Y> class ncmap : public std::map<std::string, Y, ncmap_compare> {
00075 public:
00076 ncmap() {}
00077 };
00078 class Uri {
00079 public:
00080 Uri(const std::string& url);
00081
00082 const std::string& Url() const { return m_url; }
00083 const std::string& UrlUri() const { return m_uri; }
00084 const std::string& QueryString() const { return m_query_string; }
00085 const std::string& Protocol() const { return m_protocol; }
00086 const std::string& Host() const { return m_host; }
00087 int Port() const { return m_port; }
00088 const std::string& Path() const { return m_path; }
00089 const std::string& File() const { return m_file; }
00090 const std::string& Extension() const { return m_ext; }
00091
00092 const std::string& ToString() const { return m_url; }
00093
00094 const std::string& User() const { return m_user; }
00095 const std::string& Auth() const { return m_auth; }
00096
00097 private:
00098 std::string m_url;
00099 std::string m_uri;
00100 std::string m_query_string;
00101 std::string m_protocol;
00102 std::string m_host;
00103 int m_port;
00104 std::string m_path;
00105 std::string m_file;
00106 std::string m_ext;
00107 std::string m_user;
00108 std::string m_auth;
00109 };
00110 class Path
00111 {
00112 public:
00113 Path(const std::string& );
00114
00115 const std::string& GetPath() const { return m_path; }
00116 const std::string& GetFilename() const { return m_file; }
00117 const std::string& GetExtension() const { return m_ext; }
00118
00119 private:
00120 std::string m_path;
00121 std::string m_file;
00122 std::string m_ext;
00123 };
00124 public:
00125 static std::string base64(const std::string& str_in);
00126 static std::string base64d(const std::string& str_in);
00127 static std::string l2string(long l);
00128 static std::string bigint2string(int64_t l);
00129 static std::string bigint2string(uint64_t l);
00130 static uint64_t atoi64(const std::string& str);
00131 static unsigned int hex2unsigned(const std::string& str);
00132 static std::string rfc1738_encode(const std::string& src);
00133 static std::string rfc1738_decode(const std::string& src);
00134
00136 static bool isipv4(const std::string&);
00138 static bool isipv6(const std::string&);
00139
00141 static bool u2ip(const std::string&, ipaddr_t&);
00142 static bool u2ip(const std::string&, struct sockaddr_in& sa, int ai_flags = 0);
00143
00144 #ifdef ENABLE_IPV6
00145 #ifdef IPPROTO_IPV6
00146
00147 static bool u2ip(const std::string&, struct in6_addr&);
00148 static bool u2ip(const std::string&, struct sockaddr_in6& sa, int ai_flags = 0);
00149 #endif
00150 #endif
00151
00153 static bool reverse(struct sockaddr *sa, socklen_t sa_len, std::string&, int flags = 0);
00154 static bool reverse(struct sockaddr *sa, socklen_t sa_len, std::string& hostname, std::string& service, int flags = 0);
00155
00156 static bool u2service(const std::string& name, int& service, int ai_flags = 0);
00157
00159 static void l2ip(const ipaddr_t,std::string& );
00160 static void l2ip(const in_addr&,std::string& );
00161 #ifdef ENABLE_IPV6
00162 #ifdef IPPROTO_IPV6
00163
00164 static void l2ip(const struct in6_addr&,std::string& ,bool mixed = false);
00165
00167 static int in6_addr_compare(in6_addr,in6_addr);
00168 #endif
00169 #endif
00170
00171 static void ResolveLocal();
00174 static const std::string& GetLocalHostname();
00177 static ipaddr_t GetLocalIP();
00180 static const std::string& GetLocalAddress();
00181 #ifdef ENABLE_IPV6
00182 #ifdef IPPROTO_IPV6
00183
00185 static const struct in6_addr& GetLocalIP6();
00188 static const std::string& GetLocalAddress6();
00189 #endif
00190 #endif
00191
00192 static const std::string GetEnv(const std::string& name);
00196 static void SetEnv(const std::string& var,const std::string& value);
00199 static std::string Sa2String(struct sockaddr *sa);
00200
00202 static void GetTime(struct timeval *);
00203
00204 static std::auto_ptr<SocketAddress> CreateAddress(struct sockaddr *,socklen_t);
00205
00206 static unsigned long ThreadID();
00207
00208 static std::string ToLower(const std::string& str);
00209 static std::string ToUpper(const std::string& str);
00210
00211 static std::string ToString(double d);
00212
00214 static unsigned long Rnd();
00215
00216 static const char *Logo;
00217
00218 static const std::string Stack();
00219
00221 static const std::string FromUtf8(const std::string& );
00222 static const std::string ToUtf8(const std::string&);
00223
00225 static const Path CurrentDirectory();
00226 static bool ChangeDirectory(const Path& to_dir);
00227
00229 static void Sleep(int ms);
00230
00231 private:
00232 static std::string m_host;
00233 static ipaddr_t m_ip;
00234 static std::string m_addr;
00235 #ifdef ENABLE_IPV6
00236 #ifdef IPPROTO_IPV6
00237 static struct in6_addr m_local_ip6;
00238 #endif
00239 static std::string m_local_addr6;
00240 #endif
00241 static bool m_local_resolved;
00242 };
00243
00244
00245 #ifdef SOCKETS_NAMESPACE
00246 }
00247 #endif
00248
00249 #endif // _SOCKETS_Utility_H
00250