b51665a69bfc43478e873b1bb4382a86ab099edf
[m6w6/libmemcached] / clients / utilities.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11 #include "config.h"
12
13 #include <stdio.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include "utilities.h"
17
18
19 long int timedif(struct timeval a, struct timeval b)
20 {
21 long us, s;
22
23 us = (int)(a.tv_usec - b.tv_usec);
24 us /= 1000;
25 s = (int)(a.tv_sec - b.tv_sec);
26 s *= 1000;
27 return s + us;
28 }
29
30 void version_command(const char *command_name)
31 {
32 printf("%s v%u.%u\n", command_name, 1U, 0U);
33 exit(0);
34 }
35
36 static const char *lookup_help(memcached_options option)
37 {
38 switch (option)
39 {
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 case OPT_STAT_ARGS: return "Argument for statistics";
65 default: WATCHPOINT_ASSERT(0);
66 };
67
68 WATCHPOINT_ASSERT(0);
69 return "forgot to document this function :)";
70 }
71
72 void help_command(const char *command_name, const char *description,
73 const struct option *long_options,
74 memcached_programs_help_st *options __attribute__((unused)))
75 {
76 unsigned int x;
77
78 printf("%s v%u.%u\n\n", command_name, 1U, 0U);
79 printf("\t%s\n\n", description);
80 printf("Current options. A '=' means the option takes a value.\n\n");
81
82 for (x= 0; long_options[x].name; x++)
83 {
84 const char *help_message;
85
86 printf("\t --%s%c\n", long_options[x].name,
87 long_options[x].has_arg ? '=' : ' ');
88 if ((help_message= lookup_help(long_options[x].val)))
89 printf("\t\t%s\n", help_message);
90 }
91
92 printf("\n");
93 exit(0);
94 }
95
96 void process_hash_option(memcached_st *memc, char *opt_hash)
97 {
98 uint64_t set;
99 memcached_return_t rc;
100
101 if (opt_hash == NULL)
102 return;
103
104 set= MEMCACHED_HASH_DEFAULT; /* Just here to solve warning */
105 if (!strcasecmp(opt_hash, "CRC"))
106 set= MEMCACHED_HASH_CRC;
107 else if (!strcasecmp(opt_hash, "FNV1_64"))
108 set= MEMCACHED_HASH_FNV1_64;
109 else if (!strcasecmp(opt_hash, "FNV1A_64"))
110 set= MEMCACHED_HASH_FNV1A_64;
111 else if (!strcasecmp(opt_hash, "FNV1_32"))
112 set= MEMCACHED_HASH_FNV1_32;
113 else if (!strcasecmp(opt_hash, "FNV1A_32"))
114 set= MEMCACHED_HASH_FNV1A_32;
115 else
116 {
117 fprintf(stderr, "hash: type not recognized %s\n", opt_hash);
118 exit(1);
119 }
120
121 rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
122 if (rc != MEMCACHED_SUCCESS)
123 {
124 fprintf(stderr, "hash: memcache error %s\n", memcached_strerror(memc, rc));
125 exit(1);
126 }
127 }
128
129 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
130 static char *username;
131 static char *passwd;
132
133 static int get_username(void *context, int id, const char **result,
134 unsigned int *len)
135 {
136 (void)context;
137 if (!result || (id != SASL_CB_USER && id != SASL_CB_AUTHNAME))
138 return SASL_BADPARAM;
139
140 *result= username;
141 if (len)
142 *len= (username == NULL) ? 0 : (unsigned int)strlen(username);
143
144 return SASL_OK;
145 }
146
147 static int get_password(sasl_conn_t *conn, void *context, int id,
148 sasl_secret_t **psecret)
149 {
150 (void)context;
151 static sasl_secret_t* x;
152
153 if (!conn || ! psecret || id != SASL_CB_PASS)
154 return SASL_BADPARAM;
155
156 if (passwd == NULL)
157 {
158 *psecret= NULL;
159 return SASL_OK;
160 }
161
162 size_t len= strlen(passwd);
163 x = realloc(x, sizeof(sasl_secret_t) + len);
164 if (!x)
165 return SASL_NOMEM;
166
167 x->len = len;
168 strcpy((void *)x->data, passwd);
169
170 *psecret = x;
171 return SASL_OK;
172 }
173
174 /* callbacks we support */
175 static sasl_callback_t sasl_callbacks[] = {
176 {
177 SASL_CB_USER, &get_username, NULL
178 }, {
179 SASL_CB_AUTHNAME, &get_username, NULL
180 }, {
181 SASL_CB_PASS, &get_password, NULL
182 }, {
183 SASL_CB_LIST_END, NULL, NULL
184 }
185 };
186 #endif
187
188 bool initialize_sasl(memcached_st *memc, char *user, char *password)
189 {
190 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
191 if (user != NULL && password != NULL)
192 {
193 username= user;
194 passwd= password;
195
196 if (sasl_client_init(NULL) != SASL_OK)
197 {
198 fprintf(stderr, "Failed to initialize sasl library!\n");
199 return false;
200 }
201 memcached_set_sasl_callbacks(memc, sasl_callbacks);
202 }
203 #else
204 (void)memc;
205 (void)user;
206 (void)password;
207 #endif
208
209 return true;
210 }
211
212 void shutdown_sasl(void)
213 {
214 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
215 if (username != NULL || passwd != NULL)
216 sasl_done();
217 #endif
218 }