URI:
       tadmin.c - vaccinewars - be a doctor and try to vaccinate the world
  HTML git clone git://src.adamsgaard.dk/vaccinewars
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       tadmin.c (4138B)
       ---
            1 /************************************************************************
            2  * admin.c        dopewars server administration                        *
            3  * Copyright (C)  1998-2021  Ben Webb                                   *
            4  *                Email: benwebb@users.sf.net                           *
            5  *                WWW: https://dopewars.sourceforge.io/                 *
            6  *                                                                      *
            7  * This program is free software; you can redistribute it and/or        *
            8  * modify it under the terms of the GNU General Public License          *
            9  * as published by the Free Software Foundation; either version 2       *
           10  * of the License, or (at your option) any later version.               *
           11  *                                                                      *
           12  * This program is distributed in the hope that it will be useful,      *
           13  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
           14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
           15  * GNU General Public License for more details.                         *
           16  *                                                                      *
           17  * You should have received a copy of the GNU General Public License    *
           18  * along with this program; if not, write to the Free Software          *
           19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,               *
           20  *                   MA  02111-1307, USA.                               *
           21  ************************************************************************/
           22 
           23 #ifdef HAVE_CONFIG_H
           24 #include <config.h>
           25 #endif
           26 
           27 #if !defined(CYGWIN) && defined(NETWORKING)
           28 #include <sys/types.h>
           29 #include <sys/socket.h>
           30 #include <sys/un.h>
           31 #ifdef HAVE_STDLIB_H
           32 #include <stdlib.h>
           33 #endif
           34 #include <string.h>
           35 #include <errno.h>
           36 #include <stdio.h>
           37 #include <glib.h>
           38 
           39 #include "dopewars.h"
           40 #include "network.h"
           41 #include "nls.h"
           42 #include "serverside.h"
           43 
           44 static int OpenSocket(void)
           45 {
           46   struct sockaddr_un addr;
           47   int sock;
           48   gchar *sockname;
           49 
           50   sockname = GetLocalSocket();
           51 
           52   g_print(_("Attempting to connect to local dopewars server via "
           53             "Unix domain\n socket %s...\n"), sockname);
           54   sock = socket(PF_UNIX, SOCK_STREAM, 0);
           55   if (sock == -1) {
           56     perror("socket");
           57     exit(EXIT_FAILURE);
           58   }
           59 
           60   addr.sun_family = AF_UNIX;
           61   strncpy(addr.sun_path, sockname, sizeof(addr.sun_path));
           62   addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
           63 
           64   if (connect(sock, (struct sockaddr *)&addr,
           65               sizeof(struct sockaddr_un)) == -1) {
           66     perror("connect");
           67     exit(EXIT_FAILURE);
           68   }
           69 
           70   g_print(_("Connection established; use Ctrl-D to "
           71             "close your session.\n\n"));
           72   g_free(sockname);
           73 
           74   return sock;
           75 }
           76 
           77 void AdminServer(struct CMDLINE *cmdline)
           78 {
           79   int sock, topsock;
           80   NetworkBuffer *netbuf;
           81   fd_set readfds, writefds, errorfds;
           82   gchar *msg, inbuf[200];
           83   gboolean doneOK;
           84 
           85   InitConfiguration(cmdline);
           86 
           87   sock = OpenSocket();
           88   netbuf = g_new(NetworkBuffer, 1);
           89 
           90   InitNetworkBuffer(netbuf, '\n', '\r', NULL);
           91   BindNetworkBufferToSocket(netbuf, sock);
           92 
           93   while (1) {
           94     FD_ZERO(&readfds);
           95     FD_ZERO(&writefds);
           96     FD_ZERO(&errorfds);
           97 
           98     FD_SET(0, &readfds);
           99     topsock = 1;
          100     SetSelectForNetworkBuffer(netbuf, &readfds, &writefds, &errorfds,
          101                               &topsock);
          102 
          103     if (select(topsock, &readfds, &writefds, &errorfds, NULL) == -1) {
          104       if (errno == EINTR)
          105         continue;
          106       else
          107         perror("select");
          108       break;
          109     }
          110 
          111     if (FD_ISSET(0, &readfds)) {
          112       if (fgets(inbuf, sizeof(inbuf), stdin)) {
          113         inbuf[sizeof(inbuf) - 1] = '\0';
          114         if (strlen(inbuf) > 0) {
          115           if (inbuf[strlen(inbuf) - 1] == '\n')
          116             inbuf[strlen(inbuf) - 1] = '\0';
          117           QueueMessageForSend(netbuf, inbuf);
          118         }
          119       } else
          120         break;
          121     }
          122 
          123     if (RespondToSelect(netbuf, &readfds, &writefds, &errorfds, &doneOK)) {
          124       while ((msg = GetWaitingMessage(netbuf)) != NULL) {
          125         g_print("%s\n", msg);
          126         g_free(msg);
          127       }
          128     }
          129     if (!doneOK)
          130       break;
          131   }
          132   ShutdownNetworkBuffer(netbuf);
          133   g_free(netbuf);
          134   g_print("Connection closed\n");
          135 }
          136 #endif