Debian Codegraph @

Network/TcpClient.cpp

raw symbols SCIP
1 /*
2 RailControl - Model Railway Control Software
3
4 Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
5
6 RailControl is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 RailControl is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with RailControl; see the file LICENCE. If not see
18 <http://www.gnu.org/licenses/>.
19 */
20 #include <arpa/inet.h>
21 #include <fcntl.h>
22 #include <netinet/in.h>
23 #include <string.h>
24
25 #include "Network/Select.h"
26 #include "Network/TcpClient.h"
27
28 namespace Network
29 {
30 TcpConnection TcpClient::GetTcpClientConnection(Logger::Logger* logger, const std::string& host, const unsigned short port)
31 {
32 struct sockaddr_storage address;
33 struct sockaddr_in* addressPointer = reinterpret_cast<struct sockaddr_in*>(&address);
34 addressPointer->sin_family = AF_INET;
35 addressPointer->sin_port = htons(port);
36 int ok = inet_pton(AF_INET, host.c_str(), &(addressPointer->sin_addr));
37 if (ok <= 0)
38 {
39 logger->Error(Languages::TextUnableToResolveAddress, host);
40 return TcpConnection(0);
41 }
42
43 int sock = socket(AF_INET, SOCK_STREAM, 0);
44 if (sock < 0)
45 {
46 logger->Error(Languages::TextUnableToCreateTcpSocket, host, port);
47 return TcpConnection(0);
48 }
49
50 ok = ConnectWithTimeout(sock, reinterpret_cast<struct sockaddr*>(addressPointer), sizeof(address));
51 if (ok < 0)
52 {
53 Languages::TextSelector text;
54 switch (errno)
55 {
56 case ECONNREFUSED:
57 text = Languages::TextConnectionRefused;
58 break;
59
60 case ENETUNREACH:
61 text = Languages::TextNetworkUnreachable;
62 break;
63
64 default:
65 text = Languages::TextConnectionFailed;
66 }
67 logger->Error(text, host, port);
68 close(sock);
69 return TcpConnection(0);
70 }
71
72 return TcpConnection(sock, &address);
73 }
74
75 int TcpClient::ConnectWithTimeout(int sock, struct sockaddr *addr, socklen_t length)
76 {
77 // Set non-blocking
78 long arg = fcntl(sock, F_GETFL, nullptr);
79 if (arg < 0)
80 {
81 return -1;
82 }
83 arg |= O_NONBLOCK;
84 if (fcntl(sock, F_SETFL, arg) < 0)
85 {
86 return -1;
87 }
88 // Trying to connect with timeout
89 int ret = connect(sock, addr, length);
90 if (ret < 0)
91 {
92 if (errno != EINPROGRESS)
93 {
94 return -1;
95 }
96 while (true)
97 {
98 fd_set myset;
99 int valopt;
100 socklen_t lon;
101 struct timeval tv;
102 tv.tv_sec = 3;
103 tv.tv_usec = 0;
104 FD_ZERO(&myset);
105 FD_SET(sock, &myset);
106 ret = TEMP_FAILURE_RETRY(select(sock + 1, nullptr, &myset, nullptr, &tv));
107 if (ret < 0 && errno == EINTR)
108 {
109 continue;
110 }
111
112 if (ret <= 0)
113 {
114 return -1;
115 }
116
117 // Socket selected for write
118 lon = sizeof(int);
119 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*) (&valopt), &lon) < 0)
120 {
121 return -1;
122 }
123 // Check the value returned...
124 if (valopt)
125 {
126 return -1;
127 }
128 break;
129 }
130 }
131 // Set to blocking mode again...
132 if ((arg = fcntl(sock, F_GETFL, nullptr)) < 0)
133 {
134 return -1;
135 }
136 arg &= (~O_NONBLOCK);
137 if (fcntl(sock, F_SETFL, arg) < 0)
138 {
139 return -1;
140 }
141 return ret;
142 }
143 }