Logo
~Sockets~
~Examples~
~Contact~


Utility Class Reference
[Utilities]

Conversion utilities. More...

#include <Utility.h>

List of all members.


Static Public Member Functions

static std::string base64 (const std::string &str_in)
static std::string base64d (const std::string &str_in)
static std::string l2string (long l)
static std::string bigint2string (int64_t l)
static std::string bigint2string (uint64_t l)
static uint64_t atoi64 (const std::string &str)
static unsigned int hex2unsigned (const std::string &str)
static std::string rfc1738_encode (const std::string &src)
static std::string rfc1738_decode (const std::string &src)
static bool isipv4 (const std::string &)
 Checks whether a string is a valid ipv4/ipv6 ip number.
static bool isipv6 (const std::string &)
 Checks whether a string is a valid ipv4/ipv6 ip number.
static bool u2ip (const std::string &, ipaddr_t &)
 Hostname to ip resolution ipv4, not asynchronous.
static bool u2ip (const std::string &, struct sockaddr_in &sa, int ai_flags=0)
static bool reverse (struct sockaddr *sa, socklen_t sa_len, std::string &, int flags=0)
 Reverse lookup of address to hostname.
static bool reverse (struct sockaddr *sa, socklen_t sa_len, std::string &hostname, std::string &service, int flags=0)
static bool u2service (const std::string &name, int &service, int ai_flags=0)
static void l2ip (const ipaddr_t, std::string &)
 Convert binary ip address to string: ipv4.
static void l2ip (const in_addr &, std::string &)
static void ResolveLocal ()
 ResolveLocal (hostname) - call once before calling any GetLocal method.
static const std::string & GetLocalHostname ()
 Returns local hostname, ResolveLocal must be called once before using.
static ipaddr_t GetLocalIP ()
 Returns local ip, ResolveLocal must be called once before using.
static const std::string & GetLocalAddress ()
 Returns local ip number as string.
static const std::string GetEnv (const std::string &name)
 Get environment variable.
static void SetEnv (const std::string &var, const std::string &value)
 Set environment variable.
static std::string Sa2String (struct sockaddr *sa)
 Convert sockaddr struct to human readable string.
static void GetTime (struct timeval *)
 Get current time in sec/microseconds.
static std::auto_ptr
< SocketAddress
CreateAddress (struct sockaddr *, socklen_t)
static unsigned long ThreadID ()
static std::string ToLower (const std::string &str)
static std::string ToUpper (const std::string &str)
static std::string ToString (double d)
static unsigned long Rnd ()
 Returns a random 32-bit integer.
static const std::string Stack ()
static const std::string FromUtf8 (const std::string &)
 Utf8 decrypt, encrypt.
static const std::string ToUtf8 (const std::string &)
static const Path CurrentDirectory ()
 File system stuff.
static bool ChangeDirectory (const Path &to_dir)
static void Sleep (int ms)
 wait a specified number of ms

Static Public Attributes

static const char * Logo

Static Private Attributes

static std::string m_host
 local hostname
static ipaddr_t m_ip = 0
 local ip address
static std::string m_addr
 local ip address in string format
static bool m_local_resolved = false
 ResolveLocal has been called if true.

Classes

class  ncmap
class  ncmap_compare
class  Path
class  Rng
 The Mersenne Twister http://www.math.keio.ac.jp/~matumoto/emt.html. More...
class  Uri

Detailed Description

Conversion utilities.

Definition at line 53 of file Utility.h.


Member Function Documentation

std::string Utility::base64 ( const std::string &  str_in  )  [static]

Definition at line 185 of file Utility.cpp.

Referenced by HTTPSocket::url_this().

00186 {
00187         std::string str;
00188         Base64 m_b;
00189         m_b.encode(str_in, str, false); // , false == do not add cr/lf
00190         return str;
00191 }

std::string Utility::base64d ( const std::string &  str_in  )  [static]

Definition at line 194 of file Utility.cpp.

References Base64::decode().

00195 {
00196         std::string str;
00197         Base64 m_b;
00198         m_b.decode(str_in, str);
00199         return str;
00200 }

std::string Utility::bigint2string ( int64_t  l  )  [static]

Definition at line 213 of file Utility.cpp.

00214 {
00215         std::string str;
00216         int64_t tmp = l;
00217         if (l < 0)
00218         {
00219                 str = "-";
00220                 tmp = -l;
00221         }
00222         while (tmp)
00223         {
00224                 uint64_t a = tmp % 10;
00225                 str = (char)(a + 48) + str;
00226                 tmp /= 10;
00227         }
00228         if (!str.size())
00229         {
00230                 str = "0";
00231         }
00232         return str;
00233 }

std::string Utility::bigint2string ( uint64_t  l  )  [static]

Definition at line 236 of file Utility.cpp.

00237 {
00238         std::string str;
00239         uint64_t tmp = l;
00240         while (tmp)
00241         {
00242                 uint64_t a = tmp % 10;
00243                 str = (char)(a + 48) + str;
00244                 tmp /= 10;
00245         }
00246         if (!str.size())
00247         {
00248                 str = "0";
00249         }
00250         return str;
00251 }

uint64_t Utility::atoi64 ( const std::string &  str  )  [static]

Definition at line 254 of file Utility.cpp.

00255 {
00256         uint64_t l = 0;
00257         for (size_t i = 0; i < str.size(); ++i)
00258         {
00259                 l = l * 10 + str[i] - 48;
00260         }
00261         return l;
00262 }

unsigned int Utility::hex2unsigned ( const std::string &  str  )  [static]

Definition at line 265 of file Utility.cpp.

Referenced by HTTPSocket::OnRawData().

00266 {
00267         unsigned int r = 0;
00268         for (size_t i = 0; i < str.size(); ++i)
00269         {
00270                 r = r * 16 + str[i] - 48 - ((str[i] >= 'A') ? 7 : 0) - ((str[i] >= 'a') ? 32 : 0);
00271         }
00272         return r;
00273 }

std::string Utility::rfc1738_encode ( const std::string &  src  )  [static]

Definition at line 280 of file Utility.cpp.

Referenced by HttpPostSocket::OnConnect().

00281 {
00282 static  char hex[] = "0123456789ABCDEF";
00283         std::string dst;
00284         for (size_t i = 0; i < src.size(); ++i)
00285         {
00286                 unsigned char c = static_cast<unsigned char>(src[i]);
00287                 if (isalnum(c))
00288                 {
00289                         dst += c;
00290                 }
00291                 else
00292                 if (c == ' ')
00293                 {
00294                         dst += '+';
00295                 }
00296                 else
00297                 {
00298                         dst += '%';
00299                         dst += hex[c / 16];
00300                         dst += hex[c % 16];
00301                 }
00302         }
00303         return dst;
00304 } // rfc1738_encode

std::string Utility::rfc1738_decode ( const std::string &  src  )  [static]

Definition at line 311 of file Utility.cpp.

00312 {
00313         std::string dst;
00314         for (size_t i = 0; i < src.size(); ++i)
00315         {
00316                 if (src[i] == '%' && isxdigit(src[i + 1]) && isxdigit(src[i + 2]))
00317                 {
00318                         char c1 = src[++i];
00319                         char c2 = src[++i];
00320                         c1 = c1 - 48 - ((c1 >= 'A') ? 7 : 0) - ((c1 >= 'a') ? 32 : 0);
00321                         c2 = c2 - 48 - ((c2 >= 'A') ? 7 : 0) - ((c2 >= 'a') ? 32 : 0);
00322                         dst += (char)(c1 * 16 + c2);
00323                 }
00324                 else
00325                 if (src[i] == '+')
00326                 {
00327                         dst += ' ';
00328                 }
00329                 else
00330                 {
00331                         dst += src[i];
00332                 }
00333         }
00334         return dst;
00335 } // rfc1738_decode

bool Utility::isipv4 ( const std::string &  str  )  [static]

Checks whether a string is a valid ipv4/ipv6 ip number.

Definition at line 338 of file Utility.cpp.

Referenced by ResolvSocket::OnDetached(), TcpSocket::Open(), Ipv4Address::Resolve(), and u2ip().

00339 {
00340         int dots = 0;
00341         // %! ignore :port?
00342         for (size_t i = 0; i < str.size(); ++i)
00343         {
00344                 if (str[i] == '.')
00345                         dots++;
00346                 else
00347                 if (!isdigit(str[i]))
00348                         return false;
00349         }
00350         if (dots != 3)
00351                 return false;
00352         return true;
00353 }

bool Utility::isipv6 ( const std::string &  str  )  [static]

Checks whether a string is a valid ipv4/ipv6 ip number.

Definition at line 356 of file Utility.cpp.

References Parse::getword().

Referenced by ResolvSocket::OnDetached(), and TcpSocket::Open().

00357 {
00358         size_t qc = 0;
00359         size_t qd = 0;
00360         for (size_t i = 0; i < str.size(); ++i)
00361         {
00362                 qc += (str[i] == ':') ? 1 : 0;
00363                 qd += (str[i] == '.') ? 1 : 0;
00364         }
00365         if (qc > 7)
00366         {
00367                 return false;
00368         }
00369         if (qd && qd != 3)
00370         {
00371                 return false;
00372         }
00373         Parse pa(str,":.");
00374         std::string tmp = pa.getword();
00375         while (tmp.size())
00376         {
00377                 if (tmp.size() > 4)
00378                 {
00379                         return false;
00380                 }
00381                 for (size_t i = 0; i < tmp.size(); ++i)
00382                 {
00383                         if (tmp[i] < '0' || (tmp[i] > '9' && tmp[i] < 'A') ||
00384                                 (tmp[i] > 'F' && tmp[i] < 'a') || tmp[i] > 'f')
00385                         {
00386                                 return false;
00387                         }
00388                 }
00389                 //
00390                 tmp = pa.getword();
00391         }
00392         return true;
00393 }

bool Utility::u2ip ( const std::string &  str,
ipaddr_t l 
) [static]

Hostname to ip resolution ipv4, not asynchronous.

Definition at line 396 of file Utility.cpp.

Referenced by UdpSocket::AddMulticastMembership(), UdpSocket::DropMulticastMembership(), Ipv4Address::Ipv4Address(), ResolvSocket::OnDetached(), ResolvSocket::OnLine(), TcpSocket::Open(), SocketHandler::Resolve(), Ipv4Address::Resolve(), ResolveLocal(), UdpSocket::SetMulticastDefaultInterface(), SocketHandler::SetSocks4Host(), and Socket::SetSocks4Host().

00397 {
00398         struct sockaddr_in sa;
00399         bool r = Utility::u2ip(str, sa);
00400         memcpy(&l, &sa.sin_addr, sizeof(l));
00401         return r;
00402 }

bool Utility::u2ip ( const std::string &  host,
struct sockaddr_in &  sa,
int  ai_flags = 0 
) [static]

Definition at line 704 of file Utility.cpp.

References AI_NUMERICHOST, isipv4(), and Rnd().

00705 {
00706         memset(&sa, 0, sizeof(sa));
00707         sa.sin_family = AF_INET;
00708 #ifdef NO_GETADDRINFO
00709         if ((ai_flags & AI_NUMERICHOST) != 0 || isipv4(host))
00710         {
00711                 Parse pa((char *)host.c_str(), ".");
00712                 union {
00713                         struct {
00714                                 unsigned char b1;
00715                                 unsigned char b2;
00716                                 unsigned char b3;
00717                                 unsigned char b4;
00718                         } a;
00719                         ipaddr_t l;
00720                 } u;
00721                 u.a.b1 = static_cast<unsigned char>(pa.getvalue());
00722                 u.a.b2 = static_cast<unsigned char>(pa.getvalue());
00723                 u.a.b3 = static_cast<unsigned char>(pa.getvalue());
00724                 u.a.b4 = static_cast<unsigned char>(pa.getvalue());
00725                 memcpy(&sa.sin_addr, &u.l, sizeof(sa.sin_addr));
00726                 return true;
00727         }
00728 #ifndef LINUX
00729         struct hostent *he = gethostbyname( host.c_str() );
00730         if (!he)
00731         {
00732                 return false;
00733         }
00734         memcpy(&sa.sin_addr, he -> h_addr, sizeof(sa.sin_addr));
00735 #else
00736         struct hostent he;
00737         struct hostent *result = NULL;
00738         int myerrno = 0;
00739         char buf[2000];
00740         int n = gethostbyname_r(host.c_str(), &he, buf, sizeof(buf), &result, &myerrno);
00741         if (n || !result)
00742         {
00743                 return false;
00744         }
00745         if (he.h_addr_list && he.h_addr_list[0])
00746                 memcpy(&sa.sin_addr, he.h_addr, 4);
00747         else
00748                 return false;
00749 #endif
00750         return true;
00751 #else
00752         struct addrinfo hints;
00753         memset(&hints, 0, sizeof(hints));
00754         // AI_NUMERICHOST
00755         // AI_CANONNAME
00756         // AI_PASSIVE - server
00757         // AI_ADDRCONFIG
00758         // AI_V4MAPPED
00759         // AI_ALL
00760         // AI_NUMERICSERV
00761         hints.ai_flags = ai_flags;
00762         hints.ai_family = AF_INET;
00763         hints.ai_socktype = 0;
00764         hints.ai_protocol = 0;
00765         struct addrinfo *res;
00766         if (Utility::isipv4(host))
00767                 hints.ai_flags |= AI_NUMERICHOST;
00768         int n = getaddrinfo(host.c_str(), NULL, &hints, &res);
00769         if (!n)
00770         {
00771                 std::vector<struct addrinfo *> vec;
00772                 struct addrinfo *ai = res;
00773                 while (ai)
00774                 {
00775                         if (ai -> ai_addrlen == sizeof(sa))
00776                                 vec.push_back( ai );
00777                         ai = ai -> ai_next;
00778                 }
00779                 if (!vec.size())
00780                         return false;
00781                 ai = vec[Utility::Rnd() % vec.size()];
00782                 {
00783                         memcpy(&sa, ai -> ai_addr, ai -> ai_addrlen);
00784                 }
00785                 freeaddrinfo(res);
00786                 return true;
00787         }
00788         std::string error = "Error: ";
00789 #ifndef __CYGWIN__
00790         error += gai_strerror(n);
00791 #endif
00792         return false;
00793 #endif // NO_GETADDRINFO
00794 }

bool Utility::reverse ( struct sockaddr *  sa,
socklen_t  sa_len,
std::string &  hostname,
int  flags = 0 
) [static]

Reverse lookup of address to hostname.

Definition at line 913 of file Utility.cpp.

Referenced by Ipv4Address::Convert(), l2ip(), ResolvSocket::OnDetached(), and Ipv4Address::Reverse().

00914 {
00915         std::string service;
00916         return Utility::reverse(sa, sa_len, hostname, service, flags);
00917 }

bool Utility::reverse ( struct sockaddr *  sa,
socklen_t  sa_len,
std::string &  hostname,
std::string &  service,
int  flags = 0 
) [static]

Definition at line 920 of file Utility.cpp.

References NI_NUMERICHOST.

00921 {
00922         hostname = "";
00923         service = "";
00924 #ifdef NO_GETADDRINFO
00925         switch (sa -> sa_family)
00926         {
00927         case AF_INET:
00928                 if (flags & NI_NUMERICHOST)
00929                 {
00930                         union {
00931                                 struct {
00932                                         unsigned char b1;
00933                                         unsigned char b2;
00934                                         unsigned char b3;
00935                                         unsigned char b4;
00936                                 } a;
00937                                 ipaddr_t l;
00938                         } u;
00939                         struct sockaddr_in *sa_in = (struct sockaddr_in *)sa;
00940                         memcpy(&u.l, &sa_in -> sin_addr, sizeof(u.l));
00941                         char tmp[100];
00942                         snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u", u.a.b1, u.a.b2, u.a.b3, u.a.b4);
00943                         hostname = tmp;
00944                         return true;
00945                 }
00946                 else
00947                 {
00948                         struct sockaddr_in *sa_in = (struct sockaddr_in *)sa;
00949                         struct hostent *h = gethostbyaddr( (const char *)&sa_in -> sin_addr, sizeof(sa_in -> sin_addr), AF_INET);
00950                         if (h)
00951                         {
00952                                 hostname = h -> h_name;
00953                                 return true;
00954                         }
00955                 }
00956                 break;
00957 #ifdef ENABLE_IPV6
00958         case AF_INET6:
00959                 if (flags & NI_NUMERICHOST)
00960                 {
00961                         char slask[100]; // l2ip temporary
00962                         *slask = 0;
00963                         unsigned int prev = 0;
00964                         bool skipped = false;
00965                         bool ok_to_skip = true;
00966                         {
00967                                 unsigned short addr16[8];
00968                                 struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6 *)sa;
00969                                 memcpy(addr16, &sa_in6 -> sin6_addr, sizeof(addr16));
00970                                 for (size_t i = 0; i < 8; ++i)
00971                                 {
00972                                         unsigned short x = ntohs(addr16[i]);
00973                                         if (*slask && (x || !ok_to_skip || prev))
00974                                         {
00975 #if defined( _WIN32) && !defined(__CYGWIN__)
00976                                                 strcat_s(slask, sizeof(slask),":");
00977 #else
00978                                                 strcat(slask,":");
00979 #endif
00980                                         }
00981                                         if (x || !ok_to_skip)
00982                                         {
00983                                                 snprintf(slask + strlen(slask), sizeof(slask) - strlen(slask),"%x", x);
00984                                                 if (x && skipped)
00985                                                         ok_to_skip = false;
00986                                         }
00987                                         else
00988                                         {
00989                                                 skipped = true;
00990                                         }
00991                                         prev = x;
00992                                 }
00993                         }
00994                         if (!*slask)
00995                         {
00996 #if defined( _WIN32) && !defined(__CYGWIN__)
00997                                 strcpy_s(slask, sizeof(slask), "::");
00998 #else
00999                                 strcpy(slask, "::");
01000 #endif
01001                         }
01002                         hostname = slask;
01003                         return true;
01004                 }
01005                 else
01006                 {
01007                         // %! TODO: ipv6 reverse lookup
01008                         struct sockaddr_in6 *sa_in = (struct sockaddr_in6 *)sa;
01009                         struct hostent *h = gethostbyaddr( (const char *)&sa_in -> sin6_addr, sizeof(sa_in -> sin6_addr), AF_INET6);
01010                         if (h)
01011                         {
01012                                 hostname = h -> h_name;
01013                                 return true;
01014                         }
01015                 }
01016                 break;
01017 #endif
01018         }
01019         return false;
01020 #else
01021         char host[NI_MAXHOST];
01022         // NI_NOFQDN
01023         // NI_NUMERICHOST
01024         // NI_NAMEREQD
01025         // NI_NUMERICSERV
01026         // NI_DGRAM
01027         int n = getnameinfo(sa, sa_len, host, sizeof(host), NULL, 0, flags);
01028         if (n)
01029         {
01030                 // EAI_AGAIN
01031                 // EAI_BADFLAGS
01032                 // EAI_FAIL
01033                 // EAI_FAMILY
01034                 // EAI_MEMORY
01035                 // EAI_NONAME
01036                 // EAI_OVERFLOW
01037                 // EAI_SYSTEM
01038                 return false;
01039         }
01040         hostname = host;
01041         return true;
01042 #endif // NO_GETADDRINFO
01043 }

bool Utility::u2service ( const std::string &  name,
int &  service,
int  ai_flags = 0 
) [static]

Definition at line 1046 of file Utility.cpp.

01047 {
01048 #ifdef NO_GETADDRINFO
01049         // %!
01050         return false;
01051 #else
01052         struct addrinfo hints;
01053         service = 0;
01054         memset(&hints, 0, sizeof(hints));
01055         // AI_NUMERICHOST
01056         // AI_CANONNAME
01057         // AI_PASSIVE - server
01058         // AI_ADDRCONFIG
01059         // AI_V4MAPPED
01060         // AI_ALL
01061         // AI_NUMERICSERV
01062         hints.ai_flags = ai_flags;
01063         hints.ai_family = AF_UNSPEC;
01064         hints.ai_socktype = 0;
01065         hints.ai_protocol = 0;
01066         struct addrinfo *res;
01067         int n = getaddrinfo(NULL, name.c_str(), &hints, &res);
01068         if (!n)
01069         {
01070                 service = res -> ai_protocol;
01071                 freeaddrinfo(res);
01072                 return true;
01073         }
01074         return false;
01075 #endif // NO_GETADDRINFO
01076 }

void Utility::l2ip ( const   ipaddr_t,
std::string &  str 
) [static]

Convert binary ip address to string: ipv4.

Definition at line 418 of file Utility.cpp.

References NI_NUMERICHOST, and reverse().

Referenced by ResolvSocket::OnConnect(), ResolvSocket::OnDetached(), TcpSocket::Open(), ResolveLocal(), and Sa2String().

00419 {
00420         struct sockaddr_in sa;
00421         memset(&sa, 0, sizeof(sa));
00422         sa.sin_family = AF_INET;
00423         memcpy(&sa.sin_addr, &ip, sizeof(sa.sin_addr));
00424         Utility::reverse( (struct sockaddr *)&sa, sizeof(sa), str, NI_NUMERICHOST);
00425 }

void Utility::l2ip ( const in_addr &  ip,
std::string &  str 
) [static]

Definition at line 428 of file Utility.cpp.

References NI_NUMERICHOST, and reverse().

00429 {
00430         struct sockaddr_in sa;
00431         memset(&sa, 0, sizeof(sa));
00432         sa.sin_family = AF_INET;
00433         sa.sin_addr = ip;
00434         Utility::reverse( (struct sockaddr *)&sa, sizeof(sa), str, NI_NUMERICHOST);
00435 }

void Utility::ResolveLocal (  )  [static]

ResolveLocal (hostname) - call once before calling any GetLocal method.

Definition at line 508 of file Utility.cpp.

References l2ip(), m_addr, m_host, m_ip, m_local_resolved, and u2ip().

Referenced by GetLocalAddress(), GetLocalHostname(), and GetLocalIP().

00509 {
00510         char h[256];
00511 
00512         // get local hostname and translate into ip-address
00513         *h = 0;
00514         gethostname(h,255);
00515         {
00516                 if (Utility::u2ip(h, m_ip))
00517                 {
00518                         Utility::l2ip(m_ip, m_addr);
00519                 }
00520         }
00521 #ifdef ENABLE_IPV6
00522 #ifdef IPPROTO_IPV6
00523         memset(&m_local_ip6, 0, sizeof(m_local_ip6));
00524         {
00525                 if (Utility::u2ip(h, m_local_ip6))
00526                 {
00527                         Utility::l2ip(m_local_ip6, m_local_addr6);
00528                 }
00529         }
00530 #endif
00531 #endif
00532         m_host = h;
00533         m_local_resolved = true;
00534 }

const std::string & Utility::GetLocalHostname (  )  [static]

Returns local hostname, ResolveLocal must be called once before using.

See also:
ResolveLocal

Definition at line 537 of file Utility.cpp.

References m_host, m_local_resolved, and ResolveLocal().

00538 {
00539         if (!m_local_resolved)
00540         {
00541                 ResolveLocal();
00542         }
00543         return m_host;
00544 }

ipaddr_t Utility::GetLocalIP (  )  [static]

Returns local ip, ResolveLocal must be called once before using.

See also:
ResolveLocal

Definition at line 547 of file Utility.cpp.

References m_ip, m_local_resolved, and ResolveLocal().

00548 {
00549         if (!m_local_resolved)
00550         {
00551                 ResolveLocal();
00552         }
00553         return m_ip;
00554 }

const std::string & Utility::GetLocalAddress (  )  [static]

Returns local ip number as string.

See also:
ResolveLocal

Definition at line 557 of file Utility.cpp.

References m_addr, m_local_resolved, and ResolveLocal().

00558 {
00559         if (!m_local_resolved)
00560         {
00561                 ResolveLocal();
00562         }
00563         return m_addr;
00564 }

const std::string Utility::GetEnv ( const std::string &  name  )  [static]

Get environment variable.

Definition at line 591 of file Utility.cpp.

Referenced by SSLInitializer::SSLInitializer().

00592 {
00593 #if defined( _WIN32) && !defined(__CYGWIN__)
00594         size_t sz = 0;
00595         char tmp[2048];
00596         if (getenv_s(&sz, tmp, sizeof(tmp), name.c_str()))
00597         {
00598                 *tmp = 0;
00599         }
00600         return tmp;
00601 #else
00602         char *s = getenv(name.c_str());
00603         if (!s)
00604                 return "";
00605         return s;
00606 #endif
00607 }

void Utility::SetEnv ( const std::string &  var,
const std::string &  value 
) [static]

Set environment variable.

Parameters:
var Name of variable to set
value Value

Definition at line 610 of file Utility.cpp.

Referenced by HttpdSocket::OnHeaderComplete(), and SSLInitializer::SSLInitializer().

00611 {
00612 #if (defined(SOLARIS8) || defined(SOLARIS))
00613         {
00614                 static std::map<std::string, char *> vmap;
00615                 if (vmap.find(var) != vmap.end())
00616                 {
00617                         delete[] vmap[var];
00618                 }
00619                 size_t sz = var.size() + 1 + value.size() + 1;
00620                 vmap[var] = new char[sz];
00621                 snprintf(vmap[var], sz, "%s=%s", var.c_str(), value.c_str());
00622                 putenv( vmap[var] );
00623         }
00624 #elif defined _WIN32
00625         {
00626                 std::string slask = var + "=" + value;
00627                 _putenv( (char *)slask.c_str());
00628         }
00629 #else
00630         setenv(var.c_str(), value.c_str(), 1);
00631 #endif
00632 }

std::string Utility::Sa2String ( struct sockaddr *  sa  )  [static]

Convert sockaddr struct to human readable string.

Parameters:
sa Ptr to sockaddr struct

Definition at line 635 of file Utility.cpp.

References l2ip(), and l2string().

Referenced by SctpSocket::getladdrs(), and SctpSocket::getpaddrs().

00636 {
00637 #ifdef ENABLE_IPV6
00638 #ifdef IPPROTO_IPV6
00639         if (sa -> sa_family == AF_INET6)
00640         {
00641                 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
00642                 std::string tmp;
00643                 Utility::l2ip(sa6 -> sin6_addr, tmp);
00644                 return tmp + ":" + Utility::l2string(ntohs(sa6 -> sin6_port));
00645         }
00646 #endif
00647 #endif
00648         if (sa -> sa_family == AF_INET)
00649         {
00650                 struct sockaddr_in *sa4 = (struct sockaddr_in *)sa;
00651                 ipaddr_t a;
00652                 memcpy(&a, &sa4 -> sin_addr, 4);
00653                 std::string tmp;
00654                 Utility::l2ip(a, tmp);
00655                 return tmp + ":" + Utility::l2string(ntohs(sa4 -> sin_port));
00656         }
00657         return "";
00658 }

void Utility::GetTime ( struct timeval *  p  )  [static]

Get current time in sec/microseconds.

Definition at line 661 of file Utility.cpp.

Referenced by UdpSocket::OnRead().

00662 {
00663 #ifdef _WIN32
00664         FILETIME ft; // Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).
00665         GetSystemTimeAsFileTime(&ft);
00666         uint64_t tt;
00667         memcpy(&tt, &ft, sizeof(tt));
00668         tt /= 10; // make it usecs
00669         p->tv_sec = (long)tt / 1000000;
00670         p->tv_usec = (long)tt % 1000000;
00671 #else
00672         gettimeofday(p, NULL);
00673 #endif
00674 }

std::auto_ptr< SocketAddress > Utility::CreateAddress ( struct sockaddr *  sa,
socklen_t  sa_len 
) [static]

Definition at line 677 of file Utility.cpp.

00678 {
00679         switch (sa -> sa_family)
00680         {
00681         case AF_INET:
00682                 if (sa_len == sizeof(struct sockaddr_in))
00683                 {
00684                         struct sockaddr_in *p = (struct sockaddr_in *)sa;
00685                         return std::auto_ptr<SocketAddress>(new Ipv4Address(*p));
00686                 }
00687                 break;
00688 #ifdef ENABLE_IPV6
00689 #ifdef IPPROTO_IPV6
00690         case AF_INET6:
00691                 if (sa_len == sizeof(struct sockaddr_in6))
00692                 {
00693                         struct sockaddr_in6 *p = (struct sockaddr_in6 *)sa;
00694                         return std::auto_ptr<SocketAddress>(new Ipv6Address(*p));
00695                 }
00696                 break;
00697 #endif
00698 #endif
00699         }
00700         return std::auto_ptr<SocketAddress>(NULL);
00701 }

unsigned long Utility::ThreadID (  )  [static]

Definition at line 1079 of file Utility.cpp.

Referenced by Debug::Debug(), Debug::Print(), SSLInitializer::SSL_id_function(), and Debug::~Debug().

01080 {
01081 #ifdef _WIN32
01082         return GetCurrentThreadId();
01083 #else
01084         return (unsigned long)pthread_self();
01085 #endif
01086 }

std::string Utility::ToLower ( const std::string &  str  )  [static]

Definition at line 1089 of file Utility.cpp.

Referenced by HttpBaseSocket::OnFirst(), HttpBaseSocket::OnHeader(), SmtpdSocket::OnLine(), HTTPSocket::OnLine(), Ajp13Socket::ReceiveForwardRequest(), and Utility::Uri::Uri().

01090 {
01091         std::string r;
01092         for (size_t i = 0; i < str.size(); ++i)
01093         {
01094                 if (str[i] >= 'A' && str[i] <= 'Z')
01095                         r += str[i] | 32;
01096                 else
01097                         r += str[i];
01098         }
01099         return r;
01100 }

std::string Utility::ToUpper ( const std::string &  str  )  [static]

Definition at line 1103 of file Utility.cpp.

Referenced by SmtpdSocket::OnLine().

01104 {
01105         std::string r;
01106         for (size_t i = 0; i < str.size(); ++i)
01107         {
01108                 if (str[i] >= 'a' && str[i] <= 'z')
01109                         r += (char)(str[i] - 32);
01110                 else
01111                         r += str[i];
01112         }
01113         return r;
01114 }

std::string Utility::ToString ( double  d  )  [static]

Definition at line 1117 of file Utility.cpp.

Referenced by StreamWriter::operator<<().

01118 {
01119         char tmp[100];
01120         snprintf(tmp, sizeof(tmp), "%f", d);
01121         return tmp;
01122 }

unsigned long Utility::Rnd (  )  [static]

Returns a random 32-bit integer.

Definition at line 1125 of file Utility.cpp.

References Utility::Rng::Get().

Referenced by u2ip().

01126 {
01127 static  Utility::Rng generator( (unsigned long)time(NULL) );
01128         return generator.Get();
01129 }

const std::string Utility::Stack (  )  [static]

Definition at line 1268 of file Utility.cpp.

01269 {
01270 #if defined LINUX
01271 #define BFSIZE 255
01272         void *buffer[BFSIZE];
01273         int n = backtrace(buffer, BFSIZE);
01274         char **res = backtrace_symbols(buffer, n);
01275         std::string tmp;
01276         for (int i = 0; i < n; ++i)
01277         {
01278                 std::string x = res[i];
01279                 std::string plus;
01280                 std::string addr;
01281                 size_t pos = x.find("(");
01282                 if (pos != std::string::npos)
01283                 {
01284                         x = x.substr(pos + 1); // skip executable name
01285 
01286                         pos = x.find(")");
01287                         if (pos != std::string::npos)
01288                         {
01289                                 addr = x.substr(pos + 1);
01290                                 x = x.substr(0, pos);
01291                         }
01292 
01293                         pos = x.find("+");
01294                         if (pos != std::string::npos)
01295                         {
01296                                 plus = x.substr(pos);
01297                                 x = x.substr(0, pos);
01298                         }
01299                 }
01300                 char zz[1000];
01301                 {
01302                         size_t sz = 1000;
01303                         int status = 0;
01304                         abi::__cxa_demangle( x.c_str(), zz, &sz, &status);
01305 
01306                         if (!status)
01307                         {
01308                                 tmp += zz;
01309                                 tmp += plus;
01310                                 tmp += addr;
01311                         }
01312                         else
01313                         {
01314                                 tmp += res[i];
01315                         }
01316                         tmp += "\n";
01317                 }
01318                 // dladdr() test
01319                 if (0)
01320                 {
01321                         Dl_info info;
01322                         int n = dladdr(buffer[i], &info);
01323                         if (!n)
01324                                 printf("%d: dladdr() failed\n", i);
01325                         else
01326                         {
01327                                 size_t sz = 1000;
01328                                 int status = 0;
01329                                 abi::__cxa_demangle( info.dli_sname, zz, &sz, &status);
01330 
01331                                 printf("%d: %s: %s\n", i, info.dli_fname, info.dli_sname);
01332                                 if (!status)
01333                                         printf("                %s\n", zz);
01334                         }
01335                 } // end of dladdr() test
01336         } // for (i)
01337         free(res);
01338         return tmp;
01339 #else
01340         return "Not available";
01341 #endif
01342 }

const std::string Utility::FromUtf8 ( const std::string &  str  )  [static]

Utf8 decrypt, encrypt.

Definition at line 1345 of file Utility.cpp.

Referenced by XmlNode::GetContent(), XmlNode::GetNodeName(), XmlNode::GetNodeNsHref(), XmlNode::GetNodeNsPrefix(), XmlNode::GetNsMap(), XmlNode::GetNsMapRe(), and XmlNode::GetProperty().

01346 {
01347         if (!str.size())
01348                 return "";
01349         std::string r;
01350         for (size_t i = 0; i < str.size(); ++i)
01351         {
01352                 if (i < str.size() - 1 && (str[i] & 0xe0) == 0xc0 && (str[i + 1] & 0xc0) == 0x80)
01353                 {
01354                         int c1 = str[i] & 0x1f;
01355                         int c2 = str[++i] & 0x3f;
01356                         int c = (c1 << 6) + c2;
01357                         r += (char)c;
01358                 }
01359                 else
01360                 {
01361                         r += str[i];
01362                 }
01363         }
01364         return r;
01365 }

const std::string Utility::ToUtf8 ( const std::string &  str  )  [static]

Definition at line 1369 of file Utility.cpp.

01370 {
01371         if (str.empty())
01372                 return "";
01373         std::string r;
01374         for (size_t i = 0; i < str.size(); ++i)
01375         {
01376                 if (((unsigned)str[i] & 0x80) == 0x80)
01377                 {
01378                         r += (str[i] >> 6) | 0xc0;
01379                         r += (str[i] & 0x3f) | 0x80;
01380                 }
01381                 else
01382                 {
01383                         r += str[i];
01384                 }
01385         }
01386         return r;
01387 }

const Utility::Path Utility::CurrentDirectory (  )  [static]

File system stuff.

Definition at line 1390 of file Utility.cpp.

01391 {
01392 #ifdef _WIN32
01393         TCHAR slask[MAX_PATH + 1];
01394         DWORD ret =
01395 #ifdef UNICODE
01396         ::GetCurrentDirectoryW(MAX_PATH, slask);
01397 #else
01398         ::GetCurrentDirectoryA(MAX_PATH, slask);
01399 #endif
01400         if (!ret)
01401         {
01402                 *slask = 0;
01403                 DWORD err = GetLastError();
01404         }
01405         return Path(slask);
01406 #else
01407         char slask[32000];
01408         if (!getcwd(slask, 32000))
01409         {
01410                 return Path(".");
01411         }
01412         return Path(slask);
01413 #endif
01414 }

bool Utility::ChangeDirectory ( const Path to_dir  )  [static]

Definition at line 1417 of file Utility.cpp.

References Utility::Path::GetPath().

01418 {
01419 #ifdef _WIN32
01420         return SetCurrentDirectory(to_dir.GetPath().c_str()) ? true : false;
01421 #else
01422         if (chdir( to_dir.GetPath().c_str() ) == -1)
01423         {
01424                 return false;
01425         }
01426         return true;
01427 #endif
01428 }

void Utility::Sleep ( int  ms  )  [static]

wait a specified number of ms

Definition at line 1431 of file Utility.cpp.

Referenced by Thread::StartThread(), SocketThread::~SocketThread(), and Thread::~Thread().

01432 {
01433 #ifdef _WIN32
01434 	::Sleep(ms);
01435 #else
01436         struct timeval tv;
01437         tv.tv_sec = ms / 1000;
01438         tv.tv_usec = (ms % 1000) * 1000;
01439         select(0, NULL, NULL, NULL, &tv);
01440 #endif
01441 }


Member Data Documentation

const char * Utility::Logo [static]

Definition at line 216 of file Utility.h.

Referenced by HttpdSocket::OnData(), and HttpdSocket::OnHeaderComplete().

std::string Utility::m_host [static, private]

local hostname

Definition at line 232 of file Utility.h.

Referenced by GetLocalHostname(), and ResolveLocal().

ipaddr_t Utility::m_ip = 0 [static, private]

local ip address

Definition at line 233 of file Utility.h.

Referenced by GetLocalIP(), and ResolveLocal().

std::string Utility::m_addr [static, private]

local ip address in string format

Definition at line 234 of file Utility.h.

Referenced by GetLocalAddress(), and ResolveLocal().

bool Utility::m_local_resolved = false [static, private]

ResolveLocal has been called if true.

Definition at line 241 of file Utility.h.

Referenced by GetLocalAddress(), GetLocalHostname(), GetLocalIP(), and ResolveLocal().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4