2006-11-28 03:51:33 +03:00
/*
ctdb over TCP
Copyright ( C ) Andrew Tridgell 2006
This library is free software ; you can redistribute it and / or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation ; either
version 2 of the License , or ( at your option ) any later version .
This library 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
Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public
License along with this library ; if not , write to the Free Software
Foundation , Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
*/
# include "includes.h"
# include "lib/events/events.h"
2007-01-23 03:38:45 +03:00
# include "lib/util/dlinklist.h"
# include "lib/tdb/include/tdb.h"
2006-11-28 03:51:33 +03:00
# include "system/network.h"
# include "system/filesys.h"
2007-01-23 03:38:45 +03:00
# include "../include/ctdb_private.h"
2006-11-28 03:51:33 +03:00
# include "ctdb_tcp.h"
2006-12-19 04:03:10 +03:00
2006-11-28 06:15:46 +03:00
/*
2007-04-10 13:33:21 +04:00
called when a complete packet has come in
*/
void ctdb_tcp_read_cb ( uint8_t * data , size_t cnt , void * args )
2006-11-28 06:15:46 +03:00
{
2007-04-10 13:33:21 +04:00
struct ctdb_incoming * in = talloc_get_type ( args , struct ctdb_incoming ) ;
struct ctdb_req_header * hdr ;
2006-11-28 06:15:46 +03:00
2007-04-10 13:33:21 +04:00
if ( data = = NULL ) {
/* incoming socket has died */
talloc_free ( in ) ;
2006-11-28 06:15:46 +03:00
return ;
}
2007-04-10 07:17:15 +04:00
if ( cnt < sizeof ( * hdr ) ) {
2007-04-28 13:35:49 +04:00
ctdb_set_error ( in - > ctdb , " Bad packet length %u \n " , ( unsigned ) cnt ) ;
2006-11-28 06:15:46 +03:00
return ;
}
2007-04-10 07:17:15 +04:00
hdr = ( struct ctdb_req_header * ) data ;
if ( cnt ! = hdr - > length ) {
2007-04-28 13:35:49 +04:00
ctdb_set_error ( in - > ctdb , " Bad header length %u expected %u \n " ,
( unsigned ) hdr - > length , ( unsigned ) cnt ) ;
2006-11-28 06:15:46 +03:00
return ;
2006-11-28 03:51:33 +03:00
}
2006-11-28 06:15:46 +03:00
2007-04-10 07:17:15 +04:00
if ( hdr - > ctdb_magic ! = CTDB_MAGIC ) {
ctdb_set_error ( in - > ctdb , " Non CTDB packet rejected \n " ) ;
2006-11-28 06:15:46 +03:00
return ;
}
2007-04-10 07:17:15 +04:00
if ( hdr - > ctdb_version ! = CTDB_VERSION ) {
ctdb_set_error ( in - > ctdb , " Bad CTDB version 0x%x rejected \n " , hdr - > ctdb_version ) ;
2006-12-19 04:03:10 +03:00
return ;
}
2007-04-10 07:17:15 +04:00
/* most common case - we got a whole packet in one go
tell the ctdb layer above that we have a packet */
in - > ctdb - > upcalls - > recv_pkt ( in - > ctdb , data , cnt ) ;
}
2006-12-19 04:03:10 +03:00
2006-11-28 06:15:46 +03:00
/*
queue a packet for sending
*/
int ctdb_tcp_queue_pkt ( struct ctdb_node * node , uint8_t * data , uint32_t length )
{
2007-04-11 22:12:15 +04:00
struct ctdb_tcp_node * tnode = talloc_get_type ( node - > private_data ,
2006-11-28 06:15:46 +03:00
struct ctdb_tcp_node ) ;
2007-04-10 13:33:21 +04:00
return ctdb_queue_send ( tnode - > queue , data , length ) ;
2006-11-28 06:15:46 +03:00
}