src/Socket.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 30 Jul 2022 11:33:56 +0200
branchv_0
changeset 7 e6f005f3edfe
parent 5 e57e2a2798b2
child 9 de48b9278211
permissions -rw-r--r--
move options/constants from Configuration.h to Socket.h (avoid dependency from Socket to Configuration)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    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
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    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>
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    28
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    29
#include "Socket.h"
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
namespace relpipe {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
namespace out {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
namespace socket {
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    35
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
    36
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    37
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
    38
	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
    39
	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
    40
	else return defaultValue;
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    41
}
3
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    42
4
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    43
class FD {
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    44
private:
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    45
	int fd;
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
public:
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
4
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    48
	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
    49
	};
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    50
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    51
	virtual ~FD() {
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    52
		close(fd);
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    53
	}
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    54
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    55
	int getFD() {
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    56
		return fd;
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    57
	}
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    58
};
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
class UDPSocket : public Socket {
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    61
private:
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    62
	struct sockaddr_in remoteAddress;
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    63
public:
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    64
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    65
	static std::shared_ptr<Socket> open(const SocketOptions& options) {
4
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    66
		std::shared_ptr<UDPSocket> s = std::make_shared<UDPSocket>();
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    67
		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
    68
		s->remoteAddress.sin_family = AF_INET;
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    69
		s->remoteAddress.sin_addr.s_addr = inet_addr(findOption(options, OPTION_HOST, true).c_str()); // TODO: use getaddrinfo() instead (because of error -1 = 255.255.255.255)
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    70
		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
    71
		return s;
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    72
	}
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    73
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    74
	void send(const std::string& message) override {
4
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    75
		FD s(::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP));
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    76
		sendto(s.getFD(), message.c_str(), message.size(), 0, (sockaddr*) & remoteAddress, sizeof (remoteAddress));
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    77
	}
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    79
	const std::string receive() override {
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    80
		// TODO: TCP receive()
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    81
		return "TODO: receive() a message";
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
	}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
};
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    86
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
    87
class TemplateSocketFactory : public SocketFactory {
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    88
public:
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    89
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    90
	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
    91
		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
    92
				&& 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
    93
				&& findOption(options, OPTION_MODE) == mode;
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    94
	}
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    95
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    96
	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
    97
		return SocketClass::open(options);
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    98
	}
3
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    99
};
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   100
4
8d036e5e5fcc configuration: --connection-string --connection-option (role, mode)
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   101
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
   102
{
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   103
	// FIXME: different classes than UDPSocket
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   104
	std::make_shared<TemplateSocketFactory<PROTOCOL_TCP, ROLE_CLIENT, MODE_STREAM, UDPSocket >> (),
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   105
	std::make_shared<TemplateSocketFactory<PROTOCOL_TCP, ROLE_SERVER, MODE_STREAM, UDPSocket >> (),
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   106
	std::make_shared<TemplateSocketFactory<PROTOCOL_UDP, ROLE_CLIENT, MODE_DATAGRAM, UDPSocket >> (),
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   107
	std::make_shared<TemplateSocketFactory<PROTOCOL_UDP, ROLE_SERVER, MODE_DATAGRAM, UDPSocket >> (),
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   108
	std::make_shared<TemplateSocketFactory<PROTOCOL_SCTP, ROLE_CLIENT, MODE_STREAM, UDPSocket >> (),
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   109
	std::make_shared<TemplateSocketFactory<PROTOCOL_SCTP, ROLE_CLIENT, MODE_DATAGRAM, UDPSocket >> (),
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   110
	std::make_shared<TemplateSocketFactory<PROTOCOL_SCTP, ROLE_SERVER, MODE_STREAM, UDPSocket >> (),
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   111
	std::make_shared<TemplateSocketFactory<PROTOCOL_SCTP, ROLE_SERVER, MODE_DATAGRAM, UDPSocket >> (),
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   112
	std::make_shared<TemplateSocketFactory<PROTOCOL_UDS, ROLE_CLIENT, MODE_STREAM, UDPSocket >> (),
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   113
	std::make_shared<TemplateSocketFactory<PROTOCOL_UDS, ROLE_CLIENT, MODE_DATAGRAM, UDPSocket >> (),
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   114
	std::make_shared<TemplateSocketFactory<PROTOCOL_UDS, ROLE_SERVER, MODE_STREAM, UDPSocket >> (),
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   115
	std::make_shared<TemplateSocketFactory<PROTOCOL_UDS, ROLE_SERVER, MODE_DATAGRAM, UDPSocket >> (),
3
e701e06ff561 add some factories: generic/template version
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   116
};
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   117
5
e57e2a2798b2 configuration: protocol, role, mode, host, port, path
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   118
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
   119
	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
   120
	throw std::logic_error("Unable to find a SocketFactory"); // TODO: add relevant options?
2
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   121
}
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   122
fb399fd4f053 add some factories
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   123
0
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   125
}
924e354948df establish project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
}