From: victor Date: Tue, 17 Nov 2009 18:33:55 +0000 (+0000) Subject: DNS lookups X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?a=commitdiff_plain;h=d64ec5dd11e5ad07abbcae9c347f297d3336a675;p=swift-upb.git DNS lookups git-svn-id: https://ttuki.vtt.fi/svn/p2p-next/TUD/p2tp/trunk@575 e16421f0-f15b-0410-abcd-98678b794739 --- diff --git a/datagram.cpp b/datagram.cpp index d549134..bcb4fe5 100644 --- a/datagram.cpp +++ b/datagram.cpp @@ -14,6 +14,7 @@ #else #include #endif +#include #include "datagram.h" #include "compat.h" @@ -48,6 +49,16 @@ const char* tintstr (tint time) { return ret_str[i]; } +void Address::set_ipv4 (const char* ip_str) { + struct hostent *h = gethostbyname(ip_str); + if (h == NULL) { + print_error("cannot lookup address"); + return; + } else { + addr.sin_addr.s_addr = *(u_long *) h->h_addr_list[0]; + } +} + Address::Address(const char* ip_port) { clear(); @@ -137,7 +148,7 @@ tint Datagram::Time () { SOCKET Datagram::Bind (Address addr_) { struct sockaddr_in addr = addr_; SOCKET fd; - int len = sizeof(struct sockaddr_in), sndbuf=1<<20, rcvbuf=1<<20; + int len = sizeof(struct sockaddr_in), sndbuf=1<<20, rcvbuf=1<<20, enable=1; if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { print_error("socket() fails"); return INVALID_SOCKET; @@ -153,6 +164,7 @@ SOCKET Datagram::Bind (Address addr_) { print_error("setsockopt2 fails"); return INVALID_SOCKET; } + setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&enable, sizeof(int)); #else if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) return INVALID_SOCKET; @@ -164,6 +176,7 @@ SOCKET Datagram::Bind (Address addr_) { print_error("setsockopt2 fails"); return INVALID_SOCKET; } + setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)); #endif dprintf("socket buffers: %i send %i recv\n",sndbuf,rcvbuf); if (::bind(fd, (sockaddr*)&addr, len) != 0) { diff --git a/datagram.h b/datagram.h index 2293ea7..db7c9f7 100644 --- a/datagram.h +++ b/datagram.h @@ -59,9 +59,8 @@ struct Address { void set_ipv4 (uint32_t ipv4) { addr.sin_addr.s_addr = htonl(ipv4); } - void set_ipv4 (const char* ipv4_str) { - inet_aton(ipv4_str,&(addr.sin_addr)); - } + void set_ipv4 (const char* ipv4_str) ; + //{ inet_aton(ipv4_str,&(addr.sin_addr)); } void clear () { memset(&addr,0,sizeof(struct sockaddr_in)); addr.sin_family = AF_INET; diff --git a/tests/dgramtest.cpp b/tests/dgramtest.cpp index 24822a0..dea04c0 100644 --- a/tests/dgramtest.cpp +++ b/tests/dgramtest.cpp @@ -13,6 +13,12 @@ using namespace p2tp; +TEST(Datagram, AddressTest) { + Address addr("127.0.0.1:1000"); + EXPECT_EQ(INADDR_LOOPBACK,addr.ipv4()); + EXPECT_EQ(1000,addr.port()); +} + TEST(Datagram, BinaryTest) { SOCKET socket = Datagram::Bind(7001);