BlinkenArea - GitList
Repositories
Blog
Wiki
Blinker
Code
Commits
Branches
Tags
Search
Tree:
3a09c3d
Branches
Tags
master
Blinker
src
linux
Udp4Addr.cpp
implemented UDP v4 address class
Stefan Schuermans
commited
3a09c3d
at 2011-11-08 19:52:05
Udp4Addr.cpp
Blame
History
Raw
/* Blinker Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org> Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html a blinkenarea.org project */ #include <arpa/inet.h> #include <netinet/in.h> #include <sstream> #include <stdlib.h> #include <string> #include <sys/socket.h> #include <sys/types.h> #include "Udp4Addr.h" namespace Blinker { /// constructor Udp4Addr::Udp4Addr() { m_addr.sin_family = AF_INET; m_addr.sin_port = htons(0); m_addr.sin_addr.s_addr = htonl(INADDR_NONE); } /// virtual destructor Udp4Addr::~Udp4Addr() { } /// return address family int Udp4Addr::getFamily() const { return AF_INET; } /** * @brief parse from string format * @param[in] str string format * @return if parsing was successful */ bool Udp4Addr::fromStr(const std::string &str) { std::string::size_type posColon; std::string strIp, strPort; struct in_addr iaIp; unsigned long iPort; const char *szPort; char *szPortEnd; // split address into IP and port posColon = str.find(':'); if (posColon == std::string::npos) return false; strIp = str.substr(0, posColon); strPort = str.substr(posColon + 1); // parse IP if (!inet_aton(strIp.c_str(), &iaIp)) return false; // parse port szPort = strPort.c_str(); iPort = strtoul(szPort, &szPortEnd, 10); if (*szPort == 0 || szPortEnd != 0) return false; m_addr.sin_family = AF_INET; m_addr.sin_port = htons(iPort); m_addr.sin_addr = iaIp; return false; } /** * @brief convert to string format * @return string format */ std::string Udp4Addr::toStr() const { std::stringstream strm; strm << inet_ntoa(m_addr.sin_addr) << ":" << ntohs(m_addr.sin_port); return strm.str(); } /** * @brief set to sockaddr * @parma[in] addr sockaddr structure to use * @return if using sockaddr was successful */ bool Udp4Addr::fromSockaddr(const struct sockaddr &addr) { if (addr.sa_family != AF_INET) return false; m_addr = *(const struct sockaddr_in *)&addr; return true; } /** * @brief convert to sockaddr * @return sockaddr structure */ const struct sockaddr & Udp4Addr::toSockaddr() const { return *(const struct sockaddr *)&m_addr; } } // namespace Blinker