2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
16 #include "utilities.h"
19 long int timedif(struct timeval a
, struct timeval b
)
23 us
= (int)(a
.tv_usec
- b
.tv_usec
);
25 s
= (int)(a
.tv_sec
- b
.tv_sec
);
30 void version_command(const char *command_name
)
32 printf("%s v%u.%u\n", command_name
, 1U, 0U);
36 static const char *lookup_help(memcached_options option
)
40 case OPT_SERVERS
: return("List which servers you wish to connect to.");
41 case OPT_VERSION
: return("Display the version of the application and then exit.");
42 case OPT_HELP
: return("Display this message and then exit.");
43 case OPT_VERBOSE
: return("Give more details on the progression of the application.");
44 case OPT_DEBUG
: return("Provide output only useful for debugging.");
45 case OPT_FLAG
: return("Provide flag information for storage operation.");
46 case OPT_EXPIRE
: return("Set the expire option for the object.");
47 case OPT_SET
: return("Use set command with memcached when storing.");
48 case OPT_REPLACE
: return("Use replace command with memcached when storing.");
49 case OPT_ADD
: return("Use add command with memcached when storing.");
50 case OPT_SLAP_EXECUTE_NUMBER
: return("Number of times to execute the given test.");
51 case OPT_SLAP_INITIAL_LOAD
: return("Number of key pairs to load before executing tests.");
52 case OPT_SLAP_TEST
: return("Test to run (currently \"get\" or \"set\").");
53 case OPT_SLAP_CONCURRENCY
: return("Number of users to simulate with load.");
54 case OPT_SLAP_NON_BLOCK
: return("Set TCP up to use non-blocking IO.");
55 case OPT_SLAP_TCP_NODELAY
: return("Set TCP socket up to use nodelay.");
56 case OPT_FLUSH
: return("Flush servers before running tests.");
57 case OPT_HASH
: return("Select hash type.");
58 case OPT_BINARY
: return("Switch to binary protocol.");
59 case OPT_ANALYZE
: return("Analyze the provided servers.");
60 case OPT_UDP
: return("Use UDP protocol when communicating with server.");
61 case OPT_USERNAME
: return "Username to use for SASL authentication";
62 case OPT_PASSWD
: return "Password to use for SASL authentication";
63 case OPT_FILE
: return "Path to file in which to save result";
64 default: WATCHPOINT_ASSERT(0);
68 return "forgot to document this function :)";
71 void help_command(const char *command_name
, const char *description
,
72 const struct option
*long_options
,
73 memcached_programs_help_st
*options
__attribute__((unused
)))
77 printf("%s v%u.%u\n\n", command_name
, 1U, 0U);
78 printf("\t%s\n\n", description
);
79 printf("Current options. A '=' means the option takes a value.\n\n");
81 for (x
= 0; long_options
[x
].name
; x
++)
83 const char *help_message
;
85 printf("\t --%s%c\n", long_options
[x
].name
,
86 long_options
[x
].has_arg
? '=' : ' ');
87 if ((help_message
= lookup_help(long_options
[x
].val
)))
88 printf("\t\t%s\n", help_message
);
95 void process_hash_option(memcached_st
*memc
, char *opt_hash
)
98 memcached_return_t rc
;
100 if (opt_hash
== NULL
)
103 set
= MEMCACHED_HASH_DEFAULT
; /* Just here to solve warning */
104 if (!strcasecmp(opt_hash
, "CRC"))
105 set
= MEMCACHED_HASH_CRC
;
106 else if (!strcasecmp(opt_hash
, "FNV1_64"))
107 set
= MEMCACHED_HASH_FNV1_64
;
108 else if (!strcasecmp(opt_hash
, "FNV1A_64"))
109 set
= MEMCACHED_HASH_FNV1A_64
;
110 else if (!strcasecmp(opt_hash
, "FNV1_32"))
111 set
= MEMCACHED_HASH_FNV1_32
;
112 else if (!strcasecmp(opt_hash
, "FNV1A_32"))
113 set
= MEMCACHED_HASH_FNV1A_32
;
116 fprintf(stderr
, "hash: type not recognized %s\n", opt_hash
);
120 rc
= memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_HASH
, set
);
121 if (rc
!= MEMCACHED_SUCCESS
)
123 fprintf(stderr
, "hash: memcache error %s\n", memcached_strerror(memc
, rc
));
128 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
129 static char *username
;
132 static int get_username(void *context
, int id
, const char **result
,
136 if (!result
|| (id
!= SASL_CB_USER
&& id
!= SASL_CB_AUTHNAME
))
137 return SASL_BADPARAM
;
141 *len
= (username
== NULL
) ? 0 : (unsigned int)strlen(username
);
146 static int get_password(sasl_conn_t
*conn
, void *context
, int id
,
147 sasl_secret_t
**psecret
)
150 static sasl_secret_t
* x
;
152 if (!conn
|| ! psecret
|| id
!= SASL_CB_PASS
)
153 return SASL_BADPARAM
;
161 size_t len
= strlen(passwd
);
162 x
= realloc(x
, sizeof(sasl_secret_t
) + len
);
167 strcpy((void *)x
->data
, passwd
);
173 /* callbacks we support */
174 static sasl_callback_t sasl_callbacks
[] = {
176 SASL_CB_USER
, &get_username
, NULL
178 SASL_CB_AUTHNAME
, &get_username
, NULL
180 SASL_CB_PASS
, &get_password
, NULL
182 SASL_CB_LIST_END
, NULL
, NULL
187 bool initialize_sasl(memcached_st
*memc
, char *user
, char *password
)
189 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
190 if (user
!= NULL
&& password
!= NULL
)
195 if (sasl_client_init(NULL
) != SASL_OK
)
197 fprintf(stderr
, "Failed to initialize sasl library!\n");
200 memcached_set_sasl_callbacks(memc
, sasl_callbacks
);
211 void shutdown_sasl(void)
213 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
214 if (username
!= NULL
|| passwd
!= NULL
)
219 void initialize_sockets(void)
221 /* Define the function for all platforms to avoid #ifdefs in each program */
224 if (WSAStartup(MAKEWORD(2,0), &wsaData
) != 0)
226 fprintf(stderr
, "Socket Initialization Error. Program aborted\n");