#else
#include <arpa/inet.h>
#endif
+#include <netdb.h>
#include "datagram.h"
#include "compat.h"
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();
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;
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;
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) {
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;
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);