Skip to content

Commit 1f36d94

Browse files
tcp: Allow connecting by hostname
1 parent 4664e25 commit 1f36d94

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/mavsdk/core/tcp_connection.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <sys/socket.h>
1111
#include <arpa/inet.h>
1212
#include <errno.h>
13+
#include <netdb.h>
1314
#include <unistd.h> // for close()
1415
#endif
1516

@@ -80,7 +81,16 @@ ConnectionResult TcpConnection::setup_port()
8081
struct sockaddr_in remote_addr {};
8182
remote_addr.sin_family = AF_INET;
8283
remote_addr.sin_port = htons(_remote_port_number);
83-
remote_addr.sin_addr.s_addr = inet_addr(_remote_ip.c_str());
84+
85+
struct hostent *hp;
86+
hp = gethostbyname(_remote_ip.c_str());
87+
if (hp == nullptr) {
88+
LogErr() << "Could not get host by name";
89+
_is_ok = false;
90+
return ConnectionResult::SocketConnectionError;
91+
}
92+
93+
memcpy(&remote_addr.sin_addr, hp->h_addr, hp->h_length);
8494

8595
if (connect(_socket_fd, reinterpret_cast<sockaddr*>(&remote_addr), sizeof(struct sockaddr_in)) <
8696
0) {

0 commit comments

Comments
 (0)