00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00059 static void
00060 usage(void)
00061 {
00062 printf("Usage: wdctl [options] command [arguments]\n");
00063 printf("\n");
00064 printf("options:\n");
00065 printf(" -s <path> Path to the socket\n");
00066 printf(" -h Print usage\n");
00067 printf("\n");
00068 printf("commands:\n");
00069 printf(" reset [mac|ip] Reset the specified mac or ip connection\n");
00070 printf(" status Obtain the status of wifidog\n");
00071 printf(" stop Stop the running wifidog\n");
00072 printf("\n");
00073 }
00074
00079 static void
00080 init_config(void)
00081 {
00082
00083 config.socket = strdup(DEFAULT_SOCK);
00084 config.command = WDCTL_UNDEF;
00085 }
00086
00091 void
00092 parse_commandline(int argc, char **argv)
00093 {
00094 extern int optind;
00095 int c;
00096
00097 while (-1 != (c = getopt(argc, argv, "s:h"))) {
00098 switch(c) {
00099 case 'h':
00100 usage();
00101 exit(1);
00102 break;
00103
00104 case 's':
00105 if (optarg) {
00106 free(config.socket);
00107 config.socket = strdup(optarg);
00108 }
00109 break;
00110
00111 default:
00112 usage();
00113 exit(1);
00114 break;
00115 }
00116 }
00117
00118 if ((argc - optind) <= 0) {
00119 usage();
00120 exit(1);
00121 }
00122
00123 if (strcmp(*(argv + optind), "status") == 0) {
00124 config.command = WDCTL_STATUS;
00125 } else if (strcmp(*(argv + optind), "stop") == 0) {
00126 config.command = WDCTL_STOP;
00127 } else if (strcmp(*(argv + optind), "reset") == 0) {
00128 config.command = WDCTL_KILL;
00129 if ((argc - (optind + 1)) <= 0) {
00130 fprintf(stderr, "wdctl: Error: You must specify an IP "
00131 "or a Mac address to reset\n");
00132 usage();
00133 exit(1);
00134 }
00135 config.param = strdup(*(argv + optind + 1));
00136 }
00137 else {
00138 fprintf(stderr, "wdctl: Error: Invalid command \"%s\"\n", *(argv + optind));
00139 usage();
00140 exit(1);
00141 }
00142 }
00143
00144 static int
00145 connect_to_server(char *sock_name)
00146 {
00147 int sock;
00148 struct sockaddr_un sa_un;
00149
00150
00151 sock = socket(AF_UNIX, SOCK_STREAM, 0);
00152 memset(&sa_un, 0, sizeof(sa_un));
00153 sa_un.sun_family = AF_UNIX;
00154 strncpy(sa_un.sun_path, sock_name, (sizeof(sa_un.sun_path) - 1));
00155
00156 if (connect(sock, (struct sockaddr *)&sa_un,
00157 strlen(sa_un.sun_path) + sizeof(sa_un.sun_family))) {
00158 fprintf(stderr, "wdctl: wifidog probably not started (Error: %s)\n", strerror(errno));
00159 exit(1);
00160 }
00161
00162 return sock;
00163 }
00164
00165 static int
00166 send_request(int sock, char *request)
00167 {
00168 ssize_t len,
00169 written;
00170
00171 len = 0;
00172 while (len != strlen(request)) {
00173 written = write(sock, (request + len), strlen(request) - len);
00174 if (written == -1) {
00175 fprintf(stderr, "Write to wifidog failed: %s\n",
00176 strerror(errno));
00177 exit(1);
00178 }
00179 len += written;
00180 }
00181
00182 return((int)len);
00183 }
00184
00185 static void
00186 wdctl_status(void)
00187 {
00188 int sock;
00189 char buffer[4096];
00190 char request[16];
00191 int len;
00192
00193 sock = connect_to_server(config.socket);
00194
00195 strncpy(request, "status\r\n\r\n", 15);
00196
00197 len = send_request(sock, request);
00198
00199 while ((len = read(sock, buffer, sizeof(buffer))) > 0) {
00200 buffer[len] = '\0';
00201 printf("%s", buffer);
00202 }
00203
00204 shutdown(sock, 2);
00205 close(sock);
00206 }
00207
00208 static void
00209 wdctl_stop(void)
00210 {
00211 int sock;
00212 char buffer[4096];
00213 char request[16];
00214 int len;
00215
00216 sock = connect_to_server(config.socket);
00217
00218 strncpy(request, "stop\r\n\r\n", 15);
00219
00220 len = send_request(sock, request);
00221
00222 while ((len = read(sock, buffer, sizeof(buffer))) > 0) {
00223 buffer[len] = '\0';
00224 printf("%s", buffer);
00225 }
00226
00227 shutdown(sock, 2);
00228 close(sock);
00229 }
00230
00231 void
00232 wdctl_reset(void)
00233 {
00234 int sock;
00235 char buffer[4096];
00236 char request[64];
00237 int len,
00238 rlen;
00239
00240 sock = connect_to_server(config.socket);
00241
00242 strncpy(request, "reset ", 64);
00243 strncat(request, config.param, (64 - strlen(request)));
00244 strncat(request, "\r\n\r\n", (64 - strlen(request)));
00245
00246 len = send_request(sock, request);
00247
00248 len = 0;
00249 memset(buffer, 0, sizeof(buffer));
00250 while ((len < sizeof(buffer)) && ((rlen = read(sock, (buffer + len),
00251 (sizeof(buffer) - len))) > 0)){
00252 len += rlen;
00253 }
00254
00255 if (strcmp(buffer, "Yes") == 0) {
00256 printf("Connection %s successfully reset.\n", config.param);
00257 } else if (strcmp(buffer, "No") == 0) {
00258 printf("Connection %s was not active.\n", config.param);
00259 } else {
00260 fprintf(stderr, "wdctl: Error: WiFiDog sent an abnormal "
00261 "reply.\n");
00262 }
00263
00264 shutdown(sock, 2);
00265 close(sock);
00266 }
00267
00268 int
00269 main(int argc, char **argv)
00270 {
00271
00272
00273 init_config();
00274 parse_commandline(argc, argv);
00275
00276 switch(config.command) {
00277 case WDCTL_STATUS:
00278 wdctl_status();
00279 break;
00280
00281 case WDCTL_STOP:
00282 wdctl_stop();
00283 break;
00284
00285 case WDCTL_KILL:
00286 wdctl_reset();
00287 break;
00288
00289 default:
00290
00291 fprintf(stderr, "Oops\n");
00292 exit(1);
00293 break;
00294 }
00295 exit(0);
00296 }