summaryrefslogtreecommitdiff
path: root/drivers/unix/stream_peer_tcp_posix.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/unix/stream_peer_tcp_posix.cpp')
-rw-r--r--drivers/unix/stream_peer_tcp_posix.cpp26
1 files changed, 11 insertions, 15 deletions
diff --git a/drivers/unix/stream_peer_tcp_posix.cpp b/drivers/unix/stream_peer_tcp_posix.cpp
index 45a4b934c1..f2915b0a16 100644
--- a/drivers/unix/stream_peer_tcp_posix.cpp
+++ b/drivers/unix/stream_peer_tcp_posix.cpp
@@ -61,13 +61,7 @@
#define MSG_NOSIGNAL SO_NOSIGPIPE
#endif
-static void set_addr_in(struct sockaddr_in& their_addr, const IP_Address& p_host, uint16_t p_port) {
-
- their_addr.sin_family = AF_INET; // host byte order
- their_addr.sin_port = htons(p_port); // short, network byte order
- their_addr.sin_addr = *((struct in_addr*)&p_host.host);
- memset(&(their_addr.sin_zero), '\0', 8);
-};
+#include "drivers/unix/socket_helpers.h"
StreamPeerTCP* StreamPeerTCPPosix::_create() {
@@ -103,9 +97,10 @@ Error StreamPeerTCPPosix::_poll_connection(bool p_block) const {
_block(sockfd, false, true);
};
- struct sockaddr_in their_addr;
- set_addr_in(their_addr, peer_host, peer_port);
- if (::connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1) {
+ struct sockaddr_storage their_addr;
+ size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port);
+
+ if (::connect(sockfd, (struct sockaddr *)&their_addr,addr_size) == -1) {
if (errno == EISCONN) {
status = STATUS_CONNECTED;
@@ -140,9 +135,10 @@ void StreamPeerTCPPosix::set_socket(int p_sockfd, IP_Address p_host, int p_port)
Error StreamPeerTCPPosix::connect(const IP_Address& p_host, uint16_t p_port) {
- ERR_FAIL_COND_V( p_host.host == 0, ERR_INVALID_PARAMETER);
+ ERR_FAIL_COND_V( p_host.type == IP_Address::TYPE_NONE, ERR_INVALID_PARAMETER);
- if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
+ int family = p_host.type == IP_Address::TYPE_IPV6 ? AF_INET6 : AF_INET;
+ if ((sockfd = socket(family, SOCK_STREAM, 0)) == -1) {
ERR_PRINT("Socket creation failed!");
disconnect();
//perror("socket");
@@ -156,11 +152,11 @@ Error StreamPeerTCPPosix::connect(const IP_Address& p_host, uint16_t p_port) {
ioctl(sockfd, FIONBIO, &bval);
#endif
- struct sockaddr_in their_addr;
- set_addr_in(their_addr, p_host, p_port);
+ struct sockaddr_storage their_addr;
+ size_t addr_size = _set_sockaddr(&their_addr, p_host, p_port);
errno = 0;
- if (::connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1 && errno != EINPROGRESS) {
+ if (::connect(sockfd, (struct sockaddr *)&their_addr,addr_size) == -1 && errno != EINPROGRESS) {
ERR_PRINT("Connection to remote host failed!");
disconnect();