~ubuntu-branches/debian/sid/freeciv/sid

« back to all changes in this revision

Viewing changes to client/connectdlg_common.c

  • Committer: Bazaar Package Importer
  • Author(s): Clint Adams, Karl Goetz, Clint Adams
  • Date: 2010-02-23 22:09:02 UTC
  • mfrom: (7.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20100223220902-s3spqi1x4e190y0t
[ Karl Goetz ]
* Remove civserver files in /etc/ggzd/ (Closes: 523772, 517787)
* Adding ${misc:Depends} to all binary packages (lintian warnings)

[ Clint Adams ]
* New upstream version.
  - Drop data_dsc_use_bindir.diff (binary pathnames have changed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
#include <sys/wait.h>
38
38
#endif
39
39
 
 
40
/* common & utility */
40
41
#include "capability.h"
 
42
#include "fciconv.h"
41
43
#include "fcintl.h"
 
44
#include "ioz.h"
42
45
#include "log.h"
43
46
#include "mem.h"
44
47
#include "netintf.h"
 
48
#include "rand.h"
45
49
#include "registry.h"
46
 
#include "shared.h"
47
50
#include "support.h"
48
51
 
49
 
#include "civclient.h"
 
52
/* client */
 
53
#include "client_main.h"
50
54
#include "climisc.h"
51
 
#include "clinet.h"
 
55
#include "clinet.h"             /* connect_to_server() */
52
56
#include "packhand_gen.h"
53
57
 
54
58
#include "chatline_common.h"
67
71
static pid_t server_pid = - 1;
68
72
#endif
69
73
 
70
 
char leader_name[MAX_LEN_NAME];
71
 
 
72
74
static char challenge_fullname[MAX_LEN_PATH];
73
75
static bool client_has_hack = FALSE;
74
76
 
75
77
int internal_server_port;
76
78
 
77
 
const char *skill_level_names[NUM_SKILL_LEVELS] = { 
78
 
  N_("novice"),
79
 
  N_("easy"), 
80
 
  N_("normal"), 
81
 
  N_("hard")
82
 
 ,N_("experimental")
83
 
};
84
 
 
85
79
/************************************************************************** 
86
80
The general chain of events:
87
81
 
137
131
void client_kill_server(bool force)
138
132
{
139
133
  if (is_server_running()) {
140
 
    if (aconnection.used) {
 
134
    if (client.conn.used) {
141
135
      /* This does a "soft" shutdown of the server by sending a /quit.
142
136
       *
143
137
       * This is useful when closing the client or disconnecting because it
189
183
#if !defined(HAVE_WORKING_FORK) && !defined(WIN32_NATIVE)
190
184
  /* Can't do much without fork */
191
185
  return FALSE;
192
 
#else
 
186
#else /* HAVE_WORKING_FORK || WIN32_NATIVE */
193
187
  char buf[512];
194
188
  int connect_tries = 0;
195
189
# ifdef WIN32_NATIVE
203
197
  char cmdline3[512];
204
198
  char logcmdline[512];
205
199
  char scriptcmdline[512];
206
 
# endif
 
200
  char savescmdline[512];
 
201
# endif /* WIN32_NATIVE */
207
202
 
208
203
  /* only one server (forked from this client) shall be running at a time */
209
204
  /* This also resets client_has_hack. */
210
205
  client_kill_server(TRUE);
211
206
  
212
 
  append_output_window(_("Starting server..."));
 
207
  output_window_append(ftc_client, _("Starting server..."));
213
208
 
214
209
  /* find a free port */ 
215
210
  internal_server_port = find_next_free_port(DEFAULT_SOCK_PORT);
226
221
 
227
222
    /* Set up the command-line parameters. */
228
223
    my_snprintf(port_buf, sizeof(port_buf), "%d", internal_server_port);
229
 
    argv[argc++] = "civserver";
 
224
    argv[argc++] = "freeciv-server";
230
225
    argv[argc++] = "-p";
231
226
    argv[argc++] = port_buf;
232
227
    argv[argc++] = "-q";
234
229
    argv[argc++] = "-e";
235
230
    argv[argc++] = "--saves";
236
231
    argv[argc++] = "~/.freeciv/saves";
 
232
    argv[argc++] = "--scenarios";
 
233
    argv[argc++] = "~/.freeciv/scenarios";
237
234
    if (logfile) {
238
235
      argv[argc++] = "--debug";
239
236
      argv[argc++] = "3";
265
262
    }
266
263
 
267
264
    /* If it's still attatched to our terminal, things get messed up, 
268
 
      but civserver needs *something* */ 
 
265
      but freeciv-server needs *something* */ 
269
266
    fclose(stdin);
270
267
    fd = open("/dev/null", O_RDONLY);
271
268
    if (fd != 0) {
274
271
 
275
272
    /* these won't return on success */ 
276
273
    execvp("./ser", argv);
277
 
    execvp("./server/civserver", argv);
278
 
    execvp("civserver", argv);
 
274
    execvp("./server/freeciv-server", argv);
 
275
    execvp("freeciv-server", argv);
279
276
    
280
 
    /* This line is only reached if civserver cannot be started, 
 
277
    /* This line is only reached if freeciv-server cannot be started, 
281
278
     * so we kill the forked process.
282
279
     * Calling exit here is dangerous due to X11 problems (async replies) */ 
283
280
    _exit(1);
284
281
  } 
285
 
# else
 
282
# else /* HAVE_WORKING_FORK */
286
283
#  ifdef WIN32_NATIVE
287
284
  if (logfile) {
288
285
    loghandle = CreateFile(logfile, GENERIC_WRITE,
302
299
  logcmdline[0] = 0;
303
300
  scriptcmdline[0] = 0;
304
301
 
 
302
  /* the server expects command line arguments to be in local encoding */ 
305
303
  if (logfile) {
 
304
    char *logfile_in_local_encoding = internal_to_local_string_malloc(logfile);
306
305
    my_snprintf(logcmdline, sizeof(logcmdline), " --debug 3 --log %s",
307
 
                logfile);
 
306
                logfile_in_local_encoding);
 
307
    free(logfile_in_local_encoding);
308
308
  }
309
309
  if (scriptfile) {
 
310
    char *scriptfile_in_local_encoding = internal_to_local_string_malloc(scriptfile);
310
311
    my_snprintf(scriptcmdline, sizeof(scriptcmdline),  " --read %s",
311
 
                scriptfile);
 
312
                scriptfile_in_local_encoding);
 
313
    free(scriptfile_in_local_encoding);
312
314
  }
313
315
 
314
316
  interpret_tilde(savesdir, sizeof(savesdir), "~/.freeciv/saves");
 
317
  internal_to_local_string_buffer(savesdir, savescmdline, sizeof(savescmdline));
315
318
 
316
319
  my_snprintf(options, sizeof(options), "-p %d -q 1 -e%s%s --saves \"%s\"",
317
 
              internal_server_port, logcmdline, scriptcmdline, savesdir);
 
320
              internal_server_port, logcmdline, scriptcmdline, savescmdline);
318
321
  my_snprintf(cmdline1, sizeof(cmdline1), "./ser %s", options);
319
 
  my_snprintf(cmdline2, sizeof(cmdline2), "./server/civserver %s", options);
320
 
  my_snprintf(cmdline3, sizeof(cmdline3), "civserver %s", options);
 
322
  my_snprintf(cmdline2, sizeof(cmdline2),
 
323
              "./server/freeciv-server %s", options);
 
324
  my_snprintf(cmdline3, sizeof(cmdline3),
 
325
              "freeciv-server %s", options);
321
326
 
322
327
  if (!CreateProcess(NULL, cmdline1, NULL, NULL, TRUE,
323
328
                     DETACHED_PROCESS | NORMAL_PRIORITY_CLASS,
328
333
      && !CreateProcess(NULL, cmdline3, NULL, NULL, TRUE,
329
334
                        DETACHED_PROCESS | NORMAL_PRIORITY_CLASS,
330
335
                        NULL, NULL, &si, &pi)) {
331
 
    append_output_window(_("Couldn't start the server."));
332
 
    append_output_window(_("You'll have to " "start one manually. Sorry..."));
 
336
    output_window_append(ftc_client, _("Couldn't start the server."));
 
337
    output_window_append(ftc_client,
 
338
                         _("You'll have to start one manually. Sorry..."));
333
339
    return FALSE;
334
340
  }
335
341
  
336
342
  server_process = pi.hProcess;
337
343
 
338
 
#  endif
339
 
# endif
 
344
#  endif /* WIN32_NATIVE */
 
345
# endif /* HAVE_WORKING_FORK */
340
346
 
341
347
  /* a reasonable number of tries */ 
342
348
  while (connect_to_server(user_name, "localhost", internal_server_port, 
347
353
    if (waitpid(server_pid, NULL, WNOHANG) != 0) {
348
354
      break;
349
355
    }
350
 
#endif
351
 
#endif
 
356
#endif /* WIN32_NATIVE */
 
357
#endif /* HAVE_WORKING_FORK */
352
358
    if (connect_tries++ > NUMBER_OF_TRIES) {
353
359
      break;
354
360
    }
356
362
 
357
363
  /* weird, but could happen, if server doesn't support new startup stuff
358
364
   * capabilities won't help us here... */ 
359
 
  if (!aconnection.used) {
 
365
  if (!client.conn.used) {
360
366
    /* possible that server is still running. kill it */ 
361
367
    client_kill_server(TRUE);
362
368
 
363
 
    append_output_window(_("Couldn't connect to the server."));
364
 
    append_output_window(_("We probably couldn't start it from here."));
365
 
    append_output_window(_("You'll have to start one manually. Sorry..."));
 
369
    output_window_append(ftc_client, _("Couldn't connect to the server."));
 
370
    output_window_append(ftc_client,
 
371
                         _("We probably couldn't start it from here."));
 
372
    output_window_append(ftc_client,
 
373
                         _("You'll have to start one manually. Sorry..."));
366
374
    return FALSE;
367
375
  }
368
376
 
381
389
   * Setting the option here is a bit of a hack, but so long as the client
382
390
   * has sufficient permissions to do so (it doesn't have HACK access yet) it
383
391
   * is safe enough.  Note that if you load a savegame the topology will be
384
 
   * set but then overwritten during the load. */
385
 
  my_snprintf(buf, sizeof(buf), "/set topology %d",
386
 
              (TF_WRAPX
387
 
               | ((tileset_is_isometric(tileset)
388
 
                   && tileset_hex_height(tileset) == 0) ? TF_ISO : 0)
389
 
               | ((tileset_hex_width(tileset) != 0
390
 
                   || tileset_hex_height(tileset) != 0) ? TF_HEX : 0)));
391
 
  send_chat(buf);
 
392
   * set but then overwritten during the load.
 
393
   *
 
394
   * Don't send it now, it will be sent to the server when receiving the
 
395
   * server setting infos. */
 
396
  {
 
397
    char buf[16];
 
398
 
 
399
    my_snprintf(buf, sizeof(buf), "%d",
 
400
                (TF_WRAPX
 
401
                 | ((tileset_is_isometric(tileset)
 
402
                    && tileset_hex_height(tileset) == 0) ? TF_ISO : 0)
 
403
                 | ((tileset_hex_width(tileset) != 0
 
404
                    || tileset_hex_height(tileset) != 0) ? TF_HEX : 0)));
 
405
    desired_settable_option_update("topology", buf, FALSE);
 
406
  }
392
407
 
393
408
  return TRUE;
394
 
#endif
 
409
#endif /* HAVE_WORKING_FORK || WIN32_NATIVE */
 
410
}
 
411
 
 
412
/*************************************************************************
 
413
  generate a random string.
 
414
*************************************************************************/
 
415
static void randomize_string(char *str, size_t n)
 
416
{
 
417
  const char chars[] =
 
418
    "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
419
  int i;
 
420
 
 
421
  for (i = 0; i < n - 1; i++) {
 
422
    str[i] = chars[myrand(sizeof(chars) - 1)];
 
423
  }
 
424
  str[i] = '\0';
395
425
}
396
426
 
397
427
/*************************************************************************
436
466
    sz_strlcat(challenge_fullname, filename);
437
467
 
438
468
    /* generate an authentication token */ 
439
 
    randomize_base64url_string(req.token, sizeof(req.token));
 
469
    randomize_string(req.token, sizeof(req.token));
440
470
 
441
471
    section_file_init(&file);
442
472
    secfile_insert_str(&file, req.token, "challenge.token");
443
 
    if (!section_file_save(&file, challenge_fullname, 0)) {
 
473
    if (!section_file_save(&file, challenge_fullname, 0, FZ_PLAIN)) {
444
474
      freelog(LOG_ERROR, "Couldn't write token to temporary file: %s",
445
475
              challenge_fullname);
446
476
    }
447
477
    section_file_free(&file);
448
478
 
449
479
    /* tell the server what we put into the file */ 
450
 
    send_packet_single_want_hack_req(&aconnection, &req);
 
480
    send_packet_single_want_hack_req(&client.conn, &req);
451
481
  }
452
482
}
453
483
 
454
484
/**************************************************************** 
455
 
  client has hack (or not).
 
485
handle response (by the server) if the client has got hack or not.
456
486
*****************************************************************/ 
457
487
void handle_single_want_hack_reply(bool you_have_hack)
458
488
{
459
489
  /* remove challenge file */
460
490
  if (challenge_fullname[0] != '\0') {
461
 
    if (remove(challenge_fullname) == -1) {
 
491
    if (fc_remove(challenge_fullname) == -1) {
462
492
      freelog(LOG_ERROR, "Couldn't remove temporary file: %s",
463
493
              challenge_fullname);
464
494
    }
466
496
  }
467
497
 
468
498
  if (you_have_hack) {
469
 
    append_output_window(_("Established control over the server. "
 
499
    output_window_append(ftc_client,
 
500
                         _("Established control over the server. "
470
501
                           "You have command access level 'hack'."));
471
502
    client_has_hack = TRUE;
472
503
  } else if (is_server_running()) {
473
504
    /* only output this if we started the server and we NEED hack */
474
 
    append_output_window(_("Failed to obtain the required access "
 
505
    output_window_append(ftc_client,
 
506
                         _("Failed to obtain the required access "
475
507
                           "level to take control of the server. "
476
508
                           "The server will now be shutdown."));
477
509
    client_kill_server(TRUE);
479
511
}
480
512
 
481
513
/**************************************************************** 
482
 
send server commands to start a saved game.
483
 
*****************************************************************/ 
484
 
void send_start_saved_game(void)
485
 
{   
486
 
  char buf[MAX_LEN_MSG];
487
 
 
488
 
  send_chat("/set timeout 0");
489
 
  my_snprintf(buf, sizeof(buf), "/take \"%s\" \"%s\"",
490
 
              user_name, leader_name);
491
 
  send_chat(buf);
492
 
  send_chat("/start");
493
 
}
494
 
 
495
 
/**************************************************************** 
496
514
send server command to save game.
497
515
*****************************************************************/ 
498
516
void send_save_game(char *filename)
499
517
{   
500
 
  char message[MAX_LEN_MSG];
501
 
 
502
518
  if (filename) {
503
 
    my_snprintf(message, MAX_LEN_MSG, "/save %s", filename);
 
519
    send_chat_printf("/save %s", filename);
504
520
  } else {
505
 
    my_snprintf(message, MAX_LEN_MSG, "/save");
 
521
    send_chat("/save");
506
522
  }
507
 
 
508
 
  send_chat(message);
509
523
}
510
524
 
511
525
/**************************************************************************