2 ** \file Ipv4Address.cpp
4 ** \author grymse@alhem.net
7 Copyright (C) 2007 Anders Hedstrom
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "Ipv4Address.h"
31 #ifdef SOCKETS_NAMESPACE
32 namespace SOCKETS_NAMESPACE
{
37 Ipv4Address::Ipv4Address(port_t port
) : m_valid(true)
39 memset(&m_addr
, 0, sizeof(m_addr
));
40 m_addr
.sin_family
= AF_INET
;
41 m_addr
.sin_port
= htons( port
);
45 Ipv4Address::Ipv4Address(ipaddr_t a
,port_t port
) : m_valid(true)
47 memset(&m_addr
, 0, sizeof(m_addr
));
48 m_addr
.sin_family
= AF_INET
;
49 m_addr
.sin_port
= htons( port
);
50 memcpy(&m_addr
.sin_addr
, &a
, sizeof(struct in_addr
));
54 Ipv4Address::Ipv4Address(struct in_addr
& a
,port_t port
) : m_valid(true)
56 memset(&m_addr
, 0, sizeof(m_addr
));
57 m_addr
.sin_family
= AF_INET
;
58 m_addr
.sin_port
= htons( port
);
63 Ipv4Address::Ipv4Address(const std::string
& host
,port_t port
) : m_valid(false)
65 memset(&m_addr
, 0, sizeof(m_addr
));
66 m_addr
.sin_family
= AF_INET
;
67 m_addr
.sin_port
= htons( port
);
70 if (Utility::u2ip(host
, a
))
72 memcpy(&m_addr
.sin_addr
, &a
, sizeof(struct in_addr
));
79 Ipv4Address::Ipv4Address(struct sockaddr_in
& sa
)
82 m_valid
= sa
.sin_family
== AF_INET
;
86 Ipv4Address::~Ipv4Address()
91 Ipv4Address::operator struct sockaddr
*()
93 return (struct sockaddr
*)&m_addr
;
97 Ipv4Address::operator socklen_t()
99 return sizeof(struct sockaddr_in
);
103 void Ipv4Address::SetPort(port_t port
)
105 m_addr
.sin_port
= htons( port
);
109 port_t
Ipv4Address::GetPort()
111 return ntohs( m_addr
.sin_port
);
115 bool Ipv4Address::Resolve(const std::string
& hostname
,struct in_addr
& a
)
117 struct sockaddr_in sa
;
118 memset(&a
, 0, sizeof(a
));
119 if (Utility::isipv4(hostname
))
121 if (!Utility::u2ip(hostname
, sa
, AI_NUMERICHOST
))
126 if (!Utility::u2ip(hostname
, sa
))
133 bool Ipv4Address::Reverse(struct in_addr
& a
,std::string
& name
)
135 struct sockaddr_in sa
;
136 memset(&sa
, 0, sizeof(sa
));
137 sa
.sin_family
= AF_INET
;
139 return Utility::reverse((struct sockaddr
*)&sa
, sizeof(sa
), name
);
143 std::string
Ipv4Address::Convert(bool include_port
)
146 return Convert(m_addr
.sin_addr
) + ":" + Utility::l2string(GetPort());
147 return Convert(m_addr
.sin_addr
);
151 std::string
Ipv4Address::Convert(struct in_addr
& a
)
153 struct sockaddr_in sa
;
154 memset(&sa
, 0, sizeof(sa
));
155 sa
.sin_family
= AF_INET
;
158 Utility::reverse((struct sockaddr
*)&sa
, sizeof(sa
), name
, NI_NUMERICHOST
);
163 void Ipv4Address::SetAddress(struct sockaddr
*sa
)
165 memcpy(&m_addr
, sa
, sizeof(struct sockaddr_in
));
169 int Ipv4Address::GetFamily()
171 return m_addr
.sin_family
;
175 bool Ipv4Address::IsValid()
181 bool Ipv4Address::operator==(SocketAddress
& a
)
183 if (a
.GetFamily() != GetFamily())
185 if ((socklen_t
)a
!= sizeof(m_addr
))
187 struct sockaddr
*sa
= a
;
188 struct sockaddr_in
*p
= (struct sockaddr_in
*)sa
;
189 if (p
-> sin_port
!= m_addr
.sin_port
)
191 if (memcmp(&p
-> sin_addr
, &m_addr
.sin_addr
, 4))
197 std::auto_ptr
<SocketAddress
> Ipv4Address::GetCopy()
199 return std::auto_ptr
<SocketAddress
>(new Ipv4Address(m_addr
));
203 std::string
Ipv4Address::Reverse()
206 Reverse(m_addr
.sin_addr
, tmp
);
211 #ifdef SOCKETS_NAMESPACE
212 } // namespace SOCKETS_NAMESPACE {