Commit 81884b67fbead530e7dd9ef7df39fa5238cc9974

Authored by Imanol-Mikel Barba Sabariego
1 parent fdd9c743

Updated cs2 server checks

Showing 1 changed file with 147 additions and 145 deletions
check_csgo/check_csgo.cpp
1 #include "check_csgo.h" 1 #include "check_csgo.h"
  2 +#include <vector>
2 3
3 using namespace std; 4 using namespace std;
4 5
5 char *servicename = (char*)"CS:GO"; 6 char *servicename = (char*)"CS:GO";
6 7
7 -void printVersion()  
8 -{  
9 - cout << "check_csgo v" << VERSION << endl << endl; 8 +void printVersion() {
  9 + cout << "check_csgo v" << VERSION << endl << endl;
10 } 10 }
11 11
12 -void printHelp(bool longVersion)  
13 -{  
14 - if(longVersion)  
15 - {  
16 - printVersion();  
17 - cout << "Check CS:GO DS instance." << endl << endl;  
18 - printHelp(false);  
19 - cout << "Options:" << endl;  
20 - cout << " -h" << endl;  
21 - cout << " Print detailed help screen" << endl;  
22 - cout << " -V" << endl;  
23 - cout << " Print version information" << endl;  
24 - cout << " -H HOSTADDRESS" << endl;  
25 - cout << " Host where the Source DS is running" << endl;  
26 - cout << " -p" << endl;  
27 - cout << " Port where the Source DS is listening. Default is 27015." << endl << endl;  
28 - return;  
29 - }  
30 - cout << "Usage: " << endl << "check_csgo [-hV] -H HOSTADDRESS [-p PORT]" << endl << endl; 12 +void printHelp(bool longVersion) {
  13 + if(longVersion) {
  14 + printVersion();
  15 + cout << "Check CS:GO DS instance." << endl << endl;
  16 + printHelp(false);
  17 + cout << "Options:" << endl;
  18 + cout << " -h" << endl;
  19 + cout << " Print detailed help screen" << endl;
  20 + cout << " -V" << endl;
  21 + cout << " Print version information" << endl;
  22 + cout << " -H HOSTADDRESS" << endl;
  23 + cout << " Host where the Source DS is running" << endl;
  24 + cout << " -p" << endl;
  25 + cout << " Port where the Source DS is listening. Default is 27015." << endl << endl;
  26 + return;
  27 + }
  28 + cout << "Usage: " << endl << "check_csgo [-hV] -H HOSTADDRESS [-p PORT]" << endl << endl;
31 } 29 }
32 30
33 -SOURCEDS_PACKET* getSourceDSResponse(int s, sockaddr_in *server)  
34 -{  
35 - char *data = new char[DATAGRAM_LENGTH];  
36 - int dataLength = recvMsg(s, data, DATAGRAM_LENGTH, server);  
37 - if(dataLength == -1)  
38 - {  
39 - return NULL;  
40 - }  
41 - char *header = (char*) "\xFF\xFF\xFF\xFF";  
42 - if(memcmp(data,header,HDR_SIZE))  
43 - {  
44 - //Invalid response, or non-supported multi-packet response format  
45 - return NULL;  
46 - }  
47 - SOURCEDS_PACKET *response = new SOURCEDS_PACKET;  
48 - response->data = data+HDR_SIZE;  
49 - response->length = dataLength;  
50 - return response; 31 +SOURCEDS_PACKET* getSourceDSResponse(int s, sockaddr_in *server) {
  32 + char *data = new char[DATAGRAM_LENGTH];
  33 + int dataLength = recvMsg(s, data, DATAGRAM_LENGTH, server);
  34 + if(dataLength == -1) {
  35 + return NULL;
  36 + }
  37 + char *header = (char*) "\xFF\xFF\xFF\xFF";
  38 + if(memcmp(data,header,HDR_SIZE)) {
  39 + //Invalid response, or non-supported multi-packet response format
  40 + return NULL;
  41 + }
  42 + SOURCEDS_PACKET *response = new SOURCEDS_PACKET;
  43 + response->data = data+HDR_SIZE;
  44 + response->length = dataLength;
  45 + return response;
51 } 46 }
52 47
53 -int check_csgo(char *hostname, uint16_t port, SERVERINFO *server_info)  
54 -{  
55 - struct sockaddr_in si;  
56 - struct hostent *host = NULL;  
57 - int s;  
58 - int timeout = 10;  
59 - setupSocket(port,hostname,host,timeout,&si,&s);  
60 - string discard;  
61 -  
62 - char *info_query = (char*) "\xFF\xFF\xFF\xFF\x54Source Engine Query\x00";  
63 -  
64 - sendMsg(s,info_query, strlen(info_query)+1,&si);  
65 - SOURCEDS_PACKET *response = getSourceDSResponse(s,&si);  
66 - if(response == NULL)  
67 - {  
68 - return 2;  
69 - }  
70 -  
71 - ssize_t offset = 2;  
72 - server_info->name = string(response->data+offset,strlen(response->data+offset));  
73 - offset += server_info->name.size()+1;  
74 - server_info->map = string(response->data+offset,strlen(response->data+offset));  
75 - offset += server_info->map.size()+1;  
76 - discard = string(response->data+offset,strlen(response->data+offset));  
77 - offset += discard.size() + 1;  
78 - server_info->game = string(response->data+offset,strlen(response->data+offset));  
79 - offset += server_info->game.size()+1;  
80 - offset += 2;  
81 - server_info->players = (uint8_t) *((response->data)+offset);  
82 - server_info->max_players = (uint8_t) *((response->data)+(++offset));  
83 - 48 +int check_csgo(char *hostname, uint16_t port, SERVERINFO *server_info) {
  49 + struct sockaddr_in si;
  50 + struct hostent *host = NULL;
  51 + int s;
  52 + int timeout = 10;
  53 + setupSocket(port,hostname,host,timeout,&si,&s);
  54 + string discard;
  55 +
  56 + std::vector<uint8_t> info_query;
  57 + info_query.resize(25);
  58 + memcpy(info_query.data(), "\xFF\xFF\xFF\xFF\x54Source Engine Query\x00", 25);
  59 +
  60 + sendMsg(s, reinterpret_cast<char*>(info_query.data()), info_query.size(), &si);
  61 + SOURCEDS_PACKET *response = getSourceDSResponse(s,&si);
  62 + if(response == NULL) {
  63 + return 2;
  64 + }
  65 +
  66 + if(response->data[0] == 'A') {
  67 + // Challenge response mechanism
  68 + uint32_t challenge;
  69 + size_t current_pkt_size = info_query.size();
  70 + info_query.resize(current_pkt_size + 4);
  71 + memcpy(info_query.data() + current_pkt_size, response->data+1, 4);
84 delete[] (response->data-HDR_SIZE); 72 delete[] (response->data-HDR_SIZE);
85 delete response; 73 delete response;
86 74
87 - if(server_info->players == server_info->max_players)  
88 - {  
89 - return 1; 75 + sendMsg(s, reinterpret_cast<char*>(info_query.data()), info_query.size(), &si);
  76 + response = getSourceDSResponse(s, &si);
  77 + if(response == NULL) {
  78 + return 2;
90 } 79 }
91 -  
92 - return 0; 80 + }
  81 +
  82 + ssize_t offset = 2;
  83 + server_info->name = string(response->data+offset,strlen(response->data+offset));
  84 + offset += server_info->name.size()+1;
  85 + server_info->map = string(response->data+offset,strlen(response->data+offset));
  86 + offset += server_info->map.size()+1;
  87 + discard = string(response->data+offset,strlen(response->data+offset));
  88 + offset += discard.size() + 1;
  89 + server_info->game = string(response->data+offset,strlen(response->data+offset));
  90 + offset += server_info->game.size()+1;
  91 + offset += 2;
  92 + server_info->players = (uint8_t) *((response->data)+offset);
  93 + server_info->max_players = (uint8_t) *((response->data)+(++offset));
  94 +
  95 + delete[] (response->data-HDR_SIZE);
  96 + delete response;
  97 +
  98 + if(server_info->players == server_info->max_players) {
  99 + return 1;
  100 + }
  101 +
  102 + return 0;
93 } 103 }
94 104
95 -int main(int argc, char **argv)  
96 -{  
97 - struct itimerval timer;  
98 - timer.it_value.tv_sec = 10;  
99 - timer.it_value.tv_usec = 0;  
100 - timer.it_interval.tv_sec = 0;  
101 - timer.it_interval.tv_usec = 0;  
102 - setitimer (ITIMER_VIRTUAL, &timer, 0);  
103 -  
104 - struct sigaction sa;  
105 - memset (&sa, 0, sizeof (sa));  
106 - sa.sa_handler = &timer_handler;  
107 - sigaction (SIGVTALRM, &sa, 0);  
108 -  
109 - uint16_t port = 27015;  
110 - char *hostname = NULL;  
111 - int c;  
112 -  
113 - while ((c = getopt (argc, argv, "H:p:Vh")) != -1)  
114 - {  
115 - switch(c)  
116 - {  
117 - case 'H':  
118 - hostname = optarg;  
119 - break;  
120 - case 'p':  
121 - port = (uint16_t) str2int(optarg);  
122 - break;  
123 - case 'V':  
124 - printVersion();  
125 - return 0;  
126 - case 'h':  
127 - printHelp(true);  
128 - return 0;  
129 - case '?':  
130 - printHelp(false);  
131 - return 3;  
132 - }  
133 - }  
134 -  
135 - if(hostname == NULL)  
136 - {  
137 - cout << "No HOSTADDRESS specified. Exiting." << endl;  
138 - return 3;  
139 - }  
140 -  
141 - SERVERINFO server_info;  
142 - int returnCode = check_csgo(hostname,port,&server_info);  
143 -  
144 - cout << servicename;  
145 - switch(returnCode)  
146 - {  
147 - case 0:  
148 - cout << " OK";  
149 - cout << " - " << hostname << " " << server_info.name << " " << server_info.game << " " << server_info.map  
150 - << " " << (int)server_info.players << "/" << (int)server_info.max_players << " players" << endl ;  
151 - break;  
152 -  
153 - case 1:  
154 - cout << " WARNING - Server is full";  
155 - break;  
156 -  
157 - case 2:  
158 - cout << " CRITICAL - No response";  
159 - break;  
160 - }  
161 -  
162 -  
163 - return returnCode; 105 +int main(int argc, char **argv) {
  106 + struct itimerval timer;
  107 + timer.it_value.tv_sec = 10;
  108 + timer.it_value.tv_usec = 0;
  109 + timer.it_interval.tv_sec = 0;
  110 + timer.it_interval.tv_usec = 0;
  111 + setitimer (ITIMER_VIRTUAL, &timer, 0);
  112 +
  113 + struct sigaction sa;
  114 + memset (&sa, 0, sizeof (sa));
  115 + sa.sa_handler = &timer_handler;
  116 + sigaction (SIGVTALRM, &sa, 0);
  117 +
  118 + uint16_t port = 27015;
  119 + char *hostname = NULL;
  120 + int c;
  121 +
  122 + while ((c = getopt (argc, argv, "H:p:Vh")) != -1) {
  123 + switch(c) {
  124 + case 'H':
  125 + hostname = optarg;
  126 + break;
  127 + case 'p':
  128 + port = (uint16_t) str2int(optarg);
  129 + break;
  130 + case 'V':
  131 + printVersion();
  132 + return 0;
  133 + case 'h':
  134 + printHelp(true);
  135 + return 0;
  136 + case '?':
  137 + printHelp(false);
  138 + return 3;
  139 + }
  140 + }
  141 +
  142 + if(hostname == NULL) {
  143 + cout << "No HOSTADDRESS specified. Exiting." << endl;
  144 + return 3;
  145 + }
  146 +
  147 + SERVERINFO server_info;
  148 + int returnCode = check_csgo(hostname,port,&server_info);
  149 +
  150 + cout << servicename;
  151 + switch(returnCode) {
  152 + case 0:
  153 + cout << " OK";
  154 + cout << " - " << hostname << " " << server_info.name << " " << server_info.game << " " << server_info.map
  155 + << " " << (int)server_info.players << "/" << (int)server_info.max_players << " players" << endl ;
  156 + break;
  157 +
  158 + case 1:
  159 + cout << " WARNING - Server is full";
  160 + break;
  161 + case 2:
  162 + cout << " CRITICAL - No response";
  163 + break;
  164 + }
  165 + return returnCode;
164 } 166 }