summaryrefslogtreecommitdiffstats
path: root/src/Net/IPAddress.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/Net/IPAddress.h')
-rw-r--r--src/Net/IPAddress.h73
1 files changed, 9 insertions, 64 deletions
diff --git a/src/Net/IPAddress.h b/src/Net/IPAddress.h
index 78b41dd..1d6140f 100644
--- a/src/Net/IPAddress.h
+++ b/src/Net/IPAddress.h
@@ -20,10 +20,11 @@
#ifndef MAD_NET_IPADDRESS_H_
#define MAD_NET_IPADDRESS_H_
+#include "InvalidAddressException.h"
+
#include <string>
-#include <cstdlib>
#include <arpa/inet.h>
-#include "InvalidAddressException.h"
+#include <stdint.h>
namespace Mad {
namespace Net {
@@ -36,72 +37,16 @@ class IPAddress {
public:
// TODO Default port
- IPAddress(uint16_t port0 = 6666) : addr(INADDR_ANY), port(port0) {
- sa.sin_family = AF_INET;
- sa.sin_port = htons(port);
- sa.sin_addr.s_addr = INADDR_ANY;
- }
-
- IPAddress(uint32_t address, uint16_t port0) : addr(address), port(port0) {
- sa.sin_family = AF_INET;
- sa.sin_port = htons(port);
- sa.sin_addr.s_addr = htonl(addr);
- }
-
- IPAddress(const std::string &address) throw(InvalidAddressException) {
- std::string ip;
- size_t pos = address.find_first_of(':');
-
- if(pos == std::string::npos) {
- ip = address;
- // TODO Default port
- port = 6666;
- }
- else {
- ip = address.substr(0, pos);
-
- char *endptr;
- port = std::strtol(address.substr(pos+1).c_str(), &endptr, 10);
- if(*endptr != 0 || port == 0)
- throw InvalidAddressException(address);
- }
-
- sa.sin_family = AF_INET;
- sa.sin_port = htons(port);
-
- if(ip == "*")
- sa.sin_addr.s_addr = INADDR_ANY;
- else if(!inet_pton(AF_INET, ip.c_str(), &sa.sin_addr))
- throw InvalidAddressException(address);
-
- addr = ntohl(sa.sin_addr.s_addr);
- }
-
- IPAddress(const std::string &address, uint16_t port0) throw(InvalidAddressException) : port(port0) {
- sa.sin_family = AF_INET;
- sa.sin_port = htons(port);
-
- if(!inet_pton(AF_INET, address.c_str(), &sa.sin_addr))
- throw InvalidAddressException(address);
-
- addr = ntohl(sa.sin_addr.s_addr);
- }
-
- IPAddress(const struct sockaddr_in &address) : sa(address) {
- port = ntohs(sa.sin_port);
- addr = ntohl(sa.sin_addr.s_addr);
- }
+ IPAddress(uint16_t port0 = 6666);
+ IPAddress(uint32_t address, uint16_t port0);
+ IPAddress(const std::string &address) throw(InvalidAddressException);
+ IPAddress(const std::string &address, uint16_t port0) throw(InvalidAddressException);
+ IPAddress(const struct sockaddr_in &address);
uint32_t getAddress() const {return addr;}
uint16_t getPort() const {return port;}
- std::string getAddressString() const {
- char buf[INET_ADDRSTRLEN];
- uint32_t address = htonl(addr);
-
- inet_ntop(AF_INET, &address, buf, sizeof(buf));
- return std::string(buf);
- }
+ std::string getAddressString() const;
struct sockaddr* getSockAddr() {return (struct sockaddr*)&sa;}
socklen_t getSockAddrLength() const {return sizeof(sa);}