-
Notifications
You must be signed in to change notification settings - Fork 20
/
socket_system.hpp
169 lines (134 loc) · 4.02 KB
/
socket_system.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
utrack is a very small an efficient BitTorrent tracker
Copyright (C) 2013-2014 Arvid Norberg
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SOCKET_SYSTEM_HPP
#define SOCKET_SYSTEM_HPP
#include <array>
#include <cassert>
#ifndef _WIN32
#include <unistd.h> // for close
#include <poll.h> // for poll
#else
#include <winsock2.h>
#endif
#include "utils.hpp" // for address_eth
#include "config.hpp"
struct packet_buffer;
struct packet_socket
{
friend struct packet_buffer;
packet_socket(int listen_port, bool receive = false);
~packet_socket();
packet_socket(packet_socket&& s);
packet_socket(packet_socket const&) = delete;
void close();
bool send(packet_buffer& packets);
void local_endpoint(sockaddr_in* addr);
void add_arp_entry(sockaddr_in const* addr, address_eth const& mac) {}
// receive at least one packet on the socket. No more than num (defaults
// to 1000). For each received packet, callback is called with the following
// arguments: (sockaddr_in* from, uint8_t* buffer, int len)
// the buffer is valid until the callback returns
// returns -1 on error
template <class F>
int receive(F callback, int num = 1000)
{
if (num == 0) return 0;
sockaddr_in from;
socklen_t fromlen = sizeof(from);
// if there's no data available, try a few times in a row right away.
// if there's still no data after that, go to sleep waiting for more
int spincount = receive_spin_count;
std::array<uint64_t, 1500/8> buf;
// this loop is primarily here to be able to restart
// in the event of EINTR and also in the case of no data
// being available immediately (in which case we block in poll)
while (true)
{
fromlen = sizeof(from);
int size = recvfrom(m_socket, (char*)buf.data(), buf.size()*8, 0
, (sockaddr*)&from, &fromlen);
if (size == -1)
{
#ifdef _WIN32
int err = WSAGetLastError();
#else
int err = errno;
#endif
if (err == EINTR) continue;
#ifdef _WIN32
if (err == WSAEWOULDBLOCK)
#else
if (err == EAGAIN || errno == EWOULDBLOCK)
#endif
{
--spincount;
if (spincount > 0) continue;
// the first read came back empty, wait for new data
// on the socket and try again
pollfd e;
e.fd = m_socket;
e.events = POLLIN;
e.revents = 0;
spincount = receive_spin_count;
#ifdef _WIN32
int r = WSAPoll(&e, 1, 2000);
#else
int r = poll(&e, 1, 2000);
#endif
if (r == -1)
{
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
continue;
fprintf(stderr, "poll failed (%d): %s\n", err, strerror(err));
return -1;
}
if (r == 0)
{
// no events, see if the socket was closed
if (m_socket == -1) return -1;
continue;
}
if ((e.revents & POLLHUP) || (e.revents & POLLERR))
{
fprintf(stderr, "poll returned socket failure (%d): %s\n"
, err, strerror(err));
return -1;
}
continue;
}
fprintf(stderr, "recvfrom failed (%d): %s\n", err, strerror(err));
return -1;
}
callback(&from, (uint8_t const*)buf.data(), size);
break;
}
return 1;
}
private:
int m_socket;
bool m_receive;
};
struct packet_buffer
{
friend struct packet_socket;
explicit packet_buffer(packet_socket& s)
: m_socket(s.m_socket)
{}
bool is_full(int buf_size) const { return false; }
bool append(iovec const* v, int num, sockaddr_in const* to);
private:
int m_socket;
};
#endif // SOCKET_SYSTEM_HPP