7 #include <packager/file/udp_file.h>
11 #define close closesocket
12 #define EINTR_CODE WSAEINTR
14 #include <arpa/inet.h>
16 #include <netinet/in.h>
18 #include <sys/socket.h>
21 #define INVALID_SOCKET -1
22 #define EINTR_CODE EINTR
25 #ifndef IP_MULTICAST_ALL
26 #define IP_MULTICAST_ALL 49
32 #include <absl/log/check.h>
33 #include <absl/log/log.h>
35 #include <packager/file/udp_options.h>
36 #include <packager/macros/classes.h>
37 #include <packager/macros/compiler.h>
38 #include <packager/macros/logging.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() {}
63 bool UdpFile::Close() {
64 if (socket_ != INVALID_SOCKET) {
66 socket_ = INVALID_SOCKET;
76 int64_t UdpFile::Read(
void* buffer, uint64_t length) {
78 DCHECK_GE(length, 65535u)
79 <<
"Buffer may be too small to read entire datagram.";
81 if (socket_ == INVALID_SOCKET)
86 result = recvfrom(socket_,
reinterpret_cast<char*
>(buffer),
87 static_cast<int>(length), 0, NULL, 0);
88 }
while (result == -1 && GetSocketErrorCode() == EINTR_CODE);
93 int64_t UdpFile::Write(
const void* buffer, uint64_t length) {
96 NOTIMPLEMENTED() <<
"UdpFile is unwritable!";
100 void UdpFile::CloseForWriting() {
102 shutdown(socket_, SD_SEND);
104 shutdown(socket_, SHUT_WR);
108 int64_t UdpFile::Size() {
109 if (socket_ == INVALID_SOCKET)
112 return std::numeric_limits<int64_t>::max();
115 bool UdpFile::Flush() {
116 NOTIMPLEMENTED() <<
"UdpFile is unflushable!";
120 bool UdpFile::Seek(uint64_t position) {
122 NOTIMPLEMENTED() <<
"UdpFile is unseekable!";
126 bool UdpFile::Tell(uint64_t* position) {
128 NOTIMPLEMENTED() <<
"UdpFile is unseekable!";
134 explicit ScopedSocket(SOCKET sock_fd) : sock_fd_(sock_fd) {}
137 if (sock_fd_ != INVALID_SOCKET)
141 SOCKET get() {
return sock_fd_; }
144 SOCKET socket = sock_fd_;
145 sock_fd_ = INVALID_SOCKET;
152 DISALLOW_COPY_AND_ASSIGN(ScopedSocket);
155 bool UdpFile::Open() {
158 int wsa_error = WSAStartup(MAKEWORD(2, 2), &wsa_data);
159 if (wsa_error != 0) {
160 LOG(ERROR) <<
"Winsock start up failed with error " << wsa_error;
166 DCHECK_EQ(INVALID_SOCKET, socket_);
168 std::unique_ptr<UdpOptions> options =
173 ScopedSocket new_socket(socket(AF_INET, SOCK_DGRAM, 0));
174 if (new_socket.get() == INVALID_SOCKET) {
175 LOG(ERROR) <<
"Could not allocate socket, error = " << GetSocketErrorCode();
179 struct in_addr local_in_addr = {0};
180 if (inet_pton(AF_INET, options->address().c_str(), &local_in_addr) != 1) {
181 LOG(ERROR) <<
"Malformed IPv4 address " << options->address();
186 struct sockaddr_in local_sock_addr;
187 memset(&local_sock_addr, 0,
sizeof(local_sock_addr));
188 local_sock_addr.sin_family = AF_INET;
189 local_sock_addr.sin_port = htons(options->port());
191 const bool is_multicast = IsIpv4MulticastAddress(local_in_addr);
193 local_sock_addr.sin_addr.s_addr = htonl(INADDR_ANY);
195 local_sock_addr.sin_addr = local_in_addr;
198 if (options->reuse()) {
199 const int optval = 1;
200 if (setsockopt(new_socket.get(), SOL_SOCKET, SO_REUSEADDR,
201 reinterpret_cast<const char*
>(&optval),
202 sizeof(optval)) < 0) {
203 LOG(ERROR) <<
"Could not apply the SO_REUSEADDR property to the UDP "
205 << GetSocketErrorCode();
210 if (bind(new_socket.get(),
211 reinterpret_cast<struct sockaddr*
>(&local_sock_addr),
212 sizeof(local_sock_addr)) < 0) {
213 LOG(ERROR) <<
"Could not bind UDP socket, error = " << GetSocketErrorCode();
218 if (options->is_source_specific_multicast()) {
219 struct ip_mreq_source source_multicast_group;
221 source_multicast_group.imr_multiaddr = local_in_addr;
222 if (inet_pton(AF_INET,
223 options->interface_address().c_str(),
224 &source_multicast_group.imr_interface) != 1) {
225 LOG(ERROR) <<
"Malformed IPv4 interface address "
226 << options->interface_address();
229 if (inet_pton(AF_INET,
230 options->source_address().c_str(),
231 &source_multicast_group.imr_sourceaddr) != 1) {
232 LOG(ERROR) <<
"Malformed IPv4 source specific multicast address "
233 << options->source_address();
237 if (setsockopt(new_socket.get(),
239 IP_ADD_SOURCE_MEMBERSHIP,
240 reinterpret_cast<const char*
>(&source_multicast_group),
241 sizeof(source_multicast_group)) < 0) {
242 LOG(ERROR) <<
"Failed to join multicast group, error = "
243 << GetSocketErrorCode();
248 struct ip_mreq multicast_group;
250 multicast_group.imr_multiaddr = local_in_addr;
252 if (inet_pton(AF_INET, options->interface_address().c_str(),
253 &multicast_group.imr_interface) != 1) {
254 LOG(ERROR) <<
"Malformed IPv4 interface address "
255 << options->interface_address();
259 if (setsockopt(new_socket.get(), IPPROTO_IP, IP_ADD_MEMBERSHIP,
260 reinterpret_cast<const char*
>(&multicast_group),
261 sizeof(multicast_group)) < 0) {
262 LOG(ERROR) <<
"Failed to join multicast group, error = "
263 << GetSocketErrorCode();
268 #if defined(__linux__)
271 const int optval_zero = 0;
272 if (setsockopt(new_socket.get(), IPPROTO_IP, IP_MULTICAST_ALL,
273 reinterpret_cast<const char*
>(&optval_zero),
274 sizeof(optval_zero)) < 0 &&
275 GetSocketErrorCode() != ENOPROTOOPT) {
276 LOG(ERROR) <<
"Failed to disable IP_MULTICAST_ALL option, error = "
277 << GetSocketErrorCode();
284 if (options->timeout_us() != 0) {
286 tv.tv_sec = options->timeout_us() / 1000000;
287 tv.tv_usec = options->timeout_us() % 1000000;
288 if (setsockopt(new_socket.get(), SOL_SOCKET, SO_RCVTIMEO,
289 reinterpret_cast<const char*
>(&tv),
sizeof(tv)) < 0) {
290 LOG(ERROR) <<
"Failed to set socket timeout, error = "
291 << GetSocketErrorCode();
296 if (options->buffer_size() > 0) {
297 const int receive_buffer_size = options->buffer_size();
298 if (setsockopt(new_socket.get(), SOL_SOCKET, SO_RCVBUF,
299 reinterpret_cast<const char*
>(&receive_buffer_size),
300 sizeof(receive_buffer_size)) < 0) {
301 LOG(ERROR) <<
"Failed to set the maximum receive buffer size, error = "
302 << GetSocketErrorCode();
307 socket_ = new_socket.release();
UdpFile(const char *address_and_port)
static std::unique_ptr< UdpOptions > ParseFromString(std::string_view udp_url)
All the methods that are virtual are virtual for mocking.