2015-04-14 10:20:05 +03:00
/*
CTDB protocol marshalling
Copyright ( C ) Amitay Isaacs 2015
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; if not , see < http : //www.gnu.org/licenses/>.
*/
# include "replace.h"
# include "system/network.h"
# include <talloc.h>
# include <tdb.h>
# include "protocol.h"
# include "protocol_api.h"
# include "protocol_private.h"
static size_t ctdb_message_data_len ( union ctdb_message_data * mdata ,
uint64_t srvid )
{
size_t len = 0 ;
switch ( srvid ) {
2016-03-17 09:16:09 +03:00
case CTDB_SRVID_BANNING :
2017-06-29 15:14:23 +03:00
len = ctdb_uint32_len ( & mdata - > pnn ) ;
2016-03-17 09:16:09 +03:00
break ;
2015-04-14 10:20:05 +03:00
case CTDB_SRVID_ELECTION :
len = ctdb_election_message_len ( mdata - > election ) ;
break ;
case CTDB_SRVID_RECONFIGURE :
break ;
case CTDB_SRVID_RELEASE_IP :
2017-06-29 11:48:51 +03:00
len = ctdb_string_len ( & mdata - > ipaddr ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_TAKE_IP :
2017-06-29 11:48:51 +03:00
len = ctdb_string_len ( & mdata - > ipaddr ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_SET_NODE_FLAGS :
len = ctdb_node_flag_change_len ( mdata - > flag_change ) ;
break ;
case CTDB_SRVID_RECD_UPDATE_IP :
len = ctdb_public_ip_len ( mdata - > pubip ) ;
break ;
case CTDB_SRVID_VACUUM_FETCH :
len = ctdb_rec_buffer_len ( mdata - > recbuf ) ;
break ;
case CTDB_SRVID_DETACH_DATABASE :
2017-06-29 15:14:23 +03:00
len = ctdb_uint32_len ( & mdata - > db_id ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_MEM_DUMP :
len = ctdb_srvid_message_len ( mdata - > msg ) ;
break ;
2017-07-21 07:40:01 +03:00
case CTDB_SRVID_GETLOG :
break ;
case CTDB_SRVID_CLEARLOG :
break ;
2015-04-14 10:20:05 +03:00
case CTDB_SRVID_PUSH_NODE_FLAGS :
len = ctdb_node_flag_change_len ( mdata - > flag_change ) ;
break ;
case CTDB_SRVID_RELOAD_NODES :
break ;
case CTDB_SRVID_TAKEOVER_RUN :
len = ctdb_srvid_message_len ( mdata - > msg ) ;
break ;
case CTDB_SRVID_REBALANCE_NODE :
2017-06-29 15:14:23 +03:00
len = ctdb_uint32_len ( & mdata - > pnn ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_DISABLE_TAKEOVER_RUNS :
len = ctdb_disable_message_len ( mdata - > disable ) ;
break ;
case CTDB_SRVID_DISABLE_RECOVERIES :
len = ctdb_disable_message_len ( mdata - > disable ) ;
break ;
case CTDB_SRVID_DISABLE_IP_CHECK :
2017-06-29 15:14:23 +03:00
len = ctdb_uint32_len ( & mdata - > timeout ) ;
2015-04-14 10:20:05 +03:00
break ;
2015-11-09 07:58:56 +03:00
default :
2017-06-30 10:15:47 +03:00
len = ctdb_tdb_data_len ( & mdata - > data ) ;
2015-11-09 07:58:56 +03:00
break ;
2015-04-14 10:20:05 +03:00
}
return len ;
}
static void ctdb_message_data_push ( union ctdb_message_data * mdata ,
2017-07-21 07:40:01 +03:00
uint64_t srvid , uint8_t * buf ,
size_t * npush )
2015-04-14 10:20:05 +03:00
{
2017-07-21 07:40:01 +03:00
size_t np = 0 ;
2017-06-29 15:14:23 +03:00
2015-04-14 10:20:05 +03:00
switch ( srvid ) {
2016-03-17 09:16:09 +03:00
case CTDB_SRVID_BANNING :
2017-06-29 15:14:23 +03:00
ctdb_uint32_push ( & mdata - > pnn , buf , & np ) ;
2016-03-17 09:16:09 +03:00
break ;
2015-04-14 10:20:05 +03:00
case CTDB_SRVID_ELECTION :
2017-07-13 07:56:50 +03:00
ctdb_election_message_push ( mdata - > election , buf , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_RECONFIGURE :
break ;
case CTDB_SRVID_RELEASE_IP :
2017-06-29 11:48:51 +03:00
ctdb_string_push ( & mdata - > ipaddr , buf , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_TAKE_IP :
2017-06-29 11:48:51 +03:00
ctdb_string_push ( & mdata - > ipaddr , buf , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_SET_NODE_FLAGS :
2017-06-29 17:42:53 +03:00
ctdb_node_flag_change_push ( mdata - > flag_change , buf , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_RECD_UPDATE_IP :
2017-07-06 07:04:51 +03:00
ctdb_public_ip_push ( mdata - > pubip , buf , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_VACUUM_FETCH :
2017-06-29 16:41:08 +03:00
ctdb_rec_buffer_push ( mdata - > recbuf , buf , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_DETACH_DATABASE :
2017-06-29 15:14:23 +03:00
ctdb_uint32_push ( & mdata - > db_id , buf , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_MEM_DUMP :
2017-07-13 08:01:37 +03:00
ctdb_srvid_message_push ( mdata - > msg , buf , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
2017-07-21 07:40:01 +03:00
case CTDB_SRVID_GETLOG :
break ;
case CTDB_SRVID_CLEARLOG :
break ;
2015-04-14 10:20:05 +03:00
case CTDB_SRVID_PUSH_NODE_FLAGS :
2017-06-29 17:42:53 +03:00
ctdb_node_flag_change_push ( mdata - > flag_change , buf , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_RELOAD_NODES :
break ;
case CTDB_SRVID_TAKEOVER_RUN :
2017-07-13 08:01:37 +03:00
ctdb_srvid_message_push ( mdata - > msg , buf , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_REBALANCE_NODE :
2017-06-29 15:14:23 +03:00
ctdb_uint32_push ( & mdata - > pnn , buf , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_DISABLE_TAKEOVER_RUNS :
2017-07-13 08:09:31 +03:00
ctdb_disable_message_push ( mdata - > disable , buf , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_DISABLE_RECOVERIES :
2017-07-13 08:09:31 +03:00
ctdb_disable_message_push ( mdata - > disable , buf , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_DISABLE_IP_CHECK :
2017-06-29 15:14:23 +03:00
ctdb_uint32_push ( & mdata - > timeout , buf , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
2015-11-09 07:58:56 +03:00
default :
2017-06-30 10:15:47 +03:00
ctdb_tdb_data_push ( & mdata - > data , buf , & np ) ;
2015-11-09 07:58:56 +03:00
break ;
2015-04-14 10:20:05 +03:00
}
2017-07-21 07:40:01 +03:00
* npush = np ;
2015-04-14 10:20:05 +03:00
}
static int ctdb_message_data_pull ( uint8_t * buf , size_t buflen ,
uint64_t srvid , TALLOC_CTX * mem_ctx ,
2017-07-21 07:40:01 +03:00
union ctdb_message_data * mdata ,
size_t * npull )
2015-04-14 10:20:05 +03:00
{
int ret = 0 ;
2017-07-21 07:40:01 +03:00
size_t np = 0 ;
2015-04-14 10:20:05 +03:00
switch ( srvid ) {
2016-03-17 09:16:09 +03:00
case CTDB_SRVID_BANNING :
2017-06-29 15:14:23 +03:00
ret = ctdb_uint32_pull ( buf , buflen , & mdata - > pnn , & np ) ;
2016-03-17 09:16:09 +03:00
break ;
2015-04-14 10:20:05 +03:00
case CTDB_SRVID_ELECTION :
ret = ctdb_election_message_pull ( buf , buflen , mem_ctx ,
2017-07-13 07:56:50 +03:00
& mdata - > election , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_RECONFIGURE :
break ;
case CTDB_SRVID_RELEASE_IP :
2017-06-29 11:48:51 +03:00
ret = ctdb_string_pull ( buf , buflen , mem_ctx , & mdata - > ipaddr ,
& np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_TAKE_IP :
2017-06-29 11:48:51 +03:00
ret = ctdb_string_pull ( buf , buflen , mem_ctx , & mdata - > ipaddr ,
& np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_SET_NODE_FLAGS :
ret = ctdb_node_flag_change_pull ( buf , buflen , mem_ctx ,
2017-06-29 17:42:53 +03:00
& mdata - > flag_change , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_RECD_UPDATE_IP :
ret = ctdb_public_ip_pull ( buf , buflen , mem_ctx ,
2017-07-06 07:04:51 +03:00
& mdata - > pubip , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_VACUUM_FETCH :
ret = ctdb_rec_buffer_pull ( buf , buflen , mem_ctx ,
2017-06-29 16:41:08 +03:00
& mdata - > recbuf , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_DETACH_DATABASE :
2017-06-29 15:14:23 +03:00
ret = ctdb_uint32_pull ( buf , buflen , & mdata - > db_id , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_MEM_DUMP :
ret = ctdb_srvid_message_pull ( buf , buflen , mem_ctx ,
2017-07-13 08:01:37 +03:00
& mdata - > msg , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
2017-07-21 07:40:01 +03:00
case CTDB_SRVID_GETLOG :
break ;
case CTDB_SRVID_CLEARLOG :
break ;
2015-04-14 10:20:05 +03:00
case CTDB_SRVID_PUSH_NODE_FLAGS :
ret = ctdb_node_flag_change_pull ( buf , buflen , mem_ctx ,
2017-06-29 17:42:53 +03:00
& mdata - > flag_change , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_RELOAD_NODES :
break ;
case CTDB_SRVID_TAKEOVER_RUN :
ret = ctdb_srvid_message_pull ( buf , buflen , mem_ctx ,
2017-07-13 08:01:37 +03:00
& mdata - > msg , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_REBALANCE_NODE :
2017-06-29 15:14:23 +03:00
ret = ctdb_uint32_pull ( buf , buflen , & mdata - > pnn , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_DISABLE_TAKEOVER_RUNS :
ret = ctdb_disable_message_pull ( buf , buflen , mem_ctx ,
2017-07-13 08:09:31 +03:00
& mdata - > disable , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_DISABLE_RECOVERIES :
ret = ctdb_disable_message_pull ( buf , buflen , mem_ctx ,
2017-07-13 08:09:31 +03:00
& mdata - > disable , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
case CTDB_SRVID_DISABLE_IP_CHECK :
2017-06-29 15:14:23 +03:00
ret = ctdb_uint32_pull ( buf , buflen , & mdata - > timeout , & np ) ;
2015-04-14 10:20:05 +03:00
break ;
2015-11-09 07:58:56 +03:00
default :
2017-06-30 10:15:47 +03:00
ret = ctdb_tdb_data_pull ( buf , buflen , mem_ctx , & mdata - > data ,
& np ) ;
2015-11-09 07:58:56 +03:00
break ;
2015-04-14 10:20:05 +03:00
}
2017-07-21 07:40:01 +03:00
if ( ret ! = 0 ) {
return ret ;
}
* npull = np ;
return 0 ;
2015-04-14 10:20:05 +03:00
}
2016-04-21 17:45:01 +03:00
size_t ctdb_req_message_len ( struct ctdb_req_header * h ,
struct ctdb_req_message * c )
{
2017-07-21 08:07:30 +03:00
uint32_t u32 = ctdb_message_data_len ( & c - > data , c - > srvid ) ;
return ctdb_req_header_len ( h ) +
ctdb_uint64_len ( & c - > srvid ) +
ctdb_uint32_len ( & u32 ) + u32 ;
2016-04-21 17:45:01 +03:00
}
2015-04-14 10:20:05 +03:00
int ctdb_req_message_push ( struct ctdb_req_header * h ,
2017-07-21 08:07:30 +03:00
struct ctdb_req_message * c ,
2016-05-01 15:13:35 +03:00
uint8_t * buf , size_t * buflen )
2015-04-14 10:20:05 +03:00
{
2017-07-21 08:07:30 +03:00
size_t offset = 0 , np ;
size_t length ;
uint32_t u32 ;
2015-04-14 10:20:05 +03:00
2017-07-21 08:07:30 +03:00
length = ctdb_req_message_len ( h , c ) ;
2016-05-01 15:13:35 +03:00
if ( * buflen < length ) {
* buflen = length ;
2016-04-21 18:08:11 +03:00
return EMSGSIZE ;
2015-04-14 10:20:05 +03:00
}
2016-05-01 15:13:35 +03:00
h - > length = * buflen ;
2017-07-21 08:07:30 +03:00
ctdb_req_header_push ( h , buf + offset , & np ) ;
offset + = np ;
ctdb_uint64_push ( & c - > srvid , buf + offset , & np ) ;
offset + = np ;
u32 = ctdb_message_data_len ( & c - > data , c - > srvid ) ;
ctdb_uint32_push ( & u32 , buf + offset , & np ) ;
offset + = np ;
2015-04-14 10:20:05 +03:00
2017-07-21 08:07:30 +03:00
ctdb_message_data_push ( & c - > data , c - > srvid , buf + offset , & np ) ;
offset + = np ;
2015-04-14 10:20:05 +03:00
return 0 ;
}
2016-04-21 18:25:06 +03:00
int ctdb_req_message_pull ( uint8_t * buf , size_t buflen ,
2015-04-14 10:20:05 +03:00
struct ctdb_req_header * h ,
TALLOC_CTX * mem_ctx ,
2016-04-21 18:25:06 +03:00
struct ctdb_req_message * c )
2015-04-14 10:20:05 +03:00
{
2017-07-21 08:07:30 +03:00
struct ctdb_req_header header ;
size_t offset = 0 , np ;
uint32_t u32 ;
2015-04-14 10:20:05 +03:00
int ret ;
2017-07-21 08:07:30 +03:00
ret = ctdb_req_header_pull ( buf + offset , buflen - offset , & header , & np ) ;
if ( ret ! = 0 ) {
return ret ;
2015-04-14 10:20:05 +03:00
}
2017-07-21 08:07:30 +03:00
offset + = np ;
if ( h ! = NULL ) {
* h = header ;
2016-05-03 05:53:24 +03:00
}
2017-07-21 08:07:30 +03:00
ret = ctdb_uint64_pull ( buf + offset , buflen - offset , & c - > srvid , & np ) ;
if ( ret ! = 0 ) {
return ret ;
2016-05-03 05:53:24 +03:00
}
2017-07-21 08:07:30 +03:00
offset + = np ;
ret = ctdb_uint32_pull ( buf + offset , buflen - offset , & u32 , & np ) ;
if ( ret ! = 0 ) {
return ret ;
2015-04-14 10:20:05 +03:00
}
2017-07-21 08:07:30 +03:00
offset + = np ;
2015-04-14 10:20:05 +03:00
2017-07-21 08:07:30 +03:00
if ( buflen - offset < u32 ) {
return EMSGSIZE ;
2016-03-10 07:43:37 +03:00
}
2015-04-14 10:20:05 +03:00
2017-07-21 08:07:30 +03:00
ret = ctdb_message_data_pull ( buf + offset , u32 , c - > srvid ,
2017-07-21 07:40:01 +03:00
mem_ctx , & c - > data , & np ) ;
2017-07-21 08:07:30 +03:00
if ( ret ! = 0 ) {
return ret ;
}
offset + = np ;
2015-04-14 10:20:05 +03:00
return ret ;
}
2016-04-21 17:45:01 +03:00
size_t ctdb_req_message_data_len ( struct ctdb_req_header * h ,
struct ctdb_req_message_data * c )
{
2017-07-21 08:21:01 +03:00
return ctdb_req_header_len ( h ) +
ctdb_uint64_len ( & c - > srvid ) +
ctdb_tdb_datan_len ( & c - > data ) ;
2016-04-21 17:45:01 +03:00
}
2015-04-14 10:20:05 +03:00
int ctdb_req_message_data_push ( struct ctdb_req_header * h ,
2017-07-21 08:21:01 +03:00
struct ctdb_req_message_data * c ,
2016-05-01 15:13:35 +03:00
uint8_t * buf , size_t * buflen )
2015-04-14 10:20:05 +03:00
{
2017-07-21 08:21:01 +03:00
size_t offset = 0 , np ;
size_t length ;
2015-04-14 10:20:05 +03:00
2017-07-21 08:21:01 +03:00
length = ctdb_req_message_data_len ( h , c ) ;
2016-05-01 15:13:35 +03:00
if ( * buflen < length ) {
* buflen = length ;
2016-04-21 18:08:11 +03:00
return EMSGSIZE ;
2015-04-14 10:20:05 +03:00
}
2016-05-01 15:13:35 +03:00
h - > length = * buflen ;
2017-07-21 08:21:01 +03:00
ctdb_req_header_push ( h , buf + offset , & np ) ;
offset + = np ;
2015-04-14 10:20:05 +03:00
2017-07-21 08:21:01 +03:00
ctdb_uint64_push ( & c - > srvid , buf + offset , & np ) ;
offset + = np ;
ctdb_tdb_datan_push ( & c - > data , buf + offset , & np ) ;
offset + = np ;
2015-04-14 10:20:05 +03:00
return 0 ;
}
2016-04-21 18:25:06 +03:00
int ctdb_req_message_data_pull ( uint8_t * buf , size_t buflen ,
2015-04-14 10:20:05 +03:00
struct ctdb_req_header * h ,
TALLOC_CTX * mem_ctx ,
2016-04-21 18:25:06 +03:00
struct ctdb_req_message_data * c )
2015-04-14 10:20:05 +03:00
{
2017-07-21 08:21:01 +03:00
struct ctdb_req_header header ;
size_t offset = 0 , np ;
2016-04-21 16:50:01 +03:00
int ret ;
2015-04-14 10:20:05 +03:00
2017-07-21 08:21:01 +03:00
ret = ctdb_req_header_pull ( buf + offset , buflen - offset , & header , & np ) ;
if ( ret ! = 0 ) {
return ret ;
2015-04-14 10:20:05 +03:00
}
2017-07-21 08:21:01 +03:00
offset + = np ;
2015-04-14 10:20:05 +03:00
2016-03-10 07:43:37 +03:00
if ( h ! = NULL ) {
2017-07-21 08:21:01 +03:00
* h = header ;
2016-03-10 07:43:37 +03:00
}
2015-04-14 10:20:05 +03:00
2017-07-21 08:21:01 +03:00
ret = ctdb_uint64_pull ( buf + offset , buflen - offset , & c - > srvid , & np ) ;
if ( ret ! = 0 ) {
return ret ;
}
offset + = np ;
2016-04-21 16:50:01 +03:00
2017-07-21 08:21:01 +03:00
ret = ctdb_tdb_datan_pull ( buf + offset , buflen - offset ,
mem_ctx , & c - > data , & np ) ;
2016-04-21 16:50:01 +03:00
if ( ret ! = 0 ) {
return ret ;
2015-04-14 10:20:05 +03:00
}
2017-07-21 08:21:01 +03:00
offset + = np ;
2015-04-14 10:20:05 +03:00
return 0 ;
}