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 #ifndef _SOCKETS_Utility_H
00031 #define _SOCKETS_Utility_H
00032
00033 #include "sockets-config.h"
00034 #include <ctype.h>
00035 #ifdef _WIN32
00036 typedef unsigned __int64 uint64_t;
00037 #else
00038 #include <stdlib.h>
00039 #ifdef SOLARIS
00040 # include <sys/types.h>
00041 #else
00042 # include <stdint.h>
00043 #endif
00044 #endif
00045 #include <memory>
00046 #include "Base64.h"
00047 #include "socket_include.h"
00048
00049 #ifdef SOCKETS_NAMESPACE
00050 namespace SOCKETS_NAMESPACE {
00051 #endif
00052
00053 class SocketAddress;
00054
00057 class Utility
00058 {
00059 public:
00060 static std::string base64(const std::string& str_in);
00061 static std::string base64d(const std::string& str_in);
00062 static std::string l2string(long l);
00063 static std::string bigint2string(uint64_t l);
00064 static uint64_t atoi64(const std::string& str);
00065 static unsigned int hex2unsigned(const std::string& str);
00066 static std::string rfc1738_encode(const std::string& src);
00067 static std::string rfc1738_decode(const std::string& src);
00068
00070 static bool isipv4(const std::string&);
00072 static bool isipv6(const std::string&);
00073
00075 static bool u2ip(const std::string&, ipaddr_t&);
00076 static bool u2ip(const std::string&, struct sockaddr_in& sa, int ai_flags = 0);
00077
00078 #ifdef ENABLE_IPV6
00079 #ifdef IPPROTO_IPV6
00080
00081 static bool u2ip(const std::string&, struct in6_addr&);
00082 static bool u2ip(const std::string&, struct sockaddr_in6& sa, int ai_flags = 0);
00083 #endif
00084 #endif
00085
00087 static bool reverse(struct sockaddr *sa, socklen_t sa_len, std::string&, int flags = 0);
00088 static bool reverse(struct sockaddr *sa, socklen_t sa_len, std::string& hostname, std::string& service, int flags = 0);
00089
00090 static bool u2service(const std::string& name, int& service, int ai_flags = 0);
00091
00093 static void l2ip(const ipaddr_t,std::string& );
00094 static void l2ip(const in_addr&,std::string& );
00095 #ifdef ENABLE_IPV6
00096 #ifdef IPPROTO_IPV6
00097
00098 static void l2ip(const struct in6_addr&,std::string& ,bool mixed = false);
00099
00101 static int in6_addr_compare(in6_addr,in6_addr);
00102 #endif
00103 #endif
00104
00105 static void ResolveLocal();
00108 static const std::string& GetLocalHostname();
00111 static ipaddr_t GetLocalIP();
00114 static const std::string& GetLocalAddress();
00115 #ifdef ENABLE_IPV6
00116 #ifdef IPPROTO_IPV6
00117
00119 static const struct in6_addr& GetLocalIP6();
00122 static const std::string& GetLocalAddress6();
00123 #endif
00124 #endif
00125
00128 static void SetEnv(const std::string& var,const std::string& value);
00131 static std::string Sa2String(struct sockaddr *sa);
00132
00134 static void GetTime(struct timeval *);
00135
00136 static std::auto_ptr<SocketAddress> CreateAddress(struct sockaddr *,socklen_t);
00137
00138 static unsigned long ThreadID();
00139
00140 private:
00141 static std::string m_host;
00142 static ipaddr_t m_ip;
00143 static std::string m_addr;
00144 #ifdef ENABLE_IPV6
00145 #ifdef IPPROTO_IPV6
00146 static struct in6_addr m_local_ip6;
00147 #endif
00148 static std::string m_local_addr6;
00149 #endif
00150 static bool m_local_resolved;
00151 };
00152
00153
00154 #ifdef SOCKETS_NAMESPACE
00155 }
00156 #endif
00157
00158 #endif // _SOCKETS_Utility_H