Advertisement
Guest User

mcrcon

a guest
Nov 30th, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 19.91 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2012-2013, Tiiffi <tiiffi -> gmail_dot_com>
  3.  *
  4.  * This software is provided 'as-is', without any express or implied
  5.  * warranty. In no event will the authors be held liable for any damages
  6.  * arising from the use of this software.
  7.  *
  8.  * Permission is granted to anyone to use this software for any purpose,
  9.  * including commercial applications, and to alter it and redistribute it
  10.  * freely, subject to the following restrictions:
  11.  *
  12.  *   1. The origin of this software must not be misrepresented; you must not
  13.  *   claim that you wrote the original software. If you use this software
  14.  *   in a product, an acknowledgment in the product documentation would be
  15.  *   appreciated but is not required.
  16.  *
  17.  *   2. Altered source versions must be plainly marked as such, and must not be
  18.  *   misrepresented as being the original software.
  19.  *
  20.  *   3. This notice may not be removed or altered from any source
  21.  *   distribution.
  22.  */
  23.  
  24. /*
  25.  * Name:     mcrcon (minecraft rcon)
  26.  * Version:  0.0.5
  27.  * Date:     31.08.2012
  28.  *
  29.  * License: zlib/libpng License
  30.  *
  31.  *
  32.  * Contact:
  33.  *  WWW:  http://sourceforge.net/projects/mcrcon/
  34.  *  MAIL: tiiffi_at_gmail_dot_com
  35.  *  IRC:  tiiffi @ quakenet
  36.  *
  37.  *
  38.  * Description:
  39.  *  Mcrcon is powerful IPv6 compliant minecraft rcon client with bukkit coloring support.
  40.  *  It is well suited for remote administration and to be used as part of automated server maintenance scripts.
  41.  *  Does not cause "IO: Broken pipe" or "IO: Connection reset" spam in server console.
  42.  *
  43.  *
  44.  * Features:
  45.  *  - Interacive terminal mode. Keeps the connection alive.
  46.  *  - Send multiple commands in one command line.
  47.  *  - Silent mode. Does not print rcon output.
  48.  *  - Support for bukkit coloring on Windows and Linux (sh compatible shells).
  49.  *  - Multiplatform code. Compiles on many platforms with minor changes.
  50.  *  - IPv6 support.
  51.  *
  52.  *
  53.  * Version history:
  54.  *
  55.  * 0.0.5
  56.  *
  57.  *  - IPv6 support!
  58.  *     * Thanks to 'Tanja84dk' for addressing the real need of IPv6.
  59.  *
  60.  *  - Fixed bug causing crash / segmentation fault (invalid write) when receiving malformed rcon packet.
  61.  *
  62.  *  - Program makes use of C99 feature (variable-length arrays) so "-std=gnu99" flag on
  63.  *    GCC-compiler must be used to avoid unecessary warnings.
  64.  *
  65.  *  - Rcon receive buffer is now bigger (2024 bytes -> 10240 bytes).
  66.  *     * Thanks to 'gman_ftw' @ Bukkit forums.
  67.  *
  68.  *  - Fixed invalid error message when receiving empty rcon packet (10 bytes).
  69.  *     * Thanks to 'pkmnfrk' @ bukkit forums.
  70.  *
  71.  *  - Terminal mode now closes automatically when rcon socket is closed by server
  72.  *    or if packet size cannot be retrieved correctly.
  73.  *
  74.  *  - Client now tries to clean the incoming socket data if last package was out of spec.
  75.  *
  76.  *
  77.  * 0.0.4
  78.  *  - Reverted back to default getopts options error handler (opterr = 1).
  79.  *    Custom error handler requires rewriting.
  80.  *  - Some comestic fixes in program output strings.
  81.  *  - Program usage(); function now waits for enter before exiting on Windows.
  82.  *
  83.  *
  84.  * 0.0.3
  85.  *  - Colors are now supported on Windows too!
  86.  *  - Terminal mode is now triggered with "-t" flag. "-i" flag still works for
  87.  *    backwards compatibility.
  88.  *  - Bug fixes (Packet size check always evaluating false and color validity
  89.  *    check always evaluating true).
  90.  *
  91.  *
  92.  * 0.0.2
  93.  *  - License changed from 'ISC License' to 'zlib/libpng License'.
  94.  *  - Bug fixes & code cleanups
  95.  *  - Interactive mode (-i flag). Client acts as interactive terminal.
  96.  *  - Program return value is now the number of rcon commmands sent successfully.
  97.  *    If connecting or authentication fails, the return value is -1.
  98.  *  - Colors are now enabled by default. Now '-c' flag disables the color support.
  99.  *
  100.  *
  101.  * 0.0.1
  102.  *  - Added experimental support for bukkit colors.
  103.  *    Should work with any sh compatible shell.
  104.  *  - Packet string data limited to max 2048 (DATA_BUFFSIZE) bytes.
  105.  *    No idea how Minecraft handles multiple rcon packets.
  106.  *    If someone knows, please mail me so I can implement it.
  107.  *
  108.  *
  109.  * TODO:
  110.  *  - Make the receive buffer dynamic??
  111.  *  - Change some of the packet size issues to fatal errors.
  112.  *  - Code cleanups.
  113.  *  - Check global variables (remove if possible).
  114.  *  - Add some protocol checks (proper packet id check etc..).
  115.  *  - Preprocessor (#ifdef / #ifndef) cleanups.
  116.  *  - Follow valve rcon protocol standard strictly?
  117.  *  - Multiple packet support if minecraft supports it?!
  118.  *  - Investigate if player chat messages gets sent through rcon.
  119.  *    If they are, the messaging system requires rewriting.
  120.  *  - Name resolving should be integrated to connection creation function.
  121.  *  - Dont try to cleanup the socket if not authenticated
  122.  *  - Better sockets error reporting
  123.  *  - Better error function (VA_ARGS support)
  124.  *
  125.  *
  126.  * Bug reports and feature requests to tiiffi_at_gmail_dot_com.
  127.  *
  128.  */
  129.  
  130.  
  131. #include <stdio.h>
  132. #include <stdlib.h>
  133. #include <string.h>
  134. #include <unistd.h>
  135. #include <signal.h>
  136.  
  137. #ifdef _WIN32
  138.     /* for name resolving on windows */
  139.     #define _WIN32_WINNT 0x0501
  140.  
  141.     #include <ws2tcpip.h>
  142.     #include <winsock2.h>
  143.     #include <windows.h>
  144. #else
  145.     #include <sys/types.h>
  146.     #include <sys/socket.h>
  147.     #include <netinet/in.h>
  148.     #include <arpa/inet.h>
  149.     #include <netdb.h>
  150. #endif
  151.  
  152. /* absolute value macro
  153. #define absolute(x) (x < 0) ? (0 - x) : x
  154. */
  155.  
  156. #define RCON_EXEC_COMMAND       2
  157. #define RCON_AUTHENTICATE       3
  158. #define RCON_RESPONSEVALUE      0
  159. #define RCON_AUTH_RESPONSE      2
  160. #define RCON_PID                42
  161.  
  162. /* Safe value I think. This should me made dynamic for more stable performance! */
  163. #define DATA_BUFFSIZE 10240
  164.  
  165. #define VERSION "0.0.5"
  166. #define IN_NAME "mcrcon"
  167. #define VER_STR IN_NAME" "VERSION
  168.  
  169. /* rcon packet structure */
  170. typedef struct _rc_packet {
  171.     int size;
  172.     int id;
  173.     int cmd;
  174.     char data[DATA_BUFFSIZE];
  175.     /* ignoring string2 atm.. */
  176. } rc_packet;
  177.  
  178. /* functions */
  179. void            usage(void);
  180. void            error(char *errstring);
  181. #ifndef _WIN32
  182. void            print_color(int color);
  183. #endif
  184.  
  185. struct addrinfo *net_resolve(char *host, char *port);
  186. void            net_close_socket(int sd);
  187. int             net_open_socket(char *host, char *port);
  188. int             net_send_packet(int sd, rc_packet *packet);
  189. rc_packet*      net_recv_packet(int sd);
  190. #ifdef _WIN32
  191. void            net_init_WSA(void);
  192. #endif
  193. int             net_clean_incoming(int sd, int size);
  194.  
  195. rc_packet*      packet_build(int id, int cmd, char *s1);
  196. void            packet_print(rc_packet *packet);
  197.  
  198. int             rcon_auth(int rsock, char *passwd);
  199. int             rcon_command(int rsock, char *command);
  200.  
  201. int             get_line(char *buffer, int len);
  202. int             run_terminal_mode(int rsock);
  203. int             run_commands(int argc, char *argv[]);
  204.  
  205.  
  206. /* some globals */
  207. int silent_mode = 0;
  208. int print_colors = 1;
  209. int connection_alive = 1;
  210. int rsock; /* rcon socket */
  211.  
  212. #ifdef _WIN32
  213.   /* console coloring on windows */
  214.   HANDLE console_handle;
  215. #endif
  216.  
  217. /* safety stuff (windows is still misbehaving) */
  218. void exit_proc(void) {
  219.     if(rsock != -1) net_close_socket(rsock);
  220. }
  221.  
  222. /* Check windows & linux behaviour !!! */
  223. void sighandler(/*int sig*/) {
  224.     connection_alive = 0;
  225.     #ifndef _WIN32
  226.       exit(-1);
  227.     #endif
  228. }
  229.  
  230. int main(int argc, char *argv[])
  231. {
  232.     int opt, ret = 0;
  233.     int terminal_mode = 0;
  234.  
  235.     char *host = NULL;
  236.     char *pass = "";
  237.     char *port = "25575";
  238.  
  239.     if(argc < 2) usage();
  240.  
  241.     opterr = 1; /* default error handler enabled */
  242.     while((opt = getopt(argc, argv, "tcshH:p:P:i")) != -1)
  243.     {
  244.         switch(opt)
  245.         {
  246.             case 'H': host = optarg;        break;
  247.             case 'P': port = optarg;        break;
  248.             case 'p': pass = optarg;        break;
  249.             case 'C':
  250.             case 'c': print_colors = 0;     break;
  251.             case 'S':
  252.             case 's': silent_mode = 1;      break;
  253.             case 'T':
  254.             case 't':
  255.             case 'I':
  256.             case 'i': terminal_mode = 1;    break;
  257.             case 'h':
  258.             case '?':
  259.                 /*
  260.                 if(optopt == 'P' || optopt == 'H' || optopt == 'p')
  261.                     fprintf (stderr, "Option -%c requires an argument.\n\n", optopt);
  262.                 */
  263.  
  264.                 /* else fprintf (stderr, "Unknown option -%c\n\n", optopt); */
  265.  
  266.                 usage();
  267.             break;
  268.  
  269.             default: abort();
  270.         }
  271.     }
  272.  
  273.     if(host == NULL) {
  274.         fputs("Host not defined. Check -H flag.\n\n", stdout);
  275.         usage();
  276.     }
  277.  
  278.     if(optind == argc && terminal_mode == 0) {
  279.         fputs("No commands specified.\n\n", stdout);
  280.         usage();
  281.     }
  282.  
  283.     /* safety features to prevent "IO: Connection reset" bug on the server side */
  284.     atexit(&exit_proc);
  285.     signal(SIGABRT, &sighandler);
  286.     signal(SIGTERM, &sighandler);
  287.     signal(SIGINT, &sighandler);
  288.  
  289.     #ifdef _WIN32
  290.       net_init_WSA();
  291.       console_handle = GetStdHandle(STD_OUTPUT_HANDLE);
  292.       if(console_handle == INVALID_HANDLE_VALUE) console_handle = NULL;
  293.     #endif
  294.  
  295.     /* open socket */
  296.     rsock = net_open_socket(host, port);
  297.  
  298.     /* auth & commands */
  299.     if(rcon_auth(rsock, pass))
  300.     {
  301.         if(terminal_mode)
  302.             ret = run_terminal_mode(rsock);
  303.         else
  304.             ret = run_commands(argc, argv);
  305.     }
  306.     else /* auth failed */
  307.     {
  308.         ret = -1;
  309.         fprintf(stdout, "Authentication failed!\n");
  310.     }
  311.  
  312.     /* cleanup */
  313.     net_close_socket(rsock);
  314.     rsock = -1;
  315.  
  316.     return ret;
  317. }
  318.  
  319. void usage(void)
  320. {
  321.     fputs(
  322.         "Usage: "IN_NAME" [OPTIONS]... [COMMANDS]...\n"
  323.         "Sends rcon commands to minecraft server.\n\n"
  324.         "Option:\n"
  325.         "  -h\t\tPrints usage.\n"
  326.         "  -s\t\tSilent mode. Do not print data received from rcon.\n"
  327.         "  -t\t\tTerminal mode. Acts as interactive terminal.\n"
  328.         "  -p\t\tRcon password. Default: \"\".\n"
  329.         "  -H\t\tHost address or ip.\n"
  330.         "  -P\t\tPort. Default: 25575.\n"
  331.         "  -c\t\tDo not print colors. Disables bukkit color printing.\n"
  332.     ,stdout);
  333.  
  334.     puts("\nInvidual commands must be separated with spaces.\n");
  335.     puts("Example:\n  "IN_NAME" -c -H 192.168.1.42 -P 9999 -p password cmd1 \"cmd2 with spaces\"\n");
  336.     puts("minecraft rcon ("IN_NAME") "VERSION".\nReport bugs to tiiffi_at_gmail_dot_com.\n");
  337.  
  338.     #ifdef _WIN32
  339.       puts("Press enter to exit.");
  340.       getchar();
  341.     #endif
  342.     exit(0);
  343. }
  344.  
  345. void error(char *errstring)
  346. {
  347.     fputs(errstring, stderr);
  348.     exit(-1);
  349. }
  350.  
  351. #ifdef _WIN32
  352. void net_init_WSA(void)
  353. {
  354.     WSADATA wsadata;
  355.     int err;
  356.  
  357.     err = WSAStartup(MAKEWORD(1, 1), &wsadata);
  358.     if(err != 0)
  359.     {
  360.         fprintf(stderr, "WSAStartup failed. Errno: %d.\n", err);
  361.         exit(-1);
  362.     }
  363. }
  364. #endif
  365.  
  366. struct addrinfo *net_resolve(char *host, char *port)
  367. {
  368.     /* !!! This function should be integrated to open_socket function for cleaner code !!! */
  369.  
  370.     //struct in_addr6 serveraddr;
  371.     struct addrinfo hints, *result;
  372.     int ret;
  373.  
  374.     memset(&hints, 0, sizeof(hints));
  375.     hints.ai_family   = AF_UNSPEC;
  376.     hints.ai_socktype = SOCK_STREAM;
  377.     hints.ai_protocol = IPPROTO_TCP;
  378.     /* hints.ai_flags    = AI_NUMERICSERV; // Windows retardism */
  379.  
  380.     ret = getaddrinfo(host, port, &hints, &result);
  381.     if(ret != 0)
  382.     {
  383.         if(ret == EAI_SERVICE) fprintf(stderr, "Invalid port (%s).\n", port);
  384.         fprintf(stderr, "Error: Unable to resolve hostname (%s).\n", host);
  385.         exit(-1);
  386.     }
  387.  
  388.     return result;
  389. }
  390.  
  391. /* socket close and cleanup */
  392. void net_close_socket(int sd)
  393. {
  394.     #ifdef _WIN32
  395.         closesocket(sd);
  396.         WSACleanup();
  397.     #else
  398.         close(sd);
  399.     #endif
  400. }
  401.  
  402. /* Opens and connects socket */
  403. int net_open_socket(char *host, char *port)
  404. {
  405.     int sd;
  406.  
  407.     struct addrinfo *serverinfo;
  408.  
  409.     serverinfo = net_resolve(host, port);
  410.  
  411.     sd = socket(serverinfo->ai_family, serverinfo->ai_socktype, serverinfo->ai_protocol);
  412.     if(sd < 0)
  413.     {
  414.         #ifdef _WIN32
  415.             WSACleanup();
  416.         #endif
  417.         freeaddrinfo(serverinfo);
  418.         error("Error: cannot create socket.\n");
  419.     }
  420.  
  421.     if(connect(sd, serverinfo->ai_addr, serverinfo->ai_addrlen) != 0)
  422.     {
  423.         net_close_socket(sd);
  424.         fprintf(stderr, "Error: connection failed (%s).\n", host);
  425.         freeaddrinfo(serverinfo);
  426.         exit(-1);
  427.     }
  428.  
  429.     freeaddrinfo(serverinfo);
  430.     return sd;
  431. }
  432.  
  433. int net_send_packet(int sd, rc_packet *packet)
  434. {
  435.     int len;
  436.     int total = 0;        /* how many bytes we've sent */
  437.     int bytesleft;        /* how many we have left to send */
  438.     int ret = -1;
  439.  
  440.     bytesleft = len = packet->size + sizeof(int);
  441.  
  442.     while(total < len)
  443.     {
  444.         ret = send(sd, (char *) packet + total, bytesleft, 0);
  445.         if(ret == -1) { break; }
  446.         total += ret;
  447.         bytesleft -= ret;
  448.     }
  449.  
  450.     /* return -1 on failure, 0 on success */
  451.     return ret == -1 ? -1 : 1;
  452. }
  453.  
  454. rc_packet *net_recv_packet(int sd)
  455. {
  456.     int psize;
  457.     static rc_packet packet = {0, 0, 0, { 0x00 }};
  458.  
  459.     /* packet.size = packet.id = packet.cmd = 0; */
  460.  
  461.     int ret = recv(sd, (char *) &psize, sizeof(int), 0);
  462.  
  463.     if(ret == 0) {
  464.         fprintf(stderr, "Connection lost.\n");
  465.         connection_alive = 0;
  466.         return NULL;
  467.     }
  468.  
  469.     if(ret != sizeof(int)) {
  470.         fprintf(stderr, "Error: recv() failed. Invalid packet size (%d).\n", ret);
  471.         connection_alive = 0;
  472.         return NULL;
  473.     }
  474.  
  475.     if(psize < 10 || psize > DATA_BUFFSIZE) {
  476.         fprintf(stderr, "Warning: invalid packet size (%d). Must over 10 and less than %d.\n", psize, DATA_BUFFSIZE);
  477.         if(psize > DATA_BUFFSIZE  || psize < 0) psize = DATA_BUFFSIZE;
  478.         net_clean_incoming(sd, psize);
  479.         return NULL;
  480.     }
  481.  
  482.     packet.size = psize;
  483.  
  484.     ret = recv(sd, (char *) &packet + sizeof(int), psize, 0);
  485.     if(ret == 0) {
  486.         fprintf(stderr, "Connection lost.\n");
  487.         connection_alive = 0;
  488.         return NULL;
  489.     }
  490.     if(ret != psize) {
  491.         fprintf(stderr, "Warning: recv() return value (%d) does not match expected packet size (%d).\n", ret, psize);
  492.         net_clean_incoming(sd, DATA_BUFFSIZE); /* Should be enough. Needs some checking */
  493.         return NULL;
  494.     }
  495.  
  496.     return &packet;
  497. }
  498.  
  499. int net_clean_incoming(int sd, int size)
  500. {
  501.     char tmp[size];
  502.  
  503.     int ret = recv(sd, tmp, size, 0);
  504.  
  505.     if(ret == 0) {
  506.         fprintf(stderr, "Connection lost.\n");
  507.         connection_alive = 0;
  508.     }
  509.  
  510.     return ret;
  511. }
  512.  
  513. void print_color(int color)
  514. {
  515.     /* sh compatible color array */
  516.     #ifndef _WIN32
  517.     char *colors[] = {
  518.         "\033[0;30m", /* 00 BLACK    0x30 */
  519.         "\033[0;34m", /* 01 BLUE     0x31 */
  520.         "\033[0;32m", /* 02 GREEN    0x32 */
  521.         "\033[0;36m", /* 03 CYAN     0x33 */
  522.         "\033[0;31m", /* 04 RED      0x34 */
  523.         "\033[0;35m", /* 05 PURPLE   0x35 */
  524.         "\033[0;33m", /* 06 GOLD     0x36 */
  525.         "\033[0;37m", /* 07 GREY     0x37 */
  526.         "\033[1;30m", /* 08 DGREY    0x38 */
  527.         "\033[1;34m", /* 09 LBLUE    0x39 */
  528.         "\033[1;32m", /* 10 LGREEN   0x61 */
  529.         "\033[1;36m", /* 11 LCYAN    0x62 */
  530.         "\033[1;31m", /* 12 LRED     0x63 */
  531.         "\033[1;35m", /* 13 LPURPLE  0x64 */
  532.         "\033[1;33m", /* 14 YELLOW   0x65 */
  533.         "\033[1;37m", /* 15 WHITE    0x66 */
  534.     };
  535.  
  536.     if(color == 0) {
  537.         fputs("\033[0m", stdout); /* CANCEL COLOR */
  538.     }
  539.     else
  540.     #endif
  541.     {
  542.         if(color >= 0x61 && color <= 0x66) color -= 0x57;
  543.         else if(color >= 0x30 && color <= 0x39) color -= 0x30;
  544.         else return;
  545.  
  546.         #ifndef _WIN32
  547.           fputs(colors[color], stdout);
  548.         #else
  549.           SetConsoleTextAttribute(console_handle, color);
  550.         #endif
  551.     }
  552. }
  553.  
  554. /* this hacky mess might use some optmizing */
  555. void packet_print(rc_packet *packet)
  556. {
  557.     int i;
  558.     int def_color = 0;
  559.  
  560.     #ifdef _WIN32
  561.       CONSOLE_SCREEN_BUFFER_INFO console_info;
  562.       if(GetConsoleScreenBufferInfo(console_handle, &console_info) != 0)
  563.           def_color = console_info.wAttributes + 0x30;
  564.       else def_color = 0x37;
  565.     #endif
  566.  
  567.     /* colors enabled so try to handle the bukkit colors for terminal */
  568.     if(print_colors == 1) {
  569.  
  570.         for(i = 0; (unsigned char) packet->data[i] != 0; ++i) {
  571.             if((unsigned char) packet->data[i] == 0xa7) {
  572.                 ++i;
  573.                 print_color(packet->data[i]);
  574.                 continue;
  575.             }
  576.             if(packet->data[i] == 0x0A) print_color(def_color);
  577.  
  578.             putchar(packet->data[i]);
  579.         }
  580.         print_color(def_color); /* cancel coloring */
  581.  
  582.     }
  583.     /* strip colors */
  584.     else
  585.     {
  586.         for(i = 0; (unsigned char) packet->data[i] != 0; ++i) {
  587.             if((unsigned char) packet->data[i] == 0xa7) {
  588.                 ++i;
  589.                 continue;
  590.             }
  591.             putchar(packet->data[i]);
  592.         }
  593.     }
  594.  
  595.     /* print newline if string has no newline */
  596.     if(packet->data[i-1] != 10 && packet->data[i-1] != 13)
  597.         putchar('\n');
  598. }
  599.  
  600. rc_packet *packet_build(int id, int cmd, char *s1)
  601. {   /* hacky function */
  602.     static rc_packet packet = {0, 0, 0, { 0x00 }};
  603.  
  604.     /* size + id + cmd + s1 + s2 NULL terminator */
  605.     int s1_len = strlen(s1);
  606.     if(s1_len > DATA_BUFFSIZE) {
  607.         fprintf(stderr, "Warning: Command string too long (%d). Maximum allowed: %d.\n", s1_len, DATA_BUFFSIZE);
  608.         return NULL;
  609.     }
  610.  
  611.     packet.size = sizeof(int) * 2 + s1_len + 2;
  612.     packet.id = id;
  613.     packet.cmd = cmd;
  614.     strncpy(packet.data, s1, DATA_BUFFSIZE);
  615.  
  616.     return &packet;
  617. }
  618.  
  619. int rcon_auth(int rsock, char *passwd)
  620. {
  621.     int ret;
  622.  
  623.     rc_packet *packet = packet_build(RCON_PID, RCON_AUTHENTICATE, passwd);
  624.     if(packet == NULL) return 0;
  625.  
  626.     ret = net_send_packet(rsock, packet);
  627.     if(!ret) return 0; /* send failed */
  628.  
  629.     packet = net_recv_packet(rsock);
  630.     if(packet == NULL) return 0;
  631.  
  632.     /* return 1 if authentication OK */
  633.     return packet->id == -1 ? 0 : 1;
  634. }
  635.  
  636. int rcon_command(int rsock, char *command)
  637. {
  638.     int ret;
  639.  
  640.     rc_packet *packet = packet_build(RCON_PID, RCON_EXEC_COMMAND, command);
  641.     if(packet == NULL) {
  642.         connection_alive = 0;
  643.         return 0;
  644.     }
  645.  
  646.     ret = net_send_packet(rsock, packet);
  647.     if(!ret) return 0; /* send failed */
  648.  
  649.     packet = net_recv_packet(rsock);
  650.     if(packet == NULL) return 0;
  651.  
  652.     if(packet->id != RCON_PID) return 0; /* wrong packet id */
  653.  
  654.     if(!silent_mode) {
  655.         /*
  656.         if(packet->size == 10) {
  657.             printf("Unknown command \"%s\". Type \"help\" or \"?\" for help.\n", command);
  658.         }
  659.         else
  660.         */
  661.         if(packet->size > 10)
  662.             packet_print(packet);
  663.     }
  664.  
  665.     /* return 1 if world was saved */
  666.     return 1;
  667. }
  668.  
  669. int run_commands(int argc, char *argv[])
  670. {
  671.     int i, ok = 1, ret = 0;
  672.  
  673.     for(i = optind; i < argc && ok; i++) {
  674.         ok = rcon_command(rsock, argv[i]);
  675.         ret += ok;
  676.     }
  677.  
  678.     return ret;
  679. }
  680.  
  681. /* interactive terminal mode */
  682. int run_terminal_mode(int rsock)
  683. {
  684.     int ret = 0;
  685.     char command[DATA_BUFFSIZE] = {0x00};
  686.  
  687.     puts("Logged in. Type \"Q\" to quit!");
  688.  
  689.     while(connection_alive) {
  690.  
  691.         int len = get_line(command, DATA_BUFFSIZE);
  692.         if(command[0] == 'Q' && command[1] == 0) break;
  693.  
  694.         if(len > 0 && connection_alive) ret += rcon_command(rsock, command);
  695.  
  696.         command[0] = len = 0;
  697.     }
  698.  
  699.     return ret;
  700. }
  701.  
  702. /* gets line from stdin and deals with rubbish left in input buffer */
  703. int get_line(char *buffer, int bsize)
  704. {
  705.     int ch, len;
  706.  
  707.     fputs("> ", stdout);
  708.     fgets(buffer, bsize, stdin);
  709.  
  710.     if(buffer[0] == 0) connection_alive = 0;
  711.  
  712.     /* remove unwanted characters from the buffer */
  713.     buffer[strcspn(buffer, "\r\n")] = '\0';
  714.  
  715.     len = strlen(buffer);
  716.  
  717.     /* clean input buffer if needed */
  718.     if(len == bsize - 1)
  719.         while ((ch = getchar()) != '\n' && ch != EOF);
  720.  
  721.     return len;
  722. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement