MCPcopy Create free account
hub / github.com/OpenFOAM/OpenFOAM-dev / ping

Function ping

src/OSspecific/POSIX/POSIX.C:1109–1184  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1107
1108
1109bool Foam::ping
1110(
1111 const string& destName,
1112 const label destPort,
1113 const label timeOut
1114)
1115{
1116 struct hostent *hostPtr;
1117 volatile int sockfd;
1118 struct sockaddr_in destAddr; // will hold the destination addr
1119 u_int addr;
1120
1121 if ((hostPtr = ::gethostbyname(destName.c_str())) == nullptr)
1122 {
1123 FatalErrorInFunction
1124 << "gethostbyname error " << h_errno << " for host " << destName
1125 << abort(FatalError);
1126 }
1127
1128 // Get first of the SLL of addresses
1129 addr = (reinterpret_cast<struct in_addr*>(*(hostPtr->h_addr_list)))->s_addr;
1130
1131 // Allocate socket
1132 sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
1133 if (sockfd < 0)
1134 {
1135 FatalErrorInFunction
1136 << "socket error"
1137 << abort(FatalError);
1138 }
1139
1140 // Fill sockaddr_in structure with dest address and port
1141 memset(reinterpret_cast<char *>(&destAddr), '\0', sizeof(destAddr));
1142 destAddr.sin_family = AF_INET;
1143 destAddr.sin_port = htons(ushort(destPort));
1144 destAddr.sin_addr.s_addr = addr;
1145
1146
1147 timer myTimer(timeOut);
1148
1149 if (timedOut(myTimer))
1150 {
1151 // Setjmp from timer jumps back to here
1152 fdClose(sockfd);
1153 return false;
1154 }
1155
1156 if
1157 (
1158 ::connect
1159 (
1160 sockfd,
1161 reinterpret_cast<struct sockaddr*>(&destAddr),
1162 sizeof(struct sockaddr)
1163 ) != 0
1164 )
1165 {
1166 // Connection refused. Check if network was actually used or not.

Callers

nothing calls this directly

Calls 2

fdCloseFunction · 0.85
abortFunction · 0.50

Tested by

no test coverage detected