#ifndef MEMCACHE_LITE_PRIVATE_H #define MEMCACHE_LITE_PRIVATE_H #include #include #include #include #include #define MC_APPEND(a,b) a##b #define MC(name) MC_APPEND(memcache_private_, name) #define MEMCACHE_BUFFER_START_SIZE MEMCACHE_BUFFER_BLOCK #define MEMCACHE_BUFFER_BLOCK 4096 #define MEMCACHE_BUFFER_MAX_SIZE (MEMCACHE_BUFFER_BLOCK*1024) #define MEMCACHE_MAX_KEY_LEN 250 #define MEMCACHE_MAX_KEY_LEN_S "250" // make a private return collection // memcache_buffer_return_t perhaps? // just pass state around? typedef enum { ERROR_BUG = -INT_MAX, ERROR_NULL, ERROR_MEM, ERROR_SOCKET, ERROR_TOOBIG, ERROR_CONNECT, ERROR_NOTSTORED, ERROR_NOTFOUND, ERROR_PASS = -1, ERROR_NONE, RETURN_FAILURE = ERROR_PASS, RETURN_SUCCESS = ERROR_NONE, } MC(return_t); // typedef struct { // enum MC(return_code) code; // int errno; // } MC(return_t); // const static MC(RETURN_SUCCESS) = { ERROR_NONE, 0 }; /* private data structures */ typedef struct MC(buffer) { char *data; char *cursor; size_t allocated; size_t length; size_t remaining; io_context_t *io_context; } MC(buffer_t); typedef struct MC(state) { short connected; char path_to_socket[PATH_MAX + 1]; int socket; MC(return_t) error_has_occurred; int last_errno; char error_message[256]; MC(buffer_t) read; MC(buffer_t) write; struct sigaction sigpipe_original; io_context_t io_context; } MC(state_t); typedef struct MC(result) { short found; char key[MEMCACHE_MAX_KEY_LEN + 1]; char *data; size_t size; short flags; } MC(result_t); /* client messages - like apr_memcache */ #define MC_EOL "\r\n" #define MC_EOL_LEN (sizeof(MC_EOL)-1) #define MC_GET "get " #define MC_GET_LEN (sizeof(MC_GET)-1) #define MC_SET "set " #define MC_SET_LEN (sizeof(MC_SET)-1) #define MC_ADD "add " #define MC_ADD_LEN (sizeof(MC_ADD)-1) #define MC_REPLACE "replace " #define MC_REPLACE_LEN (sizeof(MC_REPLACE)-1) #define MC_DELETE "delete " #define MC_DELETE_LEN (sizeof(MC_DELETE)-1) #define MC_INCR "incr " #define MC_INCR_LEN (sizeof(MC_INCR)-1) #define MC_DECR "decr " #define MC_DECR_LEN (sizeof(MC_DECR)-1) #define MC_VERSION "version" #define MC_VERSION_LEN (sizeof(MC_VERSION)-1) #define MC_STATS "stats" #define MC_STATS_LEN (sizeof(MC_STATS)-1) /* Strings for Server Replies */ #define MS_STORED "STORED" #define MS_STORED_LEN (sizeof(MS_STORED)-1) #define MS_NOT_STORED "NOT_STORED" #define MS_NOT_STORED_LEN (sizeof(MS_NOT_STORED)-1) #define MS_DELETED "DELETED" #define MS_DELETED_LEN (sizeof(MS_DELETED)-1) #define MS_NOT_FOUND "NOT_FOUND" #define MS_NOT_FOUND_LEN (sizeof(MS_NOT_FOUND)-1) #define MS_VALUE "VALUE" #define MS_VALUE_LEN (sizeof(MS_VALUE)-1) #define MS_ERROR "ERROR" #define MS_ERROR_LEN (sizeof(MS_ERROR)-1) #define MS_VERSION "VERSION" #define MS_VERSION_LEN (sizeof(MS_VERSION)-1) #define MS_STAT "STAT" #define MS_STAT_LEN (sizeof(MS_STAT)-1) #define MS_END "END" #define MS_END_LEN (sizeof(MS_END)-1) /* "VALUE 65535 4294967296\r\n */ #define MAX_RESPONSE_HEADER_LEN 275 #define LOG(msg, ...) { \ char m[256]; \ snprintf(m, 256, "%s:" msg "\n", __func__, ##__VA_ARGS__); \ write(1, m, strlen(m)); \ } #ifdef DEBUG #define DLOG(msg, ...) LOG(msg, ##__VA_ARGS__) #else #define DLOG(msg, ...) {} #endif #define DLOGR(return_value, message, ...) { \ DLOG(message, ##__VA_ARGS__); \ return return_value; \ } // log calls may alter the errno value... #define LOGR(return_value, message) { \ LOG(message); \ return return_value; \ } /* used by higher level functions */ #define FAILURE(error_type, message) { \ ERROR(error_type, message); \ return RETURN_FAILURE; \ } #define ERROR(error_type, message) { \ state->last_errno = errno; \ state->error_has_occurred = error_type; \ snprintf(state->error_message, sizeof(state->error_message), \ "%s:%s", __func__, message); \ } /* private functions */ //static memcache_return_t key_is_safe(char **key); //static char *key_sanitize(const char *key); MC(return_t) MC(command)(MC(state_t) *state, const char *fmt, ...); MC(return_t) MC(storage)(MC(state_t) *state, const char *cmd, const char *key, const char *data, unsigned int flags, time_t expiration, size_t length); MC(return_t) MC(buffer_destroy)(MC(buffer_t) *buffer); MC(return_t) MC(buffer_initialize)(MC(buffer_t) *buffer); MC(return_t) MC(buffer_available)(MC(buffer_t) *buffer, size_t *size); MC(return_t) MC(buffer_used)(MC(buffer_t) *buffer, size_t *size); MC(return_t) MC(buffer_get)(MC(buffer_t) *buffer, char **dst, size_t *size); MC(return_t) MC(buffer_getn)(MC(buffer_t) *buffer, char *data, size_t size); MC(return_t) MC(buffer_put)(MC(buffer_t) *buffer, const char *data, size_t size); MC(return_t) MC(buffer_flush)(MC(buffer_t) *buffer, size_t size); MC(return_t) MC(buffer_flush_all)(MC(buffer_t) *buffer); MC(return_t) MC(buffer_resize_if_needed)(MC(buffer_t) *buffer); MC(return_t) MC(buffer_write_fd)(MC(buffer_t) *buffer, int fd); MC(return_t) MC(buffer_read_fd)(MC(buffer_t) *buffer, int fd); MC(return_t) MC(io_connect_unix)(const char *path, int *fd); MC(return_t) MC(io_readn)(MC(state_t) *state, char *dst, size_t size); MC(return_t) MC(io_readn_until)(MC(state_t) *state, char end, char *dst, size_t max); MC(return_t) MC(io_write)(MC(state_t) *state, const char *src, size_t len); MC(return_t) MC(io_write_sync)(MC(state_t) *state); void MC(sigpipe_action)(int signal, siginfo_t *info, void *state_ptr); #endif