small repair
[swift-upb.git] / datagram.h
1 /*
2  *  datagram.h
3  *  serp++
4  *
5  *  Created by Victor Grishchenko on 3/9/09.
6  *  Copyright 2009 Delft University of Technology. All rights reserved.
7  *
8  */
9 #ifndef DATAGRAM_H
10 #define DATAGRAM_H
11
12 #ifdef _MSC_VER
13     #include "compat/stdint.h"
14 #else
15     #include <stdint.h>
16 #endif
17 #ifdef _WIN32
18         #include <winsock2.h>
19         #include "compat.h"
20 #else
21     typedef int SOCKET;
22
23     #include <arpa/inet.h>
24     #include <sys/select.h>
25     #include <sys/socket.h>
26     #include <netinet/in.h>
27     #include <unistd.h>
28 #endif
29 #include <stdlib.h>
30 #include <fcntl.h>
31 #include <sys/stat.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <string>
35 #include "hashtree.h"
36 #include "compat/util.h"
37 #include "compat.h"
38
39
40 namespace p2tp {
41
42 #define MAXDGRAMSZ 2800
43 #ifndef _WIN32
44 #define INVALID_SOCKET -1
45 #endif
46
47
48 struct Address {
49     struct sockaddr_in  addr;
50     static uint32_t LOCALHOST;
51     void set_port (uint16_t port) {
52         addr.sin_port = htons(port);
53     }
54     void set_port (const char* port_str) {
55         int p;
56         if (sscanf(port_str,"%i",&p))
57             set_port(p);
58     }
59     void set_ipv4 (uint32_t ipv4) {
60         addr.sin_addr.s_addr = htonl(ipv4);
61     }
62     void set_ipv4 (const char* ipv4_str) {
63         inet_aton(ipv4_str,&(addr.sin_addr));
64     }
65     void clear () {
66         memset(&addr,0,sizeof(struct sockaddr_in));
67         addr.sin_family = AF_INET;
68     }
69     Address() {
70         clear();
71     }
72     Address(const char* ip, uint16_t port)  {
73         clear();
74         set_ipv4(ip);
75         set_port(port);
76     }
77     Address(const char* ip_port);
78     Address(uint16_t port) {
79         clear();
80         set_ipv4(LOCALHOST);
81         set_port(port);
82     }
83     Address(uint32_t ipv4addr, uint16_t port) {
84         clear();
85         set_ipv4(ipv4addr);
86         set_port(port);
87     }
88     Address(const struct sockaddr_in& address) : addr(address) {}
89     uint32_t ipv4 () const { return ntohl(addr.sin_addr.s_addr); }
90     uint16_t port () const { return ntohs(addr.sin_port); }
91     operator sockaddr_in () const {return addr;}
92     bool operator == (const Address& b) const {
93         return addr.sin_family==b.addr.sin_family &&
94         addr.sin_port==b.addr.sin_port &&
95         addr.sin_addr.s_addr==b.addr.sin_addr.s_addr;
96     }
97     std::string str () const {
98         char s[32];
99         sprintf(s,"%i.%i.%i.%i:%i",ipv4()>>24,(ipv4()>>16)&0xff,
100                 (ipv4()>>8)&0xff,ipv4()&0xff,port());
101         return std::string(s);
102     }
103     bool operator != (const Address& b) const { return !(*this==b); }
104 };
105
106
107 struct Datagram {
108
109         Address addr;
110         SOCKET sock;
111         int offset, length;
112         uint8_t buf[MAXDGRAMSZ*2];
113
114         static SOCKET Bind(Address address);
115         static void Close(int port);
116         static tint Time();
117         static SOCKET Wait (int sockcnt, SOCKET* sockets, tint usec=0);
118         static tint now, epoch;
119     static uint64_t dgrams_up, dgrams_down, bytes_up, bytes_down;
120
121         Datagram (SOCKET socket, const Address addr_) : addr(addr_), offset(0),
122                 length(0), sock(socket) {}
123         Datagram (SOCKET socket) : offset(0), length(0), sock(socket) {
124         }
125
126         int space () const { return MAXDGRAMSZ-length; }
127         int size() const { return length-offset; }
128         std::string str() const { return std::string((char*)buf+offset,size()); }
129     const uint8_t* operator * () const { return buf+offset; }
130
131         int Push (const uint8_t* data, int l) { // scatter-gather one day
132                 int toc = l<space() ? l : space();
133                 memcpy(buf+length,data,toc);
134                 length += toc;
135                 return toc;
136         }
137         int Pull (uint8_t** data, int l) {
138                 int toc = l<size() ? l : size();
139                 //memcpy(data,buf+offset,toc);
140                 *data = buf+offset;
141                 offset += toc;
142                 return toc;
143         }
144
145         int Send ();
146         int Recv ();
147         const Address& address() const { return addr; }
148     void Clear() { offset=length=0; }
149
150         void    PushString (std::string str) {
151                 Push((uint8_t*)str.c_str(),str.size());
152         }
153         void    Push8 (uint8_t b) {
154                 buf[length++] = b;
155         }
156         void    Push16 (uint16_t w) {
157                 *(uint16_t*)(buf+length) = htons(w);
158                 length+=2;
159         }
160         void    Push32 (uint32_t i) {
161                 *(uint32_t*)(buf+length) = htonl(i);
162                 length+=4;
163         }
164         void    Push64 (uint64_t l) {
165                 *(uint32_t*)(buf+length) = htonl((uint32_t)(l>>32));
166                 *(uint32_t*)(buf+length+4) = htonl((uint32_t)(l&0xffffffff));
167                 length+=8;
168         }
169         void    PushHash (const Sha1Hash& hash) {
170                 Push((uint8_t*)hash.bits, Sha1Hash::SIZE);
171         }
172
173         uint8_t Pull8() {
174                 if (size()<1) return 0;
175                 return buf[offset++];
176         }
177         uint16_t Pull16() {
178                 if (size()<2) return 0;
179                 offset+=2;
180                 return ntohs(*(uint16_t*)(buf+offset-2));
181         }
182         uint32_t Pull32() {
183                 if (size()<4) return 0;
184                 uint32_t i = ntohl(*(uint32_t*)(buf+offset));
185                 offset+=4;
186                 return i;
187         }
188         uint64_t Pull64() {
189                 if (size()<8) return 0;
190                 uint64_t l = ntohl(*(uint32_t*)(buf+offset));
191                 l<<=32;
192                 l |= ntohl(*(uint32_t*)(buf+offset+4));
193                 offset+=8;
194                 return l;
195         }
196         Sha1Hash PullHash() {
197                 if (size()<Sha1Hash::SIZE) return Sha1Hash::ZERO;
198                 offset += Sha1Hash::SIZE;
199                 return Sha1Hash(false,(char*)buf+offset-Sha1Hash::SIZE);
200         }
201         //std::string   to_string () const ;
202
203 };
204
205 const char* tintstr(tint t=0);
206 std::string sock2str (struct sockaddr_in addr);
207 #define dprintf(...) printf(__VA_ARGS__)
208 #define eprintf(...) fprintf(stderr,__VA_ARGS__)
209 //#define dprintf(...) {}
210
211 }
212
213 #endif