7 #include "packager/file/udp_file.h" 13 #define close closesocket 17 #include <arpa/inet.h> 20 #include <sys/socket.h> 23 #define INVALID_SOCKET -1 27 #ifndef IP_MULTICAST_ALL 28 #define IP_MULTICAST_ALL 49 31 #endif // defined(OS_WIN) 35 #include "packager/base/logging.h" 36 #include "packager/file/udp_options.h" 42 bool IsIpv4MulticastAddress(
const struct in_addr& addr) {
43 return (ntohl(addr.s_addr) & 0xf0000000) == 0xe0000000;
49 :
File(file_name), socket_(INVALID_SOCKET) {}
51 UdpFile::~UdpFile() {}
54 if (socket_ != INVALID_SOCKET) {
56 socket_ = INVALID_SOCKET;
64 DCHECK_GE(length, 65535u)
65 <<
"Buffer may be too small to read entire datagram.";
67 if (socket_ == INVALID_SOCKET)
73 recvfrom(socket_, reinterpret_cast<char*>(buffer), length, 0, NULL, 0);
74 }
while ((result == -1) && (errno == EINTR));
85 if (socket_ == INVALID_SOCKET)
88 return std::numeric_limits<int64_t>::max();
107 class LibWinsockInitializer {
109 LibWinsockInitializer() {
111 error_ = WSAStartup(MAKEWORD(2, 2), &wsa_data);
114 ~LibWinsockInitializer() {
119 int error()
const {
return error_; }
124 #endif // defined(OS_WIN) 128 explicit ScopedSocket(SOCKET sock_fd) : sock_fd_(sock_fd) {}
131 if (sock_fd_ != INVALID_SOCKET)
135 SOCKET
get() {
return sock_fd_; }
138 SOCKET socket = sock_fd_;
139 sock_fd_ = INVALID_SOCKET;
146 DISALLOW_COPY_AND_ASSIGN(ScopedSocket);
151 static LibWinsockInitializer lib_winsock_initializer;
152 if (lib_winsock_initializer.error() != 0) {
153 LOG(ERROR) <<
"Winsock start up failed with error " 154 << lib_winsock_initializer.error();
157 #endif // defined(OS_WIN) 159 DCHECK_EQ(INVALID_SOCKET, socket_);
161 std::unique_ptr<UdpOptions> options =
166 ScopedSocket new_socket(socket(AF_INET, SOCK_DGRAM, 0));
167 if (new_socket.get() == INVALID_SOCKET) {
168 LOG(ERROR) <<
"Could not allocate socket.";
172 struct in_addr local_in_addr = {0};
173 if (inet_pton(AF_INET, options->address().c_str(), &local_in_addr) != 1) {
174 LOG(ERROR) <<
"Malformed IPv4 address " << options->address();
178 struct sockaddr_in local_sock_addr = {0};
180 local_sock_addr.sin_family = AF_INET;
181 local_sock_addr.sin_port = htons(options->port());
182 const bool is_multicast = IsIpv4MulticastAddress(local_in_addr);
184 local_sock_addr.sin_addr.s_addr = htonl(INADDR_ANY);
186 local_sock_addr.sin_addr = local_in_addr;
189 if (options->reuse()) {
190 const int optval = 1;
191 if (setsockopt(new_socket.get(), SOL_SOCKET, SO_REUSEADDR,
192 reinterpret_cast<const char*
>(&optval),
193 sizeof(optval)) < 0) {
195 <<
"Could not apply the SO_REUSEADDR property to the UDP socket";
200 if (bind(new_socket.get(),
201 reinterpret_cast<struct sockaddr*
>(&local_sock_addr),
202 sizeof(local_sock_addr))) {
203 LOG(ERROR) <<
"Could not bind UDP socket";
208 if (options->is_source_specific_multicast()) {
209 struct ip_mreq_source source_multicast_group;
211 source_multicast_group.imr_multiaddr = local_in_addr;
212 if (inet_pton(AF_INET,
213 options->interface_address().c_str(),
214 &source_multicast_group.imr_interface) != 1) {
215 LOG(ERROR) <<
"Malformed IPv4 interface address " 216 << options->interface_address();
219 if (inet_pton(AF_INET,
220 options->source_address().c_str(),
221 &source_multicast_group.imr_sourceaddr) != 1) {
222 LOG(ERROR) <<
"Malformed IPv4 source specific multicast address " 223 << options->source_address();
227 if (setsockopt(new_socket.get(),
229 IP_ADD_SOURCE_MEMBERSHIP,
230 reinterpret_cast<const char*
>(&source_multicast_group),
231 sizeof(source_multicast_group)) < 0) {
232 LOG(ERROR) <<
"Failed to join multicast group.";
237 struct ip_mreq multicast_group;
239 multicast_group.imr_multiaddr = local_in_addr;
241 if (inet_pton(AF_INET, options->interface_address().c_str(),
242 &multicast_group.imr_interface) != 1) {
243 LOG(ERROR) <<
"Malformed IPv4 interface address " 244 << options->interface_address();
248 if (setsockopt(new_socket.get(), IPPROTO_IP, IP_ADD_MEMBERSHIP,
249 reinterpret_cast<const char*
>(&multicast_group),
250 sizeof(multicast_group)) < 0) {
251 LOG(ERROR) <<
"Failed to join multicast group.";
257 #if defined(__linux__) 260 const int optval_zero = 0;
261 if (setsockopt(new_socket.get(), IPPROTO_IP, IP_MULTICAST_ALL,
262 reinterpret_cast<const char*
>(&optval_zero),
263 sizeof(optval_zero)) < 0 &&
264 errno != ENOPROTOOPT) {
265 LOG(ERROR) <<
"Failed to disable IP_MULTICAST_ALL option.";
268 #endif // #if defined(__linux__) 272 if (options->timeout_us() != 0) {
274 tv.tv_sec = options->timeout_us() / 1000000;
275 tv.tv_usec = options->timeout_us() % 1000000;
276 if (setsockopt(new_socket.get(), SOL_SOCKET, SO_RCVTIMEO,
277 reinterpret_cast<const char*
>(&tv),
sizeof(tv)) < 0) {
278 LOG(ERROR) <<
"Failed to set socket timeout.";
283 if (options->buffer_size() > 0) {
284 const int receive_buffer_size = options->buffer_size();
285 if (setsockopt(new_socket.get(), SOL_SOCKET, SO_RCVBUF,
286 reinterpret_cast<const char*
>(&receive_buffer_size),
287 sizeof(receive_buffer_size)) < 0) {
288 LOG(ERROR) <<
"Failed to set the maximum receive buffer size: " 294 socket_ = new_socket.release();
int64_t Read(void *buffer, uint64_t length) override
bool Tell(uint64_t *position) override
bool Seek(uint64_t position) override
UdpFile(const char *address_and_port)
static std::unique_ptr< UdpOptions > ParseFromString(base::StringPiece udp_url)
Define an abstract file interface.
int64_t Write(const void *buffer, uint64_t length) override
const std::string & file_name() const
All the methods that are virtual are virtual for mocking.
bool Open() override
Internal open. Should not be used directly.