1 /*
2  * Copyright (C) Tildeslash Ltd. All rights reserved.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License version 3.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU Affero General Public License
13  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14  *
15  * In addition, as a special exception, the copyright holders give
16  * permission to link the code of portions of this program with the
17  * OpenSSL library under certain conditions as described in each
18  * individual source file, and distribute linked combinations
19  * including the two.
20  *
21  * You must obey the GNU Affero General Public License in all respects
22  * for all of the code used other than OpenSSL.
23  */
24 
25 #include "config.h"
26 
27 #include "protocol.h"
28 
29 // libmonit
30 #include "exceptions/IOException.h"
31 #include "exceptions/ProtocolException.h"
32 
33 
34 /* --------------------------------------------------------------- Public */
35 
36 
37 /**
38  * Simple redis RESP protocol ping test:
39  *
40  *     1. send a PING command
41  *     2. expect a PONG response
42  *     3. send a QUIT command
43  *
44  * @see http://redis.io/topics/protocol
45  *
46  * @file
47  */
check_redis(Socket_T socket)48 void check_redis(Socket_T socket) {
49         ASSERT(socket);
50         char buf[STRLEN];
51 
52         if (Socket_print(socket, "*1\r\n$4\r\nPING\r\n") < 0)
53                 THROW(IOException, "REDIS: PING command error -- %s", STRERROR);
54         if (! Socket_readLine(socket, buf, sizeof(buf)))
55                 THROW(IOException, "REDIS: PING response error -- %s", STRERROR);
56         Str_chomp(buf);
57         if (! Str_isEqual(buf, "+PONG") && ! Str_startsWith(buf, "-NOAUTH")) // We accept authentication error (-NOAUTH Authentication required): redis responded to request, but requires authentication => we assume it works
58                 THROW(ProtocolException, "REDIS: PING error -- %s", buf);
59         if (Socket_print(socket, "*1\r\n$4\r\nQUIT\r\n") < 0)
60                 THROW(IOException, "REDIS: QUIT command error -- %s", STRERROR);
61 }
62 
63