2 Copyright 2020 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP. If not, see <https://www.gnu.org/licenses/>.
20 #include <string.h> // memcmp
26 #include <unistd.h> // pipe2
27 #include <fcntl.h> // pipe2-O_NONBLOCK fcntl
28 #include <sys/socket.h> // socket connect getaddrinfo
29 #include <netdb.h> // getaddrinfo
30 #include <sys/select.h> // select
31 #include <linux/if_packet.h> // for getMAC
32 #include <net/ethernet.h> // for getMAC
33 #include <ifaddrs.h> // getifaddrs
36 #include <sys/types.h> // socket connect getaddrinfo
37 #include <string.h> // memset
38 #include <errno.h> // errno var
50 CLOSESOCKET(abortSocket);
52 if (abortPipe[0] != -1)
64 logger = Log::getInstance();
67 abortSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
68 if (abortSocket == INVALID_SOCKET)
70 logger->log("TCP", Log::CRIT, "socket error");
74 if (pipe2(abortPipe, O_NONBLOCK) == -1)
76 logger->log("TCP", Log::CRIT, "pipe2 error");
84 bool TCP::connect(const std::string& ip, USHORT port)
86 if (connected) return false;
88 struct addrinfo hints;
89 memset(&hints, 0, sizeof(struct addrinfo));
90 hints.ai_family = AF_UNSPEC;
91 hints.ai_socktype = SOCK_STREAM;
92 hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
96 SNPRINTF(portString, 10, "%u", port);
97 int gaiResult = getaddrinfo(ip.c_str(), portString, &hints, &aip);
99 if ((gaiResult != 0) || !aip)
101 logger->log("TCP", Log::CRIT, "getaddrinfo error");
105 sockfd = socket(aip->ai_family, SOCK_STREAM, 0);
106 if (sockfd == -1) { logger->log("TCP", Log::CRIT, "socket error"); return false; }
110 ioctlsocket(sockfd, FIONBIO, ¶m);
112 fcntl(sockfd, F_SETFL, O_NONBLOCK);
116 // There should only be one aip result..
117 int connectResult = ::connect(sockfd, aip->ai_addr, aip->ai_addrlen);
119 if (connectResult == 0) // success
126 if (errno != EINPROGRESS)
130 logger->log("TCP", Log::CRIT, "connect error");
134 // Wait for connect to complete
138 FD_SET(sockfd, &writefds);
142 FD_SET(abortSocket, &writefds);
144 FD_SET(abortPipe[0], &writefds);
145 if (abortPipe[0] > maxfd) maxfd = abortPipe[0];
148 tv.tv_sec = 5; // Allow 5s for a connect
151 int selectResult = select(maxfd + 1, NULL, &writefds, NULL, &tv);
153 if ((selectResult == 1) || FD_ISSET(sockfd, &writefds))
156 logger->log("TCP", Log::INFO, "connected");
164 logger->log("TCP", Log::CRIT, "connect/select error");
171 if (!connected) return;
182 void TCP::abortCall()
185 CLOSESOCKET(abortSocket);
187 ::write(abortPipe[1], "X", 1);
191 bool TCP::write(void* src, ULONG numBytes)
193 if (!connected) return false;
195 std::lock_guard<std::mutex> lg(writeMutex);
196 int result = send(sockfd, reinterpret_cast<char*>(src), numBytes, 0); // FIXME does send return < numBytes? Might need loop
197 if (result < 0) return false;
198 if (static_cast<ULONG>(result) != numBytes) return false;
203 bool TCP::read(void* dst, ULONG numBytes)
205 if (!connected) return false;
210 ULONG totalReceived = 0;
212 char* pointer = static_cast<char*>(dst); // WIN32 requires char*
218 logger->log("TCP", Log::DEBUG, "abortCount = 5");
223 FD_SET(sockfd, &readfds);
224 int maxfd = sockfd; // WIN32 ignores
227 FD_SET(abortSocket, &readfds);
229 FD_SET(abortPipe[0], &readfds);
230 if (abortPipe[0] > maxfd) maxfd = abortPipe[0];
236 int selectResult = select(maxfd + 1, &readfds, NULL, NULL, &tv);
238 if (selectResult == -1) { shutdown(); return false; }
239 if (selectResult == 0) return false;
242 if (FD_ISSET(abortSocket, &readfds)) { logger->log("TCP", Log::DEBUG, "aborting..."); return false; }
244 if (FD_ISSET(abortPipe[0], &readfds)) { logger->log("TCP", Log::DEBUG, "aborting..."); return false; }
247 int recvResult = recv(sockfd, pointer, numBytes - totalReceived, 0);
248 if (recvResult == -1) { shutdown(); return false; }
249 totalReceived += recvResult;
250 pointer += recvResult;
252 } while (totalReceived < numBytes);
257 MACAddress TCP::getMAC()
260 MACAddress macerror{ 00, 00, 00, 00, 00, 00 };
261 if (!connected) return macerror;
264 struct sockaddr - man 2 bind
265 struct sockaddr_in - man 7 ip
266 struct sockaddr_in6 - man 7 ipv6
267 struct sockaddr_ll - man 7 packet
270 // Measured sizeof(struct sockaddr_in) = 16, and in6 = 28
272 unsigned char gsnBuffer[buflen];
273 struct sockaddr* sap = reinterpret_cast<struct sockaddr*>(gsnBuffer);
274 if (getsockname(sockfd, sap, &buflen) == -1) return macerror;
277 unsigned char my6Addr[16];
279 if (sap->sa_family == AF_INET)
281 struct sockaddr_in* sap4 = reinterpret_cast<struct sockaddr_in*>(gsnBuffer);
282 my4AddrU32 = sap4->sin_addr.s_addr;
284 else if (sap->sa_family == AF_INET6)
286 struct sockaddr_in6* sap6 = reinterpret_cast<struct sockaddr_in6*>(gsnBuffer);
287 memcpy(my6Addr, sap6->sin6_addr.s6_addr, 16);
294 struct ifaddrs* ifAddrs;
295 if (getifaddrs(&ifAddrs) == -1) return macerror;
299 for(struct ifaddrs* ifa = ifAddrs; ifa != NULL; ifa = ifa->ifa_next)
301 if (ifa->ifa_addr->sa_family != sap->sa_family) continue;
303 if (sap->sa_family == AF_INET)
305 struct sockaddr_in* test = reinterpret_cast<struct sockaddr_in*>(ifa->ifa_addr);
306 if (test->sin_addr.s_addr == my4AddrU32)
308 ifName = ifa->ifa_name;
312 else if (sap->sa_family == AF_INET6)
314 struct sockaddr_in6* test = reinterpret_cast<struct sockaddr_in6*>(ifa->ifa_addr);
315 if (!memcmp(my6Addr, test->sin6_addr.s6_addr, 16))
317 ifName = ifa->ifa_name;
325 freeifaddrs(ifAddrs);
329 // Walk the list again to get the MAC for ifName using family == AF_PACKET
330 MACAddress toReturn = macerror;
332 for(struct ifaddrs* ifa = ifAddrs; ifa != NULL; ifa = ifa->ifa_next)
334 if (ifa->ifa_addr->sa_family != AF_PACKET) continue;
335 if (strcmp(ifName, ifa->ifa_name)) continue;
336 struct sockaddr_ll* sall = reinterpret_cast<struct sockaddr_ll*>(ifa->ifa_addr);
337 if (sall->sll_halen != 6) continue;
339 memcpy(&toReturn, sall->sll_addr, 6);
343 freeifaddrs(ifAddrs);
347 PIP_ADAPTER_INFO daptinfo = NULL;
349 GetAdaptersInfo(daptinfo, &size);
350 daptinfo = (PIP_ADAPTER_INFO)new char[size + 1];
351 MACAddress macresult;
352 memcpy(¯esult, "ABCDEF", 6);//Dummy Address
353 sockaddr_in sock_address;
354 int sockname_len = sizeof(sock_address);
355 getsockname(sockfd, (sockaddr*)&sock_address, &sockname_len);
356 ULONG sockip = sock_address.sin_addr.s_addr;
357 if (GetAdaptersInfo(daptinfo, &size) == ERROR_SUCCESS)
359 PIP_ADAPTER_INFO daptinfo_it = daptinfo;
360 while (daptinfo_it != NULL)
362 ULONG ipaddress = inet_addr(daptinfo_it->IpAddressList.IpAddress.String);
363 if (ipaddress == sockip)
365 memcpy(¯esult, daptinfo_it->Address, 6);
368 daptinfo_it = daptinfo_it->Next;
369 if (daptinfo_it == daptinfo) break;