Utility Class ReferenceConversion utilities.
More...
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 < 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. | |
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 | Rng |
| The Mersenne Twister http://www.math.keio.ac.jp/~matumoto/emt.html. More... | |
| class | Uri |
Definition at line 51 of file Utility.h.
| std::string Utility::base64 | ( | const std::string & | str_in | ) | [static] |
Definition at line 181 of file Utility.cpp.
00182 { 00183 std::string str; 00184 Base64 m_b; 00185 m_b.encode(str_in, str, false); // , false == do not add cr/lf 00186 return str; 00187 }
| std::string Utility::base64d | ( | const std::string & | str_in | ) | [static] |
Definition at line 190 of file Utility.cpp.
References Base64::decode().
00191 { 00192 std::string str; 00193 Base64 m_b; 00194 m_b.decode(str_in, str); 00195 return str; 00196 }
| std::string Utility::l2string | ( | long | l | ) | [static] |
Definition at line 199 of file Utility.cpp.
Referenced by UdpSocket::Bind(), SctpSocket::Bind(), ListenSocket< X >::Bind(), Ipv4Address::Convert(), HttpPostSocket::DoMultipartPost(), HttpClientSocket::HttpClientSocket(), HttpPostSocket::HttpPostSocket(), HttpPutSocket::OnConnect(), HttpPostSocket::OnConnect(), HttpGetSocket::OnConnect(), TcpSocket::Open(), StreamWriter::operator<<(), Debug::operator<<(), Ajp13Socket::ReceiveForwardRequest(), HttpdCookies::replacevalue(), HttpBaseSocket::Respond(), Sa2String(), HttpdSocket::Send64(), HttpRequest::SetAttribute(), and HttpTransaction::SetHeader().
00200 { 00201 std::string str; 00202 char tmp[100]; 00203 sprintf(tmp,"%ld",l); 00204 str = tmp; 00205 return str; 00206 }
| std::string Utility::bigint2string | ( | uint64_t | l | ) | [static] |
Definition at line 209 of file Utility.cpp.
Referenced by StreamWriter::operator<<().
00210 { 00211 std::string str; 00212 uint64_t tmp = l; 00213 while (tmp) 00214 { 00215 uint64_t a = tmp % 10; 00216 str = (char)(a + 48) + str; 00217 tmp /= 10; 00218 } 00219 if (!str.size()) 00220 { 00221 str = "0"; 00222 } 00223 return str; 00224 }
| uint64_t Utility::atoi64 | ( | const std::string & | str | ) | [static] |
Definition at line 227 of file Utility.cpp.
00228 { 00229 uint64_t l = 0; 00230 for (size_t i = 0; i < str.size(); i++) 00231 { 00232 l = l * 10 + str[i] - 48; 00233 } 00234 return l; 00235 }
| unsigned int Utility::hex2unsigned | ( | const std::string & | str | ) | [static] |
Definition at line 238 of file Utility.cpp.
Referenced by HTTPSocket::OnRawData().
00239 { 00240 unsigned int r = 0; 00241 for (size_t i = 0; i < str.size(); i++) 00242 { 00243 r = r * 16 + str[i] - 48 - ((str[i] >= 'A') ? 7 : 0) - ((str[i] >= 'a') ? 32 : 0); 00244 } 00245 return r; 00246 }
| std::string Utility::rfc1738_encode | ( | const std::string & | src | ) | [static] |
Definition at line 253 of file Utility.cpp.
Referenced by HttpPostSocket::OnConnect().
00254 { 00255 static char hex[] = "0123456789ABCDEF"; 00256 std::string dst; 00257 for (size_t i = 0; i < src.size(); i++) 00258 { 00259 if (isalnum(src[i])) 00260 { 00261 dst += src[i]; 00262 } 00263 else 00264 if (src[i] == ' ') 00265 { 00266 dst += '+'; 00267 } 00268 else 00269 { 00270 unsigned char c = static_cast<unsigned char>(src[i]); 00271 dst += '%'; 00272 dst += hex[c / 16]; 00273 dst += hex[c % 16]; 00274 } 00275 } 00276 return dst; 00277 } // rfc1738_encode
| std::string Utility::rfc1738_decode | ( | const std::string & | src | ) | [static] |
Definition at line 284 of file Utility.cpp.
Referenced by HttpRequest::SetUri().
00285 { 00286 std::string dst; 00287 for (size_t i = 0; i < src.size(); i++) 00288 { 00289 if (src[i] == '%' && isxdigit(src[i + 1]) && isxdigit(src[i + 2])) 00290 { 00291 char c1 = src[++i]; 00292 char c2 = src[++i]; 00293 c1 = c1 - 48 - ((c1 >= 'A') ? 7 : 0) - ((c1 >= 'a') ? 32 : 0); 00294 c2 = c2 - 48 - ((c2 >= 'A') ? 7 : 0) - ((c2 >= 'a') ? 32 : 0); 00295 dst += (char)(c1 * 16 + c2); 00296 } 00297 else 00298 if (src[i] == '+') 00299 { 00300 dst += ' '; 00301 } 00302 else 00303 { 00304 dst += src[i]; 00305 } 00306 } 00307 return dst; 00308 } // rfc1738_decode
| bool Utility::isipv4 | ( | const std::string & | str | ) | [static] |
Checks whether a string is a valid ipv4/ipv6 ip number.
Definition at line 311 of file Utility.cpp.
Referenced by ResolvSocket::OnDetached(), TcpSocket::Open(), Ipv4Address::Resolve(), and u2ip().
00312 { 00313 int dots = 0; 00314 // %! ignore :port? 00315 for (size_t i = 0; i < str.size(); i++) 00316 { 00317 if (str[i] == '.') 00318 dots++; 00319 else 00320 if (!isdigit(str[i])) 00321 return false; 00322 } 00323 if (dots != 3) 00324 return false; 00325 return true; 00326 }
| bool Utility::isipv6 | ( | const std::string & | str | ) | [static] |
Checks whether a string is a valid ipv4/ipv6 ip number.
Definition at line 329 of file Utility.cpp.
References Parse::getword().
Referenced by ResolvSocket::OnDetached(), and TcpSocket::Open().
00330 { 00331 size_t qc = 0; 00332 size_t qd = 0; 00333 for (size_t i = 0; i < str.size(); i++) 00334 { 00335 qc += (str[i] == ':') ? 1 : 0; 00336 qd += (str[i] == '.') ? 1 : 0; 00337 } 00338 if (qc > 7) 00339 { 00340 return false; 00341 } 00342 if (qd && qd != 3) 00343 { 00344 return false; 00345 } 00346 Parse pa(str,":."); 00347 std::string tmp = pa.getword(); 00348 while (tmp.size()) 00349 { 00350 if (tmp.size() > 4) 00351 { 00352 return false; 00353 } 00354 for (size_t i = 0; i < tmp.size(); i++) 00355 { 00356 if (tmp[i] < '0' || (tmp[i] > '9' && tmp[i] < 'A') || 00357 (tmp[i] > 'F' && tmp[i] < 'a') || tmp[i] > 'f') 00358 { 00359 return false; 00360 } 00361 } 00362 // 00363 tmp = pa.getword(); 00364 } 00365 return true; 00366 }
| bool Utility::u2ip | ( | const std::string & | str, | |
| ipaddr_t & | l | |||
| ) | [static] |
Hostname to ip resolution ipv4, not asynchronous.
Definition at line 369 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().
00370 { 00371 struct sockaddr_in sa; 00372 bool r = Utility::u2ip(str, sa); 00373 memcpy(&l, &sa.sin_addr, sizeof(l)); 00374 return r; 00375 }
| bool Utility::u2ip | ( | const std::string & | host, | |
| struct sockaddr_in & | sa, | |||
| int | ai_flags = 0 | |||
| ) | [static] |
Definition at line 651 of file Utility.cpp.
References AI_NUMERICHOST, isipv4(), and Rnd().
00652 { 00653 memset(&sa, 0, sizeof(sa)); 00654 sa.sin_family = AF_INET; 00655 #ifdef NO_GETADDRINFO 00656 if ((ai_flags & AI_NUMERICHOST) != 0 || isipv4(host)) 00657 { 00658 Parse pa((char *)host.c_str(), "."); 00659 union { 00660 struct { 00661 unsigned char b1; 00662 unsigned char b2; 00663 unsigned char b3; 00664 unsigned char b4; 00665 } a; 00666 ipaddr_t l; 00667 } u; 00668 u.a.b1 = static_cast<unsigned char>(pa.getvalue()); 00669 u.a.b2 = static_cast<unsigned char>(pa.getvalue()); 00670 u.a.b3 = static_cast<unsigned char>(pa.getvalue()); 00671 u.a.b4 = static_cast<unsigned char>(pa.getvalue()); 00672 memcpy(&sa.sin_addr, &u.l, sizeof(sa.sin_addr)); 00673 return true; 00674 } 00675 #ifndef LINUX 00676 struct hostent *he = gethostbyname( host.c_str() ); 00677 if (!he) 00678 { 00679 return false; 00680 } 00681 memcpy(&sa.sin_addr, he -> h_addr, sizeof(sa.sin_addr)); 00682 #else 00683 struct hostent he; 00684 struct hostent *result = NULL; 00685 int myerrno = 0; 00686 char buf[2000]; 00687 int n = gethostbyname_r(host.c_str(), &he, buf, sizeof(buf), &result, &myerrno); 00688 if (n || !result) 00689 { 00690 return false; 00691 } 00692 if (he.h_addr_list && he.h_addr_list[0]) 00693 memcpy(&sa.sin_addr, he.h_addr, 4); 00694 else 00695 return false; 00696 #endif 00697 return true; 00698 #else 00699 struct addrinfo hints; 00700 memset(&hints, 0, sizeof(hints)); 00701 // AI_NUMERICHOST 00702 // AI_CANONNAME 00703 // AI_PASSIVE - server 00704 // AI_ADDRCONFIG 00705 // AI_V4MAPPED 00706 // AI_ALL 00707 // AI_NUMERICSERV 00708 hints.ai_flags = ai_flags; 00709 hints.ai_family = AF_INET; 00710 hints.ai_socktype = 0; 00711 hints.ai_protocol = 0; 00712 struct addrinfo *res; 00713 if (Utility::isipv4(host)) 00714 hints.ai_flags |= AI_NUMERICHOST; 00715 int n = getaddrinfo(host.c_str(), NULL, &hints, &res); 00716 if (!n) 00717 { 00718 std::vector<struct addrinfo *> vec; 00719 struct addrinfo *ai = res; 00720 while (ai) 00721 { 00722 if (ai -> ai_addrlen == sizeof(sa)) 00723 vec.push_back( ai ); 00724 ai = ai -> ai_next; 00725 } 00726 if (!vec.size()) 00727 return false; 00728 ai = vec[Utility::Rnd() % vec.size()]; 00729 { 00730 memcpy(&sa, ai -> ai_addr, ai -> ai_addrlen); 00731 } 00732 freeaddrinfo(res); 00733 return true; 00734 } 00735 std::string error = "Error: "; 00736 #ifndef __CYGWIN__ 00737 error += gai_strerror(n); 00738 #endif 00739 return false; 00740 #endif // NO_GETADDRINFO 00741 }
| 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 860 of file Utility.cpp.
Referenced by Ipv4Address::Convert(), l2ip(), ResolvSocket::OnDetached(), and Ipv4Address::Reverse().
00861 { 00862 std::string service; 00863 return Utility::reverse(sa, sa_len, hostname, service, flags); 00864 }
| bool Utility::reverse | ( | struct sockaddr * | sa, | |
| socklen_t | sa_len, | |||
| std::string & | hostname, | |||
| std::string & | service, | |||
| int | flags = 0 | |||
| ) | [static] |
Definition at line 867 of file Utility.cpp.
References NI_NUMERICHOST.
00868 { 00869 hostname = ""; 00870 service = ""; 00871 #ifdef NO_GETADDRINFO 00872 switch (sa -> sa_family) 00873 { 00874 case AF_INET: 00875 if (flags & NI_NUMERICHOST) 00876 { 00877 union { 00878 struct { 00879 unsigned char b1; 00880 unsigned char b2; 00881 unsigned char b3; 00882 unsigned char b4; 00883 } a; 00884 ipaddr_t l; 00885 } u; 00886 struct sockaddr_in *sa_in = (struct sockaddr_in *)sa; 00887 memcpy(&u.l, &sa_in -> sin_addr, sizeof(u.l)); 00888 char tmp[100]; 00889 sprintf(tmp, "%u.%u.%u.%u", u.a.b1, u.a.b2, u.a.b3, u.a.b4); 00890 hostname = tmp; 00891 return true; 00892 } 00893 else 00894 { 00895 struct sockaddr_in *sa_in = (struct sockaddr_in *)sa; 00896 struct hostent *h = gethostbyaddr( (const char *)&sa_in -> sin_addr, sizeof(sa_in -> sin_addr), AF_INET); 00897 if (h) 00898 { 00899 hostname = h -> h_name; 00900 return true; 00901 } 00902 } 00903 break; 00904 #ifdef ENABLE_IPV6 00905 case AF_INET6: 00906 if (flags & NI_NUMERICHOST) 00907 { 00908 char slask[100]; // l2ip temporary 00909 *slask = 0; 00910 unsigned int prev = 0; 00911 bool skipped = false; 00912 bool ok_to_skip = true; 00913 { 00914 unsigned short addr16[8]; 00915 struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6 *)sa; 00916 memcpy(addr16, &sa_in6 -> sin6_addr, sizeof(addr16)); 00917 for (size_t i = 0; i < 8; i++) 00918 { 00919 unsigned short x = ntohs(addr16[i]); 00920 if (*slask && (x || !ok_to_skip || prev)) 00921 strcat(slask,":"); 00922 if (x || !ok_to_skip) 00923 { 00924 sprintf(slask + strlen(slask),"%x", x); 00925 if (x && skipped) 00926 ok_to_skip = false; 00927 } 00928 else 00929 { 00930 skipped = true; 00931 } 00932 prev = x; 00933 } 00934 } 00935 if (!*slask) 00936 strcpy(slask, "::"); 00937 hostname = slask; 00938 return true; 00939 } 00940 else 00941 { 00942 // %! TODO: ipv6 reverse lookup 00943 struct sockaddr_in6 *sa_in = (struct sockaddr_in6 *)sa; 00944 struct hostent *h = gethostbyaddr( (const char *)&sa_in -> sin6_addr, sizeof(sa_in -> sin6_addr), AF_INET6); 00945 if (h) 00946 { 00947 hostname = h -> h_name; 00948 return true; 00949 } 00950 } 00951 break; 00952 #endif 00953 } 00954 return false; 00955 #else 00956 char host[NI_MAXHOST]; 00957 char serv[NI_MAXSERV]; 00958 // NI_NOFQDN 00959 // NI_NUMERICHOST 00960 // NI_NAMEREQD 00961 // NI_NUMERICSERV 00962 // NI_DGRAM 00963 int n = getnameinfo(sa, sa_len, host, sizeof(host), serv, sizeof(serv), flags); 00964 if (n) 00965 { 00966 // EAI_AGAIN 00967 // EAI_BADFLAGS 00968 // EAI_FAIL 00969 // EAI_FAMILY 00970 // EAI_MEMORY 00971 // EAI_NONAME 00972 // EAI_OVERFLOW 00973 // EAI_SYSTEM 00974 return false; 00975 } 00976 hostname = host; 00977 service = serv; 00978 return true; 00979 #endif // NO_GETADDRINFO 00980 }
| bool Utility::u2service | ( | const std::string & | name, | |
| int & | service, | |||
| int | ai_flags = 0 | |||
| ) | [static] |
Definition at line 983 of file Utility.cpp.
00984 { 00985 #ifdef NO_GETADDRINFO 00986 // %! 00987 return false; 00988 #else 00989 struct addrinfo hints; 00990 service = 0; 00991 memset(&hints, 0, sizeof(hints)); 00992 // AI_NUMERICHOST 00993 // AI_CANONNAME 00994 // AI_PASSIVE - server 00995 // AI_ADDRCONFIG 00996 // AI_V4MAPPED 00997 // AI_ALL 00998 // AI_NUMERICSERV 00999 hints.ai_flags = ai_flags; 01000 hints.ai_family = AF_UNSPEC; 01001 hints.ai_socktype = 0; 01002 hints.ai_protocol = 0; 01003 struct addrinfo *res; 01004 int n = getaddrinfo(NULL, name.c_str(), &hints, &res); 01005 if (!n) 01006 { 01007 service = res -> ai_protocol; 01008 freeaddrinfo(res); 01009 return true; 01010 } 01011 return false; 01012 #endif // NO_GETADDRINFO 01013 }
| void Utility::l2ip | ( | const | ipaddr_t, | |
| std::string & | str | |||
| ) | [static] |
Convert binary ip address to string: ipv4.
Definition at line 391 of file Utility.cpp.
References NI_NUMERICHOST, and reverse().
Referenced by ResolvSocket::OnConnect(), ResolvSocket::OnDetached(), TcpSocket::Open(), ResolveLocal(), and Sa2String().
00392 { 00393 struct sockaddr_in sa; 00394 memset(&sa, 0, sizeof(sa)); 00395 sa.sin_family = AF_INET; 00396 memcpy(&sa.sin_addr, &ip, sizeof(sa.sin_addr)); 00397 Utility::reverse( (struct sockaddr *)&sa, sizeof(sa), str, NI_NUMERICHOST); 00398 }
| void Utility::l2ip | ( | const in_addr & | ip, | |
| std::string & | str | |||
| ) | [static] |
Definition at line 401 of file Utility.cpp.
References NI_NUMERICHOST, and reverse().
00402 { 00403 struct sockaddr_in sa; 00404 memset(&sa, 0, sizeof(sa)); 00405 sa.sin_family = AF_INET; 00406 sa.sin_addr = ip; 00407 Utility::reverse( (struct sockaddr *)&sa, sizeof(sa), str, NI_NUMERICHOST); 00408 }
| void Utility::ResolveLocal | ( | ) | [static] |
ResolveLocal (hostname) - call once before calling any GetLocal method.
Definition at line 475 of file Utility.cpp.
References l2ip(), m_addr, m_host, m_ip, m_local_resolved, and u2ip().
Referenced by GetLocalAddress(), GetLocalHostname(), and GetLocalIP().
00476 { 00477 char h[256]; 00478 00479 // get local hostname and translate into ip-address 00480 *h = 0; 00481 gethostname(h,255); 00482 { 00483 if (Utility::u2ip(h, m_ip)) 00484 { 00485 Utility::l2ip(m_ip, m_addr); 00486 } 00487 } 00488 #ifdef ENABLE_IPV6 00489 #ifdef IPPROTO_IPV6 00490 memset(&m_local_ip6, 0, sizeof(m_local_ip6)); 00491 { 00492 if (Utility::u2ip(h, m_local_ip6)) 00493 { 00494 Utility::l2ip(m_local_ip6, m_local_addr6); 00495 } 00496 } 00497 #endif 00498 #endif 00499 m_host = h; 00500 m_local_resolved = true; 00501 }
| const std::string & Utility::GetLocalHostname | ( | ) | [static] |
Returns local hostname, ResolveLocal must be called once before using.
Definition at line 504 of file Utility.cpp.
References m_host, m_local_resolved, and ResolveLocal().
00505 { 00506 if (!m_local_resolved) 00507 { 00508 ResolveLocal(); 00509 } 00510 return m_host; 00511 }
| ipaddr_t Utility::GetLocalIP | ( | ) | [static] |
Returns local ip, ResolveLocal must be called once before using.
Definition at line 514 of file Utility.cpp.
References m_ip, m_local_resolved, and ResolveLocal().
00515 { 00516 if (!m_local_resolved) 00517 { 00518 ResolveLocal(); 00519 } 00520 return m_ip; 00521 }
| const std::string & Utility::GetLocalAddress | ( | ) | [static] |
Returns local ip number as string.
Definition at line 524 of file Utility.cpp.
References m_addr, m_local_resolved, and ResolveLocal().
00525 { 00526 if (!m_local_resolved) 00527 { 00528 ResolveLocal(); 00529 } 00530 return m_addr; 00531 }
| void Utility::SetEnv | ( | const std::string & | var, | |
| const std::string & | value | |||
| ) | [static] |
Set environment variable.
| var | Name of variable to set | |
| value | Value |
Definition at line 558 of file Utility.cpp.
Referenced by HttpdSocket::OnHeaderComplete(), and SSLInitializer::SSLInitializer().
00559 { 00560 #if (defined(SOLARIS8) || defined(SOLARIS)) 00561 { 00562 static std::map<std::string, char *> vmap; 00563 if (vmap.find(var) != vmap.end()) 00564 { 00565 delete[] vmap[var]; 00566 } 00567 vmap[var] = new char[var.size() + 1 + value.size() + 1]; 00568 sprintf(vmap[var], "%s=%s", var.c_str(), value.c_str()); 00569 putenv( vmap[var] ); 00570 } 00571 #elif defined _WIN32 00572 { 00573 std::string slask = var + "=" + value; 00574 _putenv( (char *)slask.c_str()); 00575 } 00576 #else 00577 setenv(var.c_str(), value.c_str(), 1); 00578 #endif 00579 }
| std::string Utility::Sa2String | ( | struct sockaddr * | sa | ) | [static] |
Convert sockaddr struct to human readable string.
| sa | Ptr to sockaddr struct |
Definition at line 582 of file Utility.cpp.
References l2ip(), and l2string().
Referenced by SctpSocket::getladdrs(), and SctpSocket::getpaddrs().
00583 { 00584 #ifdef ENABLE_IPV6 00585 #ifdef IPPROTO_IPV6 00586 if (sa -> sa_family == AF_INET6) 00587 { 00588 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa; 00589 std::string tmp; 00590 Utility::l2ip(sa6 -> sin6_addr, tmp); 00591 return tmp + ":" + Utility::l2string(ntohs(sa6 -> sin6_port)); 00592 } 00593 #endif 00594 #endif 00595 if (sa -> sa_family == AF_INET) 00596 { 00597 struct sockaddr_in *sa4 = (struct sockaddr_in *)sa; 00598 ipaddr_t a; 00599 memcpy(&a, &sa4 -> sin_addr, 4); 00600 std::string tmp; 00601 Utility::l2ip(a, tmp); 00602 return tmp + ":" + Utility::l2string(ntohs(sa4 -> sin_port)); 00603 } 00604 return ""; 00605 }
| void Utility::GetTime | ( | struct timeval * | p | ) | [static] |
Get current time in sec/microseconds.
Definition at line 608 of file Utility.cpp.
Referenced by UdpSocket::OnRead().
00609 { 00610 #ifdef _WIN32 00611 FILETIME ft; // Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC). 00612 GetSystemTimeAsFileTime(&ft); 00613 uint64_t tt; 00614 memcpy(&tt, &ft, sizeof(tt)); 00615 tt /= 10; // make it usecs 00616 p->tv_sec = (long)tt / 1000000; 00617 p->tv_usec = (long)tt % 1000000; 00618 #else 00619 gettimeofday(p, NULL); 00620 #endif 00621 }
| std::auto_ptr< SocketAddress > Utility::CreateAddress | ( | struct sockaddr * | sa, | |
| socklen_t | sa_len | |||
| ) | [static] |
Definition at line 624 of file Utility.cpp.
00625 { 00626 switch (sa -> sa_family) 00627 { 00628 case AF_INET: 00629 if (sa_len == sizeof(struct sockaddr_in)) 00630 { 00631 struct sockaddr_in *p = (struct sockaddr_in *)sa; 00632 return std::auto_ptr<SocketAddress>(new Ipv4Address(*p)); 00633 } 00634 break; 00635 #ifdef ENABLE_IPV6 00636 #ifdef IPPROTO_IPV6 00637 case AF_INET6: 00638 if (sa_len == sizeof(struct sockaddr_in6)) 00639 { 00640 struct sockaddr_in6 *p = (struct sockaddr_in6 *)sa; 00641 return std::auto_ptr<SocketAddress>(new Ipv6Address(*p)); 00642 } 00643 break; 00644 #endif 00645 #endif 00646 } 00647 return std::auto_ptr<SocketAddress>(NULL); 00648 }
| unsigned long Utility::ThreadID | ( | ) | [static] |
Definition at line 1016 of file Utility.cpp.
Referenced by Debug::Debug(), Debug::Print(), SSLInitializer::SSL_id_function(), and Debug::~Debug().
01017 { 01018 #ifdef _WIN32 01019 return GetCurrentThreadId(); 01020 #else 01021 return (unsigned long)pthread_self(); 01022 #endif 01023 }
| std::string Utility::ToLower | ( | const std::string & | str | ) | [static] |
Definition at line 1026 of file Utility.cpp.
Referenced by HttpBaseSocket::OnFirst(), HttpBaseSocket::OnHeader(), SmtpdSocket::OnLine(), HTTPSocket::OnLine(), Ajp13Socket::ReceiveForwardRequest(), and Utility::Uri::Uri().
01027 { 01028 std::string r; 01029 for (size_t i = 0; i < str.size(); i++) 01030 { 01031 if (str[i] >= 'A' && str[i] <= 'Z') 01032 r += str[i] | 32; 01033 else 01034 r += str[i]; 01035 } 01036 return r; 01037 }
| std::string Utility::ToUpper | ( | const std::string & | str | ) | [static] |
Definition at line 1040 of file Utility.cpp.
Referenced by SmtpdSocket::OnLine().
01041 { 01042 std::string r; 01043 for (size_t i = 0; i < str.size(); i++) 01044 { 01045 if (str[i] >= 'a' && str[i] <= 'z') 01046 r += (char)(str[i] - 32); 01047 else 01048 r += str[i]; 01049 } 01050 return r; 01051 }
| std::string Utility::ToString | ( | double | d | ) | [static] |
| unsigned long Utility::Rnd | ( | ) | [static] |
Returns a random 32-bit integer.
Definition at line 1062 of file Utility.cpp.
References Utility::Rng::Get().
Referenced by u2ip().
01063 { 01064 static Utility::Rng generator( (unsigned long)time(NULL) ); 01065 return generator.Get(); 01066 }
| const std::string Utility::Stack | ( | ) | [static] |
Definition at line 1143 of file Utility.cpp.
01144 { 01145 #if defined LINUX 01146 #define BFSIZE 255 01147 void *buffer[BFSIZE]; 01148 int n = backtrace(buffer, BFSIZE); 01149 char **res = backtrace_symbols(buffer, n); 01150 std::string tmp; 01151 for (int i = 0; i < n; i++) 01152 { 01153 std::string x = res[i]; 01154 std::string plus; 01155 std::string addr; 01156 size_t pos = x.find("("); 01157 if (pos != std::string::npos) 01158 { 01159 x = x.substr(pos + 1); // skip executable name 01160 01161 pos = x.find(")"); 01162 if (pos != std::string::npos) 01163 { 01164 addr = x.substr(pos + 1); 01165 x = x.substr(0, pos); 01166 } 01167 01168 pos = x.find("+"); 01169 if (pos != std::string::npos) 01170 { 01171 plus = x.substr(pos); 01172 x = x.substr(0, pos); 01173 } 01174 } 01175 char zz[1000]; 01176 { 01177 size_t sz = 1000; 01178 int status = 0; 01179 abi::__cxa_demangle( x.c_str(), zz, &sz, &status); 01180 01181 if (!status) 01182 { 01183 tmp += zz; 01184 tmp += plus; 01185 tmp += addr; 01186 } 01187 else 01188 { 01189 tmp += res[i]; 01190 } 01191 tmp += "\n"; 01192 } 01193 // dladdr() test 01194 if (0) 01195 { 01196 Dl_info info; 01197 int n = dladdr(buffer[i], &info); 01198 if (!n) 01199 printf("%d: dladdr() failed\n", i); 01200 else 01201 { 01202 size_t sz = 1000; 01203 int status = 0; 01204 abi::__cxa_demangle( info.dli_sname, zz, &sz, &status); 01205 01206 printf("%d: %s: %s\n", i, info.dli_fname, info.dli_sname); 01207 if (!status) 01208 printf(" %s\n", zz); 01209 } 01210 } // end of dladdr() test 01211 } // for (i) 01212 free(res); 01213 return tmp; 01214 #else 01215 return "Not available"; 01216 #endif 01217 }
| const std::string Utility::FromUtf8 | ( | const std::string & | str | ) | [static] |
Utf8 decrypt.
Definition at line 1220 of file Utility.cpp.
01221 { 01222 if (!str.size()) 01223 return ""; 01224 std::string r; 01225 for (size_t i = 0; i < str.size(); i++) 01226 { 01227 if (i < str.size() - 1 && (str[i] & 0xe0) == 0xc0 && (str[i + 1] & 0xc0) == 0x80) 01228 { 01229 int c1 = str[i] & 0x1f; 01230 int c2 = str[++i] & 0x3f; 01231 int c = (c1 << 6) + c2; 01232 r += (char)c; 01233 } 01234 else 01235 { 01236 r += str[i]; 01237 } 01238 } 01239 return r; 01240 }
const char * Utility::Logo [static] |
Definition at line 183 of file Utility.h.
Referenced by HttpdSocket::OnData(), and HttpdSocket::OnHeaderComplete().
std::string Utility::m_host [static, private] |
local hostname
Definition at line 191 of file Utility.h.
Referenced by GetLocalHostname(), and ResolveLocal().
ipaddr_t Utility::m_ip = 0 [static, private] |
local ip address
Definition at line 192 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 193 of file Utility.h.
Referenced by GetLocalAddress(), and ResolveLocal().
bool Utility::m_local_resolved = false [static, private] |