modbus_check_ip_connection.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Pilz GmbH & Co. KG
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <cstring>
19 #include <chrono>
20 #include <thread>
21 #include <fcntl.h>
22 #include <arpa/inet.h>
23 #include <unistd.h>
25 
26 namespace prbt_hardware_support
27 {
28 timeval initTimeout(const unsigned int& secs, const unsigned int& usecs)
29 {
30  timeval ret;
31  ret.tv_sec = secs;
32  ret.tv_usec = usecs;
33  return ret;
34 }
35 
36 sockaddr_in initSockAddrIn(const char* ip, const unsigned int& port)
37 {
38  sockaddr_in ret;
39  std::memset((char*)&ret, 0, sizeof(ret));
40  ret.sin_family = AF_INET;
41  ret.sin_port = htons((short unsigned int)port);
42  ret.sin_addr.s_addr = inet_addr(ip);
43  return ret;
44 }
45 
46 void setConnectionToNonBlocking(const int& sockfd)
47 {
48  int file_descr_flags{ fcntl(sockfd, F_GETFL, NULL) };
49  file_descr_flags |= O_NONBLOCK;
50  fcntl(sockfd, F_SETFL, file_descr_flags);
51 }
52 
53 bool isSocketReadyForWriteOp(const int& sockfd)
54 {
55  fd_set writeset;
56  FD_ZERO(&writeset);
57  FD_SET(sockfd, &writeset);
58  timeval timeout_in_s{ initTimeout(1, 0) };
59  const int socket_ready_for_writing{ select(sockfd + 1, nullptr, &writeset, nullptr, &timeout_in_s) };
60  return socket_ready_for_writing > 0;
61 }
62 
63 bool hasSocketPendingErrors(const int& sockfd)
64 {
65  int optval;
66  socklen_t optlen{ sizeof(optval) };
67  const int read_pending_errors_failed{ getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void*)&optval, &optlen) };
68  return !(read_pending_errors_failed == 0) || !(optval == 0);
69 }
70 
71 bool checkIPConnection(const char* ip, const unsigned int& port)
72 {
73  const int sockfd{ socket(AF_INET, SOCK_STREAM, 0) };
74  const sockaddr_in serv_addr{ initSockAddrIn(ip, port) };
75 
77  connect(sockfd, (const sockaddr*)&serv_addr, sizeof(serv_addr));
78 
79  const bool connection_ok{ isSocketReadyForWriteOp(sockfd) && !hasSocketPendingErrors(sockfd) };
80 
81  close(sockfd);
82  // wait one second to grant a free port
83  std::this_thread::sleep_for(std::chrono::duration<double>(1));
84 
85  return connection_ok;
86 }
87 
88 } // namespace prbt_hardware_support
timeval initTimeout(const unsigned int &secs, const unsigned int &usecs)
bool checkIPConnection(const char *ip, const unsigned int &port)
Test the ip connection by connecting to the modbus server.
bool hasSocketPendingErrors(const int &sockfd)
bool isSocketReadyForWriteOp(const int &sockfd)
sockaddr_in initSockAddrIn(const char *ip, const unsigned int &port)
void setConnectionToNonBlocking(const int &sockfd)


prbt_hardware_support
Author(s):
autogenerated on Thu Sep 10 2020 03:14:36