26 #include <system_error>
29 #include <sys/types.h>
30 #include <netinet/in.h>
32 #include <arpa/inet.h>
40 #include <curl/curl.h>
41 #include <curl/easy.h>
58 if (
pl->ob->map == NULL)
68 if (
pl->socket->is_bot)
154 static size_t metaserver2_writer(
void *ptr,
size_t size,
size_t nmemb,
void *
data) {
156 size_t realsize = size*nmemb;
157 LOG(
llevError,
"Message from metaserver:\n%s\n", (
const char*)ptr);
161 static void metaserver2_build_form(
struct curl_httppost **formpost) {
162 struct curl_httppost *lastptr = NULL;
171 curl_formadd(formpost, &lastptr,
172 CURLFORM_COPYNAME,
"hostname",
177 curl_formadd(formpost, &lastptr,
178 CURLFORM_COPYNAME,
"port",
179 CURLFORM_COPYCONTENTS,
buf,
182 curl_formadd(formpost, &lastptr,
183 CURLFORM_COPYNAME,
"html_comment",
187 curl_formadd(formpost, &lastptr,
188 CURLFORM_COPYNAME,
"text_comment",
192 curl_formadd(formpost, &lastptr,
193 CURLFORM_COPYNAME,
"archbase",
197 curl_formadd(formpost, &lastptr,
198 CURLFORM_COPYNAME,
"mapbase",
202 curl_formadd(formpost, &lastptr,
203 CURLFORM_COPYNAME,
"codebase",
207 curl_formadd(formpost, &lastptr,
208 CURLFORM_COPYNAME,
"flags",
215 curl_formadd(formpost, &lastptr,
216 CURLFORM_COPYNAME,
"num_players",
217 CURLFORM_COPYCONTENTS,
buf,
221 curl_formadd(formpost, &lastptr,
222 CURLFORM_COPYNAME,
"in_bytes",
223 CURLFORM_COPYCONTENTS,
buf,
227 curl_formadd(formpost, &lastptr,
228 CURLFORM_COPYNAME,
"out_bytes",
229 CURLFORM_COPYCONTENTS,
buf,
233 curl_formadd(formpost, &lastptr,
234 CURLFORM_COPYNAME,
"uptime",
235 CURLFORM_COPYCONTENTS,
buf,
243 curl_formadd(formpost, &lastptr,
244 CURLFORM_COPYNAME,
"version",
249 curl_formadd(formpost, &lastptr,
250 CURLFORM_COPYNAME,
"sc_version",
251 CURLFORM_COPYCONTENTS,
buf,
255 curl_formadd(formpost, &lastptr,
256 CURLFORM_COPYNAME,
"cs_version",
257 CURLFORM_COPYCONTENTS,
buf,
269 struct curl_httppost *formpost = NULL;
270 metaserver2_build_form(&formpost);
273 CURL *curl = curl_easy_init();
275 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30000);
278 curl_easy_setopt(curl, CURLOPT_URL, hostname.c_str());
279 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
285 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, metaserver2_writer);
287 char errbuf[CURL_ERROR_SIZE];
288 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
290 CURLcode
res = curl_easy_perform(curl);
292 LOG(
llevError,
"metaserver update failed: %s\n", errbuf);
296 curl_easy_cleanup(curl);
298 LOG(
llevError,
"metaserver: could not initialize curl\n");
302 curl_formfree(formpost);
339 static int has_init = 0;
345 curl_global_init(CURL_GLOBAL_ALL);
369 if ((fp = fopen(
buf,
"r")) == NULL) {
373 while (fgets(
buf,
sizeof(
buf), fp) != NULL) {
377 if ((cp = strrchr(
buf,
'\n')) != NULL)
386 if ((cp = strpbrk(
buf,
" \t")) != NULL) {
402 LOG(
llevError,
"metaserver2: Unknown value for metaserver2_notification: %s\n", cp);
408 LOG(
llevError,
"metaserver2: metaserver2_server must have a value.\n");
414 LOG(
llevError,
"metaserver2: localhostname must have a value.\n");
420 LOG(
llevError,
"metaserver2: portnumber must have a value.\n");
450 LOG(
llevError,
"metaserver2 file is set to do notification, but libcurl is not found.\n");
451 LOG(
llevError,
"Either fix your compilation, or turn off metaserver2 notification in \n");
481 catch (
const std::system_error &err) {
482 LOG(
llevError,
"metaserver2_init: failed to create thread, code %d, what %s\n", err.code().value(), err.what());