11 #include "../../hal/socket.h" 13 #include <sys/socket.h> 14 #include <netinet/in.h> 15 #include <netinet/tcp.h> 16 #include <arpa/inet.h> 19 namespace rp{
namespace net {
33 assert(!
"should not reach here");
44 reinterpret_cast<sockaddr_storage *
>(
_platform_data)->ss_family = AF_INET;
61 reinterpret_cast<sockaddr_storage *
>(
_platform_data)->ss_family = AF_INET;
85 switch(reinterpret_cast<const sockaddr_storage *>(
_platform_data)->ss_family) {
91 assert(!
"should not reach here");
100 return (
int)ntohs(reinterpret_cast<const sockaddr_in *>(
_platform_data)->sin_port);
102 return (
int)ntohs(reinterpret_cast<const sockaddr_in6 *>(
_platform_data)->sin6_port);
112 reinterpret_cast<sockaddr_in *
>(
_platform_data)->sin_port = htons((
short)port);
115 reinterpret_cast<sockaddr_in6 *
>(
_platform_data)->sin6_port = htons((
short)port);
129 reinterpret_cast<sockaddr_storage *
>(
_platform_data)->ss_family = AF_INET;
130 ans = inet_pton(AF_INET,
138 reinterpret_cast<sockaddr_storage *
>(
_platform_data)->ss_family = AF_INET6;
139 ans = inet_pton(AF_INET6,
156 int net_family =
reinterpret_cast<const sockaddr_storage *
>(
_platform_data)->ss_family;
157 const char *ans = NULL;
158 switch (net_family) {
160 ans = inet_ntop(net_family, &reinterpret_cast<const sockaddr_in *>(
_platform_data)->sin_addr,
165 ans = inet_ntop(net_family, &reinterpret_cast<const sockaddr_in6 *>(
_platform_data)->sin6_addr,
177 struct addrinfo hints;
181 memset(&hints, 0,
sizeof(
struct addrinfo));
183 hints.ai_flags = AI_PASSIVE;
186 hints.ai_family |= AI_NUMERICSERV | AI_NUMERICHOST;
190 ans = getaddrinfo(hostname, sevicename, &hints, &result);
200 for (
struct addrinfo * cursor = result; cursor != NULL; cursor = cursor->ai_next) {
202 sockaddr_storage * storagebuffer =
new sockaddr_storage;
203 assert(
sizeof(sockaddr_storage) >= cursor->ai_addrlen);
204 memcpy(storagebuffer, cursor->ai_addr, cursor->ai_addrlen);
210 freeaddrinfo(result);
212 return addresspool.size();
222 memcpy(buffer, &reinterpret_cast<const sockaddr_in *>(
_platform_data)->sin_addr.s_addr,
sizeof(reinterpret_cast<const sockaddr_in *>(
_platform_data)->sin_addr.s_addr));
228 memcpy(buffer, reinterpret_cast<const sockaddr_in6 *>(
_platform_data)->sin6_addr.s6_addr,
sizeof(reinterpret_cast<const sockaddr_in6 *>(
_platform_data)->sin6_addr.s6_addr));
245 sockaddr_in * addrv4 =
reinterpret_cast<sockaddr_in *
>(
_platform_data);
246 addrv4->sin_family = AF_INET;
247 addrv4->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
252 sockaddr_in6 * addrv6 =
reinterpret_cast<sockaddr_in6 *
>(
_platform_data);
253 addrv6->sin6_family = AF_INET6;
254 addrv6->sin6_addr = in6addr_loopback;
269 sockaddr_in * addrv4 =
reinterpret_cast<sockaddr_in *
>(
_platform_data);
270 addrv4->sin_family = AF_INET;
271 addrv4->sin_addr.s_addr = htonl(INADDR_BROADCAST);
282 sockaddr_in * addrv4 =
reinterpret_cast<sockaddr_in *
>(
_platform_data);
283 addrv4->sin_family = AF_INET;
284 addrv4->sin_addr.s_addr = htonl(INADDR_ANY);
289 sockaddr_in6 * addrv6 =
reinterpret_cast<sockaddr_in6 *
>(
_platform_data);
290 addrv6->sin6_family = AF_INET6;
291 addrv6->sin6_addr = in6addr_any;
312 namespace rp {
namespace arch {
namespace net{
325 ::setsockopt( _socket_fd, SOL_SOCKET, SO_REUSEADDR , (
char *)&bool_true,
sizeof(bool_true) );
328 this->setTimeout(DEFAULT_SOCKET_TIMEOUT, SOCKET_DIR_BOTH);
344 const struct sockaddr * addr =
reinterpret_cast<const struct sockaddr *
>(localaddr.
getPlatformData());
346 int ans = ::bind(_socket_fd, addr,
sizeof(sockaddr_storage));
356 struct sockaddr * addr =
reinterpret_cast<struct sockaddr *
>(
const_cast<void *
>(localaddr.
getPlatformData()));
359 size_t actualsize =
sizeof(sockaddr_storage);
360 int ans = ::getsockname(_socket_fd, addr, (socklen_t*)&actualsize);
362 assert(actualsize <=
sizeof(sockaddr_storage));
363 assert(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
372 tv.tv_sec = timeout / 1000;
373 tv.tv_usec = (timeout % 1000) * 1000;
375 if (msk & SOCKET_DIR_RD) {
376 ans = ::setsockopt( _socket_fd, SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof(tv) );
380 if (msk & SOCKET_DIR_WR) {
381 ans = ::setsockopt( _socket_fd, SOL_SOCKET, SO_SNDTIMEO, &tv,
sizeof(tv) );
390 const struct sockaddr * addr =
reinterpret_cast<const struct sockaddr *
>(pairAddress.
getPlatformData());
391 int ans = ::connect(_socket_fd, addr,
sizeof(sockaddr_storage));
411 int ans = ::listen( _socket_fd, backlog);
419 addrsize =
sizeof(sockaddr_storage);
420 int pair_socket = ::accept( _socket_fd, pairAddress?reinterpret_cast<struct sockaddr *>(const_cast<void *>(pairAddress->
getPlatformData())):NULL
421 , (socklen_t*)&addrsize);
423 if (pair_socket>=0) {
432 return waitforData(timeout);
437 size_t ans = ::send( _socket_fd, buffer, len, MSG_NOSIGNAL);
438 if (ans == (
int)len) {
443 #if EWOULDBLOCK!=EAGAIN 457 size_t ans = ::recv( _socket_fd, buf, len, 0);
458 if (ans == (
size_t)-1) {
463 #if EWOULDBLOCK!=EAGAIN 480 virtual u_result recvNoWait(
void *buf,
size_t len,
size_t & recv_len)
482 size_t ans = ::recv( _socket_fd, buf, len, MSG_DONTWAIT);
483 if (ans == (
size_t)-1) {
485 if (errno == EAGAIN || errno == EWOULDBLOCK) {
502 struct sockaddr * addr =
reinterpret_cast<struct sockaddr *
>(
const_cast<void *
>(peerAddr.
getPlatformData()));
504 size_t actualsize =
sizeof(sockaddr_storage);
505 int ans = ::getpeername(_socket_fd, addr, (socklen_t*)&actualsize);
507 assert(actualsize <=
sizeof(sockaddr_storage));
508 assert(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
520 shutdw_opt = SHUT_RD;
523 shutdw_opt = SHUT_WR;
525 case SOCKET_DIR_BOTH:
527 shutdw_opt = SHUT_RDWR;
530 int ans = ::shutdown(_socket_fd, shutdw_opt);
536 int bool_true = enable?1:0;
542 int bool_true = enable?1:0;
550 FD_SET(_socket_fd, &wrset);
553 tv.tv_sec = timeout / 1000;
554 tv.tv_usec = (timeout % 1000) * 1000;
555 int ans = ::select(_socket_fd+1, NULL, &wrset, NULL, &tv);
574 FD_SET(_socket_fd, &rdset);
577 tv.tv_sec = timeout / 1000;
578 tv.tv_usec = (timeout % 1000) * 1000;
579 int ans = ::select(_socket_fd+1, &rdset, NULL, NULL, &tv);
610 ::setsockopt( _socket_fd, SOL_SOCKET, SO_REUSEADDR | SO_BROADCAST , (
char *)&bool_true,
sizeof(bool_true) );
611 setTimeout(DEFAULT_SOCKET_TIMEOUT, SOCKET_DIR_BOTH);
627 const struct sockaddr * addr =
reinterpret_cast<const struct sockaddr *
>(localaddr.
getPlatformData());
629 int ans = ::bind(_socket_fd, addr,
sizeof(sockaddr_storage));
639 struct sockaddr * addr =
reinterpret_cast<struct sockaddr *
>(
const_cast<void *
>((localaddr.
getPlatformData())));
642 size_t actualsize =
sizeof(sockaddr_storage);
643 int ans = ::getsockname(_socket_fd, addr, (socklen_t*)&actualsize);
645 assert(actualsize <=
sizeof(sockaddr_storage));
646 assert(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
655 tv.tv_sec = timeout / 1000;
656 tv.tv_usec = (timeout % 1000) * 1000;
658 if (msk & SOCKET_DIR_RD) {
659 ans = ::setsockopt( _socket_fd, SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof(tv) );
663 if (msk & SOCKET_DIR_WR) {
664 ans = ::setsockopt( _socket_fd, SOL_SOCKET, SO_SNDTIMEO, &tv,
sizeof(tv) );
676 FD_SET(_socket_fd, &wrset);
679 tv.tv_sec = timeout / 1000;
680 tv.tv_usec = (timeout % 1000) * 1000;
681 int ans = ::select(_socket_fd+1, NULL, &wrset, NULL, &tv);
700 FD_SET(_socket_fd, &rdset);
703 tv.tv_sec = timeout / 1000;
704 tv.tv_usec = (timeout % 1000) * 1000;
705 int ans = ::select(_socket_fd+1, &rdset, NULL, NULL, &tv);
722 const struct sockaddr * addr =
reinterpret_cast<const struct sockaddr *
>(target.
getPlatformData());
724 size_t ans = ::sendto( _socket_fd, buffer, len, 0, addr,
sizeof(sockaddr_storage));
725 if (ans != (
size_t)-1) {
726 assert(ans == (
int)len);
731 #if EWOULDBLOCK!=EAGAIN 749 struct sockaddr * addr = (sourceAddr?
reinterpret_cast<struct sockaddr *
>(
const_cast<void *
>(sourceAddr->
getPlatformData())):NULL);
750 size_t source_addr_size = (sourceAddr?
sizeof(sockaddr_storage):0);
752 size_t ans = ::recvfrom( _socket_fd, buf, len, 0, addr, (socklen_t*)&source_addr_size);
753 if (ans == (
size_t)-1) {
757 #if EWOULDBLOCK!=EAGAIN 773 virtual u_result recvFromNoWait(
void *buf,
size_t len,
size_t & recv_len,
SocketAddress * sourceAddr)
775 struct sockaddr * addr = (sourceAddr?
reinterpret_cast<struct sockaddr *
>(
const_cast<void *
>(sourceAddr->
getPlatformData())):NULL);
776 size_t source_addr_size = (sourceAddr?
sizeof(sockaddr_storage):0);
779 size_t ans = ::recvfrom( _socket_fd, buf, len, MSG_DONTWAIT, addr, &source_addr_size);
781 if (ans == (
size_t)-1) {
783 if (errno == EAGAIN || errno == EWOULDBLOCK) {
807 namespace rp {
namespace net{
820 assert(!
"should not reach here");
828 if (family == SOCKET_FAMILY_RAW)
return NULL;
832 int socket_fd = ::socket(socket_family, SOCK_STREAM, 0);
833 if (socket_fd == -1)
return NULL;
846 int socket_fd = ::socket(socket_family, (family==SOCKET_FAMILY_RAW)?SOCK_RAW:SOCK_DGRAM, 0);
847 if (socket_fd == -1)
return NULL;
virtual StreamSocket * accept(SocketAddress *pairAddress)
static int _socketHalFamilyToOSFamily(SocketBase::socket_family_t family)
virtual void setAnyAddress(address_type_t=ADDRESS_TYPE_INET)
virtual u_result setPort(int port)
virtual void setLoopbackAddress(address_type_t=ADDRESS_TYPE_INET)
virtual u_result connect(const SocketAddress &pairAddress)
virtual u_result setTimeout(_u32 timeout, socket_direction_mask msk)
static int _halAddrTypeToOSType(SocketAddress::address_type_t type)
virtual void setBroadcastAddressIPv4()
virtual u_result recvFrom(void *buf, size_t len, size_t &recv_len, SocketAddress *sourceAddr)
SocketAddress & operator=(const SocketAddress &)
virtual u_result recv(void *buf, size_t len, size_t &recv_len)
virtual ~StreamSocketImpl()
virtual u_result getAddressAsString(char *buffer, size_t buffersize) const
virtual u_result waitforData(_u32 timeout)
#define RESULT_INSUFFICIENT_MEMORY
static size_t LoopUpHostName(const char *hostname, const char *sevicename, std::vector< SocketAddress > &addresspool, bool performDNS=true, address_type_t=ADDRESS_TYPE_INET)
virtual u_result enableKeepAlive(bool enable)
virtual u_result waitforSent(_u32 timeout)
virtual ~DGramSocketImpl()
virtual u_result setTimeout(_u32 timeout, socket_direction_mask msk)
virtual u_result setAddressFromString(const char *address_string, address_type_t=ADDRESS_TYPE_INET)
const void * getPlatformData() const
virtual u_result waitforSent(_u32 timeout)
virtual u_result sendTo(const SocketAddress &target, const void *buffer, size_t len)
virtual u_result getLocalAddress(SocketAddress &localaddr)
virtual u_result listen(int backlog)
virtual u_result bind(const SocketAddress &localaddr)
static StreamSocket * CreateSocket(socket_family_t family=SOCKET_FAMILY_INET)
#define RESULT_OPERATION_TIMEOUT
virtual u_result getLocalAddress(SocketAddress &localaddr)
virtual u_result send(const void *buffer, size_t len)
virtual u_result waitforData(_u32 timeout)
virtual address_type_t getAddressType() const
virtual u_result enableNoDelay(bool enable)
virtual u_result getPeerAddress(SocketAddress &peerAddr)
virtual u_result shutdown(socket_direction_mask mask)
#define RESULT_OPERATION_NOT_SUPPORT
virtual u_result bind(const SocketAddress &localaddr)
#define RESULT_OPERATION_FAIL
virtual u_result getRawAddress(_u8 *buffer, size_t bufferSize) const
virtual int getPort() const
#define RESULT_INVALID_DATA
static DGramSocket * CreateSocket(socket_family_t family=SOCKET_FAMILY_INET)
virtual u_result waitforIncomingConnection(_u32 timeout)