| 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 | |
| 21 | #include <arpa/inet.h> |
| 22 | #include <cstring> // memset |
| 23 | #include <unistd.h> // close & TEMP_FAILURE_RETRY; |
| 24 | |
| 25 | #include "Network/Select.h" |
| 26 | #include "Network/UdpConnection.h" |
| 27 | #include "Utils/Utils.h" |
| 28 | |
| 29 | namespace Network |
| 30 | { |
| 31 | UdpConnection::UdpConnection(Logger::Logger* logger, |
| 32 | const std::string& server, |
| 33 | const unsigned short port) |
| 34 | : logger(logger), |
| 35 | connected(false), |
| 36 | server(server), |
| 37 | port(port) |
| 38 | { |
| 39 | memset((char*)&sockaddr, 0, sizeof(sockaddr)); |
| 40 | struct sockaddr_in* sockaddr_in = reinterpret_cast<struct sockaddr_in*>(&sockaddr); |
| 41 | sockaddr_in->sin_family = AF_INET; |
| 42 | sockaddr_in->sin_port = htons(port); |
| 43 | int ok = inet_pton(AF_INET, server.c_str(), &sockaddr_in->sin_addr); |
| 44 | if (ok <= 0) |
| 45 | { |
| 46 | logger->Error(Languages::TextUnableToResolveAddress, server); |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | CreateUdpSocket(); |
| 51 | } |
| 52 | |
| 53 | UdpConnection::UdpConnection(Logger::Logger* logger, |
| 54 | struct sockaddr* sockaddr) |
| 55 | : logger(logger), |
| 56 | connected(false), |
| 57 | sockaddr(*sockaddr) |
| 58 | { |
| 59 | const struct sockaddr_in* sockaddr_in = reinterpret_cast<const struct sockaddr_in*>(&(this->sockaddr)); |
| 60 | char serverC[20]; |
| 61 | inet_ntop(AF_INET, &sockaddr_in->sin_addr, serverC, sizeof(serverC)); |
| 62 | server = serverC; |
| 63 | port = ntohs(sockaddr_in->sin_port); |
| 64 | |
| 65 | CreateUdpSocket(); |
| 66 | } |
| 67 | |
| 68 | void UdpConnection::CreateUdpSocket() |
| 69 | { |
| 70 | // create socket |
| 71 | connectionSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 72 | if (connectionSocket == -1) |
| 73 | { |
| 74 | logger->Error(Languages::TextUnableToCreateUdpSocket, server, port); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | // setting receive timeout to 1s |
| 79 | struct timeval tv; |
| 80 | tv.tv_sec = 1; |
| 81 | tv.tv_usec = 0; |
| 82 | setsockopt(connectionSocket, SOL_SOCKET, SO_RCVTIMEO, (const char*) &tv, sizeof(struct timeval)); |
| 83 | connected = true; |
| 84 | } |
| 85 | |
| 86 | bool UdpConnection::Bind() |
| 87 | { |
| 88 | int ret = bind(connectionSocket, &sockaddr, sizeof(sockaddr)); |
| 89 | if (ret == 0) |
| 90 | { |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | logger->Error(Languages::TextUnableToBindSocketToPort, port); |
| 95 | connected = false; |
| 96 | close(connectionSocket); |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | void UdpConnection::Terminate() |
| 101 | { |
| 102 | if (connected) |
| 103 | { |
| 104 | connected = false; |
| 105 | close(connectionSocket); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | int UdpConnection::Send(const char* buffer, const size_t bufferLength) |
| 110 | { |
| 111 | if (!connected) |
| 112 | { |
| 113 | logger->Error(Languages::TextConnectionReset); |
| 114 | errno = ECONNRESET; |
| 115 | return -1; |
| 116 | } |
| 117 | return sendto(connectionSocket, buffer, bufferLength, 0, &sockaddr, sizeof(sockaddr)); |
| 118 | } |
| 119 | |
| 120 | int UdpConnection::Receive(char* buffer, const size_t bufferLength) |
| 121 | { |
| 122 | if (!connected) |
| 123 | { |
| 124 | logger->Error(Languages::TextConnectionReset); |
| 125 | errno = ECONNRESET; |
| 126 | return -1; |
| 127 | } |
| 128 | ssize_t ret; |
| 129 | do |
| 130 | { |
| 131 | if (!connected) |
| 132 | { |
| 133 | return 0; |
| 134 | } |
| 135 | ret = recvfrom(connectionSocket, buffer, bufferLength, 0, NULL, NULL); |
| 136 | } while(ret < 0 && errno == EAGAIN); |
| 137 | return ret; |
| 138 | } |
| 139 | } |