MXS-2197: Make SERV_LISTENER a C++ class

The class is still mostly the same as the old C version but it now uses
std::string instead of char pointers. Changed configuration default values
so that the parameters passed to the listener allocation are always valid.
This commit is contained in:
Markus Mäkelä
2018-11-28 14:22:12 +02:00
parent 77585bdb8c
commit 01e9f71af1
15 changed files with 353 additions and 367 deletions

View File

@ -23,6 +23,8 @@
#include <maxbase/jansson.h>
#include <maxscale/buffer.h>
class SERV_LISTENER;
MXS_BEGIN_DECLS
/**
@ -44,7 +46,6 @@ typedef enum authenticator_capability
struct dcb;
struct server;
struct session;
struct servlistener;
/**
* @verbatim
@ -96,8 +97,8 @@ typedef struct mxs_authenticator
int (* authenticate)(struct dcb*);
void (* free)(struct dcb*);
void (* destroy)(void*);
int (* loadusers)(struct servlistener*);
void (* diagnostic)(struct dcb*, struct servlistener*);
int (* loadusers)(SERV_LISTENER*);
void (* diagnostic)(struct dcb*, SERV_LISTENER*);
/**
* @brief Return diagnostic information about the authenticator
@ -111,7 +112,7 @@ typedef struct mxs_authenticator
*
* @see jansson.h
*/
json_t* (*diagnostic_json)(const struct servlistener* listener);
json_t* (*diagnostic_json)(const SERV_LISTENER * listener);
/** This entry point was added to avoid calling authenticator functions
* directly when a COM_CHANGE_USER command is executed. */

View File

@ -26,14 +26,15 @@
#include <maxscale/protocol.h>
#include <maxscale/ssl.h>
class SERVICE;
class SERV_LISTENER;
MXS_BEGIN_DECLS
#define ERRHANDLE
struct session;
struct server;
struct service;
struct servlistener;
struct dcb;
@ -188,7 +189,7 @@ typedef struct dcb
size_t protocol_packet_length; /**< How long the protocol specific packet is */
size_t protocol_bytes_processed; /**< How many bytes of a packet have been read */
struct session* session; /**< The owning session */
struct servlistener* listener; /**< For a client DCB, the listener data */
SERV_LISTENER* listener; /**< For a client DCB, the listener data */
MXS_PROTOCOL func; /**< The protocol functions for this descriptor */
MXS_AUTHENTICATOR authfunc; /**< The authenticator functions for this descriptor
* */
@ -201,26 +202,26 @@ typedef struct dcb
GWBUF* fakeq; /**< Fake event queue for generated events */
uint32_t fake_event; /**< Fake event to be delivered to handler */
DCBSTATS stats; /**< DCB related statistics */
struct dcb* nextpersistent; /**< Next DCB in the persistent pool for SERVER */
time_t persistentstart; /**< 0: Not in the persistent pool.
* -1: Evicted from the persistent pool and being closed.
* non-0: Time when placed in the persistent pool.
*/
struct service* service; /**< The related service */
void* data; /**< Specific client data, shared between DCBs of this session */
void* authenticator_data; /**< The authenticator data for this DCB */
DCB_CALLBACK* callbacks; /**< The list of callbacks for the DCB */
int64_t last_read; /*< Last time the DCB received data */
struct server* server; /**< The associated backend server */
SSL* ssl; /*< SSL struct for connection */
bool ssl_read_want_read; /*< Flag */
bool ssl_read_want_write; /*< Flag */
bool ssl_write_want_read; /*< Flag */
bool ssl_write_want_write; /*< Flag */
bool was_persistent; /**< Whether this DCB was in the persistent pool */
bool high_water_reached; /** High water mark reached, to determine whether need release
* throttle */
DCBSTATS stats; /**< DCB related statistics */
struct dcb* nextpersistent; /**< Next DCB in the persistent pool for SERVER */
time_t persistentstart; /**< 0: Not in the persistent pool.
* -1: Evicted from the persistent pool and being closed.
* non-0: Time when placed in the persistent pool.
*/
SERVICE* service; /**< The related service */
void* data; /**< Specific client data, shared between DCBs of this session */
void* authenticator_data; /**< The authenticator data for this DCB */
DCB_CALLBACK* callbacks; /**< The list of callbacks for the DCB */
int64_t last_read; /*< Last time the DCB received data */
struct server* server; /**< The associated backend server */
SSL* ssl; /*< SSL struct for connection */
bool ssl_read_want_read; /*< Flag */
bool ssl_read_want_write; /*< Flag */
bool ssl_write_want_read; /*< Flag */
bool ssl_write_want_write;/*< Flag */
bool was_persistent; /**< Whether this DCB was in the persistent pool */
bool high_water_reached; /** High water mark reached, to determine whether need release
* throttle */
struct
{
struct dcb* next; /**< Next DCB in owning thread's list */
@ -260,7 +261,7 @@ void dcb_global_init();
int dcb_write(DCB*, GWBUF*);
DCB* dcb_accept(DCB* listener);
DCB* dcb_alloc(dcb_role_t, struct servlistener*);
DCB* dcb_alloc(dcb_role_t, SERV_LISTENER*);
DCB* dcb_connect(struct server*, struct session*, const char*);
int dcb_read(DCB*, GWBUF**, int);
int dcb_drain_writeq(DCB*);

View File

@ -1,162 +0,0 @@
/*
* Copyright (c) 2018 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2022-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#pragma once
/**
* @file listener.h
*/
#include <maxscale/cdefs.h>
#include <maxbase/jansson.h>
#include <maxscale/protocol.h>
#include <maxscale/ssl.h>
MXS_BEGIN_DECLS
struct dcb;
struct service;
/**
* The servlistener structure is used to link a service to the protocols that
* are used to support that service. It defines the name of the protocol module
* that should be loaded to support the client connection and the port that the
* protocol should use to listen for incoming client connections.
*/
typedef struct servlistener
{
char* name; /**< Name of the listener */
char* protocol; /**< Protocol module to load */
unsigned short port; /**< Port to listen on */
char* address; /**< Address to listen with */
char* authenticator; /**< Name of authenticator */
char* auth_options; /**< Authenticator options */
void* auth_instance; /**< Authenticator instance created in MXS_AUTHENTICATOR::initialize()
* */
SSL_LISTENER* ssl; /**< Structure of SSL data or NULL */
struct dcb* listener; /**< The DCB for the listener */
struct users* users; /**< The user data for this listener */
struct service* service; /**< The service which used by this listener */
pthread_mutex_t lock;
int active; /**< True if the port has not been deleted */
struct servlistener* next; /**< Next service protocol */
} SERV_LISTENER; // TODO: Rename to LISTENER
typedef struct listener_iterator
{
SERV_LISTENER* current;
} LISTENER_ITERATOR;
/**
* @brief Serialize a listener to a file
*
* This converts @c listener into an INI format file. This allows created listeners
* to be persisted to disk. This will replace any existing files with the same
* name.
*
* @param listener Listener to serialize
* @return True if the serialization of the listener was successful, false if it fails
*/
bool listener_serialize(const SERV_LISTENER* listener);
/**
* @brief Convert listener to JSON
*
* @param listener Listener to convert
*
* @return Converted listener
*/
json_t* listener_to_json(const SERV_LISTENER* listener);
SERV_LISTENER* listener_alloc(struct service* service,
const char* name,
const char* protocol,
const char* address,
unsigned short port,
const char* authenticator,
const char* auth_options,
SSL_LISTENER* ssl);
void listener_free(SERV_LISTENER* listener);
int listener_set_ssl_version(SSL_LISTENER* ssl_listener, const char* version);
void listener_set_certificates(SSL_LISTENER* ssl_listener, char* cert, char* key, char* ca_cert);
/**
* Initialize SSL configuration
*
* This sets up the generated RSA encryption keys, chooses the listener
* encryption level and configures the listener certificate, private key and
* certificate authority file.
*
* @note This function should not be called directly, use config_create_ssl() instead
*
* @todo Combine this with config_create_ssl() into one function
*
* @param ssl SSL configuration to initialize
*
* @return True on success, false on error
*/
bool SSL_LISTENER_init(SSL_LISTENER* ssl);
/**
* Free an SSL_LISTENER
*
* @param ssl SSL_LISTENER to free
*/
void SSL_LISTENER_free(SSL_LISTENER* ssl);
/**
* @brief Check if listener is active
*
* @param listener Listener to check
*
* @return True if listener is active
*/
bool listener_is_active(SERV_LISTENER* listener);
/**
* @brief Modify listener active state
*
* @param listener Listener to modify
* @param active True to activate, false to disable
*/
void listener_set_active(SERV_LISTENER* listener, bool active);
/**
* @brief Initialize a listener iterator for iterating service listeners
*
* @param service Service whose listeners are iterated
* @param iter Pointer to iterator to initialize
*
* @return The first value pointed by the iterator
*/
SERV_LISTENER* listener_iterator_init(const struct service* service, LISTENER_ITERATOR* iter);
/**
* @brief Get the next listener
*
* @param iter Listener iterator
*
* @return The next listener or NULL on end of list
*/
SERV_LISTENER* listener_iterator_next(LISTENER_ITERATOR* iter);
/**
* Get listener state as a string
*
* @param listener Listener to inspect
*
* @return State of the listener as a string
*/
const char* listener_state_to_string(const SERV_LISTENER* listener);
MXS_END_DECLS

View File

@ -0,0 +1,213 @@
/*
* Copyright (c) 2018 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2022-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#pragma once
#include <maxscale/ccdefs.hh>
#include <string>
#include <maxbase/jansson.h>
#include <maxscale/protocol.h>
#include <maxscale/ssl.h>
#include <maxscale/service.hh>
struct dcb;
class SERVICE;
/**
* The Listener class is used to link a network port to a service. It defines the name of the
* protocol module that should be loaded as well as the authenticator that is used.
*/
// TODO: Rename to Listener
class SERV_LISTENER
{
public:
SERV_LISTENER(SERVICE* service, const std::string& name, const std::string& address, uint16_t port,
const std::string& protocol, const std::string& authenticator,
const std::string& auth_opts, void* auth_instance, SSL_LISTENER* ssl);
~SERV_LISTENER();
public:
std::string name; /**< Name of the listener */
std::string protocol; /**< Protocol module to load */
uint16_t port; /**< Port to listen on */
std::string address; /**< Address to listen with */
std::string authenticator;/**< Name of authenticator */
std::string auth_options; /**< Authenticator options */
void* auth_instance;/**< Authenticator instance */
SSL_LISTENER* ssl; /**< Structure of SSL data or NULL */
struct dcb* listener; /**< The DCB for the listener */
struct users* users; /**< The user data for this listener */
SERVICE* service; /**< The service which used by this listener */
int active; /**< True if the port has not been deleted */
SERV_LISTENER* next; /**< Next service protocol */
};
typedef struct listener_iterator
{
SERV_LISTENER* current;
} LISTENER_ITERATOR;
/**
* @brief Serialize a listener to a file
*
* This converts @c listener into an INI format file. This allows created listeners
* to be persisted to disk. This will replace any existing files with the same
* name.
*
* @param listener Listener to serialize
* @return True if the serialization of the listener was successful, false if it fails
*/
bool listener_serialize(const SERV_LISTENER* listener);
/**
* @brief Convert listener to JSON
*
* @param listener Listener to convert
*
* @return Converted listener
*/
json_t* listener_to_json(const SERV_LISTENER* listener);
/**
* Create a new listener
*
* @param service Service where the listener points to
* @param name Name of the listener
* @param protocol Protocol module to use
* @param address The address to listen with
* @param port The port to listen on
* @param authenticator Name of the authenticator to be used
* @param auth_options Authenticator options
* @param ssl SSL configuration
*
* @return New listener or nullptr on error
*/
SERV_LISTENER* listener_alloc(SERVICE* service,
const char* name,
const char* protocol,
const char* address,
unsigned short port,
const char* authenticator,
const char* auth_options,
SSL_LISTENER* ssl);
/**
* @brief Free a listener
*
* The listener must be destroyed before it can be freed.
*
* @param listener Listener to free
*/
void listener_free(SERV_LISTENER* listener);
/**
* Destroy a listener
*
* This deactivates the listener and closes the network port it listens on. Once destroyed, the listener
* can no longer be used.
*
* @param listener Listener to destroy
*/
void listener_destroy(SERV_LISTENER* listener);
/**
* Stop a listener
*
* @param listener Listener to stop
*
* @return True if listener was successfully stopped
*/
bool listener_stop(SERV_LISTENER* listener);
/**
* Start a stopped listener
*
* @param listener Listener to start
*
* @return True if listener was successfully started
*/
bool listener_start(SERV_LISTENER* listener);
int listener_set_ssl_version(SSL_LISTENER* ssl_listener, const char* version);
void listener_set_certificates(SSL_LISTENER* ssl_listener, char* cert, char* key, char* ca_cert);
/**
* Initialize SSL configuration
*
* This sets up the generated RSA encryption keys, chooses the listener
* encryption level and configures the listener certificate, private key and
* certificate authority file.
*
* @note This function should not be called directly, use config_create_ssl() instead
*
* @todo Combine this with config_create_ssl() into one function
*
* @param ssl SSL configuration to initialize
*
* @return True on success, false on error
*/
bool SSL_LISTENER_init(SSL_LISTENER* ssl);
/**
* Free an SSL_LISTENER
*
* @param ssl SSL_LISTENER to free
*/
void SSL_LISTENER_free(SSL_LISTENER* ssl);
/**
* @brief Check if listener is active
*
* @param listener Listener to check
*
* @return True if listener is active
*/
bool listener_is_active(SERV_LISTENER* listener);
/**
* @brief Modify listener active state
*
* @param listener Listener to modify
* @param active True to activate, false to disable
*/
void listener_set_active(SERV_LISTENER* listener, bool active);
/**
* @brief Initialize a listener iterator for iterating service listeners
*
* @param service Service whose listeners are iterated
* @param iter Pointer to iterator to initialize
*
* @return The first value pointed by the iterator
*/
SERV_LISTENER* listener_iterator_init(const SERVICE* service, LISTENER_ITERATOR* iter);
/**
* @brief Get the next listener
*
* @param iter Listener iterator
*
* @return The next listener or NULL on end of list
*/
SERV_LISTENER* listener_iterator_next(LISTENER_ITERATOR* iter);
/**
* Get listener state as a string
*
* @param listener Listener to inspect
*
* @return State of the listener as a string
*/
const char* listener_state_to_string(const SERV_LISTENER* listener);

View File

@ -19,7 +19,7 @@
#include <maxscale/cdefs.h>
#include <maxbase/jansson.h>
#include <maxscale/dcb.h>
#include <maxscale/listener.h>
#include <maxscale/listener.hh>
#include <maxscale/service.hh>
#include <openssl/sha.h>