#include "../include/sibs/IpAddress.hpp" #include namespace sibs { Ipv4::Ipv4(const char *ip, unsigned short port) { address.sin_family = AF_INET; address.sin_port = htons(port); if(ip) { if(strlen(ip) > 15) throw InvalidAddressException("Ip address is too long"); if(inet_pton(AF_INET, ip, &address.sin_addr.s_addr) != 1) { std::string errMsg = "Ip "; errMsg += ip; errMsg += " is not a valid ip"; throw InvalidAddressException(errMsg); } } else address.sin_addr.s_addr = INADDR_ANY; memset(address.sin_zero, 0, sizeof(address.sin_zero)); } std::string Ipv4::getAddress() const { std::string result; result.resize(INET_ADDRSTRLEN); inet_ntop(AF_INET, &address.sin_addr, &result[0], INET_ADDRSTRLEN); return result; } unsigned short Ipv4::getPort() const { return ntohs(address.sin_port); } }