5 * Created by Victor Grishchenko on 3/9/09.
6 * Copyright 2009 Delft University of Technology. All rights reserved.
13 #include "compat/stdint.h"
15 #include "compat/unixio.h"
19 #include <arpa/inet.h>
20 #include <sys/select.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
32 #include "compat/hirestimeofday.h"
37 #define MAXDGRAMSZ 1400
39 #define INVALID_SOCKET -1
45 struct sockaddr_in addr;
46 static uint32_t LOCALHOST;
47 void init(uint32_t ipv4=0, uint16_t port=0) {
48 memset(&addr,0,sizeof(struct sockaddr_in));
49 addr.sin_family = AF_INET;
50 addr.sin_port = htons(port);
51 addr.sin_addr.s_addr = htonl(ipv4);
54 Address(const char* ip, uint16_t port) {
56 inet_aton(ip,&(addr.sin_addr));
58 Address(uint16_t port) {
61 Address(uint32_t ipv4addr, uint16_t port) {
64 Address(const struct sockaddr_in& address) : addr(address) {}
65 operator sockaddr_in () const {return addr;}
66 bool operator == (const Address& b) {
67 return addr.sin_family==b.addr.sin_family &&
68 addr.sin_port==b.addr.sin_port &&
69 addr.sin_addr.s_addr==b.addr.sin_addr.s_addr;
71 bool operator != (const Address& b) { return !(*this==b); }
77 uint8_t buf[MAXDGRAMSZ*2];
79 static SOCKET Bind(Address address);
80 static void Close(int port);
82 static SOCKET Wait (int sockcnt, SOCKET* sockets, tint usec=0);
85 Datagram (SOCKET socket, const Address addr_) : addr(addr_), offset(0),
86 length(0), sock(socket) {}
87 Datagram (SOCKET socket) : offset(0), length(0), sock(socket) {
90 int space () const { return MAXDGRAMSZ-length; }
91 int size() const { return length-offset; }
92 std::string str() const { return std::string((char*)buf+offset,size()); }
93 const uint8_t* operator * () const { return buf+offset; }
95 int Push (const uint8_t* data, int l) { // scatter-gather one day
96 int toc = l<space() ? l : space();
97 memcpy(buf+length,data,toc);
101 int Pull (uint8_t** data, int l) {
102 int toc = l<size() ? l : size();
103 //memcpy(data,buf+offset,toc);
111 const Address& address() const { return addr; }
112 void Clear() { offset=length=0; }
114 void PushString (std::string str) {
115 Push((uint8_t*)str.c_str(),str.size());
117 void Push8 (uint8_t b) {
120 void Push16 (uint16_t w) {
121 *(uint16_t*)(buf+length) = htons(w);
124 void Push32 (uint32_t i) {
125 *(uint32_t*)(buf+length) = htonl(i);
128 void Push64 (uint64_t l) {
129 *(uint32_t*)(buf+length) = htonl((uint32_t)(l>>32));
130 *(uint32_t*)(buf+length+4) = htonl((uint32_t)(l&0xffffffff));
133 void PushHash (const Sha1Hash& hash) {
134 Push(hash.bits, Sha1Hash::SIZE);
138 if (size()<1) return 0;
139 return buf[offset++];
142 if (size()<2) return 0;
144 return ntohs(*(uint16_t*)(buf+offset-2));
147 if (size()<4) return 0;
148 uint32_t i = ntohl(*(uint32_t*)(buf+offset));
153 if (size()<8) return 0;
154 uint64_t l = ntohl(*(uint32_t*)(buf+offset));
156 l |= ntohl(*(uint32_t*)(buf+offset+4));
160 Sha1Hash PullHash() {
161 if (size()<Sha1Hash::SIZE) return Sha1Hash::ZERO;
162 offset += Sha1Hash::SIZE;
163 return Sha1Hash(false,(char*)buf+offset-Sha1Hash::SIZE);
165 //std::string to_string () const ;
169 std::string sock2str (struct sockaddr_in addr);