BlinkenArea - GitList
Repositories
Blog
Wiki
Blinker
Code
Commits
Branches
Tags
Search
Tree:
b189cba
Branches
Tags
master
Blinker
src
linux
Udp4Addr.cpp
implemented dynamic destinations in sender module
Stefan Schuermans
commited
b189cba
at 2011-11-15 23:54:16
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() { } /// less-than operator (to allow usage as map key) bool Udp4Addr::operator<(const Udp4Addr &that) const { if (m_addr.sin_family < that.m_addr.sin_family) return true; if (m_addr.sin_family > that.m_addr.sin_family) return false; if (ntohl(m_addr.sin_addr.s_addr) < ntohl(that.m_addr.sin_addr.s_addr)) return true; if (ntohl(m_addr.sin_addr.s_addr) > ntohl(that.m_addr.sin_addr.s_addr)) return false; if (ntohs(m_addr.sin_port) < ntohs(that.m_addr.sin_port)) return true; if (ntohs(m_addr.sin_port) > ntohs(that.m_addr.sin_port)) return false; return false; } /// 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 true; } /** * @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(); } } // namespace Blinker