author | František Kučera <franta-hg@frantovo.cz> |
Thu, 04 Aug 2022 02:13:22 +0200 | |
branch | v_0 |
changeset 10 | 6a6b93507856 |
parent 9 | de48b9278211 |
child 11 | a910051cd975 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, version 3 of the License. |
|
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 General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
#include <string> |
|
1
e3265afd1111
first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
18 |
#include <cstring> |
e3265afd1111
first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
19 |
#include <unistd.h> |
0 | 20 |
#include <stdexcept> |
1
e3265afd1111
first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
21 |
#include <arpa/inet.h> |
e3265afd1111
first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
22 |
#include <sys/types.h> |
e3265afd1111
first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
23 |
#include <sys/socket.h> |
e3265afd1111
first version: TCP-only + fixed parameters
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
24 |
#include <netinet/in.h> |
2 | 25 |
#include <vector> |
3
e701e06ff561
add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
26 |
#include <memory> |
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
27 |
#include <regex> |
10
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
28 |
#include <iostream> |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
29 |
#include <netdb.h> |
2 | 30 |
|
31 |
#include "Socket.h" |
|
0 | 32 |
|
33 |
namespace relpipe { |
|
34 |
namespace out { |
|
35 |
namespace socket { |
|
36 |
||
5
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
37 |
using namespace relpipe::out::socket::options; |
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
38 |
|
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
39 |
static const std::string findOption(SocketOptions options, std::string name, bool required = false, const std::string defaultValue = "") { |
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
40 |
for (auto o : options) if (o.name == name) return o.value; |
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
41 |
if (required) throw std::invalid_argument("Option " + name + " is required but was not found"); |
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
42 |
else return defaultValue; |
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
43 |
} |
3
e701e06ff561
add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
44 |
|
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
45 |
class FD { |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
46 |
private: |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
47 |
int fd; |
0 | 48 |
public: |
49 |
||
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
50 |
FD(int fd) : fd(fd) { |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
51 |
}; |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
52 |
|
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
53 |
virtual ~FD() { |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
54 |
close(fd); |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
55 |
} |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
56 |
|
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
57 |
int getFD() { |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
58 |
return fd; |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
59 |
} |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
60 |
}; |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
61 |
|
9
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
62 |
static void check(int result, std::string message) { |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
63 |
if (result == 0); // OK |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
64 |
else throw std::logic_error("Got error result: " + std::to_string(result) + " - " + message); |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
65 |
} |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
66 |
|
10
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
67 |
template <typename T> static T* castAddress(addrinfo* ai, sa_family_t family) { |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
68 |
if (ai && ai->ai_family == family && sizeof (T) == ai->ai_addrlen) return (T*) ai->ai_addr; |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
69 |
else return nullptr; |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
70 |
} |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
71 |
|
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
72 |
static std::string ip2string(const addrinfo* ai) { |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
73 |
//char buffer[INET_ADDRSTRLEN] = {0}; |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
74 |
char buffer[INET6_ADDRSTRLEN] = {0}; |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
75 |
//return inet_ntop(ai->ai_family, &ai->ai_addr, buffer, sizeof (buffer)); |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
76 |
//return inet_ntop(ai->ai_family, &ai->ai_addr->sa_data, buffer, sizeof (buffer)); |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
77 |
if (ai->ai_family == AF_INET) return inet_ntop(ai->ai_family, &((sockaddr_in const *) ai->ai_addr)->sin_addr, buffer, sizeof (buffer)); // TODO: check 0 result |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
78 |
else if (ai->ai_family == AF_INET6) return inet_ntop(ai->ai_family, &((sockaddr_in6 const *) ai->ai_addr)->sin6_addr, buffer, sizeof (buffer)); // TODO: check 0 result |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
79 |
else throw std::logic_error("Invalid address family: " + std::to_string(ai->ai_family)); |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
80 |
} |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
81 |
|
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
82 |
static in_addr_t getAddress(const std::string& host) { |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
83 |
|
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
84 |
struct addrinfo* addrInfo; |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
85 |
check(getaddrinfo(host.c_str(), "", nullptr, &addrInfo), "getaddrinfo"); |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
86 |
|
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
87 |
std::cerr << "found: " << addrInfo << std::endl; |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
88 |
if (addrInfo) { |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
89 |
for (addrinfo* ai = addrInfo; ai; ai = ai->ai_next) { |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
90 |
std::cerr << "address info: family=" << std::to_string(ai->ai_addr->sa_family); |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
91 |
if (sockaddr_in * sa = castAddress<sockaddr_in>(ai, AF_INET)) std::cerr << " IPv4=" << ip2string(ai); |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
92 |
else if (sockaddr_in6 * sa = castAddress<sockaddr_in6>(ai, AF_INET6)) std::cerr << " IPv6=" << ip2string(ai); |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
93 |
std::cerr << std::endl; |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
94 |
} |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
95 |
} |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
96 |
|
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
97 |
if (addrInfo) { |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
98 |
for (addrinfo* ai = addrInfo; ai; ai = ai->ai_next) { |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
99 |
if (sockaddr_in * sa = castAddress<sockaddr_in>(ai, AF_INET)) { |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
100 |
in_addr_t address = sa->sin_addr.s_addr; |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
101 |
freeaddrinfo(addrInfo); |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
102 |
return address; |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
103 |
|
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
104 |
} |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
105 |
} |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
106 |
} |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
107 |
|
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
108 |
freeaddrinfo(addrInfo); |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
109 |
|
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
110 |
// TODO: rather throw exception – requires IP address in the host string: |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
111 |
return inet_addr(host.c_str()); |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
112 |
} |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
113 |
|
9
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
114 |
class UDPClientSocket : public Socket { |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
115 |
private: |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
116 |
struct sockaddr_in remoteAddress; |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
117 |
int protocol = IPPROTO_UDP; |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
118 |
int type = SOCK_DGRAM; |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
119 |
public: |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
120 |
|
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
121 |
static std::shared_ptr<Socket> open(const SocketOptions& options) { |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
122 |
std::shared_ptr<UDPClientSocket> s = std::make_shared<UDPClientSocket>(); |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
123 |
memset((char *) &s->remoteAddress, 0, sizeof (s->remoteAddress)); |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
124 |
s->remoteAddress.sin_family = AF_INET; |
10
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
125 |
s->remoteAddress.sin_addr.s_addr = getAddress(findOption(options, OPTION_HOST, true)); // TODO: use getaddrinfo() instead (because of error -1 = 255.255.255.255) |
9
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
126 |
s->remoteAddress.sin_port = htons(std::stoi(findOption(options, OPTION_PORT, true))); |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
127 |
|
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
128 |
auto protocol = findOption(options, OPTION_PROTOCOL); |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
129 |
if (protocol == PROTOCOL_SCTP) { |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
130 |
s->protocol = IPPROTO_SCTP; |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
131 |
s->type = SOCK_SEQPACKET; |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
132 |
} |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
133 |
|
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
134 |
return s; |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
135 |
} |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
136 |
|
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
137 |
void send(const std::string& message) override { |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
138 |
FD s(::socket(AF_INET, type, protocol)); |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
139 |
sendto(s.getFD(), message.c_str(), message.size(), 0, (sockaddr*) & remoteAddress, sizeof (remoteAddress)); |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
140 |
} |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
141 |
|
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
142 |
const std::string receive() override { |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
143 |
// TODO: TCP receive() |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
144 |
return "TODO: receive() a message"; |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
145 |
} |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
146 |
|
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
147 |
}; |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
148 |
|
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
149 |
class TCPClientSocket : public Socket { |
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
150 |
private: |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
151 |
struct sockaddr_in remoteAddress; |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
152 |
public: |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
153 |
|
5
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
154 |
static std::shared_ptr<Socket> open(const SocketOptions& options) { |
9
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
155 |
std::shared_ptr<TCPClientSocket> s = std::make_shared<TCPClientSocket>(); |
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
156 |
memset((char *) &s->remoteAddress, 0, sizeof (s->remoteAddress)); |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
157 |
s->remoteAddress.sin_family = AF_INET; |
10
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
158 |
s->remoteAddress.sin_addr.s_addr = getAddress(findOption(options, OPTION_HOST, true).c_str()); // TODO: use getaddrinfo() instead (because of error -1 = 255.255.255.255) |
5
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
159 |
s->remoteAddress.sin_port = htons(std::stoi(findOption(options, OPTION_PORT, true))); |
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
160 |
return s; |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
161 |
} |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
162 |
|
2 | 163 |
void send(const std::string& message) override { |
9
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
164 |
FD s(::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)); |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
165 |
check(::connect(s.getFD(), (sockaddr*) & remoteAddress, sizeof (remoteAddress)), "connect socket"); |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
166 |
ssize_t written = ::write(s.getFD(), message.c_str(), message.size()); |
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
167 |
if (written != message.size()) throw std::logic_error("writing to the socket failed"); |
2 | 168 |
} |
0 | 169 |
|
2 | 170 |
const std::string receive() override { |
171 |
// TODO: TCP receive() |
|
172 |
return "TODO: receive() a message"; |
|
0 | 173 |
} |
174 |
||
9
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
175 |
|
0 | 176 |
}; |
177 |
||
5
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
178 |
template<const char* protocol, const char* role, const char* mode, typename SocketClass> |
3
e701e06ff561
add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
179 |
class TemplateSocketFactory : public SocketFactory { |
2 | 180 |
public: |
181 |
||
5
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
182 |
bool canHandle(const SocketOptions& options) override { |
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
183 |
return findOption(options, OPTION_PROTOCOL) == protocol |
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
184 |
&& findOption(options, OPTION_ROLE) == role |
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
185 |
&& findOption(options, OPTION_MODE) == mode; |
2 | 186 |
} |
187 |
||
5
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
188 |
std::shared_ptr<Socket> open(const SocketOptions& options) override { |
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
189 |
return SocketClass::open(options); |
2 | 190 |
} |
3
e701e06ff561
add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
191 |
}; |
2 | 192 |
|
4
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
193 |
static std::vector<std::shared_ptr<SocketFactory>> factories |
8d036e5e5fcc
configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
194 |
{ |
9
de48b9278211
TCP and UDP client sockets - first version
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
195 |
std::make_shared<TemplateSocketFactory<PROTOCOL_TCP, ROLE_CLIENT, MODE_STREAM, TCPClientSocket >> (), |
10
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
196 |
std::make_shared<TemplateSocketFactory<PROTOCOL_TCP, ROLE_SERVER, MODE_STREAM, TCPClientSocket >> (), // TODO: correct class |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
197 |
std::make_shared<TemplateSocketFactory<PROTOCOL_UDP, ROLE_CLIENT, MODE_DATAGRAM, UDPClientSocket >> (), |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
198 |
std::make_shared<TemplateSocketFactory<PROTOCOL_UDP, ROLE_SERVER, MODE_DATAGRAM, UDPClientSocket >> (), // TODO: correct class |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
199 |
std::make_shared<TemplateSocketFactory<PROTOCOL_SCTP, ROLE_CLIENT, MODE_STREAM, UDPClientSocket >> (), // TODO: correct class |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
200 |
std::make_shared<TemplateSocketFactory<PROTOCOL_SCTP, ROLE_CLIENT, MODE_DATAGRAM, UDPClientSocket >> (), // TODO: correct class |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
201 |
std::make_shared<TemplateSocketFactory<PROTOCOL_SCTP, ROLE_SERVER, MODE_STREAM, UDPClientSocket >> (), // TODO: correct class |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
202 |
std::make_shared<TemplateSocketFactory<PROTOCOL_SCTP, ROLE_SERVER, MODE_DATAGRAM, UDPClientSocket >> (), // TODO: correct class |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
203 |
std::make_shared<TemplateSocketFactory<PROTOCOL_UDS, ROLE_CLIENT, MODE_STREAM, UDPClientSocket >> (), // TODO: correct class |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
204 |
std::make_shared<TemplateSocketFactory<PROTOCOL_UDS, ROLE_CLIENT, MODE_DATAGRAM, UDPClientSocket >> (), // TODO: correct class |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
205 |
std::make_shared<TemplateSocketFactory<PROTOCOL_UDS, ROLE_SERVER, MODE_STREAM, UDPClientSocket >> (), // TODO: correct class |
6a6b93507856
use getaddrinfo(), inet_ntop(), some DNS and IPv4 support
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
206 |
std::make_shared<TemplateSocketFactory<PROTOCOL_UDS, ROLE_SERVER, MODE_DATAGRAM, UDPClientSocket >> (), // TODO: correct class |
3
e701e06ff561
add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
207 |
}; |
2 | 208 |
|
5
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
209 |
std::shared_ptr<SocketFactory> SocketFactory::find(const SocketOptions& options) { |
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
210 |
for (auto f : factories) if (f->canHandle(options)) return f; |
e57e2a2798b2
configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
211 |
throw std::logic_error("Unable to find a SocketFactory"); // TODO: add relevant options? |
2 | 212 |
} |
213 |
||
214 |
||
0 | 215 |
} |
216 |
} |
|
217 |
} |