Merge pull request #140 from hussainnaqvee/patch-1
[awesomized/libmemcached] / src / libmemcachedutil / version.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached-awesome - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020-2021 Michael Wallner https://awesome.co/ |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "libmemcachedutil/common.h"
17 #include <cassert>
18
19 struct local_context {
20 uint8_t major_version;
21 uint8_t minor_version;
22 uint8_t micro_version;
23
24 bool truth;
25 };
26
27 static memcached_return_t
28 check_server_version(const memcached_st *, const memcached_instance_st *instance, void *context) {
29 /* Do Nothing */
30 struct local_context *check = (struct local_context *) context;
31
32 if (memcached_server_major_version(instance) != UINT8_MAX) {
33 uint32_t sv, cv;
34
35 sv = memcached_server_micro_version(instance) | memcached_server_minor_version(instance) << 8
36 | memcached_server_major_version(instance) << 16;
37 cv = check->micro_version | check->minor_version << 8 | check->major_version << 16;
38
39 if (sv >= cv) {
40 return MEMCACHED_SUCCESS;
41 }
42 }
43
44 check->truth = false;
45
46 return MEMCACHED_FAILURE;
47 }
48
49 bool libmemcached_util_version_check(memcached_st *memc, uint8_t major_version,
50 uint8_t minor_version, uint8_t micro_version) {
51 if (memcached_failed(memcached_version(memc))) {
52 return false;
53 }
54
55 struct local_context check = {major_version, minor_version, micro_version, true};
56
57 memcached_server_fn callbacks[1];
58 callbacks[0] = check_server_version;
59 memcached_server_cursor(memc, callbacks, (void *) &check, 1);
60
61 return check.truth;
62 }