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 (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 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< SocketAddressCreateAddress (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 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.

Detailed Description

Conversion utilities.

Definition at line 57 of file Utility.h.


Member Function Documentation

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

Definition at line 62 of file Utility.cpp.

00063 {
00064         std::string str;
00065         Base64 m_b;
00066         m_b.encode(str_in, str, false); // , false == do not add cr/lf
00067         return str;
00068 }

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

Definition at line 71 of file Utility.cpp.

References Base64::decode().

00072 {
00073         std::string str;
00074         Base64 m_b;
00075         m_b.decode(str_in, str);
00076         return str;
00077 }

std::string Utility::l2string ( long  l  )  [static]

Definition at line 80 of file Utility.cpp.

Referenced by Ipv4Address::Convert(), HttpPostSocket::DoMultipartPost(), HttpPostSocket::HttpPostSocket(), HttpPutSocket::OnConnect(), HttpPostSocket::OnConnect(), HttpGetSocket::OnConnect(), TcpSocket::Open(), HttpdCookies::replacevalue(), Sa2String(), and HttpdSocket::Send64().

00081 {
00082         std::string str;
00083         char tmp[100];
00084         sprintf(tmp,"%ld",l);
00085         str = tmp;
00086         return str;
00087 }

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

Definition at line 90 of file Utility.cpp.

00091 {
00092         std::string str;
00093         uint64_t tmp = l;
00094         while (tmp)
00095         {
00096                 uint64_t a = tmp % 10;
00097                 str = (char)(a + 48) + str;
00098                 tmp /= 10;
00099         }
00100         if (!str.size())
00101         {
00102                 str = "0";
00103         }
00104         return str;
00105 }

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

Definition at line 108 of file Utility.cpp.

00109 {
00110         uint64_t l = 0;
00111         for (size_t i = 0; i < str.size(); i++)
00112         {
00113                 l = l * 10 + str[i] - 48;
00114         }
00115         return l;
00116 }

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

Definition at line 119 of file Utility.cpp.

00120 {
00121         unsigned int r = 0;
00122         for (size_t i = 0; i < str.size(); i++)
00123         {
00124                 r = r * 16 + str[i] - 48 - ((str[i] >= 'A') ? 7 : 0) - ((str[i] >= 'a') ? 32 : 0);
00125         }
00126         return r;
00127 }

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

Definition at line 134 of file Utility.cpp.

Referenced by HttpPostSocket::OnConnect().

00135 {
00136 static  char hex[] = "0123456789ABCDEF";
00137         std::string dst;
00138         for (size_t i = 0; i < src.size(); i++)
00139         {
00140                 if (isalnum(src[i]))
00141                 {
00142                         dst += src[i];
00143                 }
00144                 else
00145                 if (src[i] == ' ')
00146                 {
00147                         dst += '+';
00148                 }
00149                 else
00150                 {
00151                         unsigned char c = static_cast<unsigned char>(src[i]);
00152                         dst += '%';
00153                         dst += hex[c / 16];
00154                         dst += hex[c % 16];
00155                 }
00156         }
00157         return dst;
00158 } // rfc1738_encode

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

Definition at line 165 of file Utility.cpp.

00166 {
00167         std::string dst;
00168         for (size_t i = 0; i < src.size(); i++)
00169         {
00170                 if (src[i] == '%' && isxdigit(src[i + 1]) && isxdigit(src[i + 2]))
00171                 {
00172                         char c1 = src[++i];
00173                         char c2 = src[++i];
00174                         c1 = c1 - 48 - ((c1 >= 'A') ? 7 : 0) - ((c1 >= 'a') ? 32 : 0);
00175                         c2 = c2 - 48 - ((c2 >= 'A') ? 7 : 0) - ((c2 >= 'a') ? 32 : 0);
00176                         dst += (char)(c1 * 16 + c2);
00177                 }
00178                 else
00179                 if (src[i] == '+')
00180                 {
00181                         dst += ' ';
00182                 }
00183                 else
00184                 {
00185                         dst += src[i];
00186                 }
00187         }
00188         return dst;
00189 } // rfc1738_decode

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

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

Definition at line 192 of file Utility.cpp.

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

00193 {
00194         int dots = 0;
00195         // %! ignore :port?
00196         for (size_t i = 0; i < str.size(); i++)
00197         {
00198                 if (str[i] == '.')
00199                         dots++;
00200                 else
00201                 if (!isdigit(str[i]))
00202                         return false;
00203         }
00204         if (dots != 3)
00205                 return false;
00206         return true;
00207 }

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

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

Definition at line 210 of file Utility.cpp.

References Parse::getword().

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

00211 {
00212         size_t qc = 0;
00213         size_t qd = 0;
00214         for (size_t i = 0; i < str.size(); i++)
00215         {
00216                 qc += (str[i] == ':') ? 1 : 0;
00217                 qd += (str[i] == '.') ? 1 : 0;
00218         }
00219         if (qc > 7)
00220         {
00221                 return false;
00222         }
00223         if (qd && qd != 3)
00224         {
00225                 return false;
00226         }
00227         Parse pa(str,":.");
00228         std::string tmp = pa.getword();
00229         while (tmp.size())
00230         {
00231                 if (tmp.size() > 4)
00232                 {
00233                         return false;
00234                 }
00235                 for (size_t i = 0; i < tmp.size(); i++)
00236                 {
00237                         if (tmp[i] < '0' || (tmp[i] > '9' && tmp[i] < 'A') ||
00238                                 (tmp[i] > 'F' && tmp[i] < 'a') || tmp[i] > 'f')
00239                         {
00240                                 return false;
00241                         }
00242                 }
00243                 //
00244                 tmp = pa.getword();
00245         }
00246         return true;
00247 }

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

Hostname to ip resolution ipv4, not asynchronous.

Definition at line 250 of file Utility.cpp.

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

00251 {
00252         struct sockaddr_in sa;
00253         bool r = Utility::u2ip(str, sa);
00254         memcpy(&l, &sa.sin_addr, sizeof(l));
00255         return r;
00256 }

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

Definition at line 532 of file Utility.cpp.

References AI_NUMERICHOST, isipv4(), and RandomNumber::next().

00533 {
00534         memset(&sa, 0, sizeof(sa));
00535         sa.sin_family = AF_INET;
00536 #ifdef NO_GETADDRINFO
00537         if ((ai_flags & AI_NUMERICHOST) != 0 || isipv4(host))
00538         {
00539                 Parse pa((char *)host.c_str(), ".");
00540                 union {
00541                         struct {
00542                                 unsigned char b1;
00543                                 unsigned char b2;
00544                                 unsigned char b3;
00545                                 unsigned char b4;
00546                         } a;
00547                         ipaddr_t l;
00548                 } u;
00549                 u.a.b1 = static_cast<unsigned char>(pa.getvalue());
00550                 u.a.b2 = static_cast<unsigned char>(pa.getvalue());
00551                 u.a.b3 = static_cast<unsigned char>(pa.getvalue());
00552                 u.a.b4 = static_cast<unsigned char>(pa.getvalue());
00553                 memcpy(&sa.sin_addr, &u.l, sizeof(sa.sin_addr));
00554                 return true;
00555         }
00556 #ifndef LINUX
00557         struct hostent *he = gethostbyname( host.c_str() );
00558         if (!he)
00559         {
00560                 return false;
00561         }
00562         memcpy(&sa.sin_addr, he -> h_addr, sizeof(sa.sin_addr));
00563 #else
00564         struct hostent he;
00565         struct hostent *result;
00566         int myerrno;
00567         char buf[2000];
00568         int n = gethostbyname_r(host.c_str(), &he, buf, sizeof(buf), &result, &myerrno);
00569         if (n)
00570         {
00571                 return false;
00572         }
00573         memcpy(&sa.sin_addr, he.h_addr, 4);
00574 #endif
00575         return true;
00576 #else
00577         struct addrinfo hints;
00578         memset(&hints, 0, sizeof(hints));
00579         // AI_NUMERICHOST
00580         // AI_CANONNAME
00581         // AI_PASSIVE - server
00582         // AI_ADDRCONFIG
00583         // AI_V4MAPPED
00584         // AI_ALL
00585         // AI_NUMERICSERV
00586         hints.ai_flags = ai_flags;
00587         hints.ai_family = AF_INET;
00588         hints.ai_socktype = 0;
00589         hints.ai_protocol = 0;
00590         struct addrinfo *res;
00591         if (Utility::isipv4(host))
00592                 hints.ai_flags |= AI_NUMERICHOST;
00593         int n = getaddrinfo(host.c_str(), NULL, &hints, &res);
00594         if (!n)
00595         {
00596                 static RandomNumber prng( true );
00597                 std::vector<struct addrinfo *> vec;
00598                 struct addrinfo *ai = res;
00599                 while (ai)
00600                 {
00601                         if (ai -> ai_addrlen == sizeof(sa))
00602                                 vec.push_back( ai );
00603                         prng.next();
00604                         //
00605                         ai = ai -> ai_next;
00606                 }
00607                 if (!vec.size())
00608                         return false;
00609                 ai = vec[prng.next() % vec.size()];
00610                 {
00611                         memcpy(&sa, ai -> ai_addr, ai -> ai_addrlen);
00612                 }
00613                 freeaddrinfo(res);
00614                 return true;
00615         }
00616         std::string error = "Error: ";
00617 #ifndef __CYGWIN__
00618         error += gai_strerror(n);
00619 #endif
00620         return false;
00621 #endif // NO_GETADDRINFO
00622 }

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

Reverse lookup of address to hostname.

Definition at line 744 of file Utility.cpp.

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

00745 {
00746         std::string service;
00747         return Utility::reverse(sa, sa_len, hostname, service, flags);
00748 }

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

Definition at line 751 of file Utility.cpp.

References NI_NUMERICHOST.

00752 {
00753         hostname = "";
00754         service = "";
00755 #ifdef NO_GETADDRINFO
00756         switch (sa -> sa_family)
00757         {
00758         case AF_INET:
00759                 if (flags & NI_NUMERICHOST)
00760                 {
00761                         union {
00762                                 struct {
00763                                         unsigned char b1;
00764                                         unsigned char b2;
00765                                         unsigned char b3;
00766                                         unsigned char b4;
00767                                 } a;
00768                                 ipaddr_t l;
00769                         } u;
00770                         struct sockaddr_in *sa_in = (struct sockaddr_in *)sa;
00771                         memcpy(&u.l, &sa_in -> sin_addr, sizeof(u.l));
00772                         char tmp[100];
00773                         sprintf(tmp, "%u.%u.%u.%u", u.a.b1, u.a.b2, u.a.b3, u.a.b4);
00774                         hostname = tmp;
00775                         return true;
00776                 }
00777                 else
00778                 {
00779                         struct sockaddr_in *sa_in = (struct sockaddr_in *)sa;
00780                         struct hostent *h = gethostbyaddr( (const char *)&sa_in -> sin_addr, sizeof(sa_in -> sin_addr), AF_INET);
00781                         if (h)
00782                         {
00783                                 hostname = h -> h_name;
00784                                 return true;
00785                         }
00786                 }
00787                 break;
00788 #ifdef ENABLE_IPV6
00789         case AF_INET6:
00790                 if (flags & NI_NUMERICHOST)
00791                 {
00792                         char slask[100]; // l2ip temporary
00793                         *slask = 0;
00794                         unsigned int prev = 0;
00795                         bool skipped = false;
00796                         bool ok_to_skip = true;
00797                         {
00798                                 unsigned short addr16[8];
00799                                 struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6 *)sa;
00800                                 memcpy(addr16, &sa_in6 -> sin6_addr, sizeof(addr16));
00801                                 for (size_t i = 0; i < 8; i++)
00802                                 {
00803                                         unsigned short x = ntohs(addr16[i]);
00804                                         if (*slask && (x || !ok_to_skip || prev))
00805                                                 strcat(slask,":");
00806                                         if (x || !ok_to_skip)
00807                                         {
00808                                                 sprintf(slask + strlen(slask),"%x", x);
00809                                                 if (x && skipped)
00810                                                         ok_to_skip = false;
00811                                         }
00812                                         else
00813                                         {
00814                                                 skipped = true;
00815                                         }
00816                                         prev = x;
00817                                 }
00818                         }
00819                         if (!*slask)
00820                                 strcpy(slask, "::");
00821                         hostname = slask;
00822                         return true;
00823                 }
00824                 else
00825                 {
00826                         // %! TODO: ipv6 reverse lookup
00827                         struct sockaddr_in6 *sa_in = (struct sockaddr_in6 *)sa;
00828                         struct hostent *h = gethostbyaddr( (const char *)&sa_in -> sin6_addr, sizeof(sa_in -> sin6_addr), AF_INET6);
00829                         if (h)
00830                         {
00831                                 hostname = h -> h_name;
00832                                 return true;
00833                         }
00834                 }
00835                 break;
00836 #endif
00837         }
00838         return false;
00839 #else
00840         char host[NI_MAXHOST];
00841         char serv[NI_MAXSERV];
00842         // NI_NOFQDN
00843         // NI_NUMERICHOST
00844         // NI_NAMEREQD
00845         // NI_NUMERICSERV
00846         // NI_DGRAM
00847         int n = getnameinfo(sa, sa_len, host, sizeof(host), serv, sizeof(serv), flags);
00848         if (n)
00849         {
00850                 // EAI_AGAIN
00851                 // EAI_BADFLAGS
00852                 // EAI_FAIL
00853                 // EAI_FAMILY
00854                 // EAI_MEMORY
00855                 // EAI_NONAME
00856                 // EAI_OVERFLOW
00857                 // EAI_SYSTEM
00858                 return false;
00859         }
00860         hostname = host;
00861         service = serv;
00862         return true;
00863 #endif // NO_GETADDRINFO
00864 }

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

Definition at line 867 of file Utility.cpp.

00868 {
00869 #ifdef NO_GETADDRINFO
00870         // %!
00871         return false;
00872 #else
00873         struct addrinfo hints;
00874         service = 0;
00875         memset(&hints, 0, sizeof(hints));
00876         // AI_NUMERICHOST
00877         // AI_CANONNAME
00878         // AI_PASSIVE - server
00879         // AI_ADDRCONFIG
00880         // AI_V4MAPPED
00881         // AI_ALL
00882         // AI_NUMERICSERV
00883         hints.ai_flags = ai_flags;
00884         hints.ai_family = AF_UNSPEC;
00885         hints.ai_socktype = 0;
00886         hints.ai_protocol = 0;
00887         struct addrinfo *res;
00888         int n = getaddrinfo(NULL, name.c_str(), &hints, &res);
00889         if (!n)
00890         {
00891                 service = res -> ai_protocol;
00892                 freeaddrinfo(res);
00893                 return true;
00894         }
00895         return false;
00896 #endif // NO_GETADDRINFO
00897 }

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

Convert binary ip address to string: ipv4.

Definition at line 272 of file Utility.cpp.

References NI_NUMERICHOST, and reverse().

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

00273 {
00274         struct sockaddr_in sa;
00275         memset(&sa, 0, sizeof(sa));
00276         sa.sin_family = AF_INET;
00277         memcpy(&sa.sin_addr, &ip, sizeof(sa.sin_addr));
00278         Utility::reverse( (struct sockaddr *)&sa, sizeof(sa), str, NI_NUMERICHOST);
00279 }

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

Definition at line 282 of file Utility.cpp.

References NI_NUMERICHOST, and reverse().

00283 {
00284         struct sockaddr_in sa;
00285         memset(&sa, 0, sizeof(sa));
00286         sa.sin_family = AF_INET;
00287         sa.sin_addr = ip;
00288         Utility::reverse( (struct sockaddr *)&sa, sizeof(sa), str, NI_NUMERICHOST);
00289 }

void Utility::ResolveLocal (  )  [static]

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

Definition at line 356 of file Utility.cpp.

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

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

00357 {
00358         char h[256];
00359 
00360         // get local hostname and translate into ip-address
00361         *h = 0;
00362         gethostname(h,255);
00363         {
00364                 if (Utility::u2ip(h, m_ip))
00365                 {
00366                         Utility::l2ip(m_ip, m_addr);
00367                 }
00368         }
00369 #ifdef ENABLE_IPV6
00370 #ifdef IPPROTO_IPV6
00371         memset(&m_local_ip6, 0, sizeof(m_local_ip6));
00372         {
00373                 if (Utility::u2ip(h, m_local_ip6))
00374                 {
00375                         Utility::l2ip(m_local_ip6, m_local_addr6);
00376                 }
00377         }
00378 #endif
00379 #endif
00380         m_host = h;
00381         m_local_resolved = true;
00382 }

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

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

See also:
ResolveLocal

Definition at line 385 of file Utility.cpp.

References m_host, m_local_resolved, and ResolveLocal().

00386 {
00387         if (!m_local_resolved)
00388         {
00389                 ResolveLocal();
00390         }
00391         return m_host;
00392 }

ipaddr_t Utility::GetLocalIP (  )  [static]

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

See also:
ResolveLocal

Definition at line 395 of file Utility.cpp.

References m_ip, m_local_resolved, and ResolveLocal().

00396 {
00397         if (!m_local_resolved)
00398         {
00399                 ResolveLocal();
00400         }
00401         return m_ip;
00402 }

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

Returns local ip number as string.

See also:
ResolveLocal

Definition at line 405 of file Utility.cpp.

References m_addr, m_local_resolved, and ResolveLocal().

00406 {
00407         if (!m_local_resolved)
00408         {
00409                 ResolveLocal();
00410         }
00411         return m_addr;
00412 }

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 439 of file Utility.cpp.

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

00440 {
00441 #if (defined(SOLARIS8) || defined(SOLARIS))
00442         {
00443                 static std::map<std::string, char *> vmap;
00444                 if (vmap.find(var) != vmap.end())
00445                 {
00446                         delete[] vmap[var];
00447                 }
00448                 vmap[var] = new char[var.size() + 1 + value.size() + 1];
00449                 sprintf(vmap[var], "%s=%s", var.c_str(), value.c_str());
00450                 putenv( vmap[var] );
00451         }
00452 #elif defined _WIN32
00453         {
00454                 std::string slask = var + "=" + value;
00455                 _putenv( (char *)slask.c_str());
00456         }
00457 #else
00458         setenv(var.c_str(), value.c_str(), 1);
00459 #endif
00460 }

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

Convert sockaddr struct to human readable string.

Parameters:
sa Ptr to sockaddr struct

Definition at line 463 of file Utility.cpp.

References l2ip(), and l2string().

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

00464 {
00465 #ifdef ENABLE_IPV6
00466 #ifdef IPPROTO_IPV6
00467         if (sa -> sa_family == AF_INET6)
00468         {
00469                 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
00470                 std::string tmp;
00471                 Utility::l2ip(sa6 -> sin6_addr, tmp);
00472                 return tmp + ":" + Utility::l2string(ntohs(sa6 -> sin6_port));
00473         }
00474 #endif
00475 #endif
00476         if (sa -> sa_family == AF_INET)
00477         {
00478                 struct sockaddr_in *sa4 = (struct sockaddr_in *)sa;
00479                 ipaddr_t a;
00480                 memcpy(&a, &sa4 -> sin_addr, 4);
00481                 std::string tmp;
00482                 Utility::l2ip(a, tmp);
00483                 return tmp + ":" + Utility::l2string(ntohs(sa4 -> sin_port));
00484         }
00485         return "";
00486 }

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

Get current time in sec/microseconds.

Definition at line 489 of file Utility.cpp.

00490 {
00491 #ifdef _WIN32
00492         FILETIME ft; // Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).
00493         GetSystemTimeAsFileTime(&ft);
00494         uint64_t tt;
00495         memcpy(&tt, &ft, sizeof(tt));
00496         tt /= 10; // make it usecs
00497         p->tv_sec = (long)tt / 1000000;
00498         p->tv_usec = (long)tt % 1000000;
00499 #else
00500         gettimeofday(p, NULL);
00501 #endif
00502 }

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

Definition at line 505 of file Utility.cpp.

00506 {
00507         switch (sa -> sa_family)
00508         {
00509         case AF_INET:
00510                 if (sa_len == sizeof(struct sockaddr_in))
00511                 {
00512                         struct sockaddr_in *p = (struct sockaddr_in *)sa;
00513                         return std::auto_ptr<SocketAddress>(new Ipv4Address(*p));
00514                 }
00515                 break;
00516 #ifdef ENABLE_IPV6
00517 #ifdef IPPROTO_IPV6
00518         case AF_INET6:
00519                 if (sa_len == sizeof(struct sockaddr_in6))
00520                 {
00521                         struct sockaddr_in6 *p = (struct sockaddr_in6 *)sa;
00522                         return std::auto_ptr<SocketAddress>(new Ipv6Address(*p));
00523                 }
00524                 break;
00525 #endif
00526 #endif
00527         }
00528         return std::auto_ptr<SocketAddress>(NULL);
00529 }

unsigned long Utility::ThreadID (  )  [static]

Definition at line 900 of file Utility.cpp.

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

00901 {
00902 #ifdef _WIN32
00903         return GetCurrentThreadId();
00904 #else
00905         return (unsigned long)pthread_self();
00906 #endif
00907 }

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

Definition at line 910 of file Utility.cpp.

Referenced by SmtpdSocket::OnLine().

00911 {
00912         std::string r;
00913         for (size_t i = 0; i < str.size(); i++)
00914         {
00915                 if (str[i] >= 'A' && str[i] <= 'Z')
00916                         r += str[i] | 32;
00917                 else
00918                         r += str[i];
00919         }
00920         return r;
00921 }

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

Definition at line 924 of file Utility.cpp.

Referenced by SmtpdSocket::OnLine().

00925 {
00926         std::string r;
00927         for (size_t i = 0; i < str.size(); i++)
00928         {
00929                 if (str[i] >= 'a' && str[i] <= 'z')
00930                         r += (char)(str[i] - 32);
00931                 else
00932                         r += str[i];
00933         }
00934         return r;
00935 }

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

Definition at line 938 of file Utility.cpp.

00939 {
00940         char tmp[100];
00941         sprintf(tmp, "%f", d);
00942         return tmp;
00943 }


Member Data Documentation

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

local hostname

Definition at line 146 of file Utility.h.

Referenced by GetLocalHostname(), and ResolveLocal().

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

local ip address

Definition at line 147 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 148 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 155 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