Main Page | Data Structures | Directories | File List | Data Fields

wifidog-1.1.3_beta2/src/wdctl.c

00001 /********************************************************************\
00002  * This program is free software; you can redistribute it and/or    *
00003  * modify it under the terms of the GNU General Public License as   *
00004  * published by the Free Software Foundation; either version 2 of   *
00005  * the License, or (at your option) any later version.              *
00006  *                                                                  *
00007  * This program is distributed in the hope that it will be useful,  *
00008  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00009  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00010  * GNU General Public License for more details.                     *
00011  *                                                                  *
00012  * You should have received a copy of the GNU General Public License*
00013  * along with this program; if not, contact:                        *
00014  *                                                                  *
00015  * Free Software Foundation           Voice:  +1-617-542-5942       *
00016  * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652       *
00017  * Boston, MA  02111-1307,  USA       gnu@gnu.org                   *
00018  *                                                                  *
00019 \********************************************************************/
00020 
00021 /* $Id: wdctl.c 901 2006-01-17 18:58:13Z mina $ */
00027 #define _GNU_SOURCE
00028 
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <pthread.h>
00032 #include <string.h>
00033 #include <stdarg.h>
00034 #include <sys/types.h>
00035 #include <sys/socket.h>
00036 #include <sys/un.h>
00037 #include <unistd.h>
00038 #include <syslog.h>
00039 #include <errno.h>
00040 
00041 #include "wdctl.h"
00042 
00043 s_config config;
00044 
00045 static void usage(void);
00046 static void init_config(void);
00047 static void parse_commandline(int, char **);
00048 static int connect_to_server(char *);
00049 static int send_request(int, char *);
00050 static void wdctl_status(void);
00051 static void wdctl_stop(void);
00052 static void wdctl_reset(void);
00053 static void wdctl_restart(void);
00054 
00060 static void
00061 usage(void)
00062 {
00063     printf("Usage: wdctl [options] command [arguments]\n");
00064     printf("\n");
00065     printf("options:\n");
00066     printf("  -s <path>         Path to the socket\n");
00067     printf("  -h                Print usage\n");
00068     printf("\n");
00069     printf("commands:\n");
00070     printf("  reset [mac|ip]    Reset the specified mac or ip connection\n");
00071     printf("  status            Obtain the status of wifidog\n");
00072     printf("  stop              Stop the running wifidog\n");
00073     printf("  restart           Re-start the running wifidog (without disconnecting active users!)\n");
00074     printf("\n");
00075 }
00076 
00081 static void
00082 init_config(void)
00083 {
00084 
00085         config.socket = strdup(DEFAULT_SOCK);
00086         config.command = WDCTL_UNDEF;
00087 }
00088 
00093 void
00094 parse_commandline(int argc, char **argv)
00095 {
00096     extern int optind;
00097     int c;
00098 
00099     while (-1 != (c = getopt(argc, argv, "s:h"))) {
00100         switch(c) {
00101             case 'h':
00102                 usage();
00103                 exit(1);
00104                 break;
00105 
00106             case 's':
00107                 if (optarg) {
00108                     free(config.socket);
00109                     config.socket = strdup(optarg);
00110                 }
00111                 break;
00112 
00113             default:
00114                 usage();
00115                 exit(1);
00116                 break;
00117         }
00118     }
00119 
00120     if ((argc - optind) <= 0) {
00121             usage();
00122             exit(1);
00123     }
00124 
00125     if (strcmp(*(argv + optind), "status") == 0) {
00126             config.command = WDCTL_STATUS;
00127     } else if (strcmp(*(argv + optind), "stop") == 0) {
00128             config.command = WDCTL_STOP;
00129     } else if (strcmp(*(argv + optind), "reset") == 0) {
00130             config.command = WDCTL_KILL;
00131             if ((argc - (optind + 1)) <= 0) {
00132                     fprintf(stderr, "wdctl: Error: You must specify an IP "
00133                                     "or a Mac address to reset\n");
00134                     usage();
00135                     exit(1);
00136             }
00137             config.param = strdup(*(argv + optind + 1));
00138     } else if (strcmp(*(argv + optind), "restart") == 0) {
00139             config.command = WDCTL_RESTART;
00140     }
00141          else {
00142             fprintf(stderr, "wdctl: Error: Invalid command \"%s\"\n", *(argv + optind));
00143             usage();
00144             exit(1);
00145     }
00146 }
00147 
00148 static int
00149 connect_to_server(char *sock_name)
00150 {
00151         int sock;
00152         struct sockaddr_un      sa_un;
00153         
00154         /* Connect to socket */
00155         sock = socket(AF_UNIX, SOCK_STREAM, 0);
00156         memset(&sa_un, 0, sizeof(sa_un));
00157         sa_un.sun_family = AF_UNIX;
00158         strncpy(sa_un.sun_path, sock_name, (sizeof(sa_un.sun_path) - 1));
00159 
00160         if (connect(sock, (struct sockaddr *)&sa_un, 
00161                         strlen(sa_un.sun_path) + sizeof(sa_un.sun_family))) {
00162                 fprintf(stderr, "wdctl: wifidog probably not started (Error: %s)\n", strerror(errno));
00163                 exit(1);
00164         }
00165 
00166         return sock;
00167 }
00168 
00169 static int
00170 send_request(int sock, char *request)
00171 {
00172         ssize_t len,
00173                 written;
00174                 
00175         len = 0;
00176         while (len != strlen(request)) {
00177                 written = write(sock, (request + len), strlen(request) - len);
00178                 if (written == -1) {
00179                         fprintf(stderr, "Write to wifidog failed: %s\n",
00180                                         strerror(errno));
00181                         exit(1);
00182                 }
00183                 len += written;
00184         }
00185 
00186         return((int)len);
00187 }
00188 
00189 static void
00190 wdctl_status(void)
00191 {
00192         int     sock;
00193         char    buffer[4096];
00194         char    request[16];
00195         int     len;
00196 
00197         sock = connect_to_server(config.socket);
00198                 
00199         strncpy(request, "status\r\n\r\n", 15);
00200 
00201         len = send_request(sock, request);
00202         
00203         while ((len = read(sock, buffer, sizeof(buffer))) > 0) {
00204                 buffer[len] = '\0';
00205                 printf("%s", buffer);
00206         }
00207 
00208         shutdown(sock, 2);
00209         close(sock);
00210 }
00211 
00212 static void
00213 wdctl_stop(void)
00214 {
00215         int     sock;
00216         char    buffer[4096];
00217         char    request[16];
00218         int     len;
00219 
00220         sock = connect_to_server(config.socket);
00221                 
00222         strncpy(request, "stop\r\n\r\n", 15);
00223 
00224         len = send_request(sock, request);
00225         
00226         while ((len = read(sock, buffer, sizeof(buffer))) > 0) {
00227                 buffer[len] = '\0';
00228                 printf("%s", buffer);
00229         }
00230 
00231         shutdown(sock, 2);
00232         close(sock);
00233 }
00234 
00235 void
00236 wdctl_reset(void)
00237 {
00238         int     sock;
00239         char    buffer[4096];
00240         char    request[64];
00241         int     len,
00242                 rlen;
00243 
00244         sock = connect_to_server(config.socket);
00245                 
00246         strncpy(request, "reset ", 64);
00247         strncat(request, config.param, (64 - strlen(request)));
00248         strncat(request, "\r\n\r\n", (64 - strlen(request)));
00249 
00250         len = send_request(sock, request);
00251         
00252         len = 0;
00253         memset(buffer, 0, sizeof(buffer));
00254         while ((len < sizeof(buffer)) && ((rlen = read(sock, (buffer + len),
00255                                 (sizeof(buffer) - len))) > 0)){
00256                 len += rlen;
00257         }
00258 
00259         if (strcmp(buffer, "Yes") == 0) {
00260                 printf("Connection %s successfully reset.\n", config.param);
00261         } else if (strcmp(buffer, "No") == 0) {
00262                 printf("Connection %s was not active.\n", config.param);
00263         } else {
00264                 fprintf(stderr, "wdctl: Error: WiFiDog sent an abnormal "
00265                                 "reply.\n");
00266         }
00267 
00268         shutdown(sock, 2);
00269         close(sock);
00270 }
00271 
00272 static void
00273 wdctl_restart(void)
00274 {
00275         int     sock;
00276         char    buffer[4096];
00277         char    request[16];
00278         int     len;
00279 
00280         sock = connect_to_server(config.socket);
00281                 
00282         strncpy(request, "restart\r\n\r\n", 15);
00283 
00284         len = send_request(sock, request);
00285         
00286         while ((len = read(sock, buffer, sizeof(buffer))) > 0) {
00287                 buffer[len] = '\0';
00288                 printf("%s", buffer);
00289         }
00290 
00291         shutdown(sock, 2);
00292         close(sock);
00293 }
00294 
00295 int
00296 main(int argc, char **argv)
00297 {
00298 
00299         /* Init configuration */
00300         init_config();
00301         parse_commandline(argc, argv);
00302 
00303         switch(config.command) {
00304         case WDCTL_STATUS:
00305                 wdctl_status();
00306                 break;
00307         
00308         case WDCTL_STOP:
00309                 wdctl_stop();
00310                 break;
00311 
00312         case WDCTL_KILL:
00313                 wdctl_reset();
00314                 break;
00315                 
00316         case WDCTL_RESTART:
00317                 wdctl_restart();
00318                 break;
00319 
00320         default:
00321                 /* XXX NEVER REACHED */
00322                 fprintf(stderr, "Oops\n");
00323                 exit(1);
00324                 break;
00325         }
00326         exit(0);
00327 }

Generated on Tue Jan 31 23:13:16 2006 for WifiDog by  doxygen 1.4.4