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 typedef unsigned __int64
uint64_t;
40 # include <sys/types.h>
47 #include "socket_include.h"
49 #ifdef SOCKETS_NAMESPACE
50 namespace SOCKETS_NAMESPACE
{
55 /** Conversion utilities.
60 static std::string
base64(const std::string
& str_in
);
61 static std::string
base64d(const std::string
& str_in
);
62 static std::string
l2string(long l
);
63 static std::string
bigint2string(uint64_t l
);
64 static uint64_t atoi64(const std::string
& str
);
65 static unsigned int hex2unsigned(const std::string
& str
);
66 static std::string
rfc1738_encode(const std::string
& src
);
67 static std::string
rfc1738_decode(const std::string
& src
);
69 /** Checks whether a string is a valid ipv4/ipv6 ip number. */
70 static bool isipv4(const std::string
&);
71 /** Checks whether a string is a valid ipv4/ipv6 ip number. */
72 static bool isipv6(const std::string
&);
74 /** Hostname to ip resolution ipv4, not asynchronous. */
75 static bool u2ip(const std::string
&, ipaddr_t
&);
76 static bool u2ip(const std::string
&, struct sockaddr_in
& sa
, int ai_flags
= 0);
80 /** Hostname to ip resolution ipv6, not asynchronous. */
81 static bool u2ip(const std::string
&, struct in6_addr
&);
82 static bool u2ip(const std::string
&, struct sockaddr_in6
& sa
, int ai_flags
= 0);
86 /** Reverse lookup of address to hostname */
87 static bool reverse(struct sockaddr
*sa
, socklen_t sa_len
, std::string
&, int flags
= 0);
88 static bool reverse(struct sockaddr
*sa
, socklen_t sa_len
, std::string
& hostname
, std::string
& service
, int flags
= 0);
90 static bool u2service(const std::string
& name
, int& service
, int ai_flags
= 0);
92 /** Convert binary ip address to string: ipv4. */
93 static void l2ip(const ipaddr_t
,std::string
& );
94 static void l2ip(const in_addr
&,std::string
& );
97 /** Convert binary ip address to string: ipv6. */
98 static void l2ip(const struct in6_addr
&,std::string
& ,bool mixed
= false);
100 /** ipv6 address compare. */
101 static int in6_addr_compare(in6_addr
,in6_addr
);
104 /** ResolveLocal (hostname) - call once before calling any GetLocal method. */
105 static void ResolveLocal();
106 /** Returns local hostname, ResolveLocal must be called once before using.
108 static const std::string
& GetLocalHostname();
109 /** Returns local ip, ResolveLocal must be called once before using.
111 static ipaddr_t
GetLocalIP();
112 /** Returns local ip number as string.
114 static const std::string
& GetLocalAddress();
117 /** Returns local ipv6 ip.
119 static const struct in6_addr
& GetLocalIP6();
120 /** Returns local ipv6 address.
122 static const std::string
& GetLocalAddress6();
125 /** Set environment variable.
126 \param var Name of variable to set
127 \param value Value */
128 static void SetEnv(const std::string
& var
,const std::string
& value
);
129 /** Convert sockaddr struct to human readable string.
130 \param sa Ptr to sockaddr struct */
131 static std::string
Sa2String(struct sockaddr
*sa
);
133 /** Get current time in sec/microseconds. */
134 static void GetTime(struct timeval
*);
136 static std::auto_ptr
<SocketAddress
> CreateAddress(struct sockaddr
*,socklen_t
);
138 static unsigned long ThreadID();
140 static std::string
ToLower(const std::string
& str
);
141 static std::string
ToUpper(const std::string
& str
);
143 static std::string
ToString(double d
);
146 static std::string m_host
; ///< local hostname
147 static ipaddr_t m_ip
; ///< local ip address
148 static std::string m_addr
; ///< local ip address in string format
151 static struct in6_addr m_local_ip6
; ///< local ipv6 address
153 static std::string m_local_addr6
; ///< local ipv6 address in string format
155 static bool m_local_resolved
; ///< ResolveLocal has been called if true
159 #ifdef SOCKETS_NAMESPACE
163 #endif // _SOCKETS_Utility_H