#include "net/socket/socket_options.h"
#include <cerrno>
#include "build/build_config.h"
#include "net/base/net_errors.h"
#if BUILDFLAG(IS_WIN)
#include <winsock2.h>
#include <ws2tcpip.h>
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#endif
namespace net {
int SetTCPNoDelay(SocketDescriptor fd, bool no_delay) {
#if BUILDFLAG(IS_WIN)
BOOL on = no_delay ? TRUE : FALSE;
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
int on = no_delay ? 1 : 0;
#endif
int rv = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
reinterpret_cast<const char*>(&on), sizeof(on));
return rv == -1 ? MapSystemError(errno) : OK;
}
int SetReuseAddr(SocketDescriptor fd, bool reuse) {
#if BUILDFLAG(IS_WIN)
BOOL boolean_value = reuse ? TRUE : FALSE;
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
int boolean_value = reuse ? 1 : 0;
#endif
int rv = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
reinterpret_cast<const char*>(&boolean_value),
sizeof(boolean_value));
return rv == -1 ? MapSystemError(errno) : OK;
}
int SetSocketReceiveBufferSize(SocketDescriptor fd, int32_t size) {
int rv = setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
reinterpret_cast<const char*>(&size), sizeof(size));
#if BUILDFLAG(IS_WIN)
int os_error = WSAGetLastError();
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
int os_error = errno;
#endif
int net_error = (rv == -1) ? MapSystemError(os_error) : OK;
if (net_error != OK) {
DLOG(ERROR) << "Could not set socket receive buffer size: " << net_error;
}
return net_error;
}
int SetSocketSendBufferSize(SocketDescriptor fd, int32_t size) {
int rv = setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
reinterpret_cast<const char*>(&size), sizeof(size));
#if BUILDFLAG(IS_WIN)
int os_error = WSAGetLastError();
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
int os_error = errno;
#endif
int net_error = (rv == -1) ? MapSystemError(os_error) : OK;
if (net_error != OK) {
DLOG(ERROR) << "Could not set socket send buffer size: " << net_error;
}
return net_error;
}
int SetIPv6Only(SocketDescriptor fd, bool ipv6_only) {
#if BUILDFLAG(IS_WIN)
DWORD on = ipv6_only ? 1 : 0;
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
int on = ipv6_only ? 1 : 0;
#endif
int rv = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
reinterpret_cast<const char*>(&on), sizeof(on));
return rv == -1 ? MapSystemError(errno) : OK;
}
}