Skip to content

Commit

Permalink
Add mchello to test the hello command
Browse files Browse the repository at this point in the history
Change-Id: I91873f793b6d4d99ea200861796c256e5dca5bfd
Reviewed-on: http://review.couchbase.org/33245
Reviewed-by: Trond Norbye <trond.norbye@gmail.com>
Tested-by: Trond Norbye <trond.norbye@gmail.com>
  • Loading branch information
trondn committed Feb 5, 2014
1 parent d62d240 commit a066110
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
/mcbasher
/memcached
/mcstat
/mchello
/memcached_sizes
/memcached_testapp
/timedrun
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ ADD_EXECUTABLE(cbsasladm programs/cbsasladm.c
ADD_EXECUTABLE(mcstat programs/mcstat.c
programs/utilities.c
programs/utilities.h)
ADD_EXECUTABLE(mchello programs/mchello.c
programs/utilities.c
programs/utilities.h)
ADD_EXECUTABLE(memcached_sizes programs/sizes.c)
ADD_EXECUTABLE(memcached
daemon/alloc_hooks.c
Expand Down Expand Up @@ -136,6 +139,7 @@ TARGET_LINK_LIBRARIES(engine_testapp mcd_util platform ${COUCHBASE_NETWORK_LIBS}
TARGET_LINK_LIBRARIES(bucket_engine_testapp mcd_util platform ${COUCHBASE_NETWORK_LIBS} ${COUCHBASE_MATH_LIBS})
TARGET_LINK_LIBRARIES(cbsasladm platform ${OPENSSL_LIBRARIES} ${COUCHBASE_NETWORK_LIBS})
TARGET_LINK_LIBRARIES(mcstat platform ${OPENSSL_LIBRARIES} ${COUCHBASE_NETWORK_LIBS})
TARGET_LINK_LIBRARIES(mchello platform ${OPENSSL_LIBRARIES} ${COUCHBASE_NETWORK_LIBS})
TARGET_LINK_LIBRARIES(tap_mock_engine platform ${COUCHBASE_NETWORK_LIBS})

TARGET_LINK_LIBRARIES(mcd_util platform)
Expand Down
6 changes: 6 additions & 0 deletions include/memcached/protocol_binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,12 @@ extern "C"
PROTOCOL_BINARY_FEATURE_DATATYPE = 0x01
} protocol_binary_hello_features;

#define MEMCACHED_FIRST_HELLO_FEATURE 0x01
#define MEMCACHED_TOTAL_HELLO_FEATURES 0x01

#define protocol_feature_2_text(a) \
(a == PROTOCOL_BINARY_FEATURE_DATATYPE) ? "Datatype" : "Unknown"

/**
* The HELLO command is used by the client and the server to agree
* upon the set of features the other end supports. It is initiated
Expand Down
119 changes: 119 additions & 0 deletions programs/mchello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#include "config.h"

#include <memcached/protocol_binary.h>
#include <memcached/openssl.h>
#include <platform/platform.h>

#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>

#include "utilities.h"

/**
* Request all features from the server and dump the available ones
* @param sock socket connected to the server
* @param key the name of the stat to receive (NULL == ALL)
*/
static void request_hello(BIO *bio, const char *key)
{
protocol_binary_request_hello req;
protocol_binary_response_hello res;
char buffer[1024];
const char useragent[] = "mchello v1.0";
uint16_t nkey = (uint16_t)strlen(useragent);
uint16_t features[MEMCACHED_TOTAL_HELLO_FEATURES];
uint32_t total = nkey + MEMCACHED_TOTAL_HELLO_FEATURES * 2;
int ii;

memset(&req, 0, sizeof(req));
req.message.header.request.magic = PROTOCOL_BINARY_REQ;
req.message.header.request.opcode = PROTOCOL_BINARY_CMD_HELLO;
req.message.header.request.bodylen = htonl(total);
req.message.header.request.keylen = htons(nkey);
req.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES;

ensure_send(bio, &req, sizeof(req));
ensure_send(bio, useragent, nkey);

for (ii = 0; ii < MEMCACHED_TOTAL_HELLO_FEATURES; ++ii) {
features[ii] = htons(MEMCACHED_FIRST_HELLO_FEATURE + ii);
}
ensure_send(bio, &features, MEMCACHED_TOTAL_HELLO_FEATURES * 2);
ensure_recv(bio, &res, sizeof(res.bytes));
total = ntohl(res.message.header.response.bodylen);
ensure_recv(bio, buffer, total);

if (res.message.header.response.status != 0) {
fprintf(stderr, "Got return value: %d\n",
ntohs(res.message.header.response.status));
return;
}

memcpy(features, buffer, total);
total /= 2;

fprintf(stdout, "Features:\n");
for (ii = 0; ii < total; ++ii) {
fprintf(stderr, "\t- %s\n",
protocol_feature_2_text(ntohs(features[ii])));
}
}

int main(int argc, char** argv) {
int cmd;
const char *port = "11210";
const char *host = "localhost";
const char *user = NULL;
const char *pass = NULL;
int secure = 0;
char *ptr;
SSL_CTX* ctx;
BIO* bio;

/* Initialize the socket subsystem */
cb_initialize_sockets();

while ((cmd = getopt(argc, argv, "h:p:u:P:s")) != EOF) {
switch (cmd) {
case 'h' :
host = optarg;
ptr = strchr(optarg, ':');
if (ptr != NULL) {
*ptr = '\0';
port = ptr + 1;
}
break;
case 'p':
port = optarg;
break;
case 'u' :
user = optarg;
break;
case 'P':
pass = optarg;
break;
case 's':
secure = 1;
break;
default:
fprintf(stderr,
"Usage mchello [-h host[:port]] [-p port] [-u user] [-p pass] [-s]\n");
return 1;
}
}

if (create_ssl_connection(&ctx, &bio, host, port, user, pass, secure) != 0) {
return 1;
}

request_hello(bio, NULL);

BIO_free_all(bio);
if (secure) {
SSL_CTX_free(ctx);
}

return EXIT_SUCCESS;
}

0 comments on commit a066110

Please sign in to comment.