12 #include "..\..\hal\socket.h" 19 #pragma comment (lib, "Ws2_32.lib") 21 namespace rp{
namespace net {
28 if (!_isWSAStartupCalled) {
29 iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
33 _isWSAStartupCalled =
true;
38 static const char*
_inet_ntop(
int af,
const void* src,
char* dst,
int cnt){
40 struct sockaddr_storage srcaddr;
45 memset(&srcaddr, 0,
sizeof(
struct sockaddr_storage));
48 srcaddr.ss_family = af;
53 struct sockaddr_in * ipv4 =
reinterpret_cast< struct sockaddr_in *
>(&srcaddr);
54 memcpy(&(ipv4->sin_addr), src,
sizeof(ipv4->sin_addr));
59 struct sockaddr_in6 * ipv6 =
reinterpret_cast< struct sockaddr_in6 *
>(&srcaddr);
60 memcpy(&(ipv6->sin6_addr), src,
sizeof(ipv6->sin6_addr));
65 if (WSAAddressToStringA((
struct sockaddr*) &srcaddr,
sizeof(
struct sockaddr_storage), 0, dst, (LPDWORD) &cnt) != 0) {
66 DWORD rv = WSAGetLastError();
72 static int _inet_pton(
int Family,
const char * pszAddrString,
void* pAddrBuf)
74 struct sockaddr_storage tmpholder;
75 int actualSize =
sizeof(sockaddr_storage);
77 int result = WSAStringToAddressA((
char *)pszAddrString, Family, NULL, (sockaddr*)&tmpholder, &actualSize);
78 if (result)
return -1;
83 struct sockaddr_in * ipv4 =
reinterpret_cast< struct sockaddr_in *
>(&tmpholder);
84 memcpy(pAddrBuf, &(ipv4->sin_addr),
sizeof(ipv4->sin_addr));
89 struct sockaddr_in6 * ipv6 =
reinterpret_cast< struct sockaddr_in6 *
>(&tmpholder);
90 memcpy(pAddrBuf, &(ipv6->sin6_addr),
sizeof(ipv6->sin6_addr));
108 assert(!
"should not reach here");
120 reinterpret_cast<sockaddr_storage *
>(
_platform_data)->ss_family = AF_INET;
138 reinterpret_cast<sockaddr_storage *
>(
_platform_data)->ss_family = AF_INET;
162 switch(reinterpret_cast<const sockaddr_storage *>(
_platform_data)->ss_family) {
168 assert(!
"should not reach here");
177 return (
int)ntohs(reinterpret_cast<const sockaddr_in *>(
_platform_data)->sin_port);
179 return (
int)ntohs(reinterpret_cast<const sockaddr_in6 *>(
_platform_data)->sin6_port);
189 reinterpret_cast<sockaddr_in *
>(
_platform_data)->sin_port = htons((
short)port);
192 reinterpret_cast<sockaddr_in6 *
>(
_platform_data)->sin6_port = htons((
short)port);
206 reinterpret_cast<sockaddr_storage *
>(
_platform_data)->ss_family = AF_INET;
215 reinterpret_cast<sockaddr_storage *
>(
_platform_data)->ss_family = AF_INET6;
233 int net_family =
reinterpret_cast<const sockaddr_storage *
>(
_platform_data)->ss_family;
234 const char *ans = NULL;
235 switch (net_family) {
254 struct addrinfo hints;
258 memset(&hints, 0,
sizeof(
struct addrinfo));
260 hints.ai_flags = AI_PASSIVE;
263 hints.ai_family |= AI_NUMERICSERV | AI_NUMERICHOST;
267 ans = getaddrinfo(hostname, sevicename, &hints, &result);
277 for (
struct addrinfo * cursor = result; cursor != NULL; cursor = cursor->ai_next) {
279 sockaddr_storage * storagebuffer =
new sockaddr_storage;
280 assert(
sizeof(sockaddr_storage) >= cursor->ai_addrlen);
281 memcpy(storagebuffer, cursor->ai_addr, cursor->ai_addrlen);
287 freeaddrinfo(result);
289 return addresspool.size();
299 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));
305 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));
322 sockaddr_in * addrv4 =
reinterpret_cast<sockaddr_in *
>(
_platform_data);
323 addrv4->sin_family = AF_INET;
324 addrv4->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
329 sockaddr_in6 * addrv6 =
reinterpret_cast<sockaddr_in6 *
>(
_platform_data);
330 addrv6->sin6_family = AF_INET6;
331 addrv6->sin6_addr = in6addr_loopback;
346 sockaddr_in * addrv4 =
reinterpret_cast<sockaddr_in *
>(
_platform_data);
347 addrv4->sin_family = AF_INET;
348 addrv4->sin_addr.s_addr = htonl(INADDR_BROADCAST);
359 sockaddr_in * addrv4 =
reinterpret_cast<sockaddr_in *
>(
_platform_data);
360 addrv4->sin_family = AF_INET;
361 addrv4->sin_addr.s_addr = htonl(INADDR_ANY);
366 sockaddr_in6 * addrv6 =
reinterpret_cast<sockaddr_in6 *
>(
_platform_data);
367 addrv6->sin6_family = AF_INET6;
368 addrv6->sin6_addr = in6addr_any;
389 namespace rp {
namespace arch {
namespace net{
402 ::setsockopt( _socket_fd, SOL_SOCKET, SO_REUSEADDR , (
char *)&bool_true, (
int)
sizeof(bool_true) );
405 this->setTimeout(DEFAULT_SOCKET_TIMEOUT, SOCKET_DIR_BOTH);
410 closesocket(_socket_fd);
421 const struct sockaddr * addr =
reinterpret_cast<const struct sockaddr *
>(localaddr.
getPlatformData());
423 int ans = ::bind(_socket_fd, addr, (
int)
sizeof(sockaddr_storage));
433 struct sockaddr * addr =
reinterpret_cast<struct sockaddr *
>(
const_cast<void *
>(localaddr.
getPlatformData()));
436 int actualsize =
sizeof(sockaddr_storage);
437 int ans = ::getsockname(_socket_fd, addr, &actualsize);
439 assert(actualsize <=
sizeof(sockaddr_storage));
440 assert(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
449 tv.tv_sec = timeout / 1000;
450 tv.tv_usec = (timeout % 1000) * 1000;
452 if (msk & SOCKET_DIR_RD) {
453 ans = ::setsockopt( _socket_fd, SOL_SOCKET, SO_RCVTIMEO, (
char *)&tv, (
int)
sizeof(tv) );
457 if (msk & SOCKET_DIR_WR) {
458 ans = ::setsockopt( _socket_fd, SOL_SOCKET, SO_SNDTIMEO, (
char *)&tv, (
int)
sizeof(tv) );
467 const struct sockaddr * addr =
reinterpret_cast<const struct sockaddr *
>(pairAddress.
getPlatformData());
468 int ans = ::connect(_socket_fd, addr, (
int)
sizeof(sockaddr_storage));
472 switch (WSAGetLastError()) {
473 case WSAEAFNOSUPPORT:
488 int ans = ::listen( _socket_fd, backlog);
496 addrsize =
sizeof(sockaddr_storage);
497 SOCKET pair_socket = ::accept( _socket_fd, pairAddress?reinterpret_cast<struct sockaddr *>(const_cast<void *>(pairAddress->
getPlatformData())):NULL
500 if (pair_socket>=0) {
501 return new StreamSocketImpl(pair_socket);
509 return waitforData(timeout);
514 int ans = ::send( _socket_fd, (
const char *)buffer, len, 0);
515 if (ans != SOCKET_ERROR ) {
516 assert(ans == (
int)len);
520 switch(WSAGetLastError()) {
533 int ans = ::recv( _socket_fd, (
char *)buf, len, 0);
536 if (ans == SOCKET_ERROR) {
538 switch(WSAGetLastError()) {
552 struct sockaddr * addr =
reinterpret_cast<struct sockaddr *
>(
const_cast<void *
>(peerAddr.
getPlatformData()));
554 int actualsize = (int)
sizeof(sockaddr_storage);
555 int ans = ::getpeername(_socket_fd, addr, &actualsize);
557 assert(actualsize <= (
int)
sizeof(sockaddr_storage));
558 assert(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
570 shutdw_opt = SD_RECEIVE;
573 shutdw_opt = SD_SEND;
575 case SOCKET_DIR_BOTH:
577 shutdw_opt = SD_BOTH;
580 int ans = ::shutdown(_socket_fd, shutdw_opt);
586 int bool_true = enable?1:0;
592 int bool_true = enable?1:0;
600 FD_SET(_socket_fd, &wrset);
603 tv.tv_sec = timeout / 1000;
604 tv.tv_usec = (timeout % 1000) * 1000;
605 int ans = ::select(NULL, NULL, &wrset, NULL, &tv);
624 FD_SET(_socket_fd, &rdset);
627 tv.tv_sec = timeout / 1000;
628 tv.tv_usec = (timeout % 1000) * 1000;
629 int ans = ::select(_socket_fd+1, &rdset, NULL, NULL, &tv);
661 ::setsockopt( _socket_fd, SOL_SOCKET, SO_REUSEADDR | SO_BROADCAST , (
char *)&bool_true, (
int)
sizeof(bool_true) );
662 setTimeout(DEFAULT_SOCKET_TIMEOUT, SOCKET_DIR_BOTH);
667 closesocket(_socket_fd);
678 const struct sockaddr * addr =
reinterpret_cast<const struct sockaddr *
>(localaddr.
getPlatformData());
680 int ans = ::bind(_socket_fd, addr, (
int)
sizeof(sockaddr_storage));
690 struct sockaddr * addr =
reinterpret_cast<struct sockaddr *
>(
const_cast<void *
>((localaddr.
getPlatformData())));
693 int actualsize = (int)
sizeof(sockaddr_storage);
694 int ans = ::getsockname(_socket_fd, addr, &actualsize);
696 assert(actualsize <= (
int)
sizeof(sockaddr_storage));
697 assert(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
706 tv.tv_sec = timeout / 1000;
707 tv.tv_usec = (timeout % 1000) * 1000;
709 if (msk & SOCKET_DIR_RD) {
710 ans = ::setsockopt( _socket_fd, SOL_SOCKET, SO_RCVTIMEO, (
const char *)&tv, (
int)
sizeof(tv) );
714 if (msk & SOCKET_DIR_WR) {
715 ans = ::setsockopt( _socket_fd, SOL_SOCKET, SO_SNDTIMEO, (
const char *)&tv, (
int)
sizeof(tv) );
727 FD_SET(_socket_fd, &wrset);
730 tv.tv_sec = timeout / 1000;
731 tv.tv_usec = (timeout % 1000) * 1000;
732 int ans = ::select(NULL, NULL, &wrset, NULL, &tv);
751 FD_SET(_socket_fd, &rdset);
754 tv.tv_sec = timeout / 1000;
755 tv.tv_usec = (timeout % 1000) * 1000;
756 int ans = ::select(NULL, &rdset, NULL, NULL, &tv);
773 const struct sockaddr * addr =
reinterpret_cast<const struct sockaddr *
>(target.
getPlatformData());
775 int ans = ::sendto( _socket_fd, (
const char *)buffer, (
int)len, 0, addr, (
int)
sizeof(sockaddr_storage));
776 if (ans != SOCKET_ERROR) {
777 assert(ans == (
int)len);
780 switch(WSAGetLastError()) {
795 struct sockaddr * addr = (sourceAddr?
reinterpret_cast<struct sockaddr *
>(
const_cast<void *
>(sourceAddr->
getPlatformData())):NULL);
796 int source_addr_size = (sourceAddr?
sizeof(sockaddr_storage):0);
798 int ans = ::recvfrom( _socket_fd, (
char *)buf, (
int)len, 0, addr, addr?&source_addr_size:NULL);
799 if (ans == SOCKET_ERROR) {
801 int errCode = WSAGetLastError();
826 namespace rp {
namespace net{
840 assert(!
"should not reach here");
849 if (family == SOCKET_FAMILY_RAW)
return NULL;
853 int socket_fd = ::socket(socket_family, SOCK_STREAM, 0);
854 if (socket_fd == -1)
return NULL;
867 int socket_fd = ::socket(socket_family, (family==SOCKET_FAMILY_RAW)?SOCK_RAW:SOCK_DGRAM, 0);
868 if (socket_fd == -1)
return NULL;
virtual StreamSocket * accept(SocketAddress *pairAddress)
static const char * _inet_ntop(int af, const void *src, char *dst, int cnt)
static int _socketHalFamilyToOSFamily(SocketBase::socket_family_t family)
static volatile bool _isWSAStartupCalled
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 &)
DGramSocketImpl(SOCKET fd)
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)
static int _inet_pton(int Family, const char *pszAddrString, void *pAddrBuf)
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 bool _checkWSAStartup()
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
StreamSocketImpl(SOCKET fd)
#define RESULT_INVALID_DATA
static DGramSocket * CreateSocket(socket_family_t family=SOCKET_FAMILY_INET)
virtual u_result waitforIncomingConnection(_u32 timeout)