3 ** \author grymse@alhem.net
6 Copyright (C) 2004-2007 Anders Hedstrom
8 This library is made available under the terms of the GNU GPL.
10 If you would like to use this library in a closed-source application,
11 a separate license agreement is available. For information about
12 the closed-source license agreement for the C++ sockets library,
13 please visit http://www.alhem.net/Sockets/license.html and/or
14 email license@alhem.net.
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public License
18 as published by the Free Software Foundation; either version 2
19 of the License, or (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #ifndef _SOCKETS_Utility_H
31 #define _SOCKETS_Utility_H
33 #include "sockets-config.h"
36 #include "socket_include.h"
38 #ifdef SOCKETS_NAMESPACE
39 namespace SOCKETS_NAMESPACE
{
44 /** Conversion utilities.
49 static std::string
base64(const std::string
& str_in
);
50 static std::string
base64d(const std::string
& str_in
);
51 static std::string
l2string(long l
);
52 static std::string
bigint2string(uint64_t l
);
53 static uint64_t atoi64(const std::string
& str
);
54 static unsigned int hex2unsigned(const std::string
& str
);
55 static std::string
rfc1738_encode(const std::string
& src
);
56 static std::string
rfc1738_decode(const std::string
& src
);
58 /** Checks whether a string is a valid ipv4/ipv6 ip number. */
59 static bool isipv4(const std::string
&);
60 /** Checks whether a string is a valid ipv4/ipv6 ip number. */
61 static bool isipv6(const std::string
&);
63 /** Hostname to ip resolution ipv4, not asynchronous. */
64 static bool u2ip(const std::string
&, ipaddr_t
&);
65 static bool u2ip(const std::string
&, struct sockaddr_in
& sa
, int ai_flags
= 0);
69 /** Hostname to ip resolution ipv6, not asynchronous. */
70 static bool u2ip(const std::string
&, struct in6_addr
&);
71 static bool u2ip(const std::string
&, struct sockaddr_in6
& sa
, int ai_flags
= 0);
75 /** Reverse lookup of address to hostname */
76 static bool reverse(struct sockaddr
*sa
, socklen_t sa_len
, std::string
&, int flags
= 0);
77 static bool reverse(struct sockaddr
*sa
, socklen_t sa_len
, std::string
& hostname
, std::string
& service
, int flags
= 0);
79 static bool u2service(const std::string
& name
, int& service
, int ai_flags
= 0);
81 /** Convert binary ip address to string: ipv4. */
82 static void l2ip(const ipaddr_t
,std::string
& );
83 static void l2ip(const in_addr
&,std::string
& );
86 /** Convert binary ip address to string: ipv6. */
87 static void l2ip(const struct in6_addr
&,std::string
& ,bool mixed
= false);
89 /** ipv6 address compare. */
90 static int in6_addr_compare(in6_addr
,in6_addr
);
93 /** ResolveLocal (hostname) - call once before calling any GetLocal method. */
94 static void ResolveLocal();
95 /** Returns local hostname, ResolveLocal must be called once before using.
97 static const std::string
& GetLocalHostname();
98 /** Returns local ip, ResolveLocal must be called once before using.
100 static ipaddr_t
GetLocalIP();
101 /** Returns local ip number as string.
103 static const std::string
& GetLocalAddress();
106 /** Returns local ipv6 ip.
108 static const struct in6_addr
& GetLocalIP6();
109 /** Returns local ipv6 address.
111 static const std::string
& GetLocalAddress6();
114 /** Set environment variable.
115 \param var Name of variable to set
116 \param value Value */
117 static void SetEnv(const std::string
& var
,const std::string
& value
);
118 /** Convert sockaddr struct to human readable string.
119 \param sa Ptr to sockaddr struct */
120 static std::string
Sa2String(struct sockaddr
*sa
);
122 /** Get current time in sec/microseconds. */
123 static void GetTime(struct timeval
*);
125 static std::auto_ptr
<SocketAddress
> CreateAddress(struct sockaddr
*,socklen_t
);
127 static unsigned long ThreadID();
129 static std::string
ToLower(const std::string
& str
);
130 static std::string
ToUpper(const std::string
& str
);
132 static std::string
ToString(double d
);
135 static std::string m_host
; ///< local hostname
136 static ipaddr_t m_ip
; ///< local ip address
137 static std::string m_addr
; ///< local ip address in string format
140 static struct in6_addr m_local_ip6
; ///< local ipv6 address
142 static std::string m_local_addr6
; ///< local ipv6 address in string format
144 static bool m_local_resolved
; ///< ResolveLocal has been called if true
148 #ifdef SOCKETS_NAMESPACE
152 #endif // _SOCKETS_Utility_H