7 #include "packager/file/udp_file.h"
13 #define close closesocket
14 #define EINTR_CODE WSAEINTR
18 #include <arpa/inet.h>
21 #include <sys/socket.h>
24 #define INVALID_SOCKET -1
25 #define EINTR_CODE EINTR
29 #ifndef IP_MULTICAST_ALL
30 #define IP_MULTICAST_ALL 49
37 #include "packager/base/logging.h"
38 #include "packager/file/udp_options.h"
44 bool IsIpv4MulticastAddress(
const struct in_addr& addr) {
45 return (ntohl(addr.s_addr) & 0xf0000000) == 0xe0000000;
48 int GetSocketErrorCode() {
50 return WSAGetLastError();
59 :
File(file_name), socket_(INVALID_SOCKET) {}
61 UdpFile::~UdpFile() {}
64 if (socket_ != INVALID_SOCKET) {
66 socket_ = INVALID_SOCKET;
78 DCHECK_GE(length, 65535u)
79 <<
"Buffer may be too small to read entire datagram.";
81 if (socket_ == INVALID_SOCKET)
87 recvfrom(socket_,
reinterpret_cast<char*
>(buffer), length, 0, NULL, 0);
88 }
while (result == -1 && GetSocketErrorCode() == EINTR_CODE);
99 if (socket_ == INVALID_SOCKET)
102 return std::numeric_limits<int64_t>::max();
122 explicit ScopedSocket(SOCKET sock_fd) : sock_fd_(sock_fd) {}
125 if (sock_fd_ != INVALID_SOCKET)
129 SOCKET get() {
return sock_fd_; }
132 SOCKET socket = sock_fd_;
133 sock_fd_ = INVALID_SOCKET;
140 DISALLOW_COPY_AND_ASSIGN(ScopedSocket);
146 int wsa_error = WSAStartup(MAKEWORD(2, 2), &wsa_data);
147 if (wsa_error != 0) {
148 LOG(ERROR) <<
"Winsock start up failed with error " << wsa_error;
154 DCHECK_EQ(INVALID_SOCKET, socket_);
156 std::unique_ptr<UdpOptions> options =
161 ScopedSocket new_socket(socket(AF_INET, SOCK_DGRAM, 0));
162 if (new_socket.get() == INVALID_SOCKET) {
163 LOG(ERROR) <<
"Could not allocate socket, error = " << GetSocketErrorCode();
167 struct in_addr local_in_addr = {0};
168 if (inet_pton(AF_INET, options->address().c_str(), &local_in_addr) != 1) {
169 LOG(ERROR) <<
"Malformed IPv4 address " << options->address();
173 struct sockaddr_in local_sock_addr = {0};
175 local_sock_addr.sin_family = AF_INET;
176 local_sock_addr.sin_port = htons(options->port());
177 const bool is_multicast = IsIpv4MulticastAddress(local_in_addr);
179 local_sock_addr.sin_addr.s_addr = htonl(INADDR_ANY);
181 local_sock_addr.sin_addr = local_in_addr;
184 if (options->reuse()) {
185 const int optval = 1;
186 if (setsockopt(new_socket.get(), SOL_SOCKET, SO_REUSEADDR,
187 reinterpret_cast<const char*
>(&optval),
188 sizeof(optval)) < 0) {
189 LOG(ERROR) <<
"Could not apply the SO_REUSEADDR property to the UDP "
191 << GetSocketErrorCode();
196 if (bind(new_socket.get(),
197 reinterpret_cast<struct sockaddr*
>(&local_sock_addr),
198 sizeof(local_sock_addr)) < 0) {
199 LOG(ERROR) <<
"Could not bind UDP socket, error = " << GetSocketErrorCode();
204 if (options->is_source_specific_multicast()) {
205 struct ip_mreq_source source_multicast_group;
207 source_multicast_group.imr_multiaddr = local_in_addr;
208 if (inet_pton(AF_INET,
209 options->interface_address().c_str(),
210 &source_multicast_group.imr_interface) != 1) {
211 LOG(ERROR) <<
"Malformed IPv4 interface address "
212 << options->interface_address();
215 if (inet_pton(AF_INET,
216 options->source_address().c_str(),
217 &source_multicast_group.imr_sourceaddr) != 1) {
218 LOG(ERROR) <<
"Malformed IPv4 source specific multicast address "
219 << options->source_address();
223 if (setsockopt(new_socket.get(),
225 IP_ADD_SOURCE_MEMBERSHIP,
226 reinterpret_cast<const char*
>(&source_multicast_group),
227 sizeof(source_multicast_group)) < 0) {
228 LOG(ERROR) <<
"Failed to join multicast group, error = "
229 << GetSocketErrorCode();
234 struct ip_mreq multicast_group;
236 multicast_group.imr_multiaddr = local_in_addr;
238 if (inet_pton(AF_INET, options->interface_address().c_str(),
239 &multicast_group.imr_interface) != 1) {
240 LOG(ERROR) <<
"Malformed IPv4 interface address "
241 << options->interface_address();
245 if (setsockopt(new_socket.get(), IPPROTO_IP, IP_ADD_MEMBERSHIP,
246 reinterpret_cast<const char*
>(&multicast_group),
247 sizeof(multicast_group)) < 0) {
248 LOG(ERROR) <<
"Failed to join multicast group, error = "
249 << GetSocketErrorCode();
254 #if defined(__linux__)
257 const int optval_zero = 0;
258 if (setsockopt(new_socket.get(), IPPROTO_IP, IP_MULTICAST_ALL,
259 reinterpret_cast<const char*
>(&optval_zero),
260 sizeof(optval_zero)) < 0 &&
261 GetSocketErrorCode() != ENOPROTOOPT) {
262 LOG(ERROR) <<
"Failed to disable IP_MULTICAST_ALL option, error = "
263 << GetSocketErrorCode();
270 if (options->timeout_us() != 0) {
272 tv.tv_sec = options->timeout_us() / 1000000;
273 tv.tv_usec = options->timeout_us() % 1000000;
274 if (setsockopt(new_socket.get(), SOL_SOCKET, SO_RCVTIMEO,
275 reinterpret_cast<const char*
>(&tv),
sizeof(tv)) < 0) {
276 LOG(ERROR) <<
"Failed to set socket timeout, error = "
277 << GetSocketErrorCode();
282 if (options->buffer_size() > 0) {
283 const int receive_buffer_size = options->buffer_size();
284 if (setsockopt(new_socket.get(), SOL_SOCKET, SO_RCVBUF,
285 reinterpret_cast<const char*
>(&receive_buffer_size),
286 sizeof(receive_buffer_size)) < 0) {
287 LOG(ERROR) <<
"Failed to set the maximum receive buffer size, error = "
288 << GetSocketErrorCode();
293 socket_ = new_socket.release();
Define an abstract file interface.
const std::string & file_name() const
bool Seek(uint64_t position) override
int64_t Write(const void *buffer, uint64_t length) override
bool Tell(uint64_t *position) override
int64_t Read(void *buffer, uint64_t length) override
bool Open() override
Internal open. Should not be used directly.
UdpFile(const char *address_and_port)
static std::unique_ptr< UdpOptions > ParseFromString(base::StringPiece udp_url)
All the methods that are virtual are virtual for mocking.