Moved init for socket to options.
[m6w6/libmemcached] / libmemcached / common.h
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
12 /*
13 Common include file for libmemached
14 */
15
16 #ifndef LIBMEMCACHED_COMMON_H
17 #define LIBMEMCACHED_COMMON_H
18
19 #include "config.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <strings.h>
25 #include <ctype.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <netdb.h>
31 #include <unistd.h>
32 #include <limits.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <sys/un.h>
36 #include <netinet/tcp.h>
37 #ifdef TIME_WITH_SYS_TIME
38 # include <sys/time.h>
39 # include <time.h>
40 #else
41 # ifdef HAVE_SYS_TIME_H
42 # include <sys/time.h>
43 # else
44 # include <time.h>
45 # endif
46 #endif
47
48 /* Define this here, which will turn on the visibilty controls while we're
49 * building libmemcached.
50 */
51 #define BUILDING_LIBMEMCACHED 1
52
53
54 #include "libmemcached/memcached.h"
55 #include "libmemcached/watchpoint.h"
56
57 /* These are private not to be installed headers */
58 #include "libmemcached/io.h"
59 #include "libmemcached/internal.h"
60 #include "libmemcached/libmemcached_probes.h"
61 #include "libmemcached/memcached/protocol_binary.h"
62 #include "libmemcached/byteorder.h"
63
64 /* string value */
65 struct memcached_continuum_item_st
66 {
67 uint32_t index;
68 uint32_t value;
69 };
70
71
72 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
73
74 #define likely(x) if((x))
75 #define unlikely(x) if((x))
76
77 #else
78
79 #define likely(x) if(__builtin_expect((x) != 0, 1))
80 #define unlikely(x) if(__builtin_expect((x) != 0, 0))
81 #endif
82
83
84 #define MEMCACHED_BLOCK_SIZE 1024
85 #define MEMCACHED_DEFAULT_COMMAND_SIZE 350
86 #define SMALL_STRING_LEN 1024
87 #define HUGE_STRING_LEN 8196
88
89
90 typedef enum {
91 MEM_NO_BLOCK= (1 << 0),
92 MEM_TCP_NODELAY= (1 << 1),
93 MEM_REUSE_MEMORY= (1 << 2),
94 MEM_USE_MD5= (1 << 3),
95 /* 4 was once Ketama */
96 MEM_USE_CRC= (1 << 5),
97 MEM_USE_CACHE_LOOKUPS= (1 << 6),
98 MEM_SUPPORT_CAS= (1 << 7),
99 MEM_BUFFER_REQUESTS= (1 << 8),
100 MEM_USE_SORT_HOSTS= (1 << 9),
101 MEM_VERIFY_KEY= (1 << 10),
102 /* 11 used for weighted ketama */
103 MEM_KETAMA_WEIGHTED= (1 << 11),
104 MEM_BINARY_PROTOCOL= (1 << 12),
105 MEM_HASH_WITH_PREFIX_KEY= (1 << 13),
106 MEM_NOREPLY= (1 << 14),
107 MEM_USE_UDP= (1 << 15),
108 MEM_AUTO_EJECT_HOSTS= (1 << 16),
109 MEM_RANDOMIZE_REPLICA_READ= (1 << 17)
110 } memcached_flags;
111
112 /* Hashing algo */
113
114 LIBMEMCACHED_LOCAL
115 void md5_signature(const unsigned char *key, unsigned int length, unsigned char *result);
116 LIBMEMCACHED_LOCAL
117 uint32_t hash_crc32(const char *data,
118 size_t data_len);
119 #ifdef HAVE_HSIEH_HASH
120 LIBMEMCACHED_LOCAL
121 uint32_t hsieh_hash(const char *key, size_t key_length);
122 #endif
123 LIBMEMCACHED_LOCAL
124 uint32_t murmur_hash(const char *key, size_t key_length);
125 LIBMEMCACHED_LOCAL
126 uint32_t jenkins_hash(const void *key, size_t length, uint32_t initval);
127
128 LIBMEMCACHED_LOCAL
129 memcached_return_t memcached_connect(memcached_server_st *ptr);
130 LIBMEMCACHED_LOCAL
131 memcached_return_t memcached_response(memcached_server_st *ptr,
132 char *buffer, size_t buffer_length,
133 memcached_result_st *result);
134 LIBMEMCACHED_LOCAL
135 void memcached_quit_server(memcached_server_st *ptr, uint8_t io_death);
136
137 #define memcached_server_response_increment(A) (A)->cursor_active++
138 #define memcached_server_response_decrement(A) (A)->cursor_active--
139 #define memcached_server_response_reset(A) (A)->cursor_active=0
140
141 LIBMEMCACHED_LOCAL
142 memcached_return_t memcached_do(memcached_server_st *ptr, const void *commmand,
143 size_t command_length, uint8_t with_flush);
144 LIBMEMCACHED_LOCAL
145 memcached_return_t value_fetch(memcached_server_st *ptr,
146 char *buffer,
147 memcached_result_st *result);
148 LIBMEMCACHED_LOCAL
149 void server_list_free(memcached_st *ptr, memcached_server_st *servers);
150
151 LIBMEMCACHED_LOCAL
152 memcached_return_t memcached_key_test(const char * const *keys,
153 const size_t *key_length,
154 size_t number_of_keys);
155
156
157 LIBMEMCACHED_LOCAL
158 uint32_t generate_hash(memcached_st *ptr, const char *key, size_t key_length);
159
160 LIBMEMCACHED_LOCAL
161 memcached_return_t memcached_purge(memcached_server_st *ptr);
162
163 static inline memcached_return_t memcached_validate_key_length(size_t key_length, bool binary)
164 {
165 unlikely (key_length == 0)
166 return MEMCACHED_BAD_KEY_PROVIDED;
167
168 if (binary)
169 {
170 unlikely (key_length > 0xffff)
171 return MEMCACHED_BAD_KEY_PROVIDED;
172 }
173 else
174 {
175 unlikely (key_length >= MEMCACHED_MAX_KEY)
176 return MEMCACHED_BAD_KEY_PROVIDED;
177 }
178
179 return MEMCACHED_SUCCESS;
180 }
181
182 #endif /* LIBMEMCACHED_COMMON_H */