1 /* -*- mode: C -*-
2  *
3  * Copyright (c) 2004-2005 Sean Chittenden <sean@chittenden.org>
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without
8  * restriction, including without limitation the rights to use, copy,
9  * modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 /*
27  * The following copyright is included as the TAILQ_* macros come from
28  * sys/queue.h which has the following LICENSE/Copyright notice.  XXX
29  *
30  * Copyright (c) 1991, 1993
31  *      The Regents of the University of California.  All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  * 4. Neither the name of the University nor the names of its contributors
42  *    may be used to endorse or promote products derived from this software
43  *    without specific prior written permission.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  *
57  *      @(#)queue.h     8.5 (Berkeley) 8/20/94
58  * $FreeBSD: src/sys/sys/queue.h,v 1.58 2004/04/07 04:19:49 imp Exp $
59  */
60 
61 #ifndef MEMCACHE_H
62 #define MEMCACHE_H
63 
64 #include <netdb.h>
65 #include <sys/types.h>
66 #include <sys/time.h>
67 #include <unistd.h>
68 
69 #ifdef __MEMCACHE__
70 # define mc_const
71 #else
72 # define mc_const const
73 #endif
74 
75 #ifdef __cplusplus
76 extern "C" {
77 #endif
78 
79 /* Macros for the platform int definitions */
80 typedef uint16_t u_int16_t;
81 typedef uint32_t u_int32_t;
82 
83 
84 /* Macros for testing versions */
85 #define MEMCACHE_VER		"1.4.0.rc2"
86 #define MEMCACHE_VERNUM		10400
87 #define MEMCACHE_RELDATE	20060220
88 
89 /* A handful of #define's for callbacks that discourage developers
90  * from improperly using the interface. */
91 #define MCM_CALLBACK_CTXT	md597f78ab4a96c3e71c3bb9fe036f37367
92 #define MCM_CALLBACK_KEY	md5783365dee46035fa36d8d9e6fe15da0f
93 #define MCM_CALLBACK_LEN	md54c1f40392e708222b8a25a4337cd05fd
94 #define MCM_CALLBACK_MC		md5646277084faa5a0f511793a728521ef5
95 #define MCM_CALLBACK_PTR	md57f5686851aeded9e00761d4aaaac20ee
96 #define MCM_CALLBACK_RES	md52e8a13a74753effc0f149a3ce900dc28
97 #define MCM_ERR_MASK		md5fc9f4e2249ad88de0081136fef04defa
98 #define MCM_ERR_FUNC_ERR_CTXT	md5096953205b8982964e25927bd8154148
99 #define MCM_ERR_FUNC_MC_CTXT	md521e67c8edeaac774b00d960e230c8686
100 #define MCM_KEY_VALID_KEY	md58926735b19e189407440aa1ba3ab1962
101 #define MCM_KEY_VALID_LEN	md5d6077405f1a7b35ddeac18ccc2a8f4c7
102 
103 /* Our initial read(2) buffer has to be long enough to read the
104  * first line of the response.  ie:
105  *
106  * "VALUE #{'k' * 250} #{2 ** 15} #{2 ** 32}\r\n.length => 275
107  *
108  * However, since we want to avoid the number of system calls
109  * necessary, include trailing part of the protocol in our estimate:
110  *
111  * "\r\nEND\r\n".length => 7
112  *
113  * Which yields a mandatory limit of 282 bytes for a successful
114  * response.  If we wish to try and get lucky with our first read(2)
115  * call and be able to read(2) in small values without making a second
116  * read(2) call, pad this number with a sufficiently large byte value.
117  * If most of your keys are 512B, then a INIT_GET_BUF_SIZE of 794
118  * would be prudent (512 + 282).
119  *
120  * The default value of 4096 means that values less than 724 bytes
121  * will always be read(2) via the first read(2) call.  Increasing this
122  * value to large values is not beneficial.  If a second read(2) call
123  * is necessary, the read(2) will be made with a sufficiently large
124  * buffer already allocated. */
125 #ifndef INIT_GET_BUF_SIZE
126 # define INIT_GET_BUF_SIZE ((size_t)4096)
127 #endif
128 
129 #define MAX_KEY_LEN 250
130 
131 /* Error severity levels */
132 #define MCM_ERR_LVL_NONE	0x00
133 /* INFO: Execution information */
134 #define MCM_ERR_LVL_INFO	0x01
135 /* NOTICE: Routine usage information (ie: deactivated a down server) */
136 #define MCM_ERR_LVL_NOTICE	0x02
137 /* WARN: Problem during execution (ie: invalid data of sorts) */
138 #define MCM_ERR_LVL_WARN	0x04
139 /* ERR: Severe problem during execution (ie: OS system/library call failed) */
140 #define MCM_ERR_LVL_ERR		0x08
141 /* FATAL: Unable to proceed with execution (ie: protocol ) */
142 #define MCM_ERR_LVL_FATAL	0x10
143 
144 /* MC_ compatiblity for severity */
145 #define MC_ERR_LVL_INFO		MCM_ERR_LVL_INFO
146 #define MC_ERR_LVL_NOTICE	MCM_ERR_LVL_NOTICE
147 #define MC_ERR_LVL_WARN		MCM_ERR_LVL_WARN
148 #define MC_ERR_LVL_ERR		MCM_ERR_LVL_ERR
149 #define MC_ERR_LVL_FATAL	MCM_ERR_LVL_FATAL
150 
151 /* Set various error codes */
152 #define MCM_ERR_NONE			 0
153 #define MCM_ERR_ASSERT		 	 1
154 #define MCM_ERR_LIB_SNPRINTF		 2
155 #define MCM_ERR_LIB_STRTOL		 3
156 #define MCM_ERR_LIB_STRTOLL		 4
157 #define MCM_ERR_MC_RECONN		 5
158 #define MCM_ERR_MC_SEND_CMD		 6
159 #define MCM_ERR_MC_SERV_LIST		 7
160 #define MCM_ERR_MC_STORE		 8
161 #define MCM_ERR_MC_VALID_SERVER		 9
162 #define MCM_ERR_MEM_MALLOC		10
163 #define MCM_ERR_MEM_REALLOC		11
164 #define MCM_ERR_NET_CONNECT		12
165 #define MCM_ERR_NET_HOST		13
166 #define MCM_ERR_PROTO			14
167 #define MCM_ERR_SYS_CLOSE		16
168 #define MCM_ERR_SYS_CONNECT		17
169 #define MCM_ERR_SYS_FCNTL		18
170 #define MCM_ERR_SYS_READ		19
171 #define MCM_ERR_SYS_SELECT		20
172 #define MCM_ERR_SYS_SETSOCKOPT		21
173 #define MCM_ERR_SYS_SOCKET		22
174 #define MCM_ERR_SYS_WRITEV		23
175 #define MCM_ERR_TEST			24
176 #define MCM_ERR_TIMEOUT			25
177 #define MCM_ERR_TRACE			26
178 #define MCM_ERR_UNKNOWN_STAT		27
179 
180 
181 /* Various values for _flags.  Use their function counterparts instead
182  * of testing these bits directly (ie: mcm_res_free_on_delete(),
183  * mcm_res_found(), and mcm_res_attempted()). */
184 #define MCM_RES_FREE_ON_DELETE		0x01
185 #define MCM_RES_NO_FREE_ON_DELETE	0x02
186 #define MCM_RES_FOUND			0x04
187 #define MCM_RES_ATTEMPTED		0x08
188 #define MCM_RES_NEED_FREE_KEY		0x10
189 
190 /* Aliases for MCM_RES_* #define's. Use their function counterparts
191  * instead of testing these bits directly (ie:
192  * mc_res_free_on_delete(), mc_res_found(), and
193  * mc_res_attempted()).  */
194 #define MC_RES_FREE_ON_DELETE		MCM_RES_FREE_ON_DELETE
195 #define MC_RES_NO_FREE_ON_DELETE	MCM_RES_NO_FREE_ON_DELETE
196 #define MC_RES_FOUND			MCM_RES_FOUND
197 #define MC_RES_ATTEMPTED		MCM_RES_ATTEMPTED
198 #define MC_RES_NEED_FREE_KEY		MCM_RES_NEED_FREE_KEY
199 
200 /* A convenience macro that lets people avoid the expense of strlen(3)
201  * if they're using a string that's defined at compile time. */
202 #define MCM_CSTRLEN(_str) (sizeof(_str) - 1)
203 
204 /*
205  * Function authors MUST use the following two macros instead of
206  * explicitly defining the various struct members.  These MD5s *will*
207  * change every release to ensure developers (ie, YOU!) use these
208  * macros.  Consider yourself warned. */
209 
210 /* Error function signature and arguments */
211 #define MCM_CALLBACK_FUNC	const struct memcache_ctxt *MCM_CALLBACK_CTXT, struct memcache_res *MCM_CALLBACK_RES, void *MCM_CALLBACK_PTR
212 #define MCM_CALLBACK_SIG	const struct memcache_ctxt *, struct memcache_res *, void *
213 #define MCM_ERR_FUNC_ARGS	const void *MCM_ERR_FUNC_MC_CTXT, void *MCM_ERR_FUNC_ERR_CTXT
214 #define MCM_ERR_FUNC_SIG	const void *, void *
215 
216 /* This see memcache.c:mcm_err_func() for an example on how to use
217  * this. */
218 #define MCM_ERR_INIT_CTXT(_ctxt, _ectxt) do { \
219 	_ctxt = (const struct memcache_ctxt *)MCM_ERR_FUNC_MC_CTXT; \
220 	_ectxt = (struct memcache_err_ctxt *)MCM_ERR_FUNC_ERR_CTXT; \
221 } while (0)
222 
223 /* Key validation function signature and arguments */
224 #define MCM_KEY_VALID_FUNC_ARGS	const void *MCM_KEY_VALID_CTXT, char *MCM_KEY_VALID_KEY, size_t MCM_KEY_VALID_LEN
225 #define MCM_KEY_VALID_FUNC_SIG	const void *, char *, size_t
226 
227 #define MCM_KEY_VALID_INIT(_ctxt, _key, _len) do { \
228 	_ctxt = (const struct memcache_ctxt *)MCM_KEY_VALID_CTXT; \
229 	_key = (char *)MCM_KEY_VALID_KEY; \
230 	_len = MCM_KEY_VALID_LEN; \
231 } while (0)
232 
233 /* Hash function signature and arguments.
234  *
235  * Arg 1: struct memcache_ctxt *
236  * Arg 2: struct memcache *
237  * Arg 3: key
238  * Arg 4: key length
239  */
240 #define MCM_HASH_FUNC		const void *MCM_CALLBACK_CTXT, const void *MCM_CALLBACK_MC, const char *MCM_CALLBACK_KEY, const size_t MCM_CALLBACK_LEN
241 #define MCM_HASH_SIG		const void *, const void *, const char *, const size_t
242 
243 #define MCM_HASH_INIT(_ctxt, _mc, _key, _len) do { \
244 	_ctxt = (const struct memcache_ctxt *)MCM_CALLBACK_CTXT; \
245 	_mc = (const struct memcache *)MCM_CALLBACK_MC; \
246 	_key = (const char *)MCM_CALLBACK_KEY; \
247 	_len = MCM_CALLBACK_LEN; \
248 } while (0)
249 
250 /* Begin various TAILQ macros */
251 #define TRASHIT(x)	do {(x) = (void *)-1;} while (0)
252 
253 #define TAILQ_HEAD_INITIALIZER(head)	{ NULL, &(head).tqh_first }
254 
255 #define TAILQ_HEAD(name, type)					\
256 struct name {							\
257 	struct type *tqh_first; /* first element */		\
258 	struct type **tqh_last; /* addr of last next element */	\
259 }
260 
261 #define TAILQ_ENTRY(type)						\
262 struct {								\
263 	struct type *tqe_next;	/* next element */			\
264 	struct type **tqe_prev;	/* address of previous next element */	\
265 }
266 
267 #define TAILQ_FIRST(head)	((head)->tqh_first)
268 
269 #define TAILQ_NEXT(elm, field)	((elm)->field.tqe_next)
270 
271 #define TAILQ_FOREACH(var, head, field)                                 \
272 	for (var = TAILQ_FIRST(head); var; var = TAILQ_NEXT(var, field))
273 
274 #define TAILQ_INIT(head) do {				\
275 	TAILQ_FIRST((head)) = NULL;			\
276 	(head)->tqh_last = &TAILQ_FIRST((head));	\
277 } while (0)
278 
279 #define TAILQ_INSERT_TAIL(head, elm, field) do {	\
280 	TAILQ_NEXT((elm), field) = NULL;		\
281 	(elm)->field.tqe_prev = (head)->tqh_last;	\
282 	*(head)->tqh_last = (elm);			\
283 	(head)->tqh_last = &TAILQ_NEXT((elm), field);	\
284 } while (0)
285 
286 #define TAILQ_REMOVE(head, elm, field) do {						\
287 	if ((TAILQ_NEXT((elm), field)) != NULL)						\
288 		TAILQ_NEXT((elm), field)->field.tqe_prev = (elm)->field.tqe_prev;	\
289 	else										\
290 		(head)->tqh_last = (elm)->field.tqe_prev;				\
291 	*(elm)->field.tqe_prev = TAILQ_NEXT((elm), field);				\
292 	TRASHIT((elm)->field.tqe_next);							\
293 	TRASHIT((elm)->field.tqe_prev);							\
294 } while (0)
295 /* End various TAILQ macros */
296 
297 
298 /* Load struct memcache_buf, but none of the prototypes that operate
299  * on struct memcache_buf. */
300 #include <memcache/_buffer.h>
301 
302 
303 /* Error handling information struct */
304 struct memcache_err_ctxt {
305   /* A generic pointer not used by memcache(3), but can be used by
306    * calling programs. */
307   void *misc;
308 
309   /* The function name that generated the error */
310   const char *funcname;
311 
312   /* The line number in source file that had the problem */
313   mc_const u_int32_t lineno;
314 
315   /* The errno.  If zero, there was no errno set. */
316   mc_const u_int32_t errnum;
317 
318   /* memcache(3) specific error code */
319   mc_const u_int32_t errcode;
320 
321   /* Severity of the error (INFO, NOTICE, WARN, ERR, FATAL) */
322   mc_const char severity;
323 
324   /* Should we continue executing after we return from this error
325    * handler?  'y'es, 'n'o, or 'a'bort?  Handlers can modify this
326    * value and change the execution of the error handler
327    * dispatcher. */
328   char cont;
329 
330   /* An optional return code that error handlers can set to return
331    * application specific status codes to memcache(3) callers.  Where
332    * integers are returned, this value will be returned to the caller
333    * if it is a non-zero value. */
334   int32_t retcode;
335 
336   /* If memcache(3) causes the program to exit from an error handler,
337    * it will use the following exit status. */
338   int sysexit;
339 
340   /* The error message that pertains to the given error code */
341   const char *errstr;
342 
343   /* Per-error specific error message (null terminated) */
344   const char *errmsg;
345 
346   /* The length of the error message. */
347   mc_const size_t errlen;
348 
349   /* Pointer to the struct memcache_ctxt of the calling function. */
350   const void *ctxt;
351 };
352 
353 /* The memcache API allows callers to provide their own memory
354  * allocation routines to aid in embedability with existing programs,
355  * libraries, programming languages, and environments that have their
356  * own memory handling routines. */
357 typedef void	 (*mcFreeFunc)(void *mem);
358 typedef void	*(*mcMallocFunc)(const size_t size);
359 typedef void	*(*mcReallocFunc)(void *mem, const size_t size);
360 
361 /* See the MCM_ERR_FUNC_* macros for details on the arguments and
362  * memcache.c:mcm_err_func() for an example of how to use this. */
363 typedef int32_t	 (*mcErrFunc)(MCM_ERR_FUNC_ARGS);
364 
365 /* Function prototype for scanning keys and either changing values of
366  * invalid keys, or returning an error code. See the MCM_KEY_VALID_*
367  * macros to gain access to the passed in values. */
368 typedef int32_t	 (*mcKeyValidFunc)(const void *, char *, size_t);
369 
370 /* Function prototype for hashing keys.  See MCM_HASH_FUNC docs for
371  * argument explaination. */
372 typedef u_int32_t	(*mcHashKeyFunc)(MCM_HASH_FUNC);
373 
374 /* Function prototype for finding a server.
375  *
376  * Arg1: struct memcache_ctxt *
377  * Arg2: struct memcache *
378  * Arg3: hash value */
379 typedef void *(*mcServerFindFunc)(const void *, void *, const u_int32_t);
380 
381 /* This structure is only used to support multiple memory contexts.
382  * By default, all memcache(3) API calls use the global memory
383  * context, mcGlobalCtxt.  Under special circumstances (ie, Apache),
384  * it is necessary to have multiple memory contexts that correspond
385  * with their various different calling libraries (PHP, PostgreSQL,
386  * APR, etc).  struct memcache_ctxt and its friends mcm_*() are used
387  * to fulfill this goal.  Under most instances, programs, use of
388  * mc_*() is sufficient, however there is nothing wrong with defining
389  * your own memory context.
390  *
391  * mcMallocAtomic is used where applicable in the event that the
392  * calling application makes use of a garbage collection mechanism
393  * (ie, Boehm).  In non-GC'ed environments, this should be set to the
394  * same things as mcMalloc.  When allocating a struct memcache_ctxt
395  * object, it is absolutely necessary to allocate a new context and
396  * assign it to call mcMemNewCtxt() instead of independently creating
397  * a struct memcache_ctxt object (ie: "struct memcache_ctxt *ctxt;
398  * ctxt = mcMemNewCtxt(...);" == good.  "struct memcache_ctxt ctxt;
399  * bzero(ctxt, sizeof(struct memcache_ctxt));" == bad. */
400 struct memcache_ctxt {
401   /* Memory context function pointers. */
402   mcFreeFunc	mcFree;
403   mcMallocFunc	mcMalloc;
404   mcMallocFunc	mcMallocAtomic;
405   mcReallocFunc	mcRealloc;
406 
407   /* Error handling system.  Users only need to worry about having
408    * mcErrFunc().  All warnings, etc., go through this function.  See
409    * struct mcm_errctxt for details on information passed to the
410    * handler. */
411   mcErrFunc mcErr;
412 
413   /* Key validation system.  Certain applications need to verify the
414    * contents of keys and ensure that various values are contained
415    * inside of keys.  If this function pointer is not null, before
416    * keys are sent to the server, they will be passed to this function
417    * pointer for testing/modification.  All modifications to the key
418    * must be in place and can not change the memory location or the
419    * length of the key. */
420   mcKeyValidFunc mcKeyValid;
421 
422   /* Key hash function. */
423   mcHashKeyFunc mcHashKey;
424 
425   /* Find Server function. */
426   mcServerFindFunc mcServerFind;
427 
428   /* In non-fatal, normal operating conditions, it is plausble (indeed
429    * likely) that certain operations are going to fail, but do so
430    * wihin the bounds of normal operating levels for memcache(7).
431    * Prime examples include performing an increment operation on a key
432    * that doesn't exist.  Given that such cases are to be considered
433    * normal, it is less than desirable to fall back to the error
434    * handling system in memcache(3).  The following errnum value acts
435    * to mimic the global errno(2) value, but on a per-context
436    * basis. */
437   u_int32_t errnum;
438 
439   /* Internal.  Pointer to the last command that was written out: the
440    * state of this pointer is not guaranteed and its use is internal
441    * to memcache(3).  Only used when a server dies and we need to pass
442    * around the server's last command or other data. */
443   struct memcache_buf *_rbuf;
444   struct memcache_buf *_wbuf;
445   u_int32_t _last_hash;
446 
447   /* Pointer to the context's error context/information. */
448   struct memcache_err_ctxt *ectxt;
449 
450   /* Determines what error levels are ignored.  See mc_err_filter_*()
451    * for the interface to this struct member.  Manual manipulation is
452    * prohibited. */
453   u_int32_t MCM_ERR_MASK;
454 };
455 
456 struct memcache_server {
457   /* A generic pointer not used by memcache(3), but can be used by
458    * calling programs. */
459   void *misc;
460 
461   /* The hostname of the server. */
462   char *hostname;
463 
464   /* Port number of the host we're connecting to. */
465   char *port;
466 
467   /* The file descriptor for this server */
468   int fd;
469 
470   /* The timeout for this server */
471   struct timeval tv;
472 
473   /* Is this particular server active or not?
474    *
475    * 'd' == Down	Last request was unsuccessful
476    * 'n' == No host	The hostname doesn't exist
477    * 't' == Try		Haven't connected to it yet, will attempt
478    * 'u' == Up		Has been connected to successfully
479    */
480   char active;
481 
482   /* A cached copy of the looked up host. */
483   struct addrinfo *hostinfo;
484 
485   /* The number of addresses in the cached copy.  If there is more
486    * than one per DNS entry (discouraged), we establish a connection
487    * to them all. */
488   u_int32_t num_addrs;
489 
490 #ifdef HAVE_SELECT
491   /* Reduces the amount of user time required when reading data. */
492   fd_set fds;
493 #endif
494 
495   /* read(2) buffer */
496   struct memcache_buf *rbuf;
497 
498   /* write(2) buffer */
499   struct memcache_buf *wbuf;
500 
501   u_int32_t _last_hash;
502 
503   /* Internal.  A cursor for where we are in the buffer.  This changes
504    * every time we examine a bit of data in our buffer. */
505   size_t soff;
506 
507   /* Internal.  A pointer to the start of the current line in the
508    * buffer. */
509   size_t startoff;
510 
511   /* Misc list bits */
512   TAILQ_ENTRY(memcache_server) entries;
513 };
514 
515 
516 struct memcache_server_stats {
517   pid_t pid;
518   time_t uptime;
519   time_t time;
520   char *version;
521   struct timeval rusage_user;
522   struct timeval rusage_system;
523   u_int32_t curr_items;
524   u_int64_t total_items;
525   u_int64_t bytes;
526   u_int32_t curr_connections;
527   u_int64_t total_connections;
528   u_int32_t connection_structures;
529   u_int64_t cmd_get;
530 #ifdef SEAN_HACKS
531   u_int64_t cmd_refresh;
532 #endif
533   u_int64_t cmd_set;
534   u_int64_t get_hits;
535   u_int64_t get_misses;
536 #ifdef SEAN_HACKS
537   u_int64_t refresh_hits;
538   u_int64_t refresh_misses;
539 #endif
540   u_int64_t bytes_read;
541   u_int64_t bytes_written;
542   u_int64_t limit_maxbytes;
543 };
544 
545 
546 /* struct memcache.  Any of the bits that are commented as "Internal"
547  * should not be twiddled with, ever.  The misc member can be used by
548  * applications and is *never* touched/accessed by memcache(3).  Its
549  * primary purpose is to aid in embedding memcache(3) in other
550  * programming languages. */
551 struct memcache {
552   /* A generic pointer not used by memcache(3), but can be used by
553    * calling programs. */
554   void *misc;
555 
556   /* The default timeout for all servers. */
557   struct timeval tv;
558 
559   /* The number of servers in **servers. */
560   u_int32_t num_servers;
561 
562   /* An array of usable memcache_servers. */
563   struct memcache_server **servers;
564 
565   /* The complete list of servers. */
566   TAILQ_HEAD(memcache_server_list, memcache_server) server_list;
567 };
568 
569 
570 struct memcache_res {
571   /* A generic pointer not used by memcache(3), but can be used by
572    * calling programs. */
573   void *misc;
574 
575   char *key;		/* key */
576   size_t len;		/* length of key */
577   u_int32_t hash;	/* hash of the key */
578   size_t bytes;		/* length of val */
579   void *val;		/* the value */
580 
581   /* If size is zero (default), the memory for val is automatically
582    * allocated using mcMalloc(3).  If size is zero, _flags has its
583    * MC_RES_FREE_ON_DELETE bit set.
584    *
585    * If size is non-zero, memcache(3) assumes that the caller has set
586    * val to an available portion of memory that is size bytes long.
587    * memcache(3) will only populate val with as many bytes as are
588    * specified by size (ie, it will trim the value in order to fit
589    * into val). If size is non-zero, _flags has its
590    * MC_RES_NO_FREE_ON_DELETE bit set by default. */
591   size_t size;
592 
593   TAILQ_ENTRY(memcache_res) entries;
594 
595   /* This is the client supplied flags.  Please note, this flags is
596    * very different than _flags (_flags is an internal bit and
597    * shouldn't be read/changed, etc). */
598   u_int16_t flags;
599 
600   /* If _flags has 0x01 set, val will be free(3)'ed on when this
601    * struct is cleaned up via mc_res_free() or the request is cleaned
602    * up via mc_req_free().
603    *
604    * If _flags has is 0x02 set, val will not be free(3)'ed when this
605    * response or its parent request are cleaned up.
606    *
607    * Note: Use mc_res_free_on_delete() to set the "free on delete"
608    * bits. */
609   char _flags;
610 };
611 
612 struct memcache_req {
613   /* A generic pointer not used by memcache(3), but can be used by
614    * calling programs. */
615   void *misc;
616 
617   TAILQ_HEAD(memcache_res_list, memcache_res) query;
618   TAILQ_HEAD(memcache_res_cb_list, memcache_res_cb) cb;
619   u_int16_t num_keys;
620 };
621 
622 
623 /* Call back interface bits.  memcache(3) offers a callback
624  * mechanism wherein many get requests can be lumped together into a
625  * single get request.  After a response has been read from the
626  * server, the callbacks are executed.
627  *
628  * Example:
629  *
630  * static void my_callback_func(MCM_CALLBACK_SIG);
631  * static void
632  * my_callback_func(MCM_CALLBACK_FUNC) {
633  *   struct my_struct *ptr = (struct my_struct *)MCM_CALLBACK_PTR;
634  *   struct memcache_ctxt *ctxt = MCM_CALLBACK_CTXT;
635  *   struct memcache_res *res = MCM_CALLBACK_RES;
636  *   ...
637  * }
638  *
639  * and callbacks are registered like:
640  *
641  * struct memcache_req *req = mc_req_new();
642  * struct memcache_res *res = mc_req_add(req, key, key_len);
643  * mc_res_register_fetch_cb(req, res, my_callback_func, NULL);
644  *
645  * or, if you want to pass an optional void * pointer:
646  *
647  * struct my_struct *ptr;
648  * struct memcache_res_cb *cb = mc_res_register_fetch_cb(req, res, my_callback_func, ptr);
649  *
650  * Then call mc_get() like normal whenever and your callback will be
651  * executed.  Ex:
652  *
653  * mc_get(req);
654  */
655 
656 typedef void (*mcResCallback)(MCM_CALLBACK_FUNC);
657 struct memcache_res_cb {
658   void *misc;
659   const struct memcache_ctxt *ctxt;
660   struct memcache_req *req;
661   struct memcache_res *res;
662   mcResCallback cb;
663   TAILQ_ENTRY(memcache_res_cb) entries;
664 };
665 
666 
667 /* Adds a given key to the cache */
668 int			 mc_add(struct memcache *mc,
669 				char *key, const size_t key_len,
670 				const void *val, const size_t bytes,
671 				const time_t expire, const u_int16_t flags);
672 
673 /* Gets the value from memcache and allocates the data for the caller.
674  * It is the caller's responsibility to free the returned value.
675  * mc_get() is the preferred interface, however. */
676 void			*mc_aget(struct memcache *mc, char *key, const size_t len);
677 
678 /* Gets the value from memcache and allocates the data for the caller.
679  * It is the caller's responsibility to free the returned value.  If
680  * retlen is non-null, the size of the response will be set upon
681  * return, or zero in the event of a failure.  mc_get() is the
682  * preferred interface, however. */
683 void			*mc_aget2(struct memcache *mc, char *key, const size_t len, size_t *retlen);
684 
685 #ifdef SEAN_HACKS
686 void			*mc_alisten(struct memcache *mc, char *key, const size_t len);
687 
688 /* Gets the value from memcache and allocates the data for the caller.
689  * It is the caller's responsibility to free the returned value.
690  * mc_refresh() is the preferred interface, however. */
691 void			*mc_arefresh(struct memcache *mc, char *key, const size_t len);
692 #endif
693 
694 /* Decrements a given key */
695 u_int32_t		 mc_decr(struct memcache *mc, char *key, const size_t key_len, const u_int32_t val);
696 
697 /* Deletes a given key */
698 int			 mc_delete(struct memcache *mc, char *key, const size_t key_len, const time_t hold);
699 
700 /* Returns true if the given error level was added to the filter */
701 int			 mc_err_filter_add(const u_int32_t err_mask);
702 
703 /* Returns true if the given error level was removed from the filter */
704 int			 mc_err_filter_del(const u_int32_t err_mask);
705 
706 /* Returns the current filter mask.  This interface has a questionable
707  * future and may get removed or have its result set change from
708  * release to release. */
709 u_int32_t		 mc_err_filter_get(void);
710 
711 /* Returns true if the specified error level is being filtered. */
712 int			 mc_err_filter_test(const u_int32_t err_lvl);
713 
714 /* Executes the error handler for testing */
715 void			 mc_err_test(void);
716 
717 /* Flushes all keys on a given server */
718 int			 mc_flush(struct memcache *mc, struct memcache_server *ms);
719 
720 /* Flushes all keys on all servers */
721 int			 mc_flush_all(struct memcache *mc);
722 
723 /* cleans up a memcache object. */
724 void			 mc_free(struct memcache *mc);
725 
726 /* mc_get() is the preferred method of accessing memcache.  It accepts
727  * multiple keys and lets a user (should they so choose) perform
728  * memory caching to reduce the number of mcMalloc(3) calls makes. */
729 void			 mc_get(struct memcache *mc, struct memcache_req *req);
730 
731 /* Generates a hash value from a given key */
732 u_int32_t		 mc_hash(const struct memcache *mc, const char *key, const size_t len);
733 
734 /* Generates a hash value from a given key (depreciated, use mc_hash())*/
735 u_int32_t		 mc_hash_key(const char *key, const size_t len);
736 
737 /* Increments a given key */
738 u_int32_t		 mc_incr(struct memcache *mc, char *key, const size_t key_len, const u_int32_t val);
739 
740 /* Allocates a new memcache object */
741 struct memcache	*mc_new(void);
742 
743 #ifdef SEAN_HACKS
744 void			 mc_listen(struct memcache *mc, struct memcache_req *req);
745 
746 /* mc_refresh() is the preferred method of accessing memcache.  It
747  * accepts multiple keys and lets a user (should they so choose)
748  * perform memory caching to reduce the number of mcMalloc(3) calls
749  * makes.  mc_refresh() differs from mc_get() in that mc_refresh
750  * updates the expiration time to be now + whatever the expiration for
751  * the item was set to.  Sessions should use this as a way of noting
752  * sessions expiring. */
753 void			 mc_refresh(struct memcache *mc, struct memcache_req *req);
754 #endif
755 
756 /* Returns the release date for the library */
757 u_int32_t		 mc_reldate(void);
758 
759 /* Replaces a given key to the cache */
760 int			 mc_replace(struct memcache *mc,
761 				    char *key, const size_t key_len,
762 				    const void *val, const size_t bytes,
763 				    const time_t expire, const u_int16_t flags);
764 
765 /* Safely adds a key to a given request (the key is mc_strdup()'ed).
766    See mc_req_add_ref() to avoid the mc_strdup(): note the warning in
767    mc_req_add_ref() if you decide to use the other function. */
768 struct memcache_res	*mc_req_add(struct memcache_req *req, char *key, const size_t len);
769 
770 /* Adds a key to a given request. Stores a pointer to key.  key can
771    not be modified or free(3)'ed until the passed in request and the
772    returning struct memcache_res object is done being used. */
773 struct memcache_res	*mc_req_add_ref(struct memcache_req *req, char *key, const size_t len);
774 
775 /* Cleans up a given request and its subsequent responses.  If _flags
776  * has the MC_RES_FREE_ON_DELETE bit set (default), it will clean up
777  * the value too.  If _flags has MC_RES_NO_FREE_ON_DELETE set,
778  * however, it is the caller's responsibility to free the value.  To
779  * prevent double free(3) errors, if a value is free(3)'ed before
780  * mc_req_free() is called, set val to NULL. */
781 void			 mc_req_free(struct memcache_req *req);
782 
783 /* Allocates a new memcache request object. */
784 struct memcache_req	*mc_req_new(void);
785 
786 /* Returns 1 if there has been an attempt by the library to fill the
787  * struct's bits from a memcache server.  Returns 0 if there has been
788  * no attempt to fill this struct's data. */
789 int			 mc_res_attempted(const struct memcache_res *res);
790 
791 /* Tells the response object to free the allocated memory when it gets
792  * cleaned up or to let the caller manage the memory. */
793 void			 mc_res_free_on_delete(struct memcache_res *res, const int free_on_delete);
794 
795 /* Returns 1 if the given memcache_res struct contains data that was
796  * found and if there has been an attempt at filling the data.  Return
797  * 0 if no value was found for the key *or* there has been no attempt
798  * at filling the data. */
799 int			 mc_res_found(const struct memcache_res *res);
800 
801 /* Cleans up an individual response object.  Normally this is not
802  * necessary as a call to mc_req_free() will clean up its response
803  * objects. */
804 void			 mc_res_free(struct memcache_req *req, struct memcache_res *res);
805 
806 /* Registers a callback with the request so that upon completion of a
807  * fetch request, a callback gets executed. */
808 int			 mc_res_register_fetch_cb(struct memcache_req *req, struct memcache_res *res,
809 						  mcResCallback cb, void *misc);
810 
811 /* Marks a given server as available again.  Does not reconnect to the
812  * server, however. */
813 int			 mc_server_activate(struct memcache *mc, struct memcache_server *ms);
814 
815 /* Mark all servers as available.  Does not connect to any servers,
816  * but marks them as available. */
817 int			 mc_server_activate_all(struct memcache *mc);
818 
819 /* Disconnects from a given server and marks it as down. */
820 void			 mc_server_deactivate(struct memcache *mc, struct memcache_server *ms);
821 
822 /* Disconnects from a given server */
823 void			 mc_server_disconnect(struct memcache_server *ms);
824 
825 /* Disconnects from all servers (leaves their active flag alone). */
826 void			 mc_server_disconnect_all(const struct memcache *mc);
827 
828 /* When given a hash value, this function returns the appropriate
829  * server to connect to in order to find the key. */
830 struct memcache_server	*mc_server_find(struct memcache *mc, const u_int32_t hash);
831 
832 /* Adds a server to the list of available servers.  By default,
833  * servers are assumed to be available.  Return codes:
834  *
835  * 0:	success
836  * -1:	Unable to allocate a new server instance
837  * -2:	Unable to strdup hostname
838  * -3:	Unable to strdup port
839  * -4:	Unable to Unable to resolve the host, server deactivated, but added to list
840  * -5:	Unable to realloc(3) the server list, server list unchanged */
841 int			 mc_server_add(struct memcache *mc, const char *hostname, const char *port);
842 int			 mc_server_add2(struct memcache *mc,
843 					const char *hostname, const size_t hostname_len,
844 					const char *port, const size_t port_len);
845 int			 mc_server_add3(struct memcache *mc, struct memcache_server *ms);
846 
847 /* Like the above, except hostport can be in the format:
848  * "hostname:port" or just "hostname".  Ex: "127.0.0.1:11211" */
849 int			 mc_server_add4(struct memcache *mc, mc_const char *hostport);
850 
851 /* Same as mc_server_add4(), except with a length for hostport */
852 int			 mc_server_add5(struct memcache *mc, mc_const char *hostport, const size_t hostlen);
853 
854 /* Free's the space from a struct memcache_server.  Use mc_free(3)
855  * instead: only use this function if you really know what you're
856  * doing. */
857 void			 mc_server_free(struct memcache_server *ms);
858 
859 /* Creates a new server struct */
860 struct memcache_server	*mc_server_new(void);
861 
862 
863 /* Cleans up a given stat's object */
864 void			 mc_server_stats_free(struct memcache_server_stats *s);
865 
866 /* Gets a stats object from the given server.  It is the caller's
867  * responsibility to cleanup the resulting object via
868  * mc_server_stats_free(). */
869 struct memcache_server_stats	*mc_server_stats(struct memcache *mc, struct memcache_server *ms);
870 
871 /* Set the timeout on a per server basis */
872 int			 mc_server_timeout(struct memcache_server *ms, const int sec, const int usec);
873 
874 /* Sets a given key */
875 int			 mc_set(struct memcache *mc,
876 				char *key, const size_t key_len,
877 				const void *val, const size_t bytes,
878 				const time_t expire, const u_int16_t flags);
879 
880 /* Creates a stats object for all available servers and returns the
881  * cumulative stats.  Per host-specific data is generally the same as
882  * the last server queried. */
883 struct memcache_server_stats	*mc_stats(struct memcache *mc);
884 
885 /* memcache(3)'s strdup */
886 char			*mc_strdup(const char *str);
887 
888 /* memcache(3)'s strnchr(3) */
889 char			*mc_strnchr(mc_const char *str, const int c, const size_t len);
890 
891 /* memcache(3)'s strndup: returns a dup of str up to len bytes long,
892  * and pads the string with a null character (ie: len + 1). */
893 char			*mc_strndup(const char *str, const size_t len);
894 
895 
896 /* Sets the default timeout for new servers. */
897 void			 mc_timeout(struct memcache *mc, const int sec, const int usec);
898 
899 /* Returns a numeric version of the library */
900 u_int32_t		 mc_vernum(void);
901 
902 /* Returns a string version of the library */
903 const char		*mc_version(void);
904 
905 
906 
907 /* BEGIN memory management API functions and support for multiple
908  * memory contexts.  Most users of memcache(3) should use the
909  * functions prototyped above.  The below functions should be used by
910  * advanced developers seeking a tad bit more control over their app's
911  * use of memcache(3), authors of language extensions, or developers
912  * who need to write modules that exist inside of a single process
913  * with multiple memory allocation routines (ex: Apache and PHP). */
914 
915 
916 /* The following two functions are used to setup additional memory
917  * allocations for programs that use memcache(3), but are not using
918  * the standard system memory management routines (ex: PostgreSQL,
919  * Ruby, etc.) */
920 int	mcMemSetup(mcFreeFunc freeFunc, mcMallocFunc mallocFunc,
921 		   mcMallocFunc mallocAtomicFunc, mcReallocFunc reallocFunc);
922 int	mcMemGet(mcFreeFunc *freeFunc, mcMallocFunc *mallocFunc,
923 		 mcMallocFunc *mallocAtomicFunc, mcReallocFunc *reallocFunc);
924 
925 /* Returns a pointer to the global context. */
926 inline struct memcache_ctxt *mc_global_ctxt(void);
927 
928 /* The next two functions are used to setup an error handler. */
929 int	mcErrSetup(mcErrFunc errFunc);
930 int	mcErrSetupCtxt(struct memcache_ctxt *ctxt, mcErrFunc errFunc);
931 int	mcErrGet(mcErrFunc *errFunc);
932 
933 
934 /* From here on out, the API assumes callers are providing a valid
935  * memory context.  This allows multiple memory contexts to exist
936  * inside the same process.  Very handy for PHP/Apache/APR authors, or
937  * developers in similar situations.  For maximum portability and
938  * embedability, use of the mcm_*() functions is *strongly*
939  * encouraged. */
940 
941 /* Creates a new memory context from scratch: should be sufficient for
942  * most applications. */
943 struct memcache_ctxt	*mcMemNewCtxt(mcFreeFunc freeFunc, mcMallocFunc mallocFunc,
944 				      mcMallocFunc mallocAtomicFunc, mcReallocFunc reallocFunc);
945 
946 /* Safely assigns the various function pointers to the passed in
947  * memory context.  Only needed when an application needs to change
948  * its memory allocation routines (not sure why this would ever need
949  * to happen, to be honest). */
950 int                     mcMemSetupCtxt(struct memcache_ctxt *ctxt, mcFreeFunc freeFunc,
951 				       mcMallocFunc mallocFunc, mcMallocFunc mallocAtomicFunc,
952 				       mcReallocFunc reallocFunc);
953 
954 
955 /* Free's a given memcache context */
956 void			 mcMemFreeCtxt(struct memcache_ctxt *ctxt);
957 
958 /* Functions from here to the bottom of the section behave identically
959  * to the above functions, except they have one additional argument, a
960  * struct memcache_ctxt pointer.  See above for documentation. */
961 int			 mcm_add(struct memcache_ctxt *ctxt, struct memcache *mc,
962 				 char *key, const size_t key_len,
963 				 const void *val, const size_t bytes,
964 				 const time_t expire, const u_int16_t flags);
965 void			*mcm_aget(struct memcache_ctxt *ctxt, struct memcache *mc,
966 				  char *key, const size_t len);
967 void			*mcm_aget2(struct memcache_ctxt *ctxt, struct memcache *mc,
968 				   char *key, const size_t len, size_t *retlen);
969 #ifdef SEAN_HACKS
970 void			*mcm_alisten(struct memcache_ctxt *ctxt, struct memcache *mc,
971 				     char *key, const size_t len);
972 void			*mcm_arefresh(struct memcache_ctxt *ctxt, struct memcache *mc,
973 				      char *key, const size_t len);
974 #endif
975 u_int32_t		 mcm_decr(struct memcache_ctxt *ctxt, struct memcache *mc,
976 				  char *key, const size_t key_len, const u_int32_t val);
977 int			 mcm_delete(struct memcache_ctxt *ctxt, struct memcache *mc,
978 				    char *key, const size_t key_len, const time_t hold);
979 void			 mcm_err(const struct memcache_ctxt *ctxt, const u_int32_t flags,
980 				 const char *funcname, const u_int32_t lineno, const u_int32_t errcode,
981 				 const char *msg, const u_int32_t msglen, const u_int32_t errlvl);
982 int			 mcm_err_filter_add(struct memcache_ctxt *ctxt, const u_int32_t err_mask);
983 int			 mcm_err_filter_del(struct memcache_ctxt *ctxt, const u_int32_t err_mask);
984 u_int32_t		 mcm_err_filter_get(const struct memcache_ctxt *ctxt);
985 int			 mcm_err_filter_test(const struct memcache_ctxt *ctxt, const u_int32_t err_lvl);
986 void			 mcm_err_test(const struct memcache_ctxt *ctxt);
987 int			 mcm_flush(struct memcache_ctxt *ctxt, struct memcache *mc,
988 				   struct memcache_server *ms);
989 int			 mcm_flush_all(struct memcache_ctxt *ctxt, struct memcache *mc);
990 void			 mcm_free(struct memcache_ctxt *ctxt, struct memcache *mc);
991 void			 mcm_get(struct memcache_ctxt *ctxt, struct memcache *mc, struct memcache_req *req);
992 u_int32_t		 mcm_hash(const struct memcache_ctxt *ctxt, const struct memcache *mc, const char *key, const size_t len);
993 u_int32_t		 mcm_hash_key(const struct memcache_ctxt *ctxt, const char *key, const size_t len);
994 u_int32_t		 mcm_incr(struct memcache_ctxt *ctxt, struct memcache *mc,
995 				  char *key, const size_t key_len, const u_int32_t val);
996 struct memcache		*mcm_new(struct memcache_ctxt *ctxt);
997 #ifdef SEAN_HACKS
998 void			 mcm_listen(struct memcache_ctxt *ctxt, struct memcache *mc,
999 				     struct memcache_req *req);
1000 void			 mcm_refresh(struct memcache_ctxt *ctxt, struct memcache *mc,
1001 				     struct memcache_req *req);
1002 #endif
1003 u_int32_t		 mcm_reldate(const struct memcache_ctxt *ctxt);
1004 int			 mcm_replace(struct memcache_ctxt *ctxt, struct memcache *mc,
1005 				     char *key, const size_t key_len,
1006 				     const void *val, const size_t bytes,
1007 				     const time_t expire, const u_int16_t flags);
1008 struct memcache_res	*mcm_req_add(const struct memcache_ctxt *ctxt, struct memcache_req *req,
1009 				     char *key, const size_t len);
1010 struct memcache_res	*mcm_req_add_ref(const struct memcache_ctxt *ctxt, struct memcache_req *req,
1011 					 char *key, const size_t len);
1012 void			 mcm_req_free(const struct memcache_ctxt *ctxt, struct memcache_req *req);
1013 struct memcache_req	*mcm_req_new(const struct memcache_ctxt *ctxt);
1014 int			 mcm_res_attempted(const struct memcache_ctxt *ctxt, const struct memcache_res *res);
1015 int			 mcm_res_found(const struct memcache_ctxt *ctxt, const struct memcache_res *res);
1016 void			 mcm_res_free(const struct memcache_ctxt *ctxt, struct memcache_req *req,
1017 				      struct memcache_res *res);
1018 void			 mcm_res_free_on_delete(const struct memcache_ctxt *ctxt, struct memcache_res *res,
1019 						const int free_on_delete);
1020 int			 mcm_res_register_fetch_cb(struct memcache_ctxt *ctxt, struct memcache_req *req,
1021 						   struct memcache_res *res, mcResCallback callback, void *misc);
1022 int			 mcm_server_activate(const struct memcache_ctxt *ctxt, struct memcache *mc, struct memcache_server *ms);
1023 int			 mcm_server_activate_all(const struct memcache_ctxt *ctxt, struct memcache *mc);
1024 int			 mcm_server_add(struct memcache_ctxt *ctxt, struct memcache *mc,
1025 					const char *hostname, const char *port);
1026 int			 mcm_server_add2(struct memcache_ctxt *ctxt, struct memcache *mc,
1027 					 const char *hostname, const size_t hostname_len,
1028 					 const char *port, const size_t port_len);
1029 int			 mcm_server_add3(struct memcache_ctxt *ctxt, struct memcache *mc,
1030 					 struct memcache_server *ms);
1031 int			 mcm_server_add4(struct memcache_ctxt *ctxt, struct memcache *mc,
1032 					 mc_const char *hostport);
1033 int			 mcm_server_add5(struct memcache_ctxt *ctxt, struct memcache *mc,
1034 					 mc_const char *hostport, const size_t hostlen);
1035 void			 mcm_server_deactivate(struct memcache_ctxt *ctxt, struct memcache *mc,
1036 					       struct memcache_server *ms);
1037 void			 mcm_server_disconnect(const struct memcache_ctxt *ctxt, struct memcache_server *ms);
1038 void			 mcm_server_disconnect_all(const struct memcache_ctxt *ctxt, const struct memcache *mc);
1039 struct memcache_server	*mcm_server_find(const struct memcache_ctxt *ctxt,
1040 					 struct memcache *mc, const u_int32_t hash);
1041 void			 mcm_server_free(struct memcache_ctxt *ctxt, struct memcache_server *ms);
1042 struct memcache_server	*mcm_server_new(struct memcache_ctxt *ctxt);
1043 void			 mcm_server_stats_free(const struct memcache_ctxt *ctxt, struct memcache_server_stats *s);
1044 struct memcache_server_stats	*mcm_server_stats(struct memcache_ctxt *ctxt, struct memcache *mc, struct memcache_server *ms);
1045 int			 mcm_server_timeout(const struct memcache_ctxt *ctxt, struct memcache_server *ms, const int sec, const int usec);
1046 int			 mcm_set(struct memcache_ctxt *ctxt, struct memcache *mc,
1047 				 char *key, const size_t key_len,
1048 				 const void *val, const size_t bytes,
1049 				 const time_t expire, const u_int16_t flags);
1050 struct memcache_server_stats	*mcm_stats(struct memcache_ctxt *ctxt, struct memcache *mc);
1051 char			*mcm_strdup(const struct memcache_ctxt *ctxt, const char *str);
1052 char			*mcm_strnchr(const struct memcache_ctxt *ctxt, mc_const char *str, const int c, const size_t len);
1053 char			*mcm_strndup(const struct memcache_ctxt *ctxt, const char *str, const size_t len);
1054 char			*mcm_strnstr(const struct memcache_ctxt *ctxt, mc_const char *s, const char *find, size_t slen);
1055 void			 mcm_timeout(const struct memcache_ctxt *ctxt, struct memcache *mc, const int sec, const int usec);
1056 u_int32_t		 mcm_vernum(const struct memcache_ctxt *ctxt);
1057 const char		*mcm_version(const struct memcache_ctxt *ctxt);
1058 /* END memory management API functions */
1059 
1060 
1061 /* BEGIN - Error handling convenience macros */
1062 #define ERR_FLAG	0x1
1063 #define WARN_FLAG	0x2
1064 #define NO_ERRNO_FLAG	0x4
1065 #define MCM_ERR(_code)			mcm_err(ctxt, ERR_FLAG, __FUNCTION__, __LINE__, _code, NULL, 0, 0)
1066 #define MCM_ERR_MSG(_code, _msg)	mcm_err(ctxt, ERR_FLAG, __FUNCTION__, __LINE__, _code, _msg, (_msg != NULL ? strlen(_msg) : 0), 0)
1067 #define MCM_ERR_MSG_LVL(_code, _msg, _lvl)	mcm_err(ctxt, ERR_FLAG, __FUNCTION__, __LINE__, _code, _msg, (_msg != NULL ? strlen(_msg) : 0), _lvl)
1068 #define MCM_ERRX(_code)			mcm_err(ctxt, ERR_FLAG|NO_ERRNO_FLAG, __FUNCTION__, __LINE__, _code, NULL, 0, 0)
1069 #define MCM_ERRX_MSG_LVL(_code, _msg, _lvl)	mcm_err(ctxt, ERR_FLAG|NO_ERRNO_FLAG, __FUNCTION__, __LINE__, _code, _msg, (_msg != NULL ? strlen(_msg) : 0), _lvl)
1070 #define MCM_ERRX_MSG(_code, _msg)	mcm_err(ctxt, ERR_FLAG|NO_ERRNO_FLAG, __FUNCTION__, __LINE__, _code, _msg, (_msg != NULL ? strlen(_msg) : 0), 0)
1071 #define MCM_WARNX(_code, _msg)		mcm_err(ctxt, WARN_FLAG|NO_ERRNO_FLAG, __FUNCTION__, __LINE__, _code, _msg, (_msg != NULL ? strlen(_msg) : 0), 0)
1072 #define MCM_WARN_MSG(_code, _msg)	mcm_err(ctxt, WARN_FLAG, __FUNCTION__, __LINE__, _code, _msg, (_msg != NULL ? strlen(_msg) : 0), 0)
1073 #define MCM_WARN_MSGLEN(_code, _m, _l)	mcm_err(ctxt, WARN_FLAG, __FUNCTION__, __LINE__, _code, _m, _l, 0)
1074 #define MCM_WARN_MSG_LVL(_code, _msg, _lvl)	mcm_err(ctxt, WARN_FLAG, __FUNCTION__, __LINE__, _code, _msg, (_msg != NULL ? strlen(_msg) : 0), _lvl)
1075 #define MCM_WARNX_MSG(_code, _msg)	mcm_err(ctxt, WARN_FLAG|NO_ERRNO_FLAG, __FUNCTION__, __LINE__, _code, _msg, (_msg != NULL ? strlen(_msg) : 0), 0)
1076 #define MCM_WARNX_MSGLEN(_code, _m, _l)	mcm_err(ctxt, WARN_FLAG|NO_ERRNO_FLAG, __FUNCTION__, __LINE__, _code, _m, _l, 0)
1077 #define MCM_RET_CODE(_v)		(ctxt->ectxt->retcode != 0 ? ctxt->ectxt->retcode : _v)
1078 /* END - Error handling convenience macros */
1079 
1080 
1081 /* APIs that should be implemented: */
1082 
1083 /* Resets all DNS entries */
1084 void mc_server_reset_all_dns(struct memcache *mc);
1085 
1086 /* Resets only one host's DNS cache */
1087 void mc_server_reset_dns(struct memcache *mc, const char *hostname, const int port);
1088 
1089 #ifdef TCP_NODELAY
1090 /* Enable/disable TCP_NODELAY */
1091 void mc_nodelay_enable(struct memcache *mc, const int enable);
1092 
1093 /* Enable/disable TCP_NODELAY for a given server */
1094 void mc_server_nodelay_enable(struct memcache_server *ms, const int enable);
1095 #endif
1096 
1097 /* Set the number of seconds you're willing to wait in total for a
1098  * response. ??? */
1099 
1100 #ifdef __cplusplus
1101 }
1102 #endif
1103 
1104 #endif
1105