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.
24 #include "Ipv4Address.h"
32 #ifdef SOCKETS_NAMESPACE
33 namespace SOCKETS_NAMESPACE
{
38 Ipv4Address::Ipv4Address(port_t port
) : m_valid(true)
40 memset(&m_addr
, 0, sizeof(m_addr
));
41 m_addr
.sin_family
= AF_INET
;
42 m_addr
.sin_port
= htons( port
);
46 Ipv4Address::Ipv4Address(ipaddr_t a
,port_t port
) : m_valid(true)
48 memset(&m_addr
, 0, sizeof(m_addr
));
49 m_addr
.sin_family
= AF_INET
;
50 m_addr
.sin_port
= htons( port
);
51 memcpy(&m_addr
.sin_addr
, &a
, sizeof(struct in_addr
));
55 Ipv4Address::Ipv4Address(struct in_addr
& a
,port_t port
) : m_valid(true)
57 memset(&m_addr
, 0, sizeof(m_addr
));
58 m_addr
.sin_family
= AF_INET
;
59 m_addr
.sin_port
= htons( port
);
64 Ipv4Address::Ipv4Address(const std::string
& host
,port_t port
) : m_valid(false)
66 memset(&m_addr
, 0, sizeof(m_addr
));
67 m_addr
.sin_family
= AF_INET
;
68 m_addr
.sin_port
= htons( port
);
71 if (Utility::u2ip(host
, a
))
73 memcpy(&m_addr
.sin_addr
, &a
, sizeof(struct in_addr
));
80 Ipv4Address::Ipv4Address(struct sockaddr_in
& sa
)
83 m_valid
= sa
.sin_family
== AF_INET
;
87 Ipv4Address::~Ipv4Address()
92 Ipv4Address::operator struct sockaddr
*()
94 return (struct sockaddr
*)&m_addr
;
98 Ipv4Address::operator socklen_t()
100 return sizeof(struct sockaddr_in
);
104 void Ipv4Address::SetPort(port_t port
)
106 m_addr
.sin_port
= htons( port
);
110 port_t
Ipv4Address::GetPort()
112 return ntohs( m_addr
.sin_port
);
116 bool Ipv4Address::Resolve(const std::string
& hostname
,struct in_addr
& a
)
118 struct sockaddr_in sa
;
119 memset(&a
, 0, sizeof(a
));
120 if (Utility::isipv4(hostname
))
122 if (!Utility::u2ip(hostname
, sa
, AI_NUMERICHOST
))
127 if (!Utility::u2ip(hostname
, sa
))
134 bool Ipv4Address::Reverse(struct in_addr
& a
,std::string
& name
)
136 struct sockaddr_in sa
;
137 memset(&sa
, 0, sizeof(sa
));
138 sa
.sin_family
= AF_INET
;
140 return Utility::reverse((struct sockaddr
*)&sa
, sizeof(sa
), name
);
144 std::string
Ipv4Address::Convert(bool include_port
)
147 return Convert(m_addr
.sin_addr
) + ":" + Utility::l2string(GetPort());
148 return Convert(m_addr
.sin_addr
);
152 std::string
Ipv4Address::Convert(struct in_addr
& a
)
154 struct sockaddr_in sa
;
155 memset(&sa
, 0, sizeof(sa
));
156 sa
.sin_family
= AF_INET
;
159 Utility::reverse((struct sockaddr
*)&sa
, sizeof(sa
), name
, NI_NUMERICHOST
);
164 void Ipv4Address::SetAddress(struct sockaddr
*sa
)
166 memcpy(&m_addr
, sa
, sizeof(struct sockaddr_in
));
170 int Ipv4Address::GetFamily()
172 return m_addr
.sin_family
;
176 bool Ipv4Address::IsValid()
182 bool Ipv4Address::operator==(SocketAddress
& a
)
184 if (a
.GetFamily() != GetFamily())
186 if ((socklen_t
)a
!= sizeof(m_addr
))
188 struct sockaddr
*sa
= a
;
189 struct sockaddr_in
*p
= (struct sockaddr_in
*)sa
;
190 if (p
-> sin_port
!= m_addr
.sin_port
)
192 if (memcmp(&p
-> sin_addr
, &m_addr
.sin_addr
, 4))
198 std::auto_ptr
<SocketAddress
> Ipv4Address::GetCopy()
200 return std::auto_ptr
<SocketAddress
>(new Ipv4Address(m_addr
));
204 std::string
Ipv4Address::Reverse()
207 Reverse(m_addr
.sin_addr
, tmp
);
212 #ifdef SOCKETS_NAMESPACE
213 } // namespace SOCKETS_NAMESPACE {