Logo
~Sockets~
~Examples~
~Contact~


Utility.h

Go to the documentation of this file.
00001 
00005 /*
00006 Copyright (C) 2004-2007  Anders Hedstrom
00007 
00008 This library is made available under the terms of the GNU GPL.
00009 
00010 If you would like to use this library in a closed-source application,
00011 a separate license agreement is available. For information about 
00012 the closed-source license agreement for the C++ sockets library,
00013 please visit http://www.alhem.net/Sockets/license.html and/or
00014 email license@alhem.net.
00015 
00016 This program is free software; you can redistribute it and/or
00017 modify it under the terms of the GNU General Public License
00018 as published by the Free Software Foundation; either version 2
00019 of the License, or (at your option) any later version.
00020 
00021 This program is distributed in the hope that it will be useful,
00022 but WITHOUT ANY WARRANTY; without even the implied warranty of
00023 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024 GNU General Public License for more details.
00025 
00026 You should have received a copy of the GNU General Public License
00027 along with this program; if not, write to the Free Software
00028 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00029 */
00030 #ifndef _SOCKETS_Utility_H
00031 #define _SOCKETS_Utility_H
00032 
00033 #include "sockets-config.h"
00034 #include <ctype.h>
00035 #ifdef _WIN32
00036 typedef unsigned __int64 uint64_t;
00037 #else
00038 #include <stdlib.h>
00039 #ifdef SOLARIS
00040 # include <sys/types.h>
00041 #else
00042 # include <stdint.h>
00043 #endif
00044 #endif
00045 #include <memory>
00046 #include "Base64.h"
00047 #include "socket_include.h"
00048 
00049 #ifdef SOCKETS_NAMESPACE
00050 namespace SOCKETS_NAMESPACE {
00051 #endif
00052 
00053 class SocketAddress;
00054 
00057 class Utility
00058 {
00059 public:
00060         static std::string base64(const std::string& str_in);
00061         static std::string base64d(const std::string& str_in);
00062         static std::string l2string(long l);
00063         static std::string bigint2string(uint64_t l);
00064         static uint64_t atoi64(const std::string& str);
00065         static unsigned int hex2unsigned(const std::string& str);
00066         static std::string rfc1738_encode(const std::string& src);
00067         static std::string rfc1738_decode(const std::string& src);
00068 
00070         static bool isipv4(const std::string&);
00072         static bool isipv6(const std::string&);
00073 
00075         static bool u2ip(const std::string&, ipaddr_t&);
00076         static bool u2ip(const std::string&, struct sockaddr_in& sa, int ai_flags = 0);
00077 
00078 #ifdef ENABLE_IPV6
00079 #ifdef IPPROTO_IPV6
00080 
00081         static bool u2ip(const std::string&, struct in6_addr&);
00082         static bool u2ip(const std::string&, struct sockaddr_in6& sa, int ai_flags = 0);
00083 #endif
00084 #endif
00085 
00087         static bool reverse(struct sockaddr *sa, socklen_t sa_len, std::string&, int flags = 0);
00088         static bool reverse(struct sockaddr *sa, socklen_t sa_len, std::string& hostname, std::string& service, int flags = 0);
00089 
00090         static bool u2service(const std::string& name, int& service, int ai_flags = 0);
00091 
00093         static void l2ip(const ipaddr_t,std::string& );
00094         static void l2ip(const in_addr&,std::string& );
00095 #ifdef ENABLE_IPV6
00096 #ifdef IPPROTO_IPV6
00097 
00098         static void l2ip(const struct in6_addr&,std::string& ,bool mixed = false);
00099 
00101         static int in6_addr_compare(in6_addr,in6_addr);
00102 #endif
00103 #endif
00104 
00105         static void ResolveLocal();
00108         static const std::string& GetLocalHostname();
00111         static ipaddr_t GetLocalIP();
00114         static const std::string& GetLocalAddress();
00115 #ifdef ENABLE_IPV6
00116 #ifdef IPPROTO_IPV6
00117 
00119         static const struct in6_addr& GetLocalIP6();
00122         static const std::string& GetLocalAddress6();
00123 #endif
00124 #endif
00125 
00128         static void SetEnv(const std::string& var,const std::string& value);
00131         static std::string Sa2String(struct sockaddr *sa);
00132 
00134         static void GetTime(struct timeval *);
00135 
00136         static std::auto_ptr<SocketAddress> CreateAddress(struct sockaddr *,socklen_t);
00137 
00138         static unsigned long ThreadID();
00139 
00140         static std::string ToLower(const std::string& str);
00141         static std::string ToUpper(const std::string& str);
00142 
00143         static std::string ToString(double d);
00144 
00145 private:
00146         static std::string m_host; 
00147         static ipaddr_t m_ip; 
00148         static std::string m_addr; 
00149 #ifdef ENABLE_IPV6
00150 #ifdef IPPROTO_IPV6
00151         static struct in6_addr m_local_ip6; 
00152 #endif
00153         static std::string m_local_addr6; 
00154 #endif
00155         static bool m_local_resolved; 
00156 };
00157 
00158 
00159 #ifdef SOCKETS_NAMESPACE
00160 }
00161 #endif
00162 
00163 #endif // _SOCKETS_Utility_H
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4