Debian Codegraph @

Utils/Network.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
21 #include <arpa/inet.h>
22 #include <cstring>
23 #include <netdb.h>
24
25 #include "Utils/Network.h"
26
27 using std::string;
28
29 namespace Utils
30 {
31 string Network::AddressToString(const struct sockaddr_storage *sockaddr)
32 {
33 char buffer[40] = { 0 };
34 unsigned short port = 0;
35 switch(sockaddr->ss_family) {
36 case AF_INET:
37 {
38 const struct sockaddr_in* sa4 = reinterpret_cast<const struct sockaddr_in*>(sockaddr);
39 inet_ntop(AF_INET, &(sa4->sin_addr), buffer, sizeof(buffer));
40 port = ntohs(sa4->sin_port);
41 break;
42 }
43
44 case AF_INET6:
45 {
46 const struct sockaddr_in6* sa6 = reinterpret_cast<const struct sockaddr_in6*>(sockaddr);
47 inet_ntop(AF_INET6, &(sa6->sin6_addr), buffer, sizeof(buffer));
48 port = ntohs(sa6->sin6_port);
49 break;
50 }
51
52 default:
53 break;
54 }
55 return string("[") + string(buffer) + string("]:") + std::to_string(port);
56 }
57
58 bool Network::CompareAddresses(const struct sockaddr_storage *address1, const struct sockaddr_storage *address2)
59 {
60 const unsigned short family = (reinterpret_cast<const struct sockaddr*>(address1))->sa_family;
61 if (family != (reinterpret_cast<const struct sockaddr*>(address2))->sa_family)
62 {
63 return false;
64 }
65
66 switch (family)
67 {
68 case AF_INET:
69 {
70 const struct sockaddr_in* inet1 = reinterpret_cast<const struct sockaddr_in*>(address1);
71 const struct sockaddr_in* inet2 = reinterpret_cast<const struct sockaddr_in*>(address2);
72 if (inet1->sin_port != inet2->sin_port)
73 {
74 return false;
75 }
76 return (0 == memcmp(&(inet1->sin_addr), &(inet2->sin_addr), sizeof(inet1->sin_addr)));
77 }
78
79 case AF_INET6:
80 {
81 const struct sockaddr_in6* inet1 = reinterpret_cast<const struct sockaddr_in6*>(address1);
82 const struct sockaddr_in6* inet2 = reinterpret_cast<const struct sockaddr_in6*>(address2);
83 if (inet1->sin6_port != inet2->sin6_port)
84 {
85 return false;
86 }
87 return (0 == memcmp(&(inet1->sin6_addr), &(inet2->sin6_addr), sizeof(inet1->sin6_addr)));
88 }
89
90 default:
91 return false;
92 }
93 }
94
95 bool Network::HostResolves(const string& host)
96 {
97 struct addrinfo hints;
98 memset(&hints, 0, sizeof(hints));
99 hints.ai_family = PF_UNSPEC;
100 hints.ai_socktype = SOCK_STREAM;
101 hints.ai_flags |= AI_CANONNAME;
102
103 struct addrinfo* res;
104 struct addrinfo* result;
105 int errcode = getaddrinfo(host.c_str(), NULL, &hints, &result);
106 if (errcode != 0)
107 {
108 return false;
109 }
110
111 res = result;
112
113 while (res)
114 {
115 char addrstr[100];
116 inet_ntop(res->ai_family, res->ai_addr->sa_data, addrstr, sizeof(addrstr));
117
118 switch (res->ai_family)
119 {
120 case AF_INET:
121 case AF_INET6:
122 freeaddrinfo(result);
123 return true;
124
125 default:
126 res = res->ai_next;
127 break;
128 }
129 }
130
131 freeaddrinfo(result);
132 return false;
133 }
134 } // namespace Utils