5 * Created by Victor Grishchenko on 3/9/09.
6 * Copyright 2009 Delft University of Technology. All rights reserved.
13 #include "compat/stdint.h"
23 #include <arpa/inet.h>
24 #include <sys/select.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
36 #include "compat/util.h"
41 #define MAXDGRAMSZ 2800
43 #define INVALID_SOCKET -1
48 struct sockaddr_in addr;
49 static uint32_t LOCALHOST;
50 void set_port (uint16_t port) {
51 addr.sin_port = htons(port);
53 void set_port (const char* port_str) {
55 if (sscanf(port_str,"%i",&p))
58 void set_ipv4 (uint32_t ipv4) {
59 addr.sin_addr.s_addr = htonl(ipv4);
61 void set_ipv4 (const char* ipv4_str) {
62 inet_aton(ipv4_str,&(addr.sin_addr));
65 memset(&addr,0,sizeof(struct sockaddr_in));
66 addr.sin_family = AF_INET;
71 Address(const char* ip, uint16_t port) {
76 Address(const char* ip_port);
77 Address(uint16_t port) {
82 Address(uint32_t ipv4addr, uint16_t port) {
87 Address(const struct sockaddr_in& address) : addr(address) {}
88 uint32_t ipv4 () const { return ntohl(addr.sin_addr.s_addr); }
89 uint16_t port () const { return ntohs(addr.sin_port); }
90 operator sockaddr_in () const {return addr;}
91 bool operator == (const Address& b) const {
92 return addr.sin_family==b.addr.sin_family &&
93 addr.sin_port==b.addr.sin_port &&
94 addr.sin_addr.s_addr==b.addr.sin_addr.s_addr;
96 std::string str () const {
98 sprintf(s,"%i.%i.%i.%i:%i",ipv4()>>24,(ipv4()>>16)&0xff,
99 (ipv4()>>8)&0xff,ipv4()&0xff,port());
100 return std::string(s);
102 bool operator != (const Address& b) const { return !(*this==b); }
111 uint8_t buf[MAXDGRAMSZ*2];
113 static SOCKET Bind(Address address);
114 static void Close(int port);
116 static SOCKET Wait (int sockcnt, SOCKET* sockets, tint usec=0);
117 static tint now, epoch;
118 static uint64_t dgrams_up, dgrams_down, bytes_up, bytes_down;
120 Datagram (SOCKET socket, const Address addr_) : addr(addr_), offset(0),
121 length(0), sock(socket) {}
122 Datagram (SOCKET socket) : offset(0), length(0), sock(socket) {
125 int space () const { return MAXDGRAMSZ-length; }
126 int size() const { return length-offset; }
127 std::string str() const { return std::string((char*)buf+offset,size()); }
128 const uint8_t* operator * () const { return buf+offset; }
130 int Push (const uint8_t* data, int l) { // scatter-gather one day
131 int toc = l<space() ? l : space();
132 memcpy(buf+length,data,toc);
136 int Pull (uint8_t** data, int l) {
137 int toc = l<size() ? l : size();
138 //memcpy(data,buf+offset,toc);
146 const Address& address() const { return addr; }
147 void Clear() { offset=length=0; }
149 void PushString (std::string str) {
150 Push((uint8_t*)str.c_str(),str.size());
152 void Push8 (uint8_t b) {
155 void Push16 (uint16_t w) {
156 *(uint16_t*)(buf+length) = htons(w);
159 void Push32 (uint32_t i) {
160 *(uint32_t*)(buf+length) = htonl(i);
163 void Push64 (uint64_t l) {
164 *(uint32_t*)(buf+length) = htonl((uint32_t)(l>>32));
165 *(uint32_t*)(buf+length+4) = htonl((uint32_t)(l&0xffffffff));
168 void PushHash (const Sha1Hash& hash) {
169 Push((uint8_t*)hash.bits, Sha1Hash::SIZE);
173 if (size()<1) return 0;
174 return buf[offset++];
177 if (size()<2) return 0;
179 return ntohs(*(uint16_t*)(buf+offset-2));
182 if (size()<4) return 0;
183 uint32_t i = ntohl(*(uint32_t*)(buf+offset));
188 if (size()<8) return 0;
189 uint64_t l = ntohl(*(uint32_t*)(buf+offset));
191 l |= ntohl(*(uint32_t*)(buf+offset+4));
195 Sha1Hash PullHash() {
196 if (size()<Sha1Hash::SIZE) return Sha1Hash::ZERO;
197 offset += Sha1Hash::SIZE;
198 return Sha1Hash(false,(char*)buf+offset-Sha1Hash::SIZE);
200 //std::string to_string () const ;
204 const char* tintstr(tint t=0);
205 std::string sock2str (struct sockaddr_in addr);
206 #define dprintf(...) printf(__VA_ARGS__)
207 #define eprintf(...) fprintf(stderr,__VA_ARGS__)
208 //#define dprintf(...) {}