Debian Codegraph @

src/bntrackd/bntrackd.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1999 Mark Baysinger ([email protected])
3 * Copyright (C) 2000 Ross Combs ([email protected])
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19 #include "common/setup_before.h"
20 #define TRACKER_INTERNAL_ACCESS
21
22 #include <cinttypes>
23 #include <cstdio>
24 #include <cstring>
25 #include <ctime>
26 #include <cstdlib>
27
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31
32 #include "compat/stdfileno.h"
33 #include "compat/psock.h"
34 #include "compat/pgetpid.h"
35 #include "common/tracker.h"
36 #include "common/eventlog.h"
37 #include "common/list.h"
38 #include "common/xalloc.h"
39 #include "common/util.h"
40 #include "common/version.h"
41 #include "common/bn_type.h"
42
43 #ifdef HAVE_ARPA_INET_H
44 # include <arpa/inet.h>
45 #endif
46 #ifdef HAVE_WS2TCPIP_H
47 # include <Ws2tcpip.h>
48 #endif
49
50 #include "common/setup_after.h"
51
52 using namespace pvpgn;
53
54 namespace {
55
56 /******************************************************************************
57 * TYPES
58 *****************************************************************************/
59 typedef struct
60 {
61 struct in_addr address;
62 std::time_t updated;
63 t_trackpacket info;
64 } t_server;
65
66 typedef struct
67 {
68 int foreground;
69 int debug;
70 int XML_mode;
71 unsigned int expire;
72 unsigned int update;
73 unsigned short port;
74 char const * outfile;
75 char const * pidfile;
76 char const * logfile;
77 char const * process;
78 } t_prefs;
79
80
81 /******************************************************************************
82 * STATIC FUNCTION PROTOTYPES
83 *****************************************************************************/
84 int server_process(int sockfd);
85 void usage(char const * progname);
86 void getprefs(int argc, char * argv[]);
87 void fixup_str(bn_byte * str, unsigned int size);
88
89
90 /******************************************************************************
91 * GLOBAL VARIABLES
92 *****************************************************************************/
93 t_prefs prefs;
94 t_list * serverlist_head;
95 }
96
97
98 extern int main(int argc, char * argv[])
99 {
100 int sockfd;
101 int result;
102
103 if (argc < 1 || !argv || !argv[0])
104 {
105 std::fprintf(stderr, "bad arguments\n");
106 return EXIT_FAILURE;
107 }
108
109 getprefs(argc, argv);
110
111 if (!prefs.debug)
112 eventlog_del_level("debug");
113 if (prefs.logfile)
114 {
115 eventlog_set(stderr);
116 if (eventlog_open(prefs.logfile) < 0)
117 {
118 eventlog(eventlog_level_fatal, __FUNCTION__, "could not use file \"{}\" for the eventlog (exiting)", prefs.logfile);
119 return EXIT_FAILURE;
120 }
121 }
122
123 #ifdef DO_DAEMONIZE
124 if (!prefs.foreground)
125 {
126 switch (fork())
127 {
128 case -1:
129 eventlog(eventlog_level_error, __FUNCTION__, "could not fork (fork: {})", std::strerror(errno));
130 return EXIT_FAILURE;
131 case 0: /* child */
132 break;
133 default: /* parent */
134 return EXIT_SUCCESS;
135 }
136
137 close(STDINFD);
138 close(STDOUTFD);
139 close(STDERRFD);
140
141 # ifdef HAVE_SETPGID
142 if (setpgid(0, 0) < 0)
143 {
144 eventlog(eventlog_level_error, __FUNCTION__, "could not create new process group (setpgid: {})\n", std::strerror(errno));
145 return EXIT_FAILURE;
146 }
147 # else
148 # ifdef HAVE_SETPGRP
149 # ifdef SETPGRP_VOID
150 if (setpgrp() < 0)
151 {
152 eventlog(eventlog_level_error, __FUNCTION__, "could not create new process group (setpgrp: {})\n", std::strerror(errno));
153 return EXIT_FAILURE;
154 }
155 # else
156 if (setpgrp(0, 0) < 0)
157 {
158 eventlog(eventlog_level_error, __FUNCTION__, "could not create new process group (setpgrp: {})\n", std::strerror(errno));
159 return EXIT_FAILURE;
160 }
161 # endif
162 # else
163 # ifdef HAVE_SETSID
164 if (setsid() < 0)
165 {
166 eventlog(eventlog_level_error, __FUNCTION__, "could not create new process group (setsid: {})\n", std::strerror(errno));
167 return EXIT_FAILURE;
168 }
169 # else
170 # error "One of setpgid(), setpgrp(), or setsid() is required"
171 # endif
172 # endif
173 # endif
174 }
175 #endif
176
177 if (prefs.pidfile)
178 {
179 #ifdef HAVE_GETPID
180 std::FILE * fp;
181
182 if (!(fp = std::fopen(prefs.pidfile, "w")))
183 {
184 eventlog(eventlog_level_error, __FUNCTION__, "unable to open pid file \"{}\" for writing (std::fopen: {})", prefs.pidfile, std::strerror(errno));
185 prefs.pidfile = NULL;
186 }
187 else
188 {
189 std::fprintf(fp, "%u", (unsigned int)getpid());
190 if (std::fclose(fp) < 0)
191 eventlog(eventlog_level_error, __FUNCTION__, "could not close pid file \"{}\" after writing (std::fclose: {})", prefs.pidfile, std::strerror(errno));
192 }
193 #else
194 eventlog(eventlog_level_warn, __FUNCTION__, "no getpid() system call, do not use the -P or the --pidfile option");
195 prefs.pidfile = NULL;
196 #endif
197 }
198
199 #ifdef HAVE_GETPID
200 eventlog(eventlog_level_info, __FUNCTION__, "bntrackd version " PVPGN_VERSION " process {}", getpid());
201 #else
202 eventlog(eventlog_level_info, __FUNCTION__, "bntrackd version " PVPGN_VERSION);
203 #endif
204
205 if (psock_init() < 0)
206 {
207 eventlog(eventlog_level_error, __FUNCTION__, "could not initialize socket functions");
208 return EXIT_FAILURE;
209 }
210
211 /* create the socket */
212 if ((sockfd = psock_socket(PSOCK_PF_INET, PSOCK_SOCK_DGRAM, PSOCK_IPPROTO_UDP)) < 0)
213 {
214 eventlog(eventlog_level_error, __FUNCTION__, "could not create UDP listen socket (psock_socket: {})\n", std::strerror(psock_errno()));
215 return EXIT_FAILURE;
216 }
217
218 {
219 struct sockaddr_in servaddr;
220
221 /* bind the socket to correct port and interface */
222 std::memset(&servaddr, 0, sizeof(servaddr));
223 servaddr.sin_family = PSOCK_AF_INET;
224 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
225 servaddr.sin_port = htons(prefs.port);
226 if (psock_bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
227 {
228 eventlog(eventlog_level_error, __FUNCTION__, "could not bind to UDP port {} (psock_bind: {})\n", prefs.port, std::strerror(psock_errno()));
229 return EXIT_FAILURE;
230 }
231 }
232
233 if (!(serverlist_head = list_create()))
234 {
235 eventlog(eventlog_level_error, __FUNCTION__, "could not create server list");
236 return EXIT_FAILURE;;
237 }
238
239
240 result = server_process(sockfd);
241
242
243 if (serverlist_head != NULL){
244 list_destroy(serverlist_head);
245 }
246
247 if (result < 0)
248 return EXIT_FAILURE;
249
250 return EXIT_SUCCESS;
251 }
252
253 namespace {
254
255 int server_process(int sockfd)
256 {
257 t_elem * curr;
258 t_server * server;
259 struct sockaddr_in cliaddr;
260 t_psock_fd_set rfds;
261 struct timeval tv;
262 std::time_t last;
263 std::FILE * outfile;
264 psock_t_socklen len;
265 t_trackpacket packet;
266
267 /* the main loop */
268 last = std::time(NULL) - prefs.update;
269 for (;;)
270 {
271 /* time to dump our list to disk and call the process command */
272 /* (I'm making the assumption that this won't take very long.) */
273 if (last + (signed)prefs.update < std::time(NULL))
274 {
275 last = std::time(NULL);
276
277 if (!(outfile = std::fopen(prefs.outfile, "w")))
278 {
279 eventlog(eventlog_level_error, __FUNCTION__, "unable to open file \"{}\" for writing (std::fopen: {})", prefs.outfile, std::strerror(errno));
280 continue;
281 }
282
283 LIST_TRAVERSE(serverlist_head, curr)
284 {
285 server = (t_server*)elem_get_data(curr);
286
287 if (server->updated + (signed)prefs.expire < last)
288 {
289 list_remove_elem(serverlist_head, &curr);
290 xfree(server);
291 }
292 else
293 {
294 char addrstr[INET_ADDRSTRLEN] = { 0 };
295 inet_ntop(AF_INET, &(server->address), addrstr, sizeof(addrstr));
296 if (prefs.XML_mode == 1)
297 {
298 std::fprintf(outfile, "<server>\n\t<address>%s</address>\n", addrstr);
299 std::fprintf(outfile, "\t<port>%" PRIu16 "</port>\n", bn_short_nget(server->info.port));
300 std::fprintf(outfile, "\t<location>%s</location>\n", reinterpret_cast<char*>(server->info.server_location));
301 std::fprintf(outfile, "\t<software>%s</software>\n", reinterpret_cast<char*>(server->info.software));
302 std::fprintf(outfile, "\t<version>%s</version>\n", reinterpret_cast<char*>(server->info.version));
303 std::fprintf(outfile, "\t<users>%" PRIu32 "</users>\n", bn_int_nget(server->info.users));
304 std::fprintf(outfile, "\t<channels>%" PRIu32 "</channels>\n", bn_int_nget(server->info.channels));
305 std::fprintf(outfile, "\t<games>%" PRIu32 "</games>\n", bn_int_nget(server->info.games));
306 std::fprintf(outfile, "\t<description>%s</description>\n", reinterpret_cast<char*>(server->info.server_desc));
307 std::fprintf(outfile, "\t<platform>%s</platform>\n", reinterpret_cast<char*>(server->info.platform));
308 std::fprintf(outfile, "\t<url>%s</url>\n", reinterpret_cast<char*>(server->info.server_url));
309 std::fprintf(outfile, "\t<contact_name>%s</contact_name>\n", reinterpret_cast<char*>(server->info.contact_name));
310 std::fprintf(outfile, "\t<contact_email>%s</contact_email>\n", reinterpret_cast<char*>(server->info.contact_email));
311 std::fprintf(outfile, "\t<uptime>%" PRIu32 "</uptime>\n", bn_int_nget(server->info.uptime));
312 std::fprintf(outfile, "\t<total_games>%" PRIu32 "</total_games>\n", bn_int_nget(server->info.total_games));
313 std::fprintf(outfile, "\t<logins>%" PRIu32 "</logins>\n", bn_int_nget(server->info.total_logins));
314 std::fprintf(outfile, "</server>\n");
315 }
316 else
317 {
318 std::fprintf(outfile, "%s\n##\n", addrstr);
319 std::fprintf(outfile, "%" PRIu16 "\n##\n", bn_short_nget(server->info.port));
320 std::fprintf(outfile, "%s\n##\n", reinterpret_cast<char*>(server->info.server_location));
321 std::fprintf(outfile, "%s\n##\n", reinterpret_cast<char*>(server->info.software));
322 std::fprintf(outfile, "%s\n##\n", reinterpret_cast<char*>(server->info.version));
323 std::fprintf(outfile, "%" PRIu32 "\n##\n", bn_int_nget(server->info.users));
324 std::fprintf(outfile, "%" PRIu32 "\n##\n", bn_int_nget(server->info.channels));
325 std::fprintf(outfile, "%" PRIu32 "\n##\n", bn_int_nget(server->info.games));
326 std::fprintf(outfile, "%s\n##\n", reinterpret_cast<char*>(server->info.server_desc));
327 std::fprintf(outfile, "%s\n##\n", reinterpret_cast<char*>(server->info.platform));
328 std::fprintf(outfile, "%s\n##\n", reinterpret_cast<char*>(server->info.server_url));
329 std::fprintf(outfile, "%s\n##\n", reinterpret_cast<char*>(server->info.contact_name));
330 std::fprintf(outfile, "%s\n##\n", reinterpret_cast<char*>(server->info.contact_email));
331 std::fprintf(outfile, "%" PRIu32 "\n##\n", bn_int_nget(server->info.uptime));
332 std::fprintf(outfile, "%" PRIu32 "\n##\n", bn_int_nget(server->info.total_games));
333 std::fprintf(outfile, "%" PRIu32 "\n##\n", bn_int_nget(server->info.total_logins));
334 std::fprintf(outfile, "###\n");
335 }
336 }
337 }
338 if (std::fclose(outfile) < 0)
339 eventlog(eventlog_level_error, __FUNCTION__, "could not close output file \"{}\" after writing (std::fclose: {})", prefs.outfile, std::strerror(errno));
340
341 if (prefs.process[0] != '\0')
342 std::system(prefs.process);
343 }
344
345 /* select socket to operate on */
346 PSOCK_FD_ZERO(&rfds);
347 PSOCK_FD_SET(sockfd, &rfds);
348 tv.tv_sec = BNTRACKD_GRANULARITY;
349 tv.tv_usec = 0;
350 switch (psock_select(sockfd + 1, &rfds, NULL, NULL, &tv))
351 {
352 case -1: /* error */
353 if (
354 #ifdef PSOCK_EINTR
355 errno != PSOCK_EINTR &&
356 #endif
357 1)
358 eventlog(eventlog_level_error, __FUNCTION__, "select failed (select: {})", std::strerror(errno));
359 case 0: /* timeout and no sockets ready */
360 continue;
361 }
362
363 /* New tracking packet */
364 if (PSOCK_FD_ISSET(sockfd, &rfds))
365 {
366
367 len = sizeof(cliaddr);
368 if (psock_recvfrom(sockfd, &packet, sizeof(packet), 0, (struct sockaddr *)&cliaddr, &len) >= 0)
369 {
370
371 if (bn_short_nget(packet.packet_version) >= TRACK_VERSION)
372 {
373 bn_byte_set(&packet.software[sizeof(packet.software) - 1], '\0');
374 fixup_str(packet.software, sizeof(packet.software));
375 bn_byte_set(&packet.version[sizeof(packet.version) - 1], '\0');
376 fixup_str(packet.version, sizeof(packet.version));
377 bn_byte_set(&packet.platform[sizeof(packet.platform) - 1], '\0');
378 fixup_str(packet.platform, sizeof(packet.platform));
379 bn_byte_set(&packet.server_desc[sizeof(packet.server_desc) - 1], '\0');
380 fixup_str(packet.server_desc, sizeof(packet.server_desc));
381 bn_byte_set(&packet.server_location[sizeof(packet.server_location) - 1], '\0');
382 fixup_str(packet.server_location, sizeof(packet.server_location));
383 bn_byte_set(&packet.server_url[sizeof(packet.server_url) - 1], '\0');
384 fixup_str(packet.server_url, sizeof(packet.server_url));
385 bn_byte_set(&packet.contact_name[sizeof(packet.contact_name) - 1], '\0');
386 fixup_str(packet.contact_name, sizeof(packet.contact_name));
387 bn_byte_set(&packet.contact_email[sizeof(packet.contact_email) - 1], '\0');
388 fixup_str(packet.contact_email, sizeof(packet.contact_email));
389
390 /* Find this server's slot */
391 LIST_TRAVERSE(serverlist_head, curr)
392 {
393 server = (t_server*)elem_get_data(curr);
394
395 if (!std::memcmp(&server->address, &cliaddr.sin_addr, sizeof(struct in_addr)))
396 {
397 if (bn_int_nget(packet.flags)&TF_SHUTDOWN)
398 {
399 list_remove_elem(serverlist_head, &curr);
400 xfree(server);
401 }
402 else
403 {
404 /* update in place */
405 server->info = packet;
406 server->updated = std::time(NULL);
407 }
408 break;
409 }
410 }
411
412 /* Not found? Make a new slot */
413 if (!(bn_int_nget(packet.flags)&TF_SHUTDOWN) && !curr)
414 {
415 server = (t_server*)xmalloc(sizeof(t_server));
416 server->address = cliaddr.sin_addr;
417 server->info = packet;
418 server->updated = std::time(NULL);
419
420 list_append_data(serverlist_head, server);
421 }
422
423 char addrstr2[INET_ADDRSTRLEN] = { 0 };
424 inet_ntop(AF_INET, &(cliaddr.sin_addr), addrstr2, sizeof(addrstr2));
425
426 eventlog(eventlog_level_debug, __FUNCTION__,
427 "Packet received from {}:"
428 " packet_version={}"
429 " flags=0x{:08}"
430 " port={}"
431 " software=\"{}\""
432 " version=\"{}\""
433 " platform=\"{}\""
434 " server_desc=\"{}\""
435 " server_location=\"{}\""
436 " server_url=\"{}\""
437 " contact_name=\"{}\""
438 " contact_email=\"{}\""
439 " uptime={}"
440 " total_games={}"
441 " total_logins={}",
442 addrstr2,
443 bn_short_nget(packet.packet_version),
444 bn_int_nget(packet.flags),
445 bn_short_nget(packet.port),
446 reinterpret_cast<char*>(packet.software),
447 reinterpret_cast<char*>(packet.version),
448 reinterpret_cast<char*>(packet.platform),
449 reinterpret_cast<char*>(packet.server_desc),
450 reinterpret_cast<char*>(packet.server_location),
451 reinterpret_cast<char*>(packet.server_url),
452 reinterpret_cast<char*>(packet.contact_name),
453 reinterpret_cast<char*>(packet.contact_email),
454 bn_int_nget(packet.uptime),
455 bn_int_nget(packet.total_games),
456 bn_int_nget(packet.total_logins));
457 }
458
459 }
460 }
461
462 }
463 }
464
465
466 void usage(char const * progname)
467 {
468 std::fprintf(stderr, "usage: %s [<options>]\n", progname);
469 std::fprintf(stderr,
470 " -c COMMAND, --command=COMMAND execute COMMAND update\n"
471 " -d, --debug turn on debug mode\n"
472 " -e SECS, --expire SECS forget a list entry after SEC seconds\n"
473 #ifdef DO_DAEMONIZE
474 " -f, --foreground don't daemonize\n"
475 #else
476 " -f, --foreground don't daemonize (default)\n"
477 #endif
478 " -l FILE, --logfile=FILE write event messages to FILE\n"
479 " -o FILE, --outfile=FILE write server list to FILE\n");
480 std::fprintf(stderr,
481 " -p PORT, --port=PORT listen for announcments on UDP port PORT\n"
482 " -P FILE, --pidfile=FILE write pid to FILE\n"
483 " -u SECS, --update SECS write output file every SEC seconds\n"
484 " -x, --XML write output file in XML format\n"
485 " -h, --help, --usage show this information and exit\n"
486 " -v, --version print version number and exit\n");
487 std::exit(EXIT_FAILURE);
488 }
489
490
491 void getprefs(int argc, char * argv[])
492 {
493 int a;
494
495 prefs.foreground = 0;
496 prefs.debug = 0;
497 prefs.expire = 0;
498 prefs.update = 0;
499 prefs.port = 0;
500 prefs.XML_mode = 0;
501 prefs.outfile = NULL;
502 prefs.pidfile = NULL;
503 prefs.process = NULL;
504 prefs.logfile = NULL;
505
506 for (a = 1; a < argc; a++)
507 if (std::strncmp(argv[a], "--command=", 10) == 0)
508 {
509 if (prefs.process)
510 {
511 std::fprintf(stderr, "%s: processing command was already specified as \"%s\"\n", argv[0], prefs.process);
512 usage(argv[0]);
513 }
514 prefs.process = &argv[a][10];
515 }
516 else if (std::strcmp(argv[a], "-c") == 0)
517 {
518 if (a + 1 >= argc)
519 {
520 std::fprintf(stderr, "%s: option \"%s\" requires an argument\n", argv[0], argv[a]);
521 usage(argv[0]);
522 }
523 if (prefs.process)
524 {
525 std::fprintf(stderr, "%s: processing command was already specified as \"%s\"\n", argv[0], prefs.process);
526 usage(argv[0]);
527 }
528 a++;
529 prefs.process = argv[a];
530 }
531 else if (std::strcmp(argv[a], "-d") == 0 || std::strcmp(argv[a], "--debug") == 0)
532 prefs.debug = 1;
533 else if (std::strncmp(argv[a], "--expire=", 9) == 0)
534 {
535 if (prefs.expire)
536 {
537 std::fprintf(stderr, "%s: expiration period was already specified as \"%u\"\n", argv[0], prefs.expire);
538 usage(argv[0]);
539 }
540 if (str_to_uint(&argv[a][9], &prefs.expire) < 0)
541 {
542 std::fprintf(stderr, "%s: \"%s\" should be a positive integer\n", argv[0], &argv[a][9]);
543 usage(argv[0]);
544 }
545 }
546 else if (std::strcmp(argv[a], "-e") == 0)
547 {
548 if (a + 1 >= argc)
549 {
550 std::fprintf(stderr, "%s: option \"%s\" requires an argument\n", argv[0], argv[a]);
551 usage(argv[0]);
552 }
553 if (prefs.expire)
554 {
555 std::fprintf(stderr, "%s: expiration period was already specified as \"%u\"\n", argv[0], prefs.expire);
556 usage(argv[0]);
557 }
558 a++;
559 if (str_to_uint(argv[a], &prefs.expire) < 0)
560 {
561 std::fprintf(stderr, "%s: \"%s\" should be a positive integer\n", argv[0], argv[a]);
562 usage(argv[0]);
563 }
564 }
565 else if (std::strcmp(argv[a], "-f") == 0 || std::strcmp(argv[a], "--foreground") == 0)
566 prefs.foreground = 1;
567 else if (std::strcmp(argv[a], "-x") == 0 || std::strcmp(argv[a], "--XML") == 0)
568 prefs.XML_mode = 1;
569 else if (std::strncmp(argv[a], "--logfile=", 10) == 0)
570 {
571 if (prefs.logfile)
572 {
573 std::fprintf(stderr, "%s: eventlog file was already specified as \"%s\"\n", argv[0], prefs.logfile);
574 usage(argv[0]);
575 }
576 prefs.logfile = &argv[a][10];
577 }
578 else if (std::strcmp(argv[a], "-l") == 0)
579 {
580 if (a + 1 >= argc)
581 {
582 std::fprintf(stderr, "%s: option \"%s\" requires an argument\n", argv[0], argv[a]);
583 usage(argv[0]);
584 }
585 if (prefs.logfile)
586 {
587 std::fprintf(stderr, "%s: eventlog file was already specified as \"%s\"\n", argv[0], prefs.logfile);
588 usage(argv[0]);
589 }
590 a++;
591 prefs.logfile = argv[a];
592 }
593 else if (std::strncmp(argv[a], "--outfile=", 10) == 0)
594 {
595 if (prefs.outfile)
596 {
597 std::fprintf(stderr, "%s: output file was already specified as \"%s\"\n", argv[0], prefs.outfile);
598 usage(argv[0]);
599 }
600 prefs.outfile = &argv[a][10];
601 }
602 else if (std::strcmp(argv[a], "-o") == 0)
603 {
604 if (a + 1 >= argc)
605 {
606 std::fprintf(stderr, "%s: option \"%s\" requires an argument\n", argv[0], argv[a]);
607 usage(argv[0]);
608 }
609 if (prefs.outfile)
610 {
611 std::fprintf(stderr, "%s: output file was already specified as \"%s\"\n", argv[0], prefs.outfile);
612 usage(argv[0]);
613 }
614 a++;
615 prefs.outfile = argv[a];
616 }
617 else if (std::strncmp(argv[a], "--pidfile=", 10) == 0)
618 {
619 if (prefs.pidfile)
620 {
621 std::fprintf(stderr, "%s: pid file was already specified as \"%s\"\n", argv[0], prefs.pidfile);
622 usage(argv[0]);
623 }
624 prefs.pidfile = &argv[a][10];
625 }
626 else if (std::strncmp(argv[a], "--port=", 7) == 0)
627 {
628 if (prefs.port)
629 {
630 std::fprintf(stderr, "%s: port number was already specified as \"%hu\"\n", argv[0], prefs.port);
631 usage(argv[0]);
632 }
633 if (str_to_ushort(&argv[a][7], &prefs.port) < 0)
634 {
635 std::fprintf(stderr, "%s: \"%s\" should be a positive integer\n", argv[0], &argv[a][7]);
636 usage(argv[0]);
637 }
638 }
639 else if (std::strcmp(argv[a], "-p") == 0)
640 {
641 if (a + 1 >= argc)
642 {
643 std::fprintf(stderr, "%s: option \"%s\" requires an argument\n", argv[0], argv[a]);
644 usage(argv[0]);
645 }
646 if (prefs.port)
647 {
648 std::fprintf(stderr, "%s: port number was already specified as \"%hu\"\n", argv[0], prefs.port);
649 usage(argv[0]);
650 }
651 a++;
652 if (str_to_ushort(argv[a], &prefs.port) < 0)
653 {
654 std::fprintf(stderr, "%s: \"%s\" should be a positive integer\n", argv[0], argv[a]);
655 usage(argv[0]);
656 }
657 }
658 else if (std::strcmp(argv[a], "-P") == 0)
659 {
660 if (a + 1 >= argc)
661 {
662 std::fprintf(stderr, "%s: option \"%s\" requires an argument\n", argv[0], argv[a]);
663 usage(argv[0]);
664 }
665 if (prefs.pidfile)
666 {
667 std::fprintf(stderr, "%s: pid file was already specified as \"%s\"\n", argv[0], prefs.pidfile);
668 usage(argv[0]);
669 }
670 a++;
671 prefs.pidfile = argv[a];
672 }
673 else if (std::strncmp(argv[a], "--update=", 9) == 0)
674 {
675 if (prefs.update)
676 {
677 std::fprintf(stderr, "%s: update period was already specified as \"%u\"\n", argv[0], prefs.expire);
678 usage(argv[0]);
679 }
680 if (str_to_uint(&argv[a][9], &prefs.update) < 0)
681 {
682 std::fprintf(stderr, "%s: \"%s\" should be a positive integer\n", argv[0], &argv[a][9]);
683 usage(argv[0]);
684 }
685 }
686 else if (std::strcmp(argv[a], "-u") == 0)
687 {
688 if (a + 1 >= argc)
689 {
690 std::fprintf(stderr, "%s: option \"%s\" requires an argument\n", argv[0], argv[a]);
691 usage(argv[0]);
692 }
693 if (prefs.update)
694 {
695 std::fprintf(stderr, "%s: update period was already specified as \"%u\"\n", argv[0], prefs.expire);
696 usage(argv[0]);
697 }
698 a++;
699 if (str_to_uint(argv[a], &prefs.update) < 0)
700 {
701 std::fprintf(stderr, "%s: \"%s\" should be a positive integer\n", argv[0], argv[a]);
702 usage(argv[0]);
703 }
704 }
705 else if (std::strcmp(argv[a], "-h") == 0 || std::strcmp(argv[a], "--help") == 0 || std::strcmp(argv[a], "--usage")
706 == 0)
707 usage(argv[0]);
708 else if (std::strcmp(argv[a], "-v") == 0 || std::strcmp(argv[a], "--version") == 0)
709 {
710 std::printf("version " PVPGN_VERSION "\n");
711 std::exit(0);
712 }
713 else if (std::strcmp(argv[a], "--command") == 0 || std::strcmp(argv[a], "--expire") == 0 ||
714 std::strcmp(argv[a], "--logfile") == 0 || std::strcmp(argv[a], "--outfile") == 0 ||
715 std::strcmp(argv[a], "--port") == 0 || std::strcmp(argv[a], "--pidfile") == 0 ||
716 std::strcmp(argv[a], "--update") == 0)
717 {
718 std::fprintf(stderr, "%s: option \"%s\" requires and argument.\n", argv[0], argv[a]);
719 usage(argv[0]);
720 }
721 else
722 {
723 std::fprintf(stderr, "%s: unknown option \"%s\"\n", argv[0], argv[a]);
724 usage(argv[0]);
725 }
726
727 if (!prefs.process)
728 prefs.process = BNTRACKD_PROCESS;
729 if (prefs.expire == 0)
730 prefs.expire = BNTRACKD_EXPIRE;
731 if (!prefs.logfile)
732 prefs.logfile = BNTRACKD_LOGFILE;
733 if (!prefs.outfile)
734 prefs.outfile = BNTRACKD_OUTFILE;
735 if (prefs.port == 0)
736 prefs.port = BNTRACKD_SERVER_PORT;
737 if (!prefs.pidfile)
738 prefs.pidfile = BNTRACKD_PIDFILE;
739 if (prefs.expire == 0)
740 prefs.update = BNTRACKD_UPDATE;
741
742 if (prefs.logfile[0] == '\0')
743 prefs.logfile = NULL;
744 if (prefs.pidfile[0] == '\0')
745 prefs.pidfile = NULL;
746 }
747
748
749 void fixup_str(bn_byte * str, unsigned int size)
750 {
751 bn_byte prev;
752 unsigned int i;
753
754 bn_byte_set(&prev, '\0');
755
756 for (i = 0; i < size, bn_byte_get(str[i]) != '\0'; bn_byte_set(&prev, bn_byte_get(str[i])), i++)
757 if (bn_byte_get(prev) == '#' && bn_byte_get(str[i]) == '#')
758 bn_byte_set(&str[i], '%');
759 }
760
761 }